diff --git a/src/protocol/test/.gitignore b/src/protocol/test/.gitignore index 08f363c3..6656e2d0 100644 --- a/src/protocol/test/.gitignore +++ b/src/protocol/test/.gitignore @@ -1 +1 @@ -test_uri +uri-parser diff --git a/src/protocol/test/Makefile b/src/protocol/test/Makefile index fdff72ae..45585d0d 100644 --- a/src/protocol/test/Makefile +++ b/src/protocol/test/Makefile @@ -2,7 +2,7 @@ top_builddir=../../.. include $(top_builddir)/Makefile.config TEST_PROGS = \ - test_uri + uri-test TESTDEPS = \ $(top_builddir)/src/protocol/protocol.o \ @@ -11,6 +11,6 @@ TESTDEPS = \ CLEAN = stub.o -test_uri:: stub.o +uri-test:: stub.o include $(top_srcdir)/Makefile.lib diff --git a/src/protocol/test/harness.h b/src/protocol/test/harness.h deleted file mode 100644 index a2d00a1e..00000000 --- a/src/protocol/test/harness.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef EL__PROTOCOL_TEST_HARNESS_H -#define EL__PROTOCOL_TEST_HARNESS_H - -void test_failed(); - -#endif diff --git a/src/protocol/test/stub.c b/src/protocol/test/stub.c index 506d773d..4ed9b672 100644 --- a/src/protocol/test/stub.c +++ b/src/protocol/test/stub.c @@ -2,11 +2,15 @@ #include "config.h" #endif +#include +#include +#include +#include + #include "elinks.h" #include "bfu/msgbox.h" #include "main/module.h" -#include "protocol/test/harness.h" #include "protocol/user.h" #include "session/session.h" @@ -34,11 +38,25 @@ STUB_MODULE(smb_protocol_module); STUB_MODULE(uri_rewrite_module); STUB_MODULE(user_protocol_module); +static void +die(const char *msg, ...) +{ + va_list args; + + if (msg) { + va_start(args, msg); + vfprintf(stderr, msg, args); + fputs("\n", stderr); + va_end(args); + } + + exit(!!NULL); +} + static void stub_called(const unsigned char *fun) { - fprintf(stderr, "FAIL: stub %s\n", fun); - test_failed(); + die("FAIL: stub %s\n", fun); } #define STUB_PROTOCOL_HANDLER(name) \ diff --git a/src/protocol/test/test-normalize-uri b/src/protocol/test/test-normalize-uri new file mode 100755 index 00000000..e9ab2582 --- /dev/null +++ b/src/protocol/test/test-normalize-uri @@ -0,0 +1,98 @@ +#!/bin/sh +# + +test_description='Test URI normalizing' + +. "$TEST_LIB" + +test_uri_equals () { + before="$1"; shift + expected="$1"; shift + normalized="$(uri-test "$before")" + + test_expect_success "Normalize $before" "test \"$normalized\" = \"$expected\"" +} + + +################################################################ + +test_uri_equals "http://example.org/foo/bar/baz?a=1&b=2#frag" \ + "http://example.org/foo/bar/baz?a=1&b=2#frag" + +test_uri_equals "http://example.org/foo/bar/../?a=1&b=2#frag" \ + "http://example.org/foo/?a=1&b=2#frag" + +test_uri_equals "http://example.org/foo/bar/../../baz?a=1&b=2#frag" \ + "http://example.org/baz?a=1&b=2#frag" + +test_uri_equals "http://example.org/foo/bar/.." \ + "http://example.org/foo/" + +test_uri_equals "http://example.org/foo/bar;a=1/.." \ + "http://example.org/foo/" + +test_uri_equals "http://example.org/foo/bar.." \ + "http://example.org/foo/bar.." + +# Bug 744 - ELinks changes "//" to "/" in path component of URI +test_uri_equals "http://example.org/foo/bar/baz" \ + "http://example.org/foo/bar/baz" + +test_uri_equals "http://example.org/foo/bar/" \ + "http://example.org/foo/bar/" + +test_uri_equals "http://example.org/foo//baz" \ + "http://example.org/foo//baz" + +test_uri_equals "http://example.org/foo//" \ + "http://example.org/foo//" + +test_uri_equals "http://example.org//bar/baz" \ + "http://example.org//bar/baz" + +test_uri_equals "http://example.org//bar/" \ + "http://example.org//bar/" + +test_uri_equals "http://example.org///baz" \ + "http://example.org///baz" + +test_uri_equals "http://example.org///" \ + "http://example.org///" + +test_uri_equals "http://example.org/foo/bar/baz/.." \ + "http://example.org/foo/bar/" + +test_uri_equals "http://example.org/foo/bar//.." \ + "http://example.org/foo/bar/" + +test_uri_equals "http://example.org/foo//baz/.." \ + "http://example.org/foo//" + +test_uri_equals "http://example.org/foo///.." \ + "http://example.org/foo//" + +test_uri_equals "http://example.org//bar/baz/.." \ + "http://example.org//bar/" + +test_uri_equals "http://example.org//bar//.." \ + "http://example.org//bar/" + +test_uri_equals "http://example.org///baz/.." \ + "http://example.org///" + +test_uri_equals "http://example.org////.." \ + "http://example.org///" + +test_uri_equals "http://example.org/foo/..//bar/baz" \ + "http://example.org//bar/baz" + +test_uri_equals "http://example.org//.//foo" \ + "http://example.org///foo" + +test_uri_equals "http://example.org//./../foo" \ + "http://example.org/foo" + +test_uri_equals "http://example.org/gag///./../.." \ + "http://example.org/gag/" + +test_done diff --git a/src/protocol/test/test-uri b/src/protocol/test/test-uri deleted file mode 100644 index 599265ee..00000000 --- a/src/protocol/test/test-uri +++ /dev/null @@ -1,2 +0,0 @@ -#! /bin/sh -e -./test_uri diff --git a/src/protocol/test/test_uri.c b/src/protocol/test/test_uri.c deleted file mode 100644 index 7a92d049..00000000 --- a/src/protocol/test/test_uri.c +++ /dev/null @@ -1,142 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include - -#include "elinks.h" - -#include "protocol/test/harness.h" -#include "protocol/uri.h" -#include "util/string.h" - -static int failures = 0; -static int successes = 0; - -void -test_failed(void) -{ - ++failures; -} - -void -test_succeeded(void) -{ - ++successes; -} - -static void -test_1_normalize_uri(const unsigned char *orig, const unsigned char *good) -{ - struct string s; - unsigned char *norm; - - if (!init_string(&s)) { - fputs("FAIL: init_string\n", stderr); - test_failed(); - goto out; - } - if (!add_to_string(&s, orig)) { - fputs("FAIL: add_to_string\n", stderr); - test_failed(); - goto out; - } - - norm = normalize_uri(NULL, s.source); - if (norm == NULL) { - fprintf(stderr, "FAIL: normalize_uri NULL %s\n", orig); - test_failed(); - goto out; - } - if (strcmp(norm, good) != 0) { - fprintf(stderr, "FAIL: normalize_uri mismatch:\n" - "\toriginal: %s\n" - "\tresult: %s\n" - "\texpected: %s\n", - orig, norm, good); - test_failed(); - goto out; - } - - test_succeeded(); - -out: - done_string(&s); -} - -static void -test_normalize_uri(void) -{ - static const struct { - unsigned char *orig; - unsigned char *norm; - } tests[] = { - { "http://example.org/foo/bar/baz?a=1&b=2#frag", - "http://example.org/foo/bar/baz?a=1&b=2#frag" }, - { "http://example.org/foo/bar/../?a=1&b=2#frag", - "http://example.org/foo/?a=1&b=2#frag" }, - { "http://example.org/foo/bar/../../baz?a=1&b=2#frag", - "http://example.org/baz?a=1&b=2#frag" }, - { "http://example.org/foo/bar/..", - "http://example.org/foo/" }, - { "http://example.org/foo/bar;a=1/..", - "http://example.org/foo/" }, - { "http://example.org/foo/bar..", - "http://example.org/foo/bar.." }, - - /* Bug 744 - ELinks changes "//" to "/" in path - * component of URI */ - { "http://example.org/foo/bar/baz", - "http://example.org/foo/bar/baz" }, - { "http://example.org/foo/bar/", - "http://example.org/foo/bar/" }, - { "http://example.org/foo//baz", - "http://example.org/foo//baz" }, - { "http://example.org/foo//", - "http://example.org/foo//" }, - { "http://example.org//bar/baz", - "http://example.org//bar/baz" }, - { "http://example.org//bar/", - "http://example.org//bar/" }, - { "http://example.org///baz", - "http://example.org///baz" }, - { "http://example.org///", - "http://example.org///" }, - { "http://example.org/foo/bar/baz/..", - "http://example.org/foo/bar/" }, - { "http://example.org/foo/bar//..", - "http://example.org/foo/bar/" }, - { "http://example.org/foo//baz/..", - "http://example.org/foo//" }, - { "http://example.org/foo///..", - "http://example.org/foo//" }, - { "http://example.org//bar/baz/..", - "http://example.org//bar/" }, - { "http://example.org//bar//..", - "http://example.org//bar/" }, - { "http://example.org///baz/..", - "http://example.org///" }, - { "http://example.org////..", - "http://example.org///" }, - { "http://example.org/foo/..//bar/baz", - "http://example.org//bar/baz" }, - { "http://example.org//.//foo", - "http://example.org///foo" }, - { "http://example.org//./../foo", - "http://example.org/foo" }, - { "http://example.org/gag///./../..", - "http://example.org/gag/" }, - }; - size_t i; - - for (i = 0; i < sizeof_array(tests); ++i) - test_1_normalize_uri(tests[i].orig, tests[i].norm); -} - -int -main(int argc, char **argv) -{ - test_normalize_uri(); - printf("Total %d failures, %d successes.\n", failures, successes); - return failures ? EXIT_FAILURE : 0; -} diff --git a/src/protocol/test/uri-test.c b/src/protocol/test/uri-test.c new file mode 100644 index 00000000..c1bb40e7 --- /dev/null +++ b/src/protocol/test/uri-test.c @@ -0,0 +1,21 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include "elinks.h" + +#include "protocol/uri.h" +#include "util/string.h" + +int +main(int argc, char **argv) +{ + /* FIXME: As more protocol tests are added this could start + * taking arguments like --normalize-uri= etc. */ + if (argc == 2) { + fprintf(stdout, "%s\n", normalize_uri(NULL, argv[1])); + } + return 0; +}