eerimoq / asn1tools
1
#include <stdio.h>
2
#include <stdint.h>
3
#include <string.h>
4
#include <math.h>
5
#include "nala.h"
6

7
#include "files/c_source/oer.h"
8
#include "files/c_source/c_source-minus.h"
9

10
#define membersof(a) (sizeof(a) / (sizeof((a)[0])))
11

12 2
static bool fequal(double v1, double v2)
13
{
14 2
    return (fabs(v1 - v2) < 0.000001);
15
}
16

17 2
TEST(oer_c_source_a)
18
{
19
    uint8_t encoded[42];
20
    struct oer_c_source_a_t decoded;
21

22
    /* Encode. */
23 2
    decoded.a = -1;
24 2
    decoded.b = -2;
25 2
    decoded.c = -3;
26 2
    decoded.d = -4;
27 2
    decoded.e = 1;
28 2
    decoded.f = 2;
29 2
    decoded.g = 3;
30 2
    decoded.h = 4;
31 2
    decoded.i = true;
32 2
    memset(&decoded.j.buf[0], 5, sizeof(decoded.j.buf));
33

34 2
    memset(&encoded[0], 0, sizeof(encoded));
35 2
    ASSERT_EQ(oer_c_source_a_encode(&encoded[0],
36
                                    sizeof(encoded),
37
                                    &decoded), sizeof(encoded));
38 2
    ASSERT_MEMORY_EQ(&encoded[0],
39
                     "\xff\xff\xfe\xff\xff\xff\xfd\xff\xff\xff\xff\xff\xff"
40
                     "\xff\xfc\x01\x00\x02\x00\x00\x00\x03\x00\x00\x00\x00"
41
                     "\x00\x00\x00\x04\xff\x05\x05\x05\x05\x05\x05\x05\x05"
42
                     "\x05\x05\x05",
43
                     sizeof(encoded));
44

45
    /* Decode. */
46 2
    memset(&decoded, 0, sizeof(decoded));
47 2
    ASSERT_EQ(oer_c_source_a_decode(&decoded,
48
                                    &encoded[0],
49
                                    sizeof(encoded)), sizeof(encoded));
50

51 2
    ASSERT_EQ(decoded.a, -1);
52 2
    ASSERT_EQ(decoded.b, -2);
53 2
    ASSERT_EQ(decoded.c, -3);
54 2
    ASSERT_EQ(decoded.d, -4);
55 2
    ASSERT_EQ(decoded.e, 1);
56 2
    ASSERT_EQ(decoded.f, 2);
57 2
    ASSERT_EQ(decoded.g, 3);
58 2
    ASSERT_EQ(decoded.h, 4);
59 2
    ASSERT_TRUE(decoded.i);
60 2
    ASSERT_MEMORY_EQ(&decoded.j.buf[0],
61
                     "\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05",
62
                     sizeof(decoded.j.buf));
63
}
64

65 2
TEST(oer_c_source_a_decode_spare_data)
66
{
67 2
    uint8_t encoded[43] =
68
        "\xff\xff\xfe\xff\xff\xff\xfd\xff\xff\xff\xff\xff\xff"
69
        "\xff\xfc\x01\x00\x02\x00\x00\x00\x03\x00\x00\x00\x00"
70
        "\x00\x00\x00\x04\xff\x05\x05\x05\x05\x05\x05\x05\x05"
71
        "\x05\x05\x05\x00";
72
    struct oer_c_source_a_t decoded;
73

74 2
    memset(&decoded, 0, sizeof(decoded));
75 2
    ASSERT_EQ(oer_c_source_a_decode(&decoded,
76
                                    &encoded[0],
77
                                    sizeof(encoded)), 42);
78

79 2
    ASSERT_EQ(decoded.a, -1);
80 2
    ASSERT_EQ(decoded.b, -2);
81 2
    ASSERT_EQ(decoded.c, -3);
82 2
    ASSERT_EQ(decoded.d, -4);
83 2
    ASSERT_EQ(decoded.e, 1);
84 2
    ASSERT_EQ(decoded.f, 2);
85 2
    ASSERT_EQ(decoded.g, 3);
86 2
    ASSERT_EQ(decoded.h, 4);
87 2
    ASSERT_TRUE(decoded.i);
88 2
    ASSERT_MEMORY_EQ(&decoded.j.buf[0],
89
                     "\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05",
90
                     sizeof(decoded.j.buf));
91
}
92

93 2
TEST(oer_c_source_a_encode_error_no_mem)
94
{
95
    uint8_t encoded[41];
96
    struct oer_c_source_a_t decoded;
97

98 2
    decoded.a = -1;
99 2
    decoded.b = -2;
100 2
    decoded.c = -3;
101 2
    decoded.d = -4;
102 2
    decoded.e = 1;
103 2
    decoded.f = 2;
104 2
    decoded.g = 3;
105 2
    decoded.h = 4;
106 2
    decoded.i = true;
107 2
    memset(&decoded.j.buf[0], 5, sizeof(decoded.j.buf));
108

109 2
    ASSERT_EQ(oer_c_source_a_encode(&encoded[0],
110
                                    sizeof(encoded),
111
                                    &decoded), -ENOMEM);
112
}
113

114 2
TEST(oer_c_source_a_decode_error_out_of_data)
115
{
116 2
    uint8_t encoded[41] =
117
        "\xff\xff\xfe\xff\xff\xff\xfd\xff\xff\xff\xff\xff\xff"
118
        "\xff\xfc\x01\x00\x02\x00\x00\x00\x03\x00\x00\x00\x00"
119
        "\x00\x00\x00\x04\xff\x05\x05\x05\x05\x05\x05\x05\x05"
120
        "\x05\x05";
121
    struct oer_c_source_a_t decoded;
122

123 2
    ASSERT_EQ(oer_c_source_a_decode(&decoded,
124
                                    &encoded[0],
125
                                    sizeof(encoded)), -EOUTOFDATA);
126
}
127

128 2
TEST(oer_c_source_b_choice_a)
129
{
130
    uint8_t encoded[2];
131
    struct oer_c_source_b_t decoded;
132

133
    /* Encode. */
134 2
    decoded.choice = oer_c_source_b_choice_a_e;
135 2
    decoded.value.a = -10;
136

137 2
    memset(&encoded[0], 0, sizeof(encoded));
138 2
    ASSERT_EQ(oer_c_source_b_encode(&encoded[0],
139
                                    sizeof(encoded),
140
                                    &decoded), sizeof(encoded));
141 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x80\xf6", sizeof(encoded));
142

143
    /* Decode. */
144 2
    memset(&decoded, 0, sizeof(decoded));
145 2
    ASSERT_EQ(oer_c_source_b_decode(&decoded,
146
                                    &encoded[0],
147
                                    sizeof(encoded)), sizeof(encoded));
148

149 2
    ASSERT_EQ(decoded.choice, oer_c_source_b_choice_a_e);
150 2
    ASSERT_EQ(decoded.value.a, -10);
151
}
152

