1#include <glib.h>
2
3static void
4test_listenv (void)
5{
6 GHashTable *table;
7 gchar **list;
8 gint i;
9
10 table = g_hash_table_new_full (hash_func: g_str_hash, key_equal_func: g_str_equal,
11 key_destroy_func: g_free, value_destroy_func: g_free);
12
13 list = g_get_environ ();
14 for (i = 0; list[i]; i++)
15 {
16 gchar **parts;
17
18 parts = g_strsplit (string: list[i], delimiter: "=", max_tokens: 2);
19 g_assert (g_hash_table_lookup (table, parts[0]) == NULL);
20 if (g_strcmp0 (str1: parts[0], str2: ""))
21 g_hash_table_insert (hash_table: table, key: parts[0], value: parts[1]);
22 g_free (mem: parts);
23 }
24 g_strfreev (str_array: list);
25
26 g_assert_cmpint (g_hash_table_size (table), >, 0);
27
28 list = g_listenv ();
29 for (i = 0; list[i]; i++)
30 {
31 const gchar *expected;
32 const gchar *value;
33
34 expected = g_hash_table_lookup (hash_table: table, key: list[i]);
35 value = g_getenv (variable: list[i]);
36 g_assert_cmpstr (value, ==, expected);
37 g_hash_table_remove (hash_table: table, key: list[i]);
38 }
39 g_assert_cmpint (g_hash_table_size (table), ==, 0);
40 g_hash_table_unref (hash_table: table);
41 g_strfreev (str_array: list);
42}
43
44static void
45test_setenv (void)
46{
47 const gchar *var, *value;
48
49 var = "NOSUCHENVVAR";
50 value = "value1";
51
52 g_assert (g_getenv (var) == NULL);
53 g_setenv (variable: var, value, FALSE);
54 g_assert_cmpstr (g_getenv (var), ==, value);
55 g_assert (g_setenv (var, "value2", FALSE));
56 g_assert_cmpstr (g_getenv (var), ==, value);
57 g_assert (g_setenv (var, "value2", TRUE));
58 g_assert_cmpstr (g_getenv (var), ==, "value2");
59 g_unsetenv (variable: var);
60 g_assert (g_getenv (var) == NULL);
61}
62
63static void
64test_environ_array (void)
65{
66 gchar **env;
67 const gchar *value;
68
69 env = g_new (gchar *, 1);
70 env[0] = NULL;
71
72 value = g_environ_getenv (envp: env, variable: "foo");
73 g_assert (value == NULL);
74
75 env = g_environ_setenv (envp: env, variable: "foo", value: "bar", TRUE);
76 value = g_environ_getenv (envp: env, variable: "foo");
77 g_assert_cmpstr (value, ==, "bar");
78
79 env = g_environ_setenv (envp: env, variable: "foo2", value: "bar2", FALSE);
80 value = g_environ_getenv (envp: env, variable: "foo");
81 g_assert_cmpstr (value, ==, "bar");
82 value = g_environ_getenv (envp: env, variable: "foo2");
83 g_assert_cmpstr (value, ==, "bar2");
84
85 env = g_environ_setenv (envp: env, variable: "foo", value: "x", FALSE);
86 value = g_environ_getenv (envp: env, variable: "foo");
87 g_assert_cmpstr (value, ==, "bar");
88
89 env = g_environ_setenv (envp: env, variable: "foo", value: "x", TRUE);
90 value = g_environ_getenv (envp: env, variable: "foo");
91 g_assert_cmpstr (value, ==, "x");
92
93 env = g_environ_unsetenv (envp: env, variable: "foo2");
94 value = g_environ_getenv (envp: env, variable: "foo2");
95 g_assert (value == NULL);
96
97 g_strfreev (str_array: env);
98}
99
100static void
101test_environ_null (void)
102{
103 gchar **env;
104 const gchar *value;
105
106 env = NULL;
107
108 value = g_environ_getenv (envp: env, variable: "foo");
109 g_assert (value == NULL);
110
111 env = g_environ_setenv (NULL, variable: "foo", value: "bar", TRUE);
112 g_assert (env != NULL);
113 g_strfreev (str_array: env);
114
115 env = g_environ_unsetenv (NULL, variable: "foo");
116 g_assert (env == NULL);
117}
118
119static void
120test_environ_case (void)
121{
122 gchar **env;
123 const gchar *value;
124
125 env = NULL;
126
127 env = g_environ_setenv (envp: env, variable: "foo", value: "bar", TRUE);
128 value = g_environ_getenv (envp: env, variable: "foo");
129 g_assert_cmpstr (value, ==, "bar");
130
131 value = g_environ_getenv (envp: env, variable: "Foo");
132#ifdef G_OS_WIN32
133 g_assert_cmpstr (value, ==, "bar");
134#else
135 g_assert (value == NULL);
136#endif
137
138 env = g_environ_setenv (envp: env, variable: "FOO", value: "x", TRUE);
139 value = g_environ_getenv (envp: env, variable: "foo");
140#ifdef G_OS_WIN32
141 g_assert_cmpstr (value, ==, "x");
142#else
143 g_assert_cmpstr (value, ==, "bar");
144#endif
145
146 env = g_environ_unsetenv (envp: env, variable: "Foo");
147 value = g_environ_getenv (envp: env, variable: "foo");
148#ifdef G_OS_WIN32
149 g_assert (value == NULL);
150#else
151 g_assert_cmpstr (value, ==, "bar");
152#endif
153
154 g_strfreev (str_array: env);
155}
156
157int
158main (int argc, char **argv)
159{
160 g_test_init (argc: &argc, argv: &argv, NULL);
161
162 g_test_add_func (testpath: "/environ/listenv", test_func: test_listenv);
163 g_test_add_func (testpath: "/environ/setenv", test_func: test_setenv);
164 g_test_add_func (testpath: "/environ/array", test_func: test_environ_array);
165 g_test_add_func (testpath: "/environ/null", test_func: test_environ_null);
166 g_test_add_func (testpath: "/environ/case", test_func: test_environ_case);
167
168 return g_test_run ();
169}
170

source code of gtk/subprojects/glib/glib/tests/environment.c