summaryrefslogtreecommitdiffstats
path: root/tests/tlv
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2019-02-13 22:23:13 +0100
committerHarald Welte <laforge@gnumonks.org>2019-05-19 07:33:32 +0000
commit8006f5393e21750558a01c780641831d925382ee (patch)
tree7cb67ca98a93395015075d540613dcc46a356e9e /tests/tlv
parente0c5d700ae4289cf1f4f5fae1d061b666387cb3d (diff)
TLV: Add one-shot TLV encoder
So far, the TLV code contained two types of functions * tlp_parse() to parse all TLVs according to definition into tlvp_parsed * various helper functions to encode individual TLVs during message generation This patch implements the inverse of tlv_parse(): tlv_encode(), which takes a full 'struct tlv_pared' and encodes all IEs found in it. The order of IEs is in numerically ascending order of the tag. As many protocols have different IE/TLV ordering requirements, let's add a tlv_encode_ordered() function where the caller can specify the TLV ordering during the one-shot encode. Change-Id: I761a30bf20355a9f80a4a8e0c60b0b0f78515efe
Diffstat (limited to 'tests/tlv')
-rw-r--r--tests/tlv/tlv_test.c47
-rw-r--r--tests/tlv/tlv_test.ok2
2 files changed, 49 insertions, 0 deletions
diff --git a/tests/tlv/tlv_test.c b/tests/tlv/tlv_test.c
index e2065b00..925d7628 100644
--- a/tests/tlv/tlv_test.c
+++ b/tests/tlv/tlv_test.c
@@ -1,4 +1,6 @@
+#include <osmocom/core/msgb.h>
#include <osmocom/gsm/tlv.h>
+#include <osmocom/gsm/gsm0808.h>
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)
@@ -286,12 +288,57 @@ static void test_tlv_repeated_ie()
OSMO_ASSERT(dec3[2].lv[tag].val == &test_data[2 + 3 + 3]);
}
+static void test_tlv_encoder()
+{
+ const uint8_t enc_ies[] = {
+ 0x17, 0x14, 0x06, 0x2b, 0x12, 0x2b, 0x0b, 0x40, 0x2b, 0xb7, 0x05, 0xd0, 0x63, 0x82, 0x95, 0x03, 0x05, 0x40,
+ 0x07, 0x08, 0x43, 0x90,
+ 0x2c, 0x04,
+ 0x40, 0x42,
+ };
+ const uint8_t ie_order[] = { 0x2c, 0x40, 0x17 };
+ const uint8_t enc_ies_reordered[] = {
+ 0x2c, 0x04,
+ 0x40, 0x42,
+ 0x17, 0x14, 0x06, 0x2b, 0x12, 0x2b, 0x0b, 0x40, 0x2b, 0xb7, 0x05, 0xd0, 0x63, 0x82, 0x95, 0x03, 0x05, 0x40,
+ 0x07, 0x08, 0x43, 0x90,
+ };
+ struct tlv_parsed tp;
+ struct msgb *msg = msgb_alloc(1024, __func__);
+ int rc;
+
+ printf("Testing TLV encoder by decoding + re-encoding binary\n");
+
+ OSMO_ASSERT(msg);
+
+ /* decode BSSAP IEs specified above */
+ rc = osmo_bssap_tlv_parse(&tp, enc_ies, ARRAY_SIZE(enc_ies));
+ OSMO_ASSERT(rc == 3);
+
+ /* re-encode it */
+ rc = tlv_encode(msg, gsm0808_att_tlvdef(), &tp);
+ OSMO_ASSERT(rc == ARRAY_SIZE(enc_ies));
+ OSMO_ASSERT(!memcmp(msgb_data(msg), enc_ies, ARRAY_SIZE(enc_ies)));
+
+ msgb_reset(msg);
+
+ printf("Testing TLV encoder with IE ordering\n");
+
+ /* re-encodei in different order */
+ rc = tlv_encode_ordered(msg, gsm0808_att_tlvdef(), &tp, ie_order, ARRAY_SIZE(ie_order));
+ OSMO_ASSERT(rc == ARRAY_SIZE(enc_ies));
+ OSMO_ASSERT(!memcmp(msgb_data(msg), enc_ies_reordered, ARRAY_SIZE(enc_ies_reordered)));
+
+ msgb_free(msg);
+}
+
int main(int argc, char **argv)
{
//osmo_init_logging2(ctx, &info);
test_tlv_shift_functions();
test_tlv_repeated_ie();
+ test_tlv_encoder();
printf("Done.\n");
return EXIT_SUCCESS;
diff --git a/tests/tlv/tlv_test.ok b/tests/tlv/tlv_test.ok
index de159bfb..f3f0fd41 100644
--- a/tests/tlv/tlv_test.ok
+++ b/tests/tlv/tlv_test.ok
@@ -1,2 +1,4 @@
Test shift functions
+Testing TLV encoder by decoding + re-encoding binary
+Testing TLV encoder with IE ordering
Done.