summaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2022-01-14 15:08:02 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2022-01-14 15:08:02 +0000
commit8d76eb12d5223d0fc3ea65d8d97f1feda2fcc340 (patch)
tree0fd0dd005a08843d538923c1a31d968a417dc4bf /src/test
parentInitial commit. (diff)
downloadtinyframe-fb03707eb48eef5aa8cc502e356e5447452f2f0a.tar.xz
tinyframe-fb03707eb48eef5aa8cc502e356e5447452f2f0a.zip
Adding upstream version 0.1.1.upstream/0.1.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test')
-rw-r--r--src/test/Makefile.am29
-rw-r--r--src/test/test.fstrmbin0 -> 242 bytes
-rw-r--r--src/test/test1.c125
-rwxr-xr-xsrc/test/test1.sh8
-rw-r--r--src/test/test2.c141
-rwxr-xr-xsrc/test/test2.sh4
-rw-r--r--src/test/test3.c93
-rwxr-xr-xsrc/test/test3.sh3
8 files changed, 403 insertions, 0 deletions
diff --git a/src/test/Makefile.am b/src/test/Makefile.am
new file mode 100644
index 0000000..5e0e8a2
--- /dev/null
+++ b/src/test/Makefile.am
@@ -0,0 +1,29 @@
+MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
+
+CLEANFILES = test*.log test*.trs test2.fstrm *.gcda *.gcno *.gcov
+
+AM_CFLAGS = -I$(top_srcdir)/src
+
+check_PROGRAMS = test1 test2 test3
+TESTS = test1.sh test2.sh test3.sh
+EXTRA_DIST = $(TESTS)
+
+test1_SOURCES = test1.c
+test1_LDADD = ../libtinyframe.la
+test1_LDFLAGS = -static
+EXTRA_DIST += test.fstrm
+
+test2_SOURCES = test2.c
+test2_LDADD = ../libtinyframe.la
+test2_LDFLAGS = -static
+
+test3_SOURCES = test3.c
+test3_LDADD = ../libtinyframe.la
+test3_LDFLAGS = -static
+
+if ENABLE_GCOV
+gcov-local:
+ for src in $(test1_SOURCES) $(test2_SOURCES) $(test3_SOURCES); do \
+ gcov -l -r -s "$(srcdir)" "$$src"; \
+ done
+endif
diff --git a/src/test/test.fstrm b/src/test/test.fstrm
new file mode 100644
index 0000000..a3d164f
--- /dev/null
+++ b/src/test/test.fstrm
Binary files differ
diff --git a/src/test/test1.c b/src/test/test1.c
new file mode 100644
index 0000000..d6d18d1
--- /dev/null
+++ b/src/test/test1.c
@@ -0,0 +1,125 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2020, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the tinyframe library.
+ *
+ * tinyframe library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * tinyframe library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with tinyframe library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <tinyframe/tinyframe.h>
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+static void
+print_string(const void* data, size_t len)
+{
+ uint8_t* str = (uint8_t*)data;
+ putc('"', stdout);
+ while (len-- != 0) {
+ unsigned c = *(str++);
+ if (isprint(c)) {
+ if (c == '"')
+ puts("\\\"");
+ else
+ putc(c, stdout);
+ } else {
+ printf("\\x%02x", c);
+ }
+ }
+ putc('"', stdout);
+}
+
+int main(int argc, const char* argv[])
+{
+ if (argc < 3) {
+ return 1;
+ }
+
+ FILE* fp = fopen(argv[1], "r");
+ if (!fp) {
+ return 2;
+ }
+
+ int rbuf_len = atoi(argv[2]);
+
+ struct tinyframe_reader h = TINYFRAME_READER_INITIALIZER;
+
+ size_t s = 0, r;
+ uint8_t buf[4096], rbuf[rbuf_len];
+ while ((r = fread(rbuf, 1, sizeof(rbuf), fp)) > 0) {
+ printf("read %zu\n", r);
+
+ if (s + r > sizeof(buf)) {
+ printf("overflow\n");
+ break;
+ }
+ memcpy(&buf[s], rbuf, r);
+ s += r;
+
+ int r = 1;
+ while (r) {
+ switch (tinyframe_read(&h, buf, s)) {
+ case tinyframe_have_control:
+ printf("control type %" PRIu32 " len %" PRIu32 "\n", h.control.type, h.control.length);
+ break;
+ case tinyframe_have_control_field:
+ printf("control_field type %" PRIu32 " len %" PRIu32 " data: ", h.control_field.type, h.control_field.length);
+ print_string(h.control_field.data, h.control_field.length);
+ printf("\n");
+ break;
+ case tinyframe_have_frame:
+ printf("frame len %" PRIu32 " data: ", h.frame.length);
+ print_string(h.frame.data, h.frame.length);
+ printf("\n");
+ break;
+ case tinyframe_need_more:
+ printf("need more\n");
+ r = 0;
+ break;
+ case tinyframe_error:
+ printf("error\n");
+ fclose(fp);
+ return 2;
+ case tinyframe_stopped:
+ printf("stopped\n");
+ fclose(fp);
+ return 0;
+ case tinyframe_finished:
+ printf("finished\n");
+ fclose(fp);
+ return 0;
+ default:
+ printf("unexpected return code\n");
+ fclose(fp);
+ return 3;
+ }
+
+ if (r && h.bytes_read && h.bytes_read <= s) {
+ s -= h.bytes_read;
+ if (s) {
+ memmove(buf, &buf[h.bytes_read], s);
+ }
+ }
+ }
+ }
+
+ fclose(fp);
+ return 0;
+}
diff --git a/src/test/test1.sh b/src/test/test1.sh
new file mode 100755
index 0000000..df3d86c
--- /dev/null
+++ b/src/test/test1.sh
@@ -0,0 +1,8 @@
+#!/bin/sh -xe
+
+./test1 "$srcdir/test.fstrm" 7
+./test1 "$srcdir/test.fstrm" 15
+./test1 "$srcdir/test.fstrm" 24
+./test1 "$srcdir/test.fstrm" 44
+./test1 "$srcdir/test.fstrm" 79
+./test1 "$srcdir/test.fstrm" 134
diff --git a/src/test/test2.c b/src/test/test2.c
new file mode 100644
index 0000000..c25198b
--- /dev/null
+++ b/src/test/test2.c
@@ -0,0 +1,141 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2020, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the tinyframe library.
+ *
+ * tinyframe library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * tinyframe library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with tinyframe library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <tinyframe/tinyframe.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+static char content_type[] = "tinyframe.test";
+
+int main(int argc, const char* argv[])
+{
+ if (argc < 3) {
+ return 1;
+ }
+
+ if (argv[1][0] == 'w') {
+ FILE* fp = fopen(argv[2], "w");
+ if (!fp) {
+ return 1;
+ }
+
+ struct tinyframe_writer writer = TINYFRAME_WRITER_INITIALIZER;
+
+ uint8_t out[sizeof(content_type) + (TINYFRAME_CONTROL_FRAME_LENGTH_MAX * 3)];
+ size_t len = sizeof(out);
+ size_t wrote = 0;
+
+ if (tinyframe_write_control_start(&writer, &out[wrote], len, content_type, sizeof(content_type) - 1) != tinyframe_ok) {
+ return 1;
+ }
+ wrote += writer.bytes_wrote;
+ len -= writer.bytes_wrote;
+
+ if (tinyframe_write_frame(&writer, &out[wrote], len, (uint8_t*)content_type, sizeof(content_type)) != tinyframe_ok) {
+ return 1;
+ }
+ wrote += writer.bytes_wrote;
+ len -= writer.bytes_wrote;
+
+ if (tinyframe_write_control_stop(&writer, &out[wrote], len) != tinyframe_ok) {
+ return 1;
+ }
+ wrote += writer.bytes_wrote;
+ len -= writer.bytes_wrote;
+
+ if (fwrite(out, 1, wrote, fp) != wrote) {
+ return 1;
+ }
+
+ fclose(fp);
+ return 0;
+ } else if (argv[1][0] == 'r') {
+ FILE* fp = fopen(argv[2], "r");
+ if (!fp) {
+ return 1;
+ }
+
+ long content_length;
+
+ if (fseek(fp, 0, SEEK_END)) {
+ return 1;
+ }
+ content_length = ftell(fp);
+ if (fseek(fp, 0, SEEK_SET)) {
+ return 1;
+ }
+
+ uint8_t* content = malloc(content_length);
+ if (!content) {
+ return 1;
+ }
+
+ if (fread(content, 1, content_length, fp) != content_length) {
+ return 1;
+ }
+
+ fclose(fp);
+
+ struct tinyframe_reader reader = TINYFRAME_READER_INITIALIZER;
+ uint8_t* decoding = content;
+ size_t left = content_length;
+
+ if (tinyframe_read(&reader, decoding, left) != tinyframe_have_control
+ || reader.control.type != TINYFRAME_CONTROL_START) {
+ return 1;
+ }
+ decoding += reader.bytes_read;
+ left -= reader.bytes_read;
+
+ if (tinyframe_read(&reader, decoding, left) != tinyframe_have_control_field
+ || reader.control_field.type != TINYFRAME_CONTROL_FIELD_CONTENT_TYPE
+ || strncmp(content_type, (const char*)reader.control_field.data, reader.control_field.length)) {
+ return 1;
+ }
+ decoding += reader.bytes_read;
+ left -= reader.bytes_read;
+
+ while (1) {
+ switch (tinyframe_read(&reader, decoding, left)) {
+ case tinyframe_have_frame: {
+ if (strncmp(content_type, (char*)reader.frame.data, reader.frame.length)) {
+ return 1;
+ }
+ break;
+ }
+
+ case tinyframe_stopped:
+ case tinyframe_finished:
+ return 0;
+
+ default:
+ return 1;
+ }
+
+ decoding += reader.bytes_read;
+ left -= reader.bytes_read;
+ }
+ return 0;
+ }
+ return 1;
+}
diff --git a/src/test/test2.sh b/src/test/test2.sh
new file mode 100755
index 0000000..977ea1e
--- /dev/null
+++ b/src/test/test2.sh
@@ -0,0 +1,4 @@
+#!/bin/sh -xe
+
+./test2 w test2.fstrm
+./test2 r test2.fstrm
diff --git a/src/test/test3.c b/src/test/test3.c
new file mode 100644
index 0000000..bb2b135
--- /dev/null
+++ b/src/test/test3.c
@@ -0,0 +1,93 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2020, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the tinyframe library.
+ *
+ * tinyframe library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * tinyframe library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with tinyframe library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <tinyframe/tinyframe.h>
+
+#include <assert.h>
+#include <string.h>
+#ifdef HAVE_ENDIAN_H
+#include <endian.h>
+#else
+#ifdef HAVE_SYS_ENDIAN_H
+#include <sys/endian.h>
+#else
+#ifdef HAVE_MACHINE_ENDIAN_H
+#include <machine/endian.h>
+#endif
+#endif
+#endif
+
+static inline uint32_t _need32(const void* ptr)
+{
+ uint32_t v;
+ memcpy(&v, ptr, sizeof(v));
+ return be32toh(v);
+}
+
+int main(void)
+{
+ assert(tinyframe_frame_size(0) == 4);
+
+ struct tinyframe_writer writer = TINYFRAME_WRITER_INITIALIZER;
+ static struct tinyframe_control_field _content_type = {
+ TINYFRAME_CONTROL_FIELD_CONTENT_TYPE,
+ 4,
+ (uint8_t*)"test",
+ };
+ uint8_t out[64];
+
+ // correct control
+ assert(tinyframe_write_control(&writer, out, sizeof(out), TINYFRAME_CONTROL_READY, 0, 0) == tinyframe_ok);
+ assert(tinyframe_write_control(&writer, out, sizeof(out), TINYFRAME_CONTROL_READY, &_content_type, 1) == tinyframe_ok);
+
+ // wrong control field type
+ _content_type.type = 0;
+ assert(tinyframe_write_control(&writer, out, sizeof(out), TINYFRAME_CONTROL_READY, &_content_type, 1) == tinyframe_error);
+ _content_type.type = TINYFRAME_CONTROL_FIELD_CONTENT_TYPE;
+
+ // too large control field
+ _content_type.length = TINYFRAME_CONTROL_FIELD_CONTENT_TYPE_LENGTH_MAX + 1;
+ assert(tinyframe_write_control(&writer, out, sizeof(out), TINYFRAME_CONTROL_READY, &_content_type, 1) == tinyframe_error);
+ _content_type.length = 4;
+
+ // too small buffer
+ assert(tinyframe_write_control(&writer, out, 1, TINYFRAME_CONTROL_READY, &_content_type, 1) == tinyframe_need_more);
+
+ // too large control field
+ assert(tinyframe_write_control_start(&writer, out, sizeof(out), "test", TINYFRAME_CONTROL_FIELD_CONTENT_TYPE_LENGTH_MAX + 1) == tinyframe_error);
+
+ // too small buffer
+ assert(tinyframe_write_control_start(&writer, out, 1, "test", 4) == tinyframe_need_more);
+
+ // too small buffer
+ assert(tinyframe_write_frame(&writer, out, 1, out, 4) == tinyframe_need_more);
+
+ // too small buffer
+ assert(tinyframe_write_control_stop(&writer, out, 1) == tinyframe_need_more);
+
+ // correct
+ tinyframe_set_header(out, 111);
+ assert(_need32(out) == 111);
+
+ return 0;
+}
diff --git a/src/test/test3.sh b/src/test/test3.sh
new file mode 100755
index 0000000..26b0f6e
--- /dev/null
+++ b/src/test/test3.sh
@@ -0,0 +1,3 @@
+#!/bin/sh -xe
+
+./test3