[PATCH] libosmocore[master]: oap: add encode/decode unit test

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/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Thu Dec 8 16:54:13 UTC 2016


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

oap: add encode/decode unit test

Change-Id: I0e14099e2fc18e333a73d38bda059d53a8ca9944
---
M tests/Makefile.am
A tests/oap/Makefile.am
A tests/oap/oap_test.c
A tests/oap/oap_test.ok
M tests/testsuite.at
5 files changed, 253 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/79/1379/1

diff --git a/tests/Makefile.am b/tests/Makefile.am
index f5d095d..cb037bd 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -13,7 +13,7 @@
 		 vty/vty_test comp128/comp128_test utils/utils_test	\
 		 smscb/gsm0341_test stats/stats_test			\
 		 bitvec/bitvec_test msgb/msgb_test bits/bitcomp_test	\
-		 tlv/tlv_test gsup/gsup_test fsm/fsm_test
+		 tlv/tlv_test gsup/gsup_test fsm/fsm_test oap/oap_test
 
 if ENABLE_MSGFILE
 check_PROGRAMS += msgfile/msgfile_test
@@ -133,6 +133,10 @@
 fsm_fsm_test_SOURCES = fsm/fsm_test.c
 fsm_fsm_test_LDADD = $(top_builddir)/src/libosmocore.la
 