153 2
TEST(oer_c_source_b_choice_b)
154
{
155
    uint8_t encoded[43];
156
    struct oer_c_source_b_t decoded;
157

158
    /* Encode. */
159 2
    decoded.choice = oer_c_source_b_choice_b_e;
160 2
    decoded.value.b.a = -1;
161 2
    decoded.value.b.b = -2;
162 2
    decoded.value.b.c = -3;
163 2
    decoded.value.b.d = -4;
164 2
    decoded.value.b.e = 1;
165 2
    decoded.value.b.f = 2;
166 2
    decoded.value.b.g = 3;
167 2
    decoded.value.b.h = 4;
168 2
    decoded.value.b.i = true;
169 2
    memset(&decoded.value.b.j.buf[0], 5, sizeof(decoded.value.b.j.buf));
170

171 2
    memset(&encoded[0], 0, sizeof(encoded));
172 2
    ASSERT_EQ(oer_c_source_b_encode(&encoded[0],
173
                                    sizeof(encoded),
174
                                    &decoded), sizeof(encoded));
175 2
    ASSERT_MEMORY_EQ(&encoded[0],
176
                     "\x81\xff\xff\xfe\xff\xff\xff\xfd\xff\xff\xff\xff\xff"
177
                     "\xff\xff\xfc\x01\x00\x02\x00\x00\x00\x03\x00\x00\x00"
178
                     "\x00\x00\x00\x00\x04\xff\x05\x05\x05\x05\x05\x05\x05"
179
                     "\x05\x05\x05\x05",
180
                     sizeof(encoded));
181

182
    /* Decode. */
183 2
    memset(&decoded, 0, sizeof(decoded));
184 2
    ASSERT_EQ(oer_c_source_b_decode(&decoded,
185
                                    &encoded[0],
186
                                    sizeof(encoded)), sizeof(encoded));
187

188 2
    ASSERT_EQ(decoded.choice, oer_c_source_b_choice_b_e);
189 2
    ASSERT_EQ(decoded.value.b.a, -1);
190 2
    ASSERT_EQ(decoded.value.b.b, -2);
191 2
    ASSERT_EQ(decoded.value.b.c, -3);
192 2
    ASSERT_EQ(decoded.value.b.d, -4);
193 2
    ASSERT_EQ(decoded.value.b.e, 1);
194 2
    ASSERT_EQ(decoded.value.b.f, 2);
195 2
    ASSERT_EQ(decoded.value.b.g, 3);
196 2
    ASSERT_EQ(decoded.value.b.h, 4);
197 2
    ASSERT_TRUE(decoded.value.b.i);
198 2
    ASSERT_MEMORY_EQ(&decoded.value.b.j.buf[0],
199
                     "\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05",
200
                     sizeof(decoded.value.b.j.buf));
201
}
202

203 2
TEST(oer_c_source_b_decode_error_bad_choice)
204
{
205
    /* 0x80 (a), 0x81 (b) and 0x82 (c) are valid tags in the encoded
206
       data. */
207 2
    uint8_t encoded[2] = "\x83\x00";
208
    struct oer_c_source_b_t decoded;
209

210 2
    ASSERT_EQ(oer_c_source_b_decode(&decoded,
211
                                    &encoded[0],
212
                                    sizeof(encoded)), -EBADCHOICE);
213
}
214

215 2
TEST(oer_c_source_c_empty)
216
{
217
    uint8_t encoded[2];
218
    struct oer_c_source_c_t decoded;
219

220
    /* Encode. */
221 2
    decoded.length = 0;
222

223 2
    memset(&encoded[0], 0, sizeof(encoded));
224 2
    ASSERT_EQ(oer_c_source_c_encode(&encoded[0],
225
                                    sizeof(encoded),
226
                                    &decoded), sizeof(encoded));
227 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x01\x00", sizeof(encoded));
228

229
    /* Decode. */
230 2
    memset(&decoded, 0, sizeof(decoded));
231 2
    ASSERT_EQ(oer_c_source_c_decode(&decoded,
232
                                    &encoded[0],
233
                                    sizeof(encoded)), sizeof(encoded));
234

235 2
    ASSERT_EQ(decoded.length, 0);
236
}
237

238 2
TEST(oer_c_source_c_2_elements)
239
{
240
    uint8_t encoded[6];
241
    struct oer_c_source_c_t decoded;
242

243
    /* Encode. */
244 2
    decoded.length = 2;
245 2
    decoded.elements[0].choice = oer_c_source_b_choice_a_e;
246 2
    decoded.elements[0].value.a = -11;
247 2
    decoded.elements[1].choice = oer_c_source_b_choice_a_e;
248 2
    decoded.elements[1].value.a = 13;
249

250 2
    memset(&encoded[0], 0, sizeof(encoded));
251 2
    ASSERT_EQ(oer_c_source_c_encode(&encoded[0],
252
                                    sizeof(encoded),
253
                                    &decoded), sizeof(encoded));
254 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x01\x02\x80\xf5\x80\x0d", sizeof(encoded));
255

256
    /* Decode. */
257 2
    memset(&decoded, 0, sizeof(decoded));
258 2
    ASSERT_EQ(oer_c_source_c_decode(&decoded,
259
                                    &encoded[0],
260
                                    sizeof(encoded)), sizeof(encoded));
261

262 2
    ASSERT_EQ(decoded.length, 2);
263 2
    ASSERT_EQ(decoded.elements[0].choice, oer_c_source_b_choice_a_e);
264 2
    ASSERT_EQ(decoded.elements[0].value.a, -11);
265 2
    ASSERT_EQ(decoded.elements[1].choice, oer_c_source_b_choice_a_e);
266 2
    ASSERT_EQ(decoded.elements[1].value.a, 13);
267
}
268

269 2
TEST(oer_c_source_c_decode_error_bad_length)
270
{
271 2
    uint8_t encoded[8] = "\x01\x03\x80\xf5\x80\x0d\x80\x0e";
272
    struct oer_c_source_c_t decoded;
273

274 2
    ASSERT_EQ(oer_c_source_c_decode(&decoded,
275
                                    &encoded[0],
276
                                    sizeof(encoded)), -EBADLENGTH);
277
}
278

279 2
TEST(oer_c_source_d_all_present)
280
{
281
    uint8_t encoded[21];
282
    struct oer_c_source_d_t decoded;
283

284
    /* Encode. */
285 2
    decoded.length = 1;
286 2
    decoded.elements[0].a.b.choice = oer_c_source_d_a_b_choice_c_e;
287 2
    decoded.elements[0].a.b.value.c = 0;
288 2
    decoded.elements[0].a.e.length = 3;
289 2
    decoded.elements[0].g.h = oer_c_source_d_g_h_j_e;
290 2
    decoded.elements[0].g.l.length = 2;
291 2
    decoded.elements[0].g.l.buf[0] = 0x54;
292 2
    decoded.elements[0].g.l.buf[1] = 0x55;
293 2
    decoded.elements[0].m.is_n_present = true;
294 2
    decoded.elements[0].m.n = false;
295 2
    decoded.elements[0].m.o = 2;
296 2
    decoded.elements[0].m.is_p_present = true;
297 2
    memset(&decoded.elements[0].m.p.q.buf[0],
298
           3,
299
           sizeof(decoded.elements[0].m.p.q.buf));
300 2
    decoded.elements[0].m.p.is_r_present = true;
301 2
    decoded.elements[0].m.p.r = true;
302 2
    decoded.elements[0].m.s = true;
303

304 2
    memset(&encoded[0], 0, sizeof(encoded));
305 2
    ASSERT_EQ(oer_c_source_d_encode(&encoded[0],
306
                                    sizeof(encoded),
307
                                    &decoded), sizeof(encoded));
308 2
    ASSERT_MEMORY_EQ(&encoded[0],
309
                     "\x01\x01\x80\x00\x01\x03\x00\x02\x54\x55\xf0\x00\x02\x80\x03\x03"
310
                     "\x03\x03\x03\xff\xff",
311
                     sizeof(encoded));
312

313
    /* Decode. */
314 2
    memset(&decoded, 0, sizeof(decoded));
315 2
    ASSERT_EQ(oer_c_source_d_decode(&decoded,
316
                                    &encoded[0],
317
                                    sizeof(encoded)), sizeof(encoded));
318

319 2
    ASSERT_EQ(decoded.length, 1);
320 2
    ASSERT_EQ(decoded.elements[0].a.b.choice, oer_c_source_d_a_b_choice_c_e);
321 2
    ASSERT_EQ(decoded.elements[0].a.b.value.c, 0);
322 2
    ASSERT_EQ(decoded.elements[0].a.e.length, 3);
323 2
    ASSERT_EQ(decoded.elements[0].g.h, oer_c_source_d_g_h_j_e);
324 2
    ASSERT_EQ(decoded.elements[0].g.l.length, 2);
325 2
    ASSERT_EQ(decoded.elements[0].g.l.buf[0], 0x54);
326 2
    ASSERT_EQ(decoded.elements[0].g.l.buf[1], 0x55);
327 2
    ASSERT_TRUE(decoded.elements[0].m.is_n_present);
328 2
    ASSERT_EQ(decoded.elements[0].m.n, false);
329 2
    ASSERT_EQ(decoded.elements[0].m.o, 2);
330 2
    ASSERT_TRUE(decoded.elements[0].m.is_p_present);
331 2
    ASSERT_MEMORY_EQ(&decoded.elements[0].m.p.q.buf[0],
332
                     "\x03\x03\x03\x03\x03",
333
                     sizeof(decoded.elements[0].m.p.q.buf));
334 2
    ASSERT_TRUE(decoded.elements[0].m.p.is_r_present);
335 2
    ASSERT_EQ(decoded.elements[0].m.p.r, true);
336 2
    ASSERT_EQ(decoded.elements[0].m.s, true);
337
}
338

