1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <atf-c.h> 36 37 #include <stdio.h> 38 #include <stdlib.h> 39 40 #define TEST_VARIABLE "TEST_VAR" 41 #define TEST_SYSTEM_VARIABLE "PWD" 42 43 extern char **environ; 44 45 void 46 create_multiple_variables(int num) 47 { 48 char name[64]; 49 char value[64]; 50 int i; 51 52 for (i = 0; i < num; i++) { 53 snprintf(name, sizeof(name), "%s_%d", TEST_VARIABLE, i); 54 snprintf(value, sizeof(value), "%d", i); 55 ATF_CHECK(getenv(name) == NULL); 56 ATF_CHECK(setenv(name, value, 0) != -1); 57 ATF_CHECK_STREQ(getenv(name), value); 58 } 59 } 60 61 void 62 check_if_nulled_variables(int num) 63 { 64 char name[64]; 65 int i; 66 67 for (i = 0; i < num; i++) { 68 snprintf(name, sizeof(name), "%s_%d", TEST_VARIABLE, i); 69 ATF_CHECK(getenv(name) == NULL); 70 } 71 } 72 73 ATF_TC_WITHOUT_HEAD(clearenv__single_var_test); 74 ATF_TC_BODY(clearenv__single_var_test, tc) 75 { 76 77 ATF_CHECK(setenv(TEST_VARIABLE, "true", 0) != -1); 78 ATF_CHECK_STREQ(getenv(TEST_VARIABLE), "true"); 79 ATF_CHECK(clearenv() == 0); 80 ATF_CHECK(getenv(TEST_VARIABLE) == NULL); 81 } 82 83 ATF_TC_WITHOUT_HEAD(clearenv__multiple_vars_test); 84 ATF_TC_BODY(clearenv__multiple_vars_test, tc) 85 { 86 87 create_multiple_variables(10); 88 ATF_CHECK(clearenv() == 0); 89 check_if_nulled_variables(10); 90 } 91 92 ATF_TC_WITHOUT_HEAD(clearenv__recreated_vars_test); 93 ATF_TC_BODY(clearenv__recreated_vars_test, tc) 94 { 95 96 create_multiple_variables(10); 97 ATF_CHECK(clearenv() == 0); 98 check_if_nulled_variables(10); 99 create_multiple_variables(10); 100 } 101 102 ATF_TC_WITHOUT_HEAD(clearenv__system_var_test); 103 ATF_TC_BODY(clearenv__system_var_test, tc) 104 { 105 106 ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) != NULL); 107 ATF_CHECK(clearenv() == 0); 108 ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) == NULL); 109 } 110 111 ATF_TC_WITHOUT_HEAD(clearenv__recreated_system_var_test); 112 ATF_TC_BODY(clearenv__recreated_system_var_test, tc) 113 { 114 115 ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) != NULL); 116 ATF_CHECK(clearenv() == 0); 117 ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) == NULL); 118 ATF_CHECK(setenv(TEST_SYSTEM_VARIABLE, "test", 0) != -1); 119 ATF_CHECK_STREQ(getenv(TEST_SYSTEM_VARIABLE), "test"); 120 } 121 122 ATF_TC_WITHOUT_HEAD(clearenv__double_clear_vars); 123 ATF_TC_BODY(clearenv__double_clear_vars, tc) 124 { 125 126 create_multiple_variables(10); 127 ATF_CHECK(clearenv() == 0); 128 check_if_nulled_variables(10); 129 ATF_CHECK(clearenv() == 0); 130 check_if_nulled_variables(10); 131 create_multiple_variables(10); 132 } 133 134 ATF_TC_WITHOUT_HEAD(clearenv__environ_null); 135 ATF_TC_BODY(clearenv__environ_null, tc) 136 { 137 138 ATF_CHECK(clearenv() == 0); 139 ATF_CHECK(environ != NULL); 140 } 141 142 ATF_TC_WITHOUT_HEAD(clearenv__putenv_vars); 143 ATF_TC_BODY(clearenv__putenv_vars, tc) 144 { 145 char buf[64], ref[64]; 146 147 snprintf(buf, sizeof(buf), "%s=1", TEST_VARIABLE); 148 strcpy(ref, buf); 149 150 ATF_CHECK(getenv(TEST_VARIABLE) == NULL); 151 ATF_CHECK(putenv(buf) != -1); 152 ATF_CHECK(strcmp(getenv(TEST_VARIABLE), "1") == 0); 153 154 ATF_CHECK(clearenv() == 0); 155 156 ATF_CHECK(getenv(TEST_VARIABLE) == NULL); 157 ATF_CHECK(strcmp(buf, ref) == 0); 158 } 159 160 ATF_TP_ADD_TCS(tp) 161 { 162 163 ATF_TP_ADD_TC(tp, clearenv__single_var_test); 164 ATF_TP_ADD_TC(tp, clearenv__multiple_vars_test); 165 ATF_TP_ADD_TC(tp, clearenv__recreated_vars_test); 166 167 ATF_TP_ADD_TC(tp, clearenv__system_var_test); 168 ATF_TP_ADD_TC(tp, clearenv__recreated_system_var_test); 169 170 ATF_TP_ADD_TC(tp, clearenv__double_clear_vars); 171 ATF_TP_ADD_TC(tp, clearenv__environ_null); 172 173 ATF_TP_ADD_TC(tp, clearenv__putenv_vars); 174 175 return (atf_no_error()); 176 } 177