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
This commit is contained in:
John Mark Bell 2011-10-14 21:29:13 +00:00
parent deae02c0e7
commit ca4dfc7f1e
4 changed files with 248 additions and 191 deletions

View File

@ -5,10 +5,12 @@ LDFLAGS := `pkg-config --libs libxml-2.0 libcurl`
llcache_CFLAGS := `pkg-config --cflags libparserutils libwapcaplet`
llcache_LDFLAGS := `pkg-config --libs libparserutils libwapcaplet`
llcache_SRCS := content/fetch.c content/fetchers/fetch_curl.c \
content/fetchers/fetch_data.c content/llcache.c \
llcache_SRCS := content/fetch.c content/fetchers/curl.c \
content/fetchers/about.c content/fetchers/data.c \
content/fetchers/resource.c content/llcache.c \
content/urldb.c desktop/options.c desktop/version.c \
utils/base64.c utils/hashtable.c utils/log.c \
image/image_cache.c \
utils/base64.c utils/hashtable.c utils/log.c utils/nsurl.c \
utils/messages.c utils/url.c utils/useragent.c utils/utf8.c \
utils/utils.c test/llcache.c
@ -19,9 +21,13 @@ urldbtest_SRCS := content/urldb.c utils/url.c utils/utils.c utils/log.c \
urldbtest_CFLAGS := -O2
urldbtest_LDFLAGS :=
nsurl_SRCS := utils/hashtable.c utils/log.c utils/messages.c utils/nsurl.c utils/utils.c test/nsurl.c
nsurl_CFLAGS := `pkg-config --cflags libwapcaplet`
nsurl_LDFLAGS := `pkg-config --libs libwapcaplet`
.PHONY: all
all: llcache urldbtest
all: llcache urldbtest nsurl
llcache: $(addprefix ../,$(llcache_SRCS))
$(CC) $(CFLAGS) $(llcache_CFLAGS) $^ -o $@ $(LDFLAGS) $(llcache_LDFLAGS)
@ -29,7 +35,10 @@ llcache: $(addprefix ../,$(llcache_SRCS))
urldbtest: $(addprefix ../,$(urldbtest_SRCS))
$(CC) $(CFLAGS) $(urldbtest_CFLAGS) $^ -o $@ $(LDFLAGS) $(urldbtest_LDFLAGS)
nsurl: $(addprefix ../,$(nsurl_SRCS))
$(CC) $(CFLAGS) $(nsurl_CFLAGS) $^ -o $@ $(LDFLAGS) $(nsurl_LDFLAGS)
.PHONY: clean
clean:
$(RM) llcache urldbtest
$(RM) llcache urldbtest nsurl

View File

