Blame


1 5c2e310e 2021-01-22 op /*
2 5c2e310e 2021-01-22 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
3 5c2e310e 2021-01-22 op *
4 5c2e310e 2021-01-22 op * Permission to use, copy, modify, and distribute this software for any
5 5c2e310e 2021-01-22 op * purpose with or without fee is hereby granted, provided that the above
6 5c2e310e 2021-01-22 op * copyright notice and this permission notice appear in all copies.
7 5c2e310e 2021-01-22 op *
8 5c2e310e 2021-01-22 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5c2e310e 2021-01-22 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5c2e310e 2021-01-22 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5c2e310e 2021-01-22 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5c2e310e 2021-01-22 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5c2e310e 2021-01-22 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5c2e310e 2021-01-22 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 5c2e310e 2021-01-22 op */
16 5c2e310e 2021-01-22 op
17 5c2e310e 2021-01-22 op #include <err.h>
18 5c2e310e 2021-01-22 op #include <stdio.h>
19 5c2e310e 2021-01-22 op #include <string.h>
20 5c2e310e 2021-01-22 op
21 5c2e310e 2021-01-22 op #include "../gmid.h"
22 5c2e310e 2021-01-22 op
23 5c2e310e 2021-01-22 op #define TEST(iri, fail, exp, descr) \
24 5c2e310e 2021-01-22 op if (!run_test(iri, fail, exp)) { \
25 5c2e310e 2021-01-22 op fprintf(stderr, "%s:%d: error: %s\n", \
26 5c2e310e 2021-01-22 op __FILE__, __LINE__, descr); \
27 5c2e310e 2021-01-22 op exit(1); \
28 5c2e310e 2021-01-22 op }
29 5c2e310e 2021-01-22 op
30 5c2e310e 2021-01-22 op #define IRI(schema, host, port, path, query, frag) \
31 5c2e310e 2021-01-22 op ((struct iri){(char*)schema, (char*)host, (char*)port, \
32 5c2e310e 2021-01-22 op 0, (char*)path, (char*)query, \
33 5c2e310e 2021-01-22 op (char*)frag})
34 5c2e310e 2021-01-22 op
35 5c2e310e 2021-01-22 op #define DIFF(wanted, got, field) \
36 5c2e310e 2021-01-22 op if (wanted->field == NULL || got->field == NULL || \
37 5c2e310e 2021-01-22 op strcmp(wanted->field, got->field)) { \
38 5c2e310e 2021-01-22 op fprintf(stderr, #field ":\n\tgot: %s\n\twanted: %s\n", \
39 5c2e310e 2021-01-22 op got->field, wanted->field); \
40 5c2e310e 2021-01-22 op return 0; \
41 5c2e310e 2021-01-22 op }
42 5c2e310e 2021-01-22 op
43 5c2e310e 2021-01-22 op #define PASS 0
44 5c2e310e 2021-01-22 op #define FAIL 1
45 5c2e310e 2021-01-22 op
46 5c2e310e 2021-01-22 op int diff_iri(struct iri*, struct iri*);
47 5c2e310e 2021-01-22 op int run_test(const char*, int, struct iri);
48 5c2e310e 2021-01-22 op
49 5c2e310e 2021-01-22 op int
50 5c2e310e 2021-01-22 op diff_iri(struct iri *p, struct iri *exp)
51 5c2e310e 2021-01-22 op {
52 5c2e310e 2021-01-22 op DIFF(p, exp, schema);
53 5c2e310e 2021-01-22 op DIFF(p, exp, host);
54 5c2e310e 2021-01-22 op DIFF(p, exp, port);
55 5c2e310e 2021-01-22 op DIFF(p, exp, path);
56 5c2e310e 2021-01-22 op DIFF(p, exp, query);
57 5c2e310e 2021-01-22 op DIFF(p, exp, fragment);
58 5c2e310e 2021-01-22 op return 1;
59 5c2e310e 2021-01-22 op }
60 5c2e310e 2021-01-22 op
61 5c2e310e 2021-01-22 op int
62 5c2e310e 2021-01-22 op run_test(const char *iri, int should_fail, struct iri expected)
63 5c2e310e 2021-01-22 op {
64 5c2e310e 2021-01-22 op int failed, ok = 1;
65 5c2e310e 2021-01-22 op char *iri_copy;
66 5c2e310e 2021-01-22 op struct iri parsed;
67 5c2e310e 2021-01-22 op const char *error;
68 5c2e310e 2021-01-22 op
69 5c2e310e 2021-01-22 op if ((iri_copy = strdup(iri)) == NULL)
70 5c2e310e 2021-01-22 op err(1, "strdup");
71 5c2e310e 2021-01-22 op
72 5c2e310e 2021-01-22 op fprintf(stderr, "=> %s\n", iri);
73 5c2e310e 2021-01-22 op failed = !parse_iri(iri_copy, &parsed, &error);
74 5c2e310e 2021-01-22 op
75 5c2e310e 2021-01-22 op if (failed && should_fail)
76 5c2e310e 2021-01-22 op goto done;
77 5c2e310e 2021-01-22 op
78 5c2e310e 2021-01-22 op if (error != NULL)
79 5c2e310e 2021-01-22 op fprintf(stderr, "> %s\n", error);
80 5c2e310e 2021-01-22 op
81 5c2e310e 2021-01-22 op ok = !failed && !should_fail;
82 5c2e310e 2021-01-22 op if (ok)
83 5c2e310e 2021-01-22 op ok = diff_iri(&expected, &parsed);
84 5c2e310e 2021-01-22 op
85 5c2e310e 2021-01-22 op done:
86 5c2e310e 2021-01-22 op free(iri_copy);
87 5c2e310e 2021-01-22 op return ok;
88 5c2e310e 2021-01-22 op }
89 5c2e310e 2021-01-22 op
90 5c2e310e 2021-01-22 op int
91 5c2e310e 2021-01-22 op main(void)
92 5c2e310e 2021-01-22 op {
93 5c2e310e 2021-01-22 op struct iri empty = IRI("", "", "", "", "", "");
94 5c2e310e 2021-01-22 op
95 5c2e310e 2021-01-22 op TEST("http://omarpolo.com",
96 5c2e310e 2021-01-22 op PASS,
97 5c2e310e 2021-01-22 op IRI("http", "omarpolo.com", "", "", "", ""),
98 5c2e310e 2021-01-22 op "can parse iri with empty path");
99 5c2e310e 2021-01-22 op
100 5c2e310e 2021-01-22 op /* schema */
101 5c2e310e 2021-01-22 op TEST("omarpolo.com", FAIL, empty, "FAIL when the schema is missing");
102 5c2e310e 2021-01-22 op TEST("gemini:/omarpolo.com", FAIL, empty, "FAIL with invalid marker");
103 5c2e310e 2021-01-22 op TEST("gemini//omarpolo.com", FAIL, empty, "FAIL with invalid marker");
104 5c2e310e 2021-01-22 op TEST("h!!p://omarpolo.com", FAIL, empty, "FAIL with invalid schema");
105 5c2e310e 2021-01-22 op TEST("GEMINI://omarpolo.com",
106 5c2e310e 2021-01-22 op PASS,
107 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "", "", ""),
108 5c2e310e 2021-01-22 op "Schemas are case insensitive.");
109 5c2e310e 2021-01-22 op
110 5c2e310e 2021-01-22 op /* authority */
111 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com",
112 5c2e310e 2021-01-22 op PASS,
113 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "", "", ""),
114 5c2e310e 2021-01-22 op "can parse authority with empty path");
115 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/",
116 5c2e310e 2021-01-22 op PASS,
117 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "", "", ""),
118 5c2e310e 2021-01-22 op "can parse authority with empty path (alt)")
119 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com:1965",
120 5c2e310e 2021-01-22 op PASS,
121 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "1965", "", "", ""),
122 5c2e310e 2021-01-22 op "can parse with port and empty path");
123 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com:1965/",
124 5c2e310e 2021-01-22 op PASS,
125 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "1965", "", "", ""),
126 5c2e310e 2021-01-22 op "can parse with port and empty path")
127 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com:196s",
128 5c2e310e 2021-01-22 op FAIL,
129 5c2e310e 2021-01-22 op empty,
130 5c2e310e 2021-01-22 op "FAIL with invalid port number");
131 5c2e310e 2021-01-22 op TEST("gemini://OmArPoLo.CoM",
132 5c2e310e 2021-01-22 op PASS,
133 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "", "", ""),
134 5c2e310e 2021-01-22 op "host is case-insensitive");
135 e7c7f19c 2021-01-29 op TEST("gemini://xn--nave-6pa.omarpolo.com",
136 e7c7f19c 2021-01-29 op PASS,
137 e7c7f19c 2021-01-29 op IRI("gemini", "xn--nave-6pa.omarpolo.com", "", "", "", ""),
138 e7c7f19c 2021-01-29 op "Can parse punycode-encoded hostnames");
139 e7c7f19c 2021-01-29 op TEST("gemini://naïve.omarpolo.com",
140 e7c7f19c 2021-01-29 op PASS,
141 e7c7f19c 2021-01-29 op IRI("gemini", "naïve.omarpolo.com", "", "", "", ""),
142 e7c7f19c 2021-01-29 op "Accept non punycode-encoded hostnames");
143 e7c7f19c 2021-01-29 op TEST("gemini://na%c3%afve.omarpolo.com",
144 e7c7f19c 2021-01-29 op PASS,
145 e7c7f19c 2021-01-29 op IRI("gemini", "naïve.omarpolo.com", "", "", "", ""),
146 e7c7f19c 2021-01-29 op "Can percent decode hostnames");
147 5c2e310e 2021-01-22 op
148 5c2e310e 2021-01-22 op /* path */
149 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/foo/bar/baz",
150 5c2e310e 2021-01-22 op PASS,
151 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "foo/bar/baz", "", ""),
152 5c2e310e 2021-01-22 op "parse simple paths");
153 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/foo//bar///baz",
154 5c2e310e 2021-01-22 op PASS,
155 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "foo/bar/baz", "", ""),
156 5c2e310e 2021-01-22 op "parse paths with multiple slashes");
157 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/foo/./bar/./././baz",
158 5c2e310e 2021-01-22 op PASS,
159 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "foo/bar/baz", "", ""),
160 5c2e310e 2021-01-22 op "parse paths with . elements");
161 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/foo/bar/../bar/baz",
162 5c2e310e 2021-01-22 op PASS,
163 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "foo/bar/baz", "", ""),
164 5c2e310e 2021-01-22 op "parse paths with .. elements");
165 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/foo/../foo/bar/../bar/baz/../baz",
166 5c2e310e 2021-01-22 op PASS,
167 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "foo/bar/baz", "", ""),
168 5c2e310e 2021-01-22 op "parse paths with multiple .. elements");
169 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/foo/..",
170 5c2e310e 2021-01-22 op PASS,
171 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "", "", ""),
172 5c2e310e 2021-01-22 op "parse paths with a trailing ..");
173 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/foo/../",
174 5c2e310e 2021-01-22 op PASS,
175 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "", "", ""),
176 5c2e310e 2021-01-22 op "parse paths with a trailing ..");
177 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/foo/../..",
178 5c2e310e 2021-01-22 op FAIL,
179 5c2e310e 2021-01-22 op empty,
180 5c2e310e 2021-01-22 op "reject paths that would escape the root");
181 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/foo/../../",
182 5c2e310e 2021-01-22 op FAIL,
183 5c2e310e 2021-01-22 op empty,
184 5c2e310e 2021-01-22 op "reject paths that would escape the root")
185 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/foo/../foo/../././/bar/baz/.././.././/",
186 5c2e310e 2021-01-22 op PASS,
187 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "", "", ""),
188 5c2e310e 2021-01-22 op "parse path with lots of cleaning available");
189 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com//foo",
190 5c2e310e 2021-01-22 op PASS,
191 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "foo", "", ""),
192 5c2e310e 2021-01-22 op "Trim initial slashes");
193 5c2e310e 2021-01-22 op TEST("gemini://omarpolo.com/////foo",
194 5c2e310e 2021-01-22 op PASS,
195 5c2e310e 2021-01-22 op IRI("gemini", "omarpolo.com", "", "foo", "", ""),
196 5c2e310e 2021-01-22 op "Trim initial slashes (pt. 2)");
197 9d092b60 2021-04-12 op TEST("http://a/b/c/../..",
198 9d092b60 2021-04-12 op PASS,
199 9d092b60 2021-04-12 op IRI("http", "a", "", "", "", ""),
200 9d092b60 2021-04-12 op "avoid infinite loops (see v1.6.1)");
201 5c2e310e 2021-01-22 op
202 5c2e310e 2021-01-22 op /* query */
203 5c2e310e 2021-01-22 op TEST("foo://example.com/foo/?gne",
204 5c2e310e 2021-01-22 op PASS,
205 5c2e310e 2021-01-22 op IRI("foo", "example.com", "", "foo/", "gne", ""),
206 5c2e310e 2021-01-22 op "parse query strings");
207 5c2e310e 2021-01-22 op TEST("foo://example.com/foo/?gne&foo",
208 5c2e310e 2021-01-22 op PASS,
209 5c2e310e 2021-01-22 op IRI("foo", "example.com", "", "foo/", "gne&foo", ""),
210 5c2e310e 2021-01-22 op "parse query strings");
211 8404ec30 2021-02-05 op /* TEST("foo://example.com/foo/?gne%2F", */
212 8404ec30 2021-02-05 op /* PASS, */
213 8404ec30 2021-02-05 op /* IRI("foo", "example.com", "", "foo/", "gne/", ""), */
214 8404ec30 2021-02-05 op /* "parse query strings"); */
215 4125c94f 2021-02-06 op TEST("foo://ex.com/robots.txt?name=foobar&url=https://foo.com",
216 4125c94f 2021-02-06 op PASS,
217 4125c94f 2021-02-06 op IRI("foo", "ex.com", "", "robots.txt", "name=foobar&url=https://foo.com", ""),
218 4125c94f 2021-02-06 op "Accepts : in queries");
219 4125c94f 2021-02-06 op TEST("foo://ex.com/foo?email=foo@bar.com#quuz",
220 4125c94f 2021-02-06 op PASS,
221 4125c94f 2021-02-06 op IRI("foo", "ex.com", "", "foo", "email=foo@bar.com", "quuz"),
222 4125c94f 2021-02-06 op "Accepts @ in queries");
223 5c2e310e 2021-01-22 op
224 5c2e310e 2021-01-22 op /* fragment */
225 5c2e310e 2021-01-22 op TEST("foo://bar.co/#foo",
226 5c2e310e 2021-01-22 op PASS,
227 5c2e310e 2021-01-22 op IRI("foo", "bar.co", "", "", "", "foo"),
228 5c2e310e 2021-01-22 op "can recognize fragments");
229 5c2e310e 2021-01-22 op
230 5c2e310e 2021-01-22 op /* percent encoding */
231 5c2e310e 2021-01-22 op TEST("foo://bar.com/caf%C3%A8.gmi",
232 5c2e310e 2021-01-22 op PASS,
233 5c2e310e 2021-01-22 op IRI("foo", "bar.com", "", "cafè.gmi", "", ""),
234 5c2e310e 2021-01-22 op "can decode");
235 5c2e310e 2021-01-22 op TEST("foo://bar.com/caff%C3%A8%20macchiato.gmi",
236 5c2e310e 2021-01-22 op PASS,
237 5c2e310e 2021-01-22 op IRI("foo", "bar.com", "", "caffè macchiato.gmi", "", ""),
238 5c2e310e 2021-01-22 op "can decode");
239 5c2e310e 2021-01-22 op TEST("foo://bar.com/caff%C3%A8+macchiato.gmi",
240 5c2e310e 2021-01-22 op PASS,
241 5c2e310e 2021-01-22 op IRI("foo", "bar.com", "", "caffè+macchiato.gmi", "", ""),
242 5c2e310e 2021-01-22 op "can decode");
243 5c2e310e 2021-01-22 op TEST("foo://bar.com/foo%2F..%2F..",
244 5c2e310e 2021-01-22 op FAIL,
245 5c2e310e 2021-01-22 op empty,
246 5c2e310e 2021-01-22 op "conversion and checking are done in the correct order");
247 5c2e310e 2021-01-22 op TEST("foo://bar.com/foo%00?baz",
248 5c2e310e 2021-01-22 op FAIL,
249 5c2e310e 2021-01-22 op empty,
250 5c2e310e 2021-01-22 op "rejects %00");
251 5c2e310e 2021-01-22 op
252 5c2e310e 2021-01-22 op /* IRI */
253 5c2e310e 2021-01-22 op TEST("foo://bar.com/cafè.gmi",
254 5c2e310e 2021-01-22 op PASS,
255 5c2e310e 2021-01-22 op IRI("foo", "bar.com", "", "cafè.gmi", "" , ""),
256 5c2e310e 2021-01-22 op "decode IRI (with a 2-byte utf8 seq)");
257 5c2e310e 2021-01-22 op TEST("foo://bar.com/世界.gmi",
258 5c2e310e 2021-01-22 op PASS,
259 5c2e310e 2021-01-22 op IRI("foo", "bar.com", "", "世界.gmi", "" , ""),
260 5c2e310e 2021-01-22 op "decode IRI");
261 5c2e310e 2021-01-22 op TEST("foo://bar.com/😼.gmi",
262 5c2e310e 2021-01-22 op PASS,
263 5c2e310e 2021-01-22 op IRI("foo", "bar.com", "", "😼.gmi", "" , ""),
264 5c2e310e 2021-01-22 op "decode IRI (with a 3-byte utf8 seq)");
265 5c2e310e 2021-01-22 op TEST("foo://bar.com/😼/𤭢.gmi",
266 5c2e310e 2021-01-22 op PASS,
267 5c2e310e 2021-01-22 op IRI("foo", "bar.com", "", "😼/𤭢.gmi", "" , ""),
268 5c2e310e 2021-01-22 op "decode IRI (with a 3-byte and a 4-byte utf8 seq)");
269 5c2e310e 2021-01-22 op TEST("foo://bar.com/世界/\xC0\x80",
270 5c2e310e 2021-01-22 op FAIL,
271 5c2e310e 2021-01-22 op empty,
272 5c2e310e 2021-01-22 op "reject invalid sequence (overlong NUL)");
273 5c2e310e 2021-01-22 op
274 5c2e310e 2021-01-22 op return 0;
275 5c2e310e 2021-01-22 op }