From 606d7cc64e67fe989476dbfb3e4352f58eabe626 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Tue, 11 Oct 2011 14:59:32 +0000 Subject: [PATCH] Add nsurl testing rig. svn path=/trunk/netsurf/; revision=13035 --- utils/nsurl.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/utils/nsurl.c b/utils/nsurl.c index 884d24ce7..b58f54274 100644 --- a/utils/nsurl.c +++ b/utils/nsurl.c @@ -35,6 +35,9 @@ /* Define to enable NSURL debugging */ #undef NSURL_DEBUG +/* Define to enable NSURL testing */ +#undef NSURL_TEST + static bool nsurl__is_unreserved(unsigned char c) { @@ -927,6 +930,129 @@ static void nsurl__dump(const nsurl *url) #endif +#ifdef NSURL_TEST +/** + * Test nsurl + */ +void nsurl__test(void) +{ + nsurl *base; + nsurl *joined; + struct test_pairs { + const char* test; + const char* res; + }; + + struct test_pairs tests[] = { + /* Normal Examples rfc3986 5.4.1 */ + { "g:h", "g:h" }, + { "g", "http://a/b/c/g" }, + { "./g", "http://a/b/c/g" }, + { "g/", "http://a/b/c/g/" }, + { "/g", "http://a/g" }, + { "//g", "http://g" /* [1] */ "/" }, + { "?y", "http://a/b/c/d;p?y" }, + { "g?y", "http://a/b/c/g?y" }, + { "#s", "http://a/b/c/d;p?q#s" }, + { "g#s", "http://a/b/c/g#s" }, + { "g?y#s", "http://a/b/c/g?y#s" }, + { ";x", "http://a/b/c/;x" }, + { "g;x", "http://a/b/c/g;x" }, + { "g;x?y#s", "http://a/b/c/g;x?y#s" }, + { "", "http://a/b/c/d;p?q" }, + { ".", "http://a/b/c/" }, + { "./", "http://a/b/c/" }, + { "..", "http://a/b/" }, + { "../", "http://a/b/" }, + { "../g", "http://a/b/g" }, + { "../..", "http://a/" }, + { "../../", "http://a/" }, + { "../../g", "http://a/g" }, + + /* Abnormal Examples rfc3986 5.4.2 */ + { "../../../g", "http://a/g" }, + { "../../../../g", "http://a/g" }, + + { "/./g", "http://a/g" }, + { "/../g", "http://a/g" }, + { "g.", "http://a/b/c/g." }, + { ".g", "http://a/b/c/.g" }, + { "g..", "http://a/b/c/g.." }, + { "..g", "http://a/b/c/..g" }, + + { "./../g", "http://a/b/g" }, + { "./g/.", "http://a/b/c/g/" }, + { "g/./h", "http://a/b/c/g/h" }, + { "g/../h", "http://a/b/c/h" }, + { "g;x=1/./y", "http://a/b/c/g;x=1/y" }, + { "g;x=1/../y", "http://a/b/c/y" }, + + { "g?y/./x", "http://a/b/c/g?y/./x" }, + { "g?y/../x", "http://a/b/c/g?y/../x" }, + { "g#s/./x", "http://a/b/c/g#s/./x" }, + { "g#s/../x", "http://a/b/c/g#s/../x" }, + + { "http:g", "http:g" /* [2] */ }, + + /* Extra tests */ + { " g", "http://a/b/c/g" }, + /* [1] Extra slash beyond rfc3986 5.4.1 example, since we're + * testing normalisation in addition to joining */ + /* [2] Using the strict parsers option */ + { NULL, NULL } + }; + int index = 0; + char *string; + size_t len; + + /* Create base URL */ + if (nsurl_create("http://a/b/c/d;p?q", &base) != NSERROR_OK) { + LOG(("Failed to create base URL.")); + } else { + if (nsurl_get(base, NSURL_WITH_FRAGMENT, &string, &len) != + NSERROR_OK) { + LOG(("Failed to get string")); + } else { + LOG(("Testing nsurl_join with base %s", string)); + free(string); + } + + do { + if (nsurl_join(base, tests[index].test, + &joined) != NSERROR_OK) { + LOG(("Failed to join test URL.")); + } else { + if (nsurl_get(joined, NSURL_WITH_FRAGMENT, + &string, &len) != + NSERROR_OK) { + LOG(("Failed to get string")); + } else { + if (strcmp(tests[index].res, + string) == 0) { + LOG(("\tPASS: \"%s\"\t--> %s", + tests[index].test, + string)); + } else { + LOG(("\tFAIL: \"%s\"\t--> %s", + tests[index].test, + string)); + LOG(("\t\tExpecting: %s", + tests[index].res)); + assert(0); + } + free(string); + } + nsurl_unref(joined); + } + + } while (tests[++index].test != NULL); + + nsurl_unref(base); + } +} +#endif + + /****************************************************************************** * NetSurf URL Public API * ******************************************************************************/