1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
#include <osmocom/gsm/tlv.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)
{
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[301] = {0};
*buf = 0xfe;
uint8_t *unchanged_ptr = buf;
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 + 1;
OSMO_ASSERT(data_len <= sizeof(buf) - 1);
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 + 1 + 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[301] = {0};
*buf = 0xfe;
uint8_t *unchanged_ptr = buf;
size_t tmp_data_len = data_len;
uint8_t *value = unchanged_ptr;
uint8_t *data = buf + 1;
OSMO_ASSERT(data_len <= sizeof(buf) - 1);
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 + 1 + 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[301] = {0};
*buf = 0xfe;
uint8_t *unchanged_ptr = buf;
size_t tmp_data_len = data_len;
uint8_t *value = unchanged_ptr;
uint8_t *data = buf + 1;
OSMO_ASSERT(data_len <= sizeof(buf) - 1);
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 + 1 + 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[301] = {0};
*buf = 0xfe;
uint8_t *unchanged_ptr = buf;
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 + 1;
OSMO_ASSERT(data_len <= sizeof(buf) - 1);
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 + 1 + 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);
}
}
}
/* 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;
struct tlv_definition def;
memset(&def, 0, sizeof(def));
/* 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;
}
|