-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgitest.c
96 lines (89 loc) · 2.36 KB
/
cgitest.c
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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cgi.h"
struct var_spec {
char* var;
int n_values;
char* values[5];
};
struct test_spec {
char* input;
int n_vars;
struct var_spec vars[5];
};
struct test_spec specs[] = {
{"a=b", 1,
{{"a", 1,
{"b"}
}}
},
{"a=b&a=b", 1,
{{"a", 2,
{"b", "b"}
}}
},
{"a=b&a=c", 1,
{{"a", 2,
{"b", "c"}
}}
},
{"a=b&c=d&a=e", 1,
{{"a", 2,
{"b", "e"}
},
{"c", 1,
{"d"}
}
}
},
{"a=b&c=%3dc", 1,
{{"a", 1,
{"b"}
},
{"c", 1,
{"=c"}
}
}
},
{0}
};
int main() {
int bad = 0;
for (struct test_spec* spec = specs; spec->input; ++spec) {
setenv("QUERY_STRING", spec->input, 1);
struct cgi* cgi = cgi_init();
for (int var = 0; var < spec->n_vars; ++var) {
struct var_spec expected_var = spec->vars[var];
struct cgi_var* cgi_var = cgi_get_var(cgi, expected_var.var);
if (!cgi_var) {
printf("Query string %s: missing var %s\n",
spec->input, expected_var.var);
bad ++;
} else if (strcmp(cgi_var->var, expected_var.var)) {
printf("Query string %s: misnamed var %s (got %s)\n",
spec->input, expected_var.var, cgi_var->var);
bad++;
} else if (cgi_var->n_values != expected_var.n_values) {
printf("Query string %s: wrong number of values var %s (%d for %d)\n",
spec->input, expected_var.var,
cgi_var->n_values, expected_var.n_values);
bad++;
} else {
for (int val = 0; val < expected_var.n_values; ++val) {
if (strcmp(cgi_var->values[val], expected_var.values[val])) {
printf("Query string %s: wrong values var %s (%s for %s)\n",
spec->input, expected_var.var,
cgi_var->values[val], expected_var.values[val]);
bad++;
}
}
}
}
}
if (bad) {
printf ("Found %d errors\n", bad);
}
return bad;
}