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/oap/Makefile.am | 37 +++++++++++ tests/oap/oap_test.c | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/oap/oap_test.ok | 42 ++++++++++++ 3 files changed, 260 insertions(+) create mode 100644 tests/oap/Makefile.am create mode 100644 tests/oap/oap_test.c create mode 100644 tests/oap/oap_test.ok (limited to 'tests/oap') 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. -- cgit v1.2.3