1 /* Copyright (c) 2007 The NetBSD Foundation, Inc. 2 * All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 14 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 25 26 #include "atf-c/detail/env.h" 27 28 #include <stdlib.h> 29 #include <string.h> 30 31 #include <atf-c.h> 32 33 #include "atf-c/detail/test_helpers.h" 34 #include "atf-c/detail/text.h" 35 36 /* --------------------------------------------------------------------- 37 * Test cases for the free functions. 38 * --------------------------------------------------------------------- */ 39 40 ATF_TC(has); 41 ATF_TC_HEAD(has, tc) 42 { 43 atf_tc_set_md_var(tc, "descr", "Tests the atf_env_has function"); 44 } 45 ATF_TC_BODY(has, tc) 46 { 47 ATF_REQUIRE(atf_env_has("PATH")); 48 ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_")); 49 } 50 51 ATF_TC(get); 52 ATF_TC_HEAD(get, tc) 53 { 54 atf_tc_set_md_var(tc, "descr", "Tests the atf_env_get function"); 55 } 56 ATF_TC_BODY(get, tc) 57 { 58 const char *val; 59 60 ATF_REQUIRE(atf_env_has("PATH")); 61 62 val = atf_env_get("PATH"); 63 ATF_REQUIRE(strlen(val) > 0); 64 ATF_REQUIRE(strchr(val, ':') != NULL); 65 } 66 67 ATF_TC(get_with_default); 68 ATF_TC_HEAD(get_with_default, tc) 69 { 70 atf_tc_set_md_var(tc, "descr", "Tests the atf_env_get_with_default " 71 "function"); 72 } 73 ATF_TC_BODY(get_with_default, tc) 74 { 75 const char *val; 76 77 ATF_REQUIRE(atf_env_has("PATH")); 78 79 val = atf_env_get_with_default("PATH", "unknown"); 80 ATF_REQUIRE(strcmp(val, "unknown") != 0); 81 82 val = atf_env_get_with_default("_UNKNOWN_VARIABLE_", "foo bar"); 83 ATF_REQUIRE(strcmp(val, "foo bar") == 0); 84 } 85 86 ATF_TC(set); 87 ATF_TC_HEAD(set, tc) 88 { 89 atf_tc_set_md_var(tc, "descr", "Tests the atf_env_set function"); 90 } 91 ATF_TC_BODY(set, tc) 92 { 93 char *oldval; 94 95 ATF_REQUIRE(atf_env_has("PATH")); 96 RE(atf_text_format(&oldval, "%s", atf_env_get("PATH"))); 97 RE(atf_env_set("PATH", "foo-bar")); 98 ATF_REQUIRE(strcmp(atf_env_get("PATH"), oldval) != 0); 99 ATF_REQUIRE(strcmp(atf_env_get("PATH"), "foo-bar") == 0); 100 free(oldval); 101 102 ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_")); 103 RE(atf_env_set("_UNDEFINED_VARIABLE_", "foo2-bar2")); 104 ATF_REQUIRE(strcmp(atf_env_get("_UNDEFINED_VARIABLE_"), 105 "foo2-bar2") == 0); 106 } 107 108 ATF_TC(unset); 109 ATF_TC_HEAD(unset, tc) 110 { 111 atf_tc_set_md_var(tc, "descr", "Tests the atf_env_unset function"); 112 } 113 ATF_TC_BODY(unset, tc) 114 { 115 ATF_REQUIRE(atf_env_has("PATH")); 116 RE(atf_env_unset("PATH")); 117 ATF_REQUIRE(!atf_env_has("PATH")); 118 } 119 120 /* --------------------------------------------------------------------- 121 * Main. 122 * --------------------------------------------------------------------- */ 123 124 ATF_TP_ADD_TCS(tp) 125 { 126 ATF_TP_ADD_TC(tp, has); 127 ATF_TP_ADD_TC(tp, get); 128 ATF_TP_ADD_TC(tp, get_with_default); 129 ATF_TP_ADD_TC(tp, set); 130 ATF_TP_ADD_TC(tp, unset); 131 132 return atf_no_error(); 133 } 134