From d981efa97603594080d90956eb9e08bce8f4c371 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Thu, 8 Dec 2016 17:50:03 +0100 Subject: oap: add encode/decode unit test Change-Id: I0e14099e2fc18e333a73d38bda059d53a8ca9944 --- tests/Makefile.am | 8 ++- tests/oap/Makefile.am | 37 +++++++++++ tests/oap/oap_test.c | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/oap/oap_test.ok | 42 ++++++++++++ tests/testsuite.at | 7 ++ 5 files changed, 273 insertions(+), 2 deletions(-) create mode 100644 tests/oap/Makefile.am create mode 100644 tests/oap/oap_test.c create mode 100644 tests/oap/oap_test.ok diff --git a/tests/Makefile.am b/tests/Makefile.am index 1aad2e9c..b9eb8f23 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -13,7 +13,7 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \ 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 oap/oap_test fsm/fsm_test \ write_queue/wqueue_test if ENABLE_MSGFILE @@ -131,6 +131,9 @@ tlv_tlv_test_LDADD = $(top_builddir)/src/gsm/libosmogsm.la $(top_builddir)/src/l gsup_gsup_test_SOURCES = gsup/gsup_test.c gsup_gsup_test_LDADD = $(top_builddir)/src/gsm/libosmogsm.la $(top_builddir)/src/libosmocore.la +oap_oap_test_SOURCES = oap/oap_test.c +oap_oap_test_LDADD = $(top_builddir)/src/gsm/libosmogsm.la $(top_builddir)/src/libosmocore.la + fsm_fsm_test_SOURCES = fsm/fsm_test.c fsm_fsm_test_LDADD = $(top_builddir)/src/libosmocore.la @@ -172,7 +175,8 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \ 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 write_queue/wqueue_test.ok + oap/oap_test.ok fsm/fsm_test.ok fsm/fsm_test.err \ + write_queue/wqueue_test.ok DISTCLEANFILES = atconfig atlocal diff --git a/tests/oap/Makefile.am b/tests/oap/Makefile.am new file mode 100644 index 00000000..06ccf338 --- /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 00000000..f7fe0b78 --- /dev/null +++ b/tests/oap/oap_test.c @@ -0,0 +1,181 @@ +/* Test Osmocom Authentication Protocol */ +/* + * (C) 2016 by sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Neels Hofmeyr + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include +#include +#include + +#include +#include +#include + +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); + printf("encoded message:\n%s\n", + osmo_hexdump((void*)msgb_l2(msgb), msgb_l2len(msgb))); + 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[] = { +}; + +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 00000000..9260d442 --- /dev/null +++ b/tests/oap/oap_test.ok @@ -0,0 +1,42 @@ +Testing OAP messages +- Register Request +encoded message: +04 30 02 23 42 +ok +- Register Error +encoded message: +05 02 01 6f +ok +- Register Result +encoded message: +06 +ok +- Challenge Request, no rand, no autn +encoded message: +08 +ok +- Challenge Request, with rand, no autn +encoded message: +08 20 10 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 +ok +- Challenge Request, no rand, with autn +encoded message: +08 23 10 ce c4 e3 84 8a 33 00 00 86 78 11 58 ca 40 f1 36 +ok +- Challenge Request, with rand, with autn +encoded message: +08 20 10 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 23 10 ce c4 e3 84 8a 33 00 00 86 78 11 58 ca 40 f1 36 +ok +- Challenge Error +encoded message: +09 02 01 17 +ok +- Challenge Result +encoded message: +0a 24 08 01 02 03 04 05 06 07 08 +ok +- Sync Request +encoded message: +0c 25 10 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 01 +ok +Done. diff --git a/tests/testsuite.at b/tests/testsuite.at index c01f4afb..426c74cd 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -221,3 +221,10 @@ cat $abs_srcdir/fsm/fsm_test.ok > expout 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 -- cgit v1.2.3