Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include <err.h>
18 #include <errno.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
23 #include "iri.h"
25 static int
26 resolve(const char *base, const char *ref, const char *expected)
27 {
28 static struct iri i;
29 char buf[512];
31 if (iri_parse(base, ref, &i) == -1) {
32 fprintf(stderr, "FAIL resolve(\"%s\", \"%s\") %s\n", base, ref,
33 strerror(errno));
34 return (1);
35 }
37 if (iri_unparse(&i, buf, sizeof(buf)) == -1) {
38 fprintf(stderr, "FAIL resolve(\"%s\", \"%s\") %s\n", base, ref,
39 strerror(errno));
40 return (1);
41 }
43 if (strcmp(buf, expected) != 0) {
44 fprintf(stderr, "FAIL resolve(\"%s\", \"%s\")\n", base, ref);
45 fprintf(stderr, "got:\t%s\n", buf);
46 fprintf(stderr, "want:\t%s\n", expected);
47 return (1);
48 }
50 fprintf(stderr, "OK resolve(\"%s\", \"%s\") -> %s\n", base, ref,
51 expected);
52 return (0);
53 }
55 static int
56 setquery(const char *iri, const char *query, const char *expected)
57 {
58 static struct iri i;
59 char buf[512];
61 if (iri_parse(NULL, iri, &i) == -1) {
62 fprintf(stderr, "FAIL can't parse <%s>: %s\n", iri,
63 strerror(errno));
64 return (1);
65 }
67 if (iri_setquery(&i, query) == -1) {
68 fprintf(stderr, "FAIL setting query \"%s\": %s\n", query,
69 strerror(errno));
70 return (1);
71 }
73 if (iri_unparse(&i, buf, sizeof(buf)) == -1) {
74 fprintf(stderr, "FAIL unparsing <%s> with query %s\n",
75 iri, query);
76 return (1);
77 }
79 if (strcmp(buf, expected) != 0) {
80 fprintf(stderr, "FAIL setquery(\"%s\", \"%s\")\n", iri, query);
81 fprintf(stderr, "got:\t%s\n", buf);
82 fprintf(stderr, "want:\t%s\n", expected);
83 return (1);
84 }
86 fprintf(stderr, "OK setquery(\"%s\", \"%s\") -> %s\n", iri, query,
87 expected);
88 return (0);
89 }
91 static int
92 urlencode(const char *str, const char *exp)
93 {
94 char enc[512];
95 char dec[512];
97 if (iri_urlescape(str, enc, sizeof(enc)) == -1) {
98 fprintf(stderr, "FAIL can't percent encode %s\n", str);
99 return (1);
102 if (strcmp(enc, exp) != 0) {
103 fprintf(stderr, "FAIL %%enc: expecting <%s>; got <%s>\n",
104 exp, enc);
105 return (1);
108 if (iri_urlunescape(enc, dec, sizeof(dec)) == -1) {
109 fprintf(stderr, "FAIL can't %%decode %s\n", enc);
110 return (1);
113 if (strcmp(str, dec) != 0) {
114 fprintf(stderr,
115 "FAIL urlencode/decode not identity: <%s> vs <%s>\n",
116 str, dec);
117 return (1);
120 if (strcmp(enc, exp) != 0) {
121 fprintf(stderr, "FAIL %%enc: expecting <%s>; got <%s>\n",
122 exp, enc);
123 return (1);
126 fprintf(stderr, "OK urlencode(\"%s\") -> %s\n", str, enc);
127 return (0);
130 int
131 main(void)
133 const char *base;
134 int ret = 0;
136 /* RFC 3986 tests */
138 base = "http://a/b/c/d;p?q";
140 ret |= resolve(base, "g:h", "g:h");
141 ret |= resolve(base, "g", "http://a/b/c/g");
142 ret |= resolve(base, "./g", "http://a/b/c/g");
143 ret |= resolve(base, "g/", "http://a/b/c/g/");
144 ret |= resolve(base, "/g", "http://a/g");
146 /*
147 * the RFC says "http://g" but we always normalize an
148 * empty path to "/" if there is an authority.
149 */
150 ret |= resolve(base, "//g", "http://g/");
152 ret |= resolve(base, "?y", "http://a/b/c/d;p?y");
153 ret |= resolve(base, "g?y", "http://a/b/c/g?y");
154 ret |= resolve(base, "#s", "http://a/b/c/d;p?q#s");
155 ret |= resolve(base, "g#s", "http://a/b/c/g#s");
156 ret |= resolve(base, "g?y#s", "http://a/b/c/g?y#s");
157 ret |= resolve(base, ";x", "http://a/b/c/;x");
158 ret |= resolve(base, "g;x", "http://a/b/c/g;x");
159 ret |= resolve(base, "g;x?y#s", "http://a/b/c/g;x?y#s");
160 ret |= resolve(base, "", "http://a/b/c/d;p?q");
161 ret |= resolve(base, ".", "http://a/b/c/");
162 ret |= resolve(base, "./", "http://a/b/c/");
163 ret |= resolve(base, "..", "http://a/b/");
164 ret |= resolve(base, "../", "http://a/b/");
165 ret |= resolve(base, "../g", "http://a/b/g");
166 ret |= resolve(base, "../..", "http://a/");
167 ret |= resolve(base, "../../", "http://a/");
168 ret |= resolve(base, "../../g", "http://a/g");
170 ret |= resolve(base, "../../../g", "http://a/g");
171 ret |= resolve(base, "../../../../g", "http://a/g");
172 ret |= resolve(base, "/./g", "http://a/g");
173 ret |= resolve(base, "/../g", "http://a/g");
174 ret |= resolve(base, "g.", "http://a/b/c/g.");
175 ret |= resolve(base, ".g", "http://a/b/c/.g");
176 ret |= resolve(base, "g..", "http://a/b/c/g..");
177 ret |= resolve(base, "..g", "http://a/b/c/..g");
179 ret |= resolve(base, "./../g", "http://a/b/g");
180 ret |= resolve(base, "./g/.", "http://a/b/c/g/");
181 ret |= resolve(base, "g/./h", "http://a/b/c/g/h");
182 ret |= resolve(base, "g/../h", "http://a/b/c/h");
183 ret |= resolve(base, "g;x=1/./y", "http://a/b/c/g;x=1/y");
184 ret |= resolve(base, "g;x=1/../y", "http://a/b/c/y");
186 ret |= resolve(base, "g?y/./x", "http://a/b/c/g?y/./x");
187 ret |= resolve(base, "g?y/../x", "http://a/b/c/g?y/../x");
188 ret |= resolve(base, "g#s/./x", "http://a/b/c/g#s/./x");
189 ret |= resolve(base, "g#s/../x", "http://a/b/c/g#s/../x");
191 ret |= resolve(base, "http:g", "http:g");
193 /* extra tests */
195 ret |= resolve(base, "gopher://b:70", "gopher://b:70/");
196 ret |= resolve(base, "gopher://b:70/1/foo", "gopher://b:70/1/foo");
198 base = "gopher://b:70/1/bar/foo/quux";
199 ret |= resolve(base, "/", "gopher://b:70/");
200 ret |= resolve(base, "..", "gopher://b:70/1/bar/");
202 base = "gemini://a/b/c";
203 ret |= setquery(base, "hw", "gemini://a/b/c?hw");
204 ret |= setquery(base, "h w", "gemini://a/b/c?h%20w");
205 ret |= setquery(base, "100%", "gemini://a/b/c?100%25");
206 ret |= setquery(base, "%20", "gemini://a/b/c?%2520");
208 base = "gemini://a/?foo+bar";
209 ret |= setquery(base, "foo", "gemini://a/?foo");
210 ret |= setquery(base, "%20", "gemini://a/?%2520");
211 ret |= setquery(base, "%20%20%20", "gemini://a/?%2520%2520%2520");
213 ret |= resolve(base, "file:///tmp/foo", "file:///tmp/foo");
214 ret |= resolve(base, "file:/tmp/foo", "file:///tmp/foo");
216 ret |= urlencode("foobar", "foobar");
217 ret |= urlencode("foo/bar", "foo/bar");
218 ret |= urlencode("foo bar", "foo%20bar");
219 ret |= urlencode("/Teloschistes flavicans",
220 "/Teloschistes%20flavicans");
222 return (ret);