Skip to content

Commit 8c00f5a

Browse files
committed
tests: unit: util: Add unit test of utf8_trunc and utf8_lcpy
The two functions did not have unit tests, and at least one issue with input validation have been indentified and fixed prior to these. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 70a7280 commit 8c00f5a

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

tests/unit/util/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ cmake_minimum_required(VERSION 3.20.0)
44

55
project(util)
66
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
7-
target_sources(testbinary PRIVATE main.c ${ZEPHYR_BASE}/lib/utils/dec.c)
7+
target_sources(testbinary
8+
PRIVATE
9+
main.c
10+
${ZEPHYR_BASE}/lib/utils/dec.c
11+
${ZEPHYR_BASE}/lib/utils/utf8.c
12+
)
813

914
if(CONFIG_CPP)
1015
# When testing for C++ force test file C++ compilation

tests/unit/util/main.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <zephyr/ztest.h>
88
#include <zephyr/sys/util.h>
9+
#include <stdio.h>
910
#include <string.h>
1011

1112
ZTEST(util, test_u8_to_dec) {
@@ -813,4 +814,103 @@ ZTEST(util, test_SIZEOF_FIELD)
813814
BUILD_ASSERT(SIZEOF_FIELD(struct test_t, d) == 2, "The d member is 2-byte wide.");
814815
}
815816

817+
ZTEST(util, test_utf8_trunc_truncated)
818+
{
819+
char test_str[] = "€€€";
820+
char expected_result[] = "€€";
821+
822+
/* Remove last byte from truncated_test_str and verify that it first is incorrectly
823+
* truncated, followed by a proper truncation and verification
824+
*/
825+
test_str[strlen(test_str) - 1] = '\0';
826+
zassert(strcmp(test_str, "€€€") != 0, "Failed to do invalid truncation");
827+
zassert(strcmp(test_str, expected_result) != 0, "Failed to do invalid truncation");
828+
829+
utf8_trunc(test_str);
830+
831+
zassert_str_equal(test_str, expected_result, "Failed to truncate");
832+
}
833+
834+
ZTEST(util, test_utf8_trunc_not_truncated)
835+
{
836+
/* Attempt to truncate a valid UTF8 string and verify no changed */
837+
char test_str[] = "€€€";
838+
char expected_result[] = "€€€";
839+
840+
utf8_trunc(test_str);
841+
842+
zassert_str_equal(test_str, expected_result, "Failed to truncate");
843+
}
844+
845+
ZTEST(util, test_utf8_trunc_zero_length)
846+
{
847+
/* Attempt to truncate a valid UTF8 string and verify no changed */
848+
char test_str[] = "";
849+
char expected_result[] = "";
850+
851+
utf8_trunc(test_str);
852+
853+
zassert_str_equal(test_str, expected_result, "Failed to truncate");
854+
}
855+
856+
ZTEST(util, test_utf8_lcpy_truncated)
857+
{
858+
/* dest_str size is based on storing 2 * € plus the null terminator plus an extra space to
859+
* verify that it's truncated properly
860+
*/
861+
char dest_str[strlen("€") * 2 + 1 + 1];
862+
char test_str[] = "€€€";
863+
char expected_result[] = "€€";
864+
865+
utf8_lcpy(dest_str, test_str, sizeof((dest_str)));
866+
867+
zassert_str_equal(dest_str, expected_result, "Failed to copy");
868+
}
869+
870+
ZTEST(util, test_utf8_lcpy_not_truncated)
871+
{
872+
/* dest_str size is based on storing 3 * € plus the null terminator */
873+
char dest_str[strlen("€") * 3 + 1];
874+
char test_str[] = "€€€";
875+
char expected_result[] = "€€€";
876+
877+
utf8_lcpy(dest_str, test_str, sizeof((dest_str)));
878+
879+
zassert_str_equal(dest_str, expected_result, "Failed to truncate");
880+
}
881+
882+
ZTEST(util, test_utf8_lcpy_zero_length_copy)
883+
{
884+
/* dest_str size is based on the null terminator */
885+
char dest_str[1];
886+
char test_str[] = "";
887+
char expected_result[] = "";
888+
889+
utf8_lcpy(dest_str, test_str, sizeof((dest_str)));
890+
891+
zassert_str_equal(dest_str, expected_result, "Failed to truncate");
892+
}
893+
894+
ZTEST(util, test_utf8_lcpy_zero_length_dest)
895+
{
896+
char dest_str[] = "A";
897+
char test_str[] = "";
898+
char expected_result[] = "A"; /* expect no changes to dest_str */
899+
900+
utf8_lcpy(dest_str, test_str, 0);
901+
902+
zassert_str_equal(dest_str, expected_result, "Failed to truncate");
903+
}
904+
905+
ZTEST(util, test_utf8_lcpy_null_termination)
906+
{
907+
char dest_str[] = "DEADBEEF";
908+
char test_str[] = "DEAD";
909+
char expected_result[] = "DEAD";
910+
911+
utf8_lcpy(dest_str, test_str, sizeof(dest_str));
912+
913+
zassert_str_equal(dest_str, expected_result, "Failed to truncate");
914+
}
915+
816916
ZTEST_SUITE(util, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)