From ca4dfc7f1e5e06ca55c854ca293bcd935040eda0 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Fri, 14 Oct 2011 21:29:13 +0000 Subject: Move nsurl test suite into test/ Fix up llcache tester to reflect nsurl changes Make handling of http:,http:/,http:// consistent Fix buffer overflow when presented with an input string: "http://" svn path=/trunk/netsurf/; revision=13051 --- test/nsurl.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 test/nsurl.c (limited to 'test/nsurl.c') diff --git a/test/nsurl.c b/test/nsurl.c new file mode 100644 index 000000000..217dd42be --- /dev/null +++ b/test/nsurl.c @@ -0,0 +1,186 @@ +#include +#include +#include +#include + +#include + +#include "desktop/netsurf.h" +#include "utils/nsurl.h" +#include "utils/log.h" +#include "utils/utils.h" + +/* desktop/netsurf.h */ +bool verbose_log = true; + +/* utils/utils.h */ +void die(const char * const error) +{ +} + +/* utils/utils.h */ +void warn_user(const char *warning, const char *detail) +{ +} + +struct test_pairs { + const char* test; + const char* res; +}; + +static const struct test_pairs create_tests[] = { + { "http:", "http:" }, + { "http:/", "http:" }, + { "http://", "http:" }, + { "http:/a/b", "http://a/b" }, + { "http://a/b", "http://a/b" }, + + { "http://www.netsurf-browser.org:8080/", + "http://www.netsurf-browser.org:8080/" }, + { "http://user@www.netsurf-browser.org:8080/hello", + "http://user@www.netsurf-browser.org:8080/hello" }, + { "http://user:password@www.netsurf-browser.org:8080/hello", + "http://user:password@www.netsurf-browser.org:8080/hello" }, + + { NULL, NULL } +}; + +static const struct test_pairs join_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" }, + { "http:/b/c", "http://b/c" }, + { "http://", "http:" }, + { "http:/", "http:" }, + { "http:", "http:" }, + /* [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 } +}; + +/** + * Test nsurl + */ +int main(void) +{ + nsurl *base; + nsurl *joined; + char *string; + size_t len; + const char *url; + const struct test_pairs *test; + + /* Create base URL */ + if (nsurl_create("http://a/b/c/d;p?q", &base) != NSERROR_OK) { + assert(0 && "Failed to create base URL."); + } + + 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); + } + + for (test = join_tests; test->test != NULL; test++) { + if (nsurl_join(base, test->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(test->res, string) == 0) { + LOG(("\tPASS: \"%s\"\t--> %s", + test->test, + string)); + } else { + LOG(("\tFAIL: \"%s\"\t--> %s", + test->test, + string)); + LOG(("\t\tExpecting: %s", + test->res)); + assert(0); + } + free(string); + } + nsurl_unref(joined); + } + + } + + nsurl_unref(base); + + /* Create tests */ + LOG(("Testing nsurl_create")); + for (test = create_tests; test->test != NULL; test++) { + if (nsurl_create(test->test, &base) != NSERROR_OK) { + LOG(("Failed to create URL:\n\t\t%s.", test->test)); + } else { + if (strcmp(nsurl_access(base), test->res) == 0) { + LOG(("PASS: \"%s\"\t--> %s", + test->test, nsurl_access(base))); + } else { + LOG(("FAIL: \"%s\"\t--> %s", + test->test, nsurl_access(base))); + LOG(("\t\tExpecting %s", test->res)); + } + + nsurl_unref(base); + } + } + + return 0; +} + -- cgit v1.2.3