This repository was archived by the owner on Apr 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_env.c
More file actions
107 lines (91 loc) · 2.26 KB
/
Copy pathtest_env.c
File metadata and controls
107 lines (91 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "env.h"
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
#endif
static int test(void)
{
struct env env;
int res;
const char *testnames[] = {
"carbon", "argon", "oxygen", "helium",
"hydrogen", "", "this name has spaces", "uranium-235",
};
const char *testvalues[] = {
"cat", "dog", "pigeon", "giraffe",
"", "monkey", "bear", "rabbit",
};
unsigned i, j;
env_init(&env);
/* set various combinations */
for (j = 0; j < ARRAY_SIZE(testvalues); j++) {
for (i = 0; i < ARRAY_SIZE(testnames); i++) {
assert(testvalues[j] != NULL);
res = env_set(&env, testnames[i], testvalues[j]);
if (res) {
fprintf(stderr, "%s:env_set() failed\n", __FILE__);
return res;
}
}
}
/* set a specific pattern of values */
assert(ARRAY_SIZE(testnames) == ARRAY_SIZE(testvalues));
for (i = 0; i < ARRAY_SIZE(testnames); i++) {
res = env_set(&env, testnames[i], testvalues[i]);
if (res) {
fprintf(stderr, "%s:env_set() failed\n", __FILE__);
return res;
}
}
/* delete every other name */
for (i = 0; i < ARRAY_SIZE(testnames); i += 2) {
res = env_delete(&env, testnames[i]);
if (res) {
fprintf(stderr, "%s:env_delete() failed\n", __FILE__);
return res;
}
}
/* print the remaining list */
for (i = 1; i < ARRAY_SIZE(testnames); i += 2) {
const char *value = env_get(&env, testnames[i]);
if (!value) {
fprintf(stderr, "%s:null value for \"%s\"\n",
__FILE__, testnames[i]);
return -1;
}
fprintf(stderr, "%s:test result:\"%s\"=\"%s\"\n",
__FILE__, testnames[i], value);
/* check that value is correct */
if (strcmp(value, testvalues[i])) {
fprintf(stderr, "%s:env_get() failed\n", __FILE__);
return -1;
}
}
/* start over */
env_init(&env);
/* try to overflow the buffers */
for (i = 0; i < sizeof(env.heap); i++) {
char name[64];
char value[64];
int e;
snprintf(name, sizeof(name), "name%u", i);
snprintf(value, sizeof(value), "value is %u", i);
e = env_set(&env, name, value);
if (e) {
fprintf(stderr, "%s:env_set() returned %d\n", __FILE__, e);
break;
}
}
return 0;
}
int main()
{
if (test()) {
printf("%s:Test Failure\n", __FILE__);
return 1;
}
printf("%s:Test Success\n", __FILE__);
return 0;
}