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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
// Tests for uri.h - URI parsing and serialization
#define URI_IMPLEMENTATION
#include "../include/uri.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ASSERT(cond, msg) \
do { \
if (!(cond)) { \
fprintf(stderr, "FAIL: %s (test_uri.c:%d)\n", msg, __LINE__); \
failures++; \
} \
} while (0)
#define ASSERT_STR_EQ(a, b, msg) \
do { \
if ((a) == NULL || (b) == NULL || strcmp((a), (b)) != 0) { \
fprintf(stderr, "FAIL: %s (got '%s', expected '%s') (test_uri.c:%d)\n", \
msg, (a) ? (a) : "NULL", (b) ? (b) : "NULL", __LINE__); \
failures++; \
} \
} while (0)
static int failures = 0;
void test_init() {
uri_t *u = uri_init();
ASSERT(u != NULL, "init returns non-NULL");
for (int i = 0; i < URI_END; i++) {
ASSERT(u->data[i] == NULL, "all fields NULL");
}
uri_free(u);
}
void test_set_and_get() {
uri_t *u = uri_init();
uri_set(u, SCHEME, "https");
uri_set(u, HOST, "example.com");
uri_set(u, PATH, "/api/v1");
char *uri = uri_get(u);
ASSERT(uri != NULL, "get returns non-NULL");
ASSERT_STR_EQ(uri, "https://example.com/api/v1", "constructed URI");
free(uri);
uri_free(u);
}
void test_set_with_port() {
uri_t *u = uri_init();
uri_set(u, SCHEME, "http");
uri_set(u, HOST, "localhost");
uri_set(u, PORT, "8080");
char *uri = uri_get(u);
ASSERT_STR_EQ(uri, "http://localhost:8080", "URI with port");
free(uri);
uri_free(u);
}
void test_set_with_query() {
uri_t *u = uri_init();
uri_set(u, SCHEME, "https");
uri_set(u, HOST, "example.com");
uri_set(u, PATH, "/search");
uri_set(u, QUERY, "q=test");
char *uri = uri_get(u);
ASSERT_STR_EQ(uri, "https://example.com/search?q=test", "URI with query");
free(uri);
uri_free(u);
}
void test_set_with_fragment() {
uri_t *u = uri_init();
uri_set(u, SCHEME, "https");
uri_set(u, HOST, "example.com");
uri_set(u, PATH, "/page");
uri_set(u, FRAGMENT, "section");
char *uri = uri_get(u);
ASSERT_STR_EQ(uri, "https://example.com/page#section", "URI with fragment");
free(uri);
uri_free(u);
}
void test_full_uri() {
uri_t *u = uri_init();
uri_set(u, SCHEME, "https");
uri_set(u, USERINFO, "user:pass");
uri_set(u, HOST, "example.com");
uri_set(u, PORT, "443");
uri_set(u, PATH, "/path/to/resource");
uri_set(u, QUERY, "key=value");
uri_set(u, FRAGMENT, "anchor");
char *uri = uri_get(u);
ASSERT(uri != NULL, "full URI constructed");
uri_free(u);
free(uri);
}
void test_parse_basic() {
uri_t *u = uri_parse("https://example.com/path");
ASSERT(u != NULL, "parse returns non-NULL");
ASSERT_STR_EQ(u->data[SCHEME], "https", "parsed scheme");
ASSERT_STR_EQ(u->data[HOST], "example.com", "parsed host");
ASSERT_STR_EQ(u->data[PATH], "/path", "parsed path");
uri_free(u);
}
void test_parse_with_port() {
uri_t *u = uri_parse("http://localhost:8080/api");
ASSERT(u != NULL, "parse with port");
ASSERT_STR_EQ(u->data[SCHEME], "http", "scheme");
ASSERT_STR_EQ(u->data[HOST], "localhost", "host");
ASSERT_STR_EQ(u->data[PORT], "8080", "port");
ASSERT_STR_EQ(u->data[PATH], "/api", "path");
uri_free(u);
}
void test_parse_with_query() {
uri_t *u = uri_parse("https://example.com/search?q=hello");
ASSERT(u != NULL, "parse with query");
ASSERT_STR_EQ(u->data[QUERY], "q=hello", "query parsed");
uri_free(u);
}
void test_parse_with_fragment() {
uri_t *u = uri_parse("https://example.com/page#top");
ASSERT(u != NULL, "parse with fragment");
ASSERT_STR_EQ(u->data[FRAGMENT], "top", "fragment parsed");
uri_free(u);
}
void test_parse_full() {
uri_t *u = uri_parse("https://user:pass@example.com:443/path?q=1#frag");
ASSERT(u != NULL, "parse full URI");
ASSERT_STR_EQ(u->data[SCHEME], "https", "scheme");
ASSERT_STR_EQ(u->data[USERINFO], "user:pass", "userinfo parsed");
ASSERT_STR_EQ(u->data[HOST], "example.com", "host parsed");
ASSERT_STR_EQ(u->data[PORT], "443", "port parsed");
ASSERT_STR_EQ(u->data[PATH], "/path", "path");
ASSERT_STR_EQ(u->data[QUERY], "q=1", "query");
ASSERT_STR_EQ(u->data[FRAGMENT], "frag", "fragment");
uri_free(u);
}
void test_parse_invalid() {
uri_t *u = uri_parse("not-a-uri");
ASSERT(u == NULL, "invalid URI returns NULL");
}
void test_set_override() {
uri_t *u = uri_init();
uri_set(u, SCHEME, "http");
uri_set(u, HOST, "old.com");
uri_set(u, SCHEME, "https");
uri_set(u, HOST, "new.com");
char *uri = uri_get(u);
ASSERT_STR_EQ(uri, "https://new.com", "override works");
free(uri);
uri_free(u);
}
void test_roundtrip() {
char const *original = "https://example.com:8080/path?q=test#anchor";
uri_t *u = uri_parse(original);
ASSERT(u != NULL, "parse for roundtrip");
if (!u)
return;
char *rebuilt = uri_get(u);
ASSERT(rebuilt != NULL, "rebuilt URI");
if (!rebuilt) {
uri_free(u);
return;
}
ASSERT(strcmp(rebuilt, original) == 0, "roundtrip matches original");
uri_free(u);
free(rebuilt);
}
void test_minimal_uri() {
uri_t *u = uri_init();
uri_set(u, SCHEME, "file");
uri_set(u, HOST, "/tmp/file.txt");
char *uri = uri_get(u);
ASSERT(uri != NULL, "minimal URI");
free(uri);
uri_free(u);
}
void test_set_null() {
uri_t *u = uri_init();
uri_set(u, SCHEME, "http");
uri_set(u, HOST, "example.com");
uri_set(u, HOST, NULL);
char *uri = uri_get(u);
ASSERT(uri != NULL, "uri with NULL host");
ASSERT(strcmp(uri, "http:") == 0, "uri is just scheme");
free(uri);
uri_free(u);
}
void test_remove_element() {
uri_t *u = uri_init();
uri_set(u, SCHEME, "https");
uri_set(u, HOST, "example.com");
uri_set(u, PATH, "/path");
char *removed = uri_remove(u, SCHEME);
ASSERT(removed != NULL, "remove returns old value");
ASSERT(strcmp(removed, "https") == 0, "removed value matches");
free(removed);
char *uri = uri_get(u);
ASSERT(uri != NULL, "uri after remove");
ASSERT(strcmp(uri, "//example.com/path") == 0, "uri without scheme");
free(uri);
uri_free(u);
}
void test_null_guards() {
uri_set(NULL, SCHEME, "http");
uri_print(NULL);
uri_print_elements(NULL);
ASSERT(uri_get(NULL) == NULL, "uri_get(NULL) returns NULL");
ASSERT(uri_remove(NULL, HOST) == NULL, "uri_remove(NULL) returns NULL");
ASSERT(uri_parse(NULL) == NULL, "uri_parse(NULL) returns NULL");
}
void test_bounds_guards() {
uri_t *u = uri_init();
uri_set(u, (uri_element_t)999, "http");
ASSERT(u->data[SCHEME] == NULL, "out-of-bounds set is no-op");
char *r = uri_remove(u, (uri_element_t)-1);
ASSERT(r == NULL, "negative index remove returns NULL");
uri_free(u);
}
int main() {
test_init();
test_set_and_get();
test_set_with_port();
test_set_with_query();
test_set_with_fragment();
test_full_uri();
test_parse_basic();
test_parse_with_port();
test_parse_with_query();
test_parse_with_fragment();
test_parse_full();
test_parse_invalid();
test_set_override();
test_roundtrip();
test_minimal_uri();
test_set_null();
test_remove_element();
test_null_guards();
test_bounds_guards();
printf("test_uri: %d failures\n", failures);
return failures;
}
|