1 /* Self tests for gdb_environ for GDB, the GNU debugger.
3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbsupport/selftest.h"
21 #include "gdbsupport/environ.h"
22 #include "diagnostics.h"
24 static const char gdb_selftest_env_var
[] = "GDB_SELFTEST_ENVIRON";
27 set_contains (const std::set
<std::string
> &set
, std::string key
)
29 return set
.find (key
) != set
.end ();
33 namespace gdb_environ_tests
{
35 /* Test if the vector is initialized in a correct way. */
38 test_vector_initialization ()
42 /* When the vector is initialized, there should always be one NULL
44 SELF_CHECK (env
.envp ()[0] == NULL
);
45 SELF_CHECK (env
.user_set_env ().size () == 0);
46 SELF_CHECK (env
.user_unset_env ().size () == 0);
48 /* Make sure that there is no other element. */
49 SELF_CHECK (env
.get ("PWD") == NULL
);
52 /* Test initialization using the host's environ. */
55 test_init_from_host_environ ()
57 /* Initialize the environment vector using the host's environ. */
58 gdb_environ env
= gdb_environ::from_host_environ ();
60 /* The user-set and user-unset lists must be empty. */
61 SELF_CHECK (env
.user_set_env ().size () == 0);
62 SELF_CHECK (env
.user_unset_env ().size () == 0);
64 /* Our test environment variable should be present at the
66 SELF_CHECK (strcmp (env
.get (gdb_selftest_env_var
), "1") == 0);
69 /* Test reinitialization using the host's environ. */
72 test_reinit_from_host_environ ()
74 /* Reinitialize our environ vector using the host environ. We
75 should be able to see one (and only one) instance of the test
77 gdb_environ env
= gdb_environ::from_host_environ ();
78 env
= gdb_environ::from_host_environ ();
79 char **envp
= env
.envp ();
82 for (size_t i
= 0; envp
[i
] != NULL
; ++i
)
83 if (strcmp (envp
[i
], "GDB_SELFTEST_ENVIRON=1") == 0)
85 SELF_CHECK (num_found
== 1);
88 /* Test the case when we set a variable A, then set a variable B,
89 then unset A, and make sure that we cannot find A in the environ
90 vector, but can still find B. */
93 test_set_A_unset_B_unset_A_cannot_find_A_can_find_B ()
97 env
.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
98 SELF_CHECK (strcmp (env
.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
99 /* User-set environ var list must contain one element. */
100 SELF_CHECK (env
.user_set_env ().size () == 1);
101 SELF_CHECK (set_contains (env
.user_set_env (),
102 std::string ("GDB_SELFTEST_ENVIRON_1=aaa")));
104 env
.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
105 SELF_CHECK (strcmp (env
.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
107 env
.unset ("GDB_SELFTEST_ENVIRON_1");
108 SELF_CHECK (env
.get ("GDB_SELFTEST_ENVIRON_1") == NULL
);
109 SELF_CHECK (strcmp (env
.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
111 /* The user-set environ var list must contain only one element
113 SELF_CHECK (set_contains (env
.user_set_env (),
114 std::string ("GDB_SELFTEST_ENVIRON_2=bbb")));
115 SELF_CHECK (env
.user_set_env ().size () == 1);
118 /* Check if unset followed by a set in an empty vector works. */
121 test_unset_set_empty_vector ()
125 env
.set ("PWD", "test");
126 SELF_CHECK (strcmp (env
.get ("PWD"), "test") == 0);
127 SELF_CHECK (set_contains (env
.user_set_env (), std::string ("PWD=test")));
128 SELF_CHECK (env
.user_unset_env ().size () == 0);
129 /* The second element must be NULL. */
130 SELF_CHECK (env
.envp ()[1] == NULL
);
131 SELF_CHECK (env
.user_set_env ().size () == 1);
133 SELF_CHECK (env
.envp ()[0] == NULL
);
134 SELF_CHECK (env
.user_set_env ().size () == 0);
135 SELF_CHECK (env
.user_unset_env ().size () == 1);
136 SELF_CHECK (set_contains (env
.user_unset_env (), std::string ("PWD")));
139 /* When we clear our environ vector, there should be only one
140 element on it (NULL), and we shouldn't be able to get our test
148 env
.set (gdb_selftest_env_var
, "1");
151 SELF_CHECK (env
.envp ()[0] == NULL
);
152 SELF_CHECK (env
.user_set_env ().size () == 0);
153 SELF_CHECK (env
.user_unset_env ().size () == 0);
154 SELF_CHECK (env
.get (gdb_selftest_env_var
) == NULL
);
157 /* Test that after a std::move the moved-from object is left at a
158 valid state (i.e., its only element is NULL). */
167 SELF_CHECK (strcmp (env
.get ("A"), "1") == 0);
168 SELF_CHECK (set_contains (env
.user_set_env (), std::string ("A=1")));
169 SELF_CHECK (env
.user_set_env ().size () == 1);
171 env2
= std::move (env
);
172 SELF_CHECK (env
.envp ()[0] == NULL
);
173 SELF_CHECK (strcmp (env2
.get ("A"), "1") == 0);
174 SELF_CHECK (env2
.envp ()[1] == NULL
);
175 SELF_CHECK (env
.user_set_env ().size () == 0);
176 SELF_CHECK (set_contains (env2
.user_set_env (), std::string ("A=1")));
177 SELF_CHECK (env2
.user_set_env ().size () == 1);
179 SELF_CHECK (strcmp (env
.get ("B"), "2") == 0);
180 SELF_CHECK (env
.envp ()[1] == NULL
);
183 /* Test that the move constructor leaves everything at a valid
187 test_move_constructor ()
192 SELF_CHECK (strcmp (env
.get ("A"), "1") == 0);
193 SELF_CHECK (set_contains (env
.user_set_env (), std::string ("A=1")));
195 gdb_environ env2
= std::move (env
);
196 SELF_CHECK (env
.envp ()[0] == NULL
);
197 SELF_CHECK (env
.user_set_env ().size () == 0);
198 SELF_CHECK (strcmp (env2
.get ("A"), "1") == 0);
199 SELF_CHECK (env2
.envp ()[1] == NULL
);
200 SELF_CHECK (set_contains (env2
.user_set_env (), std::string ("A=1")));
201 SELF_CHECK (env2
.user_set_env ().size () == 1);
204 SELF_CHECK (strcmp (env
.get ("B"), "2") == 0);
205 SELF_CHECK (env
.envp ()[1] == NULL
);
206 SELF_CHECK (set_contains (env
.user_set_env (), std::string ("B=2")));
207 SELF_CHECK (env
.user_set_env ().size () == 1);
210 /* Test that self-moving works. */
217 /* Test self-move. */
219 SELF_CHECK (strcmp (env
.get ("A"), "1") == 0);
220 SELF_CHECK (set_contains (env
.user_set_env (), std::string ("A=1")));
221 SELF_CHECK (env
.user_set_env ().size () == 1);
223 /* Some compilers warn about moving to self, but that's precisely what we want
224 to test here, so turn this warning off. */
226 DIAGNOSTIC_IGNORE_SELF_MOVE
227 env
= std::move (env
);
230 SELF_CHECK (strcmp (env
.get ("A"), "1") == 0);
231 SELF_CHECK (strcmp (env
.envp ()[0], "A=1") == 0);
232 SELF_CHECK (env
.envp ()[1] == NULL
);
233 SELF_CHECK (set_contains (env
.user_set_env (), std::string ("A=1")));
234 SELF_CHECK (env
.user_set_env ().size () == 1);
237 /* Test if set/unset/reset works. */
240 test_set_unset_reset ()
242 gdb_environ env
= gdb_environ::from_host_environ ();
244 /* Our test variable should already be present in the host's environment. */
245 SELF_CHECK (env
.get ("GDB_SELFTEST_ENVIRON") != NULL
);
247 /* Set our test variable to another value. */
248 env
.set ("GDB_SELFTEST_ENVIRON", "test");
249 SELF_CHECK (strcmp (env
.get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
250 SELF_CHECK (env
.user_set_env ().size () == 1);
251 SELF_CHECK (env
.user_unset_env ().size () == 0);
253 /* And unset our test variable. The variable still exists in the
254 host's environment, but doesn't exist in our vector. */
255 env
.unset ("GDB_SELFTEST_ENVIRON");
256 SELF_CHECK (env
.get ("GDB_SELFTEST_ENVIRON") == NULL
);
257 SELF_CHECK (env
.user_set_env ().size () == 0);
258 SELF_CHECK (env
.user_unset_env ().size () == 1);
259 SELF_CHECK (set_contains (env
.user_unset_env (),
260 std::string ("GDB_SELFTEST_ENVIRON")));
262 /* Re-set the test variable. */
263 env
.set ("GDB_SELFTEST_ENVIRON", "1");
264 SELF_CHECK (strcmp (env
.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
270 /* Set a test environment variable. */
271 if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
272 error (_("Could not set environment variable for testing."));
274 test_vector_initialization ();
276 test_unset_set_empty_vector ();
278 test_init_from_host_environ ();
280 test_set_unset_reset ();
282 test_vector_clear ();
284 test_reinit_from_host_environ ();
286 /* Get rid of our test variable. We won't need it anymore. */
287 unsetenv ("GDB_SELFTEST_ENVIRON");
289 test_set_A_unset_B_unset_A_cannot_find_A_can_find_B ();
293 test_move_constructor ();
297 } /* namespace gdb_environ */
298 } /* namespace selftests */
300 void _initialize_environ_selftests ();
302 _initialize_environ_selftests ()
304 selftests::register_test ("gdb_environ",
305 selftests::gdb_environ_tests::run_tests
);