From fbd02fa8ccb27472412189febcc22f77d83ba0ac Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 25 Apr 2016 15:19:35 +0200 Subject: tlv: Import osmo_shift_* and osmo_match_shift_* from openbsc These routines have nothing to do with specifically the BSC, so import them to the TLV parser we keep in libosmogsm. --- tests/Makefile.am | 7 +- tests/testsuite.at | 6 ++ tests/tlv/tlv_test.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/tlv/tlv_test.ok | 2 + 4 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 tests/tlv/tlv_test.c create mode 100644 tests/tlv/tlv_test.ok (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index 3aaa99b5..fa814c72 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 \ - sim/sim_test + sim/sim_test tlv/tlv_test if ENABLE_MSGFILE check_PROGRAMS += msgfile/msgfile_test @@ -109,6 +109,9 @@ vty_vty_test_LDADD = $(top_builddir)/src/vty/libosmovty.la $(top_builddir)/src/l sim_sim_test_SOURCES = sim/sim_test.c sim_sim_test_LDADD = $(top_builddir)/src/sim/libosmosim.la $(top_builddir)/src/libosmocore.la +tlv_tlv_test_SOURCES = tlv/tlv_test.c +tlv_tlv_test_LDADD = $(top_builddir)/src/gsm/libosmogsm.la $(top_builddir)/src/libosmocore.la + # The `:;' works around a Bash 3.2 bug when the output is not writeable. $(srcdir)/package.m4: $(top_srcdir)/configure.ac :;{ \ @@ -143,7 +146,7 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \ vty/vty_test.ok comp128/comp128_test.ok \ utils/utils_test.ok stats/stats_test.ok \ bitvec/bitvec_test.ok msgb/msgb_test.ok bits/bitcomp_test.ok \ - sim/sim_test.ok + sim/sim_test.ok tlv/tlv_test.ok DISTCLEANFILES = atconfig diff --git a/tests/testsuite.at b/tests/testsuite.at index 902c1fcc..762b10a0 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -184,3 +184,9 @@ AT_KEYWORDS([timer]) cat $abs_srcdir/timer/timer_test.ok > expout AT_CHECK([$abs_top_builddir/tests/timer/timer_test -s 5], [0], [expout], [ignore]) AT_CLEANUP + +AT_SETUP([tlv]) +AT_KEYWORDS([tlv]) +cat $abs_srcdir/tlv/tlv_test.ok > expout +AT_CHECK([$abs_top_builddir/tests/tlv/tlv_test], [0], [expout], [ignore]) +AT_CLEANUP diff --git a/tests/tlv/tlv_test.c b/tests/tlv/tlv_test.c new file mode 100644 index 00000000..c571c3bd --- /dev/null +++ b/tests/tlv/tlv_test.c @@ -0,0 +1,250 @@ +#include + +static void check_tlv_parse(uint8_t **data, size_t *data_len, + uint8_t exp_tag, size_t exp_len, const uint8_t *exp_val) +{ + uint8_t *value; + size_t value_len; + uint8_t tag; + int rc; + uint8_t *saved_data = *data; + size_t saved_data_len = *data_len; + + rc = osmo_match_shift_tlv(data, data_len, exp_tag ^ 1, NULL, NULL); + OSMO_ASSERT(rc == 0); + + rc = osmo_match_shift_tlv(data, data_len, exp_tag, &value, &value_len); + OSMO_ASSERT(rc == (int)value_len + 2); + OSMO_ASSERT(value_len == exp_len); + OSMO_ASSERT(memcmp(value, exp_val, exp_len) == 0); + + /* restore data/data_len */ + *data = saved_data; + *data_len = saved_data_len; + + rc = osmo_shift_tlv(data, data_len, &tag, &value, &value_len); + OSMO_ASSERT(rc == (int)value_len + 2); + OSMO_ASSERT(tag == exp_tag); + OSMO_ASSERT(value_len == exp_len); + OSMO_ASSERT(memcmp(value, exp_val, exp_len) == 0); +} + +static void check_tv_fixed_match(uint8_t **data, size_t *data_len, + uint8_t tag, size_t len, const uint8_t *exp_val) +{ + uint8_t *value; + int rc; + + rc = osmo_match_shift_tv_fixed(data, data_len, tag ^ 1, len, NULL); + OSMO_ASSERT(rc == 0); + + rc = osmo_match_shift_tv_fixed(data, data_len, tag, len, &value); + OSMO_ASSERT(rc == (int)len + 1); + OSMO_ASSERT(memcmp(value, exp_val, len) == 0); +} + +static void check_v_fixed_shift(uint8_t **data, size_t *data_len, + size_t len, const uint8_t *exp_val) +{ + uint8_t *value; + int rc; + + rc = osmo_shift_v_fixed(data, data_len, len, &value); + OSMO_ASSERT(rc == (int)len); + OSMO_ASSERT(memcmp(value, exp_val, len) == 0); +} + +static void check_lv_shift(uint8_t **data, size_t *data_len, + size_t exp_len, const uint8_t *exp_val) +{ + uint8_t *value; + size_t value_len; + int rc; + + rc = osmo_shift_lv(data, data_len, &value, &value_len); + OSMO_ASSERT(rc == (int)value_len + 1); + OSMO_ASSERT(value_len == exp_len); + OSMO_ASSERT(memcmp(value, exp_val, exp_len) == 0); +} + +static void check_tlv_match_data_len(size_t data_len, uint8_t tag, size_t len, + const uint8_t *test_data) +{ + uint8_t buf[300] = {0}; + + uint8_t *unchanged_ptr = buf - 1; + size_t unchanged_len = 0xdead; + size_t tmp_data_len = data_len; + uint8_t *value = unchanged_ptr; + size_t value_len = unchanged_len; + uint8_t *data = buf; + + OSMO_ASSERT(data_len <= sizeof(buf)); + + tlv_put(data, tag, len, test_data); + if (data_len < len + 2) { + OSMO_ASSERT(-1 == osmo_match_shift_tlv(&data, &tmp_data_len, + tag, &value, &value_len)); + OSMO_ASSERT(tmp_data_len == 0); + OSMO_ASSERT(data == buf + data_len); + OSMO_ASSERT(value == unchanged_ptr); + OSMO_ASSERT(value_len == unchanged_len); + } else { + OSMO_ASSERT(0 <= osmo_match_shift_tlv(&data, &tmp_data_len, + tag, &value, &value_len)); + OSMO_ASSERT(value != unchanged_ptr); + OSMO_ASSERT(value_len != unchanged_len); + } +} + +static void check_tv_fixed_match_data_len(size_t data_len, + uint8_t tag, size_t len, + const uint8_t *test_data) +{ + uint8_t buf[300] = {0}; + + uint8_t *unchanged_ptr = buf - 1; + size_t tmp_data_len = data_len; + uint8_t *value = unchanged_ptr; + uint8_t *data = buf; + + OSMO_ASSERT(data_len <= sizeof(buf)); + + tv_fixed_put(data, tag, len, test_data); + + if (data_len < len + 1) { + OSMO_ASSERT(-1 == osmo_match_shift_tv_fixed(&data, &tmp_data_len, + tag, len, &value)); + OSMO_ASSERT(tmp_data_len == 0); + OSMO_ASSERT(data == buf + data_len); + OSMO_ASSERT(value == unchanged_ptr); + } else { + OSMO_ASSERT(0 <= osmo_match_shift_tv_fixed(&data, &tmp_data_len, + tag, len, &value)); + OSMO_ASSERT(value != unchanged_ptr); + } +} + +static void check_v_fixed_shift_data_len(size_t data_len, + size_t len, const uint8_t *test_data) +{ + uint8_t buf[300] = {0}; + + uint8_t *unchanged_ptr = buf - 1; + size_t tmp_data_len = data_len; + uint8_t *value = unchanged_ptr; + uint8_t *data = buf; + + OSMO_ASSERT(data_len <= sizeof(buf)); + + memcpy(data, test_data, len); + + if (data_len < len) { + OSMO_ASSERT(-1 == osmo_shift_v_fixed(&data, &tmp_data_len, + len, &value)); + OSMO_ASSERT(tmp_data_len == 0); + OSMO_ASSERT(data == buf + data_len); + OSMO_ASSERT(value == unchanged_ptr); + } else { + OSMO_ASSERT(0 <= osmo_shift_v_fixed(&data, &tmp_data_len, + len, &value)); + OSMO_ASSERT(value != unchanged_ptr); + } +} + +static void check_lv_shift_data_len(size_t data_len, + size_t len, const uint8_t *test_data) +{ + uint8_t buf[300] = {0}; + + uint8_t *unchanged_ptr = buf - 1; + size_t unchanged_len = 0xdead; + size_t tmp_data_len = data_len; + uint8_t *value = unchanged_ptr; + size_t value_len = unchanged_len; + uint8_t *data = buf; + + lv_put(data, len, test_data); + if (data_len < len + 1) { + OSMO_ASSERT(-1 == osmo_shift_lv(&data, &tmp_data_len, + &value, &value_len)); + OSMO_ASSERT(tmp_data_len == 0); + OSMO_ASSERT(data == buf + data_len); + OSMO_ASSERT(value == unchanged_ptr); + OSMO_ASSERT(value_len == unchanged_len); + } else { + OSMO_ASSERT(0 <= osmo_shift_lv(&data, &tmp_data_len, + &value, &value_len)); + OSMO_ASSERT(value != unchanged_ptr); + OSMO_ASSERT(value_len != unchanged_len); + } +} + +static void test_tlv_shift_functions() +{ + uint8_t test_data[1024]; + uint8_t buf[1024]; + uint8_t *data_end; + unsigned i, len; + uint8_t *data; + size_t data_len; + const uint8_t tag = 0x1a; + + printf("Test shift functions\n"); + + for (i = 0; i < ARRAY_SIZE(test_data); i++) + test_data[i] = (uint8_t)i; + + for (len = 0; len < 256; len++) { + const unsigned iterations = sizeof(buf) / (len + 2) / 4; + + memset(buf, 0xee, sizeof(buf)); + data_end = data = buf; + + for (i = 0; i < iterations; i++) { + data_end = tlv_put(data_end, tag, len, test_data); + data_end = tv_fixed_put(data_end, tag, len, test_data); + /* v_fixed_put */ + memcpy(data_end, test_data, len); + data_end += len; + data_end = lv_put(data_end, len, test_data); + } + + data_len = data_end - data; + OSMO_ASSERT(data_len <= sizeof(buf)); + + for (i = 0; i < iterations; i++) { + check_tlv_parse(&data, &data_len, tag, len, test_data); + check_tv_fixed_match(&data, &data_len, tag, len, test_data); + check_v_fixed_shift(&data, &data_len, len, test_data); + check_lv_shift(&data, &data_len, len, test_data); + } + + OSMO_ASSERT(data == data_end); + + /* Test at end of data */ + + OSMO_ASSERT(-1 == osmo_match_shift_tlv(&data, &data_len, tag, NULL, NULL)); + OSMO_ASSERT(-1 == osmo_match_shift_tv_fixed(&data, &data_len, tag, len, NULL)); + OSMO_ASSERT((len ? -1 : 0) == osmo_shift_v_fixed(&data, &data_len, len, NULL)); + OSMO_ASSERT(-1 == osmo_shift_lv(&data, &data_len, NULL, NULL)); + + /* Test invalid data_len */ + for (data_len = 0; data_len <= len + 2 + 1; data_len += 1) { + check_tlv_match_data_len(data_len, tag, len, test_data); + check_tv_fixed_match_data_len(data_len, tag, len, test_data); + check_v_fixed_shift_data_len(data_len, len, test_data); + check_lv_shift_data_len(data_len, len, test_data); + } + } +} + +int main(int argc, char **argv) +{ + //osmo_init_logging(&info); + + test_tlv_shift_functions(); + + printf("Done.\n"); + return EXIT_SUCCESS; +} diff --git a/tests/tlv/tlv_test.ok b/tests/tlv/tlv_test.ok new file mode 100644 index 00000000..de159bfb --- /dev/null +++ b/tests/tlv/tlv_test.ok @@ -0,0 +1,2 @@ +Test shift functions +Done. -- cgit v1.2.3