1*11be35a1SLionel Sambuc //
2*11be35a1SLionel Sambuc // Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc // All rights reserved.
6*11be35a1SLionel Sambuc //
7*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc // are met:
10*11be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc //
16*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc //
29*11be35a1SLionel Sambuc
30*11be35a1SLionel Sambuc extern "C" {
31*11be35a1SLionel Sambuc #include <sys/stat.h>
32*11be35a1SLionel Sambuc #include <sys/wait.h>
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <fcntl.h>
35*11be35a1SLionel Sambuc #include <unistd.h>
36*11be35a1SLionel Sambuc }
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #include <cstdlib>
39*11be35a1SLionel Sambuc #include <iostream>
40*11be35a1SLionel Sambuc #include <set>
41*11be35a1SLionel Sambuc #include <string>
42*11be35a1SLionel Sambuc #include <vector>
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc #include "macros.hpp"
45*11be35a1SLionel Sambuc #include "utils.hpp"
46*11be35a1SLionel Sambuc
47*11be35a1SLionel Sambuc #include "detail/test_helpers.hpp"
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc static std::string
read_file(const char * path)50*11be35a1SLionel Sambuc read_file(const char *path)
51*11be35a1SLionel Sambuc {
52*11be35a1SLionel Sambuc char buffer[1024];
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc const int fd = open(path, O_RDONLY);
55*11be35a1SLionel Sambuc if (fd == -1)
56*11be35a1SLionel Sambuc ATF_FAIL("Cannot open " + std::string(path));
57*11be35a1SLionel Sambuc const ssize_t length = read(fd, buffer, sizeof(buffer) - 1);
58*11be35a1SLionel Sambuc close(fd);
59*11be35a1SLionel Sambuc ATF_REQUIRE(length != -1);
60*11be35a1SLionel Sambuc if (length == sizeof(buffer) - 1)
61*11be35a1SLionel Sambuc ATF_FAIL("Internal buffer not long enough to read temporary file");
62*11be35a1SLionel Sambuc ((char *)buffer)[length] = '\0';
63*11be35a1SLionel Sambuc
64*11be35a1SLionel Sambuc return buffer;
65*11be35a1SLionel Sambuc }
66*11be35a1SLionel Sambuc
67*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
68*11be35a1SLionel Sambuc // Tests cases for the free functions.
69*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(cat_file__empty);
ATF_TEST_CASE_BODY(cat_file__empty)72*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(cat_file__empty)
73*11be35a1SLionel Sambuc {
74*11be35a1SLionel Sambuc atf::utils::create_file("file.txt", "");
75*11be35a1SLionel Sambuc atf::utils::redirect(STDOUT_FILENO, "captured.txt");
76*11be35a1SLionel Sambuc atf::utils::cat_file("file.txt", "PREFIX");
77*11be35a1SLionel Sambuc std::cout.flush();
78*11be35a1SLionel Sambuc close(STDOUT_FILENO);
79*11be35a1SLionel Sambuc
80*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("", read_file("captured.txt"));
81*11be35a1SLionel Sambuc }
82*11be35a1SLionel Sambuc
83*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(cat_file__one_line);
ATF_TEST_CASE_BODY(cat_file__one_line)84*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(cat_file__one_line)
85*11be35a1SLionel Sambuc {
86*11be35a1SLionel Sambuc atf::utils::create_file("file.txt", "This is a single line\n");
87*11be35a1SLionel Sambuc atf::utils::redirect(STDOUT_FILENO, "captured.txt");
88*11be35a1SLionel Sambuc atf::utils::cat_file("file.txt", "PREFIX");
89*11be35a1SLionel Sambuc std::cout.flush();
90*11be35a1SLionel Sambuc close(STDOUT_FILENO);
91*11be35a1SLionel Sambuc
92*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("PREFIXThis is a single line\n", read_file("captured.txt"));
93*11be35a1SLionel Sambuc }
94*11be35a1SLionel Sambuc
95*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(cat_file__several_lines);
ATF_TEST_CASE_BODY(cat_file__several_lines)96*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(cat_file__several_lines)
97*11be35a1SLionel Sambuc {
98*11be35a1SLionel Sambuc atf::utils::create_file("file.txt", "First\nSecond line\nAnd third\n");
99*11be35a1SLionel Sambuc atf::utils::redirect(STDOUT_FILENO, "captured.txt");
100*11be35a1SLionel Sambuc atf::utils::cat_file("file.txt", ">");
101*11be35a1SLionel Sambuc std::cout.flush();
102*11be35a1SLionel Sambuc close(STDOUT_FILENO);
103*11be35a1SLionel Sambuc
104*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(">First\n>Second line\n>And third\n",
105*11be35a1SLionel Sambuc read_file("captured.txt"));
106*11be35a1SLionel Sambuc }
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(cat_file__no_newline_eof);
ATF_TEST_CASE_BODY(cat_file__no_newline_eof)109*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(cat_file__no_newline_eof)
110*11be35a1SLionel Sambuc {
111*11be35a1SLionel Sambuc atf::utils::create_file("file.txt", "Foo\n bar baz");
112*11be35a1SLionel Sambuc atf::utils::redirect(STDOUT_FILENO, "captured.txt");
113*11be35a1SLionel Sambuc atf::utils::cat_file("file.txt", "PREFIX");
114*11be35a1SLionel Sambuc std::cout.flush();
115*11be35a1SLionel Sambuc close(STDOUT_FILENO);
116*11be35a1SLionel Sambuc
117*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("PREFIXFoo\nPREFIX bar baz", read_file("captured.txt"));
118*11be35a1SLionel Sambuc }
119*11be35a1SLionel Sambuc
120*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__match);
ATF_TEST_CASE_BODY(compare_file__empty__match)121*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(compare_file__empty__match)
122*11be35a1SLionel Sambuc {
123*11be35a1SLionel Sambuc atf::utils::create_file("test.txt", "");
124*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::compare_file("test.txt", ""));
125*11be35a1SLionel Sambuc }
126*11be35a1SLionel Sambuc
127*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__not_match);
ATF_TEST_CASE_BODY(compare_file__empty__not_match)128*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(compare_file__empty__not_match)
129*11be35a1SLionel Sambuc {
130*11be35a1SLionel Sambuc atf::utils::create_file("test.txt", "");
131*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
132*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", "foo"));
133*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", " "));
134*11be35a1SLionel Sambuc }
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__match);
ATF_TEST_CASE_BODY(compare_file__short__match)137*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(compare_file__short__match)
138*11be35a1SLionel Sambuc {
139*11be35a1SLionel Sambuc atf::utils::create_file("test.txt", "this is a short file");
140*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::compare_file("test.txt", "this is a short file"));
141*11be35a1SLionel Sambuc }
142*11be35a1SLionel Sambuc
143*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__not_match);
ATF_TEST_CASE_BODY(compare_file__short__not_match)144*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(compare_file__short__not_match)
145*11be35a1SLionel Sambuc {
146*11be35a1SLionel Sambuc atf::utils::create_file("test.txt", "this is a short file");
147*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", ""));
148*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
149*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a Short file"));
150*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a short fil"));
151*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a short file "));
152*11be35a1SLionel Sambuc }
153*11be35a1SLionel Sambuc
154*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__match);
ATF_TEST_CASE_BODY(compare_file__long__match)155*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(compare_file__long__match)
156*11be35a1SLionel Sambuc {
157*11be35a1SLionel Sambuc char long_contents[3456];
158*11be35a1SLionel Sambuc size_t i = 0;
159*11be35a1SLionel Sambuc for (; i < sizeof(long_contents) - 1; i++)
160*11be35a1SLionel Sambuc long_contents[i] = '0' + (i % 10);
161*11be35a1SLionel Sambuc long_contents[i] = '\0';
162*11be35a1SLionel Sambuc atf::utils::create_file("test.txt", long_contents);
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::compare_file("test.txt", long_contents));
165*11be35a1SLionel Sambuc }
166*11be35a1SLionel Sambuc
167*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__not_match);
ATF_TEST_CASE_BODY(compare_file__long__not_match)168*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(compare_file__long__not_match)
169*11be35a1SLionel Sambuc {
170*11be35a1SLionel Sambuc char long_contents[3456];
171*11be35a1SLionel Sambuc size_t i = 0;
172*11be35a1SLionel Sambuc for (; i < sizeof(long_contents) - 1; i++)
173*11be35a1SLionel Sambuc long_contents[i] = '0' + (i % 10);
174*11be35a1SLionel Sambuc long_contents[i] = '\0';
175*11be35a1SLionel Sambuc atf::utils::create_file("test.txt", long_contents);
176*11be35a1SLionel Sambuc
177*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", ""));
178*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
179*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", "0123456789"));
180*11be35a1SLionel Sambuc long_contents[i - 1] = 'Z';
181*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::compare_file("test.txt", long_contents));
182*11be35a1SLionel Sambuc }
183*11be35a1SLionel Sambuc
184*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(copy_file__empty);
ATF_TEST_CASE_BODY(copy_file__empty)185*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(copy_file__empty)
186*11be35a1SLionel Sambuc {
187*11be35a1SLionel Sambuc atf::utils::create_file("src.txt", "");
188*11be35a1SLionel Sambuc ATF_REQUIRE(chmod("src.txt", 0520) != -1);
189*11be35a1SLionel Sambuc
190*11be35a1SLionel Sambuc atf::utils::copy_file("src.txt", "dest.txt");
191*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::compare_file("dest.txt", ""));
192*11be35a1SLionel Sambuc struct stat sb;
193*11be35a1SLionel Sambuc ATF_REQUIRE(stat("dest.txt", &sb) != -1);
194*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0520, sb.st_mode & 0xfff);
195*11be35a1SLionel Sambuc }
196*11be35a1SLionel Sambuc
197*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(copy_file__some_contents);
ATF_TEST_CASE_BODY(copy_file__some_contents)198*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(copy_file__some_contents)
199*11be35a1SLionel Sambuc {
200*11be35a1SLionel Sambuc atf::utils::create_file("src.txt", "This is a\ntest file\n");
201*11be35a1SLionel Sambuc atf::utils::copy_file("src.txt", "dest.txt");
202*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::compare_file("dest.txt", "This is a\ntest file\n"));
203*11be35a1SLionel Sambuc }
204*11be35a1SLionel Sambuc
205*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(create_file);
ATF_TEST_CASE_BODY(create_file)206*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(create_file)
207*11be35a1SLionel Sambuc {
208*11be35a1SLionel Sambuc atf::utils::create_file("test.txt", "This is a %d test");
209*11be35a1SLionel Sambuc
210*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("This is a %d test", read_file("test.txt"));
211*11be35a1SLionel Sambuc }
212*11be35a1SLionel Sambuc
213*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(file_exists);
ATF_TEST_CASE_BODY(file_exists)214*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(file_exists)
215*11be35a1SLionel Sambuc {
216*11be35a1SLionel Sambuc atf::utils::create_file("test.txt", "foo");
217*11be35a1SLionel Sambuc
218*11be35a1SLionel Sambuc ATF_REQUIRE( atf::utils::file_exists("test.txt"));
219*11be35a1SLionel Sambuc ATF_REQUIRE( atf::utils::file_exists("./test.txt"));
220*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::file_exists("./test.tx"));
221*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::file_exists("test.txt2"));
222*11be35a1SLionel Sambuc }
223*11be35a1SLionel Sambuc
224*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(fork);
ATF_TEST_CASE_BODY(fork)225*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(fork)
226*11be35a1SLionel Sambuc {
227*11be35a1SLionel Sambuc std::cout << "Should not get into child\n";
228*11be35a1SLionel Sambuc std::cerr << "Should not get into child\n";
229*11be35a1SLionel Sambuc pid_t pid = atf::utils::fork();
230*11be35a1SLionel Sambuc if (pid == 0) {
231*11be35a1SLionel Sambuc std::cout << "Child stdout\n";
232*11be35a1SLionel Sambuc std::cerr << "Child stderr\n";
233*11be35a1SLionel Sambuc exit(EXIT_SUCCESS);
234*11be35a1SLionel Sambuc }
235*11be35a1SLionel Sambuc
236*11be35a1SLionel Sambuc int status;
237*11be35a1SLionel Sambuc ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
238*11be35a1SLionel Sambuc ATF_REQUIRE(WIFEXITED(status));
239*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
240*11be35a1SLionel Sambuc
241*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("Child stdout\n", read_file("atf_utils_fork_out.txt"));
242*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("Child stderr\n", read_file("atf_utils_fork_err.txt"));
243*11be35a1SLionel Sambuc }
244*11be35a1SLionel Sambuc
245*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(grep_collection__set);
ATF_TEST_CASE_BODY(grep_collection__set)246*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(grep_collection__set)
247*11be35a1SLionel Sambuc {
248*11be35a1SLionel Sambuc std::set< std::string > strings;
249*11be35a1SLionel Sambuc strings.insert("First");
250*11be35a1SLionel Sambuc strings.insert("Second");
251*11be35a1SLionel Sambuc
252*11be35a1SLionel Sambuc ATF_REQUIRE( atf::utils::grep_collection("irs", strings));
253*11be35a1SLionel Sambuc ATF_REQUIRE( atf::utils::grep_collection("cond", strings));
254*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::grep_collection("Third", strings));
255*11be35a1SLionel Sambuc }
256*11be35a1SLionel Sambuc
257*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(grep_collection__vector);
ATF_TEST_CASE_BODY(grep_collection__vector)258*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(grep_collection__vector)
259*11be35a1SLionel Sambuc {
260*11be35a1SLionel Sambuc std::vector< std::string > strings;
261*11be35a1SLionel Sambuc strings.push_back("First");
262*11be35a1SLionel Sambuc strings.push_back("Second");
263*11be35a1SLionel Sambuc
264*11be35a1SLionel Sambuc ATF_REQUIRE( atf::utils::grep_collection("irs", strings));
265*11be35a1SLionel Sambuc ATF_REQUIRE( atf::utils::grep_collection("cond", strings));
266*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::grep_collection("Third", strings));
267*11be35a1SLionel Sambuc }
268*11be35a1SLionel Sambuc
269*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(grep_file);
ATF_TEST_CASE_BODY(grep_file)270*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(grep_file)
271*11be35a1SLionel Sambuc {
272*11be35a1SLionel Sambuc atf::utils::create_file("test.txt", "line1\nthe second line\naaaabbbb\n");
273*11be35a1SLionel Sambuc
274*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("line1", "test.txt"));
275*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("second line", "test.txt"));
276*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("aa.*bb", "test.txt"));
277*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::grep_file("foo", "test.txt"));
278*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::grep_file("bar", "test.txt"));
279*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::grep_file("aaaaa", "test.txt"));
280*11be35a1SLionel Sambuc }
281*11be35a1SLionel Sambuc
282*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(grep_string);
ATF_TEST_CASE_BODY(grep_string)283*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(grep_string)
284*11be35a1SLionel Sambuc {
285*11be35a1SLionel Sambuc const char *str = "a string - aaaabbbb";
286*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_string("a string", str));
287*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_string("^a string", str));
288*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_string("aaaabbbb$", str));
289*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_string("aa.*bb", str));
290*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::grep_string("foo", str));
291*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::grep_string("bar", str));
292*11be35a1SLionel Sambuc ATF_REQUIRE(!atf::utils::grep_string("aaaaa", str));
293*11be35a1SLionel Sambuc }
294*11be35a1SLionel Sambuc
295*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(redirect__stdout);
ATF_TEST_CASE_BODY(redirect__stdout)296*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(redirect__stdout)
297*11be35a1SLionel Sambuc {
298*11be35a1SLionel Sambuc std::cout << "Buffer this";
299*11be35a1SLionel Sambuc atf::utils::redirect(STDOUT_FILENO, "captured.txt");
300*11be35a1SLionel Sambuc std::cout << "The printed message";
301*11be35a1SLionel Sambuc std::cout.flush();
302*11be35a1SLionel Sambuc
303*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("The printed message", read_file("captured.txt"));
304*11be35a1SLionel Sambuc }
305*11be35a1SLionel Sambuc
306*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(redirect__stderr);
ATF_TEST_CASE_BODY(redirect__stderr)307*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(redirect__stderr)
308*11be35a1SLionel Sambuc {
309*11be35a1SLionel Sambuc std::cerr << "Buffer this";
310*11be35a1SLionel Sambuc atf::utils::redirect(STDERR_FILENO, "captured.txt");
311*11be35a1SLionel Sambuc std::cerr << "The printed message";
312*11be35a1SLionel Sambuc std::cerr.flush();
313*11be35a1SLionel Sambuc
314*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("The printed message", read_file("captured.txt"));
315*11be35a1SLionel Sambuc }
316*11be35a1SLionel Sambuc
317*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(redirect__other);
ATF_TEST_CASE_BODY(redirect__other)318*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(redirect__other)
319*11be35a1SLionel Sambuc {
320*11be35a1SLionel Sambuc const std::string message = "Foo bar\nbaz\n";
321*11be35a1SLionel Sambuc atf::utils::redirect(15, "captured.txt");
322*11be35a1SLionel Sambuc ATF_REQUIRE(write(15, message.c_str(), message.length()) != -1);
323*11be35a1SLionel Sambuc close(15);
324*11be35a1SLionel Sambuc
325*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(message, read_file("captured.txt"));
326*11be35a1SLionel Sambuc }
327*11be35a1SLionel Sambuc
328*11be35a1SLionel Sambuc static void
fork_and_wait(const int exitstatus,const char * expout,const char * experr)329*11be35a1SLionel Sambuc fork_and_wait(const int exitstatus, const char* expout, const char* experr)
330*11be35a1SLionel Sambuc {
331*11be35a1SLionel Sambuc const pid_t pid = atf::utils::fork();
332*11be35a1SLionel Sambuc if (pid == 0) {
333*11be35a1SLionel Sambuc std::cout << "Some output\n";
334*11be35a1SLionel Sambuc std::cerr << "Some error\n";
335*11be35a1SLionel Sambuc exit(123);
336*11be35a1SLionel Sambuc }
337*11be35a1SLionel Sambuc atf::utils::wait(pid, exitstatus, expout, experr);
338*11be35a1SLionel Sambuc exit(EXIT_SUCCESS);
339*11be35a1SLionel Sambuc }
340*11be35a1SLionel Sambuc
341*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(wait__ok);
ATF_TEST_CASE_BODY(wait__ok)342*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(wait__ok)
343*11be35a1SLionel Sambuc {
344*11be35a1SLionel Sambuc const pid_t control = fork();
345*11be35a1SLionel Sambuc ATF_REQUIRE(control != -1);
346*11be35a1SLionel Sambuc if (control == 0)
347*11be35a1SLionel Sambuc fork_and_wait(123, "Some output\n", "Some error\n");
348*11be35a1SLionel Sambuc else {
349*11be35a1SLionel Sambuc int status;
350*11be35a1SLionel Sambuc ATF_REQUIRE(waitpid(control, &status, 0) != -1);
351*11be35a1SLionel Sambuc ATF_REQUIRE(WIFEXITED(status));
352*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
353*11be35a1SLionel Sambuc }
354*11be35a1SLionel Sambuc }
355*11be35a1SLionel Sambuc
356*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_exitstatus);
ATF_TEST_CASE_BODY(wait__invalid_exitstatus)357*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(wait__invalid_exitstatus)
358*11be35a1SLionel Sambuc {
359*11be35a1SLionel Sambuc const pid_t control = fork();
360*11be35a1SLionel Sambuc ATF_REQUIRE(control != -1);
361*11be35a1SLionel Sambuc if (control == 0)
362*11be35a1SLionel Sambuc fork_and_wait(120, "Some output\n", "Some error\n");
363*11be35a1SLionel Sambuc else {
364*11be35a1SLionel Sambuc int status;
365*11be35a1SLionel Sambuc ATF_REQUIRE(waitpid(control, &status, 0) != -1);
366*11be35a1SLionel Sambuc ATF_REQUIRE(WIFEXITED(status));
367*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
368*11be35a1SLionel Sambuc }
369*11be35a1SLionel Sambuc }
370*11be35a1SLionel Sambuc
371*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_stdout);
ATF_TEST_CASE_BODY(wait__invalid_stdout)372*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(wait__invalid_stdout)
373*11be35a1SLionel Sambuc {
374*11be35a1SLionel Sambuc const pid_t control = fork();
375*11be35a1SLionel Sambuc ATF_REQUIRE(control != -1);
376*11be35a1SLionel Sambuc if (control == 0)
377*11be35a1SLionel Sambuc fork_and_wait(123, "Some output foo\n", "Some error\n");
378*11be35a1SLionel Sambuc else {
379*11be35a1SLionel Sambuc int status;
380*11be35a1SLionel Sambuc ATF_REQUIRE(waitpid(control, &status, 0) != -1);
381*11be35a1SLionel Sambuc ATF_REQUIRE(WIFEXITED(status));
382*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
383*11be35a1SLionel Sambuc }
384*11be35a1SLionel Sambuc }
385*11be35a1SLionel Sambuc
386*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_stderr);
ATF_TEST_CASE_BODY(wait__invalid_stderr)387*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(wait__invalid_stderr)
388*11be35a1SLionel Sambuc {
389*11be35a1SLionel Sambuc const pid_t control = fork();
390*11be35a1SLionel Sambuc ATF_REQUIRE(control != -1);
391*11be35a1SLionel Sambuc if (control == 0)
392*11be35a1SLionel Sambuc fork_and_wait(123, "Some output\n", "Some error foo\n");
393*11be35a1SLionel Sambuc else {
394*11be35a1SLionel Sambuc int status;
395*11be35a1SLionel Sambuc ATF_REQUIRE(waitpid(control, &status, 0) != -1);
396*11be35a1SLionel Sambuc ATF_REQUIRE(WIFEXITED(status));
397*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
398*11be35a1SLionel Sambuc }
399*11be35a1SLionel Sambuc }
400*11be35a1SLionel Sambuc
401*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(wait__save_stdout);
ATF_TEST_CASE_BODY(wait__save_stdout)402*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(wait__save_stdout)
403*11be35a1SLionel Sambuc {
404*11be35a1SLionel Sambuc const pid_t control = fork();
405*11be35a1SLionel Sambuc ATF_REQUIRE(control != -1);
406*11be35a1SLionel Sambuc if (control == 0)
407*11be35a1SLionel Sambuc fork_and_wait(123, "save:my-output.txt", "Some error\n");
408*11be35a1SLionel Sambuc else {
409*11be35a1SLionel Sambuc int status;
410*11be35a1SLionel Sambuc ATF_REQUIRE(waitpid(control, &status, 0) != -1);
411*11be35a1SLionel Sambuc ATF_REQUIRE(WIFEXITED(status));
412*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
413*11be35a1SLionel Sambuc
414*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::compare_file("my-output.txt", "Some output\n"));
415*11be35a1SLionel Sambuc }
416*11be35a1SLionel Sambuc }
417*11be35a1SLionel Sambuc
418*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(wait__save_stderr);
ATF_TEST_CASE_BODY(wait__save_stderr)419*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(wait__save_stderr)
420*11be35a1SLionel Sambuc {
421*11be35a1SLionel Sambuc const pid_t control = fork();
422*11be35a1SLionel Sambuc ATF_REQUIRE(control != -1);
423*11be35a1SLionel Sambuc if (control == 0)
424*11be35a1SLionel Sambuc fork_and_wait(123, "Some output\n", "save:my-output.txt");
425*11be35a1SLionel Sambuc else {
426*11be35a1SLionel Sambuc int status;
427*11be35a1SLionel Sambuc ATF_REQUIRE(waitpid(control, &status, 0) != -1);
428*11be35a1SLionel Sambuc ATF_REQUIRE(WIFEXITED(status));
429*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
430*11be35a1SLionel Sambuc
431*11be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::compare_file("my-output.txt", "Some error\n"));
432*11be35a1SLionel Sambuc }
433*11be35a1SLionel Sambuc }
434*11be35a1SLionel Sambuc
435*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
436*11be35a1SLionel Sambuc // Tests cases for the header file.
437*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
438*11be35a1SLionel Sambuc
439*11be35a1SLionel Sambuc HEADER_TC(include, "atf-c++/utils.hpp");
440*11be35a1SLionel Sambuc
441*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
442*11be35a1SLionel Sambuc // Main.
443*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
444*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)445*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
446*11be35a1SLionel Sambuc {
447*11be35a1SLionel Sambuc // Add the test for the free functions.
448*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, cat_file__empty);
449*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, cat_file__one_line);
450*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, cat_file__several_lines);
451*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, cat_file__no_newline_eof);
452*11be35a1SLionel Sambuc
453*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, compare_file__empty__match);
454*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, compare_file__empty__not_match);
455*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, compare_file__short__match);
456*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, compare_file__short__not_match);
457*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, compare_file__long__match);
458*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, compare_file__long__not_match);
459*11be35a1SLionel Sambuc
460*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, copy_file__empty);
461*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, copy_file__some_contents);
462*11be35a1SLionel Sambuc
463*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, create_file);
464*11be35a1SLionel Sambuc
465*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, file_exists);
466*11be35a1SLionel Sambuc
467*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, fork);
468*11be35a1SLionel Sambuc
469*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, grep_collection__set);
470*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, grep_collection__vector);
471*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, grep_file);
472*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, grep_string);
473*11be35a1SLionel Sambuc
474*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, redirect__stdout);
475*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, redirect__stderr);
476*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, redirect__other);
477*11be35a1SLionel Sambuc
478*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, wait__ok);
479*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, wait__invalid_exitstatus);
480*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, wait__invalid_stdout);
481*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, wait__invalid_stderr);
482*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, wait__save_stdout);
483*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, wait__save_stderr);
484*11be35a1SLionel Sambuc
485*11be35a1SLionel Sambuc // Add the test cases for the header file.
486*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, include);
487*11be35a1SLionel Sambuc }
488