339 2
TEST(oer_c_source_d_some_missing)
340
{
341
    uint8_t encoded[19];
342
    struct oer_c_source_d_t decoded;
343

344
    /* Encode. */
345 2
    decoded.length = 1;
346 2
    decoded.elements[0].a.b.choice = oer_c_source_d_a_b_choice_d_e;
347 2
    decoded.elements[0].a.b.value.d = false;
348 2
    decoded.elements[0].a.e.length = 3;
349 2
    decoded.elements[0].g.h = oer_c_source_d_g_h_k_e;
350 2
    decoded.elements[0].g.l.length = 1;
351 2
    decoded.elements[0].g.l.buf[0] = 0x54;
352 2
    decoded.elements[0].m.is_n_present = false;
353
    /* Default value 3. */
354 2
    decoded.elements[0].m.o = 3;
355 2
    decoded.elements[0].m.is_p_present = true;
356 2
    memset(&decoded.elements[0].m.p.q.buf[0],
357
           3,
358
           sizeof(decoded.elements[0].m.p.q.buf));
359 2
    decoded.elements[0].m.p.is_r_present = false;
360 2
    decoded.elements[0].m.s = false;
361

362 2
    memset(&encoded[0], 0, sizeof(encoded));
363 2
    ASSERT_EQ(oer_c_source_d_encode(&encoded[0],
364
                                    sizeof(encoded),
365
                                    &decoded), sizeof(encoded));
366

367 2
    ASSERT_MEMORY_EQ(&encoded[0],
368
                     "\x01\x01\x81\x00\x01\x03\x80\x82\x02\x00\x01\x54\x20\x00\x03"
369
                     "\x03\x03\x03\x03",
370
                     sizeof(encoded));
371

372
    /* Decode. */
373 2
    memset(&decoded, 0, sizeof(decoded));
374 2
    ASSERT_EQ(oer_c_source_d_decode(&decoded,
375
                                    &encoded[0],
376
                                    sizeof(encoded)), sizeof(encoded));
377

378 2
    ASSERT_EQ(decoded.length, 1);
379 2
    ASSERT_EQ(decoded.elements[0].a.b.choice, oer_c_source_d_a_b_choice_d_e);
380 2
    ASSERT_EQ(decoded.elements[0].a.b.value.d, false);
381 2
    ASSERT_EQ(decoded.elements[0].a.e.length, 3);
382 2
    ASSERT_EQ(decoded.elements[0].g.h, oer_c_source_d_g_h_k_e);
383 2
    ASSERT_EQ(decoded.elements[0].g.l.length, 1);
384 2
    ASSERT_EQ(decoded.elements[0].g.l.buf[0], 0x54);
385 2
    ASSERT_FALSE(decoded.elements[0].m.is_n_present);
386 2
    ASSERT_EQ(decoded.elements[0].m.o, 3);
387 2
    ASSERT_TRUE(decoded.elements[0].m.is_p_present);
388 2
    ASSERT_MEMORY_EQ(&decoded.elements[0].m.p.q.buf[0],
389
                     "\x03\x03\x03\x03\x03",
390
                     sizeof(decoded.elements[0].m.p.q.buf));
391 2
    ASSERT_FALSE(decoded.elements[0].m.p.is_r_present);
392 2
    ASSERT_EQ(decoded.elements[0].m.s, false);
393
}
394

395 2
TEST(oer_c_source_e)
396
{
397
    uint8_t encoded[3];
398
    struct oer_c_source_e_t decoded;
399

400
    /* Encode. */
401 2
    decoded.a.choice = oer_c_source_e_a_choice_b_e;
402 2
    decoded.a.value.b.choice = oer_c_source_e_a_b_choice_c_e;
403 2
    decoded.a.value.b.value.c = true;
404

405 2
    memset(&encoded[0], 0, sizeof(encoded));
406 2
    ASSERT_EQ(oer_c_source_e_encode(&encoded[0],
407
                                    sizeof(encoded),
408
                                    &decoded), sizeof(encoded));
409 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x80\x80\xff", sizeof(encoded));
410

411
    /* Decode. */
412 2
    memset(&decoded, 0, sizeof(decoded));
413 2
    ASSERT_EQ(oer_c_source_e_decode(&decoded,
414
                                    &encoded[0],
415
                                    sizeof(encoded)), sizeof(encoded));
416

417 2
    ASSERT_EQ(decoded.a.choice, oer_c_source_e_a_choice_b_e);
418 2
    ASSERT_EQ(decoded.a.value.b.choice, oer_c_source_e_a_b_choice_c_e);
419 2
    ASSERT_EQ(decoded.a.value.b.value.c, true);
420
}
421

422 2
TEST(oer_c_source_f)
423
{
424
    uint8_t encoded[8];
425
    struct oer_c_source_f_t decoded;
426

427
    /* Encode. */
428 2
    decoded.length = 2;
429 2
    decoded.elements[0].elements[0] = false;
430 2
    decoded.elements[1].elements[0] = true;
431

432 2
    memset(&encoded[0], 0, sizeof(encoded));
433 2
    ASSERT_EQ(oer_c_source_f_encode(&encoded[0],
434
                                    sizeof(encoded),
435
                                    &decoded), sizeof(encoded));
436 2
    ASSERT_MEMORY_EQ(&encoded[0],
437
                     "\x01\x02\x01\x01\x00\x01\x01\xff",
438
                     sizeof(encoded));
439

440
    /* Decode. */
441 2
    memset(&decoded, 0, sizeof(decoded));
442 2
    ASSERT_EQ(oer_c_source_f_decode(&decoded,
443
                                    &encoded[0],
444
                                    sizeof(encoded)), sizeof(encoded));
445

446 2
    ASSERT_EQ(decoded.length, 2);
447 2
    ASSERT_EQ(decoded.elements[0].elements[0], false);
448 2
    ASSERT_EQ(decoded.elements[1].elements[0], true);
449
}
450

451 2
TEST(oer_c_source_g)
452
{
453
    uint8_t encoded[4];
454
    struct oer_c_source_g_t decoded;
455

456
    /* Encode. */
457 2
    decoded.is_a_present = true;
458 2
    decoded.a = true;
459 2
    decoded.is_b_present = false;
460 2
    decoded.is_c_present = false;
461 2
    decoded.is_d_present = false;
462 2
    decoded.is_e_present = false;
463 2
    decoded.is_f_present = false;
464 2
    decoded.is_g_present = false;
465 2
    decoded.is_h_present = false;
466 2
    decoded.is_i_present = true;
467 2
    decoded.i = true;
468

469 2
    memset(&encoded[0], 0, sizeof(encoded));
470 2
    ASSERT_EQ(oer_c_source_g_encode(&encoded[0],
471
                                    sizeof(encoded),
472
                                    &decoded), sizeof(encoded));
473 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x80\x80\xff\xff", sizeof(encoded));
474

475
    /* Decode. */
476 2
    memset(&decoded, 0, sizeof(decoded));
477 2
    ASSERT_EQ(oer_c_source_g_decode(&decoded,
478
                                    &encoded[0],
479
                                    sizeof(encoded)), sizeof(encoded));
480

481 2
    ASSERT_TRUE(decoded.is_a_present);
482 2
    ASSERT_EQ(decoded.a, true);
483 2
    ASSERT_FALSE(decoded.is_b_present);
484 2
    ASSERT_FALSE(decoded.is_c_present);
485 2
    ASSERT_FALSE(decoded.is_d_present);
486 2
    ASSERT_FALSE(decoded.is_e_present);
487 2
    ASSERT_FALSE(decoded.is_f_present);
488 2
    ASSERT_FALSE(decoded.is_g_present);
489 2
    ASSERT_FALSE(decoded.is_h_present);
490 2
    ASSERT_TRUE(decoded.is_i_present);
491 2
    ASSERT_EQ(decoded.i, true);
492
}
493

