2 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #define TEST(uri, fail, exp, descr) \
24 if (!run_test(uri, fail, exp)) { \
25 fprintf(stderr, "%s:%d: error: %s\n", \
26 __FILE__, __LINE__, descr); \
30 #define URI(schema, host, port, path, query, frag) \
31 ((struct uri){schema, host, port, 0, path, query, frag})
33 #define DIFF(wanted, got, field) \
34 if (wanted->field == NULL || got->field == NULL || \
35 strcmp(wanted->field, got->field)) { \
36 fprintf(stderr, #field ":\n\tgot: %s\n\twanted: %s\n", \
37 got->field, wanted->field); \
45 diff_uri(struct uri *p, struct uri *exp)
52 DIFF(p, exp, fragment);
57 run_test(const char *uri, int should_fail, struct uri expected)
64 if ((uri_copy = strdup(uri)) == NULL)
67 fprintf(stderr, "=> %s\n", uri);
68 failed = !parse_uri(uri_copy, &parsed, &error);
70 if (failed && should_fail)
74 fprintf(stderr, "> %s\n", error);
76 ok = !failed && !should_fail;
78 ok = diff_uri(&expected, &parsed);
88 struct uri empty = {"", "", "", PASS, "", "", ""};
90 TEST("foo://bar.com/foo%00?baz",
96 TEST("http://omarpolo.com",
98 URI("http", "omarpolo.com", "", "", "", ""),
99 "can parse uri with empty path");
102 TEST("omarpolo.com", FAIL, empty, "FAIL when the schema is missing");
103 TEST("gemini:/omarpolo.com", FAIL, empty, "FAIL with invalid marker");
104 TEST("gemini//omarpolo.com", FAIL, empty, "FAIL with invalid marker");
105 TEST("h!!p://omarpolo.com", FAIL, empty, "FAIL with invalid schema");
108 TEST("gemini://omarpolo.com",
110 URI("gemini", "omarpolo.com", "", "", "", ""),
111 "can parse authority with empty path");
112 TEST("gemini://omarpolo.com/",
114 URI("gemini", "omarpolo.com", "", "", "", ""),
115 "can parse authority with empty path (alt)")
116 TEST("gemini://omarpolo.com:1965",
118 URI("gemini", "omarpolo.com", "1965", "", "", ""),
119 "can parse with port and empty path");
120 TEST("gemini://omarpolo.com:1965/",
122 URI("gemini", "omarpolo.com", "1965", "", "", ""),
123 "can parse with port and empty path")
124 TEST("gemini://omarpolo.com:196s",
127 "FAIL with invalid port number");
130 TEST("gemini://omarpolo.com/foo/bar/baz",
132 URI("gemini", "omarpolo.com", "", "foo/bar/baz", "", ""),
133 "parse simple paths");
134 TEST("gemini://omarpolo.com/foo//bar///baz",
136 URI("gemini", "omarpolo.com", "", "foo/bar/baz", "", ""),
137 "parse paths with multiple slashes");
138 TEST("gemini://omarpolo.com/foo/./bar/./././baz",
140 URI("gemini", "omarpolo.com", "", "foo/bar/baz", "", ""),
141 "parse paths with . elements");
142 TEST("gemini://omarpolo.com/foo/bar/../bar/baz",
144 URI("gemini", "omarpolo.com", "", "foo/bar/baz", "", ""),
145 "parse paths with .. elements");
146 TEST("gemini://omarpolo.com/foo/../foo/bar/../bar/baz/../baz",
148 URI("gemini", "omarpolo.com", "", "foo/bar/baz", "", ""),
149 "parse paths with multiple .. elements");
150 TEST("gemini://omarpolo.com/foo/..",
152 URI("gemini", "omarpolo.com", "", "", "", ""),
153 "parse paths with a trailing ..");
154 TEST("gemini://omarpolo.com/foo/../",
156 URI("gemini", "omarpolo.com", "", "", "", ""),
157 "parse paths with a trailing ..");
158 TEST("gemini://omarpolo.com/foo/../..",
161 "reject paths that would escape the root");
162 TEST("gemini://omarpolo.com/foo/../foo/../././/bar/baz/.././.././/",
164 URI("gemini", "omarpolo.com", "", "", "", ""),
165 "parse path with lots of cleaning available");
168 TEST("foo://example.com/foo/?gne",
170 URI("foo", "example.com", "", "foo/", "gne", ""),
171 "parse query strings");
172 TEST("foo://example.com/foo/?gne&foo",
174 URI("foo", "example.com", "", "foo/", "gne&foo", ""),
175 "parse query strings");
176 TEST("foo://example.com/foo/?gne%2F",
178 URI("foo", "example.com", "", "foo/", "gne/", ""),
179 "parse query strings");
182 TEST("foo://bar.co/#foo",
184 URI("foo", "bar.co", "", "", "", "foo"),
185 "can recognize fragments");
187 /* percent encoding */
188 TEST("foo://bar.com/caf%C3%A8.gmi",
190 URI("foo", "bar.com", "", "cafè.gmi", "", ""),
192 TEST("foo://bar.com/caff%C3%A8%20macchiato.gmi",
194 URI("foo", "bar.com", "", "caffè macchiato.gmi", "", ""),
196 TEST("foo://bar.com/caff%C3%A8+macchiato.gmi",
198 URI("foo", "bar.com", "", "caffè+macchiato.gmi", "", ""),
200 TEST("foo://bar.com/foo%2F..%2F..",
203 "conversion and checking are done in the correct order");
204 TEST("foo://bar.com/foo%00?baz",
210 TEST("foo://bar.com/cafè.gmi",
212 URI("foo", "bar.com", "", "cafè.gmi", "" , ""),
213 "decode IRI (with a 2-byte utf8 seq)");
214 TEST("foo://bar.com/世界.gmi",
216 URI("foo", "bar.com", "", "世界.gmi", "" , ""),
218 TEST("foo://bar.com/😼.gmi",
220 URI("foo", "bar.com", "", "😼.gmi", "" , ""),
221 "decode IRI (with a 3-byte utf8 seq)");
222 TEST("foo://bar.com/😼/𤭢.gmi",
224 URI("foo", "bar.com", "", "😼/𤭢.gmi", "" , ""),
225 "decode IRI (with a 3-byte and a 4-byte utf8 seq)");
226 TEST("foo://bar.com/世界/\xC0\x80",
229 "reject invalid sequence (overlong NUL)");