diff options
author | Philipp <pmaier@sysmocom.de> | 2016-08-10 12:14:57 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2016-08-27 04:45:54 +0000 |
commit | a536fc644bc6f8ced40f7d40dbbc32f34c295721 (patch) | |
tree | 9930fd308f20c5373cd324aa563b325ebbff9287 /openbsc/tests | |
parent | d5d39ae2b680c64c2dc616d6ca3886dac9092066 (diff) |
Adding LLC-XID encoder / decoder and unit test
The lle-xid encoder/decoder is needed to encode and decode llc
xid parameter messages. We need this to exchange sndcp-parameters
(SNDCP-XID) and also simple parameters such as encryption IOVs
Change-Id: Ia06e4cb08bf9b48c2a4682606d1b1a91d19a9d37
Diffstat (limited to 'openbsc/tests')
-rw-r--r-- | openbsc/tests/Makefile.am | 2 | ||||
-rw-r--r-- | openbsc/tests/testsuite.at | 5 | ||||
-rw-r--r-- | openbsc/tests/xid/Makefile.am | 21 | ||||
-rw-r--r-- | openbsc/tests/xid/xid_test.c | 164 | ||||
-rw-r--r-- | openbsc/tests/xid/xid_test.ok | 12 |
5 files changed, 203 insertions, 1 deletions
diff --git a/openbsc/tests/Makefile.am b/openbsc/tests/Makefile.am index 09298a35c..ba5ca28d5 100644 --- a/openbsc/tests/Makefile.am +++ b/openbsc/tests/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = gsm0408 db channel mgcp gprs abis gbproxy trau subscr mm_auth +SUBDIRS = gsm0408 db channel mgcp gprs abis gbproxy trau subscr mm_auth xid if BUILD_NAT SUBDIRS += bsc-nat bsc-nat-trie diff --git a/openbsc/tests/testsuite.at b/openbsc/tests/testsuite.at index dab956888..6470ab9aa 100644 --- a/openbsc/tests/testsuite.at +++ b/openbsc/tests/testsuite.at @@ -124,3 +124,8 @@ cat $abs_srcdir/mm_auth/mm_auth_test.ok > expout AT_CHECK([$abs_top_builddir/tests/mm_auth/mm_auth_test], [], [expout], [ignore]) AT_CLEANUP +AT_SETUP([xid]) +AT_KEYWORDS([xid]) +cat $abs_srcdir/xid/xid_test.ok > expout +AT_CHECK([$abs_top_builddir/tests/xid/xid_test], [], [expout], [ignore]) +AT_CLEANUP diff --git a/openbsc/tests/xid/Makefile.am b/openbsc/tests/xid/Makefile.am new file mode 100644 index 000000000..9b6496594 --- /dev/null +++ b/openbsc/tests/xid/Makefile.am @@ -0,0 +1,21 @@ +AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include +AM_CFLAGS=-Wall -ggdb3 $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBCARES_CFLAGS) + +EXTRA_DIST = xid_test.ok + +noinst_PROGRAMS = xid_test + +xid_test_SOURCES = xid_test.c + +xid_test_LDADD = \ + $(top_builddir)/src/gprs/gprs_llc_xid.o \ + $(top_builddir)/src/libcommon/libcommon.a \ + $(LIBOSMOABIS_LIBS) \ + $(LIBOSMOCORE_LIBS) \ + $(LIBOSMOGSM_LIBS) \ + $(LIBOSMOGB_LIBS) \ + $(LIBCARES_LIBS) \ + $(LIBCRYPTO_LIBS) \ + -lgtp -lrt -lm + + diff --git a/openbsc/tests/xid/xid_test.c b/openbsc/tests/xid/xid_test.c new file mode 100644 index 000000000..b77a4ae85 --- /dev/null +++ b/openbsc/tests/xid/xid_test.c @@ -0,0 +1,164 @@ +/* Test LLC-XID Encoding/Decoding */ + +/* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> + * All Rights Reserved + * + * Author: Philipp Maier + * + * 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 <openbsc/gprs_llc_xid.h> +#include <openbsc/debug.h> + +#include <osmocom/core/talloc.h> +#include <osmocom/core/utils.h> + +#include <osmocom/core/application.h> + +#include <stdio.h> +#include <string.h> + +/* Test XID encoding */ +static void test_xid_encode(const void *ctx) +{ + struct gprs_llc_xid_field xid_field_1; + struct gprs_llc_xid_field xid_field_2; + struct gprs_llc_xid_field xid_field_3; + struct gprs_llc_xid_field xid_field_4; + LLIST_HEAD(xid_fields); + uint8_t xid[255]; + uint8_t xid_expected[] = + { 0x10, 0x8c, 0x14, 0x43, 0x43, 0x43, 0x43, 0x43, 0x0b, 0x42, 0x42, + 0x42, 0x05, 0x41 }; + int rc; + + printf("Testing LLC XID-Encoder\n"); + + /* Setup some simple XID data */ + xid_field_1.type = 1; + xid_field_2.type = 2; + xid_field_3.type = 3; + xid_field_4.type = 4; + + xid_field_1.data = (uint8_t *) "A"; + xid_field_2.data = (uint8_t *) "BBB"; + xid_field_3.data = (uint8_t *) "CCCCC"; + xid_field_4.data = NULL; + + xid_field_1.data_len = 1; + xid_field_2.data_len = 3; + xid_field_3.data_len = 5; + xid_field_4.data_len = 0; + + llist_add(&xid_field_4.list, &xid_fields); + llist_add(&xid_field_3.list, &xid_fields); + llist_add(&xid_field_2.list, &xid_fields); + llist_add(&xid_field_1.list, &xid_fields); + + printf("Data to encode:\n"); + gprs_llc_dump_xid_fields(&xid_fields, DSNDCP); + + /* Encode data */ + rc = gprs_llc_compile_xid(xid, sizeof(xid), &xid_fields); + OSMO_ASSERT(rc == 14); + printf("Encoded: %s (%i bytes)\n", osmo_hexdump_nospc(xid, rc), rc); + printf("Expected: %s (%i bytes)\n", + osmo_hexdump_nospc(xid_expected, sizeof(xid_expected)), + (int)sizeof(xid_expected)); + + OSMO_ASSERT(memcmp(xid_expected, xid, sizeof(xid_expected)) == 0); + + printf("\n"); +} + +/* Test XID decoding */ +static void test_xid_decode(const void *ctx) +{ + struct llist_head *xid_fields; + int rc; + + printf("Testing LLC XID-Decoder/Encoder\n"); + + /* Example of a real world LLC-XID message */ + uint8_t xid[] = + { 0x01, 0x00, 0x16, 0x05, 0xf0, 0x1a, 0x05, 0xf0, 0xac, 0xd8, 0x00, + 0x01, 0x00, 0x02, 0x31, 0x82, 0x02, 0x27, 0x89, 0xff, 0xe0, 0x00, 0x0f, + 0x00, 0xa8, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x02, 0x01, 0x02, + 0x00, 0x03, 0x01, 0x03, 0x00, 0x04, 0x01, 0x04, 0x00, 0x05, 0x01, 0x05, + 0x00, 0x06, 0x00, 0x07, 0x01, 0x07, 0x00, 0x08, 0x01, 0x08, 0x80, 0x00, + 0x04, 0x12, 0x00, 0x40, 0x07 }; + + uint8_t xid_r[512]; + + /* Decode and display XID fields */ + xid_fields = gprs_llc_parse_xid(ctx, xid, sizeof(xid)); + OSMO_ASSERT(xid_fields); + + printf("Decoded:\n"); + gprs_llc_dump_xid_fields(xid_fields, DSNDCP); + + + /* Encode xid-fields again */ + rc = gprs_llc_compile_xid(xid_r, sizeof(xid_r), xid_fields); + printf("Result length=%i\n",rc); + printf("Encoded: %s\n", osmo_hexdump_nospc(xid, sizeof(xid))); + printf("Rencoded: %s\n", osmo_hexdump_nospc(xid_r, rc)); + + OSMO_ASSERT(rc == 64); + OSMO_ASSERT(memcmp(xid, xid_r, sizeof(xid)) == 0); + + /* Free xid fields */ + talloc_free(xid_fields); + + printf("\n"); +} + +static struct log_info_cat gprs_categories[] = { + [DSNDCP] = { + .name = "DSNDCP", + .description = + "GPRS Sub-Network Dependent Control Protocol (SNDCP)", + .enabled = 1,.loglevel = LOGL_DEBUG, + } +}; + +static struct log_info info = { + .cat = gprs_categories, + .num_cat = ARRAY_SIZE(gprs_categories), +}; + +int main(int argc, char **argv) +{ + void *xid_ctx; + + osmo_init_logging(&info); + + xid_ctx = talloc_named_const(NULL, 0, "xid_ctx"); + + test_xid_decode(xid_ctx); + test_xid_encode(xid_ctx); + printf("Done\n"); + + talloc_report_full(xid_ctx, stderr); + OSMO_ASSERT(talloc_total_blocks(xid_ctx) == 1); + return 0; +} + +/* stubs */ +struct osmo_prim_hdr; +int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) +{ + abort(); +} diff --git a/openbsc/tests/xid/xid_test.ok b/openbsc/tests/xid/xid_test.ok new file mode 100644 index 000000000..4cf825ca5 --- /dev/null +++ b/openbsc/tests/xid/xid_test.ok @@ -0,0 +1,12 @@ +Testing LLC XID-Decoder/Encoder +Decoded: +Result length=64 +Encoded: 01001605f01a05f0acd8000100023182022789ffe0000f00a8000000010101000201020003010300040104000501050006000701070008010880000412004007 +Rencoded: 01001605f01a05f0acd8000100023182022789ffe0000f00a8000000010101000201020003010300040104000501050006000701070008010880000412004007 + +Testing LLC XID-Encoder +Data to encode: +Encoded: 108c1443434343430b4242420541 (14 bytes) +Expected: 108c1443434343430b4242420541 (14 bytes) + +Done |