librazor/test-uri.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jun 08 18:02:49 2018 +0100 (2018-06-08)
changeset 501 850be6a6885c
permissions -rw-r--r--
Added tag 0.7 for changeset f98d77376544
     1 /*
     2  * Copyright (C) 2016  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 
    19 #include "config.h"
    20 
    21 #include <stdlib.h>
    22 #include <stdio.h>
    23 #include <string.h>
    24 #include "razor.h"
    25 #include "razor-internal.h"
    26 
    27 static int test_parse(const char *uri)
    28 {
    29 	struct razor_uri ru;
    30 	struct razor_error *error = NULL;
    31 	int r;
    32 
    33 	r = razor_uri_parse(&ru, uri, &error);
    34 	razor_uri_destroy(&ru);
    35 
    36 	if (r < 0) {
    37 		fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
    38 			uri, razor_error_get_msg(error));
    39 		return -1;
    40 	}
    41 
    42 	return r;
    43 }
    44 
    45 static int test_normalize(const char *uri, const char *expected_normalized_uri)
    46 {
    47 	struct razor_uri ru;
    48 	struct razor_error *error = NULL;
    49 	int r;
    50 	char *normalized_uri;
    51 
    52 	r = razor_uri_parse(&ru, uri, &error);
    53 
    54 	if (r < 0) {
    55 		razor_uri_destroy(&ru);
    56 		fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
    57 			uri, razor_error_get_msg(error));
    58 		return -1;
    59 	}
    60 
    61 	razor_uri_normalize(&ru);
    62 	normalized_uri = razor_uri_recompose(&ru);
    63 	razor_uri_destroy(&ru);
    64 
    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);
    69 		r = -1;
    70 	}
    71 
    72 	free(normalized_uri);
    73 	return r;
    74 }
    75 
    76 static int test_resolve(const char *base_uri, const char *relative_reference,
    77   const char *expected_target_uri)
    78 {
    79 	struct razor_uri base_ru, rr_ru, target_ru;
    80 	struct razor_error *error = NULL;
    81 	char *target_uri;
    82 	int r;
    83 
    84 	r = razor_uri_parse(&base_ru, base_uri, &error);
    85 
    86 	if (r < 0) {
    87 		fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n",
    88 			base_uri, razor_error_get_msg(error));
    89 		return -1;
    90 	}
    91 
    92 	r = razor_uri_parse(&rr_ru, relative_reference, &error);
    93 
    94 	if (r < 0) {
    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);
    98 		return -1;
    99 	}
   100 
   101 	razor_uri_resolve(&target_ru, &base_ru, &rr_ru);
   102 	razor_uri_destroy(&base_ru);
   103 	razor_uri_destroy(&rr_ru);
   104 
   105 	target_uri = razor_uri_recompose(&target_ru);
   106 	razor_uri_destroy(&target_ru);
   107 
   108 	if (strcmp(target_uri, expected_target_uri)) {
   109 		fprintf(stderr,
   110 			"Fail: razor_uri_resolve(\"%s\", \"%s\") returns %s\n",
   111 			base_uri, relative_reference, target_uri);
   112 		free(target_uri);
   113 		return -1;
   114 	}
   115 
   116 	free(target_uri);
   117 
   118 	return 0;
   119 }
   120 
   121 int main(int argc, char *argv[])
   122 {
   123 	int r = 0;
   124 
   125 	r |= test_parse("file:");
   126 	r |= test_parse("file:/");
   127 	r |= test_parse("file:///");
   128 
   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");
   132 
   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");
   158 
   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");
   161 
   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");
   168 
   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");
   176 
   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");
   185 
   186 	r |= test_resolve("http://a/b/c/d;p?q", "http:g", "http:g");
   187 
   188 	/* From http://www.w3.org/2000/10/swap/uripath.py */
   189 	r |= test_resolve("foo:xyz", "bar:abc", "bar:abc");
   190 
   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");
   239 	/* Ryan Lee */
   240 	r |= test_resolve("http://example/x/abc.efg", "./",
   241 			  "http://example/x/");
   242 
   243 	exit(r ? 1 : 0);
   244 }