Skip to content

Commit 88c70cf

Browse files
tests: net: lib: lwm2m: adds test case for lwm2m_set/get_opaque
Verifies that the memory copy works as expected - return codes are correct - resource length parameter works Signed-off-by: Stefan Schwendeler <Stefan.Schwendeler@husqvarnagroup.com>
1 parent 005b49d commit 88c70cf

File tree

1 file changed

+124
-1
lines changed

1 file changed

+124
-1
lines changed

tests/net/lib/lwm2m/lwm2m_registry/src/lwm2m_registry.c

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ ZTEST(lwm2m_registry, test_strings)
437437
zassert_equal(ret, 0);
438438
memset(p, 0xff, len); /* Pre-fill buffer to check */
439439

440-
/* Handle strings in string resources */
440+
/* Handle strings in string resources. */
441441
ret = lwm2m_set_string(&path, uri);
442442
zassert_equal(ret, 0);
443443
ret = lwm2m_get_res_buf(&path, (void **)&p, NULL, &len, NULL);
@@ -482,6 +482,129 @@ ZTEST(lwm2m_registry, test_strings)
482482
zassert_equal(ret, -ENOMEM);
483483
}
484484

485+
ZTEST(lwm2m_registry, test_opaque)
486+
{
487+
int ret;
488+
struct lwm2m_obj_path path = LWM2M_OBJ(0, 0, 0);
489+
490+
uint16_t res_maxlen;
491+
uint16_t res_len;
492+
void *res_buf;
493+
494+
zassert_ok(lwm2m_get_res_buf(&path, (void **)&res_buf, &res_maxlen, &res_len, NULL));
495+
/* test cases are not useful when the resource is too small */
496+
zassert_true(res_maxlen >= 6);
497+
498+
const char pattern_set = 0xA5;
499+
char buf_set[res_maxlen];
500+
const char pattern_get = 0x5A;
501+
char buf_get[res_maxlen];
502+
503+
struct testcases {
504+
char *name;
505+
uint16_t set_len;
506+
int set_ret;
507+
uint16_t get_len;
508+
int get_ret;
509+
bool res_len_null;
510+
bool set_ptr_null;
511+
bool get_ptr_null;
512+
uint32_t checks;
513+
};
514+
515+
const struct testcases tc[] = {
516+
/* upper buffer boarder */
517+
{.name = "max buffer",
518+
.set_len = sizeof(buf_set),
519+
.get_len = sizeof(buf_get),
520+
.checks = 0x17},
521+
{.name = "set overflow",
522+
.set_len = sizeof(buf_set) + 1,
523+
.set_ret = -ENOMEM,
524+
.checks = 0x1},
525+
{.name = "get underflow",
526+
.set_len = sizeof(buf_set),
527+
.get_len = sizeof(buf_get) - 1,
528+
.get_ret = -ENOMEM,
529+
.checks = 0x3},
530+
/* lower buffer boundary */
531+
{.name = "min buffer", .set_len = 0, .get_len = sizeof(buf_get), .checks = 0x1F},
532+
/* edge case: zero sized resource fits into zero sized buffer */
533+
{.name = "zero buffer", .set_len = 0, .get_len = 0, .checks = 0x1F},
534+
/* edge case 2: zero sized resource fits into a null pointer */
535+
{.name = "null get buffer",
536+
.set_len = 0,
537+
.get_len = 0,
538+
.get_ptr_null = true,
539+
.checks = 0x1F},
540+
{.name = "null set buffer",
541+
.set_len = 0,
542+
.get_len = 0,
543+
.set_ptr_null = true,
544+
.checks = 0x1F},
545+
/* something in between the buffer boundary */
546+
{.name = "mid buffer",
547+
.set_len = sizeof(buf_set) / 2,
548+
.get_len = sizeof(buf_get),
549+
.checks = 0x1F},
550+
/* The `res_len` is optional an can be null */
551+
{.name = "null resource len",
552+
.set_len = sizeof(buf_set) / 3,
553+
.get_len = sizeof(buf_get),
554+
.res_len_null = true,
555+
.checks = 0xF},
556+
};
557+
558+
for (size_t i = 0; i < ARRAY_SIZE(tc); i++) {
559+
/* reset */
560+
uint32_t checks = 0;
561+
const uint16_t buf_set_len = tc[i].set_len;
562+
char *tc_buf_set = tc[i].set_ptr_null ? NULL : buf_set;
563+
char *tc_buf_get = tc[i].get_ptr_null ? NULL : buf_get;
564+
uint16_t buf_get_len = UINT16_MAX;
565+
uint16_t *tc_buf_get_len = tc[i].res_len_null ? NULL : &buf_get_len;
566+
567+
memset(res_buf, 0, res_maxlen);
568+
memset(buf_set, pattern_set, res_maxlen);
569+
memset(buf_get, pattern_get, res_maxlen);
570+
571+
/* set */
572+
ret = lwm2m_set_opaque(&path, tc_buf_set, buf_set_len);
573+
zassert_equal(ret, tc[i].set_ret, "case %d: %s", i, tc[i].name);
574+
WRITE_BIT(checks, 0, 1);
575+
if (tc[i].set_ret != 0) {
576+
goto finally;
577+
}
578+
579+
/* get */
580+
ret = lwm2m_get_opaque(&path, tc_buf_get, tc[i].get_len, tc_buf_get_len);
581+
zassert_equal(ret, tc[i].get_ret, "case %d: %s", i, tc[i].name);
582+
WRITE_BIT(checks, 1, 1);
583+
if (tc[i].get_ret != 0) {
584+
goto finally;
585+
}
586+
587+
/* check memory */
588+
zassert_mem_equal(buf_set, buf_get, buf_set_len, "case %d: %s", i, tc[i].name);
589+
WRITE_BIT(checks, 2, 1);
590+
if (buf_set_len < sizeof(buf_set)) {
591+
/* written too much? */
592+
zassert_equal(buf_get[buf_set_len], pattern_get, "case %d: %s", i,
593+
tc[i].name);
594+
WRITE_BIT(checks, 3, 1);
595+
}
596+
597+
/* check resource length */
598+
if (!tc[i].res_len_null) {
599+
zassert_equal(buf_get_len, tc[i].set_len, "case %d: %s", i, tc[i].name);
600+
WRITE_BIT(checks, 4, 1);
601+
}
602+
finally:
603+
zassert_equal(checks, tc[i].checks, "case %d: %s: checks: %x != %x", i, tc[i].name,
604+
checks, tc[i].checks);
605+
}
606+
}
607+
485608
ZTEST(lwm2m_registry, test_lock_unlock)
486609
{
487610
/* Should be recursive mutex and should not block */

0 commit comments

Comments
 (0)