xref: /minix/external/bsd/atf/dist/atf-c/check.c (revision cd34841d)
1 /*
2  * Automated Testing Framework (atf)
3  *
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/wait.h>
31 
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #include "atf-c/build.h"
40 #include "atf-c/check.h"
41 #include "atf-c/config.h"
42 #include "atf-c/defs.h"
43 #include "atf-c/error.h"
44 #include "atf-c/utils.h"
45 
46 #include "detail/dynstr.h"
47 #include "detail/fs.h"
48 #include "detail/list.h"
49 #include "detail/process.h"
50 #include "detail/sanity.h"
51 
52 /* ---------------------------------------------------------------------
53  * Auxiliary functions.
54  * --------------------------------------------------------------------- */
55 
56 static
57 atf_error_t
58 create_tmpdir(atf_fs_path_t *dir)
59 {
60     atf_error_t err;
61 
62     err = atf_fs_path_init_fmt(dir, "%s/check.XXXXXX",
63                                atf_config_get("atf_workdir"));
64     if (atf_is_error(err))
65         goto out;
66 
67     err = atf_fs_mkdtemp(dir);
68     if (atf_is_error(err)) {
69         atf_fs_path_fini(dir);
70         goto out;
71     }
72 
73     INV(!atf_is_error(err));
74 out:
75     return err;
76 }
77 
78 static
79 void
80 cleanup_tmpdir(const atf_fs_path_t *dir, const atf_fs_path_t *outfile,
81                const atf_fs_path_t *errfile)
82 {
83     {
84         atf_error_t err = atf_fs_unlink(outfile);
85         if (atf_is_error(err)) {
86             INV(atf_error_is(err, "libc") &&
87                 atf_libc_error_code(err) == ENOENT);
88             atf_error_free(err);
89         } else
90             INV(!atf_is_error(err));
91     }
92 
93     {
94         atf_error_t err = atf_fs_unlink(errfile);
95         if (atf_is_error(err)) {
96             INV(atf_error_is(err, "libc") &&
97                 atf_libc_error_code(err) == ENOENT);
98             atf_error_free(err);
99         } else
100             INV(!atf_is_error(err));
101     }
102 
103     {
104 #if defined(__minix) && !defined(NDEBUG)
105         atf_error_t err =
106 #endif /* defined(__minix) && !defined(NDEBUG) */
107         atf_fs_rmdir(dir);
108         INV(!atf_is_error(err));
109     }
110 }
111 
112 static
113 int
114 const_execvp(const char *file, const char *const *argv)
115 {
116 #define UNCONST(a) ((void *)(unsigned long)(const void *)(a))
117     return execvp(file, UNCONST(argv));
118 #undef UNCONST
119 }
120 
121 static
122 atf_error_t
123 init_sb(const atf_fs_path_t *path, atf_process_stream_t *sb)
124 {
125     atf_error_t err;
126 
127     if (path == NULL)
128         err = atf_process_stream_init_inherit(sb);
129     else
130         err = atf_process_stream_init_redirect_path(sb, path);
131 
132     return err;
133 }
134 
135 static
136 atf_error_t
137 init_sbs(const atf_fs_path_t *outfile, atf_process_stream_t *outsb,
138          const atf_fs_path_t *errfile, atf_process_stream_t *errsb)
139 {
140     atf_error_t err;
141 
142     err = init_sb(outfile, outsb);
143     if (atf_is_error(err))
144         goto out;
145 
146     err = init_sb(errfile, errsb);
147     if (atf_is_error(err)) {
148         atf_process_stream_fini(outsb);
149         goto out;
150     }
151 
152 out:
153     return err;
154 }
155 
156 struct exec_data {
157     const char *const *m_argv;
158 };
159 
160 static void exec_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
161 
162 static
163 void
164 exec_child(void *v)
165 {
166     struct exec_data *ea = v;
167 
168     const_execvp(ea->m_argv[0], ea->m_argv);
169     fprintf(stderr, "execvp(%s) failed: %s\n", ea->m_argv[0], strerror(errno));
170     exit(127);
171 }
172 
173 static
174 atf_error_t
175 fork_and_wait(const char *const *argv, const atf_fs_path_t *outfile,
176               const atf_fs_path_t *errfile, atf_process_status_t *status)
177 {
178     atf_error_t err;
179     atf_process_child_t child;
180     atf_process_stream_t outsb, errsb;
181     struct exec_data ea = { argv };
182 
183     err = init_sbs(outfile, &outsb, errfile, &errsb);
184     if (atf_is_error(err))
185         goto out;
186 
187     err = atf_process_fork(&child, exec_child, &outsb, &errsb, &ea);
188     if (atf_is_error(err))
189         goto out_sbs;
190 
191     err = atf_process_child_wait(&child, status);
192 
193 out_sbs:
194     atf_process_stream_fini(&errsb);
195     atf_process_stream_fini(&outsb);
196 out:
197     return err;
198 }
199 
200 static
201 void
202 update_success_from_status(const char *progname,
203                            const atf_process_status_t *status, bool *success)
204 {
205     bool s = atf_process_status_exited(status) &&
206              atf_process_status_exitstatus(status) == EXIT_SUCCESS;
207 
208     if (atf_process_status_exited(status)) {
209         if (atf_process_status_exitstatus(status) == EXIT_SUCCESS)
210             INV(s);
211         else {
212             INV(!s);
213             fprintf(stderr, "%s failed with exit code %d\n", progname,
214                     atf_process_status_exitstatus(status));
215         }
216     } else if (atf_process_status_signaled(status)) {
217         INV(!s);
218         fprintf(stderr, "%s failed due to signal %d%s\n", progname,
219                 atf_process_status_termsig(status),
220                 atf_process_status_coredump(status) ? " (core dumped)" : "");
221     } else {
222         INV(!s);
223         fprintf(stderr, "%s failed due to unknown reason\n", progname);
224     }
225 
226     *success = s;
227 }
228 
229 static
230 atf_error_t
231 array_to_list(const char *const *a, atf_list_t *l)
232 {
233     atf_error_t err;
234 
235     err = atf_list_init(l);
236     if (atf_is_error(err))
237         goto out;
238 
239     while (*a != NULL) {
240         char *item = strdup(*a);
241         if (item == NULL) {
242             err = atf_no_memory_error();
243             goto out;
244         }
245 
246         err = atf_list_append(l, item, true);
247         if (atf_is_error(err))
248             goto out;
249 
250         a++;
251     }
252 
253 out:
254     return err;
255 }
256 
257 static void
258 print_array(const char *const *array, const char *pfx)
259 {
260     const char *const *ptr;
261 
262     printf("%s", pfx);
263     for (ptr = array; *ptr != NULL; ptr++)
264         printf(" %s", *ptr);
265     printf("\n");
266 }
267 
268 static
269 atf_error_t
270 check_build_run(const char *const *argv, bool *success)
271 {
272     atf_error_t err;
273     atf_process_status_t status;
274 
275     print_array(argv, ">");
276 
277     err = fork_and_wait(argv, NULL, NULL, &status);
278     if (atf_is_error(err))
279         goto out;
280 
281     update_success_from_status(argv[0], &status, success);
282     atf_process_status_fini(&status);
283 
284     INV(!atf_is_error(err));
285 out:
286     return err;
287 }
288 
289 /* ---------------------------------------------------------------------
290  * The "atf_check_result" type.
291  * --------------------------------------------------------------------- */
292 
293 struct atf_check_result_impl {
294     atf_list_t m_argv;
295     atf_fs_path_t m_dir;
296     atf_fs_path_t m_stdout;
297     atf_fs_path_t m_stderr;
298     atf_process_status_t m_status;
299 };
300 
301 static
302 atf_error_t
303 atf_check_result_init(atf_check_result_t *r, const char *const *argv,
304                       const atf_fs_path_t *dir)
305 {
306     atf_error_t err;
307 
308     r->pimpl = malloc(sizeof(struct atf_check_result_impl));
309     if (r->pimpl == NULL)
310         return atf_no_memory_error();
311 
312     err = array_to_list(argv, &r->pimpl->m_argv);
313     if (atf_is_error(err))
314         goto out;
315 
316     err = atf_fs_path_copy(&r->pimpl->m_dir, dir);
317     if (atf_is_error(err))
318         goto err_argv;
319 
320     err = atf_fs_path_init_fmt(&r->pimpl->m_stdout, "%s/stdout",
321                                atf_fs_path_cstring(dir));
322     if (atf_is_error(err))
323         goto err_dir;
324 
325     err = atf_fs_path_init_fmt(&r->pimpl->m_stderr, "%s/stderr",
326                                atf_fs_path_cstring(dir));
327     if (atf_is_error(err))
328         goto err_stdout;
329 
330     INV(!atf_is_error(err));
331     goto out;
332 
333 err_stdout:
334     atf_fs_path_fini(&r->pimpl->m_stdout);
335 err_dir:
336     atf_fs_path_fini(&r->pimpl->m_dir);
337 err_argv:
338     atf_list_fini(&r->pimpl->m_argv);
339 out:
340     return err;
341 }
342 
343 void
344 atf_check_result_fini(atf_check_result_t *r)
345 {
346     atf_process_status_fini(&r->pimpl->m_status);
347 
348     cleanup_tmpdir(&r->pimpl->m_dir, &r->pimpl->m_stdout,
349                    &r->pimpl->m_stderr);
350     atf_fs_path_fini(&r->pimpl->m_stdout);
351     atf_fs_path_fini(&r->pimpl->m_stderr);
352     atf_fs_path_fini(&r->pimpl->m_dir);
353 
354     atf_list_fini(&r->pimpl->m_argv);
355 
356     free(r->pimpl);
357 }
358 
359 const char *
360 atf_check_result_stdout(const atf_check_result_t *r)
361 {
362     return atf_fs_path_cstring(&r->pimpl->m_stdout);
363 }
364 
365 const char *
366 atf_check_result_stderr(const atf_check_result_t *r)
367 {
368     return atf_fs_path_cstring(&r->pimpl->m_stderr);
369 }
370 
371 bool
372 atf_check_result_exited(const atf_check_result_t *r)
373 {
374     return atf_process_status_exited(&r->pimpl->m_status);
375 }
376 
377 int
378 atf_check_result_exitcode(const atf_check_result_t *r)
379 {
380     return atf_process_status_exitstatus(&r->pimpl->m_status);
381 }
382 
383 bool
384 atf_check_result_signaled(const atf_check_result_t *r)
385 {
386     return atf_process_status_signaled(&r->pimpl->m_status);
387 }
388 
389 int
390 atf_check_result_termsig(const atf_check_result_t *r)
391 {
392     return atf_process_status_termsig(&r->pimpl->m_status);
393 }
394 
395 /* ---------------------------------------------------------------------
396  * Free functions.
397  * --------------------------------------------------------------------- */
398 
399 /* XXX: This function shouldn't be in this module.  It messes with stdout
400  * and stderr, and it provides a very high-end interface.  This belongs,
401  * probably, somewhere related to test cases (such as in the tc module). */
402 atf_error_t
403 atf_check_build_c_o(const char *sfile,
404                     const char *ofile,
405                     const char *const optargs[],
406                     bool *success)
407 {
408     atf_error_t err;
409     char **argv;
410 
411     err = atf_build_c_o(sfile, ofile, optargs, &argv);
412     if (atf_is_error(err))
413         goto out;
414 
415     err = check_build_run((const char *const *)argv, success);
416 
417     atf_utils_free_charpp(argv);
418 out:
419     return err;
420 }
421 
422 atf_error_t
423 atf_check_build_cpp(const char *sfile,
424                     const char *ofile,
425                     const char *const optargs[],
426                     bool *success)
427 {
428     atf_error_t err;
429     char **argv;
430 
431     err = atf_build_cpp(sfile, ofile, optargs, &argv);
432     if (atf_is_error(err))
433         goto out;
434 
435     err = check_build_run((const char *const *)argv, success);
436 
437     atf_utils_free_charpp(argv);
438 out:
439     return err;
440 }
441 
442 atf_error_t
443 atf_check_build_cxx_o(const char *sfile,
444                       const char *ofile,
445                       const char *const optargs[],
446                       bool *success)
447 {
448     atf_error_t err;
449     char **argv;
450 
451     err = atf_build_cxx_o(sfile, ofile, optargs, &argv);
452     if (atf_is_error(err))
453         goto out;
454 
455     err = check_build_run((const char *const *)argv, success);
456 
457     atf_utils_free_charpp(argv);
458 out:
459     return err;
460 }
461 
462 atf_error_t
463 atf_check_exec_array(const char *const *argv, atf_check_result_t *r)
464 {
465     atf_error_t err;
466     atf_fs_path_t dir;
467 
468     err = create_tmpdir(&dir);
469     if (atf_is_error(err))
470         goto out;
471 
472     err = atf_check_result_init(r, argv, &dir);
473     if (atf_is_error(err)) {
474 #if defined(__minix) && !defined(NDEBUG)
475         atf_error_t err2 =
476 #endif /* defined(__minix) && !defined(NDEBUG) */
477         atf_fs_rmdir(&dir);
478         INV(!atf_is_error(err2));
479         goto out;
480     }
481 
482     err = fork_and_wait(argv, &r->pimpl->m_stdout, &r->pimpl->m_stderr,
483                         &r->pimpl->m_status);
484     if (atf_is_error(err)) {
485         atf_check_result_fini(r);
486         goto out;
487     }
488 
489     INV(!atf_is_error(err));
490 
491     atf_fs_path_fini(&dir);
492 out:
493     return err;
494 }
495