1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/ztest.h>
9
10 #include "lwm2m_util.h"
11 #include "lwm2m_rw_senml_cbor.h"
12 #include "lwm2m_engine.h"
13
14 #define TEST_OBJ_ID 0xFFFF
15 #define TEST_OBJ_INST_ID 0
16
17 #define TEST_RES_S8 0
18 #define TEST_RES_S16 1
19 #define TEST_RES_S32 2
20 #define TEST_RES_S64 3
21 #define TEST_RES_STRING 4
22 #define TEST_RES_FLOAT 5
23 #define TEST_RES_BOOL 6
24 #define TEST_RES_OBJLNK 7
25 #define TEST_RES_OPAQUE 8
26 #define TEST_RES_TIME 9
27
28 #define TEST_OBJ_RES_MAX_ID 10
29
30 #define TEST_MAX_PAYLOAD_BUFFER_LENGTH 40
31
32 static struct lwm2m_engine_obj test_obj;
33
34 struct test_payload_buffer {
35 uint8_t data[TEST_MAX_PAYLOAD_BUFFER_LENGTH];
36 size_t len;
37 };
38
39 static struct lwm2m_engine_obj_field test_fields[] = {
40 OBJ_FIELD_DATA(TEST_RES_S8, RW, S8),
41 OBJ_FIELD_DATA(TEST_RES_S16, RW, S16),
42 OBJ_FIELD_DATA(TEST_RES_S32, RW, S32),
43 OBJ_FIELD_DATA(TEST_RES_S64, RW, S64),
44 OBJ_FIELD_DATA(TEST_RES_STRING, RW, STRING),
45 OBJ_FIELD_DATA(TEST_RES_FLOAT, RW, FLOAT),
46 OBJ_FIELD_DATA(TEST_RES_BOOL, RW, BOOL),
47 OBJ_FIELD_DATA(TEST_RES_OBJLNK, RW, OBJLNK),
48 OBJ_FIELD_DATA(TEST_RES_OPAQUE, RW, OPAQUE),
49 OBJ_FIELD_DATA(TEST_RES_TIME, RW, TIME)
50 };
51
52 static struct lwm2m_engine_obj_inst test_inst;
53 static struct lwm2m_engine_res test_res[TEST_OBJ_RES_MAX_ID];
54 static struct lwm2m_engine_res_inst test_res_inst[TEST_OBJ_RES_MAX_ID];
55
56 #define TEST_STRING_MAX_SIZE 16
57 #define TEST_OPAQUE_MAX_SIZE 11
58
59 static int8_t test_s8;
60 static int16_t test_s16;
61 static int32_t test_s32;
62 static int64_t test_s64;
63 static char test_string[TEST_STRING_MAX_SIZE];
64 static double test_float;
65 static bool test_bool;
66 static struct lwm2m_objlnk test_objlnk;
67 static uint8_t test_opaque[TEST_OPAQUE_MAX_SIZE];
68 static time_t test_time;
69
test_obj_create(uint16_t obj_inst_id)70 static struct lwm2m_engine_obj_inst *test_obj_create(uint16_t obj_inst_id)
71 {
72 int i = 0, j = 0;
73
74 init_res_instance(test_res_inst, ARRAY_SIZE(test_res_inst));
75
76 INIT_OBJ_RES_DATA(TEST_RES_S8, test_res, i, test_res_inst, j,
77 &test_s8, sizeof(test_s8));
78 INIT_OBJ_RES_DATA(TEST_RES_S16, test_res, i, test_res_inst, j,
79 &test_s16, sizeof(test_s16));
80 INIT_OBJ_RES_DATA(TEST_RES_S32, test_res, i, test_res_inst, j,
81 &test_s32, sizeof(test_s32));
82 INIT_OBJ_RES_DATA(TEST_RES_S64, test_res, i, test_res_inst, j,
83 &test_s64, sizeof(test_s64));
84 INIT_OBJ_RES_DATA(TEST_RES_STRING, test_res, i, test_res_inst, j,
85 &test_string, sizeof(test_string));
86 INIT_OBJ_RES_DATA(TEST_RES_FLOAT, test_res, i, test_res_inst, j,
87 &test_float, sizeof(test_float));
88 INIT_OBJ_RES_DATA(TEST_RES_BOOL, test_res, i, test_res_inst, j,
89 &test_bool, sizeof(test_bool));
90 INIT_OBJ_RES_DATA(TEST_RES_OBJLNK, test_res, i, test_res_inst, j,
91 &test_objlnk, sizeof(test_objlnk));
92 INIT_OBJ_RES_DATA(TEST_RES_OPAQUE, test_res, i, test_res_inst, j,
93 &test_opaque, sizeof(test_opaque));
94 INIT_OBJ_RES_DATA(TEST_RES_TIME, test_res, i, test_res_inst, j,
95 &test_time, sizeof(test_time));
96
97 test_inst.resources = test_res;
98 test_inst.resource_count = i;
99
100 return &test_inst;
101 }
102
test_obj_init(void)103 static void *test_obj_init(void)
104 {
105 struct lwm2m_engine_obj_inst *obj_inst = NULL;
106
107 test_obj.obj_id = TEST_OBJ_ID;
108 test_obj.version_major = 1;
109 test_obj.version_minor = 0;
110 test_obj.is_core = false;
111 test_obj.fields = test_fields;
112 test_obj.field_count = ARRAY_SIZE(test_fields);
113 test_obj.max_instance_count = 1U;
114 test_obj.create_cb = test_obj_create;
115
116 (void)lwm2m_register_obj(&test_obj);
117 (void)lwm2m_create_obj_inst(TEST_OBJ_ID, TEST_OBJ_INST_ID, &obj_inst);
118
119 return NULL;
120 }
121
122 /* 2 bytes for Content Format option + payload marker */
123 #define TEST_PAYLOAD_OFFSET 3
124
125 static struct lwm2m_message test_msg;
126
context_reset(void)127 static void context_reset(void)
128 {
129 memset(&test_msg, 0, sizeof(test_msg));
130
131 test_msg.out.writer = &senml_cbor_writer;
132 test_msg.out.out_cpkt = &test_msg.cpkt;
133
134 test_msg.in.reader = &senml_cbor_reader;
135 test_msg.in.in_cpkt = &test_msg.cpkt;
136
137 test_msg.path.level = LWM2M_PATH_LEVEL_RESOURCE;
138 test_msg.path.obj_id = TEST_OBJ_ID;
139 test_msg.path.obj_inst_id = TEST_OBJ_INST_ID;
140
141 test_msg.cpkt.data = test_msg.msg_data;
142 test_msg.cpkt.max_len = sizeof(test_msg.msg_data);
143 }
144
test_payload_set(struct test_payload_buffer payload)145 static void test_payload_set(struct test_payload_buffer payload)
146 {
147 memcpy(test_msg.msg_data + 1, payload.data, payload.len);
148 test_msg.cpkt.offset = payload.len + 1;
149 test_msg.in.offset = 1;
150 }
151
test_prepare(void * dummy)152 static void test_prepare(void *dummy)
153 {
154 ARG_UNUSED(dummy);
155 context_reset();
156 }
157
test_prepare_nomem(void * dummy)158 static void test_prepare_nomem(void *dummy)
159 {
160 ARG_UNUSED(dummy);
161 context_reset();
162
163 /* Leave some space for Content-format option */
164
165 test_msg.cpkt.offset = sizeof(test_msg.msg_data) - TEST_PAYLOAD_OFFSET;
166 }
167
test_prepare_nodata(void * dummy)168 static void test_prepare_nodata(void *dummy)
169 {
170 ARG_UNUSED(dummy);
171 context_reset();
172 }
173
ZTEST(net_content_senml_cbor,test_put_s8)174 ZTEST(net_content_senml_cbor, test_put_s8)
175 {
176 int ret;
177 int i;
178 uint16_t offset = 0;
179 int8_t value[] = { 0, INT8_MAX, INT8_MIN };
180
181 struct test_payload_buffer expected_payload[] = {
182 {
183 .data = {
184 (0x04 << 5) | 1,
185 (0x05 << 5) | 3,
186 (0x01 << 5) | 1,
187 (0x03 << 5) | 9,
188 '/', '6', '5', '5', '3', '5', '/', '0', '/',
189 (0x00 << 5) | 0,
190 (0x03 << 5) | 1,
191 '0',
192 (0x00 << 5) | 2,
193 (0x00 << 5) | 0
194 },
195 .len = 18
196 },
197 {
198 .data = {
199 (0x04 << 5) | 1,
200 (0x05 << 5) | 3,
201 (0x01 << 5) | 1,
202 (0x03 << 5) | 9,
203 '/', '6', '5', '5', '3', '5', '/', '0', '/',
204 (0x00 << 5) | 0,
205 (0x03 << 5) | 1,
206 '0',
207 (0x00 << 5) | 2,
208 (0x00 << 5) | 24,
209 127
210 },
211 .len = 19
212 },
213 {
214 .data = {
215 (0x04 << 5) | 1,
216 (0x05 << 5) | 3,
217 (0x01 << 5) | 1,
218 (0x03 << 5) | 9,
219 '/', '6', '5', '5', '3', '5', '/', '0', '/',
220 (0x00 << 5) | 0,
221 (0x03 << 5) | 1,
222 '0',
223 (0x00 << 5) | 2,
224 (0x01 << 5) | 24,
225 127
226 },
227 .len = 19
228 },
229 };
230
231 test_msg.path.res_id = TEST_RES_S8;
232
233 for (i = 0; i < ARRAY_SIZE(expected_payload); i++) {
234 test_s8 = value[i];
235
236 ret = do_read_op_senml_cbor(&test_msg);
237 zassert_true(ret >= 0, "Error reported");
238
239 offset += TEST_PAYLOAD_OFFSET;
240
241 zassert_mem_equal(test_msg.msg_data + offset,
242 expected_payload[i].data,
243 expected_payload[i].len,
244 "Invalid payload format");
245
246 offset += expected_payload[i].len;
247 zassert_equal(test_msg.cpkt.offset, offset,
248 "Invalid packet offset");
249 }
250 }
251
ZTEST(net_content_senml_cbor_nomem,test_put_s8_nomem)252 ZTEST(net_content_senml_cbor_nomem, test_put_s8_nomem)
253 {
254 int ret;
255
256 test_msg.path.res_id = TEST_RES_S8;
257
258 ret = do_read_op_senml_cbor(&test_msg);
259 zassert_equal(ret, -ENOMEM, "Invalid error code returned");
260 }
261
ZTEST(net_content_senml_cbor,test_put_s16)262 ZTEST(net_content_senml_cbor, test_put_s16)
263 {
264 int ret;
265 int i;
266 uint16_t offset = 0;
267 int16_t value[] = { 0, INT16_MAX, INT16_MIN };
268
269 struct test_payload_buffer expected_payload[] = {
270 {
271 .data = {
272 (0x04 << 5) | 1,
273 (0x05 << 5) | 3,
274 (0x01 << 5) | 1,
275 (0x03 << 5) | 9,
276 '/', '6', '5', '5', '3', '5', '/', '0', '/',
277 (0x00 << 5) | 0,
278 (0x03 << 5) | 1,
279 '1',
280 (0x00 << 5) | 2,
281 (0x00 << 5) | 0
282 },
283 .len = 18
284 },
285 {
286 .data = {
287 (0x04 << 5) | 1,
288 (0x05 << 5) | 3,
289 (0x01 << 5) | 1,
290 (0x03 << 5) | 9,
291 '/', '6', '5', '5', '3', '5', '/', '0', '/',
292 (0x00 << 5) | 0,
293 (0x03 << 5) | 1,
294 '1',
295 (0x00 << 5) | 2,
296 (0x00 << 5) | 25,
297 127, 255
298 },
299 .len = 20
300 },
301 {
302 .data = {
303 (0x04 << 5) | 1,
304 (0x05 << 5) | 3,
305 (0x01 << 5) | 1,
306 (0x03 << 5) | 9,
307 '/', '6', '5', '5', '3', '5', '/', '0', '/',
308 (0x00 << 5) | 0,
309 (0x03 << 5) | 1,
310 '1',
311 (0x00 << 5) | 2,
312 (0x01 << 5) | 25,
313 127, 255
314 },
315 .len = 20
316 },
317 };
318
319 test_msg.path.res_id = TEST_RES_S16;
320
321 for (i = 0; i < ARRAY_SIZE(expected_payload); i++) {
322 test_s16 = value[i];
323
324 ret = do_read_op_senml_cbor(&test_msg);
325 zassert_true(ret >= 0, "Error reported");
326
327 offset += TEST_PAYLOAD_OFFSET;
328
329 zassert_mem_equal(test_msg.msg_data + offset,
330 expected_payload[i].data,
331 expected_payload[i].len,
332 "Invalid payload format");
333
334 offset += expected_payload[i].len;
335 zassert_equal(test_msg.cpkt.offset, offset,
336 "Invalid packet offset");
337 }
338 }
339
ZTEST(net_content_senml_cbor_nomem,test_put_s16_nomem)340 ZTEST(net_content_senml_cbor_nomem, test_put_s16_nomem)
341 {
342 int ret;
343
344 test_msg.path.res_id = TEST_RES_S16;
345
346 ret = do_read_op_senml_cbor(&test_msg);
347 zassert_equal(ret, -ENOMEM, "Invalid error code returned");
348 }
349
ZTEST(net_content_senml_cbor,test_put_s32)350 ZTEST(net_content_senml_cbor, test_put_s32)
351 {
352 int ret;
353 int i;
354 uint16_t offset = 0;
355 int32_t value[] = { 0, INT32_MAX, INT32_MIN };
356
357 struct test_payload_buffer expected_payload[] = {
358 {
359 .data = {
360 (0x04 << 5) | 1,
361 (0x05 << 5) | 3,
362 (0x01 << 5) | 1,
363 (0x03 << 5) | 9,
364 '/', '6', '5', '5', '3', '5', '/', '0', '/',
365 (0x00 << 5) | 0,
366 (0x03 << 5) | 1,
367 '2',
368 (0x00 << 5) | 2,
369 (0x00 << 5) | 0
370 },
371 .len = 18
372 },
373 {
374 .data = {
375 (0x04 << 5) | 1,
376 (0x05 << 5) | 3,
377 (0x01 << 5) | 1,
378 (0x03 << 5) | 9,
379 '/', '6', '5', '5', '3', '5', '/', '0', '/',
380 (0x00 << 5) | 0,
381 (0x03 << 5) | 1,
382 '2',
383 (0x00 << 5) | 2,
384 (0x00 << 5) | 26,
385 127, 255, 255, 255
386 },
387 .len = 22
388 },
389 {
390 .data = {
391 (0x04 << 5) | 1,
392 (0x05 << 5) | 3,
393 (0x01 << 5) | 1,
394 (0x03 << 5) | 9,
395 '/', '6', '5', '5', '3', '5', '/', '0', '/',
396 (0x00 << 5) | 0,
397 (0x03 << 5) | 1,
398 '2',
399 (0x00 << 5) | 2,
400 (0x01 << 5) | 26,
401 127, 255, 255, 255
402 },
403 .len = 22
404 },
405 };
406
407 test_msg.path.res_id = TEST_RES_S32;
408
409 for (i = 0; i < ARRAY_SIZE(expected_payload); i++) {
410 test_s32 = value[i];
411
412 ret = do_read_op_senml_cbor(&test_msg);
413 zassert_true(ret >= 0, "Error reported");
414
415 offset += TEST_PAYLOAD_OFFSET;
416
417 zassert_mem_equal(test_msg.msg_data + offset,
418 expected_payload[i].data,
419 expected_payload[i].len,
420 "Invalid payload format");
421
422 offset += expected_payload[i].len;
423 zassert_equal(test_msg.cpkt.offset, offset,
424 "Invalid packet offset");
425 }
426 }
427
ZTEST(net_content_senml_cbor_nomem,test_put_s32_nomem)428 ZTEST(net_content_senml_cbor_nomem, test_put_s32_nomem)
429 {
430 int ret;
431
432 test_msg.path.res_id = TEST_RES_S32;
433
434 ret = do_read_op_senml_cbor(&test_msg);
435 zassert_equal(ret, -ENOMEM, "Invalid error code returned");
436 }
437
ZTEST(net_content_senml_cbor,test_put_s64)438 ZTEST(net_content_senml_cbor, test_put_s64)
439 {
440 int ret;
441 int i;
442 uint16_t offset = 0;
443 int64_t value[] = { 1, INT64_MIN, INT64_MAX };
444
445 struct test_payload_buffer expected_payload[] = {
446 {
447 .data = {
448 (0x04 << 5) | 1,
449 (0x05 << 5) | 3,
450 (0x01 << 5) | 1,
451 (0x03 << 5) | 9,
452 '/', '6', '5', '5', '3', '5', '/', '0', '/',
453 (0x00 << 5) | 0,
454 (0x03 << 5) | 1,
455 '3',
456 (0x00 << 5) | 2,
457 (0x00 << 5) | 1
458 },
459 .len = 18
460 },
461 {
462 .data = {
463 (0x04 << 5) | 1,
464 (0x05 << 5) | 3,
465 (0x01 << 5) | 1,
466 (0x03 << 5) | 9,
467 '/', '6', '5', '5', '3', '5', '/', '0', '/',
468 (0x00 << 5) | 0,
469 (0x03 << 5) | 1,
470 '3',
471 (0x00 << 5) | 2,
472 (0x01 << 5) | 27,
473 127, 255, 255, 255, 255, 255, 255, 255
474 },
475 .len = 26
476 },
477 {
478 .data = {
479 (0x04 << 5) | 1,
480 (0x05 << 5) | 3,
481 (0x01 << 5) | 1,
482 (0x03 << 5) | 9,
483 '/', '6', '5', '5', '3', '5', '/', '0', '/',
484 (0x00 << 5) | 0,
485 (0x03 << 5) | 1,
486 '3',
487 (0x00 << 5) | 2,
488 (0x00 << 5) | 27,
489 127, 255, 255, 255, 255, 255, 255, 255
490 },
491 .len = 26
492 },
493 };
494
495 test_msg.path.res_id = TEST_RES_S64;
496
497 for (i = 0; i < ARRAY_SIZE(expected_payload); i++) {
498 test_s64 = value[i];
499
500 ret = do_read_op_senml_cbor(&test_msg);
501 zassert_true(ret >= 0, "Error reported");
502
503 offset += TEST_PAYLOAD_OFFSET;
504
505 zassert_mem_equal(test_msg.msg_data + offset,
506 expected_payload[i].data,
507 expected_payload[i].len,
508 "Invalid payload format");
509
510 offset += expected_payload[i].len;
511 zassert_equal(test_msg.cpkt.offset, offset,
512 "Invalid packet offset");
513 }
514 }
515
ZTEST(net_content_senml_cbor_nomem,test_put_s64_nomem)516 ZTEST(net_content_senml_cbor_nomem, test_put_s64_nomem)
517 {
518 int ret;
519
520 test_msg.path.res_id = TEST_RES_S64;
521
522 ret = do_read_op_senml_cbor(&test_msg);
523 zassert_equal(ret, -ENOMEM, "Invalid error code returned");
524 }
525
ZTEST(net_content_senml_cbor,test_put_string)526 ZTEST(net_content_senml_cbor, test_put_string)
527 {
528 int ret;
529 struct test_payload_buffer expected_payload = {
530 .data = {
531 (0x04 << 5) | 1,
532 (0x05 << 5) | 3,
533 (0x01 << 5) | 1,
534 (0x03 << 5) | 9,
535 '/', '6', '5', '5', '3', '5', '/', '0', '/',
536 (0x00 << 5) | 0,
537 (0x03 << 5) | 1,
538 '4',
539 (0x00 << 5) | 3,
540 (0x03 << 5) | 11,
541 't', 'e', 's', 't', '_', 's', 't', 'r', 'i', 'n', 'g'
542 },
543 .len = 29
544 };
545
546 strcpy(test_string, "test_string");
547 test_msg.path.res_id = TEST_RES_STRING;
548
549 ret = do_read_op_senml_cbor(&test_msg);
550 zassert_true(ret >= 0, "Error reported");
551
552 zassert_mem_equal(test_msg.msg_data + TEST_PAYLOAD_OFFSET,
553 expected_payload.data, expected_payload.len,
554 "Invalid payload format");
555 zassert_equal(test_msg.cpkt.offset,
556 expected_payload.len + TEST_PAYLOAD_OFFSET,
557 "Invalid packet offset");
558 }
559
ZTEST(net_content_senml_cbor_nomem,test_put_string_nomem)560 ZTEST(net_content_senml_cbor_nomem, test_put_string_nomem)
561 {
562 int ret;
563
564 test_msg.path.res_id = TEST_RES_STRING;
565
566 ret = do_read_op_senml_cbor(&test_msg);
567 zassert_equal(ret, -ENOMEM, "Invalid error code returned");
568 }
569
ZTEST(net_content_senml_cbor,test_put_float)570 ZTEST(net_content_senml_cbor, test_put_float)
571 {
572 int ret;
573 int i;
574 uint16_t offset = 0;
575 double value[] = { 0.123, -0.987, 3., -10., 2.333, -123.125 };
576 struct test_payload_buffer expected_payload[] = {
577 {
578 .data = {
579 (0x04 << 5) | 1,
580 (0x05 << 5) | 3,
581 (0x01 << 5) | 1,
582 (0x03 << 5) | 9,
583 '/', '6', '5', '5', '3', '5', '/', '0', '/',
584 (0x00 << 5) | 0,
585 (0x03 << 5) | 1,
586 '5',
587 (0x00 << 5) | 2,
588 (0x07 << 5) | 0x1b,
589 0x3F, 0xBF, 0x7C, 0xED, 0x91, 0x68, 0x72, 0xB0
590 },
591 .len = 26
592 },
593 {
594 .data = {
595 (0x04 << 5) | 1,
596 (0x05 << 5) | 3,
597 (0x01 << 5) | 1,
598 (0x03 << 5) | 9,
599 '/', '6', '5', '5', '3', '5', '/', '0', '/',
600 (0x00 << 5) | 0,
601 (0x03 << 5) | 1,
602 '5',
603 (0x00 << 5) | 2,
604 (0x07 << 5) | 0x1b,
605 0xBF, 0xEF, 0x95, 0x81, 0x06, 0x24, 0xDD, 0x2F
606 },
607 .len = 26
608 },
609 {
610 .data = {
611 (0x04 << 5) | 1,
612 (0x05 << 5) | 3,
613 (0x01 << 5) | 1,
614 (0x03 << 5) | 9,
615 '/', '6', '5', '5', '3', '5', '/', '0', '/',
616 (0x00 << 5) | 0,
617 (0x03 << 5) | 1,
618 '5',
619 (0x00 << 5) | 2,
620 (0x07 << 5) | 0x1b,
621 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
622 },
623 .len = 26
624 },
625 {
626 .data = {
627 (0x04 << 5) | 1,
628 (0x05 << 5) | 3,
629 (0x01 << 5) | 1,
630 (0x03 << 5) | 9,
631 '/', '6', '5', '5', '3', '5', '/', '0', '/',
632 (0x00 << 5) | 0,
633 (0x03 << 5) | 1,
634 '5',
635 (0x00 << 5) | 2,
636 (0x07 << 5) | 0x1b,
637 0xC0, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
638 },
639 .len = 26
640 },
641 {
642 .data = {
643 (0x04 << 5) | 1,
644 (0x05 << 5) | 3,
645 (0x01 << 5) | 1,
646 (0x03 << 5) | 9,
647 '/', '6', '5', '5', '3', '5', '/', '0', '/',
648 (0x00 << 5) | 0,
649 (0x03 << 5) | 1,
650 '5',
651 (0x00 << 5) | 2,
652 (0x07 << 5) | 0x1b,
653 0x40, 0x02, 0xA9, 0xFB, 0xE7, 0x6C, 0x8B, 0x44
654 },
655 .len = 26
656 },
657 {
658 .data = {
659 (0x04 << 5) | 1,
660 (0x05 << 5) | 3,
661 (0x01 << 5) | 1,
662 (0x03 << 5) | 9,
663 '/', '6', '5', '5', '3', '5', '/', '0', '/',
664 (0x00 << 5) | 0,
665 (0x03 << 5) | 1,
666 '5',
667 (0x00 << 5) | 2,
668 (0x07 << 5) | 0x1b,
669 0xC0, 0x5E, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00
670 },
671 .len = 26
672 }
673 };
674
675 test_msg.path.res_id = TEST_RES_FLOAT;
676
677 for (i = 0; i < ARRAY_SIZE(expected_payload); i++) {
678 test_float = value[i];
679
680 ret = do_read_op_senml_cbor(&test_msg);
681 zassert_true(ret >= 0, "Error reported");
682
683 offset += TEST_PAYLOAD_OFFSET;
684 zassert_mem_equal(test_msg.msg_data + offset,
685 expected_payload[i].data,
686 expected_payload[i].len,
687 "Invalid payload format");
688
689 offset += expected_payload[i].len;
690 zassert_equal(test_msg.cpkt.offset, offset,
691 "Invalid packet offset");
692 }
693 }
694
ZTEST(net_content_senml_cbor_nomem,test_put_float_nomem)695 ZTEST(net_content_senml_cbor_nomem, test_put_float_nomem)
696 {
697 int ret;
698
699 test_msg.path.res_id = TEST_RES_FLOAT;
700
701 ret = do_read_op_senml_cbor(&test_msg);
702 zassert_equal(ret, -ENOMEM, "Invalid error code returned");
703 }
704
ZTEST(net_content_senml_cbor,test_put_bool)705 ZTEST(net_content_senml_cbor, test_put_bool)
706 {
707 int ret;
708 int i;
709 uint16_t offset = 0;
710 bool value[] = { true, false };
711 struct test_payload_buffer expected_payload[] = {
712 {
713 .data = {
714 (0x04 << 5) | 1,
715 (0x05 << 5) | 3,
716 (0x01 << 5) | 1,
717 (0x03 << 5) | 9,
718 '/', '6', '5', '5', '3', '5', '/', '0', '/',
719 (0x00 << 5) | 0,
720 (0x03 << 5) | 1,
721 '6',
722 (0x00 << 5) | 4,
723 (0x07 << 5) | 21
724 },
725 .len = 18
726 },
727 {
728 .data = {
729 (0x04 << 5) | 1,
730 (0x05 << 5) | 3,
731 (0x01 << 5) | 1,
732 (0x03 << 5) | 9,
733 '/', '6', '5', '5', '3', '5', '/', '0', '/',
734 (0x00 << 5) | 0,
735 (0x03 << 5) | 1,
736 '6',
737 (0x00 << 5) | 4,
738 (0x07 << 5) | 20
739 },
740 .len = 18
741 }
742 };
743
744 test_msg.path.res_id = TEST_RES_BOOL;
745
746 for (i = 0; i < ARRAY_SIZE(expected_payload); i++) {
747 test_bool = value[i];
748
749 ret = do_read_op_senml_cbor(&test_msg);
750 zassert_true(ret >= 0, "Error reported");
751
752 offset += TEST_PAYLOAD_OFFSET;
753
754 zassert_mem_equal(test_msg.msg_data + offset,
755 expected_payload[i].data,
756 expected_payload[i].len,
757 "Invalid payload format");
758
759 offset += expected_payload[i].len;
760 zassert_equal(test_msg.cpkt.offset, offset,
761 "Invalid packet offset");
762 }
763 }
764
ZTEST(net_content_senml_cbor_nomem,test_put_bool_nomem)765 ZTEST(net_content_senml_cbor_nomem, test_put_bool_nomem)
766 {
767 int ret;
768
769 test_msg.path.res_id = TEST_RES_BOOL;
770
771 ret = do_read_op_senml_cbor(&test_msg);
772 zassert_equal(ret, -ENOMEM, "Invalid error code returned");
773 }
774
ZTEST(net_content_senml_cbor,test_put_objlnk)775 ZTEST(net_content_senml_cbor, test_put_objlnk)
776 {
777 int ret;
778 int i;
779 uint16_t offset = 0;
780 struct lwm2m_objlnk value[] = {
781 { 0, 0 }, { 1, 2 }, { LWM2M_OBJLNK_MAX_ID, LWM2M_OBJLNK_MAX_ID }
782 };
783 struct test_payload_buffer expected_payload[] = {
784 {
785 .data = {
786 (0x04 << 5) | 1,
787 (0x05 << 5) | 3,
788 (0x01 << 5) | 1,
789 (0x03 << 5) | 9,
790 '/', '6', '5', '5', '3', '5', '/', '0', '/',
791 (0x00 << 5) | 0,
792 (0x03 << 5) | 1,
793 '7',
794 (0x03 << 5) | 3,
795 'v', 'l', 'o',
796 (0x03 << 5) | 3,
797 '0', ':', '0',
798 },
799 .len = 24
800 },
801 {
802 .data = {
803 (0x04 << 5) | 1,
804 (0x05 << 5) | 3,
805 (0x01 << 5) | 1,
806 (0x03 << 5) | 9,
807 '/', '6', '5', '5', '3', '5', '/', '0', '/',
808 (0x00 << 5) | 0,
809 (0x03 << 5) | 1,
810 '7',
811 (0x03 << 5) | 3,
812 'v', 'l', 'o',
813 (0x03 << 5) | 3,
814 '1', ':', '2',
815 },
816 .len = 24
817 },
818 {
819 .data = {
820 (0x04 << 5) | 1,
821 (0x05 << 5) | 3,
822 (0x01 << 5) | 1,
823 (0x03 << 5) | 9,
824 '/', '6', '5', '5', '3', '5', '/', '0', '/',
825 (0x00 << 5) | 0,
826 (0x03 << 5) | 1,
827 '7',
828 (0x03 << 5) | 3,
829 'v', 'l', 'o',
830 (0x03 << 5) | 11,
831 '6', '5', '5', '3', '5', ':', '6', '5', '5', '3', '5',
832 },
833 .len = 32
834 },
835 };
836
837 test_msg.path.res_id = TEST_RES_OBJLNK;
838
839 for (i = 0; i < ARRAY_SIZE(expected_payload); i++) {
840 test_objlnk = value[i];
841
842 ret = do_read_op_senml_cbor(&test_msg);
843 zassert_true(ret >= 0, "Error reported");
844
845 offset += TEST_PAYLOAD_OFFSET;
846
847 zassert_mem_equal(test_msg.msg_data + offset,
848 expected_payload[i].data,
849 expected_payload[i].len,
850 "Invalid payload format");
851
852 offset += expected_payload[i].len;
853 zassert_equal(test_msg.cpkt.offset, offset,
854 "Invalid packet offset");
855 }
856 }
857
ZTEST(net_content_senml_cbor_nomem,test_put_objlnk_nomem)858 ZTEST(net_content_senml_cbor_nomem, test_put_objlnk_nomem)
859 {
860 int ret;
861
862 test_msg.path.res_id = TEST_RES_OBJLNK;
863
864 ret = do_read_op_senml_cbor(&test_msg);
865 zassert_equal(ret, -ENOMEM, "Invalid error code returned");
866 }
867
test_put_opaque(void)868 static void test_put_opaque(void)
869 {
870 int ret;
871 struct test_payload_buffer expected_payload = {
872 .data = {
873 (0x04 << 5) | 1,
874 (0x05 << 5) | 3,
875 (0x01 << 5) | 1,
876 (0x03 << 5) | 9,
877 '/', '6', '5', '5', '3', '5', '/', '0', '/',
878 (0x00 << 5) | 0,
879 (0x03 << 5) | 1,
880 '8',
881 (0x00 << 5) | 8,
882 (0x02 << 5) | 11,
883 't', 'e', 's', 't', '_', 'o', 'p', 'a', 'q', 'u', 'e',
884 },
885 .len = 29
886 };
887
888 memcpy(test_opaque, "test_opaque", 11 * sizeof(uint8_t));
889 test_msg.path.res_id = TEST_RES_OPAQUE;
890
891 ret = do_read_op_senml_cbor(&test_msg);
892 zassert_true(ret >= 0, "Error reported");
893
894 zassert_mem_equal(test_msg.msg_data + TEST_PAYLOAD_OFFSET,
895 expected_payload.data, expected_payload.len,
896 "Invalid payload format");
897 zassert_equal(test_msg.cpkt.offset,
898 expected_payload.len + TEST_PAYLOAD_OFFSET,
899 "Invalid packet offset");
900 }
901
ZTEST(net_content_senml_cbor_nomem,test_put_opaque_nomem)902 ZTEST(net_content_senml_cbor_nomem, test_put_opaque_nomem)
903 {
904 int ret;
905
906 test_msg.path.res_id = TEST_RES_OPAQUE;
907
908 ret = do_read_op_senml_cbor(&test_msg);
909 zassert_equal(ret, -ENOMEM, "Invalid error code returned");
910 }
911
ZTEST(net_content_senml_cbor,test_put_time)912 ZTEST(net_content_senml_cbor, test_put_time)
913 {
914 int ret;
915 time_t value = 1170111600;
916 struct test_payload_buffer expected_payload = {
917 .data = {
918 (0x04 << 5) | 1,
919 (0x05 << 5) | 3,
920 (0x01 << 5) | 1,
921 (0x03 << 5) | 9,
922 '/', '6', '5', '5', '3', '5', '/', '0', '/',
923 (0x00 << 5) | 0,
924 (0x03 << 5) | 1,
925 '9',
926 (0x00 << 5) | 2,
927 (0x00 << 5) | 26,
928 0x45, 0xbe, 0x7c, 0x70
929 },
930 .len = 22
931 };
932
933 test_msg.path.res_id = TEST_RES_TIME;
934 test_time = value;
935
936 ret = do_read_op_senml_cbor(&test_msg);
937
938 zassert_true(ret >= 0, "Error reported");
939 zassert_mem_equal(test_msg.msg_data + TEST_PAYLOAD_OFFSET,
940 expected_payload.data,
941 expected_payload.len,
942 "Invalid payload format");
943 zassert_equal(test_msg.cpkt.offset, expected_payload.len + TEST_PAYLOAD_OFFSET,
944 "Invalid packet offset");
945
946 }
947
ZTEST(net_content_senml_cbor_nomem,test_put_time_nomem)948 ZTEST(net_content_senml_cbor_nomem, test_put_time_nomem)
949 {
950 int ret;
951
952 test_msg.path.res_id = TEST_RES_TIME;
953
954 ret = do_read_op_senml_cbor(&test_msg);
955 zassert_equal(ret, -ENOMEM, "Invalid error code returned");
956 }
957
ZTEST(net_content_senml_cbor,test_get_s32)958 ZTEST(net_content_senml_cbor, test_get_s32)
959 {
960 int ret;
961 int i;
962 struct test_payload_buffer payload[] = {
963 {
964 .data = {
965 (0x04 << 5) | 1,
966 (0x05 << 5) | 3,
967 (0x01 << 5) | 1,
968 (0x03 << 5) | 9,
969 '/', '6', '5', '5', '3', '5', '/', '0', '/',
970 (0x00 << 5) | 0,
971 (0x03 << 5) | 1,
972 '2',
973 (0x00 << 5) | 2,
974 (0x00 << 5) | 0
975 },
976 .len = 18
977 },
978 {
979 .data = {
980 (0x04 << 5) | 1,
981 (0x05 << 5) | 3,
982 (0x01 << 5) | 1,
983 (0x03 << 5) | 9,
984 '/', '6', '5', '5', '3', '5', '/', '0', '/',
985 (0x00 << 5) | 0,
986 (0x03 << 5) | 1,
987 '2',
988 (0x00 << 5) | 2,
989 (0x00 << 5) | 26,
990 127, 255, 255, 255
991 },
992 .len = 22
993 },
994 {
995 .data = {
996 (0x04 << 5) | 1,
997 (0x05 << 5) | 3,
998 (0x01 << 5) | 1,
999 (0x03 << 5) | 9,
1000 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1001 (0x00 << 5) | 0,
1002 (0x03 << 5) | 1,
1003 '2',
1004 (0x00 << 5) | 2,
1005 (0x01 << 5) | 26,
1006 127, 255, 255, 255
1007 },
1008 .len = 22
1009 },
1010 };
1011 int32_t expected_value[] = { 0, INT32_MAX, INT32_MIN };
1012
1013 test_msg.path.res_id = TEST_RES_S32;
1014
1015 for (i = 0; i < ARRAY_SIZE(expected_value); i++) {
1016 test_payload_set(payload[i]);
1017
1018 ret = do_write_op_senml_cbor(&test_msg);
1019 zassert_true(ret >= 0, "Error reported");
1020 zassert_equal(test_s32, expected_value[i], "Invalid value parsed");
1021 zassert_equal(test_msg.in.offset, payload[i].len + 1,
1022 "Invalid packet offset");
1023 }
1024 }
1025
ZTEST(net_content_senml_cbor_nodata,test_get_s32_nodata)1026 ZTEST(net_content_senml_cbor_nodata, test_get_s32_nodata)
1027 {
1028 int ret;
1029
1030 test_msg.path.res_id = TEST_RES_S32;
1031
1032 ret = do_write_op_senml_cbor(&test_msg);
1033 zassert_equal(ret, -EBADMSG, "Invalid error code returned");
1034 }
1035
ZTEST(net_content_senml_cbor,test_get_s64)1036 ZTEST(net_content_senml_cbor, test_get_s64)
1037 {
1038 int ret;
1039 int i;
1040 struct test_payload_buffer payload[] = {
1041 {
1042 .data = {
1043 (0x04 << 5) | 1,
1044 (0x05 << 5) | 3,
1045 (0x01 << 5) | 1,
1046 (0x03 << 5) | 9,
1047 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1048 (0x00 << 5) | 0,
1049 (0x03 << 5) | 1,
1050 '3',
1051 (0x00 << 5) | 2,
1052 (0x00 << 5) | 0
1053 },
1054 .len = 18
1055 },
1056 {
1057 .data = {
1058 (0x04 << 5) | 1,
1059 (0x05 << 5) | 3,
1060 (0x01 << 5) | 1,
1061 (0x03 << 5) | 9,
1062 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1063 (0x00 << 5) | 0,
1064 (0x03 << 5) | 1,
1065 '3',
1066 (0x00 << 5) | 2,
1067 (0x01 << 5) | 27,
1068 127, 255, 255, 255, 255, 255, 255, 255
1069 },
1070 .len = 26
1071 },
1072 {
1073 .data = {
1074 (0x04 << 5) | 1,
1075 (0x05 << 5) | 3,
1076 (0x01 << 5) | 1,
1077 (0x03 << 5) | 9,
1078 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1079 (0x00 << 5) | 0,
1080 (0x03 << 5) | 1,
1081 '3',
1082 (0x00 << 5) | 2,
1083 (0x00 << 5) | 27,
1084 127, 255, 255, 255, 255, 255, 255, 255
1085 },
1086 .len = 26
1087 },
1088 };
1089 int64_t expected_value[] = { 0, INT64_MIN, INT64_MAX };
1090
1091 test_msg.path.res_id = TEST_RES_S64;
1092
1093 for (i = 0; i < ARRAY_SIZE(expected_value); i++) {
1094 test_payload_set(payload[i]);
1095
1096 ret = do_write_op_senml_cbor(&test_msg);
1097 zassert_true(ret >= 0, "Error reported");
1098 zassert_equal(test_s64, expected_value[i], "Invalid value parsed");
1099 zassert_equal(test_msg.in.offset, payload[i].len + 1,
1100 "Invalid packet offset");
1101 }
1102 }
1103
ZTEST(net_content_senml_cbor_nodata,test_get_s64_nodata)1104 ZTEST(net_content_senml_cbor_nodata, test_get_s64_nodata)
1105 {
1106 int ret;
1107
1108 test_msg.path.res_id = TEST_RES_S64;
1109
1110 ret = do_write_op_senml_cbor(&test_msg);
1111 zassert_equal(ret, -EBADMSG, "Invalid error code returned");
1112 }
1113
ZTEST(net_content_senml_cbor,test_get_string)1114 ZTEST(net_content_senml_cbor, test_get_string)
1115 {
1116 int ret;
1117 struct test_payload_buffer payload = {
1118 .data = {
1119 (0x04 << 5) | 1,
1120 (0x05 << 5) | 3,
1121 (0x01 << 5) | 1,
1122 (0x03 << 5) | 9,
1123 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1124 (0x00 << 5) | 0,
1125 (0x03 << 5) | 1,
1126 '4',
1127 (0x00 << 5) | 3,
1128 (0x03 << 5) | 11,
1129 't', 'e', 's', 't', '_', 's', 't', 'r', 'i', 'n', 'g'
1130 },
1131 .len = 29
1132 };
1133 const char *expected_value = "test_string";
1134
1135 test_msg.path.res_id = TEST_RES_STRING;
1136
1137 test_payload_set(payload);
1138
1139 ret = do_write_op_senml_cbor(&test_msg);
1140 zassert_true(ret >= 0, "Error reported");
1141 zassert_mem_equal(test_string, expected_value, strlen(expected_value),
1142 "Invalid value parsed");
1143 zassert_equal(test_msg.in.offset, payload.len + 1,
1144 "Invalid packet offset");
1145 }
1146
ZTEST(net_content_senml_cbor_nodata,test_get_string_nodata)1147 ZTEST(net_content_senml_cbor_nodata, test_get_string_nodata)
1148 {
1149 int ret;
1150
1151 test_msg.path.res_id = TEST_RES_STRING;
1152
1153 ret = do_write_op_senml_cbor(&test_msg);
1154 zassert_equal(ret, -EBADMSG, "Invalid error code returned");
1155 }
1156
1157 #define DOUBLE_CMP_EPSILON 0.000000001
1158
ZTEST(net_content_senml_cbor,test_get_float)1159 ZTEST(net_content_senml_cbor, test_get_float)
1160 {
1161 int ret;
1162 int i;
1163 struct test_payload_buffer payload[] = {
1164 {
1165 .data = {
1166 (0x04 << 5) | 1,
1167 (0x05 << 5) | 3,
1168 (0x01 << 5) | 1,
1169 (0x03 << 5) | 9,
1170 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1171 (0x00 << 5) | 0,
1172 (0x03 << 5) | 1,
1173 '5',
1174 (0x00 << 5) | 2,
1175 (0x07 << 5) | 0x1b,
1176 0x3F, 0xBF, 0x7C, 0xED, 0x91, 0x68, 0x72, 0xB0
1177 },
1178 .len = 26
1179 },
1180 {
1181 .data = {
1182 (0x04 << 5) | 1,
1183 (0x05 << 5) | 3,
1184 (0x01 << 5) | 1,
1185 (0x03 << 5) | 9,
1186 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1187 (0x00 << 5) | 0,
1188 (0x03 << 5) | 1,
1189 '5',
1190 (0x00 << 5) | 2,
1191 (0x07 << 5) | 0x1b,
1192 0xBF, 0xEF, 0x95, 0x81, 0x06, 0x24, 0xDD, 0x2F
1193 },
1194 .len = 26
1195 },
1196 {
1197 .data = {
1198 (0x04 << 5) | 1,
1199 (0x05 << 5) | 3,
1200 (0x01 << 5) | 1,
1201 (0x03 << 5) | 9,
1202 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1203 (0x00 << 5) | 0,
1204 (0x03 << 5) | 1,
1205 '5',
1206 (0x00 << 5) | 2,
1207 (0x07 << 5) | 0x1b,
1208 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1209 },
1210 .len = 26
1211 },
1212 {
1213 .data = {
1214 (0x04 << 5) | 1,
1215 (0x05 << 5) | 3,
1216 (0x01 << 5) | 1,
1217 (0x03 << 5) | 9,
1218 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1219 (0x00 << 5) | 0,
1220 (0x03 << 5) | 1,
1221 '5',
1222 (0x00 << 5) | 2,
1223 (0x07 << 5) | 0x1b,
1224 0xC0, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1225 },
1226 .len = 26
1227 },
1228 {
1229 .data = {
1230 (0x04 << 5) | 1,
1231 (0x05 << 5) | 3,
1232 (0x01 << 5) | 1,
1233 (0x03 << 5) | 9,
1234 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1235 (0x00 << 5) | 0,
1236 (0x03 << 5) | 1,
1237 '5',
1238 (0x00 << 5) | 2,
1239 (0x07 << 5) | 0x1b,
1240 0x40, 0x02, 0xA9, 0xFB, 0xE7, 0x6C, 0x8B, 0x44
1241 },
1242 .len = 26
1243 },
1244 {
1245 .data = {
1246 (0x04 << 5) | 1,
1247 (0x05 << 5) | 3,
1248 (0x01 << 5) | 1,
1249 (0x03 << 5) | 9,
1250 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1251 (0x00 << 5) | 0,
1252 (0x03 << 5) | 1,
1253 '5',
1254 (0x00 << 5) | 2,
1255 (0x07 << 5) | 0x1b,
1256 0xC0, 0x5E, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00
1257 },
1258 .len = 26
1259 }
1260 };
1261 double expected_value[] = {
1262 0.123, -0.987, 3., -10., 2.333, -123.125
1263 };
1264
1265 test_msg.path.res_id = TEST_RES_FLOAT;
1266
1267 for (i = 0; i < ARRAY_SIZE(expected_value); i++) {
1268 test_payload_set(payload[i]);
1269
1270 ret = do_write_op_senml_cbor(&test_msg);
1271 zassert_true(ret >= 0, "Error reported");
1272 zassert_true((test_float > expected_value[i] - DOUBLE_CMP_EPSILON) &&
1273 (test_float < expected_value[i] + DOUBLE_CMP_EPSILON),
1274 "Invalid value parsed");
1275 zassert_equal(test_msg.in.offset, payload[i].len + 1,
1276 "Invalid packet offset");
1277 }
1278 }
1279
ZTEST(net_content_senml_cbor_nodata,test_get_float_nodata)1280 ZTEST(net_content_senml_cbor_nodata, test_get_float_nodata)
1281 {
1282 int ret;
1283
1284 test_msg.path.res_id = TEST_RES_FLOAT;
1285
1286 ret = do_write_op_senml_cbor(&test_msg);
1287 zassert_equal(ret, -EBADMSG, "Invalid error code returned");
1288 }
1289
ZTEST(net_content_senml_cbor,test_get_bool)1290 ZTEST(net_content_senml_cbor, test_get_bool)
1291 {
1292 int ret;
1293 int i;
1294 struct test_payload_buffer const payload[] = {
1295 {
1296 .data = {
1297 (0x04 << 5) | 1,
1298 (0x05 << 5) | 3,
1299 (0x01 << 5) | 1,
1300 (0x03 << 5) | 9,
1301 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1302 (0x00 << 5) | 0,
1303 (0x03 << 5) | 1,
1304 '6',
1305 (0x00 << 5) | 4,
1306 (0x07 << 5) | 21
1307 },
1308 .len = 18
1309 },
1310 {
1311 .data = {
1312 (0x04 << 5) | 1,
1313 (0x05 << 5) | 3,
1314 (0x01 << 5) | 1,
1315 (0x03 << 5) | 9,
1316 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1317 (0x00 << 5) | 0,
1318 (0x03 << 5) | 1,
1319 '6',
1320 (0x00 << 5) | 4,
1321 (0x07 << 5) | 20
1322 },
1323 .len = 18
1324 }
1325 };
1326 bool expected_value[] = { true, false };
1327
1328 test_msg.path.res_id = TEST_RES_BOOL;
1329
1330 for (i = 0; i < ARRAY_SIZE(expected_value); i++) {
1331 test_payload_set(payload[i]);
1332
1333 ret = do_write_op_senml_cbor(&test_msg);
1334 zassert_true(ret >= 0, "Error reported");
1335 zassert_equal(test_bool, expected_value[i], "Invalid value parsed");
1336 zassert_equal(test_msg.in.offset, payload[i].len + 1,
1337 "Invalid packet offset");
1338 }
1339 }
1340
ZTEST(net_content_senml_cbor_nodata,test_get_bool_noda)1341 ZTEST(net_content_senml_cbor_nodata, test_get_bool_noda)
1342 {
1343 int ret;
1344
1345 test_msg.path.res_id = TEST_RES_BOOL;
1346
1347 ret = do_write_op_senml_cbor(&test_msg);
1348 zassert_equal(ret, -EBADMSG, "Invalid error code returned");
1349 }
1350
ZTEST(net_content_senml_cbor,test_get_objlnk)1351 ZTEST(net_content_senml_cbor, test_get_objlnk)
1352 {
1353 int ret;
1354 int i;
1355 struct test_payload_buffer payload[] = {
1356 {
1357 .data = {
1358 (0x04 << 5) | 1,
1359 (0x05 << 5) | 3,
1360 (0x01 << 5) | 1,
1361 (0x03 << 5) | 9,
1362 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1363 (0x00 << 5) | 0,
1364 (0x03 << 5) | 1,
1365 '7',
1366 (0x00 << 5) | 2,
1367 (0x03 << 5) | (sizeof("0:0")),
1368 '0', ':', '0', '\0'
1369 },
1370 .len = 22
1371 },
1372 {
1373 .data = {
1374 (0x04 << 5) | 1,
1375 (0x05 << 5) | 3,
1376 (0x01 << 5) | 1,
1377 (0x03 << 5) | 9,
1378 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1379 (0x00 << 5) | 0,
1380 (0x03 << 5) | 1,
1381 '7',
1382 (0x00 << 5) | 3,
1383 (0x03 << 5) | (sizeof("1:2")),
1384 '1', ':', '2', '\0'
1385 },
1386 .len = 22
1387 },
1388 {
1389 .data = {
1390 (0x04 << 5) | 1,
1391 (0x05 << 5) | 3,
1392 (0x01 << 5) | 1,
1393 (0x03 << 5) | 9,
1394 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1395 (0x00 << 5) | 0,
1396 (0x03 << 5) | 1,
1397 '7',
1398 (0x00 << 5) | 3,
1399 (0x03 << 5) | (sizeof("65535:65535")),
1400 '6', '5', '5', '3', '5', ':',
1401 '6', '5', '5', '3', '5', '\0'
1402 },
1403 .len = 30
1404 },
1405 };
1406 struct lwm2m_objlnk expected_value[] = {
1407 { 0, 0 }, { 1, 2 }, { LWM2M_OBJLNK_MAX_ID, LWM2M_OBJLNK_MAX_ID }
1408 };
1409
1410 test_msg.path.res_id = TEST_RES_OBJLNK;
1411
1412 for (i = 0; i < ARRAY_SIZE(expected_value); i++) {
1413 test_payload_set(payload[i]);
1414
1415 ret = do_write_op_senml_cbor(&test_msg);
1416 zassert_true(ret >= 0, "Error reported");
1417 zassert_mem_equal(&test_objlnk, &expected_value[i],
1418 sizeof(test_objlnk), "Invalid value parsed");
1419 zassert_equal(test_msg.in.offset, payload[i].len + 1,
1420 "Invalid packet offset");
1421 }
1422 }
1423
ZTEST(net_content_senml_cbor_nodata,test_get_objlnk_nodata)1424 ZTEST(net_content_senml_cbor_nodata, test_get_objlnk_nodata)
1425 {
1426 int ret;
1427
1428 test_msg.path.res_id = TEST_RES_OBJLNK;
1429
1430 ret = do_write_op_senml_cbor(&test_msg);
1431 zassert_equal(ret, -EBADMSG, "Invalid error code returned");
1432 }
1433
test_get_opaque(void)1434 static void test_get_opaque(void)
1435 {
1436 int ret;
1437 struct test_payload_buffer payload = {
1438 .data = {
1439 (0x04 << 5) | 1,
1440 (0x05 << 5) | 3,
1441 (0x01 << 5) | 1,
1442 (0x03 << 5) | 9,
1443 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1444 (0x00 << 5) | 0,
1445 (0x03 << 5) | 1,
1446 '4',
1447 (0x00 << 5) | 3,
1448 (0x03 << 5) | 11,
1449 't', 'e', 's', 't', '_', 'o', 'p', 'a', 'q', 'u', 'e'
1450 },
1451 .len = 29
1452 };
1453 uint8_t expected_value[11];
1454
1455 memcpy(expected_value, "test_opaque", 11 * sizeof(uint8_t));
1456
1457 test_msg.path.res_id = TEST_RES_OPAQUE;
1458
1459 test_payload_set(payload);
1460
1461 ret = do_write_op_senml_cbor(&test_msg);
1462 zassert_true(ret >= 0, "Error reported");
1463 zassert_mem_equal(test_opaque, expected_value, sizeof(expected_value),
1464 "Invalid value parsed");
1465 zassert_equal(test_msg.in.offset, payload.len + 1,
1466 "Invalid packet offset");
1467 }
1468
ZTEST(net_content_senml_cbor,test_opaque)1469 ZTEST(net_content_senml_cbor, test_opaque)
1470 {
1471 test_put_opaque();
1472 test_get_opaque();
1473 }
1474
ZTEST(net_content_senml_cbor_nodata,test_get_opaque_nodata)1475 ZTEST(net_content_senml_cbor_nodata, test_get_opaque_nodata)
1476 {
1477 int ret;
1478
1479 test_msg.path.res_id = TEST_RES_OPAQUE;
1480
1481 ret = do_write_op_senml_cbor(&test_msg);
1482 zassert_equal(ret, -EBADMSG, "Invalid error code returned");
1483 }
1484
ZTEST(net_content_senml_cbor,test_get_time)1485 ZTEST(net_content_senml_cbor, test_get_time)
1486 {
1487 int ret;
1488 time_t expected_value = 1170111600;
1489 struct test_payload_buffer payload = {
1490 .data = {
1491 (0x04 << 5) | 1,
1492 (0x05 << 5) | 3,
1493 (0x01 << 5) | 1,
1494 (0x03 << 5) | 9,
1495 '/', '6', '5', '5', '3', '5', '/', '0', '/',
1496 (0x00 << 5) | 0,
1497 (0x03 << 5) | 1,
1498 '9',
1499 (0x00 << 5) | 2,
1500 (0x00 << 5) | 26,
1501 0x45, 0xbe, 0x7c, 0x70
1502 },
1503 .len = 22
1504 };
1505
1506 test_msg.path.res_id = TEST_RES_TIME;
1507
1508 test_payload_set(payload);
1509
1510 ret = do_write_op_senml_cbor(&test_msg);
1511 zassert_true(ret >= 0, "Error reported");
1512 zassert_equal(test_time, expected_value, "Invalid value parsed");
1513 zassert_equal(test_msg.in.offset, payload.len + 1,
1514 "Invalid packet offset");
1515 }
1516
ZTEST(net_content_senml_cbor_nodata,test_get_time_nodata)1517 ZTEST(net_content_senml_cbor_nodata, test_get_time_nodata)
1518 {
1519 int ret;
1520
1521 test_msg.path.res_id = TEST_RES_TIME;
1522
1523 ret = do_write_op_senml_cbor(&test_msg);
1524 zassert_equal(ret, -EBADMSG, "Invalid error code returned");
1525 }
1526
1527 ZTEST_SUITE(net_content_senml_cbor, NULL, test_obj_init, test_prepare, NULL, NULL);
1528 ZTEST_SUITE(net_content_senml_cbor_nomem, NULL, test_obj_init, test_prepare_nomem, NULL, NULL);
1529 ZTEST_SUITE(net_content_senml_cbor_nodata, NULL, test_obj_init, test_prepare_nodata, NULL, NULL);
1530