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 "uper.h"
8
#include "boolean_uper.h"
9
#include "octet_string_uper.h"
10

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

13 2
TEST(uper_c_source_a)
14
{
15
    uint8_t encoded[42];
16
    struct uper_c_source_a_t decoded;
17

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

30 2
    memset(&encoded[0], 0, sizeof(encoded));
31 2
    ASSERT_EQ(uper_c_source_a_encode(&encoded[0],
32
                                     sizeof(encoded),
33
                                     &decoded), sizeof(encoded));
34 2
    ASSERT_MEMORY_EQ(&encoded[0],
35
                     "\x7f\x7f\xfe\x7f\xff\xff\xfd\x7f\xff\xff\xff\xff\xff\xff\xfc"
36
                     "\x01\x00\x02\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04"
37
                     "\x82\x82\x82\x82\x82\x82\x82\x82\x82\x82\x82\x80",
38
                     sizeof(encoded));
39

40
    /* Decode. */
41 2
    memset(&decoded, 0, sizeof(decoded));
42 2
    ASSERT_EQ(uper_c_source_a_decode(&decoded,
43
                                     &encoded[0],
44
                                     sizeof(encoded)), sizeof(encoded));
45

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

60 2
TEST(uper_c_source_a_encode_error_no_mem)
61
{
62
    uint8_t encoded[41];
63
    struct uper_c_source_a_t decoded;
64

65 2
    decoded.a = -1;
66 2
    decoded.b = -2;
67 2
    decoded.c = -3;
68 2
    decoded.d = -4;
69 2
    decoded.e = 1;
70 2
    decoded.f = 2;
71 2
    decoded.g = 3;
72 2
    decoded.h = 4;
73 2
    decoded.i = true;
74 2
    memset(&decoded.j.buf[0], 5, sizeof(decoded.j.buf));
75

76 2
    ASSERT_EQ(uper_c_source_a_encode(&encoded[0],
77
                                     sizeof(encoded),
78
                                     &decoded), -ENOMEM);
79
}
80

81 2
TEST(uper_c_source_a_decode_error_out_of_data)
82
{
83 2
    uint8_t encoded[41] =
84
        "\x7f\x7f\xfe\x7f\xff\xff\xfd\x7f\xff\xff\xff\xff\xff\xff\xfc"
85
        "\x01\x00\x02\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04"
86
        "\x82\x82\x82\x82\x82\x82\x82\x82\x82\x82\x82";
87
    struct uper_c_source_a_t decoded;
88

89 2
    ASSERT_EQ(uper_c_source_a_decode(&decoded,
90
                                     &encoded[0],
91
                                     sizeof(encoded)), -EOUTOFDATA);
92
}
93

94 2
TEST(uper_c_source_b_choice_a)
95
{
96
    uint8_t encoded[2];
97
    struct uper_c_source_b_t decoded;
98

99
    /* Encode. */
100 2
    decoded.choice = uper_c_source_b_choice_a_e;
101 2
    decoded.value.a = -10;
102

103 2
    memset(&encoded[0], 0, sizeof(encoded));
104 2
    ASSERT_EQ(uper_c_source_b_encode(&encoded[0],
105
                                     sizeof(encoded),
106
                                     &decoded), sizeof(encoded));
107 2
    ASSERT_MEMORY_EQ(&encoded[0],
108
                     "\x1d\x80",
109
                     sizeof(encoded));
110

111
    /* Decode. */
112 2
    memset(&decoded, 0, sizeof(decoded));
113 2
    ASSERT_EQ(uper_c_source_b_decode(&decoded,
114
                                     &encoded[0],
115
                                     sizeof(encoded)), sizeof(encoded));
116

117 2
    ASSERT_EQ(decoded.choice, uper_c_source_b_choice_a_e);
118 2
    ASSERT_EQ(decoded.value.a, -10);
119
}
120

121 2
TEST(uper_c_source_b_choice_b)
122
{
123
    uint8_t encoded[42];
124
    struct uper_c_source_b_t decoded;
125

126
    /* Encode. */
127 2
    decoded.choice = uper_c_source_b_choice_b_e;
128 2
    decoded.value.b.a = -1;
129 2
    decoded.value.b.b = -2;
130 2
    decoded.value.b.c = -3;
131 2
    decoded.value.b.d = -4;
132 2
    decoded.value.b.e = 1;
133 2
    decoded.value.b.f = 2;
134 2
    decoded.value.b.g = 3;
135 2
    decoded.value.b.h = 4;
136 2
    decoded.value.b.i = true;
137 2
    memset(&decoded.value.b.j.buf[0], 5, sizeof(decoded.value.b.j.buf));
138

139 2
    memset(&encoded[0], 0, sizeof(encoded));
140 2
    ASSERT_EQ(uper_c_source_b_encode(&encoded[0],
141
                                     sizeof(encoded),
142
                                     &decoded), sizeof(encoded));
143 2
    ASSERT_MEMORY_EQ(&encoded[0],
144
                     "\x5f\xdf\xff\x9f\xff\xff\xff\x5f\xff\xff\xff\xff\xff\xff\xff"
145
                     "\x00\x40\x00\x80\x00\x00\x00\xc0\x00\x00\x00\x00\x00\x00\x01"
146
                     "\x20\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0",
147
                     sizeof(encoded));
148

149
    /* Decode. */
150 2
    memset(&decoded, 0, sizeof(decoded));
151 2
    ASSERT_EQ(uper_c_source_b_decode(&decoded,
152
                                     &encoded[0],
153
                                     sizeof(encoded)), sizeof(encoded));
154

155 2
    ASSERT_EQ(decoded.choice, uper_c_source_b_choice_b_e);
156 2
    ASSERT_EQ(decoded.value.b.a, -1);
157 2
    ASSERT_EQ(decoded.value.b.b, -2);
158 2
    ASSERT_EQ(decoded.value.b.c, -3);
159 2
    ASSERT_EQ(decoded.value.b.d, -4);
160 2
    ASSERT_EQ(decoded.value.b.e, 1);
161 2
    ASSERT_EQ(decoded.value.b.f, 2);
162 2
    ASSERT_EQ(decoded.value.b.g, 3);
163 2
    ASSERT_EQ(decoded.value.b.h, 4);
164 2
    ASSERT_TRUE(decoded.value.b.i);
165 2
    ASSERT_MEMORY_EQ(&decoded.value.b.j.buf[0],
166
                     "\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05",
167
                     sizeof(decoded.value.b.j.buf));
168
}
169

170 2
TEST(uper_c_source_b_decode_error_bad_choice)
171
{
172 2
    uint8_t encoded[2] = "\xdd\x80";
173
    struct uper_c_source_b_t decoded;
174

175 2
    ASSERT_EQ(uper_c_source_b_decode(&decoded,
176
                                     &encoded[0],
177
                                     sizeof(encoded)), -EBADCHOICE);
178
}
179

180 2
TEST(uper_c_source_c_empty)
181
{
182
    uint8_t encoded[1];
183
    struct uper_c_source_c_t decoded;
184

185
    /* Encode. */
186 2
    decoded.length = 0;
187

188
    memset(&encoded[0], 0, sizeof(encoded));
189 2
    ASSERT_EQ(uper_c_source_c_encode(&encoded[0],
190
                                     sizeof(encoded),
191
                                     &decoded), sizeof(encoded));
192 2
    ASSERT_MEMORY_EQ(&encoded[0],
193
                     "\x00",
194
                     sizeof(encoded));
195

196
    /* Decode. */
197 2
    memset(&decoded, 0, sizeof(decoded));
198 2
    ASSERT_EQ(uper_c_source_c_decode(&decoded,
199
                                     &encoded[0],
200
                                     sizeof(encoded)), sizeof(encoded));
201

202 2
    ASSERT_EQ(decoded.length, 0);
203
}
204

205 2
TEST(uper_c_source_c_2_elements)
206
{
207
    uint8_t encoded[3];
208
    struct uper_c_source_c_t decoded;
209

210
    /* Encode. */
211 2
    decoded.length = 2;
212 2
    decoded.elements[0].choice = uper_c_source_b_choice_a_e;
213 2
    decoded.elements[0].value.a = -11;
214 2
    decoded.elements[1].choice = uper_c_source_b_choice_a_e;
215 2
    decoded.elements[1].value.a = 13;
216

217 2
    memset(&encoded[0], 0, sizeof(encoded));
218 2
    ASSERT_EQ(uper_c_source_c_encode(&encoded[0],
219
                                     sizeof(encoded),
220
                                     &decoded), sizeof(encoded));
221 2
    ASSERT_MEMORY_EQ(&encoded[0],
222
                     "\x87\x52\x34",
223
                     sizeof(encoded));
224

225
    /* Decode. */
226 2
    memset(&decoded, 0, sizeof(decoded));
227 2
    ASSERT_EQ(uper_c_source_c_decode(&decoded,
228
                                     &encoded[0],
229
                                     sizeof(encoded)), sizeof(encoded));
230

231 2
    ASSERT_EQ(decoded.length, 2);
232 2
    ASSERT_EQ(decoded.elements[0].choice, uper_c_source_b_choice_a_e);
233 2
    ASSERT_EQ(decoded.elements[0].value.a, -11);
234 2
    ASSERT_EQ(decoded.elements[1].choice, uper_c_source_b_choice_a_e);
235 2
    ASSERT_EQ(decoded.elements[1].value.a, 13);
236
}
237

238 2
TEST(uper_c_source_c_decode_error_bad_length)
239
{
240 2
    uint8_t encoded[4] = "\xc7\x52\x34\x00";
241
    struct uper_c_source_c_t decoded;
242

243 2
    ASSERT_EQ(uper_c_source_c_decode(&decoded,
244
                                     &encoded[0],
245
                                     sizeof(encoded)), -EBADLENGTH);
246
}
247

248 2
TEST(uper_c_source_d_all_present)
249
{
250
    uint8_t encoded[10];
251
    struct uper_c_source_d_t decoded;
252

253
    /* Encode. */
254 2
    decoded.length = 1;
255 2
    decoded.elements[0].a.b.choice = uper_c_source_d_a_b_choice_c_e;
256 2
    decoded.elements[0].a.b.value.c = 0;
257 2
    decoded.elements[0].a.e.length = 3;
258 2
    decoded.elements[0].g.h = uper_c_source_d_g_h_j_e;
259 2
    decoded.elements[0].g.l.length = 2;
260 2
    decoded.elements[0].g.l.buf[0] = 0x54;
261 2
    decoded.elements[0].g.l.buf[1] = 0x55;
262 2
    decoded.elements[0].m.is_n_present = true;
263 2
    decoded.elements[0].m.n = false;
264 2
    decoded.elements[0].m.o = 2;
265 2
    decoded.elements[0].m.is_p_present = true;
266 2
    memset(&decoded.elements[0].m.p.q.buf[0],
267
           3,
268
           sizeof(decoded.elements[0].m.p.q.buf));
269 2
    decoded.elements[0].m.p.is_r_present = true;
270 2
    decoded.elements[0].m.p.r = true;
271 2
    decoded.elements[0].m.s = true;
272

273 2
    ASSERT_EQ(uper_c_source_d_encode(&encoded[0],
274
                                     sizeof(encoded),
275
                                     &decoded), sizeof(encoded));
276 2
    ASSERT_MEMORY_EQ(&encoded[0],
277
                     "\x00\xaa\x2a\xfa\x40\xc0\xc0\xc0\xc0\xf0",
278
                     sizeof(encoded));
279

280
    /* Decode. */
281 2
    memset(&decoded, 0, sizeof(decoded));
282 2
    ASSERT_EQ(uper_c_source_d_decode(&decoded,
283
                                     &encoded[0],
284
                                     sizeof(encoded)), sizeof(encoded));
285

286 2
    ASSERT_EQ(decoded.length, 1);
287 2
    ASSERT_EQ(decoded.elements[0].a.b.choice, uper_c_source_d_a_b_choice_c_e);
288 2
    ASSERT_EQ(decoded.elements[0].a.b.value.c, 0);
289 2
    ASSERT_EQ(decoded.elements[0].a.e.length, 3);
290 2
    ASSERT_EQ(decoded.elements[0].g.h, uper_c_source_d_g_h_j_e);
291 2
    ASSERT_EQ(decoded.elements[0].g.l.length, 2);
292 2
    ASSERT_EQ(decoded.elements[0].g.l.buf[0], 0x54);
293 2
    ASSERT_EQ(decoded.elements[0].g.l.buf[1], 0x55);
294 2
    ASSERT_TRUE(decoded.elements[0].m.is_n_present);
295 2
    ASSERT_EQ(decoded.elements[0].m.n, false);
296 2
    ASSERT_EQ(decoded.elements[0].m.o, 2);
297 2
    ASSERT_TRUE(decoded.elements[0].m.is_p_present);
298 2
    ASSERT_MEMORY_EQ(&decoded.elements[0].m.p.q.buf[0],
299
                     "\x03\x03\x03\x03\x03",
300
                     sizeof(decoded.elements[0].m.p.q.buf));
301 2
    ASSERT_TRUE(decoded.elements[0].m.p.is_r_present);
302 2
    ASSERT_EQ(decoded.elements[0].m.p.r, true);
303 2
    ASSERT_EQ(decoded.elements[0].m.s, true);
304
}
305

306 2
TEST(uper_c_source_d_some_missing)
307
{
308
    uint8_t encoded[8];
309
    struct uper_c_source_d_t decoded;
310

311
    /* Encode. */
312 2
    decoded.length = 1;
313 2
    decoded.elements[0].a.b.choice = uper_c_source_d_a_b_choice_d_e;
314 2
    decoded.elements[0].a.b.value.d = false;
315 2
    decoded.elements[0].a.e.length = 3;
316 2
    decoded.elements[0].g.h = uper_c_source_d_g_h_k_e;
317 2
    decoded.elements[0].g.l.length = 1;
318 2
    decoded.elements[0].g.l.buf[0] = 0x54;
319 2
    decoded.elements[0].m.is_n_present = false;
320
    /* Default value 3. */
321 2
    decoded.elements[0].m.o = 3;
322 2
    decoded.elements[0].m.is_p_present = true;
323 2
    memset(&decoded.elements[0].m.p.q.buf[0],
324
           3,
325
           sizeof(decoded.elements[0].m.p.q.buf));
326 2
    decoded.elements[0].m.p.is_r_present = false;
327 2
    decoded.elements[0].m.s = false;
328

329 2
    memset(&encoded[0], 0, sizeof(encoded));
330 2
    ASSERT_EQ(uper_c_source_d_encode(&encoded[0],
331
                                     sizeof(encoded),
332
                                     &decoded), sizeof(encoded));
333 2
    ASSERT_MEMORY_EQ(&encoded[0],
334
                     "\x09\x8a\x84\x03\x03\x03\x03\x03",
335
                     sizeof(encoded));
336

337
    /* Decode. */
338 2
    memset(&decoded, 0, sizeof(decoded));
339 2
    ASSERT_EQ(uper_c_source_d_decode(&decoded,
340
                                     &encoded[0],
341
                                     sizeof(encoded)), sizeof(encoded));
342

343 2
    ASSERT_EQ(decoded.length, 1);
344 2
    ASSERT_EQ(decoded.elements[0].a.b.choice, uper_c_source_d_a_b_choice_d_e);
345 2
    ASSERT_EQ(decoded.elements[0].a.b.value.d, false);
346 2
    ASSERT_EQ(decoded.elements[0].a.e.length, 3);
347 2
    ASSERT_EQ(decoded.elements[0].g.h, uper_c_source_d_g_h_k_e);
348 2
    ASSERT_EQ(decoded.elements[0].g.l.length, 1);
349 2
    ASSERT_EQ(decoded.elements[0].g.l.buf[0], 0x54);
350 2
    ASSERT_FALSE(decoded.elements[0].m.is_n_present);
351 2
    ASSERT_EQ(decoded.elements[0].m.o, 3);
352 2
    ASSERT_TRUE(decoded.elements[0].m.is_p_present);
353 2
    ASSERT_MEMORY_EQ(&decoded.elements[0].m.p.q.buf[0],
354
                     "\x03\x03\x03\x03\x03",
355
                     sizeof(decoded.elements[0].m.p.q.buf));
356 2
    ASSERT_FALSE(decoded.elements[0].m.p.is_r_present);
357 2
    ASSERT_EQ(decoded.elements[0].m.p.r, false);
358
}
359

360 2
TEST(uper_c_source_d_decode_error_bad_enum)
361
{
362 2
    uint8_t encoded[10] = "\x01\xd5\x15\x7a\x40\xc0\xc0\xc0\xc0\xe0";
363
    struct uper_c_source_d_t decoded;
364

365 2
    ASSERT_EQ(uper_c_source_d_decode(&decoded,
366
                                     &encoded[0],
367
                                     sizeof(encoded)), -EBADENUM);
368
}
369

370 2
TEST(uper_c_source_e)
371
{
372
    uint8_t encoded[1];
373
    struct uper_c_source_e_t decoded;
374

375
    /* Encode. */
376 2
    decoded.a.choice = uper_c_source_e_a_choice_b_e;
377 2
    decoded.a.value.b.choice = uper_c_source_e_a_b_choice_c_e;
378 2
    decoded.a.value.b.value.c = true;
379

380
    memset(&encoded[0], 0, sizeof(encoded));
381 2
    ASSERT_EQ(uper_c_source_e_encode(&encoded[0],
382
                                     sizeof(encoded),
383
                                     &decoded), sizeof(encoded));
384 2
    ASSERT_MEMORY_EQ(&encoded[0],
385
                     "\x80",
386
                     sizeof(encoded));
387

388
    /* Decode. */
389 2
    memset(&decoded, 0, sizeof(decoded));
390 2
    ASSERT_EQ(uper_c_source_e_decode(&decoded,
391
                                     &encoded[0],
392
                                     sizeof(encoded)), sizeof(encoded));
393

394 2
    ASSERT_EQ(decoded.a.choice, uper_c_source_e_a_choice_b_e);
395 2
    ASSERT_EQ(decoded.a.value.b.choice, uper_c_source_e_a_b_choice_c_e);
396 2
    ASSERT_EQ(decoded.a.value.b.value.c, true);
397
}
398

399 2
TEST(uper_c_source_f)
400
{
401
    uint8_t encoded[1];
402
    struct uper_c_source_f_t decoded;
403

404
    /* Encode. */
405 2
    decoded.length = 2;
406 2
    decoded.elements[0].elements[0] = false;
407 2
    decoded.elements[1].elements[0] = true;
408

409
    memset(&encoded[0], 0, sizeof(encoded));
410 2
    ASSERT_EQ(uper_c_source_f_encode(&encoded[0],
411
                                     sizeof(encoded),
412
                                     &decoded), sizeof(encoded));
413 2
    ASSERT_MEMORY_EQ(&encoded[0],
414
                     "\xa0",
415
                     sizeof(encoded));
416

417
    /* Decode. */
418 2
    memset(&decoded, 0, sizeof(decoded));
419 2
    ASSERT_EQ(uper_c_source_f_decode(&decoded,
420
                                     &encoded[0],
421
                                     sizeof(encoded)), sizeof(encoded));
422

423 2
    ASSERT_EQ(decoded.length, 2);
424 2
    ASSERT_EQ(decoded.elements[0].elements[0], false);
425 2
    ASSERT_EQ(decoded.elements[1].elements[0], true);
426
}
427

428 2
TEST(uper_c_source_g)
429
{
430
    uint8_t encoded[2];
431
    struct uper_c_source_g_t decoded;
432

433
    /* Encode. */
434 2
    decoded.is_a_present = true;
435 2
    decoded.a = true;
436 2
    decoded.is_b_present = false;
437 2
    decoded.is_c_present = false;
438 2
    decoded.is_d_present = false;
439 2
    decoded.is_e_present = false;
440 2
    decoded.is_f_present = false;
441 2
    decoded.is_g_present = false;
442 2
    decoded.is_h_present = false;
443 2
    decoded.is_i_present = true;
444 2
    decoded.i = true;
445

446 2
    memset(&encoded[0], 0, sizeof(encoded));
447 2
    ASSERT_EQ(uper_c_source_g_encode(&encoded[0],
448
                                     sizeof(encoded),
449
                                     &decoded), sizeof(encoded));
450 2
    ASSERT_MEMORY_EQ(&encoded[0],
451
                     "\x80\xe0",
452
                     sizeof(encoded));
453

454
    /* Decode. */
455 2
    memset(&decoded, 0, sizeof(decoded));
456 2
    ASSERT_EQ(uper_c_source_g_decode(&decoded,
457
                                     &encoded[0],
458
                                     sizeof(encoded)), sizeof(encoded));
459

460 2
    ASSERT_TRUE(decoded.is_a_present);
461 2
    ASSERT_EQ(decoded.a, true);
462 2
    ASSERT_FALSE(decoded.is_b_present);
463 2
    ASSERT_FALSE(decoded.is_c_present);
464 2
    ASSERT_FALSE(decoded.is_d_present);
465 2
    ASSERT_FALSE(decoded.is_e_present);
466 2
    ASSERT_FALSE(decoded.is_f_present);
467 2
    ASSERT_FALSE(decoded.is_g_present);
468 2
    ASSERT_FALSE(decoded.is_h_present);
469 2
    ASSERT_TRUE(decoded.is_i_present);
470 2
    ASSERT_EQ(decoded.i, true);
471
}
472

473 2
TEST(uper_c_source_h)
474
{
475
    uint8_t encoded[1];
476
    struct uper_c_source_h_t decoded;
477

478
    /* Encode. */
479
    memset(&encoded[0], 0, sizeof(encoded));
480 2
    ASSERT_EQ(uper_c_source_h_encode(&encoded[0],
481
                                     sizeof(encoded),
482
                                     &decoded), 0);
483

484
    /* Decode. */
485 2
    memset(&decoded, 0, sizeof(decoded));
486 2
    ASSERT_EQ(uper_c_source_h_decode(&decoded,
487
                                     &encoded[0],
488
                                     sizeof(encoded)), 0);
489
}
490

491 2
TEST(uper_c_source_q_c256)
492
{
493
    uint8_t encoded[2];
494
    struct uper_c_source_q_t decoded;
495

496
    /* Encode. */
497 2
    decoded.choice = uper_c_source_q_choice_c256_e;
498 2
    decoded.value.c256 = true;
499

500 2
    memset(&encoded[0], 0, sizeof(encoded));
501 2
    ASSERT_EQ(uper_c_source_q_encode(&encoded[0],
502
                                     sizeof(encoded),
503
                                     &decoded), sizeof(encoded));
504 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x7f\xc0", 2);
505

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

512 2
    ASSERT_EQ(decoded.choice, uper_c_source_q_choice_c256_e);
513 2
    ASSERT_EQ(decoded.value.c256, true);
514
}
515

516 2
TEST(uper_c_source_q_c257)
517
{
518
    uint8_t encoded[2];
519
    struct uper_c_source_q_t decoded;
520

521
    /* Encode. */
522 2
    decoded.choice = uper_c_source_q_choice_c257_e;
523 2
    decoded.value.c257 = true;
524

525 2
    memset(&encoded[0], 0, sizeof(encoded));
526 2
    ASSERT_EQ(uper_c_source_q_encode(&encoded[0],
527
                                     sizeof(encoded),
528
                                     &decoded), sizeof(encoded));
529 2
    ASSERT_MEMORY_EQ(&encoded[0], "\x80\x40", 2);
530

531
    /* Decode. */
532 2
    memset(&decoded, 0, sizeof(decoded));
533 2
    ASSERT_EQ(uper_c_source_q_decode(&decoded,
534
                                     &encoded[0],
535
                                     sizeof(encoded)), sizeof(encoded));
536

537 2
    ASSERT_EQ(decoded.choice, uper_c_source_q_choice_c257_e);
538 2
    ASSERT_EQ(decoded.value.c257, true);
539
}
540

541 2
TEST(uper_c_source_r)
542
{
543
    struct data_t {
544
        int8_t decoded;
545
        uint8_t encoded[1];
546 2
    } datas[] = {
547
        {
548
            .decoded = -1,
549
            .encoded = "\x00"
550
        },
551
        {
552
            .decoded = 0,
553
            .encoded = "\x80"
554
        }
555
    };
556
    uint8_t encoded[1];
557
    struct uper_c_source_r_t decoded;
558
    unsigned int i;
559

560 2
    for (i = 0; i < membersof(datas); i++) {
561
        /* Encode. */
562 2
        decoded.value = datas[i].decoded;
563

564
        memset(&encoded[0], 0, sizeof(encoded));
565 2
        ASSERT_EQ(uper_c_source_r_encode(&encoded[0],
566
                                         sizeof(encoded),
567
                                         &decoded), sizeof(encoded));
568 2
        ASSERT_MEMORY_EQ(&encoded[0],
569
                         &datas[i].encoded[0],
570
                         sizeof(encoded));
571

572
        /* Decode. */
573 2
        memset(&decoded, 0, sizeof(decoded));
574 2
        ASSERT_EQ(uper_c_source_r_decode(&decoded,
575
                                         &encoded[0],
576
                                         sizeof(encoded)), sizeof(encoded));
577

578 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
579
    }
580
}
581

582 2
TEST(uper_c_source_s)
583
{
584
    struct data_t {
585
        int8_t decoded;
586
        uint8_t encoded[1];
587 2
    } datas[] = {
588
        {
589
            .decoded = -2,
590
            .encoded = "\x00"
591
        },
592
        {
593
            .decoded = 1,
594
            .encoded = "\xc0"
595
        }
596
    };
597
    uint8_t encoded[1];
598
    struct uper_c_source_s_t decoded;
599
    unsigned int i;
600

601 2
    for (i = 0; i < membersof(datas); i++) {
602
        /* Encode. */
603 2
        decoded.value = datas[i].decoded;
604

605
        memset(&encoded[0], 0, sizeof(encoded));
606 2
        ASSERT_EQ(uper_c_source_s_encode(&encoded[0],
607
                                         sizeof(encoded),
608
                                         &decoded), sizeof(encoded));
609 2
        ASSERT_MEMORY_EQ(&encoded[0],
610
                         &datas[i].encoded[0],
611
                         sizeof(encoded));
612

613
        /* Decode. */
614 2
        memset(&decoded, 0, sizeof(decoded));
615 2
        ASSERT_EQ(uper_c_source_s_decode(&decoded,
616
                                         &encoded[0],
617
                                         sizeof(encoded)), sizeof(encoded));
618

619 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
620
    }
621
}
622

623 2
TEST(uper_c_source_t)
624
{
625
    struct data_t {
626
        int8_t decoded;
627
        uint8_t encoded[1];
628 2
    } datas[] = {
629
        {
630
            .decoded = -1,
631
            .encoded = "\x00"
632
        },
633
        {
634
            .decoded = 2,
635
            .encoded = "\xc0"
636
        }
637
    };
638
    uint8_t encoded[1];
639
    struct uper_c_source_t_t decoded;
640
    unsigned int i;
641

642 2
    for (i = 0; i < membersof(datas); i++) {
643
        /* Encode. */
644 2
        decoded.value = datas[i].decoded;
645

646
        memset(&encoded[0], 0, sizeof(encoded));
647 2
        ASSERT_EQ(uper_c_source_t_encode(&encoded[0],
648
                                         sizeof(encoded),
649
                                         &decoded), sizeof(encoded));
650 2
        ASSERT_MEMORY_EQ(&encoded[0],
651
                         &datas[i].encoded[0],
652
                         sizeof(encoded));
653

654
        /* Decode. */
655 2
        memset(&decoded, 0, sizeof(decoded));
656 2
        ASSERT_EQ(uper_c_source_t_decode(&decoded,
657
                                         &encoded[0],
658
                                         sizeof(encoded)), sizeof(encoded));
659

660 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
661
    }
662
}
663

664 2
TEST(uper_c_source_u)
665
{
666
    struct data_t {
667
        int8_t decoded;
668
        uint8_t encoded[1];
669 2
    } datas[] = {
670
        {
671
            .decoded = -64,
672
            .encoded = "\x00"
673
        }
674
    };
675
    uint8_t encoded[1];
676
    struct uper_c_source_u_t decoded;
677
    unsigned int i;
678

679 2
    for (i = 0; i < membersof(datas); i++) {
680
        /* Encode. */
681 2
        decoded.value = datas[i].decoded;
682

683
        memset(&encoded[0], 0, sizeof(encoded));
684 2
        ASSERT_EQ(uper_c_source_u_encode(&encoded[0],
685
                                         sizeof(encoded),
686
                                         &decoded), sizeof(encoded));
687 2
        ASSERT_MEMORY_EQ(&encoded[0],
688
                         &datas[i].encoded[0],
689
                         sizeof(encoded));
690

691
        /* Decode. */
692 2
        memset(&decoded, 0, sizeof(decoded));
693 2
        ASSERT_EQ(uper_c_source_u_decode(&decoded,
694
                                         &encoded[0],
695
                                         sizeof(encoded)), sizeof(encoded));
696

697 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
698
    }
699
}
700

701 2
TEST(uper_c_source_v)
702
{
703
    struct data_t {
704
        int8_t decoded;
705
        uint8_t encoded[1];
706 2
    } datas[] = {
707
        {
708
            .decoded = -128,
709
            .encoded = "\x00"
710
        }
711
    };
712
    uint8_t encoded[1];
713
    struct uper_c_source_v_t decoded;
714
    unsigned int i;
715

716 2
    for (i = 0; i < membersof(datas); i++) {
717
        /* Encode. */
718 2
        decoded.value = datas[i].decoded;
719

720
        memset(&encoded[0], 0, sizeof(encoded));
721 2
        ASSERT_EQ(uper_c_source_v_encode(&encoded[0],
722
                                         sizeof(encoded),
723
                                         &decoded), sizeof(encoded));
724 2
        ASSERT_MEMORY_EQ(&encoded[0],
725
                         &datas[i].encoded[0],
726
                         sizeof(encoded));
727

728
        /* Decode. */
729 2
        memset(&decoded, 0, sizeof(decoded));
730 2
        ASSERT_EQ(uper_c_source_v_decode(&decoded,
731
                                         &encoded[0],
732
                                         sizeof(encoded)), sizeof(encoded));
733

734 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
735
    }
736
}
737

738 2
TEST(uper_c_source_w)
739
{
740
    struct data_t {
741
        int16_t decoded;
742
        uint8_t encoded[2];
743 2
    } datas[] = {
744
        {
745
            .decoded = -1,
746
            .encoded = "\x00\x00"
747
        },
748
        {
749
            .decoded = 510,
750
            .encoded = "\xff\x80"
751
        }
752
    };
753
    uint8_t encoded[2];
754
    struct uper_c_source_w_t decoded;
755
    unsigned int i;
756

757 2
    for (i = 0; i < membersof(datas); i++) {
758
        /* Encode. */
759 2
        decoded.value = datas[i].decoded;
760

761 2
        memset(&encoded[0], 0, sizeof(encoded));
762 2
        ASSERT_EQ(uper_c_source_w_encode(&encoded[0],
763
                                         sizeof(encoded),
764
                                         &decoded), sizeof(encoded));
765 2
        ASSERT_MEMORY_EQ(&encoded[0],
766
                         &datas[i].encoded[0],
767
                         sizeof(encoded));
768

769
        /* Decode. */
770 2
        memset(&decoded, 0, sizeof(decoded));
771 2
        ASSERT_EQ(uper_c_source_w_decode(&decoded,
772
                                         &encoded[0],
773
                                         sizeof(encoded)), sizeof(encoded));
774

775 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
776
    }
777
}
778

779 2
TEST(uper_c_source_x)
780
{
781
    struct data_t {
782
        int16_t decoded;
783
        uint8_t encoded[2];
784 2
    } datas[] = {
785
        {
786
            .decoded = -2,
787
            .encoded = "\x00\x00"
788
        },
789
        {
790
            .decoded = 510,
791
            .encoded = "\x80\x00"
792
        }
793
    };
794
    uint8_t encoded[2];
795
    struct uper_c_source_x_t decoded;
796
    unsigned int i;
797

798 2
    for (i = 0; i < membersof(datas); i++) {
799
        /* Encode. */
800 2
        decoded.value = datas[i].decoded;
801

802 2
        memset(&encoded[0], 0, sizeof(encoded));
803 2
        ASSERT_EQ(uper_c_source_x_encode(&encoded[0],
804
                                         sizeof(encoded),
805
                                         &decoded), sizeof(encoded));
806 2
        ASSERT_MEMORY_EQ(&encoded[0],
807
                         &datas[i].encoded[0],
808
                         sizeof(encoded));
809

810
        /* Decode. */
811 2
        memset(&decoded, 0, sizeof(decoded));
812 2
        ASSERT_EQ(uper_c_source_x_decode(&decoded,
813
                                         &encoded[0],
814
                                         sizeof(encoded)), sizeof(encoded));
815

816 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
817
    }
818
}
819

820 2
TEST(uper_c_source_y)
821
{
822
    struct data_t {
823
        uint16_t decoded;
824
        uint8_t encoded[2];
825 2
    } datas[] = {
826
        {
827
            .decoded = 10000,
828
            .encoded = "\x00\x00"
829
        },
830
        {
831
            .decoded = 10512,
832
            .encoded = "\x80\x00"
833
        }
834
    };
835
    uint8_t encoded[2];
836
    struct uper_c_source_y_t decoded;
837
    unsigned int i;
838

839 2
    for (i = 0; i < membersof(datas); i++) {
840
        /* Encode. */
841 2
        decoded.value = datas[i].decoded;
842

843 2
        memset(&encoded[0], 0, sizeof(encoded));
844 2
        ASSERT_EQ(uper_c_source_y_encode(&encoded[0],
845
                                         sizeof(encoded),
846
                                         &decoded), sizeof(encoded));
847 2
        ASSERT_MEMORY_EQ(&encoded[0],
848
                         &datas[i].encoded[0],
849
                         sizeof(encoded));
850

851
        /* Decode. */
852 2
        memset(&decoded, 0, sizeof(decoded));
853 2
        ASSERT_EQ(uper_c_source_y_decode(&decoded,
854
                                         &encoded[0],
855
                                         sizeof(encoded)), sizeof(encoded));
856

857 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
858
    }
859
}
860

861 2
TEST(uper_c_source_z_decode_error_out_of_data)
862
{
863
    uint8_t encoded[1];
864
    struct uper_c_source_z_t decoded;
865

866 2
    ASSERT_EQ(uper_c_source_z_decode(&decoded,
867
                                     &encoded[0],
868
                                     0), -EOUTOFDATA);
869
}
870

871 2
TEST(uper_c_source_ab)
872
{
873
    uint8_t encoded[2];
874
    struct uper_c_source_ab_t decoded;
875

876
    /* Encode. */
877 2
    decoded.a = 0;
878 2
    decoded.b = 10300;
879

880 2
    memset(&encoded[0], 0, sizeof(encoded));
881 2
    ASSERT_EQ(uper_c_source_ab_encode(&encoded[0],
882
                                      sizeof(encoded),
883
                                      &decoded), sizeof(encoded));
884 2
    ASSERT_MEMORY_EQ(&encoded[0],
885
                     "\xa5\x80",
886
                     sizeof(encoded));
887

888
    /* Decode. */
889 2
    memset(&decoded, 0, sizeof(decoded));
890 2
    ASSERT_EQ(uper_c_source_ab_decode(&decoded,
891
                                      &encoded[0],
892
                                      sizeof(encoded)), sizeof(encoded));
893

894 2
    ASSERT_EQ(decoded.a, 0);
895 2
    ASSERT_EQ(decoded.b, 10300);
896
}
897

898 2
TEST(uper_c_source_ae)
899
{
900
    uint8_t encoded[1];
901
    struct uper_c_source_ae_t decoded;
902

903
    /* Encode. */
904 2
    decoded.is_a_present = true;
905 2
    decoded.a = false;
906 2
    decoded.b = true;
907 2
    decoded.c = false;
908

909
    memset(&encoded[0], 0, sizeof(encoded));
910 2
    ASSERT_EQ(uper_c_source_ae_encode(&encoded[0],
911
                                      sizeof(encoded),
912
                                      &decoded), sizeof(encoded));
913 2
    ASSERT_MEMORY_EQ(&encoded[0],
914
                     "\x40",
915
                     sizeof(encoded));
916

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

923 2
    ASSERT_EQ(decoded.a, false);
924 2
    ASSERT_EQ(decoded.b, true);
925 2
    ASSERT_EQ(decoded.c, false);
926
}
927

928 2
TEST(uper_c_source_af)
929
{
930
    uint8_t encoded[24];
931 2
    uint8_t encoded2[24] = "\xc4\x7f\xc1\x30\x10\x11\x10\x00\x44\x80\x44\xc0"
932
        "\x45\x00\x45\x40\x45\x80\x45\xc0\x46\x00\x46\x40";
933
    struct uper_c_source_af_t decoded;
934

935
    /* Encode. */
936 2
    decoded.a = true;
937 2
    decoded.b.c = true;
938 2
    decoded.b.d = 17;
939 2
    decoded.b.is_d_addition_present = true;
940 2
    decoded.e = 18;
941 2
    decoded.is_e_addition_present = true;
942 2
    decoded.f = 19;
943 2
    decoded.is_f_addition_present = true;
944 2
    decoded.g = 20;
945 2
    decoded.is_g_addition_present = true;
946 2
    decoded.h = 21;
947 2
    decoded.is_h_addition_present = true;
948 2
    decoded.i = 22;
949 2
    decoded.is_i_addition_present = true;
950 2
    decoded.j = 23;
951 2
    decoded.is_j_addition_present = true;
952 2
    decoded.k = 24;
953 2
    decoded.is_k_addition_present = true;
954 2
    decoded.l = 25;
955 2
    decoded.is_l_addition_present = true;
956

957 2
    memset(&encoded[0], 0, sizeof(encoded));
958 2
    ASSERT_TRUE(uper_c_source_af_encode(&encoded[0],
959
                                        sizeof(encoded),
960
                                        &decoded) < 0);
961

962
    // Decode.
963 2
    memset(&decoded, 0, sizeof(decoded));
964 2
    ASSERT_TRUE(uper_c_source_af_decode(&decoded,
965
                                        &encoded2[0],
966
                                        sizeof(encoded2)) < 0);
967
}
968

969 2
TEST(uper_c_source_al)
970
{
971
    struct data_t {
972
        int16_t decoded;
973
        uint8_t encoded[2];
974 2
    } datas[] = {
975
        {
976
            .decoded = -129,
977
            .encoded = "\x00\x00"
978
        },
979
        {
980
            .decoded = 127,
981
            .encoded = "\x80\x00"
982
        }
983
    };
984
    uint8_t encoded[2];
985
    struct uper_c_source_al_t decoded;
986
    unsigned int i;
987

988 2
    for (i = 0; i < membersof(datas); i++) {
989
        /* Encode. */
990 2
        decoded.value = datas[i].decoded;
991

992 2
        memset(&encoded[0], 0, sizeof(encoded));
993 2
        ASSERT_EQ(uper_c_source_al_encode(&encoded[0],
994
                                         sizeof(encoded),
995
                                         &decoded), sizeof(encoded));
996 2
        ASSERT_MEMORY_EQ(&encoded[0],
997
                         &datas[i].encoded[0],
998
                         sizeof(encoded));
999

1000
        /* Decode. */
1001 2
        memset(&decoded, 0, sizeof(decoded));
1002 2
        ASSERT_EQ(uper_c_source_al_decode(&decoded,
1003
                                         &encoded[0],
1004
                                         sizeof(encoded)), sizeof(encoded));
1005

1006 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
1007
    }
1008
}
1009

1010 2
TEST(uper_c_source_am)
1011
{
1012
    struct data_t {
1013
        int16_t decoded;
1014
        uint8_t encoded[1];
1015 2
    } datas[] = {
1016
        {
1017
            .decoded = -2,
1018
            .encoded = "\x00"
1019
        },
1020
        {
1021
            .decoded = 128,
1022
            .encoded = "\x82"
1023
        }
1024
    };
1025
    uint8_t encoded[1];
1026
    struct uper_c_source_am_t decoded;
1027
    unsigned int i;
1028

1029 2
    for (i = 0; i < membersof(datas); i++) {
1030
        /* Encode. */
1031 2
        decoded.value = datas[i].decoded;
1032

1033
        memset(&encoded[0], 0, sizeof(encoded));
1034 2
        ASSERT_EQ(uper_c_source_am_encode(&encoded[0],
1035
                                         sizeof(encoded),
1036
                                         &decoded), sizeof(encoded));
1037 2
        ASSERT_MEMORY_EQ(&encoded[0],
1038
                         &datas[i].encoded[0],
1039
                         sizeof(encoded));
1040

1041
        /* Decode. */
1042 2
        memset(&decoded, 0, sizeof(decoded));
1043 2
        ASSERT_EQ(uper_c_source_am_decode(&decoded,
1044
                                         &encoded[0],
1045
                                         sizeof(encoded)), sizeof(encoded));
1046

1047 2
        ASSERT_EQ(decoded.value, datas[i].decoded);
1048
    }
1049
}
1050

1051 2
TEST(uper_c_source_ao)
1052
{
1053
    uint8_t encoded[17];
1054
    struct uper_c_source_ao_t decoded;
1055

1056
    /* Encode. */
1057 2
    decoded.a = UPER_C_SOURCE_AO_A_C;
1058 2
    decoded.b = UPER_C_SOURCE_AO_B_A;
1059 2
    decoded.c = 0x5;
1060 2
    decoded.d = UPER_C_SOURCE_AO_D_B;
1061 2
    decoded.e = UPER_C_SOURCE_AO_E_C;
1062

1063 2
    memset(&encoded[0], 0, sizeof(encoded));
1064 2
    ASSERT_EQ(uper_c_source_ao_encode(&encoded[0],
1065
                                      sizeof(encoded),
1066
                                      &decoded), sizeof(encoded));
1067 2
    ASSERT_MEMORY_EQ(&encoded[0],
1068
                     "\x01\x80\x00\x00\x52\x00\x00\x00\x00\x00\x00\x00\x00"
1069
                     "\x00\x00\x00\x10",
1070
                     sizeof(encoded));
1071

1072
    /* Decode. */
1073 2
    memset(&decoded, 0, sizeof(decoded));
1074 2
    ASSERT_EQ(uper_c_source_ao_decode(&decoded,
1075
                                     &encoded[0],
1076
                                     sizeof(encoded)), sizeof(encoded));
1077

1078 2
    ASSERT_EQ(decoded.a, UPER_C_SOURCE_AO_A_C);
1079 2
    ASSERT_EQ(decoded.b, UPER_C_SOURCE_AO_B_A);
1080 2
    ASSERT_EQ(decoded.c, 0x5);
1081 2
    ASSERT_EQ(decoded.d, UPER_C_SOURCE_AO_D_B);
1082 2
    ASSERT_EQ(decoded.e, UPER_C_SOURCE_AO_E_C);
1083
}
1084

1085 2
TEST(uper_c_source_ap)
1086
{
1087 2
    uint8_t encoded[2] = "\x88\x20";
1088
    struct uper_c_source_ap_t decoded;
1089

1090 2
    decoded.b.a = 16;
1091 2
    decoded.c.value = uper_c_ref_referenced_enum_b_e;
1092 2
    decoded.d = 1;
1093

1094
    /* Decode. */
1095 2
    memset(&decoded, 0, sizeof(decoded));
1096 2
    ASSERT_EQ(uper_c_source_ap_decode(&decoded,
1097
                                      &encoded[0],
1098
                                      sizeof(encoded)), sizeof(encoded));
1099

1100 2
    ASSERT_EQ(decoded.b.a, 16);
1101 2
    ASSERT_EQ(decoded.c.value, uper_c_ref_referenced_enum_b_e);
1102
}
1103

1104 2
TEST(uper_c_source_aq)
1105
{
1106 2
    uint8_t encoded[3] = "\x01\x86\xa1";
1107
    struct uper_c_source_aq_t decoded;
1108

1109 2
    decoded.value = 100001;
1110

1111
    /* Decode. */
1112 2
    memset(&decoded, 0, sizeof(decoded));
1113 2
    ASSERT_EQ(uper_c_source_aq_decode(&decoded,
1114
                                     &encoded[0],
1115
                                     sizeof(encoded)), sizeof(encoded));
1116

1117 2
    ASSERT_EQ(decoded.value, 100001);
1118
}

Read our documentation on viewing source code .

Loading