494 2
TEST(oer_c_source_h)
495
{
496
    uint8_t encoded[1];
497
    struct oer_c_source_h_t decoded;
498

499
    /* Encode. */
500
    memset(&encoded[0], 0, sizeof(encoded));
501 2
    ASSERT_EQ(oer_c_source_h_encode(&encoded[0],
502
                                    sizeof(encoded),
503
                                    &decoded), 0);
504

505
    /* Decode. */
506 2
    memset(&decoded, 0, sizeof(decoded));
507 2
    ASSERT_EQ(oer_c_source_h_decode(&decoded,
508
                                    &encoded[0],
509
                                    sizeof(encoded)), 0);
510
}
511

512 2
TEST(oer_c_source_i)
513
{
514
    uint8_t encoded[24];
515
    struct oer_c_source_i_t decoded;
516 2
    uint8_t data[24] =
517
        "\x01\x02\x03\x04\x01\x02\x03\x04\x01\x02\x03\x04\x01\x02\x03"
518
        "\x04\x01\x02\x03\x04\x01\x02\x03\x04";
519

520
    /* Encode. */
521 2
    memcpy(&decoded.buf[0], &data[0], sizeof(data));
522

523 2
    memset(&encoded[0], 0, sizeof(encoded));
524 2
    ASSERT_EQ(oer_c_source_i_encode(&encoded[0],
525
                                    sizeof(encoded),
526
                                    &decoded), sizeof(encoded));
527 2
    ASSERT_MEMORY_EQ(&encoded[0], &data[0], sizeof(encoded));
528

529
    /* Decode. */
530 2
    memset(&decoded, 0, sizeof(decoded));
531 2
    ASSERT_EQ(oer_c_source_i_decode(&decoded,
532
                                    &encoded[0],
533
                                    sizeof(encoded)), sizeof(encoded));
534

535 2
    ASSERT_MEMORY_EQ(&decoded.buf[0], &data[0], sizeof(data));
536
}
537

538 2
TEST(oer_c_source_j)
539
{
540
    uint8_t encoded[23];
541
    struct oer_c_source_j_t decoded;
542 2
    uint8_t data[22] =
543
        "\x01\x02\x03\x04\x01\x02\x03\x04\x01\x02\x03\x04\x01\x02\x03"
544
        "\x04\x01\x02\x03\x04\x01\x02";
545

546
    /* Encode. */
547 2
    decoded.length = sizeof(data);
548 2
    memcpy(&decoded.buf[0], &data[0], sizeof(data));
549

550 2
    memset(&encoded[0], 0, sizeof(encoded));
551 2
    ASSERT_EQ(oer_c_source_j_encode(&encoded[0],
552
                                    sizeof(encoded),
553
                                    &decoded), sizeof(encoded));
554 2
    ASSERT_MEMORY_EQ(&encoded[0],
555
                     "\x16\x01\x02\x03\x04\x01\x02\x03\x04\x01\x02\x03\x04\x01"
556
                     "\x02\x03\x04\x01\x02\x03\x04\x01\x02",
557
                     sizeof(encoded));
558

559
    /* Decode. */
560 2
    memset(&decoded, 0, sizeof(decoded));
561 2
    ASSERT_EQ(oer_c_source_j_decode(&decoded,
562
                                    &encoded[0],
563
                                    sizeof(encoded)), sizeof(encoded));
564

565 2
    ASSERT_EQ(decoded.length, sizeof(data));
566 2
    ASSERT_MEMORY_EQ(&decoded.buf[0], &data[0], sizeof(data));
567
}
568

569 2
TEST(oer_c_source_k)
570
{
571
    uint8_t encoded[1];
572
    struct oer_c_source_k_t decoded;
573

574
    /* Encode. */
575 2
    decoded.value = oer_c_source_k_a_e;
576

577
    memset(&encoded[0], 0, sizeof(encoded));
578 2
    ASSERT_EQ(oer_c_source_k_encode(&encoded[0],
579
                                    sizeof(encoded),
580
                                    &decoded), sizeof(encoded));
581 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x00", sizeof(encoded));
582

583
    /* Decode. */
584 2
    memset(&decoded, 0, sizeof(decoded));
585 2
    ASSERT_EQ(oer_c_source_k_decode(&decoded,
586
                                    &encoded[0],
587
                                    sizeof(encoded)), sizeof(encoded));
588

589 2
    ASSERT_EQ(decoded.value, oer_c_source_k_a_e);
590
}
591

592 2
TEST(oer_c_source_l)
593
{
594
    struct data_t {
595
        uint16_t data_length;
596
        uint16_t encoded_length;
597
        uint8_t encoded[263];
598 2
    } datas[] = {
599
        {
600
            .data_length = 0,
601
            .encoded_length = 1,
602
            .encoded = "\x00"
603
        },
604
        {
605
            .data_length = 127,
606
            .encoded_length = 128,
607
            .encoded =
608
            "\x7f\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
609
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
610
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
611
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
612
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
613
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
614
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
615
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
616
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
617
            "\xa5\xa5"
618
        },
619
        {
620
            .data_length = 128,
621
            .encoded_length = 130,
622
            .encoded =
623
            "\x81\x80\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
624
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
625
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
626
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
627
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
628
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
629
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
630
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
631
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
632
            "\xa5\xa5\xa5\xa5"
633
        },
634
        {
635
            .data_length = 260,
636
            .encoded_length = 263,
637
            .encoded =
638
            "\x82\x01\x04\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
639
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
640
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
641
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
642
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
643
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
644
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
645
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
646
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
647
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
648
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
649
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
650
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
651
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
652
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
653
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
654
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
655
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
656
            "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
657
        }
658
    };
659
    uint8_t encoded[263];
660
    struct oer_c_source_l_t decoded;
661 2
    uint8_t data[260] =
662
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
663
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
664
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
665
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
666
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
667
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
668
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
669
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
670
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
671
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
672
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
673
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
674
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
675
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
676
        "\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5\xa5"
677
        "\xa5\xa5\xa5\xa5\xa5";
678
    unsigned int i;
679

680 2
    for (i = 0; i < membersof(datas); i++) {
681
        /* Encode. */
682 2
        decoded.length = datas[i].data_length;
683 2
        memcpy(&decoded.buf[0], &data[0], decoded.length);
684

685 2
        memset(&encoded[0], 0, sizeof(encoded));
686 2
        ASSERT_EQ(oer_c_source_l_encode(&encoded[0],
687
                                        sizeof(encoded),
688
                                        &decoded), datas[i].encoded_length);
689 2
        ASSERT_MEMORY_EQ(&encoded[0],
690
                         &datas[i].encoded[0],
691
                         datas[i].encoded_length);
692

693
        /* Decode. */
694 2
        memset(&decoded, 0, sizeof(decoded));
695 2
        ASSERT_EQ(oer_c_source_l_decode(&decoded,
696
                                        &encoded[0],
697
                                        sizeof(encoded)), datas[i].encoded_length);
698

699 2
        ASSERT_EQ(decoded.length, datas[i].data_length);
700 2
        ASSERT_MEMORY_EQ(&decoded.buf[0], &data[0], datas[i].data_length);
701
    }
702
}
703

