[PATCH] osmo-ggsn[master]: Add unit tests for libgtp gtpie.[ch] functions

Harald Welte gerrit-no-reply at lists.osmocom.org
Sat Oct 14 05:59:35 UTC 2017


Review at  https://gerrit.osmocom.org/4265

Add unit tests for libgtp gtpie.[ch] functions

This doesn't yet cover all the functions in gtpie.[ch], but testing half
of them is better than not testing any of them, so let's merge this
current state with a couple of TDOO's on what we still need to test.

Change-Id: I30a6dd8a01b7a074ef2d3936d186dfff6c79e6c0
---
M configure.ac
M tests/Makefile.am
A tests/gtp/Makefile.am
A tests/gtp/gtpie_test.c
A tests/gtp/gtpie_test.ok
M tests/testsuite.at
6 files changed, 159 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-ggsn refs/changes/65/4265/1

diff --git a/configure.ac b/configure.ac
index 72cbe48..ca455ce 100644
--- a/configure.ac
+++ b/configure.ac
@@ -151,6 +151,7 @@
                  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..160af49
--- /dev/null
+++ b/tests/gtp/Makefile.am
@@ -0,0 +1,18 @@
+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 @@
 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

-- 
To view, visit https://gerrit.osmocom.org/4265
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I30a6dd8a01b7a074ef2d3936d186dfff6c79e6c0
Gerrit-PatchSet: 1
Gerrit-Project: osmo-ggsn
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>


More information about the gerrit-log mailing list