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
This commit is contained in:
Harald Welte 2017-10-13 15:57:01 +02:00
parent f6c5f9524f
commit 5943cbb73f
6 changed files with 160 additions and 0 deletions

View File

@ -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

View File

@ -1,5 +1,6 @@
SUBDIRS = \
lib \
gtp \
$(NULL)
# The `:;' works around a Bash 3.2 bug when the output is not writeable.

19
tests/gtp/Makefile.am Normal file
View File

@ -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)

127
tests/gtp/gtpie_test.c Normal file
View File

@ -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*() */
}

6
tests/gtp/gtpie_test.ok Normal file
View File

@ -0,0 +1,6 @@
Testing gtpie_tlv()
Testing gtpie_tv0()
Testing gtpie_tv1()
Testing gtpie_tv2()
Testing gtpie_tv4()
Testing gtpie_tv8()

View File

@ -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