From 5943cbb73ff5cb64e3fc90242024878c073d288d Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 13 Oct 2017 15:57:01 +0200 Subject: 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 --- configure.ac | 1 + tests/Makefile.am | 1 + tests/gtp/Makefile.am | 19 ++++++++ tests/gtp/gtpie_test.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/gtp/gtpie_test.ok | 6 +++ tests/testsuite.at | 6 +++ 6 files changed, 160 insertions(+) create mode 100644 tests/gtp/Makefile.am create mode 100644 tests/gtp/gtpie_test.c create mode 100644 tests/gtp/gtpie_test.ok 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 +#include +#include +#include +#include + +#include +#include +#include + +#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 -- cgit v1.2.3