summaryrefslogtreecommitdiffstats
path: root/openbsc
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
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')
-rw-r--r--openbsc/include/openbsc/gprs_utils.h10
-rw-r--r--openbsc/src/gprs/gprs_gb_parse.c172
-rw-r--r--openbsc/src/gprs/gprs_utils.c117
-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
7 files changed, 384 insertions, 381 deletions
diff --git a/openbsc/include/openbsc/gprs_utils.h b/openbsc/include/openbsc/gprs_utils.h
index 93c53501d..60d0ef6f5 100644
--- a/openbsc/include/openbsc/gprs_utils.h
+++ b/openbsc/include/openbsc/gprs_utils.h
@@ -35,3 +35,13 @@ int gprs_is_mi_tmsi(const uint8_t *value, size_t value_len);
int gprs_is_mi_imsi(const uint8_t *value, size_t value_len);
int gprs_parse_mi_tmsi(const uint8_t *value, size_t value_len, uint32_t *tmsi);
void gprs_parse_tmsi(const uint8_t *value, uint32_t *tmsi);
+
+int gprs_shift_v_fixed(uint8_t **data, size_t *data_len,
+ size_t len, uint8_t **value);
+int gprs_match_tv_fixed(uint8_t **data, size_t *data_len,
+ uint8_t tag, size_t len, uint8_t **value);
+int gprs_match_tlv(uint8_t **data, size_t *data_len,
+ uint8_t tag, uint8_t **value, size_t *value_len);
+int gprs_shift_lv(uint8_t **data, size_t *data_len,
+ uint8_t **value, size_t *value_len);
+
diff --git a/openbsc/src/gprs/gprs_gb_parse.c b/openbsc/src/gprs/gprs_gb_parse.c
index 6c6371ca0..609685407 100644
--- a/openbsc/src/gprs/gprs_gb_parse.c
+++ b/openbsc/src/gprs/gprs_gb_parse.c
@@ -29,122 +29,6 @@
#include <osmocom/gprs/gprs_bssgp.h>
-/* TODO: Move shift functions to libosmocore */
-
-int v_fixed_shift(uint8_t **data, size_t *data_len,
- size_t len, uint8_t **value)
-{
- if (len > *data_len)
- goto fail;
-
- if (value)
- *value = *data;
-
- *data += len;
- *data_len -= len;
-
- return len;
-
-fail:
- *data += *data_len;
- *data_len = 0;
- return -1;
-}
-
-int tv_fixed_match(uint8_t **data, size_t *data_len,
- uint8_t tag, size_t len,
- uint8_t **value)
-{
- size_t ie_len;
-
- if (*data_len == 0)
- goto fail;
-
- if ((*data)[0] != tag)
- return 0;
-
- if (len > *data_len - 1)
- goto fail;
-
- if (value)
- *value = *data + 1;
-
- ie_len = len + 1;
- *data += ie_len;
- *data_len -= ie_len;
-
- return ie_len;
-
-fail:
- *data += *data_len;
- *data_len = 0;
- return -1;
-}
-
-int tlv_match(uint8_t **data, size_t *data_len,
- uint8_t tag, uint8_t **value, size_t *value_len)
-{
- size_t len;
- size_t ie_len;
-
- if (*data_len < 2)
- goto fail;
-
- if ((*data)[0] != tag)
- return 0;
-
- len = (*data)[1];
- if (len > *data_len - 2)
- goto fail;
-
- if (value)
- *value = *data + 2;
- if (value_len)
- *value_len = len;
-
- ie_len = len + 2;
-
- *data += ie_len;
- *data_len -= ie_len;
-
- return ie_len;
-
-fail:
- *data += *data_len;
- *data_len = 0;
- return -1;
-}
-
-int lv_shift(uint8_t **data, size_t *data_len,
- uint8_t **value, size_t *value_len)
-{
- size_t len;
- size_t ie_len;
-
- if (*data_len < 1)
- goto fail;
-
- len = (*data)[0];
- if (len > *data_len - 1)
- goto fail;
-
- if (value)
- *value = *data + 1;
- if (value_len)
- *value_len = len;
-
- ie_len = len + 1;
- *data += ie_len;
- *data_len -= ie_len;
-
- return ie_len;
-
-fail:
- *data += *data_len;
- *data_len = 0;
- return -1;
-}
-
static int gprs_gb_parse_gmm_attach_req(uint8_t *data, size_t data_len,
struct gprs_gb_parse_context *parse_ctx)
{
@@ -154,7 +38,7 @@ static int gprs_gb_parse_gmm_attach_req(uint8_t *data, size_t data_len,
parse_ctx->llc_msg_name = "ATTACH_REQ";
/* Skip MS network capability */
- if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 ||
+ if (gprs_shift_lv(&data, &data_len, NULL, &value_len) <= 0 ||
value_len < 1 || value_len > 8)
/* invalid */
return 0;
@@ -162,10 +46,10 @@ static int gprs_gb_parse_gmm_attach_req(uint8_t *data, size_t data_len,
/* Skip Attach type */
/* Skip Ciphering key sequence number */
/* Skip DRX parameter */
- v_fixed_shift(&data, &data_len, 3, NULL);
+ gprs_shift_v_fixed(&data, &data_len, 3, NULL);
/* Get Mobile identity */
- if (lv_shift(&data, &data_len, &value, &value_len) <= 0 ||
+ if (gprs_shift_lv(&data, &data_len, &value, &value_len) <= 0 ||
value_len < 5 || value_len > 8)
/* invalid */
return 0;
@@ -177,7 +61,7 @@ static int gprs_gb_parse_gmm_attach_req(uint8_t *data, size_t data_len,
parse_ctx->imsi_len = value_len;
}
- if (v_fixed_shift(&data, &data_len, 6, &value) <= 0)
+ if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0)
return 0;
parse_ctx->old_raid_enc = value;
@@ -198,21 +82,21 @@ static int gprs_gb_parse_gmm_attach_ack(uint8_t *data, size_t data_len,
/* Skip Periodic RA update timer */
/* Skip Radio priority for SMS */
/* Skip Spare half octet */
- v_fixed_shift(&data, &data_len, 3, NULL);
+ gprs_shift_v_fixed(&data, &data_len, 3, NULL);
- if (v_fixed_shift(&data, &data_len, 6, &value) <= 0)
+ if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0)
return 0;
parse_ctx->raid_enc = value;
/* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */
- tv_fixed_match(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL);
+ gprs_match_tv_fixed(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL);
/* Skip Negotiated READY timer value (GPRS timer, opt, TV, length 2) */
- tv_fixed_match(&data, &data_len, GSM48_IE_GMM_TIMER_READY, 1, NULL);
+ gprs_match_tv_fixed(&data, &data_len, GSM48_IE_GMM_TIMER_READY, 1, NULL);
/* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */
- if (tlv_match(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI,
+ if (gprs_match_tlv(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI,
&value, &value_len) > 0 &&
gprs_is_mi_tmsi(value, value_len))
parse_ctx->new_ptmsi_enc = value + 1;
@@ -227,7 +111,7 @@ static int gprs_gb_parse_gmm_attach_rej(uint8_t *data, size_t data_len,
parse_ctx->llc_msg_name = "ATTACH_REJ";
/* GMM cause */
- if (v_fixed_shift(&data, &data_len, 1, &value) <= 0)
+ if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0)
return 0;
parse_ctx->invalidate_tlli = 1;
@@ -248,7 +132,7 @@ static int gprs_gb_parse_gmm_detach_req(uint8_t *data, size_t data_len,
/* Skip spare half octet */
/* Get Detach type */
- if (v_fixed_shift(&data, &data_len, 1, &value) <= 0)
+ if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0)
/* invalid */
return 0;
@@ -266,7 +150,7 @@ static int gprs_gb_parse_gmm_detach_req(uint8_t *data, size_t data_len,
parse_ctx->invalidate_tlli = 1;
/* Get P-TMSI (Mobile identity), see GSM 24.008, 9.4.5.2 */
- if (tlv_match(&data, &data_len,
+ if (gprs_match_tlv(&data, &data_len,
GSM48_IE_GMM_ALLOC_PTMSI, &value, &value_len) > 0)
{
if (gprs_is_mi_tmsi(value, value_len))
@@ -286,9 +170,9 @@ static int gprs_gb_parse_gmm_ra_upd_req(uint8_t *data, size_t data_len,
/* Skip Update type */
/* Skip GPRS ciphering key sequence number */
- v_fixed_shift(&data, &data_len, 1, NULL);
+ gprs_shift_v_fixed(&data, &data_len, 1, NULL);
- if (v_fixed_shift(&data, &data_len, 6, &value) <= 0)
+ if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0)
return 0;
parse_ctx->old_raid_enc = value;
@@ -306,14 +190,14 @@ static int gprs_gb_parse_gmm_ra_upd_rej(uint8_t *data, size_t data_len,
parse_ctx->llc_msg_name = "RA_UPD_REJ";
/* GMM cause */
- if (v_fixed_shift(&data, &data_len, 1, &value) <= 0)
+ if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0)
return 0;
cause = value[0];
/* Force to standby, 1/2 */
/* spare bits, 1/2 */
- if (v_fixed_shift(&data, &data_len, 1, &value) <= 0)
+ if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0)
return 0;
force_standby = (value[0] & 0x07) == 0x01;
@@ -337,18 +221,18 @@ static int gprs_gb_parse_gmm_ra_upd_ack(uint8_t *data, size_t data_len,
/* Skip Force to standby */
/* Skip Update result */
/* Skip Periodic RA update timer */
- v_fixed_shift(&data, &data_len, 2, NULL);
+ gprs_shift_v_fixed(&data, &data_len, 2, NULL);
- if (v_fixed_shift(&data, &data_len, 6, &value) <= 0)
+ if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0)
return 0;
parse_ctx->raid_enc = value;
/* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */
- tv_fixed_match(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL);
+ gprs_match_tv_fixed(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL);
/* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */
- if (tlv_match(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI,
+ if (gprs_match_tlv(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI,
&value, &value_len) > 0 &&
gprs_is_mi_tmsi(value, value_len))
parse_ctx->new_ptmsi_enc = value + 1;
@@ -368,11 +252,11 @@ static int gprs_gb_parse_gmm_ptmsi_reall_cmd(uint8_t *data, size_t data_len,
"Got P-TMSI Reallocation Command which is not covered by unit tests yet.\n");
/* Allocated P-TMSI */
- if (lv_shift(&data, &data_len, &value, &value_len) > 0 &&
+ if (gprs_shift_lv(&data, &data_len, &value, &value_len) > 0 &&
gprs_is_mi_tmsi(value, value_len))
parse_ctx->new_ptmsi_enc = value + 1;
- if (v_fixed_shift(&data, &data_len, 6, &value) <= 0)
+ if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0)
return 0;
parse_ctx->raid_enc = value;
@@ -389,7 +273,7 @@ static int gprs_gb_parse_gmm_id_resp(uint8_t *data, size_t data_len,
parse_ctx->llc_msg_name = "ID_RESP";
/* Mobile identity, Mobile identity 10.5.1.4, M LV 2-10 */
- if (lv_shift(&data, &data_len, &value, &value_len) <= 0 ||
+ if (gprs_shift_lv(&data, &data_len, &value, &value_len) <= 0 ||
value_len < 1 || value_len > 9)
/* invalid */
return 0;
@@ -415,22 +299,22 @@ static int gprs_gb_parse_gsm_act_pdp_req(uint8_t *data, size_t data_len,
/* Skip Requested NSAPI */
/* Skip Requested LLC SAPI */
- v_fixed_shift(&data, &data_len, 2, NULL);
+ gprs_shift_v_fixed(&data, &data_len, 2, NULL);
/* Skip Requested QoS (support 04.08 and 24.008) */
- if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 ||
+ if (gprs_shift_lv(&data, &data_len, NULL, &value_len) <= 0 ||
value_len < 4 || value_len > 14)
/* invalid */
return 0;
/* Skip Requested PDP address */
- if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 ||
+ if (gprs_shift_lv(&data, &data_len, NULL, &value_len) <= 0 ||
value_len < 2 || value_len > 18)
/* invalid */
return 0;
/* Access point name */
- old_len = tlv_match(&data, &data_len,
+ old_len = gprs_match_tlv(&data, &data_len,
GSM48_IE_GSM_APN, &value, &value_len);
if (old_len > 0 && value_len >=1 && value_len <= 100) {
@@ -446,7 +330,7 @@ int gprs_gb_parse_dtap(uint8_t *data, size_t data_len,
{
struct gsm48_hdr *g48h;
- if (v_fixed_shift(&data, &data_len, sizeof(*g48h), (uint8_t **)&g48h) <= 0)
+ if (gprs_shift_v_fixed(&data, &data_len, sizeof(*g48h), (uint8_t **)&g48h) <= 0)
return 0;
parse_ctx->g48_hdr = g48h;
diff --git a/openbsc/src/gprs/gprs_utils.c b/openbsc/src/gprs/gprs_utils.c
index 0cf8e4f95..cec07d7ec 100644
--- a/openbsc/src/gprs/gprs_utils.c
+++ b/openbsc/src/gprs/gprs_utils.c
@@ -217,3 +217,120 @@ void gprs_parse_tmsi(const uint8_t *value, uint32_t *tmsi)
*tmsi = ntohl(tmsi_be);
}
+
+/* TODO: Move shift functions to libosmocore */
+
+int gprs_shift_v_fixed(uint8_t **data, size_t *data_len,
+ size_t len, uint8_t **value)
+{
+ if (len > *data_len)
+ goto fail;
+
+ if (value)
+ *value = *data;
+
+ *data += len;
+ *data_len -= len;
+
+ return len;
+
+fail:
+ *data += *data_len;
+ *data_len = 0;
+ return -1;
+}
+
+int gprs_match_tv_fixed(uint8_t **data, size_t *data_len,
+ uint8_t tag, size_t len,
+ uint8_t **value)
+{
+ size_t ie_len;
+
+ if (*data_len == 0)
+ goto fail;
+
+ if ((*data)[0] != tag)
+ return 0;
+
+ if (len > *data_len - 1)
+ goto fail;
+
+ if (value)
+ *value = *data + 1;
+
+ ie_len = len + 1;
+ *data += ie_len;
+ *data_len -= ie_len;
+
+ return ie_len;
+
+fail:
+ *data += *data_len;
+ *data_len = 0;
+ return -1;
+}
+
+int gprs_match_tlv(uint8_t **data, size_t *data_len,
+ uint8_t tag, uint8_t **value, size_t *value_len)
+{
+ size_t len;
+ size_t ie_len;
+
+ if (*data_len < 2)
+ goto fail;
+
+ if ((*data)[0] != tag)
+ return 0;
+
+ len = (*data)[1];
+ if (len > *data_len - 2)
+ goto fail;
+
+ if (value)
+ *value = *data + 2;
+ if (value_len)
+ *value_len = len;
+
+ ie_len = len + 2;
+
+ *data += ie_len;
+ *data_len -= ie_len;
+
+ return ie_len;
+
+fail:
+ *data += *data_len;
+ *data_len = 0;
+ return -1;
+}
+
+int gprs_shift_lv(uint8_t **data, size_t *data_len,
+ uint8_t **value, size_t *value_len)
+{
+ size_t len;
+ size_t ie_len;
+
+ if (*data_len < 1)
+ goto fail;
+
+ len = (*data)[0];
+ if (len > *data_len - 1)
+ goto fail;
+
+ if (value)
+ *value = *data + 1;
+ if (value_len)
+ *value_len = len;
+
+ ie_len = len + 1;
+ *data += ie_len;
+ *data_len -= ie_len;
+
+ return ie_len;
+
+fail:
+ *data += *data_len;
+ *data_len = 0;
+ return -1;
+}
+
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.