xref: /minix3/external/bsd/atf/dist/atf-c/config_test.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*11be35a1SLionel Sambuc /*
2*11be35a1SLionel Sambuc  * Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc  *
4*11be35a1SLionel Sambuc  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc  */
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc #include <stdio.h>
31*11be35a1SLionel Sambuc #include <string.h>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include <atf-c.h>
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include "atf-c/config.h"
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include "detail/env.h"
38*11be35a1SLionel Sambuc #include "detail/test_helpers.h"
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc static const char *test_value = "env-value";
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc static struct varnames {
43*11be35a1SLionel Sambuc     const char *lc;
44*11be35a1SLionel Sambuc     const char *uc;
45*11be35a1SLionel Sambuc     bool can_be_empty;
46*11be35a1SLionel Sambuc } all_vars[] = {
47*11be35a1SLionel Sambuc     { "atf_build_cc",       "ATF_BUILD_CC",       false },
48*11be35a1SLionel Sambuc     { "atf_build_cflags",   "ATF_BUILD_CFLAGS",   true  },
49*11be35a1SLionel Sambuc     { "atf_build_cpp",      "ATF_BUILD_CPP",      false },
50*11be35a1SLionel Sambuc     { "atf_build_cppflags", "ATF_BUILD_CPPFLAGS", true  },
51*11be35a1SLionel Sambuc     { "atf_build_cxx",      "ATF_BUILD_CXX",      false },
52*11be35a1SLionel Sambuc     { "atf_build_cxxflags", "ATF_BUILD_CXXFLAGS", true  },
53*11be35a1SLionel Sambuc     { "atf_includedir",     "ATF_INCLUDEDIR",     false },
54*11be35a1SLionel Sambuc     { "atf_libexecdir",     "ATF_LIBEXECDIR",     false },
55*11be35a1SLionel Sambuc     { "atf_pkgdatadir",     "ATF_PKGDATADIR",     false },
56*11be35a1SLionel Sambuc     { "atf_shell",          "ATF_SHELL",          false },
57*11be35a1SLionel Sambuc     { "atf_workdir",        "ATF_WORKDIR",        false },
58*11be35a1SLionel Sambuc     { NULL,                 NULL,                 false }
59*11be35a1SLionel Sambuc };
60*11be35a1SLionel Sambuc 
61*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
62*11be35a1SLionel Sambuc  * Auxiliary functions.
63*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc void __atf_config_reinit(void);
66*11be35a1SLionel Sambuc 
67*11be35a1SLionel Sambuc static
68*11be35a1SLionel Sambuc void
unset_all(void)69*11be35a1SLionel Sambuc unset_all(void)
70*11be35a1SLionel Sambuc {
71*11be35a1SLionel Sambuc     const struct varnames *v;
72*11be35a1SLionel Sambuc     for (v = all_vars; v->lc != NULL; v++)
73*11be35a1SLionel Sambuc         RE(atf_env_unset(v->uc));
74*11be35a1SLionel Sambuc }
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc static
77*11be35a1SLionel Sambuc void
compare_one(const char * var,const char * expvalue)78*11be35a1SLionel Sambuc compare_one(const char *var, const char *expvalue)
79*11be35a1SLionel Sambuc {
80*11be35a1SLionel Sambuc     const struct varnames *v;
81*11be35a1SLionel Sambuc 
82*11be35a1SLionel Sambuc     printf("Checking that %s is set to %s\n", var, expvalue);
83*11be35a1SLionel Sambuc 
84*11be35a1SLionel Sambuc     for (v = all_vars; v->lc != NULL; v++) {
85*11be35a1SLionel Sambuc         if (strcmp(v->lc, var) == 0)
86*11be35a1SLionel Sambuc             ATF_CHECK_STREQ(atf_config_get(v->lc), test_value);
87*11be35a1SLionel Sambuc         else
88*11be35a1SLionel Sambuc             ATF_CHECK(strcmp(atf_config_get(v->lc), test_value) != 0);
89*11be35a1SLionel Sambuc     }
90*11be35a1SLionel Sambuc }
91*11be35a1SLionel Sambuc 
92*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
93*11be35a1SLionel Sambuc  * Test cases for the free functions.
94*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc ATF_TC(get);
ATF_TC_HEAD(get,tc)97*11be35a1SLionel Sambuc ATF_TC_HEAD(get, tc)
98*11be35a1SLionel Sambuc {
99*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the atf_config_get function");
100*11be35a1SLionel Sambuc }
ATF_TC_BODY(get,tc)101*11be35a1SLionel Sambuc ATF_TC_BODY(get, tc)
102*11be35a1SLionel Sambuc {
103*11be35a1SLionel Sambuc     const struct varnames *v;
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc     /* Unset all known environment variables and make sure the built-in
106*11be35a1SLionel Sambuc      * values do not match the bogus value we will use for testing. */
107*11be35a1SLionel Sambuc     unset_all();
108*11be35a1SLionel Sambuc     __atf_config_reinit();
109*11be35a1SLionel Sambuc     for (v = all_vars; v->lc != NULL; v++)
110*11be35a1SLionel Sambuc         ATF_CHECK(strcmp(atf_config_get(v->lc), test_value) != 0);
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc     /* Test the behavior of empty values. */
113*11be35a1SLionel Sambuc     for (v = all_vars; v->lc != NULL; v++) {
114*11be35a1SLionel Sambuc         unset_all();
115*11be35a1SLionel Sambuc         if (strcmp(atf_config_get(v->lc), "") != 0) {
116*11be35a1SLionel Sambuc             RE(atf_env_set(v->uc, ""));
117*11be35a1SLionel Sambuc             __atf_config_reinit();
118*11be35a1SLionel Sambuc             if (v->can_be_empty)
119*11be35a1SLionel Sambuc                 ATF_CHECK(strlen(atf_config_get(v->lc)) == 0);
120*11be35a1SLionel Sambuc             else
121*11be35a1SLionel Sambuc                 ATF_CHECK(strlen(atf_config_get(v->lc)) > 0);
122*11be35a1SLionel Sambuc         }
123*11be35a1SLionel Sambuc     }
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc     /* Check if every variable is recognized individually. */
126*11be35a1SLionel Sambuc     for (v = all_vars; v->lc != NULL; v++) {
127*11be35a1SLionel Sambuc         unset_all();
128*11be35a1SLionel Sambuc         RE(atf_env_set(v->uc, test_value));
129*11be35a1SLionel Sambuc         __atf_config_reinit();
130*11be35a1SLionel Sambuc         compare_one(v->lc, test_value);
131*11be35a1SLionel Sambuc     }
132*11be35a1SLionel Sambuc }
133*11be35a1SLionel Sambuc 
134*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
135*11be35a1SLionel Sambuc  * Tests cases for the header file.
136*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
137*11be35a1SLionel Sambuc 
138*11be35a1SLionel Sambuc HEADER_TC(include, "atf-c/config.h");
139*11be35a1SLionel Sambuc 
140*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
141*11be35a1SLionel Sambuc  * Main.
142*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
143*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)144*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
145*11be35a1SLionel Sambuc {
146*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, get);
147*11be35a1SLionel Sambuc 
148*11be35a1SLionel Sambuc     /* Add the test cases for the header file. */
149*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, include);
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc     return atf_no_error();
152*11be35a1SLionel Sambuc }
153