xref: /freebsd/contrib/atf/atf-c/check_test.c (revision 8f0ea33f2bbf3a6aa80235f0a02fa5f2780c2b17)
1*0677dfd1SJulio Merino /* Copyright (c) 2008 The NetBSD Foundation, Inc.
2c243e490SMarcel Moolenaar  * All rights reserved.
3c243e490SMarcel Moolenaar  *
4c243e490SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
5c243e490SMarcel Moolenaar  * modification, are permitted provided that the following conditions
6c243e490SMarcel Moolenaar  * are met:
7c243e490SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
8c243e490SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
9c243e490SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
10c243e490SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
11c243e490SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
12c243e490SMarcel Moolenaar  *
13c243e490SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14c243e490SMarcel Moolenaar  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15c243e490SMarcel Moolenaar  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16c243e490SMarcel Moolenaar  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17c243e490SMarcel Moolenaar  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18c243e490SMarcel Moolenaar  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c243e490SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20c243e490SMarcel Moolenaar  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21c243e490SMarcel Moolenaar  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22c243e490SMarcel Moolenaar  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23c243e490SMarcel Moolenaar  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24*0677dfd1SJulio Merino  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25*0677dfd1SJulio Merino 
26*0677dfd1SJulio Merino #include "atf-c/check.h"
27c243e490SMarcel Moolenaar 
28c243e490SMarcel Moolenaar #include <fcntl.h>
29c243e490SMarcel Moolenaar #include <signal.h>
30c243e490SMarcel Moolenaar #include <stdio.h>
31c243e490SMarcel Moolenaar #include <stdlib.h>
32c243e490SMarcel Moolenaar #include <string.h>
33c243e490SMarcel Moolenaar #include <unistd.h>
34c243e490SMarcel Moolenaar 
35c243e490SMarcel Moolenaar #include <atf-c.h>
36c243e490SMarcel Moolenaar 
37*0677dfd1SJulio Merino #include "atf-c/detail/fs.h"
38*0677dfd1SJulio Merino #include "atf-c/detail/map.h"
39*0677dfd1SJulio Merino #include "atf-c/detail/process.h"
40*0677dfd1SJulio Merino #include "atf-c/detail/test_helpers.h"
41c243e490SMarcel Moolenaar 
42c243e490SMarcel Moolenaar /* ---------------------------------------------------------------------
43c243e490SMarcel Moolenaar  * Auxiliary functions.
44c243e490SMarcel Moolenaar  * --------------------------------------------------------------------- */
45c243e490SMarcel Moolenaar 
46c243e490SMarcel Moolenaar static
47c243e490SMarcel Moolenaar void
do_exec(const atf_tc_t * tc,const char * helper_name,atf_check_result_t * r)48c243e490SMarcel Moolenaar do_exec(const atf_tc_t *tc, const char *helper_name, atf_check_result_t *r)
49c243e490SMarcel Moolenaar {
50c243e490SMarcel Moolenaar     atf_fs_path_t process_helpers;
51c243e490SMarcel Moolenaar     const char *argv[3];
52c243e490SMarcel Moolenaar 
53c243e490SMarcel Moolenaar     get_process_helpers_path(tc, false, &process_helpers);
54c243e490SMarcel Moolenaar 
55c243e490SMarcel Moolenaar     argv[0] = atf_fs_path_cstring(&process_helpers);
56c243e490SMarcel Moolenaar     argv[1] = helper_name;
57c243e490SMarcel Moolenaar     argv[2] = NULL;
58c243e490SMarcel Moolenaar     printf("Executing %s %s\n", argv[0], argv[1]);
59c243e490SMarcel Moolenaar     RE(atf_check_exec_array(argv, r));
60c243e490SMarcel Moolenaar 
61c243e490SMarcel Moolenaar     atf_fs_path_fini(&process_helpers);
62c243e490SMarcel Moolenaar }
63c243e490SMarcel Moolenaar 
64c243e490SMarcel Moolenaar static
65c243e490SMarcel Moolenaar void
do_exec_with_arg(const atf_tc_t * tc,const char * helper_name,const char * arg,atf_check_result_t * r)66c243e490SMarcel Moolenaar do_exec_with_arg(const atf_tc_t *tc, const char *helper_name, const char *arg,
67c243e490SMarcel Moolenaar                  atf_check_result_t *r)
68c243e490SMarcel Moolenaar {
69c243e490SMarcel Moolenaar     atf_fs_path_t process_helpers;
70c243e490SMarcel Moolenaar     const char *argv[4];
71c243e490SMarcel Moolenaar 
72c243e490SMarcel Moolenaar     get_process_helpers_path(tc, false, &process_helpers);
73c243e490SMarcel Moolenaar 
74c243e490SMarcel Moolenaar     argv[0] = atf_fs_path_cstring(&process_helpers);
75c243e490SMarcel Moolenaar     argv[1] = helper_name;
76c243e490SMarcel Moolenaar     argv[2] = arg;
77c243e490SMarcel Moolenaar     argv[3] = NULL;
78c243e490SMarcel Moolenaar     printf("Executing %s %s %s\n", argv[0], argv[1], argv[2]);
79c243e490SMarcel Moolenaar     RE(atf_check_exec_array(argv, r));
80c243e490SMarcel Moolenaar 
81c243e490SMarcel Moolenaar     atf_fs_path_fini(&process_helpers);
82c243e490SMarcel Moolenaar }
83c243e490SMarcel Moolenaar 
84c243e490SMarcel Moolenaar static
85c243e490SMarcel Moolenaar void
check_line(int fd,const char * exp)86c243e490SMarcel Moolenaar check_line(int fd, const char *exp)
87c243e490SMarcel Moolenaar {
88a18eacbeSJulio Merino     char *line = atf_utils_readline(fd);
89a18eacbeSJulio Merino     ATF_CHECK(line != NULL);
90a18eacbeSJulio Merino     ATF_CHECK_STREQ_MSG(exp, line, "read: '%s', expected: '%s'", line, exp);
91a18eacbeSJulio Merino     free(line);
92c243e490SMarcel Moolenaar }
93c243e490SMarcel Moolenaar 
94c243e490SMarcel Moolenaar /* ---------------------------------------------------------------------
95c243e490SMarcel Moolenaar  * Helper test cases for the free functions.
96c243e490SMarcel Moolenaar  * --------------------------------------------------------------------- */
97c243e490SMarcel Moolenaar 
98c243e490SMarcel Moolenaar ATF_TC(h_build_c_o_ok);
ATF_TC_HEAD(h_build_c_o_ok,tc)99c243e490SMarcel Moolenaar ATF_TC_HEAD(h_build_c_o_ok, tc)
100c243e490SMarcel Moolenaar {
101c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Helper test case for build_c_o");
102c243e490SMarcel Moolenaar }
ATF_TC_BODY(h_build_c_o_ok,tc)103c243e490SMarcel Moolenaar ATF_TC_BODY(h_build_c_o_ok, tc)
104c243e490SMarcel Moolenaar {
105c243e490SMarcel Moolenaar     FILE *sfile;
106c243e490SMarcel Moolenaar     bool success;
107c243e490SMarcel Moolenaar 
108c243e490SMarcel Moolenaar     ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
109c243e490SMarcel Moolenaar     fprintf(sfile, "#include <stdio.h>\n");
110c243e490SMarcel Moolenaar     fclose(sfile);
111c243e490SMarcel Moolenaar 
112c243e490SMarcel Moolenaar     RE(atf_check_build_c_o("test.c", "test.o", NULL, &success));
113c243e490SMarcel Moolenaar     ATF_REQUIRE(success);
114c243e490SMarcel Moolenaar }
115c243e490SMarcel Moolenaar 
116c243e490SMarcel Moolenaar ATF_TC(h_build_c_o_fail);
ATF_TC_HEAD(h_build_c_o_fail,tc)117c243e490SMarcel Moolenaar ATF_TC_HEAD(h_build_c_o_fail, tc)
118c243e490SMarcel Moolenaar {
119c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Helper test case for build_c_o");
120c243e490SMarcel Moolenaar }
ATF_TC_BODY(h_build_c_o_fail,tc)121c243e490SMarcel Moolenaar ATF_TC_BODY(h_build_c_o_fail, tc)
122c243e490SMarcel Moolenaar {
123c243e490SMarcel Moolenaar     FILE *sfile;
124c243e490SMarcel Moolenaar     bool success;
125c243e490SMarcel Moolenaar 
126c243e490SMarcel Moolenaar     ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
127c243e490SMarcel Moolenaar     fprintf(sfile, "void foo(void) { int a = UNDEFINED_SYMBOL; }\n");
128c243e490SMarcel Moolenaar     fclose(sfile);
129c243e490SMarcel Moolenaar 
130c243e490SMarcel Moolenaar     RE(atf_check_build_c_o("test.c", "test.o", NULL, &success));
131c243e490SMarcel Moolenaar     ATF_REQUIRE(!success);
132c243e490SMarcel Moolenaar }
133c243e490SMarcel Moolenaar 
134c243e490SMarcel Moolenaar ATF_TC(h_build_cpp_ok);
ATF_TC_HEAD(h_build_cpp_ok,tc)135c243e490SMarcel Moolenaar ATF_TC_HEAD(h_build_cpp_ok, tc)
136c243e490SMarcel Moolenaar {
137c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Helper test case for build_cpp");
138c243e490SMarcel Moolenaar }
ATF_TC_BODY(h_build_cpp_ok,tc)139c243e490SMarcel Moolenaar ATF_TC_BODY(h_build_cpp_ok, tc)
140c243e490SMarcel Moolenaar {
141c243e490SMarcel Moolenaar     FILE *sfile;
142c243e490SMarcel Moolenaar     bool success;
143c243e490SMarcel Moolenaar     atf_fs_path_t test_p;
144c243e490SMarcel Moolenaar 
145c243e490SMarcel Moolenaar     RE(atf_fs_path_init_fmt(&test_p, "test.p"));
146c243e490SMarcel Moolenaar 
147c243e490SMarcel Moolenaar     ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
148c243e490SMarcel Moolenaar     fprintf(sfile, "#define A foo\n");
149c243e490SMarcel Moolenaar     fprintf(sfile, "#define B bar\n");
150c243e490SMarcel Moolenaar     fprintf(sfile, "A B\n");
151c243e490SMarcel Moolenaar     fclose(sfile);
152c243e490SMarcel Moolenaar 
153c243e490SMarcel Moolenaar     RE(atf_check_build_cpp("test.c", atf_fs_path_cstring(&test_p), NULL,
154c243e490SMarcel Moolenaar                            &success));
155c243e490SMarcel Moolenaar     ATF_REQUIRE(success);
156c243e490SMarcel Moolenaar 
157c243e490SMarcel Moolenaar     atf_fs_path_fini(&test_p);
158c243e490SMarcel Moolenaar }
159c243e490SMarcel Moolenaar 
160c243e490SMarcel Moolenaar ATF_TC(h_build_cpp_fail);
ATF_TC_HEAD(h_build_cpp_fail,tc)161c243e490SMarcel Moolenaar ATF_TC_HEAD(h_build_cpp_fail, tc)
162c243e490SMarcel Moolenaar {
163c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Helper test case for build_cpp");
164c243e490SMarcel Moolenaar }
ATF_TC_BODY(h_build_cpp_fail,tc)165c243e490SMarcel Moolenaar ATF_TC_BODY(h_build_cpp_fail, tc)
166c243e490SMarcel Moolenaar {
167c243e490SMarcel Moolenaar     FILE *sfile;
168c243e490SMarcel Moolenaar     bool success;
169c243e490SMarcel Moolenaar 
170c243e490SMarcel Moolenaar     ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
171c243e490SMarcel Moolenaar     fprintf(sfile, "#include \"./non-existent.h\"\n");
172c243e490SMarcel Moolenaar     fclose(sfile);
173c243e490SMarcel Moolenaar 
174c243e490SMarcel Moolenaar     RE(atf_check_build_cpp("test.c", "test.p", NULL, &success));
175c243e490SMarcel Moolenaar     ATF_REQUIRE(!success);
176c243e490SMarcel Moolenaar }
177c243e490SMarcel Moolenaar 
178c243e490SMarcel Moolenaar ATF_TC(h_build_cxx_o_ok);
ATF_TC_HEAD(h_build_cxx_o_ok,tc)179c243e490SMarcel Moolenaar ATF_TC_HEAD(h_build_cxx_o_ok, tc)
180c243e490SMarcel Moolenaar {
181c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Helper test case for build_cxx_o");
182c243e490SMarcel Moolenaar }
ATF_TC_BODY(h_build_cxx_o_ok,tc)183c243e490SMarcel Moolenaar ATF_TC_BODY(h_build_cxx_o_ok, tc)
184c243e490SMarcel Moolenaar {
185c243e490SMarcel Moolenaar     FILE *sfile;
186c243e490SMarcel Moolenaar     bool success;
187c243e490SMarcel Moolenaar 
188c243e490SMarcel Moolenaar     ATF_REQUIRE((sfile = fopen("test.cpp", "w")) != NULL);
189c243e490SMarcel Moolenaar     fprintf(sfile, "#include <iostream>\n");
190c243e490SMarcel Moolenaar     fclose(sfile);
191c243e490SMarcel Moolenaar 
192c243e490SMarcel Moolenaar     RE(atf_check_build_cxx_o("test.cpp", "test.o", NULL, &success));
193c243e490SMarcel Moolenaar     ATF_REQUIRE(success);
194c243e490SMarcel Moolenaar }
195c243e490SMarcel Moolenaar 
196c243e490SMarcel Moolenaar ATF_TC(h_build_cxx_o_fail);
ATF_TC_HEAD(h_build_cxx_o_fail,tc)197c243e490SMarcel Moolenaar ATF_TC_HEAD(h_build_cxx_o_fail, tc)
198c243e490SMarcel Moolenaar {
199c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Helper test case for build_cxx_o");
200c243e490SMarcel Moolenaar }
ATF_TC_BODY(h_build_cxx_o_fail,tc)201c243e490SMarcel Moolenaar ATF_TC_BODY(h_build_cxx_o_fail, tc)
202c243e490SMarcel Moolenaar {
203c243e490SMarcel Moolenaar     FILE *sfile;
204c243e490SMarcel Moolenaar     bool success;
205c243e490SMarcel Moolenaar 
206c243e490SMarcel Moolenaar     ATF_REQUIRE((sfile = fopen("test.cpp", "w")) != NULL);
207c243e490SMarcel Moolenaar     fprintf(sfile, "void foo(void) { int a = UNDEFINED_SYMBOL; }\n");
208c243e490SMarcel Moolenaar     fclose(sfile);
209c243e490SMarcel Moolenaar 
210c243e490SMarcel Moolenaar     RE(atf_check_build_cxx_o("test.cpp", "test.o", NULL, &success));
211c243e490SMarcel Moolenaar     ATF_REQUIRE(!success);
212c243e490SMarcel Moolenaar }
213c243e490SMarcel Moolenaar 
214c243e490SMarcel Moolenaar /* ---------------------------------------------------------------------
215c243e490SMarcel Moolenaar  * Test cases for the free functions.
216c243e490SMarcel Moolenaar  * --------------------------------------------------------------------- */
217c243e490SMarcel Moolenaar 
218c243e490SMarcel Moolenaar static
219c243e490SMarcel Moolenaar void
init_and_run_h_tc(atf_tc_t * tc,const atf_tc_pack_t * tcpack,const char * outname,const char * errname)220c243e490SMarcel Moolenaar init_and_run_h_tc(atf_tc_t *tc, const atf_tc_pack_t *tcpack,
221c243e490SMarcel Moolenaar                   const char *outname, const char *errname)
222c243e490SMarcel Moolenaar {
223c243e490SMarcel Moolenaar     const char *const config[] = { NULL };
224c243e490SMarcel Moolenaar 
225c243e490SMarcel Moolenaar     RE(atf_tc_init_pack(tc, tcpack, config));
226c243e490SMarcel Moolenaar     run_h_tc(tc, outname, errname, "result");
227c243e490SMarcel Moolenaar     atf_tc_fini(tc);
228c243e490SMarcel Moolenaar }
229c243e490SMarcel Moolenaar 
230c243e490SMarcel Moolenaar ATF_TC(build_c_o);
ATF_TC_HEAD(build_c_o,tc)231c243e490SMarcel Moolenaar ATF_TC_HEAD(build_c_o, tc)
232c243e490SMarcel Moolenaar {
233c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Checks the atf_check_build_c_o "
234c243e490SMarcel Moolenaar                       "function");
235c243e490SMarcel Moolenaar }
ATF_TC_BODY(build_c_o,tc)236c243e490SMarcel Moolenaar ATF_TC_BODY(build_c_o, tc)
237c243e490SMarcel Moolenaar {
238c243e490SMarcel Moolenaar     init_and_run_h_tc(&ATF_TC_NAME(h_build_c_o_ok),
239c243e490SMarcel Moolenaar              &ATF_TC_PACK_NAME(h_build_c_o_ok), "stdout", "stderr");
240a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
241a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("-c test.c", "stdout"));
242c243e490SMarcel Moolenaar 
243c243e490SMarcel Moolenaar     init_and_run_h_tc(&ATF_TC_NAME(h_build_c_o_fail),
244c243e490SMarcel Moolenaar              &ATF_TC_PACK_NAME(h_build_c_o_fail), "stdout", "stderr");
245a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
246a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("-c test.c", "stdout"));
247a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("test.c", "stderr"));
248a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("UNDEFINED_SYMBOL", "stderr"));
249c243e490SMarcel Moolenaar }
250c243e490SMarcel Moolenaar 
251c243e490SMarcel Moolenaar ATF_TC(build_cpp);
ATF_TC_HEAD(build_cpp,tc)252c243e490SMarcel Moolenaar ATF_TC_HEAD(build_cpp, tc)
253c243e490SMarcel Moolenaar {
254c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Checks the atf_check_build_cpp "
255c243e490SMarcel Moolenaar                       "function");
256c243e490SMarcel Moolenaar }
ATF_TC_BODY(build_cpp,tc)257c243e490SMarcel Moolenaar ATF_TC_BODY(build_cpp, tc)
258c243e490SMarcel Moolenaar {
259c243e490SMarcel Moolenaar     init_and_run_h_tc(&ATF_TC_NAME(h_build_cpp_ok),
260c243e490SMarcel Moolenaar              &ATF_TC_PACK_NAME(h_build_cpp_ok), "stdout", "stderr");
261a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("-o.*test.p", "stdout"));
262a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("test.c", "stdout"));
263a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("foo bar", "test.p"));
264c243e490SMarcel Moolenaar 
265c243e490SMarcel Moolenaar     init_and_run_h_tc(&ATF_TC_NAME(h_build_cpp_fail),
266c243e490SMarcel Moolenaar              &ATF_TC_PACK_NAME(h_build_cpp_fail), "stdout", "stderr");
267a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("-o test.p", "stdout"));
268a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("test.c", "stdout"));
269a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("test.c", "stderr"));
270a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("non-existent.h", "stderr"));
271c243e490SMarcel Moolenaar }
272c243e490SMarcel Moolenaar 
273c243e490SMarcel Moolenaar ATF_TC(build_cxx_o);
ATF_TC_HEAD(build_cxx_o,tc)274c243e490SMarcel Moolenaar ATF_TC_HEAD(build_cxx_o, tc)
275c243e490SMarcel Moolenaar {
276c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Checks the atf_check_build_cxx_o "
277c243e490SMarcel Moolenaar                       "function");
278c243e490SMarcel Moolenaar }
ATF_TC_BODY(build_cxx_o,tc)279c243e490SMarcel Moolenaar ATF_TC_BODY(build_cxx_o, tc)
280c243e490SMarcel Moolenaar {
281c243e490SMarcel Moolenaar     init_and_run_h_tc(&ATF_TC_NAME(h_build_cxx_o_ok),
282c243e490SMarcel Moolenaar              &ATF_TC_PACK_NAME(h_build_cxx_o_ok), "stdout", "stderr");
283a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
284a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("-c test.cpp", "stdout"));
285c243e490SMarcel Moolenaar 
286c243e490SMarcel Moolenaar     init_and_run_h_tc(&ATF_TC_NAME(h_build_cxx_o_fail),
287c243e490SMarcel Moolenaar              &ATF_TC_PACK_NAME(h_build_cxx_o_fail), "stdout", "stderr");
288a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
289a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("-c test.cpp", "stdout"));
290a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("test.cpp", "stderr"));
291a18eacbeSJulio Merino     ATF_CHECK(atf_utils_grep_file("UNDEFINED_SYMBOL", "stderr"));
292c243e490SMarcel Moolenaar }
293c243e490SMarcel Moolenaar 
294c243e490SMarcel Moolenaar ATF_TC(exec_array);
ATF_TC_HEAD(exec_array,tc)295c243e490SMarcel Moolenaar ATF_TC_HEAD(exec_array, tc)
296c243e490SMarcel Moolenaar {
297c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
298c243e490SMarcel Moolenaar                       "works properly");
299c243e490SMarcel Moolenaar }
ATF_TC_BODY(exec_array,tc)300c243e490SMarcel Moolenaar ATF_TC_BODY(exec_array, tc)
301c243e490SMarcel Moolenaar {
302c243e490SMarcel Moolenaar     atf_fs_path_t process_helpers;
303c243e490SMarcel Moolenaar     atf_check_result_t result;
304c243e490SMarcel Moolenaar 
305c243e490SMarcel Moolenaar     get_process_helpers_path(tc, false, &process_helpers);
306c243e490SMarcel Moolenaar 
307c243e490SMarcel Moolenaar     const char *argv[4];
308c243e490SMarcel Moolenaar     argv[0] = atf_fs_path_cstring(&process_helpers);
309c243e490SMarcel Moolenaar     argv[1] = "echo";
310c243e490SMarcel Moolenaar     argv[2] = "test-message";
311c243e490SMarcel Moolenaar     argv[3] = NULL;
312c243e490SMarcel Moolenaar 
313c243e490SMarcel Moolenaar     RE(atf_check_exec_array(argv, &result));
314c243e490SMarcel Moolenaar 
315c243e490SMarcel Moolenaar     ATF_CHECK(atf_check_result_exited(&result));
316c243e490SMarcel Moolenaar     ATF_CHECK(atf_check_result_exitcode(&result) == EXIT_SUCCESS);
317c243e490SMarcel Moolenaar 
318c243e490SMarcel Moolenaar     {
319c243e490SMarcel Moolenaar         const char *path = atf_check_result_stdout(&result);
320c243e490SMarcel Moolenaar         int fd = open(path, O_RDONLY);
321c243e490SMarcel Moolenaar         ATF_CHECK(fd != -1);
322c243e490SMarcel Moolenaar         check_line(fd, "test-message");
323c243e490SMarcel Moolenaar         close(fd);
324c243e490SMarcel Moolenaar     }
325c243e490SMarcel Moolenaar 
326c243e490SMarcel Moolenaar     atf_check_result_fini(&result);
327c243e490SMarcel Moolenaar     atf_fs_path_fini(&process_helpers);
328c243e490SMarcel Moolenaar }
329c243e490SMarcel Moolenaar 
330c243e490SMarcel Moolenaar ATF_TC(exec_cleanup);
ATF_TC_HEAD(exec_cleanup,tc)331c243e490SMarcel Moolenaar ATF_TC_HEAD(exec_cleanup, tc)
332c243e490SMarcel Moolenaar {
333c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
334c243e490SMarcel Moolenaar                       "properly cleans up the temporary files it creates");
335c243e490SMarcel Moolenaar }
ATF_TC_BODY(exec_cleanup,tc)336c243e490SMarcel Moolenaar ATF_TC_BODY(exec_cleanup, tc)
337c243e490SMarcel Moolenaar {
338c243e490SMarcel Moolenaar     atf_fs_path_t out, err;
339c243e490SMarcel Moolenaar     atf_check_result_t result;
340c243e490SMarcel Moolenaar     bool exists;
341c243e490SMarcel Moolenaar 
342c243e490SMarcel Moolenaar     do_exec(tc, "exit-success", &result);
343c243e490SMarcel Moolenaar     RE(atf_fs_path_init_fmt(&out, "%s", atf_check_result_stdout(&result)));
344c243e490SMarcel Moolenaar     RE(atf_fs_path_init_fmt(&err, "%s", atf_check_result_stderr(&result)));
345c243e490SMarcel Moolenaar 
346c243e490SMarcel Moolenaar     RE(atf_fs_exists(&out, &exists)); ATF_CHECK(exists);
347c243e490SMarcel Moolenaar     RE(atf_fs_exists(&err, &exists)); ATF_CHECK(exists);
348c243e490SMarcel Moolenaar     atf_check_result_fini(&result);
349c243e490SMarcel Moolenaar     RE(atf_fs_exists(&out, &exists)); ATF_CHECK(!exists);
350c243e490SMarcel Moolenaar     RE(atf_fs_exists(&err, &exists)); ATF_CHECK(!exists);
351c243e490SMarcel Moolenaar 
352c243e490SMarcel Moolenaar     atf_fs_path_fini(&err);
353c243e490SMarcel Moolenaar     atf_fs_path_fini(&out);
354c243e490SMarcel Moolenaar }
355c243e490SMarcel Moolenaar 
356c243e490SMarcel Moolenaar ATF_TC(exec_exitstatus);
ATF_TC_HEAD(exec_exitstatus,tc)357c243e490SMarcel Moolenaar ATF_TC_HEAD(exec_exitstatus, tc)
358c243e490SMarcel Moolenaar {
359c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
360c243e490SMarcel Moolenaar                       "properly captures the exit status of the executed "
361c243e490SMarcel Moolenaar                       "command");
362c243e490SMarcel Moolenaar }
ATF_TC_BODY(exec_exitstatus,tc)363c243e490SMarcel Moolenaar ATF_TC_BODY(exec_exitstatus, tc)
364c243e490SMarcel Moolenaar {
365c243e490SMarcel Moolenaar     {
366c243e490SMarcel Moolenaar         atf_check_result_t result;
367c243e490SMarcel Moolenaar         do_exec(tc, "exit-success", &result);
368c243e490SMarcel Moolenaar         ATF_CHECK(atf_check_result_exited(&result));
369c243e490SMarcel Moolenaar         ATF_CHECK(!atf_check_result_signaled(&result));
370c243e490SMarcel Moolenaar         ATF_CHECK(atf_check_result_exitcode(&result) == EXIT_SUCCESS);
371c243e490SMarcel Moolenaar         atf_check_result_fini(&result);
372c243e490SMarcel Moolenaar     }
373c243e490SMarcel Moolenaar 
374c243e490SMarcel Moolenaar     {
375c243e490SMarcel Moolenaar         atf_check_result_t result;
376c243e490SMarcel Moolenaar         do_exec(tc, "exit-failure", &result);
377c243e490SMarcel Moolenaar         ATF_CHECK(atf_check_result_exited(&result));
378c243e490SMarcel Moolenaar         ATF_CHECK(!atf_check_result_signaled(&result));
379c243e490SMarcel Moolenaar         ATF_CHECK(atf_check_result_exitcode(&result) == EXIT_FAILURE);
380c243e490SMarcel Moolenaar         atf_check_result_fini(&result);
381c243e490SMarcel Moolenaar     }
382c243e490SMarcel Moolenaar 
383c243e490SMarcel Moolenaar     {
384c243e490SMarcel Moolenaar         atf_check_result_t result;
385c243e490SMarcel Moolenaar         do_exec(tc, "exit-signal", &result);
386c243e490SMarcel Moolenaar         ATF_CHECK(!atf_check_result_exited(&result));
387c243e490SMarcel Moolenaar         ATF_CHECK(atf_check_result_signaled(&result));
388c243e490SMarcel Moolenaar         ATF_CHECK(atf_check_result_termsig(&result) == SIGKILL);
389c243e490SMarcel Moolenaar         atf_check_result_fini(&result);
390c243e490SMarcel Moolenaar     }
391c243e490SMarcel Moolenaar }
392c243e490SMarcel Moolenaar 
393c243e490SMarcel Moolenaar ATF_TC(exec_stdout_stderr);
ATF_TC_HEAD(exec_stdout_stderr,tc)394c243e490SMarcel Moolenaar ATF_TC_HEAD(exec_stdout_stderr, tc)
395c243e490SMarcel Moolenaar {
396c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
397c243e490SMarcel Moolenaar                       "properly captures the stdout and stderr streams "
398c243e490SMarcel Moolenaar                       "of the child process");
399c243e490SMarcel Moolenaar }
ATF_TC_BODY(exec_stdout_stderr,tc)400c243e490SMarcel Moolenaar ATF_TC_BODY(exec_stdout_stderr, tc)
401c243e490SMarcel Moolenaar {
402c243e490SMarcel Moolenaar     atf_check_result_t result1, result2;
403c243e490SMarcel Moolenaar     const char *out1, *out2;
404c243e490SMarcel Moolenaar     const char *err1, *err2;
405c243e490SMarcel Moolenaar 
406c243e490SMarcel Moolenaar     do_exec_with_arg(tc, "stdout-stderr", "result1", &result1);
407c243e490SMarcel Moolenaar     ATF_CHECK(atf_check_result_exited(&result1));
408c243e490SMarcel Moolenaar     ATF_CHECK(atf_check_result_exitcode(&result1) == EXIT_SUCCESS);
409c243e490SMarcel Moolenaar 
410c243e490SMarcel Moolenaar     do_exec_with_arg(tc, "stdout-stderr", "result2", &result2);
411c243e490SMarcel Moolenaar     ATF_CHECK(atf_check_result_exited(&result2));
412c243e490SMarcel Moolenaar     ATF_CHECK(atf_check_result_exitcode(&result2) == EXIT_SUCCESS);
413c243e490SMarcel Moolenaar 
414c243e490SMarcel Moolenaar     out1 = atf_check_result_stdout(&result1);
415c243e490SMarcel Moolenaar     out2 = atf_check_result_stdout(&result2);
416c243e490SMarcel Moolenaar     err1 = atf_check_result_stderr(&result1);
417c243e490SMarcel Moolenaar     err2 = atf_check_result_stderr(&result2);
418c243e490SMarcel Moolenaar 
419c243e490SMarcel Moolenaar     ATF_CHECK(strstr(out1, "check.XXXXXX") == NULL);
420c243e490SMarcel Moolenaar     ATF_CHECK(strstr(out2, "check.XXXXXX") == NULL);
421c243e490SMarcel Moolenaar     ATF_CHECK(strstr(err1, "check.XXXXXX") == NULL);
422c243e490SMarcel Moolenaar     ATF_CHECK(strstr(err2, "check.XXXXXX") == NULL);
423c243e490SMarcel Moolenaar 
424c243e490SMarcel Moolenaar     ATF_CHECK(strstr(out1, "/check") != NULL);
425c243e490SMarcel Moolenaar     ATF_CHECK(strstr(out2, "/check") != NULL);
426c243e490SMarcel Moolenaar     ATF_CHECK(strstr(err1, "/check") != NULL);
427c243e490SMarcel Moolenaar     ATF_CHECK(strstr(err2, "/check") != NULL);
428c243e490SMarcel Moolenaar 
429c243e490SMarcel Moolenaar     ATF_CHECK(strstr(out1, "/stdout") != NULL);
430c243e490SMarcel Moolenaar     ATF_CHECK(strstr(out2, "/stdout") != NULL);
431c243e490SMarcel Moolenaar     ATF_CHECK(strstr(err1, "/stderr") != NULL);
432c243e490SMarcel Moolenaar     ATF_CHECK(strstr(err2, "/stderr") != NULL);
433c243e490SMarcel Moolenaar 
434c243e490SMarcel Moolenaar     ATF_CHECK(strcmp(out1, out2) != 0);
435c243e490SMarcel Moolenaar     ATF_CHECK(strcmp(err1, err2) != 0);
436c243e490SMarcel Moolenaar 
437c243e490SMarcel Moolenaar #define CHECK_LINES(path, outname, resname) \
438c243e490SMarcel Moolenaar     do { \
439c243e490SMarcel Moolenaar         int fd = open(path, O_RDONLY); \
440c243e490SMarcel Moolenaar         ATF_CHECK(fd != -1); \
441c243e490SMarcel Moolenaar         check_line(fd, "Line 1 to " outname " for " resname); \
442c243e490SMarcel Moolenaar         check_line(fd, "Line 2 to " outname " for " resname); \
443c243e490SMarcel Moolenaar         close(fd); \
444c243e490SMarcel Moolenaar     } while (false)
445c243e490SMarcel Moolenaar 
446c243e490SMarcel Moolenaar     CHECK_LINES(out1, "stdout", "result1");
447c243e490SMarcel Moolenaar     CHECK_LINES(out2, "stdout", "result2");
448c243e490SMarcel Moolenaar     CHECK_LINES(err1, "stderr", "result1");
449c243e490SMarcel Moolenaar     CHECK_LINES(err2, "stderr", "result2");
450c243e490SMarcel Moolenaar 
451c243e490SMarcel Moolenaar #undef CHECK_LINES
452c243e490SMarcel Moolenaar 
453c243e490SMarcel Moolenaar     atf_check_result_fini(&result2);
454c243e490SMarcel Moolenaar     atf_check_result_fini(&result1);
455c243e490SMarcel Moolenaar }
456c243e490SMarcel Moolenaar 
457c243e490SMarcel Moolenaar ATF_TC(exec_umask);
ATF_TC_HEAD(exec_umask,tc)458c243e490SMarcel Moolenaar ATF_TC_HEAD(exec_umask, tc)
459c243e490SMarcel Moolenaar {
460c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
461c243e490SMarcel Moolenaar                       "correctly reports an error if the umask is too "
462c243e490SMarcel Moolenaar                       "restrictive to create temporary files");
463c243e490SMarcel Moolenaar }
ATF_TC_BODY(exec_umask,tc)464c243e490SMarcel Moolenaar ATF_TC_BODY(exec_umask, tc)
465c243e490SMarcel Moolenaar {
466c243e490SMarcel Moolenaar     atf_check_result_t result;
467c243e490SMarcel Moolenaar     atf_fs_path_t process_helpers;
468c243e490SMarcel Moolenaar     const char *argv[3];
469c243e490SMarcel Moolenaar 
470c243e490SMarcel Moolenaar     get_process_helpers_path(tc, false, &process_helpers);
471c243e490SMarcel Moolenaar     argv[0] = atf_fs_path_cstring(&process_helpers);
472c243e490SMarcel Moolenaar     argv[1] = "exit-success";
473c243e490SMarcel Moolenaar     argv[2] = NULL;
474c243e490SMarcel Moolenaar 
475c243e490SMarcel Moolenaar     umask(0222);
476c243e490SMarcel Moolenaar     atf_error_t err = atf_check_exec_array(argv, &result);
477c243e490SMarcel Moolenaar     ATF_CHECK(atf_is_error(err));
478c243e490SMarcel Moolenaar     ATF_CHECK(atf_error_is(err, "invalid_umask"));
479c243e490SMarcel Moolenaar     atf_error_free(err);
480c243e490SMarcel Moolenaar 
481c243e490SMarcel Moolenaar     atf_fs_path_fini(&process_helpers);
482c243e490SMarcel Moolenaar }
483c243e490SMarcel Moolenaar 
484c243e490SMarcel Moolenaar ATF_TC(exec_unknown);
ATF_TC_HEAD(exec_unknown,tc)485c243e490SMarcel Moolenaar ATF_TC_HEAD(exec_unknown, tc)
486c243e490SMarcel Moolenaar {
487c243e490SMarcel Moolenaar     atf_tc_set_md_var(tc, "descr", "Checks that running a non-existing "
488c243e490SMarcel Moolenaar                       "binary is handled correctly");
489c243e490SMarcel Moolenaar }
ATF_TC_BODY(exec_unknown,tc)490c243e490SMarcel Moolenaar ATF_TC_BODY(exec_unknown, tc)
491c243e490SMarcel Moolenaar {
492c243e490SMarcel Moolenaar     const char *argv[2];
493*0677dfd1SJulio Merino     argv[0] = "/foo/bar/non-existent";
494c243e490SMarcel Moolenaar     argv[1] = NULL;
495c243e490SMarcel Moolenaar 
496c243e490SMarcel Moolenaar     atf_check_result_t result;
497c243e490SMarcel Moolenaar     RE(atf_check_exec_array(argv, &result));
498c243e490SMarcel Moolenaar     ATF_CHECK(atf_check_result_exited(&result));
499c243e490SMarcel Moolenaar     ATF_CHECK(atf_check_result_exitcode(&result) == 127);
500c243e490SMarcel Moolenaar     atf_check_result_fini(&result);
501c243e490SMarcel Moolenaar }
502c243e490SMarcel Moolenaar 
503c243e490SMarcel Moolenaar /* ---------------------------------------------------------------------
504c243e490SMarcel Moolenaar  * Main.
505c243e490SMarcel Moolenaar  * --------------------------------------------------------------------- */
506c243e490SMarcel Moolenaar 
ATF_TP_ADD_TCS(tp)507c243e490SMarcel Moolenaar ATF_TP_ADD_TCS(tp)
508c243e490SMarcel Moolenaar {
509c243e490SMarcel Moolenaar     /* Add the test cases for the free functions. */
510c243e490SMarcel Moolenaar     ATF_TP_ADD_TC(tp, build_c_o);
511c243e490SMarcel Moolenaar     ATF_TP_ADD_TC(tp, build_cpp);
512c243e490SMarcel Moolenaar     ATF_TP_ADD_TC(tp, build_cxx_o);
513c243e490SMarcel Moolenaar     ATF_TP_ADD_TC(tp, exec_array);
514c243e490SMarcel Moolenaar     ATF_TP_ADD_TC(tp, exec_cleanup);
515c243e490SMarcel Moolenaar     ATF_TP_ADD_TC(tp, exec_exitstatus);
516c243e490SMarcel Moolenaar     ATF_TP_ADD_TC(tp, exec_stdout_stderr);
517c243e490SMarcel Moolenaar     ATF_TP_ADD_TC(tp, exec_umask);
518c243e490SMarcel Moolenaar     ATF_TP_ADD_TC(tp, exec_unknown);
519c243e490SMarcel Moolenaar 
520c243e490SMarcel Moolenaar     return atf_no_error();
521c243e490SMarcel Moolenaar }
522