1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/librazor/test-uri.c Fri Jul 08 17:12:36 2016 +0100
1.3 @@ -0,0 +1,244 @@
1.4 +/*
1.5 + * Copyright (C) 2016 J. Ali Harlow <ali@juiblex.co.uk>
1.6 + *
1.7 + * This program is free software; you can redistribute it and/or modify
1.8 + * it under the terms of the GNU General Public License as published by
1.9 + * the Free Software Foundation; either version 2 of the License, or
1.10 + * (at your option) any later version.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License along
1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1.20 + */
1.21 +
1.22 +#include "config.h"
1.23 +
1.24 +#include <stdlib.h>
1.25 +#include <stdio.h>
1.26 +#include <string.h>
1.27 +#include "razor.h"
1.28 +#include "razor-internal.h"
1.29 +
1.30 +static int test_parse(const char *uri)
1.31 +{
1.32 + struct razor_uri ru;
1.33 + struct razor_error *error = NULL;
1.34 + int r;
1.35 +
1.36 + r = razor_uri_parse(&ru, uri, &error);
1.37 + razor_uri_destroy(&ru);
1.38 +
1.39 + if (r < 0) {
1.40 + fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
1.41 + uri, razor_error_get_msg(error));
1.42 + return -1;
1.43 + }
1.44 +
1.45 + return r;
1.46 +}
1.47 +
1.48 +static int test_normalize(const char *uri, const char *expected_normalized_uri)
1.49 +{
1.50 + struct razor_uri ru;
1.51 + struct razor_error *error = NULL;
1.52 + int r;
1.53 + char *normalized_uri;
1.54 +
1.55 + r = razor_uri_parse(&ru, uri, &error);
1.56 +
1.57 + if (r < 0) {
1.58 + razor_uri_destroy(&ru);
1.59 + fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
1.60 + uri, razor_error_get_msg(error));
1.61 + return -1;
1.62 + }
1.63 +
1.64 + razor_uri_normalize(&ru);
1.65 + normalized_uri = razor_uri_recompose(&ru);
1.66 + razor_uri_destroy(&ru);
1.67 +
1.68 + if (strcmp(normalized_uri, expected_normalized_uri)) {
1.69 + fprintf(stderr, "Fail: normalization of \"%s\" produces "
1.70 + "\"%s\", expected \"%s\"\n", uri, normalized_uri,
1.71 + expected_normalized_uri);
1.72 + r = -1;
1.73 + }
1.74 +
1.75 + free(normalized_uri);
1.76 + return r;
1.77 +}
1.78 +
1.79 +static int test_resolve(const char *base_uri, const char *relative_reference,
1.80 + const char *expected_target_uri)
1.81 +{
1.82 + struct razor_uri base_ru, rr_ru, target_ru;
1.83 + struct razor_error *error = NULL;
1.84 + char *target_uri;
1.85 + int r;
1.86 +
1.87 + r = razor_uri_parse(&base_ru, base_uri, &error);
1.88 +
1.89 + if (r < 0) {
1.90 + fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
1.91 + base_uri, razor_error_get_msg(error));
1.92 + return -1;
1.93 + }
1.94 +
1.95 + r = razor_uri_parse(&rr_ru, relative_reference, &error);
1.96 +
1.97 + if (r < 0) {
1.98 + fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
1.99 + relative_reference, razor_error_get_msg(error));
1.100 + razor_uri_destroy(&base_ru);
1.101 + return -1;
1.102 + }
1.103 +
1.104 + razor_uri_resolve(&target_ru, &base_ru, &rr_ru);
1.105 + razor_uri_destroy(&base_ru);
1.106 + razor_uri_destroy(&rr_ru);
1.107 +
1.108 + target_uri = razor_uri_recompose(&target_ru);
1.109 + razor_uri_destroy(&target_ru);
1.110 +
1.111 + if (strcmp(target_uri, expected_target_uri)) {
1.112 + fprintf(stderr,
1.113 + "Fail: razor_uri_resolve(\"%s\", \"%s\") returns %s\n",
1.114 + base_uri, relative_reference, target_uri);
1.115 + free(target_uri);
1.116 + return -1;
1.117 + }
1.118 +
1.119 + free(target_uri);
1.120 +
1.121 + return 0;
1.122 +}
1.123 +
1.124 +int main(int argc, char *argv[])
1.125 +{
1.126 + int r = 0;
1.127 +
1.128 + r |= test_parse("file:");
1.129 + r |= test_parse("file:/");
1.130 + r |= test_parse("file:///");
1.131 +
1.132 + /* From RFC 3986 § 6.2.2 */
1.133 + r |= test_normalize("eXAMPLE://a/./b/../b/%63/%7bfoo%7d",
1.134 + "example://a/b/c/%7Bfoo%7D");
1.135 +
1.136 + /* From RFC 3986 § 5.4.1 */
1.137 + r |= test_resolve("http://a/b/c/d;p?q", "g:h", "g:h");
1.138 + r |= test_resolve("http://a/b/c/d;p?q", "g", "http://a/b/c/g");
1.139 + r |= test_resolve("http://a/b/c/d;p?q", "./g", "http://a/b/c/g");
1.140 + r |= test_resolve("http://a/b/c/d;p?q", "g/", "http://a/b/c/g/");
1.141 + r |= test_resolve("http://a/b/c/d;p?q", "/g", "http://a/g");
1.142 + r |= test_resolve("http://a/b/c/d;p?q", "//g", "http://g");
1.143 + r |= test_resolve("http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y");
1.144 + r |= test_resolve("http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y");
1.145 + r |= test_resolve("http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s");
1.146 + r |= test_resolve("http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s");
1.147 + r |= test_resolve("http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s");
1.148 + r |= test_resolve("http://a/b/c/d;p?q", ";x", "http://a/b/c/;x");
1.149 + r |= test_resolve("http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x");
1.150 + r |= test_resolve("http://a/b/c/d;p?q", "g;x?y#s",
1.151 + "http://a/b/c/g;x?y#s");
1.152 + r |= test_resolve("http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q");
1.153 + r |= test_resolve("http://a/b/c/d;p?q", ".", "http://a/b/c/");
1.154 + r |= test_resolve("http://a/b/c/d;p?q", "./", "http://a/b/c/");
1.155 + r |= test_resolve("http://a/b/c/d;p?q", "..", "http://a/b/");
1.156 + r |= test_resolve("http://a/b/c/d;p?q", "../", "http://a/b/");
1.157 + r |= test_resolve("http://a/b/c/d;p?q", "../g", "http://a/b/g");
1.158 + r |= test_resolve("http://a/b/c/d;p?q", "../..", "http://a/");
1.159 + r |= test_resolve("http://a/b/c/d;p?q", "../../", "http://a/");
1.160 + r |= test_resolve("http://a/b/c/d;p?q", "../../g", "http://a/g");
1.161 +
1.162 + r |= test_resolve("http://a/b/c/d;p?q", "../../../g", "http://a/g");
1.163 + r |= test_resolve("http://a/b/c/d;p?q", "../../../../g", "http://a/g");
1.164 +
1.165 + r |= test_resolve("http://a/b/c/d;p?q", "/./g", "http://a/g");
1.166 + r |= test_resolve("http://a/b/c/d;p?q", "/../g", "http://a/g");
1.167 + r |= test_resolve("http://a/b/c/d;p?q", "g.", "http://a/b/c/g.");
1.168 + r |= test_resolve("http://a/b/c/d;p?q", ".g", "http://a/b/c/.g");
1.169 + r |= test_resolve("http://a/b/c/d;p?q", "g..", "http://a/b/c/g..");
1.170 + r |= test_resolve("http://a/b/c/d;p?q", "..g", "http://a/b/c/..g");
1.171 +
1.172 + r |= test_resolve("http://a/b/c/d;p?q", "./../g", "http://a/b/g");
1.173 + r |= test_resolve("http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/");
1.174 + r |= test_resolve("http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h");
1.175 + r |= test_resolve("http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h");
1.176 + r |= test_resolve("http://a/b/c/d;p?q", "g;x=1/./y",
1.177 + "http://a/b/c/g;x=1/y");
1.178 + r |= test_resolve("http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y");
1.179 +
1.180 + r |= test_resolve("http://a/b/c/d;p?q", "g?y/./x",
1.181 + "http://a/b/c/g?y/./x");
1.182 + r |= test_resolve("http://a/b/c/d;p?q", "g?y/../x",
1.183 + "http://a/b/c/g?y/../x");
1.184 + r |= test_resolve("http://a/b/c/d;p?q", "g#s/./x",
1.185 + "http://a/b/c/g#s/./x");
1.186 + r |= test_resolve("http://a/b/c/d;p?q", "g#s/../x",
1.187 + "http://a/b/c/g#s/../x");
1.188 +
1.189 + r |= test_resolve("http://a/b/c/d;p?q", "http:g", "http:g");
1.190 +
1.191 + /* From http://www.w3.org/2000/10/swap/uripath.py */
1.192 + r |= test_resolve("foo:xyz", "bar:abc", "bar:abc");
1.193 +
1.194 + r |= test_resolve("http://example/x/y/z", "../abc",
1.195 + "http://example/x/abc");
1.196 + r |= test_resolve("http://example2/x/y/z", "http://example/x/abc",
1.197 + "http://example/x/abc");
1.198 + r |= test_resolve("http://ex/x/y/z", "../r", "http://ex/x/r");
1.199 + // "http://ex/x/y/z", "../../r", "http://ex/r"); // DanC had this.
1.200 + r |= test_resolve("http://ex/x/y", "q/r", "http://ex/x/q/r");
1.201 + r |= test_resolve("http://ex/x/y", "q/r#s", "http://ex/x/q/r#s");
1.202 + r |= test_resolve("http://ex/x/y", "q/r#s/t", "http://ex/x/q/r#s/t");
1.203 + r |= test_resolve("http://ex/x/y", "ftp://ex/x/q/r", "ftp://ex/x/q/r");
1.204 + r |= test_resolve("http://ex/x/y", "", "http://ex/x/y");
1.205 + r |= test_resolve("http://ex/x/y/", "", "http://ex/x/y/");
1.206 + r |= test_resolve("http://ex/x/y/pdq", "", "http://ex/x/y/pdq");
1.207 + r |= test_resolve("http://ex/x/y/", "z/", "http://ex/x/y/z/");
1.208 + r |= test_resolve("file:/swap/test/animal.rdf", "#Animal",
1.209 + "file:/swap/test/animal.rdf#Animal");
1.210 + r |= test_resolve("file:/e/x/y/z", "../abc", "file:/e/x/abc");
1.211 + r |= test_resolve("file:/example2/x/y/z", "/example/x/abc",
1.212 + "file:/example/x/abc");
1.213 + r |= test_resolve("file:/ex/x/y/z", "../r", "file:/ex/x/r");
1.214 + r |= test_resolve("file:/ex/x/y/z", "/r", "file:/r");
1.215 + r |= test_resolve("file:/ex/x/y", "q/r", "file:/ex/x/q/r");
1.216 + r |= test_resolve("file:/ex/x/y", "q/r#s", "file:/ex/x/q/r#s");
1.217 + r |= test_resolve("file:/ex/x/y", "q/r#", "file:/ex/x/q/r#");
1.218 + r |= test_resolve("file:/ex/x/y", "q/r#s/t", "file:/ex/x/q/r#s/t");
1.219 + r |= test_resolve("file:/ex/x/y", "ftp://ex/x/q/r", "ftp://ex/x/q/r");
1.220 + r |= test_resolve("file:/ex/x/y", "", "file:/ex/x/y");
1.221 + r |= test_resolve("file:/ex/x/y/", "", "file:/ex/x/y/");
1.222 + r |= test_resolve("file:/ex/x/y/pdq", "", "file:/ex/x/y/pdq");
1.223 + r |= test_resolve("file:/ex/x/y/", "z/", "file:/ex/x/y/z/");
1.224 + r |= test_resolve("file:/devel/WWW/2000/10/swap/test/reluri-1.n3",
1.225 + "file://meetings.example.com/cal#m1",
1.226 + "file://meetings.example.com/cal#m1");
1.227 + r |= test_resolve("file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reluri-1.n3",
1.228 + "file://meetings.example.com/cal#m1",
1.229 + "file://meetings.example.com/cal#m1");
1.230 + r |= test_resolve("file:/some/dir/foo", "./#blort",
1.231 + "file:/some/dir/#blort");
1.232 + r |= test_resolve("file:/some/dir/foo", "./#", "file:/some/dir/#");
1.233 + /* From Graham Klyne Thu, 20 Feb 2003 18:08:17 +0000 */
1.234 + r |= test_resolve("http://example/x/y%2Fz", "abc",
1.235 + "http://example/x/abc");
1.236 + r |= test_resolve("http://example/x/y/z", "/x%2Fabc",
1.237 + "http://example/x%2Fabc");
1.238 + r |= test_resolve("http://example/x/y%2Fz", "/x%2Fabc",
1.239 + "http://example/x%2Fabc");
1.240 + r |= test_resolve("http://example/x%2Fy/z", "abc",
1.241 + "http://example/x%2Fy/abc");
1.242 + /* Ryan Lee */
1.243 + r |= test_resolve("http://example/x/abc.efg", "./",
1.244 + "http://example/x/");
1.245 +
1.246 + exit(r ? 1 : 0);
1.247 +}