1 /* $NetBSD: build_test.c,v 1.1.1.3 2014/12/10 03:34:48 christos Exp $ */ 2 3 /* 4 * Automated Testing Framework (atf) 5 * 6 * Copyright (c) 2009 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 <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 36 #include <atf-c.h> 37 38 #include "atf-c/build.h" 39 #include "atf-c/config.h" 40 #include "atf-c/utils.h" 41 42 #include "detail/env.h" 43 #include "detail/test_helpers.h" 44 #include "h_build.h" 45 46 /* --------------------------------------------------------------------- 47 * Auxiliary functions. 48 * --------------------------------------------------------------------- */ 49 50 void __atf_config_reinit(void); 51 52 static 53 bool 54 equal_arrays(const char *const *exp_array, char **actual_array) 55 { 56 bool equal; 57 58 if (*exp_array == NULL && *actual_array == NULL) 59 equal = true; 60 else if (*exp_array == NULL || *actual_array == NULL) 61 equal = false; 62 else { 63 equal = true; 64 while (*actual_array != NULL) { 65 if (*exp_array == NULL || strcmp(*exp_array, *actual_array) != 0) { 66 equal = false; 67 break; 68 } 69 exp_array++; 70 actual_array++; 71 } 72 } 73 74 return equal; 75 } 76 77 static 78 void 79 check_equal_array(const char *const *exp_array, char **actual_array) 80 { 81 { 82 const char *const *exp_ptr; 83 printf("Expected arguments:"); 84 for (exp_ptr = exp_array; *exp_ptr != NULL; exp_ptr++) 85 printf(" '%s'", *exp_ptr); 86 printf("\n"); 87 } 88 89 { 90 char **actual_ptr; 91 printf("Returned arguments:"); 92 for (actual_ptr = actual_array; *actual_ptr != NULL; actual_ptr++) 93 printf(" '%s'", *actual_ptr); 94 printf("\n"); 95 } 96 97 if (!equal_arrays(exp_array, actual_array)) 98 atf_tc_fail_nonfatal("The constructed argv differs from the " 99 "expected values"); 100 } 101 102 static 103 void 104 verbose_set_env(const char *var, const char *val) 105 { 106 printf("Setting %s to '%s'\n", var, val); 107 RE(atf_env_set(var, val)); 108 } 109 110 /* --------------------------------------------------------------------- 111 * Internal test cases. 112 * --------------------------------------------------------------------- */ 113 114 ATF_TC(equal_arrays); 115 ATF_TC_HEAD(equal_arrays, tc) 116 { 117 atf_tc_set_md_var(tc, "descr", "Tests the test case internal " 118 "equal_arrays function"); 119 } 120 ATF_TC_BODY(equal_arrays, tc) 121 { 122 { 123 const char *const exp[] = { NULL }; 124 char *actual[] = { NULL }; 125 126 ATF_CHECK(equal_arrays(exp, actual)); 127 } 128 129 { 130 const char *const exp[] = { NULL }; 131 char *actual[2] = { strdup("foo"), NULL }; 132 133 ATF_CHECK(!equal_arrays(exp, actual)); 134 free(actual[0]); 135 } 136 137 { 138 const char *const exp[] = { "foo", NULL }; 139 char *actual[] = { NULL }; 140 141 ATF_CHECK(!equal_arrays(exp, actual)); 142 } 143 144 { 145 const char *const exp[] = { "foo", NULL }; 146 char *actual[2] = { strdup("foo"), NULL }; 147 148 ATF_CHECK(equal_arrays(exp, actual)); 149 free(actual[0]); 150 } 151 } 152 153 /* --------------------------------------------------------------------- 154 * Test cases for the free functions. 155 * --------------------------------------------------------------------- */ 156 157 ATF_TC(c_o); 158 ATF_TC_HEAD(c_o, tc) 159 { 160 atf_tc_set_md_var(tc, "descr", "Tests the atf_build_c_o function"); 161 } 162 ATF_TC_BODY(c_o, tc) 163 { 164 struct c_o_test *test; 165 166 for (test = c_o_tests; test->expargv[0] != NULL; test++) { 167 printf("> Test: %s\n", test->msg); 168 169 verbose_set_env("ATF_BUILD_CC", test->cc); 170 verbose_set_env("ATF_BUILD_CFLAGS", test->cflags); 171 verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags); 172 __atf_config_reinit(); 173 174 { 175 char **argv; 176 if (test->hasoptargs) 177 RE(atf_build_c_o(test->sfile, test->ofile, test->optargs, 178 &argv)); 179 else 180 RE(atf_build_c_o(test->sfile, test->ofile, NULL, &argv)); 181 check_equal_array(test->expargv, argv); 182 atf_utils_free_charpp(argv); 183 } 184 } 185 } 186 187 ATF_TC(cpp); 188 ATF_TC_HEAD(cpp, tc) 189 { 190 atf_tc_set_md_var(tc, "descr", "Tests the atf_build_cpp function"); 191 } 192 ATF_TC_BODY(cpp, tc) 193 { 194 struct cpp_test *test; 195 196 for (test = cpp_tests; test->expargv[0] != NULL; test++) { 197 printf("> Test: %s\n", test->msg); 198 199 verbose_set_env("ATF_BUILD_CPP", test->cpp); 200 verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags); 201 __atf_config_reinit(); 202 203 { 204 char **argv; 205 if (test->hasoptargs) 206 RE(atf_build_cpp(test->sfile, test->ofile, test->optargs, 207 &argv)); 208 else 209 RE(atf_build_cpp(test->sfile, test->ofile, NULL, &argv)); 210 check_equal_array(test->expargv, argv); 211 atf_utils_free_charpp(argv); 212 } 213 } 214 } 215 216 ATF_TC(cxx_o); 217 ATF_TC_HEAD(cxx_o, tc) 218 { 219 atf_tc_set_md_var(tc, "descr", "Tests the atf_build_cxx_o function"); 220 } 221 ATF_TC_BODY(cxx_o, tc) 222 { 223 struct cxx_o_test *test; 224 225 for (test = cxx_o_tests; test->expargv[0] != NULL; test++) { 226 printf("> Test: %s\n", test->msg); 227 228 verbose_set_env("ATF_BUILD_CXX", test->cxx); 229 verbose_set_env("ATF_BUILD_CXXFLAGS", test->cxxflags); 230 verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags); 231 __atf_config_reinit(); 232 233 { 234 char **argv; 235 if (test->hasoptargs) 236 RE(atf_build_cxx_o(test->sfile, test->ofile, test->optargs, 237 &argv)); 238 else 239 RE(atf_build_cxx_o(test->sfile, test->ofile, NULL, &argv)); 240 check_equal_array(test->expargv, argv); 241 atf_utils_free_charpp(argv); 242 } 243 } 244 } 245 246 /* --------------------------------------------------------------------- 247 * Tests cases for the header file. 248 * --------------------------------------------------------------------- */ 249 250 HEADER_TC(include, "atf-c/build.h"); 251 252 /* --------------------------------------------------------------------- 253 * Main. 254 * --------------------------------------------------------------------- */ 255 256 ATF_TP_ADD_TCS(tp) 257 { 258 /* Add the internal test cases. */ 259 ATF_TP_ADD_TC(tp, equal_arrays); 260 261 /* Add the test cases for the free functions. */ 262 ATF_TP_ADD_TC(tp, c_o); 263 ATF_TP_ADD_TC(tp, cpp); 264 ATF_TP_ADD_TC(tp, cxx_o); 265 266 /* Add the test cases for the header file. */ 267 ATF_TP_ADD_TC(tp, include); 268 269 return atf_no_error(); 270 } 271