1//
2// Automated Testing Framework (atf)
3//
4// Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10// 1. Redistributions of source code must retain the above copyright
11//    notice, this list of conditions and the following disclaimer.
12// 2. Redistributions in binary form must reproduce the above copyright
13//    notice, this list of conditions and the following disclaimer in the
14//    documentation and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
30extern "C" {
31#include <regex.h>
32}
33
34#include <fstream>
35#include <iostream>
36#include <string>
37#include <vector>
38
39#include "../check.hpp"
40#include "../config.hpp"
41#include "../macros.hpp"
42
43#include "fs.hpp"
44#include "process.hpp"
45#include "test_helpers.hpp"
46
47void
48build_check_cxx_o_aux(const atf::fs::path& sfile, const char* failmsg)
49{
50    std::vector< std::string > optargs;
51    optargs.push_back("-I" + atf::config::get("atf_includedir"));
52
53    if (!atf::check::build_cxx_o(sfile.str(), "test.o",
54                                 atf::process::argv_array(optargs)))
55        ATF_FAIL(failmsg);
56}
57
58void
59build_check_cxx_o(const atf::tests::tc& tc, const char* sfile,
60                  const char* failmsg)
61{
62    const atf::fs::path sfilepath =
63        atf::fs::path(tc.get_config_var("srcdir")) / sfile;
64    build_check_cxx_o_aux(sfilepath, failmsg);
65}
66
67void
68header_check(const atf::tests::tc& tc, const char *hdrname)
69{
70    std::ofstream srcfile("test.c");
71    ATF_REQUIRE(srcfile);
72    srcfile << "#include <" << hdrname << ">\n";
73    srcfile.close();
74
75    const std::string failmsg = std::string("Header check failed; ") +
76        hdrname + " is not self-contained";
77    build_check_cxx_o_aux(atf::fs::path("test.c"), failmsg.c_str());
78}
79
80atf::fs::path
81get_process_helpers_path(const atf::tests::tc& tc)
82{
83    return atf::fs::path(tc.get_config_var("srcdir")) /
84           ".." / "atf-c" / "detail" / "process_helpers";
85}
86
87bool
88grep_file(const char* name, const char* regex)
89{
90    std::ifstream is(name);
91    ATF_REQUIRE(is);
92
93    bool found = false;
94
95    std::string line;
96    std::getline(is, line);
97    while (!found && is.good()) {
98        if (grep_string(line, regex))
99            found = true;
100        else
101            std::getline(is, line);
102    }
103
104    return found;
105}
106
107bool
108grep_string(const std::string& str, const char* regex)
109{
110    int res;
111    regex_t preg;
112
113    std::cout << "Looking for '" << regex << "' in '" << str << "'\n";
114    ATF_REQUIRE(::regcomp(&preg, regex, REG_EXTENDED) == 0);
115
116    res = ::regexec(&preg, str.c_str(), 0, NULL, 0);
117    ATF_REQUIRE(res == 0 || res == REG_NOMATCH);
118
119    ::regfree(&preg);
120
121    return res == 0;
122}
123
124void
125test_helpers_detail::check_equal(const char* expected[],
126                                 const string_vector& actual)
127{
128    const char** expected_iter = expected;
129    string_vector::const_iterator actual_iter = actual.begin();
130
131    bool equals = true;
132    while (equals && *expected_iter != NULL && actual_iter != actual.end()) {
133        if (*expected_iter != *actual_iter) {
134            equals = false;
135        } else {
136            expected_iter++;
137            actual_iter++;
138        }
139    }
140    if (equals && ((*expected_iter == NULL && actual_iter != actual.end()) ||
141                   (*expected_iter != NULL && actual_iter == actual.end())))
142        equals = false;
143
144    if (!equals) {
145        std::cerr << "EXPECTED:\n";
146        for (expected_iter = expected; *expected_iter != NULL; expected_iter++)
147            std::cerr << *expected_iter << "\n";
148
149        std::cerr << "ACTUAL:\n";
150        for (actual_iter = actual.begin(); actual_iter != actual.end();
151             actual_iter++)
152            std::cerr << *actual_iter << "\n";
153
154        ATF_FAIL("Expected results differ to actual values");
155    }
156}
157