704 2
TEST(oer_c_source_l_decode_error_bad_length)
705
{
706
    struct data_t {
707
        int res;
708
        uint16_t length;
709
        uint8_t encoded[16];
710 2
    } datas[] = {
711
        {
712
            .res = -EBADLENGTH,
713
            .length = 3,
714
            .encoded = "\x82\x01\xff"
715
        },
716
        {
717
            .res = -EBADLENGTH,
718
            .length = 4,
719
            .encoded = "\x83\x01\xff\x00"
720
        },
721
        {
722
            .res = -EBADLENGTH,
723
            .length = 5,
724
            .encoded = "\x84\x01\x00\x01\x00"
725
        },
726
        {
727
            .res = -EOUTOFDATA,
728
            .length = 1,
729
            .encoded = "\x83"
730
        },
731
        {
732
            .res = -EBADLENGTH,
733
            .length = 2,
734
            .encoded = "\xff\x00"
735
        }
736
    };
737
    struct oer_c_source_l_t decoded;
738
    unsigned int i;
739

740 2
    for (i = 0; i < membersof(datas); i++) {
741 2
        memset(&decoded, 0, sizeof(decoded));
742 2
        ASSERT_EQ(oer_c_source_l_decode(&decoded,
743
                                        &datas[i].encoded[0],
744
                                        datas[i].length), datas[i].res);
745
    }
746
}
747

748 2
TEST(oer_c_source_o)
749
{
750
    int i;
751
    uint8_t encoded[263];
752
    struct oer_c_source_o_t decoded;
753

754
    /* Encode. */
755 2
    decoded.length = 260;
756

757 2
    for (i = 0; i < 260; i++) {
758 2
        decoded.elements[i] = true;
759
    }
760

761 2
    memset(&encoded[0], 0, sizeof(encoded));
762 2
    ASSERT_EQ(oer_c_source_o_encode(&encoded[0],
763
                                    sizeof(encoded),
764
                                    &decoded), sizeof(encoded));
765 2
    ASSERT_MEMORY_EQ(&encoded[0],
766
                     "\x02\x01\x04\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
767
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
768
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
769
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
770
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
771
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
772
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
773
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
774
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
775
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
776
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
777
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
778
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
779
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
780
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
781
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
782
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
783
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
784
                     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",
785
                     sizeof(encoded));
786

787
    /* Decode. */
788 2
    memset(&decoded, 0, sizeof(decoded));
789 2
    ASSERT_EQ(oer_c_source_o_decode(&decoded,
790
                                    &encoded[0],
791
                                    sizeof(encoded)), sizeof(encoded));
792

793 2
    ASSERT_EQ(decoded.length, 260);
794

795 2
    for (i = 0; i < 260; i++) {
796 2
        ASSERT_EQ(decoded.elements[i], true);
797
    }
798
}
799

800 2
TEST(oer_c_source_q_c256)
801
{
802
    uint8_t encoded[4];
803
    struct oer_c_source_q_t decoded;
804

805
    /* Encode. */
806 2
    decoded.choice = oer_c_source_q_choice_c256_e;
807 2
    decoded.value.c256 = true;
808

809 2
    memset(&encoded[0], 0, sizeof(encoded));
810 2
    ASSERT_EQ(oer_c_source_q_encode(&encoded[0],
811
                                    sizeof(encoded),
812
                                    &decoded), sizeof(encoded));
813 2
    ASSERT_MEMORY_EQ(&encoded[0], "\xbf\x81\x7f\xff", 4);
814

815
    /* Decode. */
816 2
    memset(&decoded, 0, sizeof(decoded));
817 2
    ASSERT_EQ(oer_c_source_q_decode(&decoded,
818
                                    &encoded[0],
819
                                    sizeof(encoded)), sizeof(encoded));
820

821 2
    ASSERT_EQ(decoded.choice, oer_c_source_q_choice_c256_e);
822 2
    ASSERT_EQ(decoded.value.c256, true);
823
}
824

825 2
TEST(oer_c_source_q_c257)
826
{
827
    uint8_t encoded[4];
828
    struct oer_c_source_q_t decoded;
829

830
    /* Encode. */
831 2
    decoded.choice = oer_c_source_q_choice_c257_e;
832 2
    decoded.value.c257 = true;
833

834 2
    memset(&encoded[0], 0, sizeof(encoded));
835 2
    ASSERT_EQ(oer_c_source_q_encode(&encoded[0],
836
                                    sizeof(encoded),
837
                                    &decoded), sizeof(encoded));
838 2
    ASSERT_MEMORY_EQ(&encoded[0], "\xbf\x82\x00\xff", 4);
839

840
    /* Decode. */
841 2
    memset(&decoded, 0, sizeof(decoded));
842 2
    ASSERT_EQ(oer_c_source_q_decode(&decoded,
843
                                    &encoded[0],
844
                                    sizeof(encoded)), sizeof(encoded));
845

846 2
    ASSERT_EQ(decoded.choice, oer_c_source_q_choice_c257_e);
847 2
    ASSERT_EQ(decoded.value.c257, true);
848
}
849

850 2
TEST(oer_c_source_x)
851
{
852
    struct data_t {
853
        int16_t decoded;
854
        uint8_t encoded[2];
855 2
    } datas[] = {
856
        {
857
            .decoded = -2,
858
            .encoded = "\xff\xfe"
859
        },
860
        {
861
            .decoded = 510,
862
            .encoded = "\x01\xfe"
863
        }
864
    };
865
    uint8_t encoded[2];
866
    struct oer_c_source_x_t decoded;
867
    unsigned int i;
868

869 2
    for (i = 0; i < membersof(datas); i++) {
870
        /* Encode. */
871 2
        decoded.value = datas[i].decoded;
872

873 2
        memset(&encoded[0], 0, sizeof(encoded));
874 2
        ASSERT_EQ(oer_c_source_x_encode(&encoded[0],
875
                                        sizeof(encoded),
876
                                        &decoded), sizeof(encoded));
877 2
        ASSERT_MEMORY_EQ(&encoded[0], &datas[i].encoded[0], sizeof(encoded));
878

879
        /* Decode. */
880 2
        memset(&decoded, 0, sizeof(decoded));
881 2
        ASSERT_EQ(oer_c_source_x_decode(&decoded,
882
                                        &encoded[0],
883
                                        sizeof(encoded)), sizeof(encoded));
884

885 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
886
    }
887
}
888

889 2
TEST(oer_c_source_y)
890
{
891
    struct data_t {
892
        uint16_t decoded;
893
        uint8_t encoded[2];
894 2
    } datas[] = {
895
        {
896
            .decoded = 10000,
897
            .encoded = "\x27\x10"
898
        },
899
        {
900
            .decoded = 10512,
901
            .encoded = "\x29\x10"
902
        }
903
    };
904
    uint8_t encoded[2];
905
    struct oer_c_source_y_t decoded;
906
    unsigned int i;
907

908 2
    for (i = 0; i < membersof(datas); i++) {
909
        /* Encode. */
910 2
        decoded.value = datas[i].decoded;
911

912 2
        memset(&encoded[0], 0, sizeof(encoded));
913 2
        ASSERT_EQ(oer_c_source_y_encode(&encoded[0],
914
                                        sizeof(encoded),
915
                                        &decoded), sizeof(encoded));
916 2
        ASSERT_MEMORY_EQ(&encoded[0], &datas[i].encoded[0], sizeof(encoded));
917

918
        /* Decode. */
919 2
        memset(&decoded, 0, sizeof(decoded));
920 2
        ASSERT_EQ(oer_c_source_y_decode(&decoded,
921
                                        &encoded[0],
922
                                        sizeof(encoded)), sizeof(encoded));
923

924 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
925
    }
926
}
927

