Review at https://gerrit.osmocom.org/7322
add a regression test for TLV parsing with repeated IEs Since commit bf383a1d83661af26ccd6521c49b655fb22531d4 tlv_parse() will return the first occurrence of a repeated IE. Add a test to verify this behaviour. This test passes with the current code and fails if bf383a1d83661af26ccd6521c49b655fb22531d4 is reverted. While here, fix lies in documentation about the return value of tlv_parse() and fix a typo in another comment. Change-Id: I041f38548c5e4236920991d6c681c1c1e04de9ca Related: OS#2904 --- M src/gsm/tlv_parser.c M tests/tlv/tlv_test.c 2 files changed, 31 insertions(+), 2 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/22/7322/1 diff --git a/src/gsm/tlv_parser.c b/src/gsm/tlv_parser.c index b8c7149..f693971 100644 --- a/src/gsm/tlv_parser.c +++ b/src/gsm/tlv_parser.c @@ -48,7 +48,7 @@ struct tlv_definition tvlv_att_def; struct tlv_definition vtvlv_gan_att_def; -/*! Dump pasred TLV structure to stdout */ +/*! Dump parsed TLV structure to stdout */ int tlv_dump(struct tlv_parsed *dec) { int i; @@ -227,7 +227,7 @@ * \param[in] buf_len length of the input data buffer * \param[in] lv_tag an initial LV tag at the start of the buffer * \param[in] lv_tag2 a second initial LV tag following the \a lv_tag - * \returns number of bytes consumed by the TLV entry / IE parsed; negative in case of error + * \returns number of TLV entries parsed; negative in case of error */ int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def, const uint8_t *buf, int buf_len, uint8_t lv_tag, diff --git a/tests/tlv/tlv_test.c b/tests/tlv/tlv_test.c index 87b002f..e1c2301 100644 --- a/tests/tlv/tlv_test.c +++ b/tests/tlv/tlv_test.c @@ -245,11 +245,40 @@ } } +/* Most GSM related protocols clearly indicate that in case of duplicate + * IEs, only the first occurrence shall be used, while any further occurrences + * shall be ignored. See e.g. 3GPP TS 24.008 Section 8.6.3 */ +static void test_tlv_repeated_ie() +{ + uint8_t test_data[768]; + int i, rc; + const uint8_t tag = 0x1a; + struct tlv_parsed dec = { 0 }; + struct tlv_definition def = { 0 }; + + /* tag:1:255, tag:1:254, tag:1:253, ..., tag:1:3, tag:1:2, tag:1:1, tag:1:0 */ + for (i = 0; i < ARRAY_SIZE(test_data) - 1; i += 3) { + test_data[i] = tag; + test_data[i + 1] = 1; + test_data[i + 2] = (uint8_t)(0xff - i/2); + } + + def.def[tag].type = TLV_TYPE_TLV; + + rc = tlv_parse(&dec, &def, &test_data[1], sizeof(test_data) - 1, tag, 0); + OSMO_ASSERT(rc == i/3); + OSMO_ASSERT(dec.lv[tag].len == 1); + /* Value pointer should point at first value in test data array. */ + OSMO_ASSERT(dec.lv[tag].val == &test_data[2]); + OSMO_ASSERT(*dec.lv[tag].val == test_data[2]); +} + int main(int argc, char **argv) { //osmo_init_logging(&info); test_tlv_shift_functions(); + test_tlv_repeated_ie(); printf("Done.\n"); return EXIT_SUCCESS; -- To view, visit https://gerrit.osmocom.org/7322 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I041f38548c5e4236920991d6c681c1c1e04de9ca Gerrit-PatchSet: 1 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Stefan Sperling <ssperl...@sysmocom.de>