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 <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_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 65void __atf_config_reinit(void); 66 67static 68void 69unset_all(void) 70{ 71 const struct varnames *v; 72 for (v = all_vars; v->lc != NULL; v++) 73 RE(atf_env_unset(v->uc)); 74} 75 76static 77void 78compare_one(const char *var, const char *expvalue) 79{ 80 const struct varnames *v; 81 82 printf("Checking that %s is set to %s\n", var, expvalue); 83 84 for (v = all_vars; v->lc != NULL; v++) { 85 if (strcmp(v->lc, var) == 0) 86 ATF_CHECK_STREQ(atf_config_get(v->lc), test_value); 87 else 88 ATF_CHECK(strcmp(atf_config_get(v->lc), test_value) != 0); 89 } 90} 91 92/* --------------------------------------------------------------------- 93 * Test cases for the free functions. 94 * --------------------------------------------------------------------- */ 95 96ATF_TC(get); 97ATF_TC_HEAD(get, tc) 98{ 99 atf_tc_set_md_var(tc, "descr", "Tests the atf_config_get function"); 100} 101ATF_TC_BODY(get, tc) 102{ 103 const struct varnames *v; 104 105 /* Unset all known environment variables and make sure the built-in 106 * values do not match the bogus value we will use for testing. */ 107 unset_all(); 108 __atf_config_reinit(); 109 for (v = all_vars; v->lc != NULL; v++) 110 ATF_CHECK(strcmp(atf_config_get(v->lc), test_value) != 0); 111 112 /* Test the behavior of empty values. */ 113 for (v = all_vars; v->lc != NULL; v++) { 114 unset_all(); 115 if (strcmp(atf_config_get(v->lc), "") != 0) { 116 RE(atf_env_set(v->uc, "")); 117 __atf_config_reinit(); 118 if (v->can_be_empty) 119 ATF_CHECK(strlen(atf_config_get(v->lc)) == 0); 120 else 121 ATF_CHECK(strlen(atf_config_get(v->lc)) > 0); 122 } 123 } 124 125 /* Check if every variable is recognized individually. */ 126 for (v = all_vars; v->lc != NULL; v++) { 127 unset_all(); 128 RE(atf_env_set(v->uc, test_value)); 129 __atf_config_reinit(); 130 compare_one(v->lc, test_value); 131 } 132} 133 134/* --------------------------------------------------------------------- 135 * Tests cases for the header file. 136 * --------------------------------------------------------------------- */ 137 138HEADER_TC(include, "atf-c/config.h"); 139 140/* --------------------------------------------------------------------- 141 * Main. 142 * --------------------------------------------------------------------- */ 143 144ATF_TP_ADD_TCS(tp) 145{ 146 ATF_TP_ADD_TC(tp, get); 147 148 /* Add the test cases for the header file. */ 149 ATF_TP_ADD_TC(tp, include); 150 151 return atf_no_error(); 152} 153