928 2
TEST(oer_c_source_ab)
929
{
930
    uint8_t encoded[3];
931
    struct oer_c_source_ab_t decoded;
932

933
    /* Encode. */
934 2
    decoded.a = 0;
935 2
    decoded.b = 10300;
936

937 2
    memset(&encoded[0], 0, sizeof(encoded));
938 2
    ASSERT_EQ(oer_c_source_ab_encode(&encoded[0],
939
                                     sizeof(encoded),
940
                                     &decoded), sizeof(encoded));
941 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x00\x28\x3c", sizeof(encoded));
942

943
    /* Decode. */
944 2
    memset(&decoded, 0, sizeof(decoded));
945 2
    ASSERT_EQ(oer_c_source_ab_decode(&decoded,
946
                                     &encoded[0],
947
                                     sizeof(encoded)), sizeof(encoded));
948

949 2
    ASSERT_EQ(decoded.a, 0);
950 2
    ASSERT_EQ(decoded.b, 10300);
951
}
952

953 2
TEST(oer_c_source_ae)
954
{
955
    uint8_t encoded[3];
956
    struct oer_c_source_ae_t decoded;
957

958
    /* Encode. */
959 2
    decoded.is_a_present = true;
960 2
    decoded.a = false;
961 2
    decoded.b = true;
962 2
    decoded.c = false;
963

964 2
    memset(&encoded[0], 0, sizeof(encoded));
965 2
    ASSERT_EQ(oer_c_source_ae_encode(&encoded[0],
966
                                     sizeof(encoded),
967
                                     &decoded), sizeof(encoded));
968 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x40\x00\x00", sizeof(encoded));
969

970
    /* Decode. */
971 2
    memset(&decoded, 0, sizeof(decoded));
972 2
    ASSERT_EQ(oer_c_source_ae_decode(&decoded,
973
                                     &encoded[0],
974
                                     sizeof(encoded)), sizeof(encoded));
975

976 2
    ASSERT_EQ(decoded.a, false);
977 2
    ASSERT_EQ(decoded.b, true);
978 2
    ASSERT_EQ(decoded.c, false);
979
}
980

981 2
TEST(oer_c_source_af)
982
{
983
    uint8_t encoded[32];
984
    struct oer_c_source_af_t decoded;
985

986
    /* Encode. */
987 2
    decoded.a = true;
988 2
    decoded.b.c = true;
989 2
    decoded.is_b_addition_present = true;
990 2
    decoded.b.d = 17;
991 2
    decoded.b.is_d_addition_present = true;
992 2
    decoded.b.e = oer_c_source_ah_e_g_e;
993 2
    decoded.b.is_e_addition_present = true;
994 2
    decoded.e = 18;
995 2
    decoded.is_e_addition_present = true;
996 2
    decoded.f = 19;
997 2
    decoded.is_f_addition_present = true;
998 2
    decoded.g = 20;
999 2
    decoded.is_g_addition_present = true;
1000 2
    decoded.h = 21;
1001 2
    decoded.is_h_addition_present = true;
1002 2
    decoded.i = 22;
1003 2
    decoded.is_i_addition_present = true;
1004 2
    decoded.j = 23;
1005 2
    decoded.is_j_addition_present = true;
1006 2
    decoded.k = 24;
1007 2
    decoded.is_k_addition_present = true;
1008 2
    decoded.l = 25;
1009 2
    decoded.is_l_addition_present = true;
1010

1011 2
    memset(&encoded[0], 0, sizeof(encoded));
1012 2
    ASSERT_EQ(oer_c_source_af_encode(&encoded[0],
1013
                                     sizeof(encoded),
1014
                                     &decoded), sizeof(encoded));
1015 2
    ASSERT_MEMORY_EQ(&encoded[0],
1016
                     "\x80\xff\x03\x07\xff\x80\x09\x80\xff\x02\x06\xc0\x01\x11"
1017
                     "\x01\x01\x01\x12\x01\x13\x01\x14\x01\x15\x01\x16\x01\x17"
1018
                     "\x01\x18\x01\x19",
1019
                     sizeof(encoded));
1020

1021
    /* Decode. */
1022 2
    memset(&decoded, 0, sizeof(decoded));
1023 2
    ASSERT_EQ(oer_c_source_af_decode(&decoded,
1024
                                     &encoded[0],
1025
                                     sizeof(encoded)), sizeof(encoded));
1026

1027 2
    ASSERT_EQ(decoded.a, true);
1028 2
    ASSERT_TRUE(decoded.is_b_addition_present);
1029 2
    ASSERT_EQ(decoded.b.c, true);
1030 2
    ASSERT_TRUE(decoded.b.is_d_addition_present);
1031 2
    ASSERT_EQ(decoded.b.d, 17);
1032 2
    ASSERT_TRUE(decoded.b.is_e_addition_present);
1033 2
    ASSERT_EQ(decoded.b.e, oer_c_source_ah_e_g_e);
1034 2
    ASSERT_TRUE(decoded.is_e_addition_present);
1035 2
    ASSERT_EQ(decoded.e, 18);
1036 2
    ASSERT_TRUE(decoded.is_f_addition_present);
1037 2
    ASSERT_EQ(decoded.f, 19);
1038 2
    ASSERT_TRUE(decoded.is_g_addition_present);
1039 2
    ASSERT_EQ(decoded.g, 20);
1040 2
    ASSERT_TRUE(decoded.is_h_addition_present);
1041 2
    ASSERT_EQ(decoded.h, 21);
1042 2
    ASSERT_TRUE(decoded.is_i_addition_present);
1043 2
    ASSERT_EQ(decoded.i, 22);
1044 2
    ASSERT_TRUE(decoded.is_j_addition_present);
1045 2
    ASSERT_EQ(decoded.j, 23);
1046 2
    ASSERT_TRUE(decoded.is_k_addition_present);
1047 2
    ASSERT_EQ(decoded.k, 24);
1048 2
    ASSERT_TRUE(decoded.is_l_addition_present);
1049 2
    ASSERT_EQ(decoded.l, 25);
1050
}
1051

1052 2
TEST(oer_c_source_af_past)
1053
{
1054 2
    uint8_t encoded[12] = "\x80\xff\x02\x05\xe0\x02\x00\xff\x01\x12\x01\x13";
1055
    struct oer_c_source_af_t decoded;
1056

1057
    /* Decode. */
1058 2
    memset(&decoded, 0, sizeof(decoded));
1059 2
    ASSERT_EQ(oer_c_source_af_decode(&decoded,
1060
                                     &encoded[0],
1061
                                     sizeof(encoded)), sizeof(encoded));
1062

1063 2
    ASSERT_EQ(decoded.a, true);
1064 2
    ASSERT_TRUE(decoded.is_b_addition_present);
1065 2
    ASSERT_EQ(decoded.b.c, true);
1066 2
    ASSERT_FALSE(decoded.b.is_d_addition_present);
1067 2
    ASSERT_TRUE(decoded.is_e_addition_present);
1068 2
    ASSERT_EQ(decoded.e, 18);
1069 2
    ASSERT_TRUE(decoded.is_f_addition_present);
1070 2
    ASSERT_EQ(decoded.f, 19);
1071 2
    ASSERT_FALSE(decoded.is_g_addition_present);
1072 2
    ASSERT_FALSE(decoded.is_h_addition_present);
1073 2
    ASSERT_FALSE(decoded.is_i_addition_present);
1074 2
    ASSERT_FALSE(decoded.is_j_addition_present);
1075 2
    ASSERT_FALSE(decoded.is_k_addition_present);
1076 2
    ASSERT_FALSE(decoded.is_l_addition_present);
1077
}
1078

