1239281Sgonzo/*
2239281Sgonzo * Automated Testing Framework (atf)
3239281Sgonzo *
4239281Sgonzo * Copyright (c) 2009 The NetBSD Foundation, Inc.
5239281Sgonzo * All rights reserved.
6239281Sgonzo *
7239281Sgonzo * Redistribution and use in source and binary forms, with or without
8239281Sgonzo * modification, are permitted provided that the following conditions
9239281Sgonzo * are met:
10239281Sgonzo * 1. Redistributions of source code must retain the above copyright
11239281Sgonzo *    notice, this list of conditions and the following disclaimer.
12239281Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
13239281Sgonzo *    notice, this list of conditions and the following disclaimer in the
14239281Sgonzo *    documentation and/or other materials provided with the distribution.
15239281Sgonzo *
16239281Sgonzo * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17239281Sgonzo * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18239281Sgonzo * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19239281Sgonzo * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20239281Sgonzo * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21239281Sgonzo * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22239281Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23239281Sgonzo * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24239281Sgonzo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25239281Sgonzo * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26239281Sgonzo * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27239281Sgonzo * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28239281Sgonzo */
29239281Sgonzo
30239281Sgonzo#include <stdio.h>
31239281Sgonzo#include <stdlib.h>
32239281Sgonzo#include <string.h>
33239281Sgonzo
34239281Sgonzo#include <atf-c.h>
35239281Sgonzo
36239281Sgonzo#include "atf-c/build.h"
37239281Sgonzo#include "atf-c/config.h"
38239281Sgonzo#include "atf-c/utils.h"
39239281Sgonzo
40239281Sgonzo#include "detail/env.h"
41239281Sgonzo#include "detail/test_helpers.h"
42239281Sgonzo#include "h_build.h"
43239281Sgonzo
44239281Sgonzo/* ---------------------------------------------------------------------
45239281Sgonzo * Auxiliary functions.
46239281Sgonzo * --------------------------------------------------------------------- */
47239281Sgonzo
48239281Sgonzovoid __atf_config_reinit(void);
49239281Sgonzo
50239281Sgonzostatic
51239281Sgonzobool
52239281Sgonzoequal_arrays(const char *const *exp_array, char **actual_array)
53239281Sgonzo{
54239281Sgonzo    bool equal;
55239281Sgonzo
56239281Sgonzo    if (*exp_array == NULL && *actual_array == NULL)
57239281Sgonzo        equal = true;
58239281Sgonzo    else if (*exp_array == NULL || *actual_array == NULL)
59239281Sgonzo        equal = false;
60239281Sgonzo    else {
61239281Sgonzo        equal = true;
62239281Sgonzo        while (*actual_array != NULL) {
63239281Sgonzo            if (*exp_array == NULL || strcmp(*exp_array, *actual_array) != 0) {
64239281Sgonzo                equal = false;
65239281Sgonzo                break;
66239281Sgonzo            }
67239281Sgonzo            exp_array++;
68239281Sgonzo            actual_array++;
69239281Sgonzo        }
70239281Sgonzo    }
71239281Sgonzo
72239281Sgonzo    return equal;
73239281Sgonzo}
74239281Sgonzo
75239281Sgonzostatic
76239281Sgonzovoid
77239281Sgonzocheck_equal_array(const char *const *exp_array, char **actual_array)
78239281Sgonzo{
79239281Sgonzo    {
80239281Sgonzo        const char *const *exp_ptr;
81239281Sgonzo        printf("Expected arguments:");
82239281Sgonzo        for (exp_ptr = exp_array; *exp_ptr != NULL; exp_ptr++)
83239281Sgonzo            printf(" '%s'", *exp_ptr);
84239281Sgonzo        printf("\n");
85239281Sgonzo    }
86239281Sgonzo
87239281Sgonzo    {
88239281Sgonzo        char **actual_ptr;
89239281Sgonzo        printf("Returned arguments:");
90239281Sgonzo        for (actual_ptr = actual_array; *actual_ptr != NULL; actual_ptr++)
91239281Sgonzo            printf(" '%s'", *actual_ptr);
92239281Sgonzo        printf("\n");
93239281Sgonzo    }
94239281Sgonzo
95239281Sgonzo    if (!equal_arrays(exp_array, actual_array))
96239281Sgonzo        atf_tc_fail_nonfatal("The constructed argv differs from the "
97239281Sgonzo                             "expected values");
98239281Sgonzo}
99
100static
101void
102verbose_set_env(const char *var, const char *val)
103{
104    printf("Setting %s to '%s'\n", var, val);
105    RE(atf_env_set(var, val));
106}
107
108/* ---------------------------------------------------------------------
109 * Internal test cases.
110 * --------------------------------------------------------------------- */
111
112ATF_TC(equal_arrays);
113ATF_TC_HEAD(equal_arrays, tc)
114{
115    atf_tc_set_md_var(tc, "descr", "Tests the test case internal "
116                      "equal_arrays function");
117}
118ATF_TC_BODY(equal_arrays, tc)
119{
120    {
121        const char *const exp[] = { NULL };
122        char *actual[] = { NULL };
123
124        ATF_CHECK(equal_arrays(exp, actual));
125    }
126
127    {
128        const char *const exp[] = { NULL };
129        char *actual[2] = { strdup("foo"), NULL };
130
131        ATF_CHECK(!equal_arrays(exp, actual));
132        free(actual[0]);
133    }
134
135    {
136        const char *const exp[] = { "foo", NULL };
137        char *actual[] = { NULL };
138
139        ATF_CHECK(!equal_arrays(exp, actual));
140    }
141
142    {
143        const char *const exp[] = { "foo", NULL };
144        char *actual[2] = { strdup("foo"), NULL };
145
146        ATF_CHECK(equal_arrays(exp, actual));
147        free(actual[0]);
148    }
149}
150
151/* ---------------------------------------------------------------------
152 * Test cases for the free functions.
153 * --------------------------------------------------------------------- */
154
155ATF_TC(c_o);
156ATF_TC_HEAD(c_o, tc)
157{
158    atf_tc_set_md_var(tc, "descr", "Tests the atf_build_c_o function");
159}
160ATF_TC_BODY(c_o, tc)
161{
162    struct c_o_test *test;
163
164    for (test = c_o_tests; test->expargv[0] != NULL; test++) {
165        printf("> Test: %s\n", test->msg);
166
167        verbose_set_env("ATF_BUILD_CC", test->cc);
168        verbose_set_env("ATF_BUILD_CFLAGS", test->cflags);
169        verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
170        __atf_config_reinit();
171
172        {
173            char **argv;
174            if (test->hasoptargs)
175                RE(atf_build_c_o(test->sfile, test->ofile, test->optargs,
176                                 &argv));
177            else
178                RE(atf_build_c_o(test->sfile, test->ofile, NULL, &argv));
179            check_equal_array(test->expargv, argv);
180            atf_utils_free_charpp(argv);
181        }
182    }
183}
184
185ATF_TC(cpp);
186ATF_TC_HEAD(cpp, tc)
187{
188    atf_tc_set_md_var(tc, "descr", "Tests the atf_build_cpp function");
189}
190ATF_TC_BODY(cpp, tc)
191{
192    struct cpp_test *test;
193
194    for (test = cpp_tests; test->expargv[0] != NULL; test++) {
195        printf("> Test: %s\n", test->msg);
196
197        verbose_set_env("ATF_BUILD_CPP", test->cpp);
198        verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
199        __atf_config_reinit();
200
201        {
202            char **argv;
203            if (test->hasoptargs)
204                RE(atf_build_cpp(test->sfile, test->ofile, test->optargs,
205                                 &argv));
206            else
207                RE(atf_build_cpp(test->sfile, test->ofile, NULL, &argv));
208            check_equal_array(test->expargv, argv);
209            atf_utils_free_charpp(argv);
210        }
211    }
212}
213
214ATF_TC(cxx_o);
215ATF_TC_HEAD(cxx_o, tc)
216{
217    atf_tc_set_md_var(tc, "descr", "Tests the atf_build_cxx_o function");
218}
219ATF_TC_BODY(cxx_o, tc)
220{
221    struct cxx_o_test *test;
222
223    for (test = cxx_o_tests; test->expargv[0] != NULL; test++) {
224        printf("> Test: %s\n", test->msg);
225
226        verbose_set_env("ATF_BUILD_CXX", test->cxx);
227        verbose_set_env("ATF_BUILD_CXXFLAGS", test->cxxflags);
228        verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
229        __atf_config_reinit();
230
231        {
232            char **argv;
233            if (test->hasoptargs)
234                RE(atf_build_cxx_o(test->sfile, test->ofile, test->optargs,
235                                   &argv));
236            else
237                RE(atf_build_cxx_o(test->sfile, test->ofile, NULL, &argv));
238            check_equal_array(test->expargv, argv);
239            atf_utils_free_charpp(argv);
240        }
241    }
242}
243
244/* ---------------------------------------------------------------------
245 * Tests cases for the header file.
246 * --------------------------------------------------------------------- */
247
248HEADER_TC(include, "atf-c/build.h");
249
250/* ---------------------------------------------------------------------
251 * Main.
252 * --------------------------------------------------------------------- */
253
254ATF_TP_ADD_TCS(tp)
255{
256    /* Add the internal test cases. */
257    ATF_TP_ADD_TC(tp, equal_arrays);
258
259    /* Add the test cases for the free functions. */
260    ATF_TP_ADD_TC(tp, c_o);
261    ATF_TP_ADD_TC(tp, cpp);
262    ATF_TP_ADD_TC(tp, cxx_o);
263
264    /* Add the test cases for the header file. */
265    ATF_TP_ADD_TC(tp, include);
266
267    return atf_no_error();
268}
269