1 // Copyright (c) 2007 The NetBSD Foundation, Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
7 // 1. Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
13 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "atf-c++/utils.hpp"
43 #include <atf-c++.hpp>
46 read_file(const std::string& path)
50 const int fd = open(path.c_str(), O_RDONLY);
52 ATF_FAIL("Cannot open " + path);
53 const ssize_t length = read(fd, buffer, sizeof(buffer) - 1);
55 ATF_REQUIRE(length != -1);
56 if (length == sizeof(buffer) - 1)
57 ATF_FAIL("Internal buffer not long enough to read temporary file");
58 ((char *)buffer)[length] = '\0';
63 // ------------------------------------------------------------------------
64 // Tests cases for the free functions.
65 // ------------------------------------------------------------------------
67 ATF_TEST_CASE_WITHOUT_HEAD(cat_file__empty);
68 ATF_TEST_CASE_BODY(cat_file__empty)
70 atf::utils::create_file("file.txt", "");
71 atf::utils::redirect(STDOUT_FILENO, "captured.txt");
72 atf::utils::cat_file("file.txt", "PREFIX");
76 ATF_REQUIRE_EQ("", read_file("captured.txt"));
79 ATF_TEST_CASE_WITHOUT_HEAD(cat_file__one_line);
80 ATF_TEST_CASE_BODY(cat_file__one_line)
82 atf::utils::create_file("file.txt", "This is a single line\n");
83 atf::utils::redirect(STDOUT_FILENO, "captured.txt");
84 atf::utils::cat_file("file.txt", "PREFIX");
88 ATF_REQUIRE_EQ("PREFIXThis is a single line\n", read_file("captured.txt"));
91 ATF_TEST_CASE_WITHOUT_HEAD(cat_file__several_lines);
92 ATF_TEST_CASE_BODY(cat_file__several_lines)
94 atf::utils::create_file("file.txt", "First\nSecond line\nAnd third\n");
95 atf::utils::redirect(STDOUT_FILENO, "captured.txt");
96 atf::utils::cat_file("file.txt", ">");
100 ATF_REQUIRE_EQ(">First\n>Second line\n>And third\n",
101 read_file("captured.txt"));
104 ATF_TEST_CASE_WITHOUT_HEAD(cat_file__no_newline_eof);
105 ATF_TEST_CASE_BODY(cat_file__no_newline_eof)
107 atf::utils::create_file("file.txt", "Foo\n bar baz");
108 atf::utils::redirect(STDOUT_FILENO, "captured.txt");
109 atf::utils::cat_file("file.txt", "PREFIX");
111 close(STDOUT_FILENO);
113 ATF_REQUIRE_EQ("PREFIXFoo\nPREFIX bar baz", read_file("captured.txt"));
116 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__match);
117 ATF_TEST_CASE_BODY(compare_file__empty__match)
119 atf::utils::create_file("test.txt", "");
120 ATF_REQUIRE(atf::utils::compare_file("test.txt", ""));
123 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__not_match);
124 ATF_TEST_CASE_BODY(compare_file__empty__not_match)
126 atf::utils::create_file("test.txt", "");
127 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
128 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "foo"));
129 ATF_REQUIRE(!atf::utils::compare_file("test.txt", " "));
132 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__match);
133 ATF_TEST_CASE_BODY(compare_file__short__match)
135 atf::utils::create_file("test.txt", "this is a short file");
136 ATF_REQUIRE(atf::utils::compare_file("test.txt", "this is a short file"));
139 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__not_match);
140 ATF_TEST_CASE_BODY(compare_file__short__not_match)
142 atf::utils::create_file("test.txt", "this is a short file");
143 ATF_REQUIRE(!atf::utils::compare_file("test.txt", ""));
144 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
145 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a Short file"));
146 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a short fil"));
147 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a short file "));
150 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__match);
151 ATF_TEST_CASE_BODY(compare_file__long__match)
153 char long_contents[3456];
155 for (; i < sizeof(long_contents) - 1; i++)
156 long_contents[i] = '0' + (i % 10);
157 long_contents[i] = '\0';
158 atf::utils::create_file("test.txt", long_contents);
160 ATF_REQUIRE(atf::utils::compare_file("test.txt", long_contents));
163 ATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__not_match);
164 ATF_TEST_CASE_BODY(compare_file__long__not_match)
166 char long_contents[3456];
168 for (; i < sizeof(long_contents) - 1; i++)
169 long_contents[i] = '0' + (i % 10);
170 long_contents[i] = '\0';
171 atf::utils::create_file("test.txt", long_contents);
173 ATF_REQUIRE(!atf::utils::compare_file("test.txt", ""));
174 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
175 ATF_REQUIRE(!atf::utils::compare_file("test.txt", "0123456789"));
176 long_contents[i - 1] = 'Z';
177 ATF_REQUIRE(!atf::utils::compare_file("test.txt", long_contents));
180 ATF_TEST_CASE_WITHOUT_HEAD(copy_file__empty);
181 ATF_TEST_CASE_BODY(copy_file__empty)
183 atf::utils::create_file("src.txt", "");
184 ATF_REQUIRE(chmod("src.txt", 0520) != -1);
186 atf::utils::copy_file("src.txt", "dest.txt");
187 ATF_REQUIRE(atf::utils::compare_file("dest.txt", ""));
189 ATF_REQUIRE(stat("dest.txt", &sb) != -1);
190 ATF_REQUIRE_EQ(0520, sb.st_mode & 0xfff);
193 ATF_TEST_CASE_WITHOUT_HEAD(copy_file__some_contents);
194 ATF_TEST_CASE_BODY(copy_file__some_contents)
196 atf::utils::create_file("src.txt", "This is a\ntest file\n");
197 atf::utils::copy_file("src.txt", "dest.txt");
198 ATF_REQUIRE(atf::utils::compare_file("dest.txt", "This is a\ntest file\n"));
201 ATF_TEST_CASE_WITHOUT_HEAD(create_file);
202 ATF_TEST_CASE_BODY(create_file)
204 atf::utils::create_file("test.txt", "This is a %d test");
206 ATF_REQUIRE_EQ("This is a %d test", read_file("test.txt"));
209 ATF_TEST_CASE_WITHOUT_HEAD(file_exists);
210 ATF_TEST_CASE_BODY(file_exists)
212 atf::utils::create_file("test.txt", "foo");
214 ATF_REQUIRE( atf::utils::file_exists("test.txt"));
215 ATF_REQUIRE( atf::utils::file_exists("./test.txt"));
216 ATF_REQUIRE(!atf::utils::file_exists("./test.tx"));
217 ATF_REQUIRE(!atf::utils::file_exists("test.txt2"));
220 ATF_TEST_CASE_WITHOUT_HEAD(fork);
221 ATF_TEST_CASE_BODY(fork)
223 std::cout << "Should not get into child\n";
224 std::cerr << "Should not get into child\n";
225 pid_t pid = atf::utils::fork();
227 std::cout << "Child stdout\n";
228 std::cerr << "Child stderr\n";
233 ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
234 ATF_REQUIRE(WIFEXITED(status));
235 ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
237 std::ostringstream out_name;
238 out_name << "atf_utils_fork_" << pid << "_out.txt";
239 std::ostringstream err_name;
240 err_name << "atf_utils_fork_" << pid << "_err.txt";
242 ATF_REQUIRE_EQ("Child stdout\n", read_file(out_name.str()));
243 ATF_REQUIRE_EQ("Child stderr\n", read_file(err_name.str()));
246 ATF_TEST_CASE_WITHOUT_HEAD(grep_collection__set);
247 ATF_TEST_CASE_BODY(grep_collection__set)
249 std::set< std::string > strings;
250 strings.insert("First");
251 strings.insert("Second");
253 ATF_REQUIRE( atf::utils::grep_collection("irs", strings));
254 ATF_REQUIRE( atf::utils::grep_collection("cond", strings));
255 ATF_REQUIRE(!atf::utils::grep_collection("Third", strings));
258 ATF_TEST_CASE_WITHOUT_HEAD(grep_collection__vector);
259 ATF_TEST_CASE_BODY(grep_collection__vector)
261 std::vector< std::string > strings;
262 strings.push_back("First");
263 strings.push_back("Second");
265 ATF_REQUIRE( atf::utils::grep_collection("irs", strings));
266 ATF_REQUIRE( atf::utils::grep_collection("cond", strings));
267 ATF_REQUIRE(!atf::utils::grep_collection("Third", strings));
270 ATF_TEST_CASE_WITHOUT_HEAD(grep_file);
271 ATF_TEST_CASE_BODY(grep_file)
273 atf::utils::create_file("test.txt", "line1\nthe second line\naaaabbbb\n");
275 ATF_REQUIRE(atf::utils::grep_file("line1", "test.txt"));
276 ATF_REQUIRE(atf::utils::grep_file("second line", "test.txt"));
277 ATF_REQUIRE(atf::utils::grep_file("aa.*bb", "test.txt"));
278 ATF_REQUIRE(!atf::utils::grep_file("foo", "test.txt"));
279 ATF_REQUIRE(!atf::utils::grep_file("bar", "test.txt"));
280 ATF_REQUIRE(!atf::utils::grep_file("aaaaa", "test.txt"));
283 ATF_TEST_CASE_WITHOUT_HEAD(grep_string);
284 ATF_TEST_CASE_BODY(grep_string)
286 const char *str = "a string - aaaabbbb";
287 ATF_REQUIRE(atf::utils::grep_string("a string", str));
288 ATF_REQUIRE(atf::utils::grep_string("^a string", str));
289 ATF_REQUIRE(atf::utils::grep_string("aaaabbbb$", str));
290 ATF_REQUIRE(atf::utils::grep_string("aa.*bb", str));
291 ATF_REQUIRE(!atf::utils::grep_string("foo", str));
292 ATF_REQUIRE(!atf::utils::grep_string("bar", str));
293 ATF_REQUIRE(!atf::utils::grep_string("aaaaa", str));
296 ATF_TEST_CASE_WITHOUT_HEAD(redirect__stdout);
297 ATF_TEST_CASE_BODY(redirect__stdout)
299 std::cout << "Buffer this";
300 atf::utils::redirect(STDOUT_FILENO, "captured.txt");
301 std::cout << "The printed message";
304 ATF_REQUIRE_EQ("The printed message", read_file("captured.txt"));
307 ATF_TEST_CASE_WITHOUT_HEAD(redirect__stderr);
308 ATF_TEST_CASE_BODY(redirect__stderr)
310 std::cerr << "Buffer this";
311 atf::utils::redirect(STDERR_FILENO, "captured.txt");
312 std::cerr << "The printed message";
315 ATF_REQUIRE_EQ("The printed message", read_file("captured.txt"));
318 ATF_TEST_CASE_WITHOUT_HEAD(redirect__other);
319 ATF_TEST_CASE_BODY(redirect__other)
321 const std::string message = "Foo bar\nbaz\n";
322 atf::utils::redirect(15, "captured.txt");
323 ATF_REQUIRE(write(15, message.c_str(), message.length()) != -1);
326 ATF_REQUIRE_EQ(message, read_file("captured.txt"));
330 fork_and_wait(const int exitstatus, const char* expout, const char* experr)
332 const pid_t pid = atf::utils::fork();
334 std::cout << "Some output\n";
335 std::cerr << "Some error\n";
338 atf::utils::wait(pid, exitstatus, expout, experr);
342 ATF_TEST_CASE_WITHOUT_HEAD(wait__ok);
343 ATF_TEST_CASE_BODY(wait__ok)
345 const pid_t control = fork();
346 ATF_REQUIRE(control != -1);
348 fork_and_wait(123, "Some output\n", "Some error\n");
351 ATF_REQUIRE(waitpid(control, &status, 0) != -1);
352 ATF_REQUIRE(WIFEXITED(status));
353 ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
357 ATF_TEST_CASE_WITHOUT_HEAD(wait__ok_nested);
358 ATF_TEST_CASE_BODY(wait__ok_nested)
360 const pid_t parent = atf::utils::fork();
361 ATF_REQUIRE(parent != -1);
363 const pid_t child = atf::utils::fork();
364 ATF_REQUIRE(child != -1);
367 std::cout << "Child output\n";
369 std::cerr << "Child error\n";
372 std::cout << "Parent output\n";
373 std::cerr << "Parent error\n";
374 atf::utils::wait(child, 50, "Child output\n", "Child error\n");
378 atf::utils::wait(parent, 40,
380 "subprocess stdout: Child output\n"
381 "subprocess stderr: Child error\n",
386 ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_exitstatus);
387 ATF_TEST_CASE_BODY(wait__invalid_exitstatus)
389 const pid_t control = fork();
390 ATF_REQUIRE(control != -1);
392 fork_and_wait(120, "Some output\n", "Some error\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__invalid_stdout);
402 ATF_TEST_CASE_BODY(wait__invalid_stdout)
404 const pid_t control = fork();
405 ATF_REQUIRE(control != -1);
407 fork_and_wait(123, "Some output foo\n", "Some error\n");
410 ATF_REQUIRE(waitpid(control, &status, 0) != -1);
411 ATF_REQUIRE(WIFEXITED(status));
412 ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
416 ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_stderr);
417 ATF_TEST_CASE_BODY(wait__invalid_stderr)
419 const pid_t control = fork();
420 ATF_REQUIRE(control != -1);
422 fork_and_wait(123, "Some output\n", "Some error foo\n");
425 ATF_REQUIRE(waitpid(control, &status, 0) != -1);
426 ATF_REQUIRE(WIFEXITED(status));
427 ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
431 ATF_TEST_CASE_WITHOUT_HEAD(wait__save_stdout);
432 ATF_TEST_CASE_BODY(wait__save_stdout)
434 const pid_t control = fork();
435 ATF_REQUIRE(control != -1);
437 fork_and_wait(123, "save:my-output.txt", "Some error\n");
440 ATF_REQUIRE(waitpid(control, &status, 0) != -1);
441 ATF_REQUIRE(WIFEXITED(status));
442 ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
444 ATF_REQUIRE(atf::utils::compare_file("my-output.txt", "Some output\n"));
448 ATF_TEST_CASE_WITHOUT_HEAD(wait__save_stderr);
449 ATF_TEST_CASE_BODY(wait__save_stderr)
451 const pid_t control = fork();
452 ATF_REQUIRE(control != -1);
454 fork_and_wait(123, "Some output\n", "save:my-output.txt");
457 ATF_REQUIRE(waitpid(control, &status, 0) != -1);
458 ATF_REQUIRE(WIFEXITED(status));
459 ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
461 ATF_REQUIRE(atf::utils::compare_file("my-output.txt", "Some error\n"));
465 // ------------------------------------------------------------------------
467 // ------------------------------------------------------------------------
469 ATF_INIT_TEST_CASES(tcs)
471 // Add the test for the free functions.
472 ATF_ADD_TEST_CASE(tcs, cat_file__empty);
473 ATF_ADD_TEST_CASE(tcs, cat_file__one_line);
474 ATF_ADD_TEST_CASE(tcs, cat_file__several_lines);
475 ATF_ADD_TEST_CASE(tcs, cat_file__no_newline_eof);
477 ATF_ADD_TEST_CASE(tcs, compare_file__empty__match);
478 ATF_ADD_TEST_CASE(tcs, compare_file__empty__not_match);
479 ATF_ADD_TEST_CASE(tcs, compare_file__short__match);
480 ATF_ADD_TEST_CASE(tcs, compare_file__short__not_match);
481 ATF_ADD_TEST_CASE(tcs, compare_file__long__match);
482 ATF_ADD_TEST_CASE(tcs, compare_file__long__not_match);
484 ATF_ADD_TEST_CASE(tcs, copy_file__empty);
485 ATF_ADD_TEST_CASE(tcs, copy_file__some_contents);
487 ATF_ADD_TEST_CASE(tcs, create_file);
489 ATF_ADD_TEST_CASE(tcs, file_exists);
491 ATF_ADD_TEST_CASE(tcs, fork);
493 ATF_ADD_TEST_CASE(tcs, grep_collection__set);
494 ATF_ADD_TEST_CASE(tcs, grep_collection__vector);
495 ATF_ADD_TEST_CASE(tcs, grep_file);
496 ATF_ADD_TEST_CASE(tcs, grep_string);
498 ATF_ADD_TEST_CASE(tcs, redirect__stdout);
499 ATF_ADD_TEST_CASE(tcs, redirect__stderr);
500 ATF_ADD_TEST_CASE(tcs, redirect__other);
502 ATF_ADD_TEST_CASE(tcs, wait__ok);
503 ATF_ADD_TEST_CASE(tcs, wait__ok_nested);
504 ATF_ADD_TEST_CASE(tcs, wait__invalid_exitstatus);
505 ATF_ADD_TEST_CASE(tcs, wait__invalid_stdout);
506 ATF_ADD_TEST_CASE(tcs, wait__invalid_stderr);
507 ATF_ADD_TEST_CASE(tcs, wait__save_stdout);
508 ATF_ADD_TEST_CASE(tcs, wait__save_stderr);