2011-10-15 01:29:13 +04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <libwapcaplet/libwapcaplet.h>
|
|
|
|
|
|
|
|
#include "desktop/netsurf.h"
|
|
|
|
#include "utils/log.h"
|
2011-10-15 17:06:36 +04:00
|
|
|
#include "utils/nsurl.h"
|
2011-10-15 01:29:13 +04:00
|
|
|
|
|
|
|
/* desktop/netsurf.h */
|
|
|
|
bool verbose_log = true;
|
|
|
|
|
|
|
|
struct test_pairs {
|
|
|
|
const char* test;
|
|
|
|
const char* res;
|
|
|
|
};
|
|
|
|
|
2012-10-11 22:23:22 +04:00
|
|
|
struct test_triplets {
|
|
|
|
const char* test1;
|
|
|
|
const char* test2;
|
|
|
|
const char* res;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void netsurf_lwc_iterator(lwc_string *str, void *pw)
|
|
|
|
{
|
|
|
|
LOG(("[%3u] %.*s", str->refcnt, (int) lwc_string_length(str),
|
|
|
|
lwc_string_data(str)));
|
|
|
|
}
|
|
|
|
|
2011-10-15 01:29:13 +04:00
|
|
|
static const struct test_pairs create_tests[] = {
|
|
|
|
{ "http:", "http:" },
|
|
|
|
{ "http:/", "http:" },
|
|
|
|
{ "http://", "http:" },
|
2011-10-15 01:31:53 +04:00
|
|
|
{ "http:a", "http://a/" },
|
|
|
|
{ "http:a/", "http://a/" },
|
|
|
|
{ "http:a/b", "http://a/b" },
|
|
|
|
{ "http:/a", "http://a/" },
|
2011-10-15 01:29:13 +04:00
|
|
|
{ "http:/a/b", "http://a/b" },
|
2011-10-15 01:31:53 +04:00
|
|
|
{ "http://a", "http://a/" },
|
2011-10-15 01:29:13 +04:00
|
|
|
{ "http://a/b", "http://a/b" },
|
2011-10-15 15:51:54 +04:00
|
|
|
{ "www.example.org", "http://www.example.org/" },
|
|
|
|
{ "www.example.org/x", "http://www.example.org/x" },
|
2011-10-15 18:17:09 +04:00
|
|
|
{ "about:", "about:" },
|
|
|
|
{ "about:blank", "about:blank" },
|
2011-10-15 01:29:13 +04:00
|
|
|
|
2011-10-15 18:17:09 +04:00
|
|
|
{ "http://www.ns-b.org:8080/",
|
|
|
|
"http://www.ns-b.org:8080/" },
|
|
|
|
{ "http://user@www.ns-b.org:8080/hello",
|
|
|
|
"http://user@www.ns-b.org:8080/hello" },
|
|
|
|
{ "http://user:pass@www.ns-b.org:8080/hello",
|
|
|
|
"http://user:pass@www.ns-b.org:8080/hello" },
|
2011-10-15 01:29:13 +04:00
|
|
|
|
2011-11-01 02:24:05 +04:00
|
|
|
{ "http://www.ns-b.org:80/",
|
|
|
|
"http://www.ns-b.org/" },
|
|
|
|
{ "http://user@www.ns-b.org:80/hello",
|
|
|
|
"http://user@www.ns-b.org/hello" },
|
|
|
|
{ "http://user:pass@www.ns-b.org:80/hello",
|
|
|
|
"http://user:pass@www.ns-b.org/hello" },
|
|
|
|
|
|
|
|
{ "http://www.ns-b.org:/",
|
|
|
|
"http://www.ns-b.org/" },
|
|
|
|
{ "http://u@www.ns-b.org:/hello",
|
|
|
|
"http://u@www.ns-b.org/hello" },
|
|
|
|
{ "http://u:p@www.ns-b.org:/hello",
|
|
|
|
"http://u:p@www.ns-b.org/hello" },
|
|
|
|
|
2011-11-03 16:16:01 +04:00
|
|
|
{ "http:a/", "http://a/" },
|
|
|
|
{ "http:/a/", "http://a/" },
|
|
|
|
{ "http://u@a", "http://u@a/" },
|
|
|
|
{ "http://@a", "http://a/" },
|
|
|
|
|
|
|
|
{ "mailto:u@a", "mailto:u@a" },
|
|
|
|
{ "mailto:@a", "mailto:a" },
|
|
|
|
|
2011-10-15 01:29:13 +04:00
|
|
|
{ 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" },
|
2011-10-15 18:17:09 +04:00
|
|
|
{ "g ", "http://a/b/c/g" },
|
|
|
|
{ " g ", "http://a/b/c/g" },
|
2011-10-15 01:29:13 +04:00
|
|
|
{ "http:/b/c", "http://b/c" },
|
|
|
|
{ "http://", "http:" },
|
|
|
|
{ "http:/", "http:" },
|
|
|
|
{ "http:", "http:" },
|
2012-11-02 14:54:34 +04:00
|
|
|
{ " ", "http://a/b/c/d;p?q" },
|
|
|
|
{ " ", "http://a/b/c/d;p?q" },
|
|
|
|
{ "/", "http://a/" },
|
|
|
|
{ " / ", "http://a/" },
|
|
|
|
{ " ? ", "http://a/b/c/d;p?" },
|
|
|
|
{ " h ", "http://a/b/c/h" },
|
2011-10-15 01:29:13 +04:00
|
|
|
/* [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 }
|
|
|
|
};
|
|
|
|
|
2012-10-11 22:23:22 +04:00
|
|
|
static const struct test_triplets replace_query_tests[] = {
|
|
|
|
{ "http://netsurf-browser.org/?magical=true",
|
|
|
|
"?magical=true&result=win",
|
|
|
|
"http://netsurf-browser.org/?magical=true&result=win"},
|
|
|
|
|
|
|
|
{ "http://netsurf-browser.org/?magical=true#fragment",
|
|
|
|
"?magical=true&result=win",
|
|
|
|
"http://netsurf-browser.org/?magical=true&result=win#fragment"},
|
|
|
|
|
|
|
|
{ "http://netsurf-browser.org/#fragment",
|
|
|
|
"?magical=true&result=win",
|
|
|
|
"http://netsurf-browser.org/?magical=true&result=win#fragment"},
|
|
|
|
|
|
|
|
{ "http://netsurf-browser.org/path",
|
|
|
|
"?magical=true",
|
|
|
|
"http://netsurf-browser.org/path?magical=true"},
|
|
|
|
|
|
|
|
{ NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2011-10-15 01:29:13 +04:00
|
|
|
/**
|
|
|
|
* Test nsurl
|
|
|
|
*/
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
nsurl *base;
|
|
|
|
nsurl *joined;
|
|
|
|
char *string;
|
|
|
|
size_t len;
|
|
|
|
const char *url;
|
|
|
|
const struct test_pairs *test;
|
2012-10-11 22:23:22 +04:00
|
|
|
const struct test_triplets *ttest;
|
2011-10-15 18:17:09 +04:00
|
|
|
int passed = 0;
|
|
|
|
int count = 0;
|
2011-10-15 01:29:13 +04:00
|
|
|
|
|
|
|
/* 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));
|
2011-10-15 18:17:09 +04:00
|
|
|
passed++;
|
2011-10-15 01:29:13 +04:00
|
|
|
} else {
|
|
|
|
LOG(("\tFAIL: \"%s\"\t--> %s",
|
|
|
|
test->test,
|
|
|
|
string));
|
|
|
|
LOG(("\t\tExpecting: %s",
|
|
|
|
test->res));
|
|
|
|
}
|
|
|
|
free(string);
|
|
|
|
}
|
|
|
|
nsurl_unref(joined);
|
|
|
|
}
|
2011-10-15 18:17:09 +04:00
|
|
|
count++;
|
2011-10-15 01:29:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2011-10-15 18:17:09 +04:00
|
|
|
LOG(("\tPASS: \"%s\"\t--> %s",
|
2011-10-15 01:29:13 +04:00
|
|
|
test->test, nsurl_access(base)));
|
2011-10-15 18:17:09 +04:00
|
|
|
passed++;
|
2011-10-15 01:29:13 +04:00
|
|
|
} else {
|
2011-10-15 18:17:09 +04:00
|
|
|
LOG(("\tFAIL: \"%s\"\t--> %s",
|
2011-10-15 01:29:13 +04:00
|
|
|
test->test, nsurl_access(base)));
|
|
|
|
LOG(("\t\tExpecting %s", test->res));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsurl_unref(base);
|
|
|
|
}
|
2011-10-15 18:17:09 +04:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2012-10-11 22:23:22 +04:00
|
|
|
/* Replace query tests */
|
|
|
|
LOG(("Testing nsurl_replace_query"));
|
|
|
|
for (ttest = replace_query_tests; ttest->test1 != NULL; ttest++) {
|
|
|
|
if (nsurl_create(ttest->test1, &base) != NSERROR_OK) {
|
|
|
|
LOG(("Failed to create URL:\n\t\t%s.", ttest->test1));
|
|
|
|
} else {
|
|
|
|
if (nsurl_replace_query(base, ttest->test2, &joined) !=
|
|
|
|
NSERROR_OK) {
|
|
|
|
LOG(("Failed to make test URL"));
|
|
|
|
} else {
|
|
|
|
if (strcmp(nsurl_access(joined),
|
|
|
|
ttest->res) == 0) {
|
|
|
|
LOG(("\tPASS: \"%s\" + %s",
|
|
|
|
ttest->test1,
|
|
|
|
ttest->test2));
|
|
|
|
passed++;
|
|
|
|
} else {
|
|
|
|
LOG(("\tFAIL: \"%s\" + %s",
|
|
|
|
ttest->test1,
|
|
|
|
ttest->test2));
|
|
|
|
LOG(("\t\tExpecting %s", ttest->res));
|
|
|
|
LOG(("\t\tGot %s",
|
|
|
|
nsurl_access(joined)));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsurl_unref(joined);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsurl_unref(base);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2011-10-15 18:17:09 +04:00
|
|
|
if (passed == count) {
|
|
|
|
LOG(("Testing complete: SUCCESS"));
|
|
|
|
} else {
|
|
|
|
LOG(("Testing complete: FAILURE"));
|
|
|
|
LOG(("Failed %d out of %d", count - passed, count));
|
2011-10-15 01:29:13 +04:00
|
|
|
}
|
|
|
|
|
2012-10-11 22:23:22 +04:00
|
|
|
LOG(("Remaining lwc strings:"));
|
|
|
|
lwc_iterate_strings(netsurf_lwc_iterator, NULL);
|
|
|
|
|
2011-10-15 01:29:13 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|