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