1079 2
TEST(oer_c_source_af_future)
1080
{
1081 2
    uint8_t encoded[37] = "\x80\xff\x04\x02\xff\xc0\x00\x0b\x80\xff\x02\x03"
1082
        "\xe0\x01\x11\x01\x01\x01\xab\x01\x12\x01\x13\x01\x14\x01\x15\x01\x16\x01"
1083
        "\x17\x01\x18\x01\x19\x01\x1a";
1084
    struct oer_c_source_af_t decoded;
1085

1086
    /* Decode. */
1087 2
    memset(&decoded, 0, sizeof(decoded));
1088 2
    ASSERT_EQ(oer_c_source_af_decode(&decoded,
1089
                                     &encoded[0],
1090
                                     sizeof(encoded)), sizeof(encoded));
1091

1092 2
    ASSERT_EQ(decoded.a, true);
1093 2
    ASSERT_TRUE(decoded.is_b_addition_present);
1094 2
    ASSERT_EQ(decoded.b.c, true);
1095 2
    ASSERT_TRUE(decoded.b.is_d_addition_present);
1096 2
    ASSERT_EQ(decoded.b.d, 17);
1097 2
    ASSERT_TRUE(decoded.is_e_addition_present);
1098 2
    ASSERT_EQ(decoded.e, 18);
1099 2
    ASSERT_TRUE(decoded.is_f_addition_present);
1100 2
    ASSERT_EQ(decoded.f, 19);
1101 2
    ASSERT_TRUE(decoded.is_g_addition_present);
1102 2
    ASSERT_EQ(decoded.g, 20);
1103 2
    ASSERT_TRUE(decoded.is_h_addition_present);
1104 2
    ASSERT_EQ(decoded.h, 21);
1105 2
    ASSERT_TRUE(decoded.is_i_addition_present);
1106 2
    ASSERT_EQ(decoded.i, 22);
1107 2
    ASSERT_TRUE(decoded.is_j_addition_present);
1108 2
    ASSERT_EQ(decoded.j, 23);
1109 2
    ASSERT_TRUE(decoded.is_k_addition_present);
1110 2
    ASSERT_EQ(decoded.k, 24);
1111 2
    ASSERT_TRUE(decoded.is_l_addition_present);
1112 2
    ASSERT_EQ(decoded.l, 25);
1113
}
1114

1115 2
TEST(oer_c_source_ag)
1116
{
1117
    uint8_t encoded[36];
1118
    struct oer_c_source_ag_t decoded;
1119

1120
    /* Encode. */
1121 2
    decoded.a = true;
1122 2
    decoded.b.length = 2;
1123 2
    memcpy(&decoded.b.buf[0], "\x84\x55", 2);
1124 2
    decoded.is_b_addition_present = true;
1125 2
    decoded.c.length = 4;
1126 2
    decoded.c.elements[0] = true;
1127 2
    decoded.c.elements[1] = false;
1128 2
    decoded.c.elements[2] = true;
1129 2
    decoded.c.elements[3] = false;
1130 2
    decoded.is_c_addition_present = true;
1131 2
    decoded.d = oer_c_source_ag_d_f_e;
1132 2
    decoded.is_d_addition_present = true;
1133 2
    decoded.is_h_addition_present = true;
1134 2
    decoded.i = 1.0f;
1135 2
    decoded.is_i_addition_present = true;
1136 2
    decoded.j.choice = oer_c_source_ag_j_choice_k_e;
1137 2
    decoded.j.value.k = 60693;
1138 2
    decoded.is_j_addition_present = true;
1139 2
    memcpy(&decoded.m.buf[0], "\xf0\xf1\xf2\xf3\xf4", 5);
1140 2
    decoded.is_m_addition_present = true;
1141

1142 2
    memset(&encoded[0], 0, sizeof(encoded));
1143 2
    ASSERT_EQ(oer_c_source_ag_encode(&encoded[0],
1144
                                     sizeof(encoded),
1145
                                     &decoded), sizeof(encoded));
1146

1147 2
    ASSERT_MEMORY_EQ(&encoded[0],
1148
                     "\x80\xff\x02\x01\xfe\x03\x02\x84\x55\x06\x01\x04\xff\x00"
1149
                     "\xff\x00\x03\x82\x01\x00\x00\x04\x3f\x80\x00\x00\x03\x80"
1150
                     "\xed\x15\x05\xf0\xf1\xf2\xf3\xf4",
1151
                     sizeof(encoded));
1152

1153
    /* Decode. */
1154 2
    memset(&decoded, 0, sizeof(decoded));
1155 2
    ASSERT_EQ(oer_c_source_ag_decode(&decoded,
1156
                                     &encoded[0],
1157
                                     sizeof(encoded)), sizeof(encoded));
1158

1159 2
    ASSERT_EQ(decoded.a, true);
1160 2
    ASSERT_TRUE(decoded.is_b_addition_present);
1161 2
    ASSERT_EQ(decoded.b.length, 2);
1162 2
    ASSERT_MEMORY_EQ(&decoded.b.buf[0], "\x84\x55", 2);
1163 2
    ASSERT_TRUE(decoded.is_c_addition_present);
1164 2
    ASSERT_EQ(decoded.c.length, 4);
1165 2
    ASSERT_TRUE(decoded.c.elements[0]);
1166 2
    ASSERT_FALSE(decoded.c.elements[1]);
1167 2
    ASSERT_TRUE(decoded.c.elements[2]);
1168 2
    ASSERT_FALSE(decoded.c.elements[3]);
1169 2
    ASSERT_TRUE(decoded.is_d_addition_present);
1170 2
    ASSERT_EQ(decoded.d, oer_c_source_ag_d_f_e);
1171 2
    ASSERT_TRUE(decoded.is_h_addition_present);
1172 2
    ASSERT_TRUE(decoded.is_i_addition_present);
1173 2
    ASSERT_TRUE(decoded.i >= 1.0f);
1174 2
    ASSERT_TRUE(decoded.i <= 1.0f);
1175 2
    ASSERT_TRUE(decoded.is_j_addition_present);
1176 2
    ASSERT_EQ(decoded.j.choice, oer_c_source_ag_j_choice_k_e);
1177 2
    ASSERT_EQ(decoded.j.value.k, 60693);
1178 2
    ASSERT_TRUE(decoded.is_m_addition_present);
1179 2
    ASSERT_MEMORY_EQ(&decoded.m.buf[0], "\xf0\xf1\xf2\xf3\xf4", 5);
1180
}
1181

1182 2
TEST(oer_c_source_an)
1183
{
1184
    struct data_t {
1185
        int32_t decoded;
1186
        uint8_t encoded[5];
1187
        uint8_t encoded_length;
1188 2
    } datas[] = {
1189
        {
1190
            .decoded = oer_c_source_an_a_e,
1191
            .encoded = "\x84\xff\x00\x00\x00",
1192
            .encoded_length = 5
1193
        },
1194
        {
1195
            .decoded = oer_c_source_an_b_e,
1196
            .encoded = "\x83\x80\x00\x00",
1197
            .encoded_length = 4
1198
        },
1199
        {
1200
            .decoded = oer_c_source_an_c_e,
1201
            .encoded = "\x83\xff\x00\x00",
1202
            .encoded_length = 4
1203
        },
1204
        {
1205
            .decoded = oer_c_source_an_d_e,
1206
            .encoded = "\x82\x80\x00",
1207
            .encoded_length = 3
1208
        },
1209
        {
1210
            .decoded = oer_c_source_an_e_e,
1211
            .encoded = "\x81\x80",
1212
            .encoded_length = 2
1213
        },
1214
        {
1215
            .decoded = oer_c_source_an_f_e,
1216
            .encoded = "\x00",
1217
            .encoded_length = 1
1218
        },
1219
        {
1220
            .decoded = oer_c_source_an_g_e,
1221
            .encoded = "\x7f",
1222
            .encoded_length = 1
1223
        },
1224
        {
1225
            .decoded = oer_c_source_an_h_e,
1226
            .encoded = "\x82\x00\x80",
1227
            .encoded_length = 3
1228
        },
1229
        {
1230
            .decoded = oer_c_source_an_i_e,
1231
            .encoded = "\x82\x7f\xff",
1232
            .encoded_length = 3
1233
        },
1234
        {
1235
            .decoded = oer_c_source_an_j_e,
1236
            .encoded = "\x83\x01\x00\x00",
1237
            .encoded_length = 4
1238
        },
1239
        {
1240
            .decoded = oer_c_source_an_k_e,
1241
            .encoded = "\x84\x01\x00\x00\x00",
1242
            .encoded_length = 5
1243
        },
1244
    };
1245
    uint8_t encoded[5];
1246
    struct oer_c_source_an_t decoded;
1247
    unsigned int i;
1248

1249 2
    for (i = 0; i < membersof(datas); i++) {
1250
        /* Encode. */
1251 2
        decoded.value = datas[i].decoded;
1252

1253 2
        memset(&encoded[0], 0, sizeof(encoded));
1254 2
        ASSERT_EQ(oer_c_source_an_encode(&encoded[0],
1255
                                         sizeof(encoded),
1256
                                         &decoded), datas[i].encoded_length);
1257 2
        ASSERT_MEMORY_EQ(&encoded[0],
1258
                         &datas[i].encoded[0],
1259
                         datas[i].encoded_length);
1260

1261
        /* Decode. */
1262 2
        memset(&decoded, 0, sizeof(decoded));
1263 2
        ASSERT_EQ(oer_c_source_an_decode(&decoded,
1264
                                         &encoded[0],
1265
                                         datas[i].encoded_length),
1266
                                         datas[i].encoded_length);
1267

1268 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
1269
    }
1270
}
1271

