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