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 <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34
35 #include "atf-c/build.h"
36 #include "atf-c/check.h"
37 #include "atf-c/config.h"
38 #include "atf-c/error.h"
39 #include "atf-c/macros.h"
40
41 #include "dynstr.h"
42 #include "fs.h"
43 #include "process.h"
44 #include "test_helpers.h"
45
46 bool
build_check_c_o(const char * path)47 build_check_c_o(const char *path)
48 {
49 bool success;
50 atf_dynstr_t iflag;
51 const char *optargs[4];
52
53 RE(atf_dynstr_init_fmt(&iflag, "-I%s", atf_config_get("atf_includedir")));
54
55 optargs[0] = atf_dynstr_cstring(&iflag);
56 optargs[1] = "-Wall";
57 optargs[2] = "-Werror";
58 optargs[3] = NULL;
59
60 RE(atf_check_build_c_o(path, "test.o", optargs, &success));
61
62 atf_dynstr_fini(&iflag);
63
64 return success;
65 }
66
67 bool
build_check_c_o_srcdir(const atf_tc_t * tc,const char * sfile)68 build_check_c_o_srcdir(const atf_tc_t *tc, const char *sfile)
69 {
70 atf_fs_path_t path;
71
72 RE(atf_fs_path_init_fmt(&path, "%s/%s",
73 atf_tc_get_config_var(tc, "srcdir"), sfile));
74 const bool result = build_check_c_o(atf_fs_path_cstring(&path));
75 atf_fs_path_fini(&path);
76 return result;
77 }
78
79 void
header_check(const char * hdrname)80 header_check(const char *hdrname)
81 {
82 FILE *srcfile;
83 char failmsg[128];
84
85 srcfile = fopen("test.c", "w");
86 ATF_REQUIRE(srcfile != NULL);
87 fprintf(srcfile, "#include <%s>\n", hdrname);
88 fclose(srcfile);
89
90 snprintf(failmsg, sizeof(failmsg),
91 "Header check failed; %s is not self-contained", hdrname);
92
93 if (!build_check_c_o("test.c"))
94 atf_tc_fail("%s", failmsg);
95 }
96
97 void
get_process_helpers_path(const atf_tc_t * tc,const bool is_detail,atf_fs_path_t * path)98 get_process_helpers_path(const atf_tc_t *tc, const bool is_detail,
99 atf_fs_path_t *path)
100 {
101 RE(atf_fs_path_init_fmt(path, "%s/%sprocess_helpers",
102 atf_tc_get_config_var(tc, "srcdir"),
103 is_detail ? "" : "detail/"));
104 }
105
106 struct run_h_tc_data {
107 atf_tc_t *m_tc;
108 const char *m_resname;
109 };
110
111 static
112 void
run_h_tc_child(void * v)113 run_h_tc_child(void *v)
114 {
115 struct run_h_tc_data *data = (struct run_h_tc_data *)v;
116
117 RE(atf_tc_run(data->m_tc, data->m_resname));
118 }
119
120 /* TODO: Investigate if it's worth to add this functionality as part of
121 * the public API. I.e. a function to easily run a test case body in a
122 * subprocess. */
123 void
run_h_tc(atf_tc_t * tc,const char * outname,const char * errname,const char * resname)124 run_h_tc(atf_tc_t *tc, const char *outname, const char *errname,
125 const char *resname)
126 {
127 atf_fs_path_t outpath, errpath;
128 atf_process_stream_t outb, errb;
129 atf_process_child_t child;
130 atf_process_status_t status;
131
132 RE(atf_fs_path_init_fmt(&outpath, "%s", outname));
133 RE(atf_fs_path_init_fmt(&errpath, "%s", errname));
134
135 struct run_h_tc_data data = { tc, resname };
136
137 RE(atf_process_stream_init_redirect_path(&outb, &outpath));
138 RE(atf_process_stream_init_redirect_path(&errb, &errpath));
139 RE(atf_process_fork(&child, run_h_tc_child, &outb, &errb, &data));
140 atf_process_stream_fini(&errb);
141 atf_process_stream_fini(&outb);
142
143 RE(atf_process_child_wait(&child, &status));
144 ATF_CHECK(atf_process_status_exited(&status));
145 atf_process_status_fini(&status);
146
147 atf_fs_path_fini(&errpath);
148 atf_fs_path_fini(&outpath);
149 }
150