aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-08-15 20:11:19 +0200
committerDavid Robillard <d@drobilla.net>2022-01-13 23:03:37 -0500
commita10fddf0f697e78325ddcfbc71af8f154ffd2a82 (patch)
tree75c2dd20aef99bff82dc81c93ab29b81313a9c8e /test
parent0839a0fda214250c41c07c799c700f4432e54963 (diff)
downloadserd-a10fddf0f697e78325ddcfbc71af8f154ffd2a82.tar.gz
serd-a10fddf0f697e78325ddcfbc71af8f154ffd2a82.tar.bz2
serd-a10fddf0f697e78325ddcfbc71af8f154ffd2a82.zip
Improve reader error handling
Diffstat (limited to 'test')
-rw-r--r--test/meson.build1
-rw-r--r--test/test_overflow.c163
2 files changed, 164 insertions, 0 deletions
diff --git a/test/meson.build b/test/meson.build
index 3ec9d38b..a57e2cf2 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -8,6 +8,7 @@ unit_tests = [
'env',
'free_null',
'node',
+ 'overflow',
'read_chunk',
'reader_writer',
'sink',
diff --git a/test/test_overflow.c b/test/test_overflow.c
new file mode 100644
index 00000000..13516388
--- /dev/null
+++ b/test/test_overflow.c
@@ -0,0 +1,163 @@
+/*
+ Copyright 2018 David Robillard <d@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 <stdio.h>
+
+static const size_t min_stack_size = 4 * sizeof(size_t) + 256u;
+static const size_t max_stack_size = 1024u;
+
+static SerdStatus
+test_size(const char* const str,
+ const SerdSyntax syntax,
+ const size_t stack_size)
+{
+ SerdSink* sink = serd_sink_new(NULL, NULL);
+ SerdReader* const reader = serd_reader_new(syntax, sink, stack_size);
+ assert(reader);
+
+ serd_reader_start_string(reader, str);
+ const SerdStatus st = serd_reader_read_document(reader);
+ serd_reader_free(reader);
+ serd_sink_free(sink);
+
+ return st;
+}
+
+static void
+test_all_sizes(const char* const str, const SerdSyntax syntax)
+{
+ // Ensure reading with the maximum stack size succeeds
+ SerdStatus st = test_size(str, syntax, max_stack_size);
+ assert(!st);
+
+ // Test with an increasingly smaller stack
+ for (size_t size = max_stack_size; size > min_stack_size; --size) {
+ if ((st = test_size(str, syntax, size))) {
+ assert(st == SERD_ERR_OVERFLOW);
+ }
+ }
+
+ assert(st == SERD_ERR_OVERFLOW);
+}
+
+static void
+test_ntriples_overflow(void)
+{
+ static const char* const test_strings[] = {
+ "<http://example.org/s> <http://example.org/p> <http://example.org/o> .",
+ NULL,
+ };
+
+ for (const char* const* t = test_strings; *t; ++t) {
+ test_all_sizes(*t, SERD_NTRIPLES);
+ }
+}
+
+static void
+test_turtle_overflow(void)
+{
+ static const char* const test_strings[] = {
+ "<http://example.org/s> <http://example.org/p> <http://example.org/> .",
+ "<http://example.org/s> <http://example.org/p> "
+ "<thisisanabsurdlylongurischeme://because/testing/> .",
+ "<http://example.org/s> <http://example.org/p> 1234 .",
+ "<http://example.org/s> <http://example.org/p> (1 2 3 4) .",
+ "<http://example.org/s> <http://example.org/p> ((((((((42)))))))) .",
+ "<http://example.org/s> <http://example.org/p> \"literal\" .",
+ "<http://example.org/s> <http://example.org/p> _:blank .",
+ "<http://example.org/s> <http://example.org/p> true .",
+ "<http://example.org/s> <http://example.org/p> \"\"@en .",
+ "(((((((((42))))))))) <http://example.org/p> <http://example.org/o> .",
+ "@prefix eg: <http://example.org/ns/test> .",
+ "@base <http://example.org/base> .",
+
+ "@prefix eg: <http://example.org/> . \neg:s eg:p eg:o .\n",
+
+ "@prefix ug.dot: <http://example.org/> . \nug.dot:s ug.dot:p ug.dot:o .\n",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "@prefix øøøøøøøøø: <http://example.org/long> . \n"
+ "<http://example.org/somewhatlongsubjecttooffsetthepredicate> øøøøøøøøø:p "
+ "øøøøøøøøø:o .\n",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "<http://example.org/subject/with/a/long/path> "
+ "<http://example.org/predicate/with/a/long/path> "
+ "<http://example.org/object/with/a/long/path> .",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "<http://example.org/s> <http://example.org/p> "
+ "\"typed\"^^<http://example.org/Datatype> .",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "@prefix eg: <http://example.org/ns/test> .\n"
+ "<http://example.org/s> <http://example.org/p> "
+ "\"typed\"^^eg:Datatype .",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "@prefix eg: <http://example.org/ns/test> .\n"
+ "<http://example.org/s> <http://example.org/p> eg:foo .",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "@prefix prefix: <http://example.org/testing/curies> .\n"
+ "prefix:subject prefix:predicate prefix:object .\n",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "@prefix prefix: <http://example.org/testing/curies> .\n"
+ "prefix:subjectthatwillcomearoundtobeingfinishedanycharacternow "
+ "prefix:predicate prefix:object .\n",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "@prefix eg: <http://example.org/> .\n"
+ "eg:s eg:p [ eg:p [ eg:p [ eg:p [ eg:p eg:o ] ] ] ] .\n",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "@prefix eg: <http://example.org/> .\n"
+ "eg:s eg:p ( 1 2 3 ( 4 5 6 ( 7 8 9 ) ) ) .\n",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "@prefix eg: <http://example.org/ns/test> .\n"
+ "<http://example.org/s> <http://example.org/p> eg:%99 .",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "@prefix øøøøøøøøø: <http://example.org/long> .\n"
+ "<http://example.org/somewhatlongsubjecttooffsetthepredicate> øøøøøøøøø:p "
+ "øøøøøøøøø:o .\n",
+
+ // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
+ "@base <http://example.org/ns/test> .\n"
+ "<http://example.org/s> <http://example.org/p> <rel> .",
+
+ NULL,
+ };
+
+ for (const char* const* t = test_strings; *t; ++t) {
+ test_all_sizes(*t, SERD_TURTLE);
+ }
+}
+
+int
+main(void)
+{
+ test_ntriples_overflow();
+ test_turtle_overflow();
+ return 0;
+}