From 1fc67d22d18c912cecbe49abf9acc4474e8786a6 Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Tue, 25 Jun 2024 14:15:59 +0200 Subject: [PATCH] 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 --- tests/unit/util/CMakeLists.txt | 7 ++- tests/unit/util/main.c | 100 +++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/tests/unit/util/CMakeLists.txt b/tests/unit/util/CMakeLists.txt index a8f1f4e76db..8c550241bd7 100644 --- a/tests/unit/util/CMakeLists.txt +++ b/tests/unit/util/CMakeLists.txt @@ -4,7 +4,12 @@ cmake_minimum_required(VERSION 3.20.0) project(util) find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE}) -target_sources(testbinary PRIVATE main.c ${ZEPHYR_BASE}/lib/utils/dec.c) +target_sources(testbinary + PRIVATE + main.c + ${ZEPHYR_BASE}/lib/utils/dec.c + ${ZEPHYR_BASE}/lib/utils/utf8.c +) if(CONFIG_CPP) # When testing for C++ force test file C++ compilation diff --git a/tests/unit/util/main.c b/tests/unit/util/main.c index dad28a9ec48..161c245f2b9 100644 --- a/tests/unit/util/main.c +++ b/tests/unit/util/main.c @@ -6,6 +6,7 @@ #include #include +#include #include ZTEST(util, test_u8_to_dec) { @@ -813,4 +814,103 @@ ZTEST(util, test_SIZEOF_FIELD) BUILD_ASSERT(SIZEOF_FIELD(struct test_t, d) == 2, "The d member is 2-byte wide."); } +ZTEST(util, test_utf8_trunc_truncated) +{ + char test_str[] = "€€€"; + char expected_result[] = "€€"; + + /* Remove last byte from truncated_test_str and verify that it first is incorrectly + * truncated, followed by a proper truncation and verification + */ + test_str[strlen(test_str) - 1] = '\0'; + zassert(strcmp(test_str, "€€€") != 0, "Failed to do invalid truncation"); + zassert(strcmp(test_str, expected_result) != 0, "Failed to do invalid truncation"); + + utf8_trunc(test_str); + + zassert_str_equal(test_str, expected_result, "Failed to truncate"); +} + +ZTEST(util, test_utf8_trunc_not_truncated) +{ + /* Attempt to truncate a valid UTF8 string and verify no changed */ + char test_str[] = "€€€"; + char expected_result[] = "€€€"; + + utf8_trunc(test_str); + + zassert_str_equal(test_str, expected_result, "Failed to truncate"); +} + +ZTEST(util, test_utf8_trunc_zero_length) +{ + /* Attempt to truncate a valid UTF8 string and verify no changed */ + char test_str[] = ""; + char expected_result[] = ""; + + utf8_trunc(test_str); + + zassert_str_equal(test_str, expected_result, "Failed to truncate"); +} + +ZTEST(util, test_utf8_lcpy_truncated) +{ + /* dest_str size is based on storing 2 * € plus the null terminator plus an extra space to + * verify that it's truncated properly + */ + char dest_str[strlen("€") * 2 + 1 + 1]; + char test_str[] = "€€€"; + char expected_result[] = "€€"; + + utf8_lcpy(dest_str, test_str, sizeof((dest_str))); + + zassert_str_equal(dest_str, expected_result, "Failed to copy"); +} + +ZTEST(util, test_utf8_lcpy_not_truncated) +{ + /* dest_str size is based on storing 3 * € plus the null terminator */ + char dest_str[strlen("€") * 3 + 1]; + char test_str[] = "€€€"; + char expected_result[] = "€€€"; + + utf8_lcpy(dest_str, test_str, sizeof((dest_str))); + + zassert_str_equal(dest_str, expected_result, "Failed to truncate"); +} + +ZTEST(util, test_utf8_lcpy_zero_length_copy) +{ + /* dest_str size is based on the null terminator */ + char dest_str[1]; + char test_str[] = ""; + char expected_result[] = ""; + + utf8_lcpy(dest_str, test_str, sizeof((dest_str))); + + zassert_str_equal(dest_str, expected_result, "Failed to truncate"); +} + +ZTEST(util, test_utf8_lcpy_zero_length_dest) +{ + char dest_str[] = "A"; + char test_str[] = ""; + char expected_result[] = "A"; /* expect no changes to dest_str */ + + utf8_lcpy(dest_str, test_str, 0); + + zassert_str_equal(dest_str, expected_result, "Failed to truncate"); +} + +ZTEST(util, test_utf8_lcpy_null_termination) +{ + char dest_str[] = "DEADBEEF"; + char test_str[] = "DEAD"; + char expected_result[] = "DEAD"; + + utf8_lcpy(dest_str, test_str, sizeof(dest_str)); + + zassert_str_equal(dest_str, expected_result, "Failed to truncate"); +} + ZTEST_SUITE(util, NULL, NULL, NULL, NULL, NULL);