aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-10-14 06:16:36 +0000
committerGerrit Code Review <gerrit@2a01:4f8:191:444b:0:0:2:12>2017-10-14 06:16:36 +0000
commitbe4baa6d97e259514ff32f9cd3bd325aa572cbde (patch)
tree566b7c173ea48bb31038dfa1c384e964df428b45
parentd36901325026b69de750e4df7aaa140c95de32f0 (diff)
parent5943cbb73ff5cb64e3fc90242024878c073d288d (diff)
Merge "Add unit tests for libgtp gtpie.[ch] functions"
-rw-r--r--configure.ac1
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/gtp/Makefile.am19
-rw-r--r--tests/gtp/gtpie_test.c127
-rw-r--r--tests/gtp/gtpie_test.ok6
-rw-r--r--tests/testsuite.at6
6 files changed, 160 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac
index 72cbe48..ca455ce 100644
--- a/configure.ac
+++ b/configure.ac
@@ -151,6 +151,7 @@ AC_CONFIG_FILES([Makefile
sgsnemu/Makefile
tests/Makefile
tests/lib/Makefile
+ tests/gtp/Makefile
libgtp.pc
osmo-ggsn.spec])
AC_OUTPUT
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ca4bdd8..8430a8f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,5 +1,6 @@
SUBDIRS = \
lib \
+ gtp \
$(NULL)
# The `:;' works around a Bash 3.2 bug when the output is not writeable.
diff --git a/tests/gtp/Makefile.am b/tests/gtp/Makefile.am
new file mode 100644
index 0000000..bb01e3d
--- /dev/null
+++ b/tests/gtp/Makefile.am
@@ -0,0 +1,19 @@
+AM_CFLAGS = -Wall -I$(top_srcdir)/include $(LIBOSMOCORE_CFLAGS) -g
+
+EXTRA_DIST = \
+ gtpie_test.ok \
+ $(NULL)
+
+noinst_PROGRAMS = \
+ gtpie_test \
+ $(NULL)
+
+gtpie_test_SOURCES = \
+ gtpie_test.c \
+ $(NULL)
+
+gtpie_test_LDADD = \
+ $(top_builddir)/lib/debug.o \
+ $(top_builddir)/gtp/libgtp.la \
+ $(LIBOSMOCORE_LIBS) \
+ $(NULL)
diff --git a/tests/gtp/gtpie_test.c b/tests/gtp/gtpie_test.c
new file mode 100644
index 0000000..fe2d502
--- /dev/null
+++ b/tests/gtp/gtpie_test.c
@@ -0,0 +1,127 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/application.h>
+#include <osmocom/core/logging.h>
+
+#include "../../lib/syserr.h"
+#include "../../gtp/gtpie.h"
+
+static const uint8_t in[] = { 1,2,3,4,5,6 };
+static uint8_t buf[256];
+static int rc;
+
+static void test_gtpie_tlv()
+{
+ unsigned int len = 0;
+
+ printf("Testing gtpie_tlv()\n");
+
+ /* normal / successful case */
+ memset(buf, 0, sizeof(buf));
+ rc = gtpie_tlv(buf, &len, sizeof(buf), 23, sizeof(in), in);
+ OSMO_ASSERT(rc == 0);
+ OSMO_ASSERT(len == sizeof(in) + 3);
+ OSMO_ASSERT(buf[0] == 23);
+ OSMO_ASSERT(ntohs(*(uint16_t *) &buf[1]) == sizeof(in));
+ OSMO_ASSERT(!memcmp(buf+3, in, sizeof(in)));
+
+ /* overflow */
+ memset(buf, 0, sizeof(buf));
+ rc = gtpie_tlv(buf, &len, 4, 23, sizeof(in), in);
+ OSMO_ASSERT(rc == 1);
+}
+
+static void test_gtpie_tv0()
+{
+ unsigned int len = 0;
+
+ printf("Testing gtpie_tv0()\n");
+
+ memset(buf, 0, sizeof(buf));
+ rc = gtpie_tv0(buf, &len, sizeof(buf), 42, sizeof(in), in);
+ OSMO_ASSERT(rc == 0);
+ OSMO_ASSERT(len == sizeof(in) + 1);
+}
+
+static void test_gtpie_tv1()
+{
+ unsigned int len = 0;
+
+ printf("Testing gtpie_tv1()\n");
+
+ memset(buf, 0, sizeof(buf));
+ rc = gtpie_tv1(buf, &len, sizeof(buf), 42, 0xAD);
+ OSMO_ASSERT(rc == 0);
+ OSMO_ASSERT(len == 2);
+ OSMO_ASSERT(buf[0] == 42);
+ OSMO_ASSERT(buf[1] == 0xAD);
+}
+
+static void test_gtpie_tv2()
+{
+ unsigned int len = 0;
+
+ printf("Testing gtpie_tv2()\n");
+
+ memset(buf, 0, sizeof(buf));
+ rc = gtpie_tv2(buf, &len, sizeof(buf), 42, 0xABCD);
+ OSMO_ASSERT(rc == 0);
+ OSMO_ASSERT(len == 3);
+ OSMO_ASSERT(buf[0] == 42);
+ OSMO_ASSERT(ntohs(*(uint16_t *) &buf[1]) == 0xABCD);
+}
+
+static void test_gtpie_tv4()
+{
+ unsigned int len = 0;
+
+ printf("Testing gtpie_tv4()\n");
+
+ memset(buf, 0, sizeof(buf));
+ rc = gtpie_tv4(buf, &len, sizeof(buf), 42, 0xABCD0123);
+ OSMO_ASSERT(rc == 0);
+ OSMO_ASSERT(len == 5);
+ OSMO_ASSERT(buf[0] == 42);
+ OSMO_ASSERT(ntohl(*(uint32_t *) &buf[1]) == 0xABCD0123);
+}
+
+static void test_gtpie_tv8()
+{
+ unsigned int len = 0;
+
+ printf("Testing gtpie_tv8()\n");
+
+ memset(buf, 0, sizeof(buf));
+ rc = gtpie_tv8(buf, &len, sizeof(buf), 42, 0x0001020304050607ULL);
+ OSMO_ASSERT(rc == 0);
+ OSMO_ASSERT(len == 9);
+ OSMO_ASSERT(buf[0] == 42);
+ OSMO_ASSERT(ntohl(*(uint32_t *) &buf[1]) == 0x00010203);
+ OSMO_ASSERT(ntohl(*(uint32_t *) &buf[5]) == 0x04050607);
+}
+
+int main(int argc, char **argv)
+{
+ osmo_init_logging(&log_info);
+ log_set_use_color(osmo_stderr_target, 0);
+ log_set_print_filename(osmo_stderr_target, 0);
+
+ srand(time(NULL));
+
+ test_gtpie_tlv();
+ test_gtpie_tv0();
+ test_gtpie_tv1();
+ test_gtpie_tv2();
+ test_gtpie_tv4();
+ test_gtpie_tv8();
+
+ /* TODO: gtpie_decaps() */
+ /* TODO: gtpie_encaps() */
+ /* TODO: gtpie_encaps2() */
+ /* TODO: gtpie_getie(), gtpie_exist(), gtpie_get*() */
+}
diff --git a/tests/gtp/gtpie_test.ok b/tests/gtp/gtpie_test.ok
new file mode 100644
index 0000000..465df72
--- /dev/null
+++ b/tests/gtp/gtpie_test.ok
@@ -0,0 +1,6 @@
+Testing gtpie_tlv()
+Testing gtpie_tv0()
+Testing gtpie_tv1()
+Testing gtpie_tv2()
+Testing gtpie_tv4()
+Testing gtpie_tv8()
diff --git a/tests/testsuite.at b/tests/testsuite.at
index cc0c949..f365f95 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -13,3 +13,9 @@ AT_KEYWORDS([in46a])
cat $abs_srcdir/lib/in46a_test.ok > expout
AT_CHECK([$abs_top_builddir/tests/lib/in46a_test], [], [expout], [])
AT_CLEANUP
+
+AT_SETUP([gtpie])
+AT_KEYWORDS([gtpie])
+cat $abs_srcdir/gtp/gtpie_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/gtp/gtpie_test], [], [expout], [])
+AT_CLEANUP