diff options
author | David Robillard <d@drobilla.net> | 2020-11-09 17:22:12 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-11-09 20:03:10 +0100 |
commit | eae480cfc976a52570bd5b0adc202fa0f8a52ee3 (patch) | |
tree | 21df90d1332ea4539c550ff8be3f1b9a37a2b849 | |
parent | f6d870d67fd8f38539ded74ede295e9b19fb88bc (diff) | |
download | serd-eae480cfc976a52570bd5b0adc202fa0f8a52ee3.tar.gz serd-eae480cfc976a52570bd5b0adc202fa0f8a52ee3.tar.bz2 serd-eae480cfc976a52570bd5b0adc202fa0f8a52ee3.zip |
Split out URI tests
-rw-r--r-- | test/test_serd.c | 136 | ||||
-rw-r--r-- | test/test_uri.c | 171 | ||||
-rw-r--r-- | wscript | 4 |
3 files changed, 174 insertions, 137 deletions
diff --git a/test/test_serd.c b/test/test_serd.c index 05fb0461..34661fd5 100644 --- a/test/test_serd.c +++ b/test/test_serd.c @@ -105,31 +105,6 @@ eof_test_error(void* stream) } static void -test_file_uri(const char* hostname, - const char* path, - bool escape, - const char* expected_uri, - const char* expected_path) -{ - if (!expected_path) { - expected_path = path; - } - - SerdNode node = serd_node_new_file_uri( - USTR(path), USTR(hostname), 0, escape); - - uint8_t* out_hostname = NULL; - uint8_t* out_path = serd_file_uri_parse(node.buf, &out_hostname); - assert(!strcmp((const char*)node.buf, expected_uri)); - assert((hostname && out_hostname) || (!hostname && !out_hostname)); - assert(!strcmp((const char*)out_path, (const char*)expected_path)); - - serd_free(out_path); - serd_free(out_hostname); - serd_node_free(&node); -} - -static void test_read_chunks(void) { ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); @@ -310,57 +285,6 @@ test_strerror(void) } static void -test_uri_to_path(void) -{ - const uint8_t* uri = (const uint8_t*)"file:///home/user/foo.ttl"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")); - - uri = (const uint8_t*)"file://localhost/home/user/foo.ttl"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")); - - uri = (const uint8_t*)"file:illegal/file/uri"; - assert(!serd_uri_to_path(uri)); - - uri = (const uint8_t*)"file:///c:/awful/system"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "c:/awful/system")); - - uri = (const uint8_t*)"file:///c:awful/system"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "/c:awful/system")); - - uri = (const uint8_t*)"file:///0/1"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "/0/1")); - - uri = (const uint8_t*)"C:\\Windows\\Sucks"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "C:\\Windows\\Sucks")); - - uri = (const uint8_t*)"C|/Windows/Sucks"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "C|/Windows/Sucks")); -} - -static void -test_uri_parsing(void) -{ - test_file_uri(NULL, "C:/My 100%", true, - "file:///C:/My%20100%%", NULL); - test_file_uri("ahost", "C:\\Pointless Space", true, - "file://ahost/C:/Pointless%20Space", "C:/Pointless Space"); - test_file_uri(NULL, "/foo/bar", true, - "file:///foo/bar", NULL); - test_file_uri("bhost", "/foo/bar", true, - "file://bhost/foo/bar", NULL); - test_file_uri(NULL, "a/relative path", false, - "a/relative path", NULL); - test_file_uri(NULL, "a/relative <path>", true, - "a/relative%20%3Cpath%3E", NULL); - - // Test tolerance of parsing junk URI escapes - - uint8_t* out_path = serd_file_uri_parse(USTR("file:///foo/%0Xbar"), NULL); - assert(!strcmp((const char*)out_path, "/foo/bar")); - serd_free(out_path); -} - -static void test_node_equals(void) { const uint8_t replacement_char_str[] = { 0xEF, 0xBF, 0xBD, 0 }; @@ -407,62 +331,6 @@ test_node_from_substring(void) } static void -test_uri_from_string(void) -{ - SerdNode nonsense = serd_node_new_uri_from_string(NULL, NULL, NULL); - assert(nonsense.type == SERD_NOTHING); - - SerdURI base_uri; - SerdNode base = serd_node_new_uri_from_string(USTR("http://example.org/"), - NULL, &base_uri); - SerdNode nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL); - SerdNode nil2 = serd_node_new_uri_from_string(USTR(""), &base_uri, NULL); - assert(nil.type == SERD_URI); - assert(!strcmp((const char*)nil.buf, (const char*)base.buf)); - assert(nil2.type == SERD_URI); - assert(!strcmp((const char*)nil2.buf, (const char*)base.buf)); - serd_node_free(&nil); - serd_node_free(&nil2); - - serd_node_free(&base); -} - -static void -test_relative_uri(void) -{ - SerdURI base_uri; - SerdNode base = serd_node_new_uri_from_string(USTR("http://example.org/"), - NULL, &base_uri); - - SerdNode abs = serd_node_from_string(SERD_URI, USTR("http://example.org/foo/bar")); - SerdURI abs_uri; - serd_uri_parse(abs.buf, &abs_uri); - - SerdURI rel_uri; - SerdNode rel = serd_node_new_relative_uri(&abs_uri, &base_uri, NULL, &rel_uri); - assert(!strcmp((const char*)rel.buf, "/foo/bar")); - - SerdNode up = serd_node_new_relative_uri(&base_uri, &abs_uri, NULL, NULL); - assert(!strcmp((const char*)up.buf, "../")); - - SerdNode noup = serd_node_new_relative_uri(&base_uri, &abs_uri, &abs_uri, NULL); - assert(!strcmp((const char*)noup.buf, "http://example.org/")); - - SerdNode x = serd_node_from_string(SERD_URI, USTR("http://example.org/foo/x")); - SerdURI x_uri; - serd_uri_parse(x.buf, &x_uri); - - SerdNode x_rel = serd_node_new_relative_uri(&x_uri, &abs_uri, &abs_uri, NULL); - assert(!strcmp((const char*)x_rel.buf, "x")); - - serd_node_free(&x_rel); - serd_node_free(&noup); - serd_node_free(&up); - serd_node_free(&rel); - serd_node_free(&base); -} - -static void test_writer(const char* const path) { FILE* fd = fopen(path, "wb"); @@ -646,13 +514,9 @@ main(void) test_blob_to_node(); test_strlen(); test_strerror(); - test_uri_to_path(); - test_uri_parsing(); test_node_equals(); test_node_from_string(); test_node_from_substring(); - test_uri_from_string(); - test_relative_uri(); test_read_chunks(); const char* const path = "serd_test.ttl"; diff --git a/test/test_uri.c b/test/test_uri.c new file mode 100644 index 00000000..50fc311d --- /dev/null +++ b/test/test_uri.c @@ -0,0 +1,171 @@ +/* + Copyright 2011-2020 David Robillard <http://drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#undef NDEBUG + +#include "serd/serd.h" + +#include <assert.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#define USTR(s) ((const uint8_t*)(s)) + +static void +test_file_uri(const char* hostname, + const char* path, + bool escape, + const char* expected_uri, + const char* expected_path) +{ + if (!expected_path) { + expected_path = path; + } + + SerdNode node = serd_node_new_file_uri( + USTR(path), USTR(hostname), 0, escape); + + uint8_t* out_hostname = NULL; + uint8_t* out_path = serd_file_uri_parse(node.buf, &out_hostname); + assert(!strcmp((const char*)node.buf, expected_uri)); + assert((hostname && out_hostname) || (!hostname && !out_hostname)); + assert(!strcmp((const char*)out_path, (const char*)expected_path)); + + serd_free(out_path); + serd_free(out_hostname); + serd_node_free(&node); +} + +static void +test_uri_to_path(void) +{ + const uint8_t* uri = (const uint8_t*)"file:///home/user/foo.ttl"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")); + + uri = (const uint8_t*)"file://localhost/home/user/foo.ttl"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")); + + uri = (const uint8_t*)"file:illegal/file/uri"; + assert(!serd_uri_to_path(uri)); + + uri = (const uint8_t*)"file:///c:/awful/system"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "c:/awful/system")); + + uri = (const uint8_t*)"file:///c:awful/system"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "/c:awful/system")); + + uri = (const uint8_t*)"file:///0/1"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "/0/1")); + + uri = (const uint8_t*)"C:\\Windows\\Sucks"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "C:\\Windows\\Sucks")); + + uri = (const uint8_t*)"C|/Windows/Sucks"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "C|/Windows/Sucks")); +} + +static void +test_uri_parsing(void) +{ + test_file_uri(NULL, "C:/My 100%", true, + "file:///C:/My%20100%%", NULL); + test_file_uri("ahost", "C:\\Pointless Space", true, + "file://ahost/C:/Pointless%20Space", "C:/Pointless Space"); + test_file_uri(NULL, "/foo/bar", true, + "file:///foo/bar", NULL); + test_file_uri("bhost", "/foo/bar", true, + "file://bhost/foo/bar", NULL); + test_file_uri(NULL, "a/relative path", false, + "a/relative path", NULL); + test_file_uri(NULL, "a/relative <path>", true, + "a/relative%20%3Cpath%3E", NULL); + + // Test tolerance of parsing junk URI escapes + + uint8_t* out_path = serd_file_uri_parse(USTR("file:///foo/%0Xbar"), NULL); + assert(!strcmp((const char*)out_path, "/foo/bar")); + serd_free(out_path); +} + +static void +test_uri_from_string(void) +{ + SerdNode nonsense = serd_node_new_uri_from_string(NULL, NULL, NULL); + assert(nonsense.type == SERD_NOTHING); + + SerdURI base_uri; + SerdNode base = serd_node_new_uri_from_string(USTR("http://example.org/"), + NULL, &base_uri); + SerdNode nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL); + SerdNode nil2 = serd_node_new_uri_from_string(USTR(""), &base_uri, NULL); + assert(nil.type == SERD_URI); + assert(!strcmp((const char*)nil.buf, (const char*)base.buf)); + assert(nil2.type == SERD_URI); + assert(!strcmp((const char*)nil2.buf, (const char*)base.buf)); + serd_node_free(&nil); + serd_node_free(&nil2); + + serd_node_free(&base); +} + +static void +test_relative_uri(void) +{ + SerdURI base_uri; + SerdNode base = serd_node_new_uri_from_string(USTR("http://example.org/"), + NULL, &base_uri); + + SerdNode abs = serd_node_from_string(SERD_URI, USTR("http://example.org/foo/bar")); + SerdURI abs_uri; + serd_uri_parse(abs.buf, &abs_uri); + + SerdURI rel_uri; + SerdNode rel = serd_node_new_relative_uri(&abs_uri, &base_uri, NULL, &rel_uri); + assert(!strcmp((const char*)rel.buf, "/foo/bar")); + + SerdNode up = serd_node_new_relative_uri(&base_uri, &abs_uri, NULL, NULL); + assert(!strcmp((const char*)up.buf, "../")); + + SerdNode noup = serd_node_new_relative_uri(&base_uri, &abs_uri, &abs_uri, NULL); + assert(!strcmp((const char*)noup.buf, "http://example.org/")); + + SerdNode x = serd_node_from_string(SERD_URI, USTR("http://example.org/foo/x")); + SerdURI x_uri; + serd_uri_parse(x.buf, &x_uri); + + SerdNode x_rel = serd_node_new_relative_uri(&x_uri, &abs_uri, &abs_uri, NULL); + assert(!strcmp((const char*)x_rel.buf, "x")); + + serd_node_free(&x_rel); + serd_node_free(&noup); + serd_node_free(&up); + serd_node_free(&rel); + serd_node_free(&base); +} + +int +main(void) +{ + test_uri_to_path(); + test_uri_parsing(); + test_uri_from_string(); + test_relative_uri(); + + printf("Success\n"); + return 0; +} @@ -214,7 +214,8 @@ def build(bld): ('test_env', 'test/test_env.c'), ('test_free_null', 'test/test_free_null.c'), ('test_read_chunk', 'test/test_read_chunk.c'), - ('test_serd', 'test/test_serd.c')]: + ('test_serd', 'test/test_serd.c'), + ('test_uri', 'test/test_uri.c')]: bld(features = 'c cprogram', source = prog[1], use = 'libserd_profiled', @@ -541,6 +542,7 @@ def test(tst): check(['./test_free_null']) check(['./test_read_chunk']) check(['./test_serd']) + check(['./test_uri']) def test_syntax_io(check, in_name, check_name, lang): in_path = 'test/good/%s' % in_name |