Skip to content

Commit 50cc558

Browse files
nordic-krchnashif
authored andcommitted
tests: lib: cbprintf_package: Test for CBPRINTF_PACKAGE_COPY_KEEP_RO_STR
Extend test to validate CBPRINTF_PACKAGE_COPY_KEEP_RO_STR flag. Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent a33f7de commit 50cc558

File tree

1 file changed

+17
-9
lines changed
  • tests/lib/cbprintf_package/src

1 file changed

+17
-9
lines changed

tests/lib/cbprintf_package/src/main.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ static void test_cbprintf_ro_rw_loc_const_char_ptr(void)
670670
#undef TEST_FMT
671671
}
672672

673-
static void test_cbprintf_rw_loc_const_char_ptr(void)
673+
static void cbprintf_rw_loc_const_char_ptr(bool keep_ro_str)
674674
{
675675
/* Test requires that static packaging is applied. Runtime packaging
676676
* cannot be tricked because it checks pointers against read only
@@ -720,28 +720,30 @@ static void test_cbprintf_rw_loc_const_char_ptr(void)
720720
zassert_equal(hdr[2], 0, NULL);
721721
zassert_equal(hdr[3], 2, NULL);
722722

723-
uint32_t cpy_flags = CBPRINTF_PACKAGE_COPY_RW_STR |
724-
CBPRINTF_PACKAGE_COPY_KEEP_RO_STR;
723+
uint32_t copy_flags = CBPRINTF_PACKAGE_COPY_RW_STR |
724+
(keep_ro_str ? CBPRINTF_PACKAGE_COPY_KEEP_RO_STR : 0);
725725

726726
/* Calculate size needed for package with appended read-only strings. */
727727
clen = cbprintf_package_copy(spackage, sizeof(spackage), NULL, 0,
728-
cpy_flags, NULL, 0);
728+
copy_flags, NULL, 0);
729729

730-
/* Length will be increased by 2 string lengths + null terminators. */
731-
zassert_equal(clen, slen + (int)strlen(test_str1) + 1, NULL);
730+
int exp_len = slen + (int)strlen(test_str1) + 1 - (keep_ro_str ? 0 : 1);
731+
732+
/* Length will be increased by string length + null terminator. */
733+
zassert_equal(clen, exp_len, NULL);
732734

733735
uint8_t __aligned(CBPRINTF_PACKAGE_ALIGNMENT) cpackage[clen];
734736

735737
clen2 = cbprintf_package_copy(spackage, sizeof(spackage), cpackage, sizeof(cpackage),
736-
cpy_flags, NULL, 0);
738+
copy_flags, NULL, 0);
737739

738740
zassert_equal(clen, clen2, NULL);
739741

740742
hdr = cpackage;
741743

742-
/* Check that one string has been appended. */
744+
/* Check that one string has been appended. Second is detected to be RO */
743745
zassert_equal(hdr[1], 1, NULL);
744-
zassert_equal(hdr[2], 1, NULL);
746+
zassert_equal(hdr[2], keep_ro_str ? 1 : 0, NULL);
745747
zassert_equal(hdr[3], 0, NULL);
746748

747749
check_package(spackage, slen, exp_str);
@@ -750,6 +752,12 @@ static void test_cbprintf_rw_loc_const_char_ptr(void)
750752
#undef TEST_FMT
751753
}
752754

755+
static void test_cbprintf_rw_loc_const_char_ptr(void)
756+
{
757+
cbprintf_rw_loc_const_char_ptr(true);
758+
cbprintf_rw_loc_const_char_ptr(false);
759+
}
760+
753761
static void test_cbprintf_must_runtime_package(void)
754762
{
755763
int rv;

0 commit comments

Comments
 (0)