2 * Copyright (C) 2016 J. Ali Harlow <ali@juiblex.co.uk>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "razor-internal.h"
27 static int test_parse(const char *uri)
30 struct razor_error *error = NULL;
33 r = razor_uri_parse(&ru, uri, &error);
34 razor_uri_destroy(&ru);
37 fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
38 uri, razor_error_get_msg(error));
45 static int test_normalize(const char *uri, const char *expected_normalized_uri)
48 struct razor_error *error = NULL;
52 r = razor_uri_parse(&ru, uri, &error);
55 razor_uri_destroy(&ru);
56 fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
57 uri, razor_error_get_msg(error));
61 razor_uri_normalize(&ru);
62 normalized_uri = razor_uri_recompose(&ru);
63 razor_uri_destroy(&ru);
65 if (strcmp(normalized_uri, expected_normalized_uri)) {
66 fprintf(stderr, "Fail: normalization of \"%s\" produces "
67 "\"%s\", expected \"%s\"\n", uri, normalized_uri,
68 expected_normalized_uri);
76 static int test_resolve(const char *base_uri, const char *relative_reference,
77 const char *expected_target_uri)
79 struct razor_uri base_ru, rr_ru, target_ru;
80 struct razor_error *error = NULL;
84 r = razor_uri_parse(&base_ru, base_uri, &error);
87 fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
88 base_uri, razor_error_get_msg(error));
92 r = razor_uri_parse(&rr_ru, relative_reference, &error);
95 fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
96 relative_reference, razor_error_get_msg(error));
97 razor_uri_destroy(&base_ru);
101 razor_uri_resolve(&target_ru, &base_ru, &rr_ru);
102 razor_uri_destroy(&base_ru);
103 razor_uri_destroy(&rr_ru);
105 target_uri = razor_uri_recompose(&target_ru);
106 razor_uri_destroy(&target_ru);
108 if (strcmp(target_uri, expected_target_uri)) {
110 "Fail: razor_uri_resolve(\"%s\", \"%s\") returns %s\n",
111 base_uri, relative_reference, target_uri);
121 int main(int argc, char *argv[])
125 r |= test_parse("file:");
126 r |= test_parse("file:/");
127 r |= test_parse("file:///");
129 /* From RFC 3986 § 6.2.2 */
130 r |= test_normalize("eXAMPLE://a/./b/../b/%63/%7bfoo%7d",
131 "example://a/b/c/%7Bfoo%7D");
133 /* From RFC 3986 § 5.4.1 */
134 r |= test_resolve("http://a/b/c/d;p?q", "g:h", "g:h");
135 r |= test_resolve("http://a/b/c/d;p?q", "g", "http://a/b/c/g");
136 r |= test_resolve("http://a/b/c/d;p?q", "./g", "http://a/b/c/g");
137 r |= test_resolve("http://a/b/c/d;p?q", "g/", "http://a/b/c/g/");
138 r |= test_resolve("http://a/b/c/d;p?q", "/g", "http://a/g");
139 r |= test_resolve("http://a/b/c/d;p?q", "//g", "http://g");
140 r |= test_resolve("http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y");
141 r |= test_resolve("http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y");
142 r |= test_resolve("http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s");
143 r |= test_resolve("http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s");
144 r |= test_resolve("http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s");
145 r |= test_resolve("http://a/b/c/d;p?q", ";x", "http://a/b/c/;x");
146 r |= test_resolve("http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x");
147 r |= test_resolve("http://a/b/c/d;p?q", "g;x?y#s",
148 "http://a/b/c/g;x?y#s");
149 r |= test_resolve("http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q");
150 r |= test_resolve("http://a/b/c/d;p?q", ".", "http://a/b/c/");
151 r |= test_resolve("http://a/b/c/d;p?q", "./", "http://a/b/c/");
152 r |= test_resolve("http://a/b/c/d;p?q", "..", "http://a/b/");
153 r |= test_resolve("http://a/b/c/d;p?q", "../", "http://a/b/");
154 r |= test_resolve("http://a/b/c/d;p?q", "../g", "http://a/b/g");
155 r |= test_resolve("http://a/b/c/d;p?q", "../..", "http://a/");
156 r |= test_resolve("http://a/b/c/d;p?q", "../../", "http://a/");
157 r |= test_resolve("http://a/b/c/d;p?q", "../../g", "http://a/g");
159 r |= test_resolve("http://a/b/c/d;p?q", "../../../g", "http://a/g");
160 r |= test_resolve("http://a/b/c/d;p?q", "../../../../g", "http://a/g");
162 r |= test_resolve("http://a/b/c/d;p?q", "/./g", "http://a/g");
163 r |= test_resolve("http://a/b/c/d;p?q", "/../g", "http://a/g");
164 r |= test_resolve("http://a/b/c/d;p?q", "g.", "http://a/b/c/g.");
165 r |= test_resolve("http://a/b/c/d;p?q", ".g", "http://a/b/c/.g");
166 r |= test_resolve("http://a/b/c/d;p?q", "g..", "http://a/b/c/g..");
167 r |= test_resolve("http://a/b/c/d;p?q", "..g", "http://a/b/c/..g");
169 r |= test_resolve("http://a/b/c/d;p?q", "./../g", "http://a/b/g");
170 r |= test_resolve("http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/");
171 r |= test_resolve("http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h");
172 r |= test_resolve("http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h");
173 r |= test_resolve("http://a/b/c/d;p?q", "g;x=1/./y",
174 "http://a/b/c/g;x=1/y");
175 r |= test_resolve("http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y");
177 r |= test_resolve("http://a/b/c/d;p?q", "g?y/./x",
178 "http://a/b/c/g?y/./x");
179 r |= test_resolve("http://a/b/c/d;p?q", "g?y/../x",
180 "http://a/b/c/g?y/../x");
181 r |= test_resolve("http://a/b/c/d;p?q", "g#s/./x",
182 "http://a/b/c/g#s/./x");
183 r |= test_resolve("http://a/b/c/d;p?q", "g#s/../x",
184 "http://a/b/c/g#s/../x");
186 r |= test_resolve("http://a/b/c/d;p?q", "http:g", "http:g");
188 /* From http://www.w3.org/2000/10/swap/uripath.py */
189 r |= test_resolve("foo:xyz", "bar:abc", "bar:abc");
191 r |= test_resolve("http://example/x/y/z", "../abc",
192 "http://example/x/abc");
193 r |= test_resolve("http://example2/x/y/z", "http://example/x/abc",
194 "http://example/x/abc");
195 r |= test_resolve("http://ex/x/y/z", "../r", "http://ex/x/r");
196 // "http://ex/x/y/z", "../../r", "http://ex/r"); // DanC had this.
197 r |= test_resolve("http://ex/x/y", "q/r", "http://ex/x/q/r");
198 r |= test_resolve("http://ex/x/y", "q/r#s", "http://ex/x/q/r#s");
199 r |= test_resolve("http://ex/x/y", "q/r#s/t", "http://ex/x/q/r#s/t");
200 r |= test_resolve("http://ex/x/y", "ftp://ex/x/q/r", "ftp://ex/x/q/r");
201 r |= test_resolve("http://ex/x/y", "", "http://ex/x/y");
202 r |= test_resolve("http://ex/x/y/", "", "http://ex/x/y/");
203 r |= test_resolve("http://ex/x/y/pdq", "", "http://ex/x/y/pdq");
204 r |= test_resolve("http://ex/x/y/", "z/", "http://ex/x/y/z/");
205 r |= test_resolve("file:/swap/test/animal.rdf", "#Animal",
206 "file:/swap/test/animal.rdf#Animal");
207 r |= test_resolve("file:/e/x/y/z", "../abc", "file:/e/x/abc");
208 r |= test_resolve("file:/example2/x/y/z", "/example/x/abc",
209 "file:/example/x/abc");
210 r |= test_resolve("file:/ex/x/y/z", "../r", "file:/ex/x/r");
211 r |= test_resolve("file:/ex/x/y/z", "/r", "file:/r");
212 r |= test_resolve("file:/ex/x/y", "q/r", "file:/ex/x/q/r");
213 r |= test_resolve("file:/ex/x/y", "q/r#s", "file:/ex/x/q/r#s");
214 r |= test_resolve("file:/ex/x/y", "q/r#", "file:/ex/x/q/r#");
215 r |= test_resolve("file:/ex/x/y", "q/r#s/t", "file:/ex/x/q/r#s/t");
216 r |= test_resolve("file:/ex/x/y", "ftp://ex/x/q/r", "ftp://ex/x/q/r");
217 r |= test_resolve("file:/ex/x/y", "", "file:/ex/x/y");
218 r |= test_resolve("file:/ex/x/y/", "", "file:/ex/x/y/");
219 r |= test_resolve("file:/ex/x/y/pdq", "", "file:/ex/x/y/pdq");
220 r |= test_resolve("file:/ex/x/y/", "z/", "file:/ex/x/y/z/");
221 r |= test_resolve("file:/devel/WWW/2000/10/swap/test/reluri-1.n3",
222 "file://meetings.example.com/cal#m1",
223 "file://meetings.example.com/cal#m1");
224 r |= test_resolve("file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reluri-1.n3",
225 "file://meetings.example.com/cal#m1",
226 "file://meetings.example.com/cal#m1");
227 r |= test_resolve("file:/some/dir/foo", "./#blort",
228 "file:/some/dir/#blort");
229 r |= test_resolve("file:/some/dir/foo", "./#", "file:/some/dir/#");
230 /* From Graham Klyne Thu, 20 Feb 2003 18:08:17 +0000 */
231 r |= test_resolve("http://example/x/y%2Fz", "abc",
232 "http://example/x/abc");
233 r |= test_resolve("http://example/x/y/z", "/x%2Fabc",
234 "http://example/x%2Fabc");
235 r |= test_resolve("http://example/x/y%2Fz", "/x%2Fabc",
236 "http://example/x%2Fabc");
237 r |= test_resolve("http://example/x%2Fy/z", "abc",
238 "http://example/x%2Fy/abc");
240 r |= test_resolve("http://example/x/abc.efg", "./",
241 "http://example/x/");