diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test_node.c | 111 | ||||
-rw-r--r-- | test/test_node_syntax.c | 10 |
2 files changed, 99 insertions, 22 deletions
diff --git a/test/test_node.c b/test/test_node.c index 416d975b..d4ac6a2c 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -450,6 +450,43 @@ test_get_integer(void) } static void +test_hex(void) +{ + assert(!serd_new_hex(NULL, &SERD_URI_NULL, 0)); + + // Test valid hex blobs with a range of sizes + for (size_t size = 1; size < 256; ++size) { + uint8_t* const data = (uint8_t*)malloc(size); + for (size_t i = 0; i < size; ++i) { + data[i] = (uint8_t)((size + i) % 256); + } + + SerdNode* blob = serd_new_hex(NULL, data, size); + const char* blob_str = serd_node_string(blob); + const size_t max_size = serd_get_blob_size(blob); + uint8_t* out = (uint8_t*)calloc(1, max_size); + + const SerdWriteResult r = serd_get_blob(blob, max_size, out); + assert(r.status == SERD_SUCCESS); + assert(r.count == size); + assert(r.count <= max_size); + assert(serd_node_length(blob) == strlen(blob_str)); + + for (size_t i = 0; i < size; ++i) { + assert(out[i] == data[i]); + } + + const SerdNode* const datatype = serd_node_datatype(blob); + assert(datatype); + assert(!strcmp(serd_node_string(datatype), NS_XSD "hexBinary")); + + serd_node_free(NULL, blob); + free(out); + free(data); + } +} + +static void test_base64(void) { assert(!serd_new_base64(NULL, &SERD_URI_NULL, 0)); @@ -463,10 +500,10 @@ test_base64(void) SerdNode* blob = serd_new_base64(NULL, data, size); const char* blob_str = serd_node_string(blob); - const size_t max_size = serd_get_base64_size(blob); + const size_t max_size = serd_get_blob_size(blob); uint8_t* out = (uint8_t*)calloc(1, max_size); - const SerdWriteResult r = serd_get_base64(blob, max_size, out); + const SerdWriteResult r = serd_get_blob(blob, max_size, out); assert(r.status == SERD_SUCCESS); assert(r.count == size); assert(r.count <= max_size); @@ -487,19 +524,19 @@ test_base64(void) } static void -check_get_base64(const char* string, - const char* datatype_uri, - const char* expected) +check_get_blob(const char* string, + const char* datatype_uri, + const char* expected) { SerdNode* const node = serd_new_literal( NULL, SERD_STRING(string), SERD_HAS_DATATYPE, SERD_STRING(datatype_uri)); assert(node); - const size_t max_size = serd_get_base64_size(node); + const size_t max_size = serd_get_blob_size(node); char* const decoded = (char*)calloc(1, max_size + 1); - const SerdWriteResult r = serd_get_base64(node, max_size, decoded); + const SerdWriteResult r = serd_get_blob(node, max_size, decoded); assert(!r.status); assert(r.count <= max_size); @@ -511,22 +548,55 @@ check_get_base64(const char* string, } static void -test_get_base64(void) +test_get_blob(void) { - check_get_base64("Zm9vYmFy", NS_XSD "base64Binary", "foobar"); - check_get_base64("Zm9vYg==", NS_XSD "base64Binary", "foob"); - check_get_base64(" \f\n\r\t\vZm9v \f\n\r\t\v", NS_XSD "base64Binary", "foo"); + check_get_blob("666F6F626172", NS_XSD "hexBinary", "foobar"); + check_get_blob("666F6F62", NS_XSD "hexBinary", "foob"); - SerdNode* const node = serd_new_literal(NULL, - SERD_STRING("Zm9v"), - SERD_HAS_DATATYPE, - SERD_STRING(NS_XSD "base64Binary")); + check_get_blob("Zm9vYmFy", NS_XSD "base64Binary", "foobar"); + check_get_blob("Zm9vYg==", NS_XSD "base64Binary", "foob"); + check_get_blob(" \f\n\r\t\vZm9v \f\n\r\t\v", NS_XSD "base64Binary", "foo"); - char small[2] = {0}; - const SerdWriteResult r = serd_get_base64(node, sizeof(small), small); + char small[2] = {0}; - assert(r.status == SERD_OVERFLOW); - serd_node_free(NULL, node); + { + SerdNode* const node = serd_new_literal(NULL, + SERD_STRING("Zm9v"), + SERD_HAS_DATATYPE, + SERD_STRING(NS_XSD "base64Binary")); + + const SerdWriteResult r = serd_get_blob(node, sizeof(small), small); + + assert(r.status == SERD_OVERFLOW); + serd_node_free(NULL, node); + } + { + SerdNode* const string = + serd_new_token(NULL, SERD_LITERAL, SERD_STRING("string")); + + assert(serd_get_blob_size(string) == 0u); + + const SerdWriteResult r = serd_get_blob(string, sizeof(small), small); + + assert(r.status == SERD_BAD_ARG); + assert(r.count == 0u); + serd_node_free(NULL, string); + } + { + SerdNode* const unknown = + serd_new_literal(NULL, + SERD_STRING("secret"), + SERD_HAS_DATATYPE, + SERD_STRING("http://example.org/Datatype")); + + assert(serd_get_blob_size(unknown) == 0u); + + const SerdWriteResult r = serd_get_blob(unknown, sizeof(small), small); + + assert(r.status == SERD_BAD_ARG); + assert(r.count == 0u); + serd_node_free(NULL, unknown); + } } static void @@ -789,8 +859,9 @@ main(void) test_get_float(); test_integer(); test_get_integer(); + test_hex(); test_base64(); - test_get_base64(); + test_get_blob(); test_node_equals(); test_node_from_syntax(); test_node_from_substring(); diff --git a/test/test_node_syntax.c b/test/test_node_syntax.c index 47b67c9f..02787ebe 100644 --- a/test/test_node_syntax.c +++ b/test/test_node_syntax.c @@ -22,6 +22,7 @@ #include <assert.h> #include <stdbool.h> +#include <stdint.h> #include <string.h> static void @@ -87,7 +88,7 @@ check(SerdWorld* const world, static void test_common(SerdWorld* const world, const SerdSyntax syntax) { - static const int data[] = {4, 2}; + static const uint8_t data[] = {19u, 17u, 13u, 7u}; static const SerdStringView datatype = SERD_STRING("http://example.org/Datatype"); @@ -140,11 +141,16 @@ test_common(SerdWorld* const world, const SerdSyntax syntax) serd_nodes_value(nodes, serd_float(1.25f)), "\"1.25E0\"^^<http://www.w3.org/2001/XMLSchema#float>")); + assert(check(world, + syntax, + serd_nodes_hex(nodes, data, sizeof(data)), + "\"13110D07\"^^<http://www.w3.org/2001/XMLSchema#hexBinary>")); + assert( check(world, syntax, serd_nodes_base64(nodes, data, sizeof(data)), - "\"BAAAAAIAAAA=\"^^<http://www.w3.org/2001/XMLSchema#base64Binary>")); + "\"ExENBw==\"^^<http://www.w3.org/2001/XMLSchema#base64Binary>")); serd_nodes_free(nodes); } |