xref: /freebsd/contrib/atf/atf-c/detail/test_helpers.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25 
26 #include "atf-c/detail/test_helpers.h"
27 
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 
33 #include <atf-c.h>
34 
35 #include "atf-c/build.h"
36 #include "atf-c/check.h"
37 #include "atf-c/detail/dynstr.h"
38 #include "atf-c/detail/env.h"
39 #include "atf-c/detail/fs.h"
40 #include "atf-c/detail/process.h"
41 #include "atf-c/error.h"
42 
43 bool
44 build_check_c_o(const char *path)
45 {
46     bool success;
47     atf_dynstr_t iflag;
48     const char *optargs[4];
49 
50     RE(atf_dynstr_init_fmt(&iflag, "-I%s", atf_env_get_with_default(
51         "ATF_INCLUDEDIR", ATF_INCLUDEDIR)));
52 
53     optargs[0] = atf_dynstr_cstring(&iflag);
54     optargs[1] = "-Wall";
55     optargs[2] = "-Werror";
56     optargs[3] = NULL;
57 
58     RE(atf_check_build_c_o(path, "test.o", optargs, &success));
59 
60     atf_dynstr_fini(&iflag);
61 
62     return success;
63 }
64 
65 bool
66 build_check_c_o_srcdir(const atf_tc_t *tc, const char *sfile)
67 {
68     atf_fs_path_t path;
69 
70     RE(atf_fs_path_init_fmt(&path, "%s/%s",
71                             atf_tc_get_config_var(tc, "srcdir"), sfile));
72     const bool result = build_check_c_o(atf_fs_path_cstring(&path));
73     atf_fs_path_fini(&path);
74     return result;
75 }
76 
77 void
78 header_check(const char *hdrname)
79 {
80     FILE *srcfile;
81     char failmsg[128];
82 
83     srcfile = fopen("test.c", "w");
84     ATF_REQUIRE(srcfile != NULL);
85     fprintf(srcfile, "#include <%s>\n", hdrname);
86     fclose(srcfile);
87 
88     snprintf(failmsg, sizeof(failmsg),
89              "Header check failed; %s is not self-contained", hdrname);
90 
91     if (!build_check_c_o("test.c"))
92         atf_tc_fail("%s", failmsg);
93 }
94 
95 void
96 get_process_helpers_path(const atf_tc_t *tc, const bool is_detail,
97                          atf_fs_path_t *path)
98 {
99     RE(atf_fs_path_init_fmt(path, "%s/%sprocess_helpers",
100                             atf_tc_get_config_var(tc, "srcdir"),
101                             is_detail ? "" : "detail/"));
102 }
103 
104 struct run_h_tc_data {
105     atf_tc_t *m_tc;
106     const char *m_resname;
107 };
108 
109 static
110 void
111 run_h_tc_child(void *v)
112 {
113     struct run_h_tc_data *data = (struct run_h_tc_data *)v;
114 
115     RE(atf_tc_run(data->m_tc, data->m_resname));
116 }
117 
118 /* TODO: Investigate if it's worth to add this functionality as part of
119  * the public API.  I.e. a function to easily run a test case body in a
120  * subprocess. */
121 void
122 run_h_tc(atf_tc_t *tc, const char *outname, const char *errname,
123          const char *resname)
124 {
125     atf_fs_path_t outpath, errpath;
126     atf_process_stream_t outb, errb;
127     atf_process_child_t child;
128     atf_process_status_t status;
129 
130     RE(atf_fs_path_init_fmt(&outpath, outname));
131     RE(atf_fs_path_init_fmt(&errpath, errname));
132 
133     struct run_h_tc_data data = { tc, resname };
134 
135     RE(atf_process_stream_init_redirect_path(&outb, &outpath));
136     RE(atf_process_stream_init_redirect_path(&errb, &errpath));
137     RE(atf_process_fork(&child, run_h_tc_child, &outb, &errb, &data));
138     atf_process_stream_fini(&errb);
139     atf_process_stream_fini(&outb);
140 
141     RE(atf_process_child_wait(&child, &status));
142     ATF_CHECK(atf_process_status_exited(&status));
143     atf_process_status_fini(&status);
144 
145     atf_fs_path_fini(&errpath);
146     atf_fs_path_fini(&outpath);
147 }
148