aboutsummaryrefslogtreecommitdiffstats
path: root/tests/serd_test.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-12-08 23:21:23 +0100
committerDavid Robillard <d@drobilla.net>2019-12-08 23:21:23 +0100
commit2efb107eb4d4381198de51bd4f092ae1ead02e31 (patch)
treeee89c919e80d041d6eae2b08abfd247c2fc67be2 /tests/serd_test.c
parent41a5903cc5350b9e90ebfbeea66a3a1eabe7f663 (diff)
downloadserd-2efb107eb4d4381198de51bd4f092ae1ead02e31.tar.gz
serd-2efb107eb4d4381198de51bd4f092ae1ead02e31.tar.bz2
serd-2efb107eb4d4381198de51bd4f092ae1ead02e31.zip
Fix reading from a null-delimited socket
Diffstat (limited to 'tests/serd_test.c')
-rw-r--r--tests/serd_test.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/serd_test.c b/tests/serd_test.c
index bb05a586..0a89247b 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -111,6 +111,64 @@ test_file_uri(const char* hostname,
serd_node_free(&node);
}
+static void
+test_read_chunks()
+{
+ ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest));
+ FILE* const f = tmpfile();
+ static const char null = 0;
+ SerdReader* const reader =
+ serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL);
+
+ assert(reader);
+ assert(serd_reader_get_handle(reader) == rt);
+ assert(f);
+
+ SerdStatus st = serd_reader_start_stream(reader, f, NULL, false);
+ assert(st == SERD_SUCCESS);
+
+ // Write two statement separated by null characters
+ fprintf(f, "@prefix eg: <http://example.org/> .\n");
+ fprintf(f, "eg:s eg:p eg:o1 .\n");
+ fwrite(&null, sizeof(null), 1, f);
+ fprintf(f, "eg:s eg:p eg:o2 .\n");
+ fwrite(&null, sizeof(null), 1, f);
+ fseek(f, 0, SEEK_SET);
+
+ // Read prefix
+ st = serd_reader_read_chunk(reader);
+ assert(st == SERD_SUCCESS);
+ assert(rt->n_statements == 0);
+
+ // Read first statement
+ st = serd_reader_read_chunk(reader);
+ assert(st == SERD_SUCCESS);
+ assert(rt->n_statements == 1);
+
+ // Read terminator
+ st = serd_reader_read_chunk(reader);
+ assert(st == SERD_SUCCESS); // FIXME: return SERD_FAILURE?
+ assert(rt->n_statements == 1);
+
+ // Read second statement (after null terminator)
+ st = serd_reader_read_chunk(reader);
+ assert(st == SERD_SUCCESS);
+ assert(rt->n_statements == 2);
+
+ // Read terminator
+ st = serd_reader_read_chunk(reader);
+ assert(st == SERD_SUCCESS); // FIXME: return SERD_FAILURE?
+ assert(rt->n_statements == 2);
+
+ // EOF
+ st = serd_reader_read_chunk(reader);
+ assert(st == SERD_SUCCESS); // FIXME: return SERD_FAILURE?
+ assert(rt->n_statements == 2);
+
+ serd_reader_free(reader);
+ fclose(f);
+}
+
int
main(void)
{
@@ -490,6 +548,9 @@ main(void)
assert(!strcmp((const char*)out, "@base <http://example.org/base> .\n"));
serd_free(out);
+ // Test reading a series of chunks (like from a socket)
+ test_read_chunks();
+
// Rewind and test reader
fseek(fd, 0, SEEK_SET);