xref: /minix3/external/bsd/atf/dist/atf-c/utils_test.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*
2*11be35a1SLionel Sambuc  * Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc  *
4*11be35a1SLionel Sambuc  * Copyright (c) 2010 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 #include <sys/stat.h>
31*11be35a1SLionel Sambuc #include <sys/wait.h>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include <fcntl.h>
34*11be35a1SLionel Sambuc #include <stddef.h>
35*11be35a1SLionel Sambuc #include <stdio.h>
36*11be35a1SLionel Sambuc #include <stdlib.h>
37*11be35a1SLionel Sambuc #include <string.h>
38*11be35a1SLionel Sambuc #include <unistd.h>
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc #include <atf-c.h>
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc #include "atf-c/utils.h"
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc #include "detail/test_helpers.h"
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc /** Reads the contents of a file into a buffer.
47*11be35a1SLionel Sambuc  *
48*11be35a1SLionel Sambuc  * Up to buflen-1 characters are read into buffer.  If this function returns,
49*11be35a1SLionel Sambuc  * the contents read into the buffer are guaranteed to be nul-terminated.
50*11be35a1SLionel Sambuc  * Note, however, that if the file contains any nul characters itself,
51*11be35a1SLionel Sambuc  * comparing it "as a string" will not work.
52*11be35a1SLionel Sambuc  *
53*11be35a1SLionel Sambuc  * \param path The file to be read, which must exist.
54*11be35a1SLionel Sambuc  * \param buffer Buffer into which to store the file contents.
55*11be35a1SLionel Sambuc  * \param buflen Size of the target buffer.
56*11be35a1SLionel Sambuc  *
57*11be35a1SLionel Sambuc  * \return The count of bytes read. */
58*11be35a1SLionel Sambuc static ssize_t
read_file(const char * path,void * const buffer,const size_t buflen)59*11be35a1SLionel Sambuc read_file(const char *path, void *const buffer, const size_t buflen)
60*11be35a1SLionel Sambuc {
61*11be35a1SLionel Sambuc     const int fd = open(path, O_RDONLY);
62*11be35a1SLionel Sambuc     ATF_REQUIRE_MSG(fd != -1, "Cannot open %s", path);
63*11be35a1SLionel Sambuc     const ssize_t length = read(fd, buffer, buflen - 1);
64*11be35a1SLionel Sambuc     close(fd);
65*11be35a1SLionel Sambuc     ATF_REQUIRE(length != -1);
66*11be35a1SLionel Sambuc     ((char *)buffer)[length] = '\0';
67*11be35a1SLionel Sambuc     return length;
68*11be35a1SLionel Sambuc }
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(cat_file__empty);
ATF_TC_BODY(cat_file__empty,tc)71*11be35a1SLionel Sambuc ATF_TC_BODY(cat_file__empty, tc)
72*11be35a1SLionel Sambuc {
73*11be35a1SLionel Sambuc     atf_utils_create_file("file.txt", "%s", "");
74*11be35a1SLionel Sambuc     atf_utils_redirect(STDOUT_FILENO, "captured.txt");
75*11be35a1SLionel Sambuc     atf_utils_cat_file("file.txt", "PREFIX");
76*11be35a1SLionel Sambuc     fflush(stdout);
77*11be35a1SLionel Sambuc     close(STDOUT_FILENO);
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc     char buffer[1024];
80*11be35a1SLionel Sambuc     read_file("captured.txt", buffer, sizeof(buffer));
81*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ("", buffer);
82*11be35a1SLionel Sambuc }
83*11be35a1SLionel Sambuc 
84*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(cat_file__one_line);
ATF_TC_BODY(cat_file__one_line,tc)85*11be35a1SLionel Sambuc ATF_TC_BODY(cat_file__one_line, tc)
86*11be35a1SLionel Sambuc {
87*11be35a1SLionel Sambuc     atf_utils_create_file("file.txt", "This is a single line\n");
88*11be35a1SLionel Sambuc     atf_utils_redirect(STDOUT_FILENO, "captured.txt");
89*11be35a1SLionel Sambuc     atf_utils_cat_file("file.txt", "PREFIX");
90*11be35a1SLionel Sambuc     fflush(stdout);
91*11be35a1SLionel Sambuc     close(STDOUT_FILENO);
92*11be35a1SLionel Sambuc 
93*11be35a1SLionel Sambuc     char buffer[1024];
94*11be35a1SLionel Sambuc     read_file("captured.txt", buffer, sizeof(buffer));
95*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ("PREFIXThis is a single line\n", buffer);
96*11be35a1SLionel Sambuc }
97*11be35a1SLionel Sambuc 
98*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(cat_file__several_lines);
ATF_TC_BODY(cat_file__several_lines,tc)99*11be35a1SLionel Sambuc ATF_TC_BODY(cat_file__several_lines, tc)
100*11be35a1SLionel Sambuc {
101*11be35a1SLionel Sambuc     atf_utils_create_file("file.txt", "First\nSecond line\nAnd third\n");
102*11be35a1SLionel Sambuc     atf_utils_redirect(STDOUT_FILENO, "captured.txt");
103*11be35a1SLionel Sambuc     atf_utils_cat_file("file.txt", ">");
104*11be35a1SLionel Sambuc     fflush(stdout);
105*11be35a1SLionel Sambuc     close(STDOUT_FILENO);
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc     char buffer[1024];
108*11be35a1SLionel Sambuc     read_file("captured.txt", buffer, sizeof(buffer));
109*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ(">First\n>Second line\n>And third\n", buffer);
110*11be35a1SLionel Sambuc }
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(cat_file__no_newline_eof);
ATF_TC_BODY(cat_file__no_newline_eof,tc)113*11be35a1SLionel Sambuc ATF_TC_BODY(cat_file__no_newline_eof, tc)
114*11be35a1SLionel Sambuc {
115*11be35a1SLionel Sambuc     atf_utils_create_file("file.txt", "Foo\n bar baz");
116*11be35a1SLionel Sambuc     atf_utils_redirect(STDOUT_FILENO, "captured.txt");
117*11be35a1SLionel Sambuc     atf_utils_cat_file("file.txt", "PREFIX");
118*11be35a1SLionel Sambuc     fflush(stdout);
119*11be35a1SLionel Sambuc     close(STDOUT_FILENO);
120*11be35a1SLionel Sambuc 
121*11be35a1SLionel Sambuc     char buffer[1024];
122*11be35a1SLionel Sambuc     read_file("captured.txt", buffer, sizeof(buffer));
123*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ("PREFIXFoo\nPREFIX bar baz", buffer);
124*11be35a1SLionel Sambuc }
125*11be35a1SLionel Sambuc 
126*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(compare_file__empty__match);
ATF_TC_BODY(compare_file__empty__match,tc)127*11be35a1SLionel Sambuc ATF_TC_BODY(compare_file__empty__match, tc)
128*11be35a1SLionel Sambuc {
129*11be35a1SLionel Sambuc     atf_utils_create_file("test.txt", "%s", "");
130*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test.txt", ""));
131*11be35a1SLionel Sambuc }
132*11be35a1SLionel Sambuc 
133*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(compare_file__empty__not_match);
ATF_TC_BODY(compare_file__empty__not_match,tc)134*11be35a1SLionel Sambuc ATF_TC_BODY(compare_file__empty__not_match, tc)
135*11be35a1SLionel Sambuc {
136*11be35a1SLionel Sambuc     atf_utils_create_file("test.txt", "%s", "");
137*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", "\n"));
138*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", "foo"));
139*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", " "));
140*11be35a1SLionel Sambuc }
141*11be35a1SLionel Sambuc 
142*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(compare_file__short__match);
ATF_TC_BODY(compare_file__short__match,tc)143*11be35a1SLionel Sambuc ATF_TC_BODY(compare_file__short__match, tc)
144*11be35a1SLionel Sambuc {
145*11be35a1SLionel Sambuc     atf_utils_create_file("test.txt", "this is a short file");
146*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test.txt", "this is a short file"));
147*11be35a1SLionel Sambuc }
148*11be35a1SLionel Sambuc 
149*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(compare_file__short__not_match);
ATF_TC_BODY(compare_file__short__not_match,tc)150*11be35a1SLionel Sambuc ATF_TC_BODY(compare_file__short__not_match, tc)
151*11be35a1SLionel Sambuc {
152*11be35a1SLionel Sambuc     atf_utils_create_file("test.txt", "this is a short file");
153*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", ""));
154*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", "\n"));
155*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", "this is a Short file"));
156*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", "this is a short fil"));
157*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", "this is a short file "));
158*11be35a1SLionel Sambuc }
159*11be35a1SLionel Sambuc 
160*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(compare_file__long__match);
ATF_TC_BODY(compare_file__long__match,tc)161*11be35a1SLionel Sambuc ATF_TC_BODY(compare_file__long__match, tc)
162*11be35a1SLionel Sambuc {
163*11be35a1SLionel Sambuc     char long_contents[3456];
164*11be35a1SLionel Sambuc     size_t i = 0;
165*11be35a1SLionel Sambuc     for (; i < sizeof(long_contents) - 1; i++)
166*11be35a1SLionel Sambuc         long_contents[i] = '0' + (i % 10);
167*11be35a1SLionel Sambuc     long_contents[i] = '\0';
168*11be35a1SLionel Sambuc     atf_utils_create_file("test.txt", "%s", long_contents);
169*11be35a1SLionel Sambuc 
170*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test.txt", long_contents));
171*11be35a1SLionel Sambuc }
172*11be35a1SLionel Sambuc 
173*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(compare_file__long__not_match);
ATF_TC_BODY(compare_file__long__not_match,tc)174*11be35a1SLionel Sambuc ATF_TC_BODY(compare_file__long__not_match, tc)
175*11be35a1SLionel Sambuc {
176*11be35a1SLionel Sambuc     char long_contents[3456];
177*11be35a1SLionel Sambuc     size_t i = 0;
178*11be35a1SLionel Sambuc     for (; i < sizeof(long_contents) - 1; i++)
179*11be35a1SLionel Sambuc         long_contents[i] = '0' + (i % 10);
180*11be35a1SLionel Sambuc     long_contents[i] = '\0';
181*11be35a1SLionel Sambuc     atf_utils_create_file("test.txt", "%s", long_contents);
182*11be35a1SLionel Sambuc 
183*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", ""));
184*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", "\n"));
185*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", "0123456789"));
186*11be35a1SLionel Sambuc     long_contents[i - 1] = 'Z';
187*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_compare_file("test.txt", long_contents));
188*11be35a1SLionel Sambuc }
189*11be35a1SLionel Sambuc 
190*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(copy_file__empty);
ATF_TC_BODY(copy_file__empty,tc)191*11be35a1SLionel Sambuc ATF_TC_BODY(copy_file__empty, tc)
192*11be35a1SLionel Sambuc {
193*11be35a1SLionel Sambuc     atf_utils_create_file("src.txt", "%s", "");
194*11be35a1SLionel Sambuc     ATF_REQUIRE(chmod("src.txt", 0520) != -1);
195*11be35a1SLionel Sambuc 
196*11be35a1SLionel Sambuc     atf_utils_copy_file("src.txt", "dest.txt");
197*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("dest.txt", ""));
198*11be35a1SLionel Sambuc     struct stat sb;
199*11be35a1SLionel Sambuc     ATF_REQUIRE(stat("dest.txt", &sb) != -1);
200*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(0520, sb.st_mode & 0xfff);
201*11be35a1SLionel Sambuc }
202*11be35a1SLionel Sambuc 
203*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(copy_file__some_contents);
ATF_TC_BODY(copy_file__some_contents,tc)204*11be35a1SLionel Sambuc ATF_TC_BODY(copy_file__some_contents, tc)
205*11be35a1SLionel Sambuc {
206*11be35a1SLionel Sambuc     atf_utils_create_file("src.txt", "This is a\ntest file\n");
207*11be35a1SLionel Sambuc     atf_utils_copy_file("src.txt", "dest.txt");
208*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("dest.txt", "This is a\ntest file\n"));
209*11be35a1SLionel Sambuc }
210*11be35a1SLionel Sambuc 
211*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(create_file);
ATF_TC_BODY(create_file,tc)212*11be35a1SLionel Sambuc ATF_TC_BODY(create_file, tc)
213*11be35a1SLionel Sambuc {
214*11be35a1SLionel Sambuc     atf_utils_create_file("test.txt", "This is a test with %d", 12345);
215*11be35a1SLionel Sambuc 
216*11be35a1SLionel Sambuc     char buffer[128];
217*11be35a1SLionel Sambuc     read_file("test.txt", buffer, sizeof(buffer));
218*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ("This is a test with 12345", buffer);
219*11be35a1SLionel Sambuc }
220*11be35a1SLionel Sambuc 
221*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(file_exists);
ATF_TC_BODY(file_exists,tc)222*11be35a1SLionel Sambuc ATF_TC_BODY(file_exists, tc)
223*11be35a1SLionel Sambuc {
224*11be35a1SLionel Sambuc     atf_utils_create_file("test.txt", "foo");
225*11be35a1SLionel Sambuc 
226*11be35a1SLionel Sambuc     ATF_REQUIRE( atf_utils_file_exists("test.txt"));
227*11be35a1SLionel Sambuc     ATF_REQUIRE( atf_utils_file_exists("./test.txt"));
228*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_file_exists("./test.tx"));
229*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_file_exists("test.txt2"));
230*11be35a1SLionel Sambuc }
231*11be35a1SLionel Sambuc 
232*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(fork);
ATF_TC_BODY(fork,tc)233*11be35a1SLionel Sambuc ATF_TC_BODY(fork, tc)
234*11be35a1SLionel Sambuc {
235*11be35a1SLionel Sambuc     fprintf(stdout, "Should not get into child\n");
236*11be35a1SLionel Sambuc     fprintf(stderr, "Should not get into child\n");
237*11be35a1SLionel Sambuc     pid_t pid = atf_utils_fork();
238*11be35a1SLionel Sambuc     if (pid == 0) {
239*11be35a1SLionel Sambuc         fprintf(stdout, "Child stdout\n");
240*11be35a1SLionel Sambuc         fprintf(stderr, "Child stderr\n");
241*11be35a1SLionel Sambuc         exit(EXIT_SUCCESS);
242*11be35a1SLionel Sambuc     }
243*11be35a1SLionel Sambuc 
244*11be35a1SLionel Sambuc     int status;
245*11be35a1SLionel Sambuc     ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
246*11be35a1SLionel Sambuc     ATF_REQUIRE(WIFEXITED(status));
247*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
248*11be35a1SLionel Sambuc 
249*11be35a1SLionel Sambuc     char buffer[1024];
250*11be35a1SLionel Sambuc     read_file("atf_utils_fork_out.txt", buffer, sizeof(buffer));
251*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ("Child stdout\n", buffer);
252*11be35a1SLionel Sambuc     read_file("atf_utils_fork_err.txt", buffer, sizeof(buffer));
253*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ("Child stderr\n", buffer);
254*11be35a1SLionel Sambuc }
255*11be35a1SLionel Sambuc 
256*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(free_charpp__empty);
ATF_TC_BODY(free_charpp__empty,tc)257*11be35a1SLionel Sambuc ATF_TC_BODY(free_charpp__empty, tc)
258*11be35a1SLionel Sambuc {
259*11be35a1SLionel Sambuc     char **array = malloc(sizeof(char *) * 1);
260*11be35a1SLionel Sambuc     array[0] = NULL;
261*11be35a1SLionel Sambuc 
262*11be35a1SLionel Sambuc     atf_utils_free_charpp(array);
263*11be35a1SLionel Sambuc }
264*11be35a1SLionel Sambuc 
265*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(free_charpp__some);
ATF_TC_BODY(free_charpp__some,tc)266*11be35a1SLionel Sambuc ATF_TC_BODY(free_charpp__some, tc)
267*11be35a1SLionel Sambuc {
268*11be35a1SLionel Sambuc     char **array = malloc(sizeof(char *) * 4);
269*11be35a1SLionel Sambuc     array[0] = strdup("first");
270*11be35a1SLionel Sambuc     array[1] = strdup("second");
271*11be35a1SLionel Sambuc     array[2] = strdup("third");
272*11be35a1SLionel Sambuc     array[3] = NULL;
273*11be35a1SLionel Sambuc 
274*11be35a1SLionel Sambuc     atf_utils_free_charpp(array);
275*11be35a1SLionel Sambuc }
276*11be35a1SLionel Sambuc 
277*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(grep_file);
ATF_TC_BODY(grep_file,tc)278*11be35a1SLionel Sambuc ATF_TC_BODY(grep_file, tc)
279*11be35a1SLionel Sambuc {
280*11be35a1SLionel Sambuc     atf_utils_create_file("test.txt", "line1\nthe second line\naaaabbbb\n");
281*11be35a1SLionel Sambuc 
282*11be35a1SLionel Sambuc     ATF_CHECK(atf_utils_grep_file("line1", "test.txt"));
283*11be35a1SLionel Sambuc     ATF_CHECK(atf_utils_grep_file("line%d", "test.txt", 1));
284*11be35a1SLionel Sambuc     ATF_CHECK(atf_utils_grep_file("second line", "test.txt"));
285*11be35a1SLionel Sambuc     ATF_CHECK(atf_utils_grep_file("aa.*bb", "test.txt"));
286*11be35a1SLionel Sambuc     ATF_CHECK(!atf_utils_grep_file("foo", "test.txt"));
287*11be35a1SLionel Sambuc     ATF_CHECK(!atf_utils_grep_file("bar", "test.txt"));
288*11be35a1SLionel Sambuc     ATF_CHECK(!atf_utils_grep_file("aaaaa", "test.txt"));
289*11be35a1SLionel Sambuc }
290*11be35a1SLionel Sambuc 
291*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(grep_string);
ATF_TC_BODY(grep_string,tc)292*11be35a1SLionel Sambuc ATF_TC_BODY(grep_string, tc)
293*11be35a1SLionel Sambuc {
294*11be35a1SLionel Sambuc     const char *str = "a string - aaaabbbb";
295*11be35a1SLionel Sambuc     ATF_CHECK(atf_utils_grep_string("a string", str));
296*11be35a1SLionel Sambuc     ATF_CHECK(atf_utils_grep_string("^a string", str));
297*11be35a1SLionel Sambuc     ATF_CHECK(atf_utils_grep_string("aaaabbbb$", str));
298*11be35a1SLionel Sambuc     ATF_CHECK(atf_utils_grep_string("a%s*bb", str, "a."));
299*11be35a1SLionel Sambuc     ATF_CHECK(!atf_utils_grep_string("foo", str));
300*11be35a1SLionel Sambuc     ATF_CHECK(!atf_utils_grep_string("bar", str));
301*11be35a1SLionel Sambuc     ATF_CHECK(!atf_utils_grep_string("aaaaa", str));
302*11be35a1SLionel Sambuc }
303*11be35a1SLionel Sambuc 
304*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(readline__none);
ATF_TC_BODY(readline__none,tc)305*11be35a1SLionel Sambuc ATF_TC_BODY(readline__none, tc)
306*11be35a1SLionel Sambuc {
307*11be35a1SLionel Sambuc     atf_utils_create_file("empty.txt", "%s", "");
308*11be35a1SLionel Sambuc 
309*11be35a1SLionel Sambuc     const int fd = open("empty.txt", O_RDONLY);
310*11be35a1SLionel Sambuc     ATF_REQUIRE(fd != -1);
311*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_readline(fd) == NULL);
312*11be35a1SLionel Sambuc     close(fd);
313*11be35a1SLionel Sambuc }
314*11be35a1SLionel Sambuc 
315*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(readline__some);
ATF_TC_BODY(readline__some,tc)316*11be35a1SLionel Sambuc ATF_TC_BODY(readline__some, tc)
317*11be35a1SLionel Sambuc {
318*11be35a1SLionel Sambuc     const char *l1 = "First line with % formatting % characters %";
319*11be35a1SLionel Sambuc     const char *l2 = "Second line; much longer than the first one";
320*11be35a1SLionel Sambuc     const char *l3 = "Last line, without terminator";
321*11be35a1SLionel Sambuc 
322*11be35a1SLionel Sambuc     atf_utils_create_file("test.txt", "%s\n%s\n%s", l1, l2, l3);
323*11be35a1SLionel Sambuc 
324*11be35a1SLionel Sambuc     const int fd = open("test.txt", O_RDONLY);
325*11be35a1SLionel Sambuc     ATF_REQUIRE(fd != -1);
326*11be35a1SLionel Sambuc 
327*11be35a1SLionel Sambuc     char *line;
328*11be35a1SLionel Sambuc 
329*11be35a1SLionel Sambuc     line = atf_utils_readline(fd);
330*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ(l1, line);
331*11be35a1SLionel Sambuc     free(line);
332*11be35a1SLionel Sambuc 
333*11be35a1SLionel Sambuc     line = atf_utils_readline(fd);
334*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ(l2, line);
335*11be35a1SLionel Sambuc     free(line);
336*11be35a1SLionel Sambuc 
337*11be35a1SLionel Sambuc     line = atf_utils_readline(fd);
338*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ(l3, line);
339*11be35a1SLionel Sambuc     free(line);
340*11be35a1SLionel Sambuc 
341*11be35a1SLionel Sambuc     close(fd);
342*11be35a1SLionel Sambuc }
343*11be35a1SLionel Sambuc 
344*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(redirect__stdout);
ATF_TC_BODY(redirect__stdout,tc)345*11be35a1SLionel Sambuc ATF_TC_BODY(redirect__stdout, tc)
346*11be35a1SLionel Sambuc {
347*11be35a1SLionel Sambuc     printf("Buffer this");
348*11be35a1SLionel Sambuc     atf_utils_redirect(STDOUT_FILENO, "captured.txt");
349*11be35a1SLionel Sambuc     printf("The printed message");
350*11be35a1SLionel Sambuc     fflush(stdout);
351*11be35a1SLionel Sambuc 
352*11be35a1SLionel Sambuc     char buffer[1024];
353*11be35a1SLionel Sambuc     read_file("captured.txt", buffer, sizeof(buffer));
354*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ("The printed message", buffer);
355*11be35a1SLionel Sambuc }
356*11be35a1SLionel Sambuc 
357*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(redirect__stderr);
ATF_TC_BODY(redirect__stderr,tc)358*11be35a1SLionel Sambuc ATF_TC_BODY(redirect__stderr, tc)
359*11be35a1SLionel Sambuc {
360*11be35a1SLionel Sambuc     fprintf(stderr, "Buffer this");
361*11be35a1SLionel Sambuc     atf_utils_redirect(STDERR_FILENO, "captured.txt");
362*11be35a1SLionel Sambuc     fprintf(stderr, "The printed message");
363*11be35a1SLionel Sambuc     fflush(stderr);
364*11be35a1SLionel Sambuc 
365*11be35a1SLionel Sambuc     char buffer[1024];
366*11be35a1SLionel Sambuc     read_file("captured.txt", buffer, sizeof(buffer));
367*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ("The printed message", buffer);
368*11be35a1SLionel Sambuc }
369*11be35a1SLionel Sambuc 
370*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(redirect__other);
ATF_TC_BODY(redirect__other,tc)371*11be35a1SLionel Sambuc ATF_TC_BODY(redirect__other, tc)
372*11be35a1SLionel Sambuc {
373*11be35a1SLionel Sambuc     const char *message = "Foo bar\nbaz\n";
374*11be35a1SLionel Sambuc     atf_utils_redirect(15, "captured.txt");
375*11be35a1SLionel Sambuc     ATF_REQUIRE(write(15, message, strlen(message)) != -1);
376*11be35a1SLionel Sambuc     close(15);
377*11be35a1SLionel Sambuc 
378*11be35a1SLionel Sambuc     char buffer[1024];
379*11be35a1SLionel Sambuc     read_file("captured.txt", buffer, sizeof(buffer));
380*11be35a1SLionel Sambuc     ATF_REQUIRE_STREQ(message, buffer);
381*11be35a1SLionel Sambuc }
382*11be35a1SLionel Sambuc 
383*11be35a1SLionel Sambuc static void
fork_and_wait(const int exitstatus,const char * expout,const char * experr)384*11be35a1SLionel Sambuc fork_and_wait(const int exitstatus, const char* expout, const char* experr)
385*11be35a1SLionel Sambuc {
386*11be35a1SLionel Sambuc     const pid_t pid = atf_utils_fork();
387*11be35a1SLionel Sambuc     if (pid == 0) {
388*11be35a1SLionel Sambuc         fprintf(stdout, "Some output\n");
389*11be35a1SLionel Sambuc         fprintf(stderr, "Some error\n");
390*11be35a1SLionel Sambuc         exit(123);
391*11be35a1SLionel Sambuc     }
392*11be35a1SLionel Sambuc     atf_utils_wait(pid, exitstatus, expout, experr);
393*11be35a1SLionel Sambuc     exit(EXIT_SUCCESS);
394*11be35a1SLionel Sambuc }
395*11be35a1SLionel Sambuc 
396*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(wait__ok);
ATF_TC_BODY(wait__ok,tc)397*11be35a1SLionel Sambuc ATF_TC_BODY(wait__ok, tc)
398*11be35a1SLionel Sambuc {
399*11be35a1SLionel Sambuc     const pid_t control = fork();
400*11be35a1SLionel Sambuc     ATF_REQUIRE(control != -1);
401*11be35a1SLionel Sambuc     if (control == 0)
402*11be35a1SLionel Sambuc         fork_and_wait(123, "Some output\n", "Some error\n");
403*11be35a1SLionel Sambuc     else {
404*11be35a1SLionel Sambuc         int status;
405*11be35a1SLionel Sambuc         ATF_REQUIRE(waitpid(control, &status, 0) != -1);
406*11be35a1SLionel Sambuc         ATF_REQUIRE(WIFEXITED(status));
407*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
408*11be35a1SLionel Sambuc     }
409*11be35a1SLionel Sambuc }
410*11be35a1SLionel Sambuc 
411*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(wait__invalid_exitstatus);
ATF_TC_BODY(wait__invalid_exitstatus,tc)412*11be35a1SLionel Sambuc ATF_TC_BODY(wait__invalid_exitstatus, tc)
413*11be35a1SLionel Sambuc {
414*11be35a1SLionel Sambuc     const pid_t control = fork();
415*11be35a1SLionel Sambuc     ATF_REQUIRE(control != -1);
416*11be35a1SLionel Sambuc     if (control == 0)
417*11be35a1SLionel Sambuc         fork_and_wait(120, "Some output\n", "Some error\n");
418*11be35a1SLionel Sambuc     else {
419*11be35a1SLionel Sambuc         int status;
420*11be35a1SLionel Sambuc         ATF_REQUIRE(waitpid(control, &status, 0) != -1);
421*11be35a1SLionel Sambuc         ATF_REQUIRE(WIFEXITED(status));
422*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
423*11be35a1SLionel Sambuc     }
424*11be35a1SLionel Sambuc }
425*11be35a1SLionel Sambuc 
426*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(wait__invalid_stdout);
ATF_TC_BODY(wait__invalid_stdout,tc)427*11be35a1SLionel Sambuc ATF_TC_BODY(wait__invalid_stdout, tc)
428*11be35a1SLionel Sambuc {
429*11be35a1SLionel Sambuc     const pid_t control = fork();
430*11be35a1SLionel Sambuc     ATF_REQUIRE(control != -1);
431*11be35a1SLionel Sambuc     if (control == 0)
432*11be35a1SLionel Sambuc         fork_and_wait(123, "Some output foo\n", "Some error\n");
433*11be35a1SLionel Sambuc     else {
434*11be35a1SLionel Sambuc         int status;
435*11be35a1SLionel Sambuc         ATF_REQUIRE(waitpid(control, &status, 0) != -1);
436*11be35a1SLionel Sambuc         ATF_REQUIRE(WIFEXITED(status));
437*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
438*11be35a1SLionel Sambuc     }
439*11be35a1SLionel Sambuc }
440*11be35a1SLionel Sambuc 
441*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(wait__invalid_stderr);
ATF_TC_BODY(wait__invalid_stderr,tc)442*11be35a1SLionel Sambuc ATF_TC_BODY(wait__invalid_stderr, tc)
443*11be35a1SLionel Sambuc {
444*11be35a1SLionel Sambuc     const pid_t control = fork();
445*11be35a1SLionel Sambuc     ATF_REQUIRE(control != -1);
446*11be35a1SLionel Sambuc     if (control == 0)
447*11be35a1SLionel Sambuc         fork_and_wait(123, "Some output\n", "Some error foo\n");
448*11be35a1SLionel Sambuc     else {
449*11be35a1SLionel Sambuc         int status;
450*11be35a1SLionel Sambuc         ATF_REQUIRE(waitpid(control, &status, 0) != -1);
451*11be35a1SLionel Sambuc         ATF_REQUIRE(WIFEXITED(status));
452*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
453*11be35a1SLionel Sambuc     }
454*11be35a1SLionel Sambuc }
455*11be35a1SLionel Sambuc 
456*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(wait__save_stdout);
ATF_TC_BODY(wait__save_stdout,tc)457*11be35a1SLionel Sambuc ATF_TC_BODY(wait__save_stdout, tc)
458*11be35a1SLionel Sambuc {
459*11be35a1SLionel Sambuc     const pid_t control = fork();
460*11be35a1SLionel Sambuc     ATF_REQUIRE(control != -1);
461*11be35a1SLionel Sambuc     if (control == 0)
462*11be35a1SLionel Sambuc         fork_and_wait(123, "save:my-output.txt", "Some error\n");
463*11be35a1SLionel Sambuc     else {
464*11be35a1SLionel Sambuc         int status;
465*11be35a1SLionel Sambuc         ATF_REQUIRE(waitpid(control, &status, 0) != -1);
466*11be35a1SLionel Sambuc         ATF_REQUIRE(WIFEXITED(status));
467*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
468*11be35a1SLionel Sambuc 
469*11be35a1SLionel Sambuc         ATF_REQUIRE(atf_utils_compare_file("my-output.txt", "Some output\n"));
470*11be35a1SLionel Sambuc     }
471*11be35a1SLionel Sambuc }
472*11be35a1SLionel Sambuc 
473*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(wait__save_stderr);
ATF_TC_BODY(wait__save_stderr,tc)474*11be35a1SLionel Sambuc ATF_TC_BODY(wait__save_stderr, tc)
475*11be35a1SLionel Sambuc {
476*11be35a1SLionel Sambuc     const pid_t control = fork();
477*11be35a1SLionel Sambuc     ATF_REQUIRE(control != -1);
478*11be35a1SLionel Sambuc     if (control == 0)
479*11be35a1SLionel Sambuc         fork_and_wait(123, "Some output\n", "save:my-output.txt");
480*11be35a1SLionel Sambuc     else {
481*11be35a1SLionel Sambuc         int status;
482*11be35a1SLionel Sambuc         ATF_REQUIRE(waitpid(control, &status, 0) != -1);
483*11be35a1SLionel Sambuc         ATF_REQUIRE(WIFEXITED(status));
484*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
485*11be35a1SLionel Sambuc 
486*11be35a1SLionel Sambuc         ATF_REQUIRE(atf_utils_compare_file("my-output.txt", "Some error\n"));
487*11be35a1SLionel Sambuc     }
488*11be35a1SLionel Sambuc }
489*11be35a1SLionel Sambuc 
490*11be35a1SLionel Sambuc HEADER_TC(include, "atf-c/utils.h");
491*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)492*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
493*11be35a1SLionel Sambuc {
494*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cat_file__empty);
495*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cat_file__one_line);
496*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cat_file__several_lines);
497*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cat_file__no_newline_eof);
498*11be35a1SLionel Sambuc 
499*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, compare_file__empty__match);
500*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, compare_file__empty__not_match);
501*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, compare_file__short__match);
502*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, compare_file__short__not_match);
503*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, compare_file__long__match);
504*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, compare_file__long__not_match);
505*11be35a1SLionel Sambuc 
506*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, copy_file__empty);
507*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, copy_file__some_contents);
508*11be35a1SLionel Sambuc 
509*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, create_file);
510*11be35a1SLionel Sambuc 
511*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, file_exists);
512*11be35a1SLionel Sambuc 
513*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, fork);
514*11be35a1SLionel Sambuc 
515*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, free_charpp__empty);
516*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, free_charpp__some);
517*11be35a1SLionel Sambuc 
518*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, grep_file);
519*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, grep_string);
520*11be35a1SLionel Sambuc 
521*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, readline__none);
522*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, readline__some);
523*11be35a1SLionel Sambuc 
524*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, redirect__stdout);
525*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, redirect__stderr);
526*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, redirect__other);
527*11be35a1SLionel Sambuc 
528*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, wait__ok);
529*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, wait__save_stdout);
530*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, wait__save_stderr);
531*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, wait__invalid_exitstatus);
532*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, wait__invalid_stdout);
533*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, wait__invalid_stderr);
534*11be35a1SLionel Sambuc 
535*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, include);
536*11be35a1SLionel Sambuc 
537*11be35a1SLionel Sambuc     return atf_no_error();
538*11be35a1SLionel Sambuc }
539