summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2018-07-30 18:14:45 +0200
committerHarald Welte <laforge@gnumonks.org>2018-07-30 18:24:49 +0200
commit6db529aedd61191e1d574f97d62bebc8bcaf057d (patch)
treea4515b4d9f9ee36c15d3c8e45f2526b396a3b5e7
parent9b04c17c7a450fa83282f133283a86f4a0c2fd6f (diff)
import oap_client_test from osmo-sgsn
As oap_client has moved from osmo-sgsn to libosmogsm, it is only fair that the related unit test shall also be moved here. Change-Id: I9d64e10b4bacac9b530cf077841bad762fc6d558
-rw-r--r--tests/Makefile.am9
-rw-r--r--tests/oap/Makefile.am37
-rw-r--r--tests/oap/oap_client_test.c271
-rw-r--r--tests/oap/oap_client_test.err35
-rw-r--r--tests/oap/oap_client_test.ok2
-rw-r--r--tests/testsuite.at7
6 files changed, 322 insertions, 39 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index eaaa8df2..072bb4a2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -23,7 +23,8 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \
coding/coding_test conv/conv_gsm0503_test \
abis/abis_test endian/endian_test sercomm/sercomm_test \
prbs/prbs_test gsm23003/gsm23003_test \
- codec/codec_ecu_fr_test timer/clk_override_test
+ codec/codec_ecu_fr_test timer/clk_override_test \
+ oap/oap_client_test
if ENABLE_MSGFILE
check_PROGRAMS += msgfile/msgfile_test
@@ -172,6 +173,9 @@ gsup_gsup_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/libosmogsm.la
oap_oap_test_SOURCES = oap/oap_test.c
oap_oap_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/libosmogsm.la
+oap_oap_client_test_SOURCES = oap/oap_client_test.c
+oap_oap_client_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/libosmogsm.la
+
fsm_fsm_test_SOURCES = fsm/fsm_test.c
fsm_fsm_test_LDADD = $(LDADD) $(top_builddir)/src/ctrl/libosmoctrl.la
@@ -253,7 +257,8 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \
conv/conv_gsm0503_test.ok endian/endian_test.ok \
sercomm/sercomm_test.ok prbs/prbs_test.ok \
gsm23003/gsm23003_test.ok \
- timer/clk_override_test.ok
+ timer/clk_override_test.ok \
+ oap/oap_client_test.ok oap/oap_client_test.err
DISTCLEANFILES = atconfig atlocal conv/gsm0503_test_vectors.c
BUILT_SOURCES = conv/gsm0503_test_vectors.c
diff --git a/tests/oap/Makefile.am b/tests/oap/Makefile.am
deleted file mode 100644
index 06ccf338..00000000
--- a/tests/oap/Makefile.am
+++ /dev/null
@@ -1,37 +0,0 @@
-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_client_test.c b/tests/oap/oap_client_test.c
new file mode 100644
index 00000000..a841b381
--- /dev/null
+++ b/tests/oap/oap_client_test.c
@@ -0,0 +1,271 @@
+/* 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/application.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/gsm/oap.h>
+
+#include <osmocom/gsm/oap_client.h>
+
+#include <stdio.h>
+#include <string.h>
+
+static void test_oap_api(void)
+{
+ printf("Testing OAP API\n");
+
+ struct osmo_oap_client_config _config;
+ struct osmo_oap_client_config *config = &_config;
+
+ struct osmo_oap_client_state _state;
+ struct osmo_oap_client_state *state = &_state;
+
+ struct osmo_oap_message oap_rx;
+ struct msgb *msg_rx;
+
+ struct osmo_oap_message oap_tx;
+ struct msgb *msg_tx;
+
+ memset(config, 0, sizeof(*config));
+ memset(state, 0, sizeof(*state));
+
+ OSMO_ASSERT(osmo_hexparse("0102030405060708090a0b0c0d0e0f10", config->secret_k, 16) == 16);
+ OSMO_ASSERT(osmo_hexparse("1112131415161718191a1b1c1d1e1f20", config->secret_opc, 16) == 16);
+
+ fprintf(stderr, "- make sure filling with zeros means uninitialized\n");
+ OSMO_ASSERT(state->state == OSMO_OAP_UNINITIALIZED);
+
+ fprintf(stderr, "- reject messages in uninitialized state\n");
+ memset(&oap_rx, 0, sizeof(oap_rx));
+ state->client_id = 1;
+ oap_rx.message_type = OAP_MSGT_REGISTER_ERROR;
+ msg_rx = osmo_oap_client_encoded(&oap_rx);
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0);
+ OSMO_ASSERT(state->state == OSMO_OAP_UNINITIALIZED);
+ msgb_free(msg_rx);
+ OSMO_ASSERT(!msg_tx);
+
+ fprintf(stderr, "- NULL config should disable\n");
+ OSMO_ASSERT( osmo_oap_client_init(NULL, state) == 0 );
+ OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
+
+ fprintf(stderr, "- reject messages in disabled state\n");
+ memset(state, 0, sizeof(*state));
+ memset(&oap_rx, 0, sizeof(oap_rx));
+ state->state = OSMO_OAP_DISABLED;
+ state->client_id = 1;
+ oap_rx.message_type = OAP_MSGT_REGISTER_ERROR;
+ msg_rx = osmo_oap_client_encoded(&oap_rx);
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0);
+ OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
+ msgb_free(msg_rx);
+ OSMO_ASSERT(!msg_tx);
+
+ fprintf(stderr, "- invalid client_id and shared secret\n");
+ memset(state, 0, sizeof(*state));
+ config->client_id = 0;
+ config->secret_k_present = 0;
+ config->secret_opc_present = 0;
+ OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 );
+ OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
+
+ fprintf(stderr, "- reset state\n");
+ memset(state, 0, sizeof(*state));
+
+ fprintf(stderr, "- only client_id is invalid\n");
+ config->client_id = 0;
+ config->secret_k_present = 1;
+ config->secret_opc_present = 1;
+ OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 );
+ OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
+
+ memset(state, 0, sizeof(*state));
+
+ fprintf(stderr, "- valid id, but omitted shared_secret (1/2)\n");
+ config->client_id = 12345;
+ config->secret_k_present = 0;
+ config->secret_opc_present = 1;
+ OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 );
+ OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
+
+ memset(state, 0, sizeof(*state));
+
+ fprintf(stderr, "- valid id, but omitted shared_secret (2/2)\n");
+ config->client_id = 12345;
+ config->secret_k_present = 1;
+ config->secret_opc_present = 0;
+ OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 );
+ OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
+
+ memset(state, 0, sizeof(*state));
+
+
+ fprintf(stderr, "- mint configuration\n");
+ config->client_id = 12345;
+ config->secret_k_present = 1;
+ config->secret_opc_present = 1;
+ /*config->secret_* buffers are still set from the top */
+ OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 );
+ OSMO_ASSERT(state->state == OSMO_OAP_INITIALIZED);
+
+
+ fprintf(stderr, "- Missing challenge data\n");
+ memset(&oap_rx, 0, sizeof(oap_rx));
+ oap_rx.message_type = OAP_MSGT_CHALLENGE_REQUEST;
+ oap_rx.rand_present = 0;
+ oap_rx.autn_present = 0;
+ msg_rx = osmo_oap_client_encoded(&oap_rx);
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2);
+ msgb_free(msg_rx);
+ OSMO_ASSERT(!msg_tx);
+
+ fprintf(stderr, "- AUTN missing\n");
+ osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
+ oap_rx.rand, 16);
+ oap_rx.rand_present = 1;
+ msg_rx = osmo_oap_client_encoded(&oap_rx);
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2);
+ msgb_free(msg_rx);
+ OSMO_ASSERT(!msg_tx);
+
+ fprintf(stderr, "- RAND missing\n");
+ oap_rx.rand_present = 0;
+ osmo_hexparse("cec4e3848a33000086781158ca40f136",
+ oap_rx.autn, 16);
+ oap_rx.autn_present = 1;
+ msg_rx = osmo_oap_client_encoded(&oap_rx);
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2);
+ msgb_free(msg_rx);
+ OSMO_ASSERT(!msg_tx);
+
+ fprintf(stderr, "- wrong autn (by one bit)\n");
+ osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
+ oap_rx.rand, 16);
+ osmo_hexparse("dec4e3848a33000086781158ca40f136",
+ oap_rx.autn, 16);
+ oap_rx.rand_present = 1;
+ oap_rx.autn_present = 1;
+ msg_rx = osmo_oap_client_encoded(&oap_rx);
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2);
+ msgb_free(msg_rx);
+ OSMO_ASSERT(!msg_tx);
+
+ fprintf(stderr, "- all data correct\n");
+ osmo_hexparse("cec4e3848a33000086781158ca40f136",
+ oap_rx.autn, 16);
+ msg_rx = osmo_oap_client_encoded(&oap_rx);
+
+ fprintf(stderr, "- but refuse to evaluate in uninitialized state\n");
+ OSMO_ASSERT(state->state == OSMO_OAP_INITIALIZED);
+
+ state->state = OSMO_OAP_UNINITIALIZED;
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0);
+ OSMO_ASSERT(!msg_tx);
+
+ state->state = OSMO_OAP_DISABLED;
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0);
+ OSMO_ASSERT(!msg_tx);
+
+ state->state = OSMO_OAP_INITIALIZED;
+
+ fprintf(stderr, "- now everything is correct\n");
+ /* a successful return value here indicates correct autn */
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == 0);
+ msgb_free(msg_rx);
+
+ fprintf(stderr, "- Expect the challenge response in msg_tx\n");
+ OSMO_ASSERT(msg_tx);
+ OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0);
+ OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_CHALLENGE_RESULT);
+ OSMO_ASSERT(strcmp("e2d05b598c61d9ba",
+ osmo_hexdump_nospc(oap_tx.xres, sizeof(oap_tx.xres)))
+ == 0);
+ OSMO_ASSERT(state->state == OSMO_OAP_SENT_CHALLENGE_RESULT);
+ msgb_free(msg_tx);
+ msg_tx = 0;
+
+ struct osmo_oap_client_state saved_state = _state;
+
+ fprintf(stderr, "- Receive registration error for the first time.\n");
+
+ memset(&oap_rx, 0, sizeof(oap_rx));
+ oap_rx.message_type = OAP_MSGT_REGISTER_ERROR;
+ oap_rx.cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
+ msg_rx = osmo_oap_client_encoded(&oap_rx);
+
+ OSMO_ASSERT(state->registration_failures == 0);
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == 0);
+ OSMO_ASSERT(state->registration_failures == 1);
+ OSMO_ASSERT(msg_tx);
+ OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0);
+ OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_REGISTER_REQUEST);
+ OSMO_ASSERT(state->state == OSMO_OAP_REQUESTED_CHALLENGE);
+ msgb_free(msg_tx);
+ msg_tx = 0;
+
+ fprintf(stderr, "- Receive registration error for the Nth time.\n");
+ state->registration_failures = 999;
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -11);
+ OSMO_ASSERT(!msg_tx);
+ OSMO_ASSERT(state->state == OSMO_OAP_INITIALIZED);
+ msgb_free(msg_tx);
+ msg_tx = 0;
+
+ msgb_free(msg_rx);
+
+ fprintf(stderr, "- Registration success\n");
+
+ _state = saved_state;
+ memset(&oap_rx, 0, sizeof(oap_rx));
+ oap_rx.message_type = OAP_MSGT_REGISTER_RESULT;
+ msg_rx = osmo_oap_client_encoded(&oap_rx);
+ OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == 0);
+ OSMO_ASSERT(!msg_tx);
+ OSMO_ASSERT(state->state == OSMO_OAP_REGISTERED);
+ msgb_free(msg_rx);
+}
+
+static struct log_info_cat oap_client_test_categories[] = {
+};
+
+static struct log_info info = {
+ .cat = oap_client_test_categories,
+ .num_cat = ARRAY_SIZE(oap_client_test_categories),
+};
+
+int main(int argc, char **argv)
+{
+ void *ctx = talloc_named_const(NULL, 0, "oap_client_test");
+ msgb_talloc_ctx_init(ctx, 0);
+ osmo_init_logging2(ctx, &info);
+
+ OSMO_ASSERT(osmo_stderr_target);
+ log_set_use_color(osmo_stderr_target, 0);
+ log_set_print_timestamp(osmo_stderr_target, 0);
+ log_set_print_filename(osmo_stderr_target, 0);
+ log_set_print_category(osmo_stderr_target, 1);
+ log_parse_category_mask(osmo_stderr_target, "DLOAP,1");
+
+ test_oap_api();
+ printf("Done\n");
+
+ return 0;
+}
+
diff --git a/tests/oap/oap_client_test.err b/tests/oap/oap_client_test.err
new file mode 100644
index 00000000..62ddc9ef
--- /dev/null
+++ b/tests/oap/oap_client_test.err
@@ -0,0 +1,35 @@
+- make sure filling with zeros means uninitialized
+- reject messages in uninitialized state
+DLOAP Received OAP message 5, but the OAP client is not initialized
+- NULL config should disable
+- reject messages in disabled state
+DLOAP Received OAP message 5, but the OAP client is disabled
+- invalid client_id and shared secret
+- reset state
+- only client_id is invalid
+- valid id, but omitted shared_secret (1/2)
+DLOAP OAP: client ID set, but secret K missing.
+- valid id, but omitted shared_secret (2/2)
+DLOAP OAP: client ID set, but secret OPC missing.
+- mint configuration
+- Missing challenge data
+DLOAP OAP challenge incomplete (rand_present: 0, autn_present: 0)
+- AUTN missing
+DLOAP OAP challenge incomplete (rand_present: 1, autn_present: 0)
+- RAND missing
+DLOAP OAP challenge incomplete (rand_present: 0, autn_present: 1)
+- wrong autn (by one bit)
+DLOAP OAP: AUTN mismatch!
+DLOAP OAP: AUTN from server: dec4e3848a33000086781158ca40f136
+DLOAP OAP: AUTN expected: cec4e3848a33000086781158ca40f136
+- all data correct
+- but refuse to evaluate in uninitialized state
+DLOAP Received OAP message 8, but the OAP client is not initialized
+DLOAP Received OAP message 8, but the OAP client is disabled
+- now everything is correct
+- Expect the challenge response in msg_tx
+- Receive registration error for the first time.
+DLOAP OAP registration failed
+- Receive registration error for the Nth time.
+DLOAP OAP registration failed
+- Registration success
diff --git a/tests/oap/oap_client_test.ok b/tests/oap/oap_client_test.ok
new file mode 100644
index 00000000..59108a79
--- /dev/null
+++ b/tests/oap/oap_client_test.ok
@@ -0,0 +1,2 @@
+Testing OAP API
+Done
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 43b58e8d..a1cf98ae 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -274,6 +274,13 @@ touch experr
AT_CHECK([$abs_top_builddir/tests/oap/oap_test], [0], [expout], [experr])
AT_CLEANUP
+AT_SETUP([oap_client])
+AT_KEYWORDS([oap_client])
+cat $abs_srcdir/oap/oap_client_test.ok > expout
+cat $abs_srcdir/oap/oap_client_test.err > experr
+AT_CHECK([$abs_top_builddir/tests/oap/oap_client_test], [0], [expout], [experr])
+AT_CLEANUP
+
AT_SETUP([socket])
AT_KEYWORDS([socket])
cat $abs_srcdir/socket/socket_test.ok > expout