1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (C) 2021 Mariusz Zaborski <oshogbo@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * Test for clearenv(3) routine. 30 */ 31 32 #include <atf-c.h> 33 34 #include <stdio.h> 35 #include <stdlib.h> 36 37 #define TEST_VARIABLE "TEST_VAR" 38 #define TEST_SYSTEM_VARIABLE "PWD" 39 40 extern char **environ; 41 42 void 43 create_multiple_variables(int num) 44 { 45 char name[64]; 46 char value[64]; 47 int i; 48 49 for (i = 0; i < num; i++) { 50 snprintf(name, sizeof(name), "%s_%d", TEST_VARIABLE, i); 51 snprintf(value, sizeof(value), "%d", i); 52 ATF_CHECK(getenv(name) == NULL); 53 ATF_CHECK(setenv(name, value, 0) != -1); 54 ATF_CHECK_STREQ(getenv(name), value); 55 } 56 } 57 58 void 59 check_if_nulled_variables(int num) 60 { 61 char name[64]; 62 int i; 63 64 for (i = 0; i < num; i++) { 65 snprintf(name, sizeof(name), "%s_%d", TEST_VARIABLE, i); 66 ATF_CHECK(getenv(name) == NULL); 67 } 68 } 69 70 ATF_TC_WITHOUT_HEAD(clearenv__single_var_test); 71 ATF_TC_BODY(clearenv__single_var_test, tc) 72 { 73 74 ATF_CHECK(setenv(TEST_VARIABLE, "true", 0) != -1); 75 ATF_CHECK_STREQ(getenv(TEST_VARIABLE), "true"); 76 ATF_CHECK(clearenv() == 0); 77 ATF_CHECK(getenv(TEST_VARIABLE) == NULL); 78 } 79 80 ATF_TC_WITHOUT_HEAD(clearenv__multiple_vars_test); 81 ATF_TC_BODY(clearenv__multiple_vars_test, tc) 82 { 83 84 create_multiple_variables(10); 85 ATF_CHECK(clearenv() == 0); 86 check_if_nulled_variables(10); 87 } 88 89 ATF_TC_WITHOUT_HEAD(clearenv__recreated_vars_test); 90 ATF_TC_BODY(clearenv__recreated_vars_test, tc) 91 { 92 93 create_multiple_variables(10); 94 ATF_CHECK(clearenv() == 0); 95 check_if_nulled_variables(10); 96 create_multiple_variables(10); 97 } 98 99 ATF_TC_WITHOUT_HEAD(clearenv__system_var_test); 100 ATF_TC_BODY(clearenv__system_var_test, tc) 101 { 102 103 ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) != NULL); 104 ATF_CHECK(clearenv() == 0); 105 ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) == NULL); 106 } 107 108 ATF_TC_WITHOUT_HEAD(clearenv__recreated_system_var_test); 109 ATF_TC_BODY(clearenv__recreated_system_var_test, tc) 110 { 111 112 ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) != NULL); 113 ATF_CHECK(clearenv() == 0); 114 ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) == NULL); 115 ATF_CHECK(setenv(TEST_SYSTEM_VARIABLE, "test", 0) != -1); 116 ATF_CHECK_STREQ(getenv(TEST_SYSTEM_VARIABLE), "test"); 117 } 118 119 ATF_TC_WITHOUT_HEAD(clearenv__double_clear_vars); 120 ATF_TC_BODY(clearenv__double_clear_vars, tc) 121 { 122 123 create_multiple_variables(10); 124 ATF_CHECK(clearenv() == 0); 125 check_if_nulled_variables(10); 126 ATF_CHECK(clearenv() == 0); 127 check_if_nulled_variables(10); 128 create_multiple_variables(10); 129 } 130 131 ATF_TC_WITHOUT_HEAD(clearenv__environ_null); 132 ATF_TC_BODY(clearenv__environ_null, tc) 133 { 134 135 ATF_CHECK(clearenv() == 0); 136 ATF_CHECK(environ != NULL); 137 } 138 139 ATF_TC_WITHOUT_HEAD(clearenv__putenv_vars); 140 ATF_TC_BODY(clearenv__putenv_vars, tc) 141 { 142 char buf[64], ref[64]; 143 144 snprintf(buf, sizeof(buf), "%s=1", TEST_VARIABLE); 145 strcpy(ref, buf); 146 147 ATF_CHECK(getenv(TEST_VARIABLE) == NULL); 148 ATF_CHECK(putenv(buf) != -1); 149 ATF_CHECK(strcmp(getenv(TEST_VARIABLE), "1") == 0); 150 151 ATF_CHECK(clearenv() == 0); 152 153 ATF_CHECK(getenv(TEST_VARIABLE) == NULL); 154 ATF_CHECK(strcmp(buf, ref) == 0); 155 } 156 157 ATF_TP_ADD_TCS(tp) 158 { 159 160 ATF_TP_ADD_TC(tp, clearenv__single_var_test); 161 ATF_TP_ADD_TC(tp, clearenv__multiple_vars_test); 162 ATF_TP_ADD_TC(tp, clearenv__recreated_vars_test); 163 164 ATF_TP_ADD_TC(tp, clearenv__system_var_test); 165 ATF_TP_ADD_TC(tp, clearenv__recreated_system_var_test); 166 167 ATF_TP_ADD_TC(tp, clearenv__double_clear_vars); 168 ATF_TP_ADD_TC(tp, clearenv__environ_null); 169 170 ATF_TP_ADD_TC(tp, clearenv__putenv_vars); 171 172 return (atf_no_error()); 173 } 174