summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2017-07-28 22:01:04 +0200
committerJoel Klinghed <the_jk@yahoo.com>2017-07-28 22:01:30 +0200
commit0898066430e0f2908565a1b4588e50de2d41a256 (patch)
tree14cdd602923c0989856faaeaf33352c24c80f440 /test
parent6bdda0ebabcd8dc34edfc413de6d0424ccf1f6e6 (diff)
Break out Package read/write
Diffstat (limited to 'test')
-rw-r--r--test/.gitignore1
-rw-r--r--test/Makefile.am5
-rw-r--r--test/test-package.cc104
3 files changed, 109 insertions, 1 deletions
diff --git a/test/.gitignore b/test/.gitignore
index aa33417..8bf01f1 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -9,3 +9,4 @@
/test-xdg
/test-observers
/test-htmlattrtext
+/test-package
diff --git a/test/Makefile.am b/test/Makefile.am
index b5d2cee..5264665 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -3,7 +3,7 @@ MAINTAINERCLEANFILES = Makefile.in
AM_CXXFLAGS = @DEFINES@
TESTS = test-url test-http test-args test-xdg test-paths test-strings \
- test-observers test-htmlattrtext
+ test-observers test-htmlattrtext test-package
check_PROGRAMS = $(TESTS)
@@ -29,3 +29,6 @@ test_observers_SOURCES = test-observers.cc
test_htmlattrtext_SOURCES = test-htmlattrtext.cc
test_htmlattrtext_LDADD = $(top_builddir)/src/libattrstr.a
+
+test_package_SOURCES = test-package.cc
+test_package_LDADD = $(top_builddir)/src/libtp.a
diff --git a/test/test-package.cc b/test/test-package.cc
new file mode 100644
index 0000000..ce15c88
--- /dev/null
+++ b/test/test-package.cc
@@ -0,0 +1,104 @@
+// -*- mode: c++; c-basic-offset: 2; -*-
+
+#include "common.hh"
+#include "test.hh"
+
+#include "package.hh"
+
+namespace {
+
+void setup(Package* pkg) {
+ pkg->id = 42;
+ pkg->timestamp.tv_sec = 123;
+ pkg->timestamp.tv_nsec = 999999999;
+ pkg->source_port = 0;
+ pkg->source_host = "source";
+ pkg->target_port = 65535;
+ pkg->target_host = "target";
+}
+
+bool pkg_eq(Package const& p1, Package const& p2) {
+ return p1.id == p2.id
+ && p1.timestamp.tv_sec == p2.timestamp.tv_sec
+ && p1.timestamp.tv_nsec == p2.timestamp.tv_nsec
+ && p1.source_port == p2.source_port
+ && p1.source_host.compare(p2.source_host) == 0
+ && p1.target_port == p2.target_port
+ && p1.target_host.compare(p2.target_host) == 0;
+}
+
+bool test_sanity() {
+ Package pkg1;
+ Package pkg2;
+
+ setup(&pkg1);
+
+ uint8_t tmp[4096];
+ auto size = write_package(pkg1, tmp, sizeof(tmp));
+ ASSERT_TRUE(size < sizeof(tmp));
+
+ ASSERT_EQ(size, read_package(&pkg2, tmp, sizeof(tmp)));
+
+ ASSERT_TRUE(pkg_eq(pkg1, pkg2));
+
+ return true;
+}
+
+bool test_overflow() {
+ Package pkg1;
+ setup(&pkg1);
+
+ uint8_t tmp[4096];
+ auto size = write_package(pkg1, tmp, sizeof(tmp));
+ for (size_t i = 0; i < size; ++i) {
+ memset(tmp + size, 0, sizeof(tmp) - size);
+ auto s2 = write_package(pkg1, tmp + size, i);
+ ASSERT_EQ(size, s2);
+ ASSERT_EQ(0, tmp[size + i]);
+ }
+ auto s2 = write_package(pkg1, tmp + size, size);
+ ASSERT_EQ(size, s2);
+ ASSERT_EQ(0, memcmp(tmp, tmp + size, size));
+ return true;
+}
+
+bool test_null() {
+ Package pkg1;
+ setup(&pkg1);
+
+ uint8_t tmp[4096];
+ auto size = write_package(pkg1, tmp, sizeof(tmp));
+ auto s2 = write_package(pkg1, NULL, 0);
+ ASSERT_EQ(size, s2);
+ s2 = write_package(pkg1, NULL, sizeof(tmp));
+ ASSERT_EQ(size, s2);
+
+ return true;
+}
+
+bool test_empty() {
+ Package pkg1;
+ Package pkg2;
+ setup(&pkg1);
+ pkg1.source_host.clear();
+ pkg1.target_host.clear();
+
+ uint8_t tmp[4096];
+ auto size = write_package(pkg1, tmp, sizeof(tmp));
+ ASSERT_EQ(size, read_package(&pkg2, tmp, sizeof(tmp)));
+
+ ASSERT_TRUE(pkg_eq(pkg1, pkg2));
+
+ return true;
+}
+
+} // namespace
+
+int main(void) {
+ BEFORE;
+ RUN(test_sanity());
+ RUN(test_overflow());
+ RUN(test_null());
+ RUN(test_empty());
+ AFTER;
+}