1 |
/* |
2 |
* Unit test functions for ASCEND: utilities/ascEnvVar.c |
3 |
* |
4 |
* Copyright (C) 2005 Jerry St.Clair |
5 |
* |
6 |
* This file is part of the Ascend Environment. |
7 |
* |
8 |
* The Ascend Environment is free software; you can redistribute it |
9 |
* and/or modify it under the terms of the GNU General Public License as |
10 |
* published by the Free Software Foundation; either version 2 of the |
11 |
* License, or (at your option) any later version. |
12 |
* |
13 |
* The Ascend Environment is distributed in hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
* General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with the program; if not, write to the Free Software Foundation, |
20 |
* Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named |
21 |
* COPYING. |
22 |
*/ |
23 |
|
24 |
#include <stdio.h> |
25 |
#include "utilities/ascConfig.h" |
26 |
#ifdef __WIN32__ |
27 |
#include <io.h> |
28 |
#endif |
29 |
#include "utilities/ascEnvVar.h" |
30 |
#include "utilities/ascMalloc.h" |
31 |
#include "general/list.h" |
32 |
#include "CUnit/CUnit.h" |
33 |
#include "test_ascEnvVar.h" |
34 |
|
35 |
#ifdef __WIN32__ |
36 |
#define SLASH '\\' |
37 |
#define PATHDIV ';' |
38 |
#else /* ! __WIN32__ */ |
39 |
#define SLASH '/' |
40 |
#define PATHDIV ':' |
41 |
#endif |
42 |
|
43 |
#define STR_LEN 250 |
44 |
|
45 |
/* compare function for strings used in this test function */ |
46 |
static |
47 |
int compare_strings(CONST VOIDPTR p1, CONST VOIDPTR p2) |
48 |
{ |
49 |
return strcmp((char *)p1, (char *)p2); |
50 |
} |
51 |
|
52 |
static void test_ascEnvVar(void) |
53 |
{ |
54 |
char str_path[STR_LEN]; |
55 |
char str_path_ss1[STR_LEN]; |
56 |
char str_path_ss2[STR_LEN]; |
57 |
char str_path_ss3[STR_LEN]; |
58 |
|
59 |
char str_pathbig[MAX_ENV_VAR_LENGTH*3]; |
60 |
char str_pathbig_ss1[MAX_ENV_VAR_LENGTH*3]; |
61 |
char str_pathbig_ss2[MAX_ENV_VAR_LENGTH*3]; |
62 |
|
63 |
int elem_count; |
64 |
char **paths; |
65 |
struct gl_list_t *name_list; |
66 |
int i; |
67 |
char test_ext_varname[] = "TEST_ASCENVVAR_EXTERNAL_ENV_VAR"; |
68 |
char *str_env; |
69 |
int i_initialized_lists = FALSE; |
70 |
unsigned long prior_meminuse; |
71 |
|
72 |
prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */ |
73 |
|
74 |
/* make sure list system is initialized */ |
75 |
if (FALSE == gl_pool_initialized()) { |
76 |
gl_init(); |
77 |
gl_init_pool(); |
78 |
i_initialized_lists = TRUE; |
79 |
} |
80 |
|
81 |
/* test Asc_InitEnvironment() */ |
82 |
CU_TEST(0 == Asc_InitEnvironment(0)); /* init with 0 */ |
83 |
CU_TEST(1 == Asc_InitEnvironment(0)); /* should not be able to initialize again */ |
84 |
Asc_DestroyEnvironment(); |
85 |
|
86 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
87 |
CU_TEST(1 == Asc_InitEnvironment(10)); /* should not be able to initialize again */ |
88 |
Asc_DestroyEnvironment(); |
89 |
|
90 |
/* test Asc_SetPathList() */ |
91 |
|
92 |
CU_TEST(1 == Asc_SetPathList("envar1", str_path)); /* not initialized */ |
93 |
|
94 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
95 |
|
96 |
CU_TEST(1 == Asc_SetPathList(NULL, str_path)); /* NULL var */ |
97 |
CU_TEST(1 == Asc_SetPathList("envar1", NULL)); /* NULL path */ |
98 |
CU_TEST(1 == Asc_SetPathList("", str_path)); /* empty var */ |
99 |
CU_TEST(1 == Asc_SetPathList("envar1", "")); /* empty path */ |
100 |
CU_TEST(1 == Asc_SetPathList("envar 1", str_path)); /* embedded space in var */ |
101 |
|
102 |
memset(str_pathbig, '.', MAX_ENV_VAR_LENGTH); |
103 |
str_pathbig[MAX_ENV_VAR_LENGTH] = '\0'; |
104 |
|
105 |
CU_TEST(1 == Asc_SetPathList(str_pathbig, str_path)); /* var too big */ |
106 |
|
107 |
Asc_DestroyEnvironment(); /* start clean in case failure cases left vars */ |
108 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
109 |
|
110 |
snprintf(str_path, STR_LEN-1, "%s", "ThisStringHasNoSpaces!"); |
111 |
|
112 |
CU_TEST(0 == Asc_SetPathList("envar1", str_path)); /* single path element */ |
113 |
paths = Asc_GetPathList("envar1", &elem_count); |
114 |
CU_TEST_FATAL(NULL != paths); |
115 |
CU_TEST_FATAL(1 == elem_count); |
116 |
CU_TEST(!strcmp(paths[0], str_path)); |
117 |
ascfree(paths); |
118 |
|
119 |
snprintf(str_path, STR_LEN-1, "%s%c%s", "ThisStringHasNoSpaces+", |
120 |
PATHDIV, |
121 |
"this one does, huh?"); |
122 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "ThisStringHasNoSpaces+"); |
123 |
snprintf(str_path_ss2, STR_LEN-1, "%s", "this one does, huh?"); |
124 |
|
125 |
CU_TEST(0 == Asc_SetPathList("envar2", str_path)); /* 2 path elements */ |
126 |
paths = Asc_GetPathList("envar2", &elem_count); |
127 |
CU_TEST_FATAL(NULL != paths); |
128 |
CU_TEST_FATAL(2 == elem_count); |
129 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
130 |
CU_TEST(!strcmp(paths[1], str_path_ss2)); |
131 |
ascfree(paths); |
132 |
|
133 |
snprintf(str_path, STR_LEN-1, "%c%s", PATHDIV, " ThisStringHasNoSpaces! "); |
134 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "ThisStringHasNoSpaces!"); |
135 |
|
136 |
CU_TEST(0 == Asc_SetPathList("envar2e1", str_path)); /* 2 elements, 1 empty */ |
137 |
paths = Asc_GetPathList("envar2e1", &elem_count); |
138 |
CU_TEST_FATAL(NULL != paths); |
139 |
CU_TEST_FATAL(1 == elem_count); |
140 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
141 |
ascfree(paths); |
142 |
|
143 |
snprintf(str_path, STR_LEN-1, "%s%c", "< I DO have spaces > ", PATHDIV); |
144 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "< I DO have spaces >"); |
145 |
|
146 |
CU_TEST(0 == Asc_SetPathList("envar2e2", str_path)); /* 2 elements, other empty */ |
147 |
paths = Asc_GetPathList("envar2e2", &elem_count); |
148 |
CU_TEST_FATAL(NULL != paths); |
149 |
CU_TEST_FATAL(1 == elem_count); |
150 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
151 |
ascfree(paths); |
152 |
|
153 |
snprintf(str_path, STR_LEN-1, "%c", PATHDIV); |
154 |
|
155 |
CU_TEST(0 == Asc_SetPathList("envar2e3", str_path)); /* 2 elements, both empty */ |
156 |
paths = Asc_GetPathList("envar2e3", &elem_count); |
157 |
CU_TEST_FATAL(NULL != paths); |
158 |
CU_TEST(paths[0] == '\0'); |
159 |
CU_TEST(0 == elem_count); |
160 |
if (NULL != paths) ascfree(paths); |
161 |
|
162 |
snprintf(str_path, STR_LEN-1, " %c ", PATHDIV); |
163 |
|
164 |
CU_TEST(0 == Asc_SetPathList("envar2e4", str_path)); /* 2 elements, both empty with ws */ |
165 |
paths = Asc_GetPathList("envar2e4", &elem_count); |
166 |
CU_TEST_FATAL(NULL != paths); |
167 |
CU_TEST(paths[0] == '\0'); |
168 |
CU_TEST(0 == elem_count); |
169 |
if (NULL != paths) ascfree(paths); |
170 |
|
171 |
snprintf(str_path, STR_LEN-1, "%s%c%s%c%s", "< spaces >", |
172 |
PATHDIV, |
173 |
"~NoSpaces~ ", |
174 |
PATHDIV, |
175 |
" another one . ~ $ "); |
176 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "< spaces >"); |
177 |
snprintf(str_path_ss2, STR_LEN-1, "%s", "~NoSpaces~"); |
178 |
snprintf(str_path_ss3, STR_LEN-1, "%s", "another one . ~ $"); |
179 |
|
180 |
CU_TEST(0 == Asc_SetPathList("envar3", str_path)); /* 3 path elements */ |
181 |
paths = Asc_GetPathList("envar3", &elem_count); |
182 |
CU_TEST_FATAL(NULL != paths); |
183 |
CU_TEST_FATAL(3 == elem_count); |
184 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
185 |
CU_TEST(!strcmp(paths[1], str_path_ss2)); |
186 |
CU_TEST(!strcmp(paths[2], str_path_ss3)); |
187 |
ascfree(paths); |
188 |
|
189 |
snprintf(str_path, STR_LEN-1, "%s%c%c%s", " < spaces > ", |
190 |
PATHDIV, |
191 |
PATHDIV, |
192 |
" another one . ~ $ "); |
193 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "< spaces >"); |
194 |
snprintf(str_path_ss2, STR_LEN-1, "%s", "another one . ~ $"); |
195 |
|
196 |
CU_TEST(0 == Asc_SetPathList("envar3e1", str_path)); /* 3 elements - middle empty */ |
197 |
paths = Asc_GetPathList("envar3e1", &elem_count); |
198 |
CU_TEST_FATAL(NULL != paths); |
199 |
CU_TEST_FATAL(2 == elem_count); |
200 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
201 |
CU_TEST(!strcmp(paths[1], str_path_ss2)); |
202 |
ascfree(paths); |
203 |
|
204 |
snprintf(str_path, STR_LEN-1, "%c%s%c", PATHDIV, |
205 |
" \t - =ASCEND rocks || \t", |
206 |
PATHDIV); |
207 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "- =ASCEND rocks ||"); |
208 |
|
209 |
CU_TEST(0 == Asc_SetPathList("envar3e2", str_path)); /* 3 elements - 1st & 3rd empty */ |
210 |
paths = Asc_GetPathList("envar3e2", &elem_count); |
211 |
CU_TEST_FATAL(NULL != paths); |
212 |
CU_TEST_FATAL(1 == elem_count); |
213 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
214 |
ascfree(paths); |
215 |
|
216 |
snprintf(str_path, STR_LEN-1, "%c %c ", PATHDIV, PATHDIV); |
217 |
|
218 |
CU_TEST(0 == Asc_SetPathList("envar3e3", str_path)); /* 3 elements - all empty */ |
219 |
paths = Asc_GetPathList("envar3e3", &elem_count); |
220 |
CU_TEST_FATAL(NULL != paths); |
221 |
CU_TEST(paths[0] == '\0'); |
222 |
CU_TEST(0 == elem_count); |
223 |
if (NULL != paths) ascfree(paths); |
224 |
|
225 |
memset(str_pathbig, '.', MAX_ENV_VAR_LENGTH-2); |
226 |
str_pathbig[MAX_ENV_VAR_LENGTH-2] = '\0'; |
227 |
|
228 |
CU_TEST(0 == Asc_SetPathList("envar4", str_pathbig)); /* large string at max length */ |
229 |
paths = Asc_GetPathList("envar4", &elem_count); |
230 |
CU_TEST_FATAL(NULL != paths); |
231 |
CU_TEST_FATAL(1 == elem_count); |
232 |
CU_TEST(!strcmp(paths[0], str_pathbig)); |
233 |
ascfree(paths); |
234 |
|
235 |
memset(str_pathbig_ss1, '%', MAX_ENV_VAR_LENGTH-1); |
236 |
str_pathbig_ss1[MAX_ENV_VAR_LENGTH-1] = '\0'; |
237 |
memset(str_pathbig_ss2, '@', MAX_ENV_VAR_LENGTH-1); |
238 |
str_pathbig_ss2[MAX_ENV_VAR_LENGTH-1] = '\0'; |
239 |
|
240 |
snprintf(str_pathbig, MAX_ENV_VAR_LENGTH*3, "%s%c%s", |
241 |
str_pathbig_ss1, |
242 |
PATHDIV, |
243 |
str_pathbig_ss2); |
244 |
|
245 |
CU_TEST(0 == Asc_SetPathList("envar5", str_pathbig)); /* multiple large strings */ |
246 |
paths = Asc_GetPathList("envar5", &elem_count); |
247 |
CU_TEST_FATAL(NULL != paths); |
248 |
CU_TEST_FATAL(2 == elem_count); |
249 |
CU_TEST(!strcmp(paths[0], str_pathbig_ss1)); |
250 |
CU_TEST(!strcmp(paths[1], str_pathbig_ss2)); |
251 |
ascfree(paths); |
252 |
|
253 |
memset(str_pathbig, '`', MAX_ENV_VAR_LENGTH*2); |
254 |
str_pathbig[MAX_ENV_VAR_LENGTH*2] = '\0'; |
255 |
memset(str_pathbig_ss1, '`', MAX_ENV_VAR_LENGTH-1); |
256 |
str_pathbig_ss1[MAX_ENV_VAR_LENGTH-1] = '\0'; |
257 |
|
258 |
CU_TEST(0 == Asc_SetPathList("envar6", str_pathbig)); /* large string over max length */ |
259 |
paths = Asc_GetPathList("envar6", &elem_count); |
260 |
CU_TEST_FATAL(NULL != paths); |
261 |
CU_TEST_FATAL(1 == elem_count); |
262 |
CU_TEST(!strcmp(paths[0], str_pathbig_ss1)); |
263 |
ascfree(paths); |
264 |
|
265 |
memset(str_pathbig, 'a', MAX_ENV_VAR_LENGTH-2); |
266 |
str_pathbig[MAX_ENV_VAR_LENGTH-2] = '\0'; |
267 |
|
268 |
CU_TEST(0 == Asc_SetPathList(str_pathbig, "\t\teasy\t\t")); /* var name at max length */ |
269 |
paths = Asc_GetPathList(str_pathbig, &elem_count); |
270 |
CU_TEST_FATAL(NULL != paths); |
271 |
CU_TEST_FATAL(1 == elem_count); |
272 |
CU_TEST(!strcmp(paths[0], "easy")); |
273 |
ascfree(paths); |
274 |
|
275 |
name_list = gl_create(15); |
276 |
CU_TEST_FATAL(NULL != name_list); |
277 |
gl_append_ptr(name_list, "envar1"); |
278 |
gl_append_ptr(name_list, "envar2"); |
279 |
gl_append_ptr(name_list, "envar2e1"); |
280 |
gl_append_ptr(name_list, "envar2e2"); |
281 |
gl_append_ptr(name_list, "envar2e3"); |
282 |
gl_append_ptr(name_list, "envar2e4"); |
283 |
gl_append_ptr(name_list, "envar3"); |
284 |
gl_append_ptr(name_list, "envar3e1"); |
285 |
gl_append_ptr(name_list, "envar3e2"); |
286 |
gl_append_ptr(name_list, "envar3e3"); |
287 |
gl_append_ptr(name_list, "envar4"); |
288 |
gl_append_ptr(name_list, "envar5"); |
289 |
gl_append_ptr(name_list, "envar6"); |
290 |
gl_append_ptr(name_list, str_pathbig); |
291 |
|
292 |
paths = Asc_EnvNames(&elem_count); /* check the registered names */ |
293 |
CU_TEST(NULL != paths); |
294 |
CU_TEST(14 == elem_count); |
295 |
for (i=0 ; i<elem_count ; ++i) { |
296 |
if (0 == gl_search(name_list, paths[i], compare_strings)) { |
297 |
snprintf(str_pathbig, MAX_ENV_VAR_LENGTH*3, "Environment variable name not registered: %s", |
298 |
paths[i]); |
299 |
CU_FAIL(str_pathbig); |
300 |
} |
301 |
} |
302 |
ascfree(paths); |
303 |
gl_destroy(name_list); |
304 |
|
305 |
Asc_DestroyEnvironment(); |
306 |
|
307 |
/* test Asc_PutEnv() */ |
308 |
|
309 |
snprintf(str_path, STR_LEN-1, "envar1=This is my string"); |
310 |
|
311 |
CU_TEST(1 == Asc_PutEnv(str_path)); /* not initialized */ |
312 |
|
313 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
314 |
|
315 |
CU_TEST(1 == Asc_PutEnv(NULL)); /* NULL string */ |
316 |
CU_TEST(1 == Asc_PutEnv("")); /* empty string */ |
317 |
CU_TEST(1 == Asc_PutEnv("=my_path_is_silly")); /* empty var name */ |
318 |
CU_TEST(1 == Asc_PutEnv("envar 1=my_path_is_silly")); /* embedded space in var */ |
319 |
CU_TEST(1 == Asc_PutEnv("my_path_is_silly")); /* no '=' */ |
320 |
|
321 |
memset(str_pathbig, '.', MAX_ENV_VAR_LENGTH-1); |
322 |
str_pathbig[MAX_ENV_VAR_LENGTH-1] = '='; |
323 |
sprintf(str_pathbig+MAX_ENV_VAR_LENGTH-1, "path"); |
324 |
|
325 |
CU_TEST(1 == Asc_PutEnv(str_pathbig)); /* var name too big */ |
326 |
|
327 |
Asc_DestroyEnvironment(); /* start clean in case failure cases left vars */ |
328 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
329 |
|
330 |
snprintf(str_path, STR_LEN-1, "%s", "envar1=ThisStringHasNoSpaces!"); |
331 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "ThisStringHasNoSpaces!"); |
332 |
|
333 |
CU_TEST(0 == Asc_PutEnv(str_path)); /* single path element */ |
334 |
paths = Asc_GetPathList("envar1", &elem_count); |
335 |
CU_TEST_FATAL(NULL != paths); |
336 |
CU_TEST_FATAL(1 == elem_count); |
337 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
338 |
ascfree(paths); |
339 |
|
340 |
snprintf(str_path, STR_LEN-1, "envar2 =\t %s%c%s", "ThisStringHasNoSpaces+", |
341 |
PATHDIV, |
342 |
"this one does, huh?"); |
343 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "ThisStringHasNoSpaces+"); |
344 |
snprintf(str_path_ss2, STR_LEN-1, "%s", "this one does, huh?"); |
345 |
|
346 |
CU_TEST(0 == Asc_PutEnv(str_path)); /* 2 path elements */ |
347 |
paths = Asc_GetPathList("envar2", &elem_count); |
348 |
CU_TEST_FATAL(NULL != paths); |
349 |
CU_TEST_FATAL(2 == elem_count); |
350 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
351 |
CU_TEST(!strcmp(paths[1], str_path_ss2)); |
352 |
ascfree(paths); |
353 |
|
354 |
snprintf(str_path, STR_LEN-1, " envar2e1 = %c%s", PATHDIV, " ThisStringHasNoSpaces! "); |
355 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "ThisStringHasNoSpaces!"); |
356 |
|
357 |
CU_TEST(0 == Asc_PutEnv(str_path)); /* 2 elements, 1 empty */ |
358 |
paths = Asc_GetPathList("envar2e1", &elem_count); |
359 |
CU_TEST_FATAL(NULL != paths); |
360 |
CU_TEST_FATAL(1 == elem_count); |
361 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
362 |
ascfree(paths); |
363 |
|
364 |
snprintf(str_path, STR_LEN-1, "envar2e2= %s%c ", "< I DO have spaces > ", PATHDIV); |
365 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "< I DO have spaces >"); |
366 |
|
367 |
CU_TEST(0 == Asc_PutEnv(str_path)); /* 2 elements, other empty */ |
368 |
paths = Asc_GetPathList("envar2e2", &elem_count); |
369 |
CU_TEST_FATAL(NULL != paths); |
370 |
CU_TEST_FATAL(1 == elem_count); |
371 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
372 |
ascfree(paths); |
373 |
|
374 |
snprintf(str_path, STR_LEN-1, "envar2e3=%c", PATHDIV); |
375 |
|
376 |
CU_TEST(0 == Asc_PutEnv(str_path)); /* 2 elements, both empty */ |
377 |
paths = Asc_GetPathList("envar2e3", &elem_count); |
378 |
CU_TEST_FATAL(NULL != paths); |
379 |
CU_TEST(paths[0] == '\0'); |
380 |
CU_TEST(0 == elem_count); |
381 |
if (NULL != paths) ascfree(paths); |
382 |
|
383 |
snprintf(str_path, STR_LEN-1, "\t envar2e4 = %c ", PATHDIV); |
384 |
|
385 |
CU_TEST(0 == Asc_PutEnv(str_path)); /* 2 elements, both empty with ws */ |
386 |
paths = Asc_GetPathList("envar2e4", &elem_count); |
387 |
CU_TEST_FATAL(NULL != paths); |
388 |
CU_TEST(paths[0] == '\0'); |
389 |
CU_TEST(0 == elem_count); |
390 |
if (NULL != paths) ascfree(paths); |
391 |
|
392 |
snprintf(str_path, STR_LEN-1, "envar3=%s%c%s%c%s", "< spaces >", |
393 |
PATHDIV, |
394 |
"~NoSpaces~ ", |
395 |
PATHDIV, |
396 |
" another one . ~ $ "); |
397 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "< spaces >"); |
398 |
snprintf(str_path_ss2, STR_LEN-1, "%s", "~NoSpaces~"); |
399 |
snprintf(str_path_ss3, STR_LEN-1, "%s", "another one . ~ $"); |
400 |
|
401 |
CU_TEST(0 == Asc_PutEnv(str_path)); /* 3 path elements */ |
402 |
paths = Asc_GetPathList("envar3", &elem_count); |
403 |
CU_TEST_FATAL(NULL != paths); |
404 |
CU_TEST_FATAL(3 == elem_count); |
405 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
406 |
CU_TEST(!strcmp(paths[1], str_path_ss2)); |
407 |
CU_TEST(!strcmp(paths[2], str_path_ss3)); |
408 |
ascfree(paths); |
409 |
|
410 |
snprintf(str_path, STR_LEN-1, "envar3e1\t=%s%c%c%s", " < spaces > ", |
411 |
PATHDIV, |
412 |
PATHDIV, |
413 |
" another one . ~ $ "); |
414 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "< spaces >"); |
415 |
snprintf(str_path_ss2, STR_LEN-1, "%s", "another one . ~ $"); |
416 |
|
417 |
CU_TEST(0 == Asc_PutEnv(str_path)); /* 3 elements - middle empty */ |
418 |
paths = Asc_GetPathList("envar3e1", &elem_count); |
419 |
CU_TEST_FATAL(NULL != paths); |
420 |
CU_TEST_FATAL(2 == elem_count); |
421 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
422 |
CU_TEST(!strcmp(paths[1], str_path_ss2)); |
423 |
ascfree(paths); |
424 |
|
425 |
snprintf(str_path, STR_LEN-1, "envar3e2=%c%s%c", PATHDIV, |
426 |
" \t - =ASCEND rocks || \t", |
427 |
PATHDIV); |
428 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "- =ASCEND rocks ||"); |
429 |
|
430 |
CU_TEST(0 == Asc_PutEnv(str_path)); /* 3 elements - 1st & 3rd empty */ |
431 |
paths = Asc_GetPathList("envar3e2", &elem_count); |
432 |
CU_TEST_FATAL(NULL != paths); |
433 |
CU_TEST_FATAL(1 == elem_count); |
434 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
435 |
ascfree(paths); |
436 |
|
437 |
snprintf(str_path, STR_LEN-1, "envar3e3=%c %c ", PATHDIV, PATHDIV); |
438 |
|
439 |
CU_TEST(0 == Asc_PutEnv(str_path)); /* 3 elements - all empty */ |
440 |
paths = Asc_GetPathList("envar3e3", &elem_count); |
441 |
CU_TEST_FATAL(NULL != paths); |
442 |
CU_TEST(paths[0] == '\0'); |
443 |
CU_TEST(0 == elem_count); |
444 |
if (NULL != paths) ascfree(paths); |
445 |
|
446 |
memset(str_pathbig_ss1, '.', MAX_ENV_VAR_LENGTH-9); |
447 |
str_pathbig_ss1[MAX_ENV_VAR_LENGTH-9] = '\0'; |
448 |
snprintf(str_pathbig, MAX_ENV_VAR_LENGTH-1, "envar4=%s", str_pathbig_ss1); |
449 |
|
450 |
CU_TEST(0 == Asc_PutEnv(str_pathbig)); /* large string at max length */ |
451 |
paths = Asc_GetPathList("envar4", &elem_count); |
452 |
CU_TEST_FATAL(NULL != paths); |
453 |
CU_TEST_FATAL(1 == elem_count); |
454 |
CU_TEST(!strcmp(paths[0], str_pathbig_ss1)); |
455 |
ascfree(paths); |
456 |
|
457 |
memset(str_pathbig_ss1, 'a', MAX_ENV_VAR_LENGTH-3); |
458 |
str_pathbig_ss1[MAX_ENV_VAR_LENGTH-3] = '\0'; |
459 |
snprintf(str_pathbig, MAX_ENV_VAR_LENGTH, "%s=1", str_pathbig_ss1); |
460 |
|
461 |
CU_TEST(0 == Asc_PutEnv(str_pathbig)); /* large string at max length */ |
462 |
paths = Asc_GetPathList(str_pathbig_ss1, &elem_count); |
463 |
CU_TEST_FATAL(NULL != paths); |
464 |
CU_TEST_FATAL(1 == elem_count); |
465 |
CU_TEST(!strcmp(paths[0], "1")); |
466 |
ascfree(paths); |
467 |
|
468 |
name_list = gl_create(15); |
469 |
CU_TEST_FATAL(NULL != name_list); |
470 |
gl_append_ptr(name_list, "envar1"); |
471 |
gl_append_ptr(name_list, "envar2"); |
472 |
gl_append_ptr(name_list, "envar2e1"); |
473 |
gl_append_ptr(name_list, "envar2e2"); |
474 |
gl_append_ptr(name_list, "envar2e3"); |
475 |
gl_append_ptr(name_list, "envar2e4"); |
476 |
gl_append_ptr(name_list, "envar3"); |
477 |
gl_append_ptr(name_list, "envar3e1"); |
478 |
gl_append_ptr(name_list, "envar3e2"); |
479 |
gl_append_ptr(name_list, "envar3e3"); |
480 |
gl_append_ptr(name_list, "envar4"); |
481 |
gl_append_ptr(name_list, str_pathbig_ss1); |
482 |
|
483 |
paths = Asc_EnvNames(&elem_count); /* check the registered names */ |
484 |
CU_TEST(NULL != paths); |
485 |
CU_TEST(12 == elem_count); |
486 |
for (i=0 ; i<elem_count ; ++i) { |
487 |
if (0 == gl_search(name_list, paths[i], compare_strings)) { |
488 |
snprintf(str_pathbig, MAX_ENV_VAR_LENGTH*3, "Environment variable name not registered: %s", |
489 |
paths[i]); |
490 |
CU_FAIL(str_pathbig); |
491 |
} |
492 |
} |
493 |
ascfree(paths); |
494 |
gl_destroy(name_list); |
495 |
|
496 |
Asc_DestroyEnvironment(); /* clean up */ |
497 |
|
498 |
/* test Asc_ImportPathList() */ |
499 |
|
500 |
CU_TEST(1 == Asc_ImportPathList("envar1")); /* not initialized */ |
501 |
|
502 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
503 |
|
504 |
CU_TEST(1 == Asc_ImportPathList(NULL)); /* NULL varname */ |
505 |
CU_TEST(1 == Asc_ImportPathList("")); /* empty varname */ |
506 |
|
507 |
Asc_DestroyEnvironment(); /* start with a clean list */ |
508 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
509 |
|
510 |
if (NULL != getenv(test_ext_varname)) { /* make sure our external env var doesn't exist */ |
511 |
snprintf(str_path, STR_LEN-1, |
512 |
"External environment variable '%s' already exists. Aborting test of Asc_ImportPathList().", |
513 |
test_ext_varname); |
514 |
str_path[STR_LEN-1] = '\0'; |
515 |
CU_FAIL(str_path); |
516 |
} |
517 |
else { |
518 |
|
519 |
CU_TEST(1 == Asc_ImportPathList(test_ext_varname)); /* non-existent external env var */ |
520 |
|
521 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "iHaveNoSpaces"); |
522 |
snprintf(str_path, STR_LEN-1, "%s=%s", test_ext_varname, str_path_ss1); |
523 |
CU_TEST_FATAL(0 == putenv(str_path)); |
524 |
|
525 |
CU_TEST(0 == Asc_ImportPathList(test_ext_varname)); /* import single path env var */ |
526 |
paths = Asc_GetPathList(test_ext_varname, &elem_count); |
527 |
CU_TEST_FATAL(NULL != paths); |
528 |
CU_TEST_FATAL(1 == elem_count); |
529 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
530 |
if (NULL != paths) ascfree(paths); |
531 |
|
532 |
snprintf(str_path_ss1, STR_LEN-1, "%s", "~This one has spaces~"); |
533 |
snprintf(str_path_ss2, STR_LEN-1, "%s", "<>NotMe<>"); |
534 |
snprintf(str_path, STR_LEN-1, "%s=%s%c%s", |
535 |
test_ext_varname, |
536 |
str_path_ss1, |
537 |
PATHDIV, |
538 |
str_path_ss2); |
539 |
CU_TEST_FATAL(0 == putenv(str_path)); |
540 |
|
541 |
CU_TEST(0 == Asc_ImportPathList(test_ext_varname)); /* import double path env var */ |
542 |
paths = Asc_GetPathList(test_ext_varname, &elem_count); |
543 |
CU_TEST_FATAL(NULL != paths); |
544 |
CU_TEST_FATAL(2 == elem_count); |
545 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
546 |
CU_TEST(!strcmp(paths[1], str_path_ss2)); |
547 |
if (NULL != paths) ascfree(paths); |
548 |
|
549 |
snprintf(str_path_ss1, STR_LEN-1, "%cusr%clocal%clib", SLASH, SLASH, SLASH); |
550 |
snprintf(str_path_ss2, STR_LEN-1, "%cc%cWindows%cTemp", SLASH, SLASH, SLASH); |
551 |
snprintf(str_path_ss3, STR_LEN-1, "server%c%caccount%csubfolder", SLASH, SLASH, SLASH); |
552 |
snprintf(str_path, STR_LEN-1, "%s=%s%c%s%c%s", |
553 |
test_ext_varname, |
554 |
str_path_ss1, |
555 |
PATHDIV, |
556 |
str_path_ss2, |
557 |
PATHDIV, |
558 |
str_path_ss3); |
559 |
CU_TEST_FATAL(0 == putenv(str_path)); |
560 |
|
561 |
CU_TEST(0 == Asc_ImportPathList(test_ext_varname)); /* import double path env var */ |
562 |
paths = Asc_GetPathList(test_ext_varname, &elem_count); |
563 |
CU_TEST_FATAL(NULL != paths); |
564 |
CU_TEST_FATAL(3 == elem_count); |
565 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
566 |
CU_TEST(!strcmp(paths[1], str_path_ss2)); |
567 |
CU_TEST(!strcmp(paths[2], str_path_ss3)); |
568 |
if (NULL != paths) ascfree(paths); |
569 |
} |
570 |
|
571 |
snprintf(str_path, STR_LEN-1, "%s=", test_ext_varname); /* clear the temporary external env var */ |
572 |
CU_TEST(0 == putenv(str_path)); |
573 |
Asc_DestroyEnvironment(); /* clean up */ |
574 |
|
575 |
/* test Asc_AppendPath() */ |
576 |
|
577 |
CU_TEST(1 == Asc_AppendPath("envar1", str_path)); /* not initialized */ |
578 |
|
579 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
580 |
|
581 |
CU_TEST(1 == Asc_AppendPath(NULL, str_path)); /* NULL var */ |
582 |
CU_TEST(1 == Asc_AppendPath("envar1", NULL)); /* NULL path */ |
583 |
CU_TEST(1 == Asc_AppendPath("", str_path)); /* empty var */ |
584 |
CU_TEST(1 == Asc_AppendPath("envar1", "")); /* empty path */ |
585 |
|
586 |
Asc_DestroyEnvironment(); /* start clean in case failure cases left vars */ |
587 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
588 |
|
589 |
snprintf(str_path_ss1, STR_LEN-1, "path/with\\many;unusual:chars"); |
590 |
snprintf(str_path_ss2, STR_LEN-1, " What should I test next? "); |
591 |
snprintf(str_path_ss3, STR_LEN-1, "T"); |
592 |
|
593 |
CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss1)); /* append a path to a new variable */ |
594 |
paths = Asc_GetPathList("envar1", &elem_count); |
595 |
CU_TEST_FATAL(NULL != paths); |
596 |
CU_TEST_FATAL(1 == elem_count); |
597 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
598 |
ascfree(paths); |
599 |
|
600 |
CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss1)); /* append a path 2nd time */ |
601 |
paths = Asc_GetPathList("envar1", &elem_count); |
602 |
CU_TEST_FATAL(NULL != paths); |
603 |
CU_TEST_FATAL(2 == elem_count); |
604 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
605 |
CU_TEST(!strcmp(paths[1], str_path_ss1)); |
606 |
ascfree(paths); |
607 |
|
608 |
CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss2)); /* append a new path */ |
609 |
paths = Asc_GetPathList("envar1", &elem_count); |
610 |
CU_TEST_FATAL(NULL != paths); |
611 |
CU_TEST_FATAL(3 == elem_count); |
612 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
613 |
CU_TEST(!strcmp(paths[1], str_path_ss1)); |
614 |
CU_TEST(!strcmp(paths[2], str_path_ss2)); |
615 |
ascfree(paths); |
616 |
|
617 |
CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss1)); /* append a path a 3rd time */ |
618 |
paths = Asc_GetPathList("envar1", &elem_count); |
619 |
CU_TEST_FATAL(NULL != paths); |
620 |
CU_TEST_FATAL(4 == elem_count); |
621 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
622 |
CU_TEST(!strcmp(paths[1], str_path_ss1)); |
623 |
CU_TEST(!strcmp(paths[2], str_path_ss2)); |
624 |
CU_TEST(!strcmp(paths[3], str_path_ss1)); |
625 |
ascfree(paths); |
626 |
|
627 |
CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss3)); /* append a new path */ |
628 |
paths = Asc_GetPathList("envar1", &elem_count); |
629 |
CU_TEST_FATAL(NULL != paths); |
630 |
CU_TEST_FATAL(5 == elem_count); |
631 |
CU_TEST(!strcmp(paths[0], str_path_ss1)); |
632 |
CU_TEST(!strcmp(paths[1], str_path_ss1)); |
633 |
CU_TEST(!strcmp(paths[2], str_path_ss2)); |
634 |
CU_TEST(!strcmp(paths[3], str_path_ss1)); |
635 |
CU_TEST(!strcmp(paths[4], str_path_ss3)); |
636 |
ascfree(paths); |
637 |
|
638 |
Asc_DestroyEnvironment(); /* clean up */ |
639 |
|
640 |
/* test Asc_GetPathList() - test unusual cases - main use covered in other tests */ |
641 |
|
642 |
paths = Asc_GetPathList("envar1", &elem_count); /* not initialized */ |
643 |
CU_TEST(-1 == elem_count); |
644 |
CU_TEST(NULL == paths); |
645 |
|
646 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
647 |
|
648 |
paths = Asc_GetPathList(NULL, &elem_count); /* NULL var */ |
649 |
CU_TEST(-1 == elem_count); |
650 |
CU_TEST(NULL == paths); |
651 |
|
652 |
paths = Asc_GetPathList("envar1", NULL); /* NULL argcPtr */ |
653 |
CU_TEST(-1 == elem_count); |
654 |
CU_TEST(NULL == paths); |
655 |
|
656 |
paths = Asc_GetPathList("", &elem_count); /* empty var */ |
657 |
CU_TEST(0 == elem_count); |
658 |
CU_TEST(NULL == paths); |
659 |
|
660 |
paths = Asc_GetPathList("envar1", &elem_count); /* non-existent var */ |
661 |
CU_TEST(0 == elem_count); |
662 |
CU_TEST(NULL == paths); |
663 |
|
664 |
Asc_DestroyEnvironment(); /* start clean in case failure cases left vars */ |
665 |
|
666 |
/* test Asc_GetEnv() */ |
667 |
|
668 |
str_env = Asc_GetEnv("envar1"); /* not initialized */ |
669 |
CU_TEST(NULL == str_env); |
670 |
|
671 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
672 |
|
673 |
str_env = Asc_GetEnv(NULL); /* NULL var */ |
674 |
CU_TEST(NULL == str_env); |
675 |
|
676 |
str_env = Asc_GetEnv(""); /* empty var */ |
677 |
CU_TEST(NULL == str_env); |
678 |
|
679 |
str_env = Asc_GetEnv("envar1"); /* non-existent var */ |
680 |
CU_TEST(NULL == str_env); |
681 |
|
682 |
snprintf(str_path_ss1, STR_LEN-1, "path/with\\many;unusual:chars"); |
683 |
snprintf(str_path_ss2, STR_LEN-1, " What should I test next? "); |
684 |
snprintf(str_path_ss3, STR_LEN-1, "T"); |
685 |
snprintf(str_path, STR_LEN-1, "%s%c%s%c%s", str_path_ss1, PATHDIV, str_path_ss2, PATHDIV, str_path_ss3); |
686 |
|
687 |
CU_TEST(0 == Asc_SetPathList("envar1", str_path_ss1)); |
688 |
str_env = Asc_GetEnv("envar1"); |
689 |
CU_TEST_FATAL(NULL != str_env); |
690 |
CU_TEST(!strcmp(str_env, str_path_ss1)); |
691 |
ascfree(str_env); |
692 |
|
693 |
CU_TEST(0 == Asc_SetPathList("envar1", str_path_ss3)); |
694 |
str_env = Asc_GetEnv("envar1"); |
695 |
CU_TEST_FATAL(NULL != str_env); |
696 |
CU_TEST(!strcmp(str_env, str_path_ss3)); |
697 |
ascfree(str_env); |
698 |
|
699 |
CU_TEST(0 == Asc_SetPathList("envar1", str_path_ss1)); |
700 |
CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss2)); |
701 |
CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss3)); |
702 |
str_env = Asc_GetEnv("envar1"); |
703 |
CU_TEST_FATAL(NULL != str_env); |
704 |
CU_TEST(!strcmp(str_env, str_path)); |
705 |
ascfree(str_env); |
706 |
|
707 |
Asc_DestroyEnvironment(); /* clean up */ |
708 |
|
709 |
/* test Asc_EnvNames() */ |
710 |
|
711 |
paths = Asc_EnvNames(&elem_count); /* not initialized */ |
712 |
CU_TEST(NULL == paths); |
713 |
CU_TEST(-1 == elem_count); |
714 |
|
715 |
CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */ |
716 |
|
717 |
paths = Asc_EnvNames(&elem_count); /* empty environment list */ |
718 |
CU_TEST_FATAL(NULL != paths); |
719 |
CU_TEST(0 == elem_count); |
720 |
ascfree(paths); |
721 |
|
722 |
Asc_SetPathList("envar1", "my val 1"); |
723 |
paths = Asc_EnvNames(&elem_count); /* 1 environment var */ |
724 |
CU_TEST_FATAL(NULL != paths); |
725 |
CU_TEST_FATAL(1 == elem_count); |
726 |
CU_TEST(!strcmp(paths[0], "envar1")); |
727 |
ascfree(paths); |
728 |
|
729 |
Asc_SetPathList("envar2", "my val 2"); |
730 |
paths = Asc_EnvNames(&elem_count); /* 2 environment var */ |
731 |
CU_TEST_FATAL(NULL != paths); |
732 |
CU_TEST_FATAL(2 == elem_count); |
733 |
CU_TEST(!strcmp(paths[0], "envar1")); |
734 |
CU_TEST(!strcmp(paths[1], "envar2")); |
735 |
ascfree(paths); |
736 |
|
737 |
Asc_SetPathList("envar3", "my val 3"); |
738 |
paths = Asc_EnvNames(&elem_count); /* 2 environment var */ |
739 |
CU_TEST_FATAL(NULL != paths); |
740 |
CU_TEST_FATAL(3 == elem_count); |
741 |
CU_TEST(!strcmp(paths[0], "envar1")); |
742 |
CU_TEST(!strcmp(paths[1], "envar2")); |
743 |
CU_TEST(!strcmp(paths[2], "envar3")); |
744 |
ascfree(paths); |
745 |
|
746 |
Asc_DestroyEnvironment(); /* start with a clean list */ |
747 |
|
748 |
if (TRUE == i_initialized_lists) { |
749 |
gl_destroy_pool(); |
750 |
} |
751 |
|
752 |
CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */ |
753 |
} |
754 |
|
755 |
/*===========================================================================*/ |
756 |
/* Registration information */ |
757 |
|
758 |
static CU_TestInfo ascEnvVar_test_list[] = { |
759 |
{"test_ascEnvVar", test_ascEnvVar}, |
760 |
CU_TEST_INFO_NULL |
761 |
}; |
762 |
|
763 |
static CU_SuiteInfo suites[] = { |
764 |
{"test_utilities_ascEnvVar", NULL, NULL, ascEnvVar_test_list}, |
765 |
CU_SUITE_INFO_NULL |
766 |
}; |
767 |
|
768 |
/*-------------------------------------------------------------------*/ |
769 |
CU_ErrorCode test_register_utilities_ascEnvVar(void) |
770 |
{ |
771 |
return CU_register_suites(suites); |
772 |
} |