1/*	$NetBSD$	*/
2
3/*
4 * Automated Testing Framework (atf)
5 *
6 * Copyright (c) 2008, 2009, 2010 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 <regex.h>
36#include <unistd.h>
37
38#include "atf-c/build.h"
39#include "atf-c/check.h"
40#include "atf-c/config.h"
41#include "atf-c/error.h"
42#include "atf-c/macros.h"
43
44#include "dynstr.h"
45#include "fs.h"
46#include "process.h"
47#include "test_helpers.h"
48
49static
50void
51build_check_c_o_aux(const char *path, const char *failmsg)
52{
53    bool success;
54    atf_dynstr_t iflag;
55    const char *optargs[2];
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] = NULL;
61
62    RE(atf_check_build_c_o(path, "test.o", optargs, &success));
63
64    atf_dynstr_fini(&iflag);
65
66    if (!success)
67        atf_tc_fail(failmsg);
68}
69
70void
71build_check_c_o(const atf_tc_t *tc, const char *sfile, const char *failmsg)
72{
73    atf_fs_path_t path;
74
75    RE(atf_fs_path_init_fmt(&path, "%s/%s",
76                            atf_tc_get_config_var(tc, "srcdir"), sfile));
77    build_check_c_o_aux(atf_fs_path_cstring(&path), failmsg);
78    atf_fs_path_fini(&path);
79}
80
81void
82header_check(const atf_tc_t *tc, const char *hdrname)
83{
84    FILE *srcfile;
85    char failmsg[128];
86
87    srcfile = fopen("test.c", "w");
88    ATF_REQUIRE(srcfile != NULL);
89    fprintf(srcfile, "#include <%s>\n", hdrname);
90    fclose(srcfile);
91
92    snprintf(failmsg, sizeof(failmsg),
93             "Header check failed; %s is not self-contained", hdrname);
94
95    build_check_c_o_aux("test.c", failmsg);
96}
97
98void
99get_process_helpers_path(const atf_tc_t *tc, const bool is_detail,
100                         atf_fs_path_t *path)
101{
102    RE(atf_fs_path_init_fmt(path, "%s/%sprocess_helpers",
103                            atf_tc_get_config_var(tc, "srcdir"),
104                            is_detail ? "" : "detail/"));
105}
106
107bool
108grep_string(const atf_dynstr_t *str, const char *regex)
109{
110    int res;
111    regex_t preg;
112
113    printf("Looking for '%s' in '%s'\n", regex, atf_dynstr_cstring(str));
114    ATF_REQUIRE(regcomp(&preg, regex, REG_EXTENDED) == 0);
115
116    res = regexec(&preg, atf_dynstr_cstring(str), 0, NULL, 0);
117    ATF_REQUIRE(res == 0 || res == REG_NOMATCH);
118
119    regfree(&preg);
120
121    return res == 0;
122}
123
124bool
125grep_file(const char *file, const char *regex, ...)
126{
127    bool done, found;
128    int fd;
129    va_list ap;
130    atf_dynstr_t formatted;
131
132    va_start(ap, regex);
133    RE(atf_dynstr_init_ap(&formatted, regex, ap));
134    va_end(ap);
135
136    done = false;
137    found = false;
138    ATF_REQUIRE((fd = open(file, O_RDONLY)) != -1);
139    do {
140        atf_dynstr_t line;
141
142        RE(atf_dynstr_init(&line));
143
144        done = read_line(fd, &line);
145        if (!done)
146            found = grep_string(&line, atf_dynstr_cstring(&formatted));
147
148        atf_dynstr_fini(&line);
149    } while (!found && !done);
150    close(fd);
151
152    atf_dynstr_fini(&formatted);
153
154    return found;
155}
156
157bool
158read_line(int fd, atf_dynstr_t *dest)
159{
160    char ch;
161    ssize_t cnt;
162
163    while ((cnt = read(fd, &ch, sizeof(ch))) == sizeof(ch) &&
164           ch != '\n') {
165        const atf_error_t err = atf_dynstr_append_fmt(dest, "%c", ch);
166        ATF_REQUIRE(!atf_is_error(err));
167    }
168    ATF_REQUIRE(cnt != -1);
169
170    return cnt == 0;
171}
172
173struct run_h_tc_data {
174    atf_tc_t *m_tc;
175    const char *m_resname;
176};
177
178static
179void
180run_h_tc_child(void *v)
181{
182    struct run_h_tc_data *data = (struct run_h_tc_data *)v;
183
184    RE(atf_tc_run(data->m_tc, data->m_resname));
185}
186
187/* TODO: Investigate if it's worth to add this functionality as part of
188 * the public API.  I.e. a function to easily run a test case body in a
189 * subprocess. */
190void
191run_h_tc(atf_tc_t *tc, const char *outname, const char *errname,
192         const char *resname)
193{
194    atf_fs_path_t outpath, errpath;
195    atf_process_stream_t outb, errb;
196    atf_process_child_t child;
197    atf_process_status_t status;
198
199    RE(atf_fs_path_init_fmt(&outpath, outname));
200    RE(atf_fs_path_init_fmt(&errpath, errname));
201
202    struct run_h_tc_data data = { tc, resname };
203
204    RE(atf_process_stream_init_redirect_path(&outb, &outpath));
205    RE(atf_process_stream_init_redirect_path(&errb, &errpath));
206    RE(atf_process_fork(&child, run_h_tc_child, &outb, &errb, &data));
207    atf_process_stream_fini(&errb);
208    atf_process_stream_fini(&outb);
209
210    RE(atf_process_child_wait(&child, &status));
211    ATF_CHECK(atf_process_status_exited(&status));
212    atf_process_status_fini(&status);
213
214    atf_fs_path_fini(&errpath);
215    atf_fs_path_fini(&outpath);
216}
217