1240116Smarcel/*
2240116Smarcel * Automated Testing Framework (atf)
3240116Smarcel *
4240116Smarcel * Copyright (c) 2007 The NetBSD Foundation, Inc.
5240116Smarcel * All rights reserved.
6240116Smarcel *
7240116Smarcel * Redistribution and use in source and binary forms, with or without
8240116Smarcel * modification, are permitted provided that the following conditions
9240116Smarcel * are met:
10240116Smarcel * 1. Redistributions of source code must retain the above copyright
11240116Smarcel *    notice, this list of conditions and the following disclaimer.
12240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel *    notice, this list of conditions and the following disclaimer in the
14240116Smarcel *    documentation and/or other materials provided with the distribution.
15240116Smarcel *
16240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27240116Smarcel * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28240116Smarcel */
29240116Smarcel
30240116Smarcel#include <stdio.h>
31240116Smarcel#include <string.h>
32240116Smarcel
33240116Smarcel#include <atf-c.h>
34240116Smarcel
35240116Smarcel#include "atf-c/config.h"
36240116Smarcel
37240116Smarcel#include "detail/env.h"
38240116Smarcel#include "detail/test_helpers.h"
39240116Smarcel
40240116Smarcelstatic const char *test_value = "env-value";
41240116Smarcel
42240116Smarcelstatic struct varnames {
43240116Smarcel    const char *lc;
44240116Smarcel    const char *uc;
45240116Smarcel    bool can_be_empty;
46240116Smarcel} all_vars[] = {
47240116Smarcel    { "atf_build_cc",       "ATF_BUILD_CC",       false },
48240116Smarcel    { "atf_build_cflags",   "ATF_BUILD_CFLAGS",   true  },
49240116Smarcel    { "atf_build_cpp",      "ATF_BUILD_CPP",      false },
50240116Smarcel    { "atf_build_cppflags", "ATF_BUILD_CPPFLAGS", true  },
51240116Smarcel    { "atf_build_cxx",      "ATF_BUILD_CXX",      false },
52240116Smarcel    { "atf_build_cxxflags", "ATF_BUILD_CXXFLAGS", true  },
53240116Smarcel    { "atf_includedir",     "ATF_INCLUDEDIR",     false },
54240116Smarcel    { "atf_libexecdir",     "ATF_LIBEXECDIR",     false },
55240116Smarcel    { "atf_pkgdatadir",     "ATF_PKGDATADIR",     false },
56240116Smarcel    { "atf_shell",          "ATF_SHELL",          false },
57240116Smarcel    { "atf_workdir",        "ATF_WORKDIR",        false },
58240116Smarcel    { NULL,                 NULL,                 false }
59240116Smarcel};
60240116Smarcel
61240116Smarcel/* ---------------------------------------------------------------------
62240116Smarcel * Auxiliary functions.
63240116Smarcel * --------------------------------------------------------------------- */
64240116Smarcel
65240116Smarcelvoid __atf_config_reinit(void);
66240116Smarcel
67240116Smarcelstatic
68240116Smarcelvoid
69240116Smarcelunset_all(void)
70240116Smarcel{
71240116Smarcel    const struct varnames *v;
72240116Smarcel    for (v = all_vars; v->lc != NULL; v++)
73240116Smarcel        RE(atf_env_unset(v->uc));
74240116Smarcel}
75240116Smarcel
76240116Smarcelstatic
77240116Smarcelvoid
78240116Smarcelcompare_one(const char *var, const char *expvalue)
79240116Smarcel{
80240116Smarcel    const struct varnames *v;
81240116Smarcel
82240116Smarcel    printf("Checking that %s is set to %s\n", var, expvalue);
83240116Smarcel
84240116Smarcel    for (v = all_vars; v->lc != NULL; v++) {
85240116Smarcel        if (strcmp(v->lc, var) == 0)
86240116Smarcel            ATF_CHECK_STREQ(atf_config_get(v->lc), test_value);
87240116Smarcel        else
88240116Smarcel            ATF_CHECK(strcmp(atf_config_get(v->lc), test_value) != 0);
89240116Smarcel    }
90240116Smarcel}
91240116Smarcel
92240116Smarcel/* ---------------------------------------------------------------------
93240116Smarcel * Test cases for the free functions.
94240116Smarcel * --------------------------------------------------------------------- */
95240116Smarcel
96240116SmarcelATF_TC(get);
97240116SmarcelATF_TC_HEAD(get, tc)
98240116Smarcel{
99240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_config_get function");
100240116Smarcel}
101240116SmarcelATF_TC_BODY(get, tc)
102240116Smarcel{
103240116Smarcel    const struct varnames *v;
104240116Smarcel
105240116Smarcel    /* Unset all known environment variables and make sure the built-in
106240116Smarcel     * values do not match the bogus value we will use for testing. */
107240116Smarcel    unset_all();
108240116Smarcel    __atf_config_reinit();
109240116Smarcel    for (v = all_vars; v->lc != NULL; v++)
110240116Smarcel        ATF_CHECK(strcmp(atf_config_get(v->lc), test_value) != 0);
111240116Smarcel
112240116Smarcel    /* Test the behavior of empty values. */
113240116Smarcel    for (v = all_vars; v->lc != NULL; v++) {
114240116Smarcel        unset_all();
115240116Smarcel        if (strcmp(atf_config_get(v->lc), "") != 0) {
116240116Smarcel            RE(atf_env_set(v->uc, ""));
117240116Smarcel            __atf_config_reinit();
118240116Smarcel            if (v->can_be_empty)
119240116Smarcel                ATF_CHECK(strlen(atf_config_get(v->lc)) == 0);
120240116Smarcel            else
121240116Smarcel                ATF_CHECK(strlen(atf_config_get(v->lc)) > 0);
122240116Smarcel        }
123240116Smarcel    }
124240116Smarcel
125240116Smarcel    /* Check if every variable is recognized individually. */
126240116Smarcel    for (v = all_vars; v->lc != NULL; v++) {
127240116Smarcel        unset_all();
128240116Smarcel        RE(atf_env_set(v->uc, test_value));
129240116Smarcel        __atf_config_reinit();
130240116Smarcel        compare_one(v->lc, test_value);
131240116Smarcel    }
132240116Smarcel}
133240116Smarcel
134240116Smarcel/* ---------------------------------------------------------------------
135240116Smarcel * Tests cases for the header file.
136240116Smarcel * --------------------------------------------------------------------- */
137240116Smarcel
138240116SmarcelHEADER_TC(include, "atf-c/config.h");
139240116Smarcel
140240116Smarcel/* ---------------------------------------------------------------------
141240116Smarcel * Main.
142240116Smarcel * --------------------------------------------------------------------- */
143240116Smarcel
144240116SmarcelATF_TP_ADD_TCS(tp)
145240116Smarcel{
146240116Smarcel    ATF_TP_ADD_TC(tp, get);
147240116Smarcel
148240116Smarcel    /* Add the test cases for the header file. */
149240116Smarcel    ATF_TP_ADD_TC(tp, include);
150240116Smarcel
151240116Smarcel    return atf_no_error();
152240116Smarcel}
153