/* * Copyright (C) 2016 J. Ali Harlow * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "config.h" #include #include #include #include "razor.h" #include "razor-internal.h" static int test_parse(const char *uri) { struct razor_uri ru; struct razor_error *error = NULL; int r; r = razor_uri_parse(&ru, uri, &error); razor_uri_destroy(&ru); if (r < 0) { fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n", uri, razor_error_get_msg(error)); return -1; } return r; } static int test_normalize(const char *uri, const char *expected_normalized_uri) { struct razor_uri ru; struct razor_error *error = NULL; int r; char *normalized_uri; r = razor_uri_parse(&ru, uri, &error); if (r < 0) { razor_uri_destroy(&ru); fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n", uri, razor_error_get_msg(error)); return -1; } razor_uri_normalize(&ru); normalized_uri = razor_uri_recompose(&ru); razor_uri_destroy(&ru); if (strcmp(normalized_uri, expected_normalized_uri)) { fprintf(stderr, "Fail: normalization of \"%s\" produces " "\"%s\", expected \"%s\"\n", uri, normalized_uri, expected_normalized_uri); r = -1; } free(normalized_uri); return r; } static int test_resolve(const char *base_uri, const char *relative_reference, const char *expected_target_uri) { struct razor_uri base_ru, rr_ru, target_ru; struct razor_error *error = NULL; char *target_uri; int r; r = razor_uri_parse(&base_ru, base_uri, &error); if (r < 0) { fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n", base_uri, razor_error_get_msg(error)); return -1; } r = razor_uri_parse(&rr_ru, relative_reference, &error); if (r < 0) { fprintf(stderr, "Fail: razor_uri_parse(\"%s\") reports %s\n", relative_reference, razor_error_get_msg(error)); razor_uri_destroy(&base_ru); return -1; } razor_uri_resolve(&target_ru, &base_ru, &rr_ru); razor_uri_destroy(&base_ru); razor_uri_destroy(&rr_ru); target_uri = razor_uri_recompose(&target_ru); razor_uri_destroy(&target_ru); if (strcmp(target_uri, expected_target_uri)) { fprintf(stderr, "Fail: razor_uri_resolve(\"%s\", \"%s\") returns %s\n", base_uri, relative_reference, target_uri); free(target_uri); return -1; } free(target_uri); return 0; } int main(int argc, char *argv[]) { int r = 0; r |= test_parse("file:"); r |= test_parse("file:/"); r |= test_parse("file:///"); /* From RFC 3986 § 6.2.2 */ r |= test_normalize("eXAMPLE://a/./b/../b/%63/%7bfoo%7d", "example://a/b/c/%7Bfoo%7D"); /* From RFC 3986 § 5.4.1 */ r |= test_resolve("http://a/b/c/d;p?q", "g:h", "g:h"); r |= test_resolve("http://a/b/c/d;p?q", "g", "http://a/b/c/g"); r |= test_resolve("http://a/b/c/d;p?q", "./g", "http://a/b/c/g"); r |= test_resolve("http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"); r |= test_resolve("http://a/b/c/d;p?q", "/g", "http://a/g"); r |= test_resolve("http://a/b/c/d;p?q", "//g", "http://g"); r |= test_resolve("http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y"); r |= test_resolve("http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"); r |= test_resolve("http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s"); r |= test_resolve("http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"); r |= test_resolve("http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"); r |= test_resolve("http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"); r |= test_resolve("http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"); r |= test_resolve("http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"); r |= test_resolve("http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q"); r |= test_resolve("http://a/b/c/d;p?q", ".", "http://a/b/c/"); r |= test_resolve("http://a/b/c/d;p?q", "./", "http://a/b/c/"); r |= test_resolve("http://a/b/c/d;p?q", "..", "http://a/b/"); r |= test_resolve("http://a/b/c/d;p?q", "../", "http://a/b/"); r |= test_resolve("http://a/b/c/d;p?q", "../g", "http://a/b/g"); r |= test_resolve("http://a/b/c/d;p?q", "../..", "http://a/"); r |= test_resolve("http://a/b/c/d;p?q", "../../", "http://a/"); r |= test_resolve("http://a/b/c/d;p?q", "../../g", "http://a/g"); r |= test_resolve("http://a/b/c/d;p?q", "../../../g", "http://a/g"); r |= test_resolve("http://a/b/c/d;p?q", "../../../../g", "http://a/g"); r |= test_resolve("http://a/b/c/d;p?q", "/./g", "http://a/g"); r |= test_resolve("http://a/b/c/d;p?q", "/../g", "http://a/g"); r |= test_resolve("http://a/b/c/d;p?q", "g.", "http://a/b/c/g."); r |= test_resolve("http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"); r |= test_resolve("http://a/b/c/d;p?q", "g..", "http://a/b/c/g.."); r |= test_resolve("http://a/b/c/d;p?q", "..g", "http://a/b/c/..g"); r |= test_resolve("http://a/b/c/d;p?q", "./../g", "http://a/b/g"); r |= test_resolve("http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"); r |= test_resolve("http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"); r |= test_resolve("http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h"); r |= test_resolve("http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"); r |= test_resolve("http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y"); r |= test_resolve("http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x"); r |= test_resolve("http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x"); r |= test_resolve("http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x"); r |= test_resolve("http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x"); r |= test_resolve("http://a/b/c/d;p?q", "http:g", "http:g"); /* From http://www.w3.org/2000/10/swap/uripath.py */ r |= test_resolve("foo:xyz", "bar:abc", "bar:abc"); r |= test_resolve("http://example/x/y/z", "../abc", "http://example/x/abc"); r |= test_resolve("http://example2/x/y/z", "http://example/x/abc", "http://example/x/abc"); r |= test_resolve("http://ex/x/y/z", "../r", "http://ex/x/r"); // "http://ex/x/y/z", "../../r", "http://ex/r"); // DanC had this. r |= test_resolve("http://ex/x/y", "q/r", "http://ex/x/q/r"); r |= test_resolve("http://ex/x/y", "q/r#s", "http://ex/x/q/r#s"); r |= test_resolve("http://ex/x/y", "q/r#s/t", "http://ex/x/q/r#s/t"); r |= test_resolve("http://ex/x/y", "ftp://ex/x/q/r", "ftp://ex/x/q/r"); r |= test_resolve("http://ex/x/y", "", "http://ex/x/y"); r |= test_resolve("http://ex/x/y/", "", "http://ex/x/y/"); r |= test_resolve("http://ex/x/y/pdq", "", "http://ex/x/y/pdq"); r |= test_resolve("http://ex/x/y/", "z/", "http://ex/x/y/z/"); r |= test_resolve("file:/swap/test/animal.rdf", "#Animal", "file:/swap/test/animal.rdf#Animal"); r |= test_resolve("file:/e/x/y/z", "../abc", "file:/e/x/abc"); r |= test_resolve("file:/example2/x/y/z", "/example/x/abc", "file:/example/x/abc"); r |= test_resolve("file:/ex/x/y/z", "../r", "file:/ex/x/r"); r |= test_resolve("file:/ex/x/y/z", "/r", "file:/r"); r |= test_resolve("file:/ex/x/y", "q/r", "file:/ex/x/q/r"); r |= test_resolve("file:/ex/x/y", "q/r#s", "file:/ex/x/q/r#s"); r |= test_resolve("file:/ex/x/y", "q/r#", "file:/ex/x/q/r#"); r |= test_resolve("file:/ex/x/y", "q/r#s/t", "file:/ex/x/q/r#s/t"); r |= test_resolve("file:/ex/x/y", "ftp://ex/x/q/r", "ftp://ex/x/q/r"); r |= test_resolve("file:/ex/x/y", "", "file:/ex/x/y"); r |= test_resolve("file:/ex/x/y/", "", "file:/ex/x/y/"); r |= test_resolve("file:/ex/x/y/pdq", "", "file:/ex/x/y/pdq"); r |= test_resolve("file:/ex/x/y/", "z/", "file:/ex/x/y/z/"); r |= test_resolve("file:/devel/WWW/2000/10/swap/test/reluri-1.n3", "file://meetings.example.com/cal#m1", "file://meetings.example.com/cal#m1"); r |= test_resolve("file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reluri-1.n3", "file://meetings.example.com/cal#m1", "file://meetings.example.com/cal#m1"); r |= test_resolve("file:/some/dir/foo", "./#blort", "file:/some/dir/#blort"); r |= test_resolve("file:/some/dir/foo", "./#", "file:/some/dir/#"); /* From Graham Klyne Thu, 20 Feb 2003 18:08:17 +0000 */ r |= test_resolve("http://example/x/y%2Fz", "abc", "http://example/x/abc"); r |= test_resolve("http://example/x/y/z", "/x%2Fabc", "http://example/x%2Fabc"); r |= test_resolve("http://example/x/y%2Fz", "/x%2Fabc", "http://example/x%2Fabc"); r |= test_resolve("http://example/x%2Fy/z", "abc", "http://example/x%2Fy/abc"); /* Ryan Lee */ r |= test_resolve("http://example/x/abc.efg", "./", "http://example/x/"); exit(r ? 1 : 0); }