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

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Sat Oct 14 06:11:36 UTC 2017


Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/4265

to look at the new patch set (#2).

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, 160 insertions(+), 0 deletions(-)


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

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..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 @@
 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: newpatchset
Gerrit-Change-Id: I30a6dd8a01b7a074ef2d3936d186dfff6c79e6c0
Gerrit-PatchSet: 2
Gerrit-Project: osmo-ggsn
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list