1272 2
TEST(oer_c_source_ao)
1273
{
1274
    uint8_t encoded[17];
1275
    struct oer_c_source_ao_t decoded;
1276

1277
    /* Encode. */
1278 2
    decoded.a = OER_C_SOURCE_AO_A_C;
1279 2
    decoded.b = OER_C_SOURCE_AO_B_A;
1280 2
    decoded.c = 0x50;
1281 2
    decoded.d = OER_C_SOURCE_AO_D_B;
1282 2
    decoded.e = OER_C_SOURCE_AO_E_C;
1283

1284 2
    memset(&encoded[0], 0, sizeof(encoded));
1285 2
    ASSERT_EQ(oer_c_source_ao_encode(&encoded[0],
1286
                                     sizeof(encoded),
1287
                                     &decoded), sizeof(encoded));
1288 2
    ASSERT_MEMORY_EQ(&encoded[0],
1289
                     "\x01\x80\x00\x00\x50\x20\x00\x00\x00\x00\x00\x00\x00\x00"
1290
                     "\x00\x00\x01",
1291
                     sizeof(encoded));
1292

1293
    /* Decode. */
1294 2
    memset(&decoded, 0, sizeof(decoded));
1295 2
    ASSERT_EQ(oer_c_source_ao_decode(&decoded,
1296
                                     &encoded[0],
1297
                                     sizeof(encoded)), sizeof(encoded));
1298

1299 2
    ASSERT_EQ(decoded.a, OER_C_SOURCE_AO_A_C);
1300 2
    ASSERT_EQ(decoded.b, OER_C_SOURCE_AO_B_A);
1301 2
    ASSERT_EQ(decoded.c, 0x50);
1302 2
    ASSERT_EQ(decoded.d, OER_C_SOURCE_AO_D_B);
1303 2
    ASSERT_EQ(decoded.e, OER_C_SOURCE_AO_E_C);
1304
}
1305

1306 2
TEST(oer_c_source_ap)
1307
{
1308 2
    uint8_t encoded[3] = "\x80\x10\x01";
1309
    struct oer_c_source_ap_t decoded;
1310

1311 2
    decoded.b.a = 16;
1312 2
    decoded.c.value = oer_c_ref_referenced_enum_b_e;
1313 2
    decoded.d = 1;
1314

1315
    /* Decode. */
1316 2
    memset(&decoded, 0, sizeof(decoded));
1317 2
    ASSERT_EQ(oer_c_source_ap_decode(&decoded,
1318
                                     &encoded[0],
1319
                                     sizeof(encoded)), sizeof(encoded));
1320

1321 2
    ASSERT_EQ(decoded.b.a, 16);
1322 2
    ASSERT_EQ(decoded.c.value, oer_c_ref_referenced_enum_b_e);
1323 2
    ASSERT_EQ(decoded.d, 1);
1324
}
1325

1326 2
TEST(oer_c_source_aq)
1327
{
1328 2
    uint8_t encoded[4] = "\x00\x01\x86\xa1";
1329
    struct oer_c_source_aq_t decoded;
1330

1331 2
    decoded.value = 100001;
1332

1333
    /* Decode. */
1334 2
    memset(&decoded, 0, sizeof(decoded));
1335 2
    ASSERT_EQ(oer_c_source_aq_decode(&decoded,
1336
                                     &encoded[0],
1337
                                     sizeof(encoded)), sizeof(encoded));
1338

1339 2
    ASSERT_EQ(decoded.value, 100001);
1340
}
1341

1342 2
TEST(oer_c_source_ag_erroneous_data)
1343
{
1344
    struct oer_c_source_ag_t decoded;
1345

1346
    // Wrong length determinant, valid unused bits
1347 2
    uint8_t encoded[4] = "\x80\xff\xff\x00";
1348 2
    ASSERT_EQ(oer_c_source_ag_decode(&decoded,
1349
                                     &encoded[0],
1350
                                     sizeof(encoded)), -EOUTOFDATA);
1351

1352
    // Valid length determinant, invalid unused bits
1353 2
    uint8_t encoded2[4] = "\x80\xff\x03\x0a";
1354 2
    ASSERT_EQ(oer_c_source_ag_decode(&decoded,
1355
                                     &encoded2[0],
1356
                                     sizeof(encoded2)), -EBADLENGTH);
1357

1358
    // Invalid addition length of unknown future additions
1359 2
    uint8_t encoded3[6] = "\x80\xff\x02\x00\x01\xff";
1360 2
    ASSERT_EQ(oer_c_source_ag_decode(&decoded,
1361
                                     &encoded3[0],
1362
                                     sizeof(encoded3)), -EOUTOFDATA);
1363
}
1364

1365 2
TEST(oer_programming_types_float)
1366
{
1367
    uint8_t encoded[4];
1368
    struct oer_programming_types_float_t decoded;
1369

1370
    /* Encode. */
1371 2
    decoded.value = 1.0f;
1372

1373 2
    memset(&encoded[0], 0, sizeof(encoded));
1374 2
    ASSERT_EQ(oer_programming_types_float_encode(&encoded[0],
1375
                                                 sizeof(encoded),
1376
                                                 &decoded), sizeof(encoded));
1377 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x3f\x80\x00\x00", sizeof(encoded));
1378

1379
    /* Decode. */
1380 2
    memset(&decoded, 0, sizeof(decoded));
1381 2
    ASSERT_EQ(oer_programming_types_float_decode(&decoded,
1382
                                                 &encoded[0],
1383
                                                 sizeof(encoded)), sizeof(encoded));
1384

1385 2
    ASSERT_TRUE(fequal(decoded.value, 1.0f));
1386
}
1387

1388 2
TEST(oer_programming_types_double)
1389
{
1390
    uint8_t encoded[8];
1391
    struct oer_programming_types_double_t decoded;
1392

1393
    /* Encode. */
1394 2
    decoded.value = 1.0;
1395

1396 2
    memset(&encoded[0], 0, sizeof(encoded));
1397 2
    ASSERT_EQ(oer_programming_types_double_encode(&encoded[0],
1398
                                                  sizeof(encoded),
1399
                                                  &decoded), sizeof(encoded));
1400 2
    ASSERT_MEMORY_EQ(&encoded[0],
1401
                     "\x3f\xf0\x00\x00\x00\x00\x00\x00",
1402
                     sizeof(encoded));
1403

1404
    /* Decode. */
1405 2
    memset(&decoded, 0, sizeof(decoded));
1406 2
    ASSERT_EQ(oer_programming_types_double_decode(&decoded,
1407
                                                  &encoded[0],
1408
                                                  sizeof(encoded)), sizeof(encoded));
1409

1410 2
    ASSERT_TRUE(fequal(decoded.value, 1.0));
1411
}

Read our documentation on viewing source code .

Loading