1240116Smarcel/*
2240116Smarcel * Automated Testing Framework (atf)
3240116Smarcel *
4240116Smarcel * Copyright (c) 2008 The NetBSD Foundation, Inc.
5240116Smarcel * All rights reserved.
6240116Smarcel *
7240116Smarcel * Redistribution and use in source and binary forms, with or without
8240116Smarcel * modification, are permitted provided that the following conditions
9240116Smarcel * are met:
10240116Smarcel * 1. Redistributions of source code must retain the above copyright
11240116Smarcel *    notice, this list of conditions and the following disclaimer.
12240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel *    notice, this list of conditions and the following disclaimer in the
14240116Smarcel *    documentation and/or other materials provided with the distribution.
15240116Smarcel *
16240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27240116Smarcel * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28240116Smarcel */
29240116Smarcel
30240116Smarcel#include <fcntl.h>
31240116Smarcel#include <signal.h>
32240116Smarcel#include <stdio.h>
33240116Smarcel#include <stdlib.h>
34240116Smarcel#include <string.h>
35240116Smarcel#include <unistd.h>
36240116Smarcel
37240116Smarcel#include <atf-c.h>
38240116Smarcel
39240116Smarcel#include "atf-c/check.h"
40240116Smarcel#include "atf-c/config.h"
41240116Smarcel
42240116Smarcel#include "detail/fs.h"
43240116Smarcel#include "detail/map.h"
44240116Smarcel#include "detail/process.h"
45240116Smarcel#include "detail/test_helpers.h"
46240116Smarcel
47240116Smarcel/* ---------------------------------------------------------------------
48240116Smarcel * Auxiliary functions.
49240116Smarcel * --------------------------------------------------------------------- */
50240116Smarcel
51240116Smarcelstatic
52240116Smarcelvoid
53240116Smarceldo_exec(const atf_tc_t *tc, const char *helper_name, atf_check_result_t *r)
54240116Smarcel{
55240116Smarcel    atf_fs_path_t process_helpers;
56240116Smarcel    const char *argv[3];
57240116Smarcel
58240116Smarcel    get_process_helpers_path(tc, false, &process_helpers);
59240116Smarcel
60240116Smarcel    argv[0] = atf_fs_path_cstring(&process_helpers);
61240116Smarcel    argv[1] = helper_name;
62240116Smarcel    argv[2] = NULL;
63240116Smarcel    printf("Executing %s %s\n", argv[0], argv[1]);
64240116Smarcel    RE(atf_check_exec_array(argv, r));
65240116Smarcel
66240116Smarcel    atf_fs_path_fini(&process_helpers);
67240116Smarcel}
68240116Smarcel
69240116Smarcelstatic
70240116Smarcelvoid
71240116Smarceldo_exec_with_arg(const atf_tc_t *tc, const char *helper_name, const char *arg,
72240116Smarcel                 atf_check_result_t *r)
73240116Smarcel{
74240116Smarcel    atf_fs_path_t process_helpers;
75240116Smarcel    const char *argv[4];
76240116Smarcel
77240116Smarcel    get_process_helpers_path(tc, false, &process_helpers);
78240116Smarcel
79240116Smarcel    argv[0] = atf_fs_path_cstring(&process_helpers);
80240116Smarcel    argv[1] = helper_name;
81240116Smarcel    argv[2] = arg;
82240116Smarcel    argv[3] = NULL;
83240116Smarcel    printf("Executing %s %s %s\n", argv[0], argv[1], argv[2]);
84240116Smarcel    RE(atf_check_exec_array(argv, r));
85240116Smarcel
86240116Smarcel    atf_fs_path_fini(&process_helpers);
87240116Smarcel}
88240116Smarcel
89240116Smarcelstatic
90240116Smarcelvoid
91240116Smarcelcheck_line(int fd, const char *exp)
92240116Smarcel{
93260029Sjmmv    char *line = atf_utils_readline(fd);
94260029Sjmmv    ATF_CHECK(line != NULL);
95260029Sjmmv    ATF_CHECK_STREQ_MSG(exp, line, "read: '%s', expected: '%s'", line, exp);
96260029Sjmmv    free(line);
97240116Smarcel}
98240116Smarcel
99240116Smarcel/* ---------------------------------------------------------------------
100240116Smarcel * Helper test cases for the free functions.
101240116Smarcel * --------------------------------------------------------------------- */
102240116Smarcel
103240116SmarcelATF_TC(h_build_c_o_ok);
104240116SmarcelATF_TC_HEAD(h_build_c_o_ok, tc)
105240116Smarcel{
106240116Smarcel    atf_tc_set_md_var(tc, "descr", "Helper test case for build_c_o");
107240116Smarcel}
108240116SmarcelATF_TC_BODY(h_build_c_o_ok, tc)
109240116Smarcel{
110240116Smarcel    FILE *sfile;
111240116Smarcel    bool success;
112240116Smarcel
113240116Smarcel    ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
114240116Smarcel    fprintf(sfile, "#include <stdio.h>\n");
115240116Smarcel    fclose(sfile);
116240116Smarcel
117240116Smarcel    RE(atf_check_build_c_o("test.c", "test.o", NULL, &success));
118240116Smarcel    ATF_REQUIRE(success);
119240116Smarcel}
120240116Smarcel
121240116SmarcelATF_TC(h_build_c_o_fail);
122240116SmarcelATF_TC_HEAD(h_build_c_o_fail, tc)
123240116Smarcel{
124240116Smarcel    atf_tc_set_md_var(tc, "descr", "Helper test case for build_c_o");
125240116Smarcel}
126240116SmarcelATF_TC_BODY(h_build_c_o_fail, tc)
127240116Smarcel{
128240116Smarcel    FILE *sfile;
129240116Smarcel    bool success;
130240116Smarcel
131240116Smarcel    ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
132240116Smarcel    fprintf(sfile, "void foo(void) { int a = UNDEFINED_SYMBOL; }\n");
133240116Smarcel    fclose(sfile);
134240116Smarcel
135240116Smarcel    RE(atf_check_build_c_o("test.c", "test.o", NULL, &success));
136240116Smarcel    ATF_REQUIRE(!success);
137240116Smarcel}
138240116Smarcel
139240116SmarcelATF_TC(h_build_cpp_ok);
140240116SmarcelATF_TC_HEAD(h_build_cpp_ok, tc)
141240116Smarcel{
142240116Smarcel    atf_tc_set_md_var(tc, "descr", "Helper test case for build_cpp");
143240116Smarcel}
144240116SmarcelATF_TC_BODY(h_build_cpp_ok, tc)
145240116Smarcel{
146240116Smarcel    FILE *sfile;
147240116Smarcel    bool success;
148240116Smarcel    atf_fs_path_t test_p;
149240116Smarcel
150240116Smarcel    RE(atf_fs_path_init_fmt(&test_p, "test.p"));
151240116Smarcel
152240116Smarcel    ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
153240116Smarcel    fprintf(sfile, "#define A foo\n");
154240116Smarcel    fprintf(sfile, "#define B bar\n");
155240116Smarcel    fprintf(sfile, "A B\n");
156240116Smarcel    fclose(sfile);
157240116Smarcel
158240116Smarcel    RE(atf_check_build_cpp("test.c", atf_fs_path_cstring(&test_p), NULL,
159240116Smarcel                           &success));
160240116Smarcel    ATF_REQUIRE(success);
161240116Smarcel
162240116Smarcel    atf_fs_path_fini(&test_p);
163240116Smarcel}
164240116Smarcel
165240116SmarcelATF_TC(h_build_cpp_fail);
166240116SmarcelATF_TC_HEAD(h_build_cpp_fail, tc)
167240116Smarcel{
168240116Smarcel    atf_tc_set_md_var(tc, "descr", "Helper test case for build_cpp");
169240116Smarcel}
170240116SmarcelATF_TC_BODY(h_build_cpp_fail, tc)
171240116Smarcel{
172240116Smarcel    FILE *sfile;
173240116Smarcel    bool success;
174240116Smarcel
175240116Smarcel    ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
176240116Smarcel    fprintf(sfile, "#include \"./non-existent.h\"\n");
177240116Smarcel    fclose(sfile);
178240116Smarcel
179240116Smarcel    RE(atf_check_build_cpp("test.c", "test.p", NULL, &success));
180240116Smarcel    ATF_REQUIRE(!success);
181240116Smarcel}
182240116Smarcel
183240116SmarcelATF_TC(h_build_cxx_o_ok);
184240116SmarcelATF_TC_HEAD(h_build_cxx_o_ok, tc)
185240116Smarcel{
186240116Smarcel    atf_tc_set_md_var(tc, "descr", "Helper test case for build_cxx_o");
187240116Smarcel}
188240116SmarcelATF_TC_BODY(h_build_cxx_o_ok, tc)
189240116Smarcel{
190240116Smarcel    FILE *sfile;
191240116Smarcel    bool success;
192240116Smarcel
193240116Smarcel    ATF_REQUIRE((sfile = fopen("test.cpp", "w")) != NULL);
194240116Smarcel    fprintf(sfile, "#include <iostream>\n");
195240116Smarcel    fclose(sfile);
196240116Smarcel
197240116Smarcel    RE(atf_check_build_cxx_o("test.cpp", "test.o", NULL, &success));
198240116Smarcel    ATF_REQUIRE(success);
199240116Smarcel}
200240116Smarcel
201240116SmarcelATF_TC(h_build_cxx_o_fail);
202240116SmarcelATF_TC_HEAD(h_build_cxx_o_fail, tc)
203240116Smarcel{
204240116Smarcel    atf_tc_set_md_var(tc, "descr", "Helper test case for build_cxx_o");
205240116Smarcel}
206240116SmarcelATF_TC_BODY(h_build_cxx_o_fail, tc)
207240116Smarcel{
208240116Smarcel    FILE *sfile;
209240116Smarcel    bool success;
210240116Smarcel
211240116Smarcel    ATF_REQUIRE((sfile = fopen("test.cpp", "w")) != NULL);
212240116Smarcel    fprintf(sfile, "void foo(void) { int a = UNDEFINED_SYMBOL; }\n");
213240116Smarcel    fclose(sfile);
214240116Smarcel
215240116Smarcel    RE(atf_check_build_cxx_o("test.cpp", "test.o", NULL, &success));
216240116Smarcel    ATF_REQUIRE(!success);
217240116Smarcel}
218240116Smarcel
219240116Smarcel/* ---------------------------------------------------------------------
220240116Smarcel * Test cases for the free functions.
221240116Smarcel * --------------------------------------------------------------------- */
222240116Smarcel
223240116Smarcelstatic
224240116Smarcelvoid
225240116Smarcelinit_and_run_h_tc(atf_tc_t *tc, const atf_tc_pack_t *tcpack,
226240116Smarcel                  const char *outname, const char *errname)
227240116Smarcel{
228240116Smarcel    const char *const config[] = { NULL };
229240116Smarcel
230240116Smarcel    RE(atf_tc_init_pack(tc, tcpack, config));
231240116Smarcel    run_h_tc(tc, outname, errname, "result");
232240116Smarcel    atf_tc_fini(tc);
233240116Smarcel}
234240116Smarcel
235240116SmarcelATF_TC(build_c_o);
236240116SmarcelATF_TC_HEAD(build_c_o, tc)
237240116Smarcel{
238240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks the atf_check_build_c_o "
239240116Smarcel                      "function");
240240116Smarcel}
241240116SmarcelATF_TC_BODY(build_c_o, tc)
242240116Smarcel{
243240116Smarcel    init_and_run_h_tc(&ATF_TC_NAME(h_build_c_o_ok),
244240116Smarcel             &ATF_TC_PACK_NAME(h_build_c_o_ok), "stdout", "stderr");
245260029Sjmmv    ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
246260029Sjmmv    ATF_CHECK(atf_utils_grep_file("-c test.c", "stdout"));
247240116Smarcel
248240116Smarcel    init_and_run_h_tc(&ATF_TC_NAME(h_build_c_o_fail),
249240116Smarcel             &ATF_TC_PACK_NAME(h_build_c_o_fail), "stdout", "stderr");
250260029Sjmmv    ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
251260029Sjmmv    ATF_CHECK(atf_utils_grep_file("-c test.c", "stdout"));
252260029Sjmmv    ATF_CHECK(atf_utils_grep_file("test.c", "stderr"));
253260029Sjmmv    ATF_CHECK(atf_utils_grep_file("UNDEFINED_SYMBOL", "stderr"));
254240116Smarcel}
255240116Smarcel
256240116SmarcelATF_TC(build_cpp);
257240116SmarcelATF_TC_HEAD(build_cpp, tc)
258240116Smarcel{
259240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks the atf_check_build_cpp "
260240116Smarcel                      "function");
261240116Smarcel}
262240116SmarcelATF_TC_BODY(build_cpp, tc)
263240116Smarcel{
264240116Smarcel    init_and_run_h_tc(&ATF_TC_NAME(h_build_cpp_ok),
265240116Smarcel             &ATF_TC_PACK_NAME(h_build_cpp_ok), "stdout", "stderr");
266260029Sjmmv    ATF_CHECK(atf_utils_grep_file("-o.*test.p", "stdout"));
267260029Sjmmv    ATF_CHECK(atf_utils_grep_file("test.c", "stdout"));
268260029Sjmmv    ATF_CHECK(atf_utils_grep_file("foo bar", "test.p"));
269240116Smarcel
270240116Smarcel    init_and_run_h_tc(&ATF_TC_NAME(h_build_cpp_fail),
271240116Smarcel             &ATF_TC_PACK_NAME(h_build_cpp_fail), "stdout", "stderr");
272260029Sjmmv    ATF_CHECK(atf_utils_grep_file("-o test.p", "stdout"));
273260029Sjmmv    ATF_CHECK(atf_utils_grep_file("test.c", "stdout"));
274260029Sjmmv    ATF_CHECK(atf_utils_grep_file("test.c", "stderr"));
275260029Sjmmv    ATF_CHECK(atf_utils_grep_file("non-existent.h", "stderr"));
276240116Smarcel}
277240116Smarcel
278240116SmarcelATF_TC(build_cxx_o);
279240116SmarcelATF_TC_HEAD(build_cxx_o, tc)
280240116Smarcel{
281240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks the atf_check_build_cxx_o "
282240116Smarcel                      "function");
283240116Smarcel}
284240116SmarcelATF_TC_BODY(build_cxx_o, tc)
285240116Smarcel{
286240116Smarcel    init_and_run_h_tc(&ATF_TC_NAME(h_build_cxx_o_ok),
287240116Smarcel             &ATF_TC_PACK_NAME(h_build_cxx_o_ok), "stdout", "stderr");
288260029Sjmmv    ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
289260029Sjmmv    ATF_CHECK(atf_utils_grep_file("-c test.cpp", "stdout"));
290240116Smarcel
291240116Smarcel    init_and_run_h_tc(&ATF_TC_NAME(h_build_cxx_o_fail),
292240116Smarcel             &ATF_TC_PACK_NAME(h_build_cxx_o_fail), "stdout", "stderr");
293260029Sjmmv    ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
294260029Sjmmv    ATF_CHECK(atf_utils_grep_file("-c test.cpp", "stdout"));
295260029Sjmmv    ATF_CHECK(atf_utils_grep_file("test.cpp", "stderr"));
296260029Sjmmv    ATF_CHECK(atf_utils_grep_file("UNDEFINED_SYMBOL", "stderr"));
297240116Smarcel}
298240116Smarcel
299240116SmarcelATF_TC(exec_array);
300240116SmarcelATF_TC_HEAD(exec_array, tc)
301240116Smarcel{
302240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
303240116Smarcel                      "works properly");
304240116Smarcel}
305240116SmarcelATF_TC_BODY(exec_array, tc)
306240116Smarcel{
307240116Smarcel    atf_fs_path_t process_helpers;
308240116Smarcel    atf_check_result_t result;
309240116Smarcel
310240116Smarcel    get_process_helpers_path(tc, false, &process_helpers);
311240116Smarcel
312240116Smarcel    const char *argv[4];
313240116Smarcel    argv[0] = atf_fs_path_cstring(&process_helpers);
314240116Smarcel    argv[1] = "echo";
315240116Smarcel    argv[2] = "test-message";
316240116Smarcel    argv[3] = NULL;
317240116Smarcel
318240116Smarcel    RE(atf_check_exec_array(argv, &result));
319240116Smarcel
320240116Smarcel    ATF_CHECK(atf_check_result_exited(&result));
321240116Smarcel    ATF_CHECK(atf_check_result_exitcode(&result) == EXIT_SUCCESS);
322240116Smarcel
323240116Smarcel    {
324240116Smarcel        const char *path = atf_check_result_stdout(&result);
325240116Smarcel        int fd = open(path, O_RDONLY);
326240116Smarcel        ATF_CHECK(fd != -1);
327240116Smarcel        check_line(fd, "test-message");
328240116Smarcel        close(fd);
329240116Smarcel    }
330240116Smarcel
331240116Smarcel    atf_check_result_fini(&result);
332240116Smarcel    atf_fs_path_fini(&process_helpers);
333240116Smarcel}
334240116Smarcel
335240116SmarcelATF_TC(exec_cleanup);
336240116SmarcelATF_TC_HEAD(exec_cleanup, tc)
337240116Smarcel{
338240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
339240116Smarcel                      "properly cleans up the temporary files it creates");
340240116Smarcel}
341240116SmarcelATF_TC_BODY(exec_cleanup, tc)
342240116Smarcel{
343240116Smarcel    atf_fs_path_t out, err;
344240116Smarcel    atf_check_result_t result;
345240116Smarcel    bool exists;
346240116Smarcel
347240116Smarcel    do_exec(tc, "exit-success", &result);
348240116Smarcel    RE(atf_fs_path_init_fmt(&out, "%s", atf_check_result_stdout(&result)));
349240116Smarcel    RE(atf_fs_path_init_fmt(&err, "%s", atf_check_result_stderr(&result)));
350240116Smarcel
351240116Smarcel    RE(atf_fs_exists(&out, &exists)); ATF_CHECK(exists);
352240116Smarcel    RE(atf_fs_exists(&err, &exists)); ATF_CHECK(exists);
353240116Smarcel    atf_check_result_fini(&result);
354240116Smarcel    RE(atf_fs_exists(&out, &exists)); ATF_CHECK(!exists);
355240116Smarcel    RE(atf_fs_exists(&err, &exists)); ATF_CHECK(!exists);
356240116Smarcel
357240116Smarcel    atf_fs_path_fini(&err);
358240116Smarcel    atf_fs_path_fini(&out);
359240116Smarcel}
360240116Smarcel
361240116SmarcelATF_TC(exec_exitstatus);
362240116SmarcelATF_TC_HEAD(exec_exitstatus, tc)
363240116Smarcel{
364240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
365240116Smarcel                      "properly captures the exit status of the executed "
366240116Smarcel                      "command");
367240116Smarcel}
368240116SmarcelATF_TC_BODY(exec_exitstatus, tc)
369240116Smarcel{
370240116Smarcel    {
371240116Smarcel        atf_check_result_t result;
372240116Smarcel        do_exec(tc, "exit-success", &result);
373240116Smarcel        ATF_CHECK(atf_check_result_exited(&result));
374240116Smarcel        ATF_CHECK(!atf_check_result_signaled(&result));
375240116Smarcel        ATF_CHECK(atf_check_result_exitcode(&result) == EXIT_SUCCESS);
376240116Smarcel        atf_check_result_fini(&result);
377240116Smarcel    }
378240116Smarcel
379240116Smarcel    {
380240116Smarcel        atf_check_result_t result;
381240116Smarcel        do_exec(tc, "exit-failure", &result);
382240116Smarcel        ATF_CHECK(atf_check_result_exited(&result));
383240116Smarcel        ATF_CHECK(!atf_check_result_signaled(&result));
384240116Smarcel        ATF_CHECK(atf_check_result_exitcode(&result) == EXIT_FAILURE);
385240116Smarcel        atf_check_result_fini(&result);
386240116Smarcel    }
387240116Smarcel
388240116Smarcel    {
389240116Smarcel        atf_check_result_t result;
390240116Smarcel        do_exec(tc, "exit-signal", &result);
391240116Smarcel        ATF_CHECK(!atf_check_result_exited(&result));
392240116Smarcel        ATF_CHECK(atf_check_result_signaled(&result));
393240116Smarcel        ATF_CHECK(atf_check_result_termsig(&result) == SIGKILL);
394240116Smarcel        atf_check_result_fini(&result);
395240116Smarcel    }
396240116Smarcel}
397240116Smarcel
398240116SmarcelATF_TC(exec_stdout_stderr);
399240116SmarcelATF_TC_HEAD(exec_stdout_stderr, tc)
400240116Smarcel{
401240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
402240116Smarcel                      "properly captures the stdout and stderr streams "
403240116Smarcel                      "of the child process");
404240116Smarcel}
405240116SmarcelATF_TC_BODY(exec_stdout_stderr, tc)
406240116Smarcel{
407240116Smarcel    atf_check_result_t result1, result2;
408240116Smarcel    const char *out1, *out2;
409240116Smarcel    const char *err1, *err2;
410240116Smarcel
411240116Smarcel    do_exec_with_arg(tc, "stdout-stderr", "result1", &result1);
412240116Smarcel    ATF_CHECK(atf_check_result_exited(&result1));
413240116Smarcel    ATF_CHECK(atf_check_result_exitcode(&result1) == EXIT_SUCCESS);
414240116Smarcel
415240116Smarcel    do_exec_with_arg(tc, "stdout-stderr", "result2", &result2);
416240116Smarcel    ATF_CHECK(atf_check_result_exited(&result2));
417240116Smarcel    ATF_CHECK(atf_check_result_exitcode(&result2) == EXIT_SUCCESS);
418240116Smarcel
419240116Smarcel    out1 = atf_check_result_stdout(&result1);
420240116Smarcel    out2 = atf_check_result_stdout(&result2);
421240116Smarcel    err1 = atf_check_result_stderr(&result1);
422240116Smarcel    err2 = atf_check_result_stderr(&result2);
423240116Smarcel
424240116Smarcel    ATF_CHECK(strstr(out1, "check.XXXXXX") == NULL);
425240116Smarcel    ATF_CHECK(strstr(out2, "check.XXXXXX") == NULL);
426240116Smarcel    ATF_CHECK(strstr(err1, "check.XXXXXX") == NULL);
427240116Smarcel    ATF_CHECK(strstr(err2, "check.XXXXXX") == NULL);
428240116Smarcel
429240116Smarcel    ATF_CHECK(strstr(out1, "/check") != NULL);
430240116Smarcel    ATF_CHECK(strstr(out2, "/check") != NULL);
431240116Smarcel    ATF_CHECK(strstr(err1, "/check") != NULL);
432240116Smarcel    ATF_CHECK(strstr(err2, "/check") != NULL);
433240116Smarcel
434240116Smarcel    ATF_CHECK(strstr(out1, "/stdout") != NULL);
435240116Smarcel    ATF_CHECK(strstr(out2, "/stdout") != NULL);
436240116Smarcel    ATF_CHECK(strstr(err1, "/stderr") != NULL);
437240116Smarcel    ATF_CHECK(strstr(err2, "/stderr") != NULL);
438240116Smarcel
439240116Smarcel    ATF_CHECK(strcmp(out1, out2) != 0);
440240116Smarcel    ATF_CHECK(strcmp(err1, err2) != 0);
441240116Smarcel
442240116Smarcel#define CHECK_LINES(path, outname, resname) \
443240116Smarcel    do { \
444240116Smarcel        int fd = open(path, O_RDONLY); \
445240116Smarcel        ATF_CHECK(fd != -1); \
446240116Smarcel        check_line(fd, "Line 1 to " outname " for " resname); \
447240116Smarcel        check_line(fd, "Line 2 to " outname " for " resname); \
448240116Smarcel        close(fd); \
449240116Smarcel    } while (false)
450240116Smarcel
451240116Smarcel    CHECK_LINES(out1, "stdout", "result1");
452240116Smarcel    CHECK_LINES(out2, "stdout", "result2");
453240116Smarcel    CHECK_LINES(err1, "stderr", "result1");
454240116Smarcel    CHECK_LINES(err2, "stderr", "result2");
455240116Smarcel
456240116Smarcel#undef CHECK_LINES
457240116Smarcel
458240116Smarcel    atf_check_result_fini(&result2);
459240116Smarcel    atf_check_result_fini(&result1);
460240116Smarcel}
461240116Smarcel
462240116SmarcelATF_TC(exec_umask);
463240116SmarcelATF_TC_HEAD(exec_umask, tc)
464240116Smarcel{
465240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
466240116Smarcel                      "correctly reports an error if the umask is too "
467240116Smarcel                      "restrictive to create temporary files");
468240116Smarcel}
469240116SmarcelATF_TC_BODY(exec_umask, tc)
470240116Smarcel{
471240116Smarcel    atf_check_result_t result;
472240116Smarcel    atf_fs_path_t process_helpers;
473240116Smarcel    const char *argv[3];
474240116Smarcel
475240116Smarcel    get_process_helpers_path(tc, false, &process_helpers);
476240116Smarcel    argv[0] = atf_fs_path_cstring(&process_helpers);
477240116Smarcel    argv[1] = "exit-success";
478240116Smarcel    argv[2] = NULL;
479240116Smarcel
480240116Smarcel    umask(0222);
481240116Smarcel    atf_error_t err = atf_check_exec_array(argv, &result);
482240116Smarcel    ATF_CHECK(atf_is_error(err));
483240116Smarcel    ATF_CHECK(atf_error_is(err, "invalid_umask"));
484240116Smarcel    atf_error_free(err);
485240116Smarcel
486240116Smarcel    atf_fs_path_fini(&process_helpers);
487240116Smarcel}
488240116Smarcel
489240116SmarcelATF_TC(exec_unknown);
490240116SmarcelATF_TC_HEAD(exec_unknown, tc)
491240116Smarcel{
492240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks that running a non-existing "
493240116Smarcel                      "binary is handled correctly");
494240116Smarcel}
495240116SmarcelATF_TC_BODY(exec_unknown, tc)
496240116Smarcel{
497240116Smarcel    char buf[1024];
498240116Smarcel    snprintf(buf, sizeof(buf), "%s/non-existent",
499240116Smarcel             atf_config_get("atf_workdir"));
500240116Smarcel
501240116Smarcel    const char *argv[2];
502240116Smarcel    argv[0] = buf;
503240116Smarcel    argv[1] = NULL;
504240116Smarcel
505240116Smarcel    atf_check_result_t result;
506240116Smarcel    RE(atf_check_exec_array(argv, &result));
507240116Smarcel    ATF_CHECK(atf_check_result_exited(&result));
508240116Smarcel    ATF_CHECK(atf_check_result_exitcode(&result) == 127);
509240116Smarcel    atf_check_result_fini(&result);
510240116Smarcel}
511240116Smarcel
512240116Smarcel/* ---------------------------------------------------------------------
513240116Smarcel * Tests cases for the header file.
514240116Smarcel * --------------------------------------------------------------------- */
515240116Smarcel
516240116SmarcelHEADER_TC(include, "atf-c/check.h");
517240116Smarcel
518240116Smarcel/* ---------------------------------------------------------------------
519240116Smarcel * Main.
520240116Smarcel * --------------------------------------------------------------------- */
521240116Smarcel
522240116SmarcelATF_TP_ADD_TCS(tp)
523240116Smarcel{
524240116Smarcel    /* Add the test cases for the free functions. */
525240116Smarcel    ATF_TP_ADD_TC(tp, build_c_o);
526240116Smarcel    ATF_TP_ADD_TC(tp, build_cpp);
527240116Smarcel    ATF_TP_ADD_TC(tp, build_cxx_o);
528240116Smarcel    ATF_TP_ADD_TC(tp, exec_array);
529240116Smarcel    ATF_TP_ADD_TC(tp, exec_cleanup);
530240116Smarcel    ATF_TP_ADD_TC(tp, exec_exitstatus);
531240116Smarcel    ATF_TP_ADD_TC(tp, exec_stdout_stderr);
532240116Smarcel    ATF_TP_ADD_TC(tp, exec_umask);
533240116Smarcel    ATF_TP_ADD_TC(tp, exec_unknown);
534240116Smarcel
535240116Smarcel    /* Add the test cases for the header file. */
536240116Smarcel    ATF_TP_ADD_TC(tp, include);
537240116Smarcel
538240116Smarcel    return atf_no_error();
539240116Smarcel}
540