/* Copyright 2011-2017 David Robillard 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. */ #include #include #include #include #include #include #include #include "serd/serd.h" #include "test_utils.h" #ifndef INFINITY # define INFINITY (DBL_MAX + DBL_MAX) #endif #ifndef NAN # define NAN (INFINITY - INFINITY) #endif #define NS_XSD "http://www.w3.org/2001/XMLSchema#" static int test_strtod(double dbl, double max_delta) { char buf[1024]; snprintf(buf, sizeof(buf), "%f", dbl); size_t end = 0; const double out = serd_strtod(buf, &end); const double diff = fabs(out - dbl); if (diff > max_delta) { FAILF("Parsed %lf != %lf (delta %lf)\n", dbl, out, diff); } if (end != strlen(buf)) { FAILF("Parsed %lf length %zu != %zu\n", end, strlen(buf)); } return 0; } static SerdStatus count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri) { ++*(int*)handle; return SERD_SUCCESS; } typedef struct { int n_statements; const SerdNode* graph; } ReaderTest; static SerdStatus test_sink(void* handle, SerdStatementFlags flags, const SerdNode* graph, const SerdNode* subject, const SerdNode* predicate, const SerdNode* object) { ReaderTest* rt = (ReaderTest*)handle; ++rt->n_statements; rt->graph = graph; return SERD_SUCCESS; } static int check_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(path, hostname, escape); const char* node_str = serd_node_get_string(node); char* out_hostname = NULL; char* out_path = serd_file_uri_parse(node_str, &out_hostname); int ret = 0; if (strcmp(node_str, expected_uri)) { ret = FAILF("Bad URI %s != %s\n", node_str, expected_uri); } else if (!hostname && out_hostname) { ret = FAILF("hostname `%s' shouldn't exist\n", out_hostname); } else if (hostname && !out_hostname) { ret = FAILF("expected hostname `%s'\n", hostname); } else if (strcmp((const char*)out_path, (const char*)expected_path)) { ret = FAILF("path=>URI=>path failure %s => %s => %s\n", path, node_str, out_path); } free(out_path); free(out_hostname); serd_node_free(node); return ret; } static int check_rel_uri(const char* uri, const SerdNode* base, const SerdNode* root, const char* expected) { SerdNode* rel = serd_node_new_relative_uri(uri, base, root); const int ret = strcmp(serd_node_get_string(rel), expected); serd_node_free(rel); if (ret) { FAILF("Bad relative URI `%s' (expected `%s')\n", serd_node_get_string(rel), expected); } return ret; } int main(void) { #define MAX 1000000 #define NUM_TESTS 1000 for (int i = 0; i < NUM_TESTS; ++i) { double dbl = rand() % MAX; dbl += (rand() % MAX) / (double)MAX; if (test_strtod(dbl, 1 / (double)MAX)) { return 1; } } const double expt_test_nums[] = { 2.0E18, -5e19, +8e20, 2e+24, -5e-5, 8e0, 9e-0, 2e+0 }; const char* expt_test_strs[] = { "02e18", "-5e019", "+8e20", "2E+24", "-5E-5", "8E0", "9e-0", " 2e+0" }; for (unsigned i = 0; i < sizeof(expt_test_nums) / sizeof(double); ++i) { const double num = serd_strtod(expt_test_strs[i], NULL); const double delta = fabs(num - expt_test_nums[i]); if (delta > DBL_EPSILON) { FAILF("Parsed `%s' %lf != %lf (delta %lf)\n", expt_test_strs[i], num, expt_test_nums[i], delta); } } // Test serd_node_new_decimal const double dbl_test_nums[] = { 0.0, 9.0, 10.0, .01, 2.05, -16.00001, 5.000000005, 0.0000000001, NAN, INFINITY }; const char* dbl_test_strs[] = { "0.0", "9.0", "10.0", "0.01", "2.05", "-16.00001", "5.00000001", "0.0", NULL, NULL }; for (unsigned i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) { SerdNode* node = serd_node_new_decimal(dbl_test_nums[i], 8, NULL); const char* node_str = serd_node_get_string(node); const bool pass = (node_str && dbl_test_strs[i]) ? !strcmp(node_str, dbl_test_strs[i]) : (node_str == dbl_test_strs[i]); if (!pass) { FAILF("Serialised `%s' != %s\n", node_str, dbl_test_strs[i]); } const size_t len = node_str ? strlen(node_str) : 0; if (serd_node_get_length(node) != len) { FAILF("Length %zu != %zu\n", serd_node_get_length(node), len); } else if (dbl_test_strs[i] && strcmp(serd_node_get_string(serd_node_get_datatype(node)), NS_XSD "decimal")) { FAIL("Decimal node has incorrect default datatype\n"); } serd_node_free(node); } // Test serd_node_new_integer const long int_test_nums[] = { 0, -0, -23, 23, -12340, 1000, -1000 }; const char* int_test_strs[] = { "0", "0", "-23", "23", "-12340", "1000", "-1000" }; for (unsigned i = 0; i < sizeof(int_test_nums) / sizeof(double); ++i) { SerdNode* node = serd_node_new_integer(int_test_nums[i], NULL); const char* node_str = serd_node_get_string(node); if (strcmp(node_str, (const char*)int_test_strs[i])) { FAILF("Serialised `%s' != %s\n", node_str, int_test_strs[i]); } const size_t len = strlen(node_str); if (serd_node_get_length(node) != len) { FAILF("Length %zu,%zu != %zu\n", serd_node_get_length(node), len); } else if (strcmp(serd_node_get_string(serd_node_get_datatype(node)), NS_XSD "integer")) { FAIL("Integer node has incorrect default datatype\n"); } serd_node_free(node); } // Test serd_node_new_blob for (size_t size = 0; size < 256; ++size) { uint8_t* data = (uint8_t*)malloc(size); for (size_t i = 0; i < size; ++i) { data[i] = (uint8_t)(rand() % 256); } size_t out_size; SerdNode* blob = serd_node_new_blob(data, size, size % 5, NULL); const char* blob_str = serd_node_get_string(blob); uint8_t* out = (uint8_t*)serd_base64_decode( blob_str, serd_node_get_length(blob), &out_size); if (out_size != size) { FAILF("Blob size %zu != %zu\n", out_size, size); } for (size_t i = 0; i < size; ++i) { if (out[i] != data[i]) { FAILF("Corrupt blob at byte %zu\n", i); } } if (strcmp(serd_node_get_string(serd_node_get_datatype(blob)), NS_XSD "base64Binary")) { FAIL("Blob node has incorrect default datatype\n"); } serd_node_free(blob); free(out); free(data); } // Test serd_strlen const uint8_t str[] = { '"', '5', 0xE2, 0x82, 0xAC, '"', '\n', 0 }; SerdNodeFlags flags; size_t n_bytes = serd_strlen((const char*)str, &flags); if (n_bytes != 7 || flags != (SERD_HAS_QUOTE|SERD_HAS_NEWLINE)) { FAILF("Bad serd_strlen(%s) n_bytes=%zu flags=%u\n", str, n_bytes, flags); } if (serd_strlen((const char*)str, NULL) != 7) { FAILF("Bad serd_strlen(%s) n_bytes=%zu no flags\n", str, n_bytes); } // Test serd_strerror const char* msg = NULL; if (strcmp((msg = serd_strerror(SERD_SUCCESS)), "Success")) { FAILF("Bad message `%s' for SERD_SUCCESS\n", msg); } for (int i = SERD_FAILURE; i <= SERD_ERR_INTERNAL; ++i) { msg = serd_strerror((SerdStatus)i); if (!strcmp((const char*)msg, "Success")) { FAILF("Bad message `%s' for (SerdStatus)%d\n", msg, i); } } msg = serd_strerror((SerdStatus)-1); // Test file URI escaping and parsing if (check_file_uri(NULL, "C:/My 100%", true, "file:///C:/My%20100%%", NULL) || check_file_uri("ahost", "C:\\Pointless Space", true, "file://ahost/C:/Pointless%20Space", "C:/Pointless Space") || check_file_uri(NULL, "/foo/bar", true, "file:///foo/bar", NULL) || check_file_uri("bhost", "/foo/bar", true, "file://bhost/foo/bar", NULL) || check_file_uri(NULL, "a/relative path", false, "a/relative path", NULL) || check_file_uri(NULL, "a/relative ", true, "a/relative%20%3Cpath%3E", NULL)) { return 1; } // Test tolerance of parsing junk URI escapes char* out_path = serd_file_uri_parse("file:///foo/%0Xbar", NULL); if (strcmp(out_path, "/foo/bar")) { FAILF("bad tolerance of junk escape: `%s'\n", out_path); } free(out_path); // Test serd_node_equals const uint8_t replacement_char_str[] = { 0xEF, 0xBF, 0xBD, 0 }; SerdNode* lhs = serd_node_new_string((const char*)replacement_char_str); SerdNode* rhs = serd_node_new_string("123"); if (serd_node_equals(lhs, rhs)) { FAILF("%s == %s\n", serd_node_get_string(lhs), serd_node_get_string(rhs)); } SerdNode* qnode = serd_node_new_curie("foo:bar"); if (serd_node_equals(lhs, qnode)) { FAILF("%s == %s\n", serd_node_get_string(lhs), serd_node_get_string(qnode)); } serd_node_free(qnode); if (!serd_node_equals(lhs, lhs)) { FAILF("%s != %s\n", serd_node_get_string(lhs), serd_node_get_string(lhs)); } if (serd_node_copy(NULL)) { FAIL("made non-null copy of null node\n"); } serd_node_free(lhs); serd_node_free(rhs); // Test serd_node_new_string SerdNode* hello = serd_node_new_string("hello\""); if (serd_node_get_length(hello) != 6 || serd_node_get_flags(hello) != SERD_HAS_QUOTE || strcmp(serd_node_get_string(hello), "hello\"")) { FAILF("Bad node %s\n", serd_node_get_string(hello)); } if (serd_node_new_string(NULL)) { FAIL("Successfully created node from NULL string\n"); } // Test serd_node_new_literal if (serd_node_new_literal(NULL, NULL, NULL)) { FAIL("Successfully created node from NULL string\n"); } SerdNode* hello2 = serd_node_new_literal("hello\"", NULL, NULL); if (serd_node_get_length(hello2) != 6 || serd_node_get_flags(hello2) != SERD_HAS_QUOTE || strcmp(serd_node_get_string(hello2), "hello\"")) { FAILF("Bad node %s\n", serd_node_get_string(hello2)); } serd_node_free(hello2); SerdNode* hello_l = serd_node_new_literal("hello_l\"", NULL, "en"); if (serd_node_get_length(hello_l) != 8 || strcmp(serd_node_get_string(hello_l), "hello_l\"") || serd_node_get_flags(hello_l) != (SERD_HAS_QUOTE | SERD_HAS_LANGUAGE) || strcmp(serd_node_get_string(serd_node_get_language(hello_l)), "en")) { FAILF("Bad node %s\n", serd_node_get_string(hello_l)); } serd_node_free(hello_l); SerdNode* eg_Thing = serd_node_new_uri("http://example.org/Thing"); SerdNode* hello_dt = serd_node_new_literal("hello_dt\"", eg_Thing, NULL); if (serd_node_get_length(hello_dt) != 9 || strcmp(serd_node_get_string(hello_dt), "hello_dt\"") || serd_node_get_flags(hello_dt) != (SERD_HAS_QUOTE | SERD_HAS_DATATYPE) || strcmp(serd_node_get_string(serd_node_get_datatype(hello_dt)), "http://example.org/Thing")) { FAILF("Bad node %s\n", serd_node_get_string(hello_dt)); } serd_node_free(hello_dt); serd_node_free(eg_Thing); // Test absolute URI creation if (serd_node_new_uri(NULL)) { FAIL("Successfully created NULL URI\n"); } SerdNode* not_a_uri = serd_node_new_string("hello"); SerdNode* root = serd_node_new_uri("http://example.org/a/b/"); SerdNode* base = serd_node_new_uri("http://example.org/a/b/c/"); SerdNode* nil = serd_node_new_resolved_uri(NULL, base); SerdNode* nil2 = serd_node_new_resolved_uri("", base); if (serd_node_new_resolved_uri("", NULL)) { FAIL("Successfully created URI resolved against NULL\n"); } else if (serd_node_new_resolved_uri("", not_a_uri)) { FAIL("Successfully created URI resolved against non-URI\n"); } else if (serd_node_get_type(nil) != SERD_URI || strcmp(serd_node_get_string(nil), serd_node_get_string(base)) || serd_node_get_type(nil2) != SERD_URI || strcmp(serd_node_get_string(nil2), serd_node_get_string(base))) { FAILF("URI %s != base %s\n", serd_node_get_string(nil), serd_node_get_string(base)); } // Test relative URI creation if (check_rel_uri("http://example.org/a/b/c/foo", base, NULL, "foo") || check_rel_uri("http://example.org/a/", base, NULL, "../../") || check_rel_uri("http://example.org/a/", base, root, "http://example.org/a/") || check_rel_uri("http://example.org/", base, NULL, "http://example.org/") || check_rel_uri("http://drobilla.net/a", base, NULL, "http://drobilla.net/a")) { return 1; } // Test URI resolution if (serd_node_resolve(NULL, base)) { FAIL("Successfully resolved null URI\n"); } else if (serd_node_resolve(nil, NULL)) { FAIL("Successfully resolved against null URI\n"); } else if (serd_node_resolve(not_a_uri, base)) { FAIL("Successfully resolved a non-URI\n"); } else if (serd_node_resolve(nil, not_a_uri)) { FAIL("Successfully resolved against a non-URI\n"); } SerdNode* rel = serd_node_new_relative_uri( "http://example.org/a/b/c/foo", base, NULL); SerdNode* resolved = serd_node_resolve(rel, base); if (strcmp(serd_node_get_string(resolved), "http://example.org/a/b/c/foo")) { FAILF("Bad resolved URI %s (expected 'http://example.org/a/b/c/foo'\n", serd_node_get_string(resolved)); } serd_node_free(nil); serd_node_free(nil2); serd_node_free(not_a_uri); serd_node_free(resolved); serd_node_free(rel); serd_node_free(base); serd_node_free(root); // Test serd_node_new_blank if (serd_node_new_blank(NULL)) { FAIL("Successfully created blank node from NULL string\n"); } SerdNode* blank = serd_node_new_blank("b0"); if (serd_node_get_length(blank) != 2 || serd_node_get_flags(blank) != 0 || strcmp(serd_node_get_string(blank), "b0")) { FAILF("Bad blank node %s\n", serd_node_get_string(blank)); } serd_node_free(blank); // Test SerdEnv SerdWorld* world = serd_world_new(); SerdNode* u = serd_node_new_uri("http://example.org/foo"); SerdNode* b = serd_node_new_curie("invalid"); SerdNode* c = serd_node_new_curie("eg.2:b"); SerdEnv* env = serd_env_new(NULL); serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/"); if (serd_env_get_base_uri(env)) { FAIL("Unexpected initial base URI\n"); } if (!serd_env_set_base_uri(env, NULL)) { FAIL("Successfully set NULL base URI\n"); } if (!serd_env_set_base_uri(env, hello)) { FAILF("Set base URI to %s\n", serd_node_get_string(hello)); } if (serd_env_get_base_uri(env)) { FAIL("Unexpected base URI\n"); } SerdSlice prefix, suffix; if (!serd_env_expand(env, b, &prefix, &suffix)) { FAILF("Expanded invalid curie %s\n", serd_node_get_string(b)); } SerdNode* xnode = serd_env_expand_node(env, hello); if (xnode) { FAILF("Expanded %s\n", serd_node_get_string(c)); } serd_node_free(hello); SerdNode* xu = serd_env_expand_node(env, u); if (strcmp(serd_node_get_string(xu), "http://example.org/foo")) { FAILF("Expanded %s to %s\n", serd_node_get_string(c), serd_node_get_string(xu)); } serd_node_free(xu); SerdNode* badpre = serd_node_new_curie("hm:what"); SerdNode* xbadpre = serd_env_expand_node(env, badpre); if (xbadpre) { FAILF("Expanded invalid curie %s\n", serd_node_get_string(badpre)); } serd_node_free(badpre); SerdNode* xc = serd_env_expand_node(env, c); if (strcmp(serd_node_get_string(xc), "http://example.org/b")) { FAILF("Expanded %s to %s\n", serd_node_get_string(c), serd_node_get_string(xc)); } serd_node_free(xc); if (!serd_env_set_prefix(env, NULL, NULL)) { FAIL("Set NULL prefix\n"); } SerdNode* lit = serd_node_new_string("hello"); if (!serd_env_set_prefix(env, b, lit)) { FAIL("Set prefix to literal\n"); } int n_prefixes = 0; serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/"); serd_env_foreach(env, count_prefixes, &n_prefixes); if (n_prefixes != 1) { FAILF("Bad prefix count %d\n", n_prefixes); } SerdNode* shorter_uri = serd_node_new_uri("urn:foo"); const SerdNode* prefix_name; if (serd_env_qualify(env, shorter_uri, &prefix_name, &suffix)) { FAILF("Qualified %s\n", serd_node_get_string(shorter_uri)); } serd_node_free(shorter_uri); serd_node_free(u); serd_node_free(b); serd_node_free(c); // Test SerdReader and SerdWriter const char* path = "serd_test.ttl"; FILE* fd = fopen(path, "wb"); if (!fd) { FAILF("Failed to open file %s\n", path); } SerdWriter* writer = serd_writer_new( world, SERD_TURTLE, (SerdStyle)0, env, serd_file_sink, fd); if (!writer) { FAIL("Failed to create writer\n"); } serd_writer_chop_blank_prefix(writer, "tmp"); serd_writer_chop_blank_prefix(writer, NULL); const SerdSinkInterface* iface = serd_writer_get_sink_interface(writer); if (!iface->base(iface->handle, lit)) { FAILF("Set base URI to %s\n", serd_node_get_string(lit)); } else if (!iface->prefix(iface->handle, lit, lit)) { FAILF("Set prefix %s to %s\n", serd_node_get_string(lit), serd_node_get_string(lit)); } else if (!iface->end(iface->handle, NULL)) { FAIL("Ended non-existent anonymous node\n"); } else if (serd_writer_get_env(writer) != env) { FAIL("Writer has incorrect env\n"); } uint8_t buf[] = { 0xEF, 0xBF, 0xBD, 0 }; SerdNode* s = serd_node_new_uri(""); SerdNode* p = serd_node_new_uri("http://example.org/pred"); SerdNode* o = serd_node_new_string((char*)buf); // Write 3 invalid statements (should write nothing) const SerdNode* junk[][5] = { { s, p, NULL }, { s, NULL, o }, { NULL, p, o }, { s, p, NULL }, { s, NULL, o }, { NULL, p, o }, { s, o, o }, { o, p, o }, { s, p, NULL }, { NULL, NULL, NULL } }; for (unsigned i = 0; i < sizeof(junk) / (sizeof(SerdNode*) * 5); ++i) { if (!iface->statement( iface->handle, 0, NULL, junk[i][0], junk[i][1], junk[i][2])) { FAILF("Successfully wrote junk statement %d\n", i); } } SerdNode* urn_Type = serd_node_new_uri("urn:Type"); SerdNode* t = serd_node_new_literal((char*)buf, urn_Type, NULL); SerdNode* l = serd_node_new_literal((char*)buf, NULL, "en"); const SerdNode* good[][5] = { { s, p, o }, { s, p, o }, { s, p, t }, { s, p, l }, { s, p, l }, { s, p, t }, { s, p, l }, { s, p, o }, { s, p, o }, { s, p, o } }; for (unsigned i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 5); ++i) { if (iface->statement( iface->handle, 0, NULL, good[i][0], good[i][1], good[i][2])) { FAILF("Failed to write good statement %d\n", i); } } // Write statements with bad UTF-8 (should be replaced) const char bad_str[] = { (char)0xFF, (char)0x90, 'h', 'i', 0 }; SerdNode* bad_lit = serd_node_new_string(bad_str); SerdNode* bad_uri = serd_node_new_uri(bad_str); if (iface->statement(iface->handle, 0, NULL, s, p, bad_lit)) { FAIL("Failed to write junk UTF-8 literal\n"); } else if (iface->statement(iface->handle, 0, NULL, s, p, bad_uri)) { FAIL("Failed to write junk UTF-8 URI\n"); } serd_node_free(bad_uri); serd_node_free(bad_lit); // Write 1 valid statement serd_node_free(o); o = serd_node_new_string("hello"); if (iface->statement(iface->handle, 0, NULL, s, p, o)) { FAIL("Failed to write valid statement\n"); } serd_writer_free(writer); serd_node_free(lit); serd_node_free(s); serd_node_free(p); serd_node_free(o); serd_node_free(t); serd_node_free(l); serd_node_free(urn_Type); // Test buffer sink SerdBuffer buffer = { NULL, 0 }; writer = serd_writer_new( world, SERD_TURTLE, (SerdStyle)0, env, serd_buffer_sink, &buffer); o = serd_node_new_uri("http://example.org/base"); if (serd_writer_set_base_uri(writer, o)) { FAIL("Failed to write to chunk sink\n"); } serd_node_free(o); serd_writer_free(writer); char* out = serd_buffer_sink_finish(&buffer); if (strcmp((const char*)out, "@base .\n")) { FAILF("Incorrect buffer output:\n%s\n", buffer.buf); } free(out); // Rewind and test reader fseek(fd, 0, SEEK_SET); ReaderTest rt = { 0, NULL }; SerdSinkInterface sink = { &rt, NULL, NULL, test_sink, NULL }; SerdReader* reader = serd_reader_new(world, SERD_TURTLE, &sink); if (!reader) { FAIL("Failed to create reader\n"); } SerdNode* g = serd_node_new_uri("http://example.org/"); serd_reader_set_default_graph(reader, g); serd_reader_add_blank_prefix(reader, "tmp"); serd_reader_add_blank_prefix(reader, NULL); serd_node_free(g); if (!serd_reader_start_file(reader, "http://notafile", false)) { FAIL("Apparently can read an http URI\n"); } else if (!serd_reader_start_file(reader, "file://invalid", false)) { FAIL("Apparently can read an invalid file URI\n"); } else if (!serd_reader_start_file(reader, "file:///nonexistant", false)) { FAIL("Apparently can read a non-existent file\n"); } if (serd_reader_start_file(reader, path, true)) { FAIL("Failed to open test file\n"); } else if (serd_reader_read_document(reader)) { FAIL("Failed reading test document\n"); } else if (rt.n_statements != 13) { FAILF("Bad statement count %d\n", rt.n_statements); } else if (!rt.graph || !serd_node_get_string(rt.graph) || strcmp(serd_node_get_string(rt.graph), "http://example.org/")) { FAILF("Bad graph %p\n", rt.graph); } serd_reader_end_stream(reader); if (!serd_reader_read_string(reader, "This isn't Turtle at all.")) { FAIL("Parsed invalid string successfully.\n"); } serd_reader_free(reader); fclose(fd); serd_env_free(env); serd_world_free(world); printf("Success\n"); return 0; }