@ -7,7 +7,10 @@
#include "content/fetch.h"
#include "content/llcache.h"
#include "utils/ring.h"
#include "utils/nsurl.h"
#include "utils/schedule.h"
#include "utils/url.h"
#include "utils/utils.h"
/******************************************************************************
* Things that we'd reasonably expect to have to implement *
@ -44,6 +47,16 @@ char *filename_from_path(char *path)
return strdup(leafname);
}
/* utils/schedule.h */
void schedule(int t, schedule_callback_fn cb, void *pw)
{
}
/* utils/schedule.h */
void schedule_remove(schedule_callback_fn cb, void *pw)
{
}
/* content/fetch.h */
const char *fetch_filetype(const char *unix_path)
{
@ -93,6 +106,7 @@ char *url_to_path(const char *url)
******************************************************************************/
#include "desktop/cookies.h"
#include "desktop/gui.h"
#include "desktop/tree.h"
/* desktop/cookies.h -- used by urldb
@ -129,6 +143,12 @@ void fetch_file_register(void)
{
}
/* desktop/gui.h -- used by image_cache through about: handler */
nsurl* gui_get_resource_url(const char *path)
{
return NULL;
}
/******************************************************************************
* test: protocol handler *
******************************************************************************/
@ -145,18 +165,18 @@ typedef struct test_context {
static test_context *ring;
bool test_initialise(const char *scheme)
bool test_initialise(lwc_string *scheme)
{
/* Nothing to do */
return true;
}
void test_finalise(const char *scheme)
void test_finalise(lwc_string *scheme)
{
/* Nothing to do */
}
void *test_setup_fetch(struct fetch *parent, const char *url, bool only_2xx,
void *test_setup_fetch(struct fetch *parent, nsurl *url, bool only_2xx,
const char *post_urlenc,
const struct fetch_multipart_data *post_multipart,
const char **headers)
@ -200,7 +220,7 @@ void test_process(test_context *ctx)
/** \todo Implement */
}
void test_poll(const char *scheme)
void test_poll(lwc_string *scheme)
{
test_context *ctx, *next;
@ -259,24 +279,36 @@ int main(int argc, char **argv)
nserror error;
llcache_handle *handle;
llcache_handle *handle2;
lwc_string *scheme;
nsurl *url;
bool done = false;
/* Initialise subsystems */
url_init();
fetch_init();
fetch_add_fetcher("test", test_initialise, test_setup_fetch,
if (lwc_intern_string("test", SLEN("test"), &scheme) != lwc_error_ok) {
fprintf(stderr, "Failed to intern \"test\"\n");
return 1;
}
fetch_add_fetcher(scheme, test_initialise, test_setup_fetch,
test_start_fetch, test_abort_fetch, test_free_fetch,
test_poll, test_finalise);
/* Initialise low-level cache */
error = llcache_initialise(query_handler, NULL);
error = llcache_initialise(query_handler, NULL, 1024 * 1024);
if (error != NSERROR_OK) {
fprintf(stderr, "llcache_initialise: %d\n", error);
return 1;
}
if (nsurl_create("http://www.netsurf-browser.org", &url) != NSERROR_OK) {
fprintf(stderr, "Failed creating url\n");
return 1;
}
/* Retrieve an URL from the low-level cache (may trigger fetch) */
error = llcache_handle_retrieve("http://www.netsurf-browser.org/",
error = llcache_handle_retrieve(url,
LLCACHE_RETRIEVE_VERIFIABLE, NULL, NULL,
event_handler, &done, &handle);
if (error != NSERROR_OK) {
@ -286,12 +318,11 @@ int main(int argc, char **argv)
/* Poll relevant components */
while (done == false) {
fetch_poll();
llcache_poll();
}
done = false;
error = llcache_handle_retrieve("http://www.netsurf-browser.org/",
error = llcache_handle_retrieve(url,
LLCACHE_RETRIEVE_VERIFIABLE, NULL, NULL,
event_handler, &done, &handle2);
if (error != NSERROR_OK) {
@ -300,7 +331,6 @@ int main(int argc, char **argv)
}
while (done == false) {
fetch_poll();
llcache_poll();
}

186
test/nsurl.c Normal file
View File

@ -0,0 +1,186 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libwapcaplet/libwapcaplet.h>
#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;
}

View File

@ -35,10 +35,6 @@
/* Define to enable NSURL debugging */
#undef NSURL_DEBUG
/* Define to enable NSURL testing */
#undef NSURL_TEST
static bool nsurl__is_unreserved(unsigned char c)
{
/* From RFC3986 section 2.3 (unreserved characters)
@ -215,7 +211,6 @@ static void nsurl__get_string_markers(const char const *url_s,
pos++;
while (*pos != ':' && *pos != '\0') {
if (!isalnum(*pos) && *pos != '+' &&
*pos != '-' && *pos != '.') {
/* This character is not valid in the
@ -284,10 +279,9 @@ static void nsurl__get_string_markers(const char const *url_s,
* If this URL is not getting joined, we are less strict in the case of
* http(s) and will accept any number of slashes, including 0.
*/
if (*pos != '\0' &&
((joining == false && is_http == true && *pos != '/') ||
(joining == true && is_http == true && *pos == '/') ||
(*pos == '/' && *(pos + 1) == '/'))) {
if ((*pos == '/' && *(pos + 1) == '/') ||
(is_http && ((joining && (*pos == '/')) ||
joining == false))) {
/* Skip over leading slashes */
if (is_http == false) {
if (*pos == '/') pos++;
@ -301,7 +295,7 @@ static void nsurl__get_string_markers(const char const *url_s,
marker.colon_last = marker.path = pos - url_s;
/* Need to get (or complete) the authority */
do {
while (*pos != '\0') {
if (*pos == '/' || *pos == '?' || *pos == '#') {
/* End of the authority */
break;
@ -322,7 +316,9 @@ static void nsurl__get_string_markers(const char const *url_s,
/* Credentials @ host separator */
marker.at = pos - url_s;
}
} while (*(++pos) != '\0');
pos++;
}
marker.path = pos - url_s;
@ -937,170 +933,6 @@ 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" },
{ " http:/b/c", "http://b/c" },
/* [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;
const char *url;
/* 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);
}
/* Other tests */
url = "http://www.netsurf-browser.org:8080/";
if (nsurl_create(url, &base) != NSERROR_OK) {
LOG(("Failed to create URL:\n\t\t%s.", url));
} else {
if (strcmp(nsurl_access(base), url) != 0)
LOG(("FAIL:\n\t\t%s\n\t\t--> %s",
url, nsurl_access(base)));
nsurl_unref(base);
}
url = "http://user@www.netsurf-browser.org:8080/hello";
if (nsurl_create(url, &base) != NSERROR_OK) {
LOG(("Failed to create URL:\n\t\t%s.", url));
} else {
if (strcmp(nsurl_access(base), url) != 0)
LOG(("FAIL:\n\t\t%s\n\t\t--> %s",
url, nsurl_access(base)));
nsurl_unref(base);
}
url = "http://user:password@www.netsurf-browser.org:8080/hello";
if (nsurl_create(url, &base) != NSERROR_OK) {
LOG(("Failed to create URL:\n\t\t%s.", url));
} else {
if (strcmp(nsurl_access(base), url) != 0)
LOG(("FAIL:\n\t\t%s\n\t\t--> %s",
url, nsurl_access(base)));
nsurl_unref(base);
}
}
#endif
/******************************************************************************
* NetSurf URL Public API *
******************************************************************************/