aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_node.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-07-17 17:31:53 -0400
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:07 -0500
commit08a3b8a18093d1623309f5f406865f51e7bff39a (patch)
tree4c1c0dab3680d3e1ed8cbdc1d698c10eb5a9ba49 /test/test_node.c
parent9c613b337712314c169d4add61212f4fc4102022 (diff)
downloadserd-08a3b8a18093d1623309f5f406865f51e7bff39a.tar.gz
serd-08a3b8a18093d1623309f5f406865f51e7bff39a.tar.bz2
serd-08a3b8a18093d1623309f5f406865f51e7bff39a.zip
Clean up base64 node construction and access API
Diffstat (limited to 'test/test_node.c')
-rw-r--r--test/test_node.c68
1 files changed, 49 insertions, 19 deletions
diff --git a/test/test_node.c b/test/test_node.c
index f0092553..ab0ceca8 100644
--- a/test/test_node.c
+++ b/test/test_node.c
@@ -5,9 +5,10 @@
#include "serd/memory.h"
#include "serd/node.h"
-#include "serd/string.h"
+#include "serd/status.h"
#include "serd/string_view.h"
#include "serd/uri.h"
+#include "serd/write_result.h"
#include <assert.h>
#include <math.h>
@@ -322,7 +323,7 @@ test_get_integer(void)
}
static void
-test_blob_to_node(void)
+test_base64(void)
{
assert(!serd_new_base64(&SERD_URI_NULL, 0, NULL));
@@ -333,14 +334,16 @@ test_blob_to_node(void)
data[i] = (uint8_t)((size + i) % 256);
}
- size_t out_size = 0;
- SerdNode* blob = serd_new_base64(data, size, NULL);
- const char* blob_str = serd_node_string(blob);
- uint8_t* out =
- (uint8_t*)serd_base64_decode(blob_str, serd_node_length(blob), &out_size);
+ SerdNode* blob = serd_new_base64(data, size, NULL);
+ const char* blob_str = serd_node_string(blob);
+ const size_t max_size = serd_get_base64_size(blob);
+ uint8_t* out = (uint8_t*)calloc(1, max_size);
+ const SerdWriteResult r = serd_get_base64(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));
- assert(out_size == size);
for (size_t i = 0; i < size; ++i) {
assert(out[i] == data[i]);
@@ -354,21 +357,47 @@ test_blob_to_node(void)
serd_free(out);
free(data);
}
+}
+
+static void
+check_get_base64(const char* string,
+ const char* datatype_uri,
+ const char* expected)
+{
+ SerdNode* const node =
+ serd_new_typed_literal(serd_string(string), serd_string(datatype_uri));
+
+ assert(node);
- // Test invalid base64 blob
+ const size_t max_size = serd_get_base64_size(node);
+ char* const decoded = (char*)calloc(1, max_size + 1);
- SerdNode* const blob = serd_new_typed_literal(
- serd_string("!nval!d$"), serd_string(NS_XSD "base64Binary"));
+ const SerdWriteResult r = serd_get_base64(node, max_size, decoded);
+ assert(!r.status);
+ assert(r.count <= max_size);
- const char* const blob_str = serd_node_string(blob);
- size_t out_size = 42;
- uint8_t* out =
- (uint8_t*)serd_base64_decode(blob_str, serd_node_length(blob), &out_size);
+ assert(!strcmp(decoded, expected));
+ assert(strlen(decoded) <= max_size);
- assert(!out);
- assert(out_size == 0);
+ free(decoded);
+ serd_node_free(node);
+}
+
+static void
+test_get_base64(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");
+
+ SerdNode* const node = serd_new_typed_literal(
+ serd_string("Zm9v"), serd_string(NS_XSD "base64Binary"));
- serd_node_free(blob);
+ char small[2] = {0};
+ const SerdWriteResult r = serd_get_base64(node, sizeof(small), small);
+
+ assert(r.status == SERD_OVERFLOW);
+ serd_node_free(node);
}
static void
@@ -514,7 +543,8 @@ main(void)
test_get_float();
test_integer();
test_get_integer();
- test_blob_to_node();
+ test_base64();
+ test_get_base64();
test_node_equals();
test_node_from_string();
test_node_from_substring();