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