summaryrefslogtreecommitdiffstats
path: root/openbsc/tests
diff options
context:
space:
mode:
authorJacob Erlbeck <jerlbeck@sysmocom.de>2014-12-11 11:01:46 +0100
committerHolger Hans Peter Freyther <holger@moiji-mobile.com>2014-12-23 15:08:52 +0100
commitdcfd456640b6e15f15c94f175eb145220b96ee1a (patch)
treecedfd3be2439683f569a3adacb536a692b7f989f /openbsc/tests
parentc93919655753591bc883165f03f204a8773c4f64 (diff)
gprs: Move TLV parser functions to gprs_utils.c and rename them
Currently the parser functions for single information elements are defined within gprs_gb_parse.c and not exported explicitely. In addition they are named like libosmocore's TLV parser functions and do not have a proper name prefix. Since it is planned to use them for other protocols, they need to be globally accessible. This patch moves them to gprs_utils.c and renames them. The new names are: lv_shift -> gprs_shift_lv v_fixed_shift -> gprs_shift_v_fixed lv_shift -> gprs_shift_lv v_fixed_shift -> gprs_shift_v_fixed In the long term, these functions should be moved to libosmocore (and renamed again). Sponsored-by: On-Waves ehf
Diffstat (limited to 'openbsc/tests')
-rw-r--r--openbsc/tests/gbproxy/gbproxy_test.c236
-rw-r--r--openbsc/tests/gbproxy/gbproxy_test.ok1
-rw-r--r--openbsc/tests/gprs/gprs_test.c228
-rw-r--r--openbsc/tests/gprs/gprs_test.ok1
4 files changed, 229 insertions, 237 deletions
diff --git a/openbsc/tests/gbproxy/gbproxy_test.c b/openbsc/tests/gbproxy/gbproxy_test.c
index d3ce39b7b..0fc8af202 100644
--- a/openbsc/tests/gbproxy/gbproxy_test.c
+++ b/openbsc/tests/gbproxy/gbproxy_test.c
@@ -4419,241 +4419,6 @@ static void test_gbproxy_keep_info()
nsi = NULL;
}
-/* TODO: Move tlv testing to libosmocore */
-int v_fixed_shift(uint8_t **data, size_t *data_len, size_t len, uint8_t **value);
-int tv_fixed_match(uint8_t **data, size_t *data_len, uint8_t tag, size_t len,
- uint8_t **value);
-int tlv_match(uint8_t **data, size_t *data_len, uint8_t tag, uint8_t **value,
- size_t *value_len);
-int lv_shift(uint8_t **data, size_t *data_len,
- uint8_t **value, size_t *value_len);
-
-static void check_tlv_match(uint8_t **data, size_t *data_len,
- uint8_t tag, size_t exp_len, const uint8_t *exp_val)
-{
- uint8_t *value;
- size_t value_len;
- int rc;
-
- rc = tlv_match(data, data_len, tag ^ 1, NULL, NULL);
- OSMO_ASSERT(rc == 0);
-
- rc = tlv_match(data, data_len, 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);
-}
-
-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 = tv_fixed_match(data, data_len, tag ^ 1, len, NULL);
- OSMO_ASSERT(rc == 0);
-
- rc = tv_fixed_match(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 = v_fixed_shift(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 = lv_shift(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 == tlv_match(&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 <= tlv_match(&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 == tv_fixed_match(&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 <= tv_fixed_match(&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 == v_fixed_shift(&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 <= v_fixed_shift(&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 == lv_shift(&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 <= lv_shift(&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_match(&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 == tlv_match(&data, &data_len, tag, NULL, NULL));
- OSMO_ASSERT(-1 == tv_fixed_match(&data, &data_len, tag, len, NULL));
- OSMO_ASSERT((len ? -1 : 0) == v_fixed_shift(&data, &data_len, len, NULL));
- OSMO_ASSERT(-1 == lv_shift(&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);
- }
- }
-}
-
struct gbproxy_link_info *register_tlli(
struct gbproxy_peer *peer, uint32_t tlli,
const uint8_t *imsi, size_t imsi_len, time_t now)
@@ -5051,7 +4816,6 @@ int main(int argc, char **argv)
printf("===== GbProxy test START\n");
gbproxy_init_config(&gbcfg);
- test_tlv_shift_functions();
test_gbproxy();
test_gbproxy_ident_changes();
test_gbproxy_imsi_matching();
diff --git a/openbsc/tests/gbproxy/gbproxy_test.ok b/openbsc/tests/gbproxy/gbproxy_test.ok
index 71a50d0e6..46bbcbd2f 100644
--- a/openbsc/tests/gbproxy/gbproxy_test.ok
+++ b/openbsc/tests/gbproxy/gbproxy_test.ok
@@ -1,5 +1,4 @@
===== GbProxy test START
-Test shift functions
=== test_gbproxy ===
--- Initialise SGSN ---
diff --git a/openbsc/tests/gprs/gprs_test.c b/openbsc/tests/gprs/gprs_test.c
index 30e900f61..5bc207343 100644
--- a/openbsc/tests/gprs/gprs_test.c
+++ b/openbsc/tests/gprs/gprs_test.c
@@ -138,10 +138,238 @@ static void test_gsm_03_03_apn(void)
}
}
+/* TODO: Move tlv testing to libosmocore */
+static void check_tlv_match(uint8_t **data, size_t *data_len,
+ uint8_t tag, size_t exp_len, const uint8_t *exp_val)
+{
+ uint8_t *value;
+ size_t value_len;
+ int rc;
+
+ rc = gprs_match_tlv(data, data_len, tag ^ 1, NULL, NULL);
+ OSMO_ASSERT(rc == 0);
+
+ rc = gprs_match_tlv(data, data_len, 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);
+}
+
+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 = gprs_match_tv_fixed(data, data_len, tag ^ 1, len, NULL);
+ OSMO_ASSERT(rc == 0);
+
+ rc = gprs_match_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 = gprs_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 = gprs_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 == gprs_match_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 <= gprs_match_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 == gprs_match_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 <= gprs_match_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 == gprs_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 <= gprs_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 == gprs_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 <= gprs_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_match(&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 == gprs_match_tlv(&data, &data_len, tag, NULL, NULL));
+ OSMO_ASSERT(-1 == gprs_match_tv_fixed(&data, &data_len, tag, len, NULL));
+ OSMO_ASSERT((len ? -1 : 0) == gprs_shift_v_fixed(&data, &data_len, len, NULL));
+ OSMO_ASSERT(-1 == gprs_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)
{
test_8_4_2();
test_gsm_03_03_apn();
+ test_tlv_shift_functions();
printf("Done.\n");
return EXIT_SUCCESS;
diff --git a/openbsc/tests/gprs/gprs_test.ok b/openbsc/tests/gprs/gprs_test.ok
index 39d37c120..56d4b6543 100644
--- a/openbsc/tests/gprs/gprs_test.ok
+++ b/openbsc/tests/gprs/gprs_test.ok
@@ -13,4 +13,5 @@ N(U) = 511, V(UR) = 511 => new
N(U) = 510, V(UR) = 511 => retransmit
N(U) = 481, V(UR) = 511 => retransmit
N(U) = 479, V(UR) = 511 => new
+Test shift functions
Done.