1 // 2 // Automated Testing Framework (atf) 3 // 4 // Copyright (c) 2007 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 <cstring> 31 #include <iostream> 32 33 #include "config.hpp" 34 #include "macros.hpp" 35 36 #include "detail/env.hpp" 37 #include "detail/exceptions.hpp" 38 #include "detail/test_helpers.hpp" 39 40 static const char *test_value = "env-value"; 41 42 static struct varnames { 43 const char *lc; 44 const char *uc; 45 bool can_be_empty; 46 } all_vars[] = { 47 { "atf_build_cc", "ATF_BUILD_CC", false }, 48 { "atf_build_cflags", "ATF_BUILD_CFLAGS", true }, 49 { "atf_build_cpp", "ATF_BUILD_CPP", false }, 50 { "atf_build_cppflags", "ATF_BUILD_CPPFLAGS", true }, 51 { "atf_build_cxx", "ATF_BUILD_CXX", false }, 52 { "atf_build_cxxflags", "ATF_BUILD_CXXFLAGS", true }, 53 { "atf_includedir", "ATF_INCLUDEDIR", false }, 54 { "atf_libexecdir", "ATF_LIBEXECDIR", false }, 55 { "atf_pkgdatadir", "ATF_PKGDATADIR", false }, 56 { "atf_shell", "ATF_SHELL", false }, 57 { "atf_workdir", "ATF_WORKDIR", false }, 58 { NULL, NULL, false } 59 }; 60 61 // ------------------------------------------------------------------------ 62 // Auxiliary functions. 63 // ------------------------------------------------------------------------ 64 65 namespace atf { 66 namespace config { 67 void __reinit(void); 68 } 69 } 70 71 static 72 void 73 set_env_var(const char* name, const char* val) 74 { 75 try { 76 atf::env::set(name, val); 77 } catch (const atf::system_error&) { 78 ATF_FAIL(std::string("set_env_var(") + name + ", " + val + 79 ") failed"); 80 } 81 } 82 83 static 84 void 85 unset_env_var(const char* name) 86 { 87 try { 88 atf::env::unset(name); 89 } catch (const atf::system_error&) { 90 ATF_FAIL(std::string("unset_env_var(") + name + ") failed"); 91 } 92 } 93 94 static 95 size_t 96 all_vars_count(void) 97 { 98 size_t count = 0; 99 for (const struct varnames* v = all_vars; v->lc != NULL; v++) 100 count++; 101 return count; 102 } 103 104 static 105 void 106 unset_all(void) 107 { 108 for (const struct varnames* v = all_vars; v->lc != NULL; v++) 109 unset_env_var(v->uc); 110 } 111 112 static 113 void 114 compare_one(const char* var, const char* expvalue) 115 { 116 std::cout << "Checking that " << var << " is set to " << expvalue << "\n"; 117 118 for (const struct varnames* v = all_vars; v->lc != NULL; v++) { 119 if (std::strcmp(v->lc, var) == 0) 120 ATF_REQUIRE_EQ(atf::config::get(v->lc), test_value); 121 else 122 ATF_REQUIRE(atf::config::get(v->lc) != test_value); 123 } 124 } 125 126 // ------------------------------------------------------------------------ 127 // Test cases for the free functions. 128 // ------------------------------------------------------------------------ 129 130 ATF_TEST_CASE(get); 131 ATF_TEST_CASE_HEAD(get) 132 { 133 set_md_var("descr", "Tests the config::get function"); 134 } 135 ATF_TEST_CASE_BODY(get) 136 { 137 // Unset all known environment variables and make sure the built-in 138 // values do not match the bogus value we will use for testing. 139 unset_all(); 140 atf::config::__reinit(); 141 for (const struct varnames* v = all_vars; v->lc != NULL; v++) 142 ATF_REQUIRE(atf::config::get(v->lc) != test_value); 143 144 // Test the behavior of empty values. 145 for (const struct varnames* v = all_vars; v->lc != NULL; v++) { 146 unset_all(); 147 if (!atf::config::get(v->lc).empty()) { 148 set_env_var(v->uc, ""); 149 atf::config::__reinit(); 150 if (v->can_be_empty) 151 ATF_REQUIRE(atf::config::get(v->lc).empty()); 152 else 153 ATF_REQUIRE(!atf::config::get(v->lc).empty()); 154 } 155 } 156 157 // Check if the ATF_ARCH variable is recognized. 158 for (const struct varnames* v = all_vars; v->lc != NULL; v++) { 159 unset_all(); 160 set_env_var(v->uc, test_value); 161 atf::config::__reinit(); 162 compare_one(v->lc, test_value); 163 } 164 } 165 166 ATF_TEST_CASE(get_all); 167 ATF_TEST_CASE_HEAD(get_all) 168 { 169 set_md_var("descr", "Tests the config::get_all function"); 170 } 171 ATF_TEST_CASE_BODY(get_all) 172 { 173 atf::config::__reinit(); 174 175 // Check that the valid variables, and only those, are returned. 176 std::map< std::string, std::string > vars = atf::config::get_all(); 177 ATF_REQUIRE_EQ(vars.size(), all_vars_count()); 178 for (const struct varnames* v = all_vars; v->lc != NULL; v++) 179 ATF_REQUIRE(vars.find(v->lc) != vars.end()); 180 } 181 182 ATF_TEST_CASE(has); 183 ATF_TEST_CASE_HEAD(has) 184 { 185 set_md_var("descr", "Tests the config::has function"); 186 } 187 ATF_TEST_CASE_BODY(has) 188 { 189 atf::config::__reinit(); 190 191 // Check for all the variables that must exist. 192 for (const struct varnames* v = all_vars; v->lc != NULL; v++) 193 ATF_REQUIRE(atf::config::has(v->lc)); 194 195 // Same as above, but using uppercase (which is incorrect). 196 for (const struct varnames* v = all_vars; v->lc != NULL; v++) 197 ATF_REQUIRE(!atf::config::has(v->uc)); 198 199 // Check for some other variables that cannot exist. 200 ATF_REQUIRE(!atf::config::has("foo")); 201 ATF_REQUIRE(!atf::config::has("BAR")); 202 ATF_REQUIRE(!atf::config::has("atf_foo")); 203 ATF_REQUIRE(!atf::config::has("ATF_BAR")); 204 ATF_REQUIRE(!atf::config::has("atf_shel")); 205 ATF_REQUIRE(!atf::config::has("atf_shells")); 206 } 207 208 // ------------------------------------------------------------------------ 209 // Tests cases for the header file. 210 // ------------------------------------------------------------------------ 211 212 HEADER_TC(include, "atf-c++/config.hpp"); 213 214 // ------------------------------------------------------------------------ 215 // Main. 216 // ------------------------------------------------------------------------ 217 218 ATF_INIT_TEST_CASES(tcs) 219 { 220 // Add the test cases for the free functions. 221 ATF_ADD_TEST_CASE(tcs, has); 222 ATF_ADD_TEST_CASE(tcs, get); 223 ATF_ADD_TEST_CASE(tcs, get_all); 224 225 // Add the test cases for the header file. 226 ATF_ADD_TEST_CASE(tcs, include); 227 } 228