+oap_oap_test_SOURCES = oap/oap_test.c
+oap_oap_test_LDADD = $(top_builddir)/src/libosmocore.la \
+		     $(top_builddir)/src/gsm/libosmogsm.la
+
 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 	:;{ \
@@ -168,7 +172,7 @@
 	     utils/utils_test.ok stats/stats_test.ok			\
 	     bitvec/bitvec_test.ok msgb/msgb_test.ok bits/bitcomp_test.ok \
 	     sim/sim_test.ok tlv/tlv_test.ok gsup/gsup_test.ok		\
-	     fsm/fsm_test.ok fsm/fsm_test.err
+	     fsm/fsm_test.ok fsm/fsm_test.err oap/oap_test.ok
 
 DISTCLEANFILES = atconfig atlocal
 
diff --git a/tests/oap/Makefile.am b/tests/oap/Makefile.am
new file mode 100644
index 0000000..06ccf33
--- /dev/null
+++ b/tests/oap/Makefile.am
@@ -0,0 +1,37 @@
+AM_CPPFLAGS = \
+	$(all_includes) \
+	-I$(top_srcdir)/include \
+	$(NULL)
+
+AM_CFLAGS = \
+	-Wall \
+	-ggdb3 \
+	$(LIBOSMOCORE_CFLAGS) \
+	$(LIBOSMOGSM_CFLAGS) \
+	$(NULL)
+
+EXTRA_DIST = \
+	oap_test.ok \
+	$(NULL)
+
+if HAVE_LIBGTP
+if HAVE_LIBCARES
+noinst_PROGRAMS = \
+	oap_test \
+	$(NULL)
+endif
+endif
+
+oap_test_SOURCES = \
+	oap_test.c \
+	$(NULL)
+
+oap_test_LDADD = \
+	$(top_builddir)/src/gprs/oap.o \
+	$(top_builddir)/src/gprs/oap_messages.o \
+	$(top_builddir)/src/gprs/gprs_utils.o \
+	$(top_builddir)/src/libcommon/libcommon.a \
+	$(LIBOSMOCORE_LIBS) \
+	$(LIBOSMOGSM_LIBS) \
+	-lrt
+
diff --git a/tests/oap/oap_test.c b/tests/oap/oap_test.c
new file mode 100644
index 0000000..c8fe494
--- /dev/null
+++ b/tests/oap/oap_test.c
@@ -0,0 +1,181 @@
+/* Test Osmocom Authentication Protocol */
+/*
+ * (C) 2015 by sysmocom s.f.m.c. GmbH
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/core/logging.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/gsm/oap.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+
+static struct msgb *oap_encoded(const struct osmo_oap_message *oap_msg)
+{
+	struct msgb *msgb = msgb_alloc_headroom(1000, 64, __func__);
+	OSMO_ASSERT(msgb);
+	osmo_oap_encode(msgb, oap_msg);
+	return msgb;
+}
+
+static bool encode_decode_makes_same_msg(struct osmo_oap_message *oap_msg)
+{
+	struct osmo_oap_message oap_msg_decoded;
+	struct msgb *msgb;
+	int rc;
+	bool result;
+	memset(&oap_msg_decoded, 0, sizeof(oap_msg_decoded));
+
+	msgb = oap_encoded(oap_msg);
+	rc = osmo_oap_decode(&oap_msg_decoded, msgb_l2(msgb), msgb_l2len(msgb));
+
+	if (rc) {
+		printf("osmo_oap_decode() returned error: %d\n", rc);
+		result = false;
+		goto free_msgb;
+	}
+
+	rc = memcmp(oap_msg, &oap_msg_decoded, sizeof(oap_msg_decoded));
+	if (rc) {
+		printf("decoded message mismatches encoded message\n");
+		printf("original:\n%s\n",
+		       osmo_hexdump((void*)oap_msg, sizeof(*oap_msg)));
+		printf("en- and decoded:\n%s\n",
+		       osmo_hexdump((void*)&oap_msg_decoded, sizeof(oap_msg_decoded)));
+		result = false;
+		goto free_msgb;
+	}
+
+	printf("ok\n");
+	result = true;
+
+free_msgb:
+	talloc_free(msgb);
+	return result;
+}
+
+static void test_oap_messages_dec_enc(void)
+{
+	printf("Testing OAP messages\n");
+
+	struct osmo_oap_message oap_msg;
+
+#define CLEAR() memset(&oap_msg, 0, sizeof(oap_msg))
+#define CHECK() OSMO_ASSERT(encode_decode_makes_same_msg(&oap_msg))
+
+	printf("- Register Request\n");
+	CLEAR();
+	oap_msg.message_type = OAP_MSGT_REGISTER_REQUEST;
+	oap_msg.client_id = 0x2342;
+	CHECK();
+
+	printf("- Register Error\n");
+	CLEAR();
+	oap_msg.message_type = OAP_MSGT_REGISTER_ERROR;
+	oap_msg.cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
+	CHECK();
+
+	printf("- Register Result\n");
+	CLEAR();
+	oap_msg.message_type = OAP_MSGT_REGISTER_RESULT;
+	CHECK();
+
+	printf("- Challenge Request, no rand, no autn\n");
+	CLEAR();
+	oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
+	oap_msg.rand_present = 0;
+	oap_msg.autn_present = 0;
+	CHECK();
+
+	printf("- Challenge Request, with rand, no autn\n");
+	CLEAR();
+	oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
+	osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
+		      oap_msg.rand, 16);
+	oap_msg.rand_present = 1;
+	oap_msg.autn_present = 0;
+	CHECK();
+
+	printf("- Challenge Request, no rand, with autn\n");
+	CLEAR();
+	oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
+	oap_msg.rand_present = 0;
+	osmo_hexparse("cec4e3848a33000086781158ca40f136",
+		      oap_msg.autn, 16);
+	oap_msg.autn_present = 1;
+	CHECK();
+
+	printf("- Challenge Request, with rand, with autn\n");
+	CLEAR();
+	oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
+	osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
+		      oap_msg.rand, 16);
+	oap_msg.rand_present = 1;
+	osmo_hexparse("cec4e3848a33000086781158ca40f136",
+		      oap_msg.autn, 16);
+	oap_msg.autn_present = 1;
+	CHECK();
+
+	printf("- Challenge Error\n");
+	CLEAR();
+	oap_msg.message_type = OAP_MSGT_CHALLENGE_ERROR;
+	oap_msg.cause = GMM_CAUSE_GSM_AUTH_UNACCEPT;
+	CHECK();
+
+	printf("- Challenge Result\n");
+	CLEAR();
+	oap_msg.message_type = OAP_MSGT_CHALLENGE_RESULT;
+	osmo_hexparse("0102030405060708",
+		      oap_msg.xres, 8);
+	oap_msg.xres_present = 1;
+	CHECK();
+
+	printf("- Sync Request\n");
+	CLEAR();
+	oap_msg.message_type = OAP_MSGT_SYNC_REQUEST;
+	osmo_hexparse("102030405060708090a0b0c0d0e0f001",
+		      oap_msg.auts, 16);
+	oap_msg.auts_present = 1;
+	CHECK();
+
+	/* Sync Error and Sync Result are not used in OAP */
+}
+
+const struct log_info_cat default_categories[] = {
+	[DLOAP] = {
+		.name = "DLOAP",
+		.description = "Osmocom Authentication Protocol",
+		.enabled = 0, .loglevel = LOGL_DEBUG,
+	},
+};
+
+static struct log_info info = {
+	.cat = default_categories,
+	.num_cat = ARRAY_SIZE(default_categories),
+};
+
+int main(int argc, char **argv)
+{
+	osmo_init_logging(&info);
+
+	test_oap_messages_dec_enc();
+
+	printf("Done.\n");
+	return EXIT_SUCCESS;
+}
diff --git a/tests/oap/oap_test.ok b/tests/oap/oap_test.ok
new file mode 100644
index 0000000..4e77c3c
--- /dev/null
+++ b/tests/oap/oap_test.ok
@@ -0,0 +1,22 @@
+Testing OAP messages
+- Register Request
+ok
+- Register Error
+ok
+- Register Result
+ok
+- Challenge Request, no rand, no autn
+ok
+- Challenge Request, with rand, no autn
+ok
+- Challenge Request, no rand, with autn
+ok
+- Challenge Request, with rand, with autn
+ok
+- Challenge Error
+ok
+- Challenge Result
+ok
+- Sync Request
+ok
+Done.
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 77038bc..57da5d0 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -215,3 +215,10 @@
 cat $abs_srcdir/fsm/fsm_test.err > experr
 AT_CHECK([$abs_top_builddir/tests/fsm/fsm_test], [0], [expout], [experr])
 AT_CLEANUP
+
+AT_SETUP([oap])
+AT_KEYWORDS([oap])
+cat $abs_srcdir/oap/oap_test.ok > expout
+touch experr
+AT_CHECK([$abs_top_builddir/tests/oap/oap_test], [0], [expout], [experr])
+AT_CLEANUP

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0e14099e2fc18e333a73d38bda059d53a8ca9944
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list