2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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.
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.
47 #include "detail/test_helpers.hpp"
50 read_file(const char *path
)
54 const int fd
= open(path
, O_RDONLY
);
56 ATF_FAIL("Cannot open " + std::string(path
));
57 const ssize_t length
= read(fd
, buffer
, sizeof(buffer
) - 1);
59 ATF_REQUIRE(length
!= -1);
60 if (length
== sizeof(buffer
) - 1)
61 ATF_FAIL("Internal buffer not long enough to read temporary file");
62 ((char *)buffer
)[length
] = '\0';
67 // ------------------------------------------------------------------------
68 // Tests cases for the free functions.
69 // ------------------------------------------------------------------------
71 ATF_TEST_CASE_WITHOUT_HEAD(cat_file__empty
);
72 ATF_TEST_CASE_BODY(cat_file__empty
)
74 atf::utils::create_file("file.txt", "");
75 atf::utils::redirect(STDOUT_FILENO
, "captured.txt");
76 atf::utils::cat_file("file.txt", "PREFIX");
80 ATF_REQUIRE_EQ("", read_file("captured.txt"));
83 ATF_TEST_CASE_WITHOUT_HEAD(cat_file__one_line
);
84 ATF_TEST_CASE_BODY(cat_file__one_line
)
86 atf::utils::create_file("file.txt", "This is a single line\n");
87 atf::utils::redirect(STDOUT_FILENO
, "captured.txt");
88 atf::utils::cat_file("file.txt", "PREFIX");
92 ATF_REQUIRE_EQ("PREFIXThis is a single line\n", read_file("captured.txt"));
95 ATF_TEST_CASE_WITHOUT_HEAD(cat_file__several_lines
);
96 ATF_TEST_CASE_BODY(cat_file__several_lines
)
98 atf::utils::create_file("file.txt", "First\nSecond line\nAnd third\n");
99 atf::utils::redirect(STDOUT_FILENO
, "captured.txt");
100 atf::utils::cat_file("file.txt", ">");
102 close(STDOUT_FILENO
);
104 ATF_REQUIRE_EQ(">First\n>Second line\n>And third\n",
105 read_file("captured.txt"));
108 ATF_TEST_CASE_WITHOUT_HEAD(cat_file__no_newline_eof
);
109 ATF_TEST_CASE_BODY(cat_file__no_newline_eof
)
111 atf::utils::create_file("file.txt", "Foo\n bar baz");
112 atf::utils::redirect(STDOUT_FILENO
, "captured.txt");
113 atf::utils::cat_file("file.txt", "PREFIX");
115 close(STDOUT_FILENO
);
117 ATF_REQUIRE_EQ("PREFIXFoo\nPREFIX bar baz", read_file("captured.txt"));
120 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__match
);
121 ATF_TEST_CASE_BODY(compare_file__empty__match
)
123 atf::utils::create_file("test.txt", "");
124 ATF_REQUIRE(atf::utils::compare_file("test.txt", ""));
127 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__not_match
);
128 ATF_TEST_CASE_BODY(compare_file__empty__not_match
)
130 atf::utils::create_file("test.txt", "");
131 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
132 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "foo"));
133 ATF_REQUIRE(!atf::utils::compare_file("test.txt", " "));
136 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__match
);
137 ATF_TEST_CASE_BODY(compare_file__short__match
)
139 atf::utils::create_file("test.txt", "this is a short file");
140 ATF_REQUIRE(atf::utils::compare_file("test.txt", "this is a short file"));
143 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__not_match
);
144 ATF_TEST_CASE_BODY(compare_file__short__not_match
)
146 atf::utils::create_file("test.txt", "this is a short file");
147 ATF_REQUIRE(!atf::utils::compare_file("test.txt", ""));
148 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
149 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a Short file"));
150 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a short fil"));
151 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a short file "));
154 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__match
);
155 ATF_TEST_CASE_BODY(compare_file__long__match
)
157 char long_contents
[3456];
159 for (; i
< sizeof(long_contents
) - 1; i
++)
160 long_contents
[i
] = '0' + (i
% 10);
161 long_contents
[i
] = '\0';
162 atf::utils::create_file("test.txt", long_contents
);
164 ATF_REQUIRE(atf::utils::compare_file("test.txt", long_contents
));
167 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__not_match
);
168 ATF_TEST_CASE_BODY(compare_file__long__not_match
)
170 char long_contents
[3456];
172 for (; i
< sizeof(long_contents
) - 1; i
++)
173 long_contents
[i
] = '0' + (i
% 10);
174 long_contents
[i
] = '\0';
175 atf::utils::create_file("test.txt", long_contents
);
177 ATF_REQUIRE(!atf::utils::compare_file("test.txt", ""));
178 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
179 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "0123456789"));
180 long_contents
[i
- 1] = 'Z';
181 ATF_REQUIRE(!atf::utils::compare_file("test.txt", long_contents
));
184 ATF_TEST_CASE_WITHOUT_HEAD(copy_file__empty
);
185 ATF_TEST_CASE_BODY(copy_file__empty
)
187 atf::utils::create_file("src.txt", "");
188 ATF_REQUIRE(chmod("src.txt", 0520) != -1);
190 atf::utils::copy_file("src.txt", "dest.txt");
191 ATF_REQUIRE(atf::utils::compare_file("dest.txt", ""));
193 ATF_REQUIRE(stat("dest.txt", &sb
) != -1);
194 ATF_REQUIRE_EQ(0520, sb
.st_mode
& 0xfff);
197 ATF_TEST_CASE_WITHOUT_HEAD(copy_file__some_contents
);
198 ATF_TEST_CASE_BODY(copy_file__some_contents
)
200 atf::utils::create_file("src.txt", "This is a\ntest file\n");
201 atf::utils::copy_file("src.txt", "dest.txt");
202 ATF_REQUIRE(atf::utils::compare_file("dest.txt", "This is a\ntest file\n"));
205 ATF_TEST_CASE_WITHOUT_HEAD(create_file
);
206 ATF_TEST_CASE_BODY(create_file
)
208 atf::utils::create_file("test.txt", "This is a %d test");
210 ATF_REQUIRE_EQ("This is a %d test", read_file("test.txt"));
213 ATF_TEST_CASE_WITHOUT_HEAD(file_exists
);
214 ATF_TEST_CASE_BODY(file_exists
)
216 atf::utils::create_file("test.txt", "foo");
218 ATF_REQUIRE( atf::utils::file_exists("test.txt"));
219 ATF_REQUIRE( atf::utils::file_exists("./test.txt"));
220 ATF_REQUIRE(!atf::utils::file_exists("./test.tx"));
221 ATF_REQUIRE(!atf::utils::file_exists("test.txt2"));
224 ATF_TEST_CASE_WITHOUT_HEAD(fork
);
225 ATF_TEST_CASE_BODY(fork
)
227 std::cout
<< "Should not get into child\n";
228 std::cerr
<< "Should not get into child\n";
229 pid_t pid
= atf::utils::fork();
231 std::cout
<< "Child stdout\n";
232 std::cerr
<< "Child stderr\n";
237 ATF_REQUIRE(waitpid(pid
, &status
, 0) != -1);
238 ATF_REQUIRE(WIFEXITED(status
));
239 ATF_REQUIRE_EQ(EXIT_SUCCESS
, WEXITSTATUS(status
));
241 ATF_REQUIRE_EQ("Child stdout\n", read_file("atf_utils_fork_out.txt"));
242 ATF_REQUIRE_EQ("Child stderr\n", read_file("atf_utils_fork_err.txt"));
245 ATF_TEST_CASE_WITHOUT_HEAD(grep_collection__set
);
246 ATF_TEST_CASE_BODY(grep_collection__set
)
248 std::set
< std::string
> strings
;
249 strings
.insert("First");
250 strings
.insert("Second");
252 ATF_REQUIRE( atf::utils::grep_collection("irs", strings
));
253 ATF_REQUIRE( atf::utils::grep_collection("cond", strings
));
254 ATF_REQUIRE(!atf::utils::grep_collection("Third", strings
));
257 ATF_TEST_CASE_WITHOUT_HEAD(grep_collection__vector
);
258 ATF_TEST_CASE_BODY(grep_collection__vector
)
260 std::vector
< std::string
> strings
;
261 strings
.push_back("First");
262 strings
.push_back("Second");
264 ATF_REQUIRE( atf::utils::grep_collection("irs", strings
));
265 ATF_REQUIRE( atf::utils::grep_collection("cond", strings
));
266 ATF_REQUIRE(!atf::utils::grep_collection("Third", strings
));
269 ATF_TEST_CASE_WITHOUT_HEAD(grep_file
);
270 ATF_TEST_CASE_BODY(grep_file
)
272 atf::utils::create_file("test.txt", "line1\nthe second line\naaaabbbb\n");
274 ATF_REQUIRE(atf::utils::grep_file("line1", "test.txt"));
275 ATF_REQUIRE(atf::utils::grep_file("second line", "test.txt"));
276 ATF_REQUIRE(atf::utils::grep_file("aa.*bb", "test.txt"));
277 ATF_REQUIRE(!atf::utils::grep_file("foo", "test.txt"));
278 ATF_REQUIRE(!atf::utils::grep_file("bar", "test.txt"));
279 ATF_REQUIRE(!atf::utils::grep_file("aaaaa", "test.txt"));
282 ATF_TEST_CASE_WITHOUT_HEAD(grep_string
);
283 ATF_TEST_CASE_BODY(grep_string
)
285 const char *str
= "a string - aaaabbbb";
286 ATF_REQUIRE(atf::utils::grep_string("a string", str
));
287 ATF_REQUIRE(atf::utils::grep_string("^a string", str
));
288 ATF_REQUIRE(atf::utils::grep_string("aaaabbbb$", str
));
289 ATF_REQUIRE(atf::utils::grep_string("aa.*bb", str
));
290 ATF_REQUIRE(!atf::utils::grep_string("foo", str
));
291 ATF_REQUIRE(!atf::utils::grep_string("bar", str
));
292 ATF_REQUIRE(!atf::utils::grep_string("aaaaa", str
));
295 ATF_TEST_CASE_WITHOUT_HEAD(redirect__stdout
);
296 ATF_TEST_CASE_BODY(redirect__stdout
)
298 std::cout
<< "Buffer this";
299 atf::utils::redirect(STDOUT_FILENO
, "captured.txt");
300 std::cout
<< "The printed message";
303 ATF_REQUIRE_EQ("The printed message", read_file("captured.txt"));
306 ATF_TEST_CASE_WITHOUT_HEAD(redirect__stderr
);
307 ATF_TEST_CASE_BODY(redirect__stderr
)
309 std::cerr
<< "Buffer this";
310 atf::utils::redirect(STDERR_FILENO
, "captured.txt");
311 std::cerr
<< "The printed message";
314 ATF_REQUIRE_EQ("The printed message", read_file("captured.txt"));
317 ATF_TEST_CASE_WITHOUT_HEAD(redirect__other
);
318 ATF_TEST_CASE_BODY(redirect__other
)
320 const std::string message
= "Foo bar\nbaz\n";
321 atf::utils::redirect(15, "captured.txt");
322 ATF_REQUIRE(write(15, message
.c_str(), message
.length()) != -1);
325 ATF_REQUIRE_EQ(message
, read_file("captured.txt"));
329 fork_and_wait(const int exitstatus
, const char* expout
, const char* experr
)
331 const pid_t pid
= atf::utils::fork();
333 std::cout
<< "Some output\n";
334 std::cerr
<< "Some error\n";
337 atf::utils::wait(pid
, exitstatus
, expout
, experr
);
341 ATF_TEST_CASE_WITHOUT_HEAD(wait__ok
);
342 ATF_TEST_CASE_BODY(wait__ok
)
344 const pid_t control
= fork();
345 ATF_REQUIRE(control
!= -1);
347 fork_and_wait(123, "Some output\n", "Some error\n");
350 ATF_REQUIRE(waitpid(control
, &status
, 0) != -1);
351 ATF_REQUIRE(WIFEXITED(status
));
352 ATF_REQUIRE_EQ(EXIT_SUCCESS
, WEXITSTATUS(status
));
356 ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_exitstatus
);
357 ATF_TEST_CASE_BODY(wait__invalid_exitstatus
)
359 const pid_t control
= fork();
360 ATF_REQUIRE(control
!= -1);
362 fork_and_wait(120, "Some output\n", "Some error\n");
365 ATF_REQUIRE(waitpid(control
, &status
, 0) != -1);
366 ATF_REQUIRE(WIFEXITED(status
));
367 ATF_REQUIRE_EQ(EXIT_FAILURE
, WEXITSTATUS(status
));
371 ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_stdout
);
372 ATF_TEST_CASE_BODY(wait__invalid_stdout
)
374 const pid_t control
= fork();
375 ATF_REQUIRE(control
!= -1);
377 fork_and_wait(123, "Some output foo\n", "Some error\n");
380 ATF_REQUIRE(waitpid(control
, &status
, 0) != -1);
381 ATF_REQUIRE(WIFEXITED(status
));
382 ATF_REQUIRE_EQ(EXIT_FAILURE
, WEXITSTATUS(status
));
386 ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_stderr
);
387 ATF_TEST_CASE_BODY(wait__invalid_stderr
)
389 const pid_t control
= fork();
390 ATF_REQUIRE(control
!= -1);
392 fork_and_wait(123, "Some output\n", "Some error foo\n");
395 ATF_REQUIRE(waitpid(control
, &status
, 0) != -1);
396 ATF_REQUIRE(WIFEXITED(status
));
397 ATF_REQUIRE_EQ(EXIT_FAILURE
, WEXITSTATUS(status
));
401 ATF_TEST_CASE_WITHOUT_HEAD(wait__save_stdout
);
402 ATF_TEST_CASE_BODY(wait__save_stdout
)
404 const pid_t control
= fork();
405 ATF_REQUIRE(control
!= -1);
407 fork_and_wait(123, "save:my-output.txt", "Some error\n");
410 ATF_REQUIRE(waitpid(control
, &status
, 0) != -1);
411 ATF_REQUIRE(WIFEXITED(status
));
412 ATF_REQUIRE_EQ(EXIT_SUCCESS
, WEXITSTATUS(status
));
414 ATF_REQUIRE(atf::utils::compare_file("my-output.txt", "Some output\n"));
418 ATF_TEST_CASE_WITHOUT_HEAD(wait__save_stderr
);
419 ATF_TEST_CASE_BODY(wait__save_stderr
)
421 const pid_t control
= fork();
422 ATF_REQUIRE(control
!= -1);
424 fork_and_wait(123, "Some output\n", "save:my-output.txt");
427 ATF_REQUIRE(waitpid(control
, &status
, 0) != -1);
428 ATF_REQUIRE(WIFEXITED(status
));
429 ATF_REQUIRE_EQ(EXIT_SUCCESS
, WEXITSTATUS(status
));
431 ATF_REQUIRE(atf::utils::compare_file("my-output.txt", "Some error\n"));
435 // ------------------------------------------------------------------------
436 // Tests cases for the header file.
437 // ------------------------------------------------------------------------
439 HEADER_TC(include
, "atf-c++/utils.hpp");
441 // ------------------------------------------------------------------------
443 // ------------------------------------------------------------------------
445 ATF_INIT_TEST_CASES(tcs
)
447 // Add the test for the free functions.
448 ATF_ADD_TEST_CASE(tcs
, cat_file__empty
);
449 ATF_ADD_TEST_CASE(tcs
, cat_file__one_line
);
450 ATF_ADD_TEST_CASE(tcs
, cat_file__several_lines
);
451 ATF_ADD_TEST_CASE(tcs
, cat_file__no_newline_eof
);
453 ATF_ADD_TEST_CASE(tcs
, compare_file__empty__match
);
454 ATF_ADD_TEST_CASE(tcs
, compare_file__empty__not_match
);
455 ATF_ADD_TEST_CASE(tcs
, compare_file__short__match
);
456 ATF_ADD_TEST_CASE(tcs
, compare_file__short__not_match
);
457 ATF_ADD_TEST_CASE(tcs
, compare_file__long__match
);
458 ATF_ADD_TEST_CASE(tcs
, compare_file__long__not_match
);
460 ATF_ADD_TEST_CASE(tcs
, copy_file__empty
);
461 ATF_ADD_TEST_CASE(tcs
, copy_file__some_contents
);
463 ATF_ADD_TEST_CASE(tcs
, create_file
);
465 ATF_ADD_TEST_CASE(tcs
, file_exists
);
467 ATF_ADD_TEST_CASE(tcs
, fork
);
469 ATF_ADD_TEST_CASE(tcs
, grep_collection__set
);
470 ATF_ADD_TEST_CASE(tcs
, grep_collection__vector
);
471 ATF_ADD_TEST_CASE(tcs
, grep_file
);
472 ATF_ADD_TEST_CASE(tcs
, grep_string
);
474 ATF_ADD_TEST_CASE(tcs
, redirect__stdout
);
475 ATF_ADD_TEST_CASE(tcs
, redirect__stderr
);
476 ATF_ADD_TEST_CASE(tcs
, redirect__other
);
478 ATF_ADD_TEST_CASE(tcs
, wait__ok
);
479 ATF_ADD_TEST_CASE(tcs
, wait__invalid_exitstatus
);
480 ATF_ADD_TEST_CASE(tcs
, wait__invalid_stdout
);
481 ATF_ADD_TEST_CASE(tcs
, wait__invalid_stderr
);
482 ATF_ADD_TEST_CASE(tcs
, wait__save_stdout
);
483 ATF_ADD_TEST_CASE(tcs
, wait__save_stderr
);
485 // Add the test cases for the header file.
486 ATF_ADD_TEST_CASE(tcs
, include
);