1 /*	$NetBSD: config_test.c,v 1.3 2014/12/10 04:38:03 christos Exp $	*/
2 
3 /*
4  * Automated Testing Framework (atf)
5  *
6  * Copyright (c) 2007 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 <string.h>
34 
35 #include <atf-c.h>
36 
37 #include "atf-c/config.h"
38 
39 #include "detail/env.h"
40 #include "detail/test_helpers.h"
41 
42 static const char *test_value = "env-value";
43 
44 static struct varnames {
45     const char *lc;
46     const char *uc;
47     bool can_be_empty;
48 } all_vars[] = {
49     { "atf_arch",           "ATF_ARCH",           false },
50     { "atf_build_cc",       "ATF_BUILD_CC",       false },
51     { "atf_build_cflags",   "ATF_BUILD_CFLAGS",   true  },
52     { "atf_build_cpp",      "ATF_BUILD_CPP",      false },
53     { "atf_build_cppflags", "ATF_BUILD_CPPFLAGS", true  },
54     { "atf_build_cxx",      "ATF_BUILD_CXX",      false },
55     { "atf_build_cxxflags", "ATF_BUILD_CXXFLAGS", true  },
56     { "atf_confdir",        "ATF_CONFDIR",        false },
57     { "atf_includedir",     "ATF_INCLUDEDIR",     false },
58     { "atf_libdir",         "ATF_LIBDIR",         false },
59     { "atf_libexecdir",     "ATF_LIBEXECDIR",     false },
60     { "atf_machine",        "ATF_MACHINE",        false },
61     { "atf_pkgdatadir",     "ATF_PKGDATADIR",     false },
62     { "atf_shell",          "ATF_SHELL",          false },
63     { "atf_workdir",        "ATF_WORKDIR",        false },
64     { NULL,                 NULL,                 false }
65 };
66 
67 /* ---------------------------------------------------------------------
68  * Auxiliary functions.
69  * --------------------------------------------------------------------- */
70 
71 void __atf_config_reinit(void);
72 
73 static
74 void
unset_all(void)75 unset_all(void)
76 {
77     const struct varnames *v;
78     for (v = all_vars; v->lc != NULL; v++)
79         RE(atf_env_unset(v->uc));
80 }
81 
82 static
83 void
compare_one(const char * var,const char * expvalue)84 compare_one(const char *var, const char *expvalue)
85 {
86     const struct varnames *v;
87 
88     printf("Checking that %s is set to %s\n", var, expvalue);
89 
90     for (v = all_vars; v->lc != NULL; v++) {
91         if (strcmp(v->lc, var) == 0)
92             ATF_CHECK_STREQ(atf_config_get(v->lc), test_value);
93         else
94             ATF_CHECK(strcmp(atf_config_get(v->lc), test_value) != 0);
95     }
96 }
97 
98 /* ---------------------------------------------------------------------
99  * Test cases for the free functions.
100  * --------------------------------------------------------------------- */
101 
102 ATF_TC(get);
ATF_TC_HEAD(get,tc)103 ATF_TC_HEAD(get, tc)
104 {
105     atf_tc_set_md_var(tc, "descr", "Tests the atf_config_get function");
106 }
ATF_TC_BODY(get,tc)107 ATF_TC_BODY(get, tc)
108 {
109     const struct varnames *v;
110 
111     /* Unset all known environment variables and make sure the built-in
112      * values do not match the bogus value we will use for testing. */
113     unset_all();
114     __atf_config_reinit();
115     for (v = all_vars; v->lc != NULL; v++)
116         ATF_CHECK(strcmp(atf_config_get(v->lc), test_value) != 0);
117 
118     /* Test the behavior of empty values. */
119     for (v = all_vars; v->lc != NULL; v++) {
120         unset_all();
121         if (strcmp(atf_config_get(v->lc), "") != 0) {
122             RE(atf_env_set(v->uc, ""));
123             __atf_config_reinit();
124             if (v->can_be_empty)
125                 ATF_CHECK(strlen(atf_config_get(v->lc)) == 0);
126             else
127                 ATF_CHECK(strlen(atf_config_get(v->lc)) > 0);
128         }
129     }
130 
131     /* Check if every variable is recognized individually. */
132     for (v = all_vars; v->lc != NULL; v++) {
133         unset_all();
134         RE(atf_env_set(v->uc, test_value));
135         __atf_config_reinit();
136         compare_one(v->lc, test_value);
137     }
138 }
139 
140 /* ---------------------------------------------------------------------
141  * Tests cases for the header file.
142  * --------------------------------------------------------------------- */
143 
144 HEADER_TC(include, "atf-c/config.h");
145 
146 /* ---------------------------------------------------------------------
147  * Main.
148  * --------------------------------------------------------------------- */
149 
ATF_TP_ADD_TCS(tp)150 ATF_TP_ADD_TCS(tp)
151 {
152     ATF_TP_ADD_TC(tp, get);
153 
154     /* Add the test cases for the header file. */
155     ATF_TP_ADD_TC(tp, include);
156 
157     return atf_no_error();
158 }
159