diff options
author | Yuval Peress <peress@google.com> | 2022-11-04 00:39:07 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-11-04 23:00:28 +0000 |
commit | 365143e8cea63db9b1016ff336cad37463899c88 (patch) | |
tree | c35d479ddc19690422215891698ba200138a6ead | |
parent | 0fe46137f42b811762bcaedcd93f2f4587f6ccfa (diff) | |
download | chrome-ec-365143e8cea63db9b1016ff336cad37463899c88.tar.gz |
test: unit test uart_printf.c
Add unit tests to get uart_printf.c to 100% coverage.
BRANCH=none
BUG=none
TEST=twister
Signed-off-by: Yuval Peress <peress@google.com>
Change-Id: I2fdbea71a7048a2187c599052bf7180a97ee3ad2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4004509
Reviewed-by: Abe Levkoy <alevkoy@chromium.org>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r-- | common/uart_printf.c | 11 | ||||
-rwxr-xr-x | util/twister_tags.py | 1 | ||||
-rw-r--r-- | zephyr/test/uart_printf/CMakeLists.txt | 29 | ||||
-rw-r--r-- | zephyr/test/uart_printf/include/common.h | 12 | ||||
-rw-r--r-- | zephyr/test/uart_printf/include/printf.h | 23 | ||||
-rw-r--r-- | zephyr/test/uart_printf/include/uart.h | 28 | ||||
-rw-r--r-- | zephyr/test/uart_printf/prj.conf | 6 | ||||
-rw-r--r-- | zephyr/test/uart_printf/src/fakes.cc | 36 | ||||
-rw-r--r-- | zephyr/test/uart_printf/src/main.cc | 103 | ||||
-rw-r--r-- | zephyr/test/uart_printf/testcase.yaml | 8 |
10 files changed, 253 insertions, 4 deletions
diff --git a/common/uart_printf.c b/common/uart_printf.c index 01fd1353d2..a4fecf5c73 100644 --- a/common/uart_printf.c +++ b/common/uart_printf.c @@ -31,9 +31,10 @@ int uart_putc(int c) int uart_puts(const char *outstr) { /* Put all characters in the output buffer */ - while (*outstr) { - if (__tx_char(NULL, *outstr++) != 0) + for (; *outstr != '\0'; ++outstr) { + if (__tx_char(NULL, *outstr) != 0) { break; + } } uart_tx_start(); @@ -48,8 +49,9 @@ int uart_put(const char *out, int len) /* Put all characters in the output buffer */ for (written = 0; written < len; written++) { - if (__tx_char(NULL, *out++) != 0) + if (__tx_char(NULL, *out++) != 0) { break; + } } uart_tx_start(); @@ -63,8 +65,9 @@ int uart_put_raw(const char *out, int len) /* Put all characters in the output buffer */ for (written = 0; written < len; written++) { - if (uart_tx_char_raw(NULL, *out++) != 0) + if (uart_tx_char_raw(NULL, *out++) != 0) { break; + } } uart_tx_start(); diff --git a/util/twister_tags.py b/util/twister_tags.py index 692fdc1e99..68ac5846bd 100755 --- a/util/twister_tags.py +++ b/util/twister_tags.py @@ -29,6 +29,7 @@ TAG_TO_DESCRIPTION = { "mkbp": "Testing the MKBP (Matrix Keyboard Protocol) stack", "system": "Directly test functions in common/system.c or shim/src/system.c", "spi": "SPI related tests", + "uart": "UART related tests", } SCRIPT_PATH = os.path.realpath(__file__) diff --git a/zephyr/test/uart_printf/CMakeLists.txt b/zephyr/test/uart_printf/CMakeLists.txt new file mode 100644 index 0000000000..78e0791652 --- /dev/null +++ b/zephyr/test/uart_printf/CMakeLists.txt @@ -0,0 +1,29 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +cmake_minimum_required(VERSION 3.20.0) +set(CMAKE_CXX_STANDARD 20) + +project(uart_printf) + +set(SOURCES + ../../../common/uart_printf.c + src/fakes.cc + src/main.cc +) + +# Create the unittest libraries and binary +find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +target_sources(testbinary + PRIVATE + ../../../common/uart_printf.c + src/fakes.cc + src/main.cc +) + +target_include_directories(testbinary + PRIVATE + include +) diff --git a/zephyr/test/uart_printf/include/common.h b/zephyr/test/uart_printf/include/common.h new file mode 100644 index 0000000000..79af9594b3 --- /dev/null +++ b/zephyr/test/uart_printf/include/common.h @@ -0,0 +1,12 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef ZEPHYR_TEST_UART_PRINTF_INCLUDE_COMMON_H_ +#define ZEPHYR_TEST_UART_PRINTF_INCLUDE_COMMON_H_ + +#define EC_SUCCESS 0 +#define EC_ERROR_OVERFLOW -1 + +#endif /* ZEPHYR_TEST_UART_PRINTF_INCLUDE_COMMON_H_ */ diff --git a/zephyr/test/uart_printf/include/printf.h b/zephyr/test/uart_printf/include/printf.h new file mode 100644 index 0000000000..b6fcf6337d --- /dev/null +++ b/zephyr/test/uart_printf/include/printf.h @@ -0,0 +1,23 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef ZEPHYR_TEST_UART_PRINTF_INCLUDE_PRINTF_H_ +#define ZEPHYR_TEST_UART_PRINTF_INCLUDE_PRINTF_H_ + +#include <zephyr/fff.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef int (*vfnprintf_addchar_t)(void *, int); +DECLARE_FAKE_VALUE_FUNC(int, vfnprintf, vfnprintf_addchar_t, void *, + const char *, va_list); + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_TEST_UART_PRINTF_INCLUDE_PRINTF_H_ */ diff --git a/zephyr/test/uart_printf/include/uart.h b/zephyr/test/uart_printf/include/uart.h new file mode 100644 index 0000000000..51fd42a17e --- /dev/null +++ b/zephyr/test/uart_printf/include/uart.h @@ -0,0 +1,28 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef ZEPHYR_TEST_UART_PRINTF_INCLUDE_UART_H_ +#define ZEPHYR_TEST_UART_PRINTF_INCLUDE_UART_H_ + +#include <zephyr/fff.h> + +#ifdef __cplusplus +extern "C" { +#endif + +int uart_putc(int c); +int uart_puts(const char *outstr); +int uart_put(const char *out, int len); +int uart_put_raw(const char *out, int len); +int uart_printf(const char *format, ...); + +DECLARE_FAKE_VALUE_FUNC(int, uart_tx_char_raw, void *, int); +DECLARE_FAKE_VOID_FUNC(uart_tx_start); + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_TEST_UART_PRINTF_INCLUDE_UART_H_ */ diff --git a/zephyr/test/uart_printf/prj.conf b/zephyr/test/uart_printf/prj.conf new file mode 100644 index 0000000000..6ec8f4d2a9 --- /dev/null +++ b/zephyr/test/uart_printf/prj.conf @@ -0,0 +1,6 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y diff --git a/zephyr/test/uart_printf/src/fakes.cc b/zephyr/test/uart_printf/src/fakes.cc new file mode 100644 index 0000000000..0bb5fea1eb --- /dev/null +++ b/zephyr/test/uart_printf/src/fakes.cc @@ -0,0 +1,36 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/fff.h> +#include <zephyr/ztest.h> + +#include "printf.h" +#include "uart.h" + +DEFINE_FFF_GLOBALS; + +/* printf.h */ +DEFINE_FAKE_VALUE_FUNC(int, vfnprintf, vfnprintf_addchar_t, void *, + const char *, va_list); + +/* uart.h */ +DEFINE_FAKE_VALUE_FUNC(int, uart_tx_char_raw, void *, int); +DEFINE_FAKE_VOID_FUNC(uart_tx_start); + +static void fake_reset_rule_before(const struct ztest_unit_test *test, + void *data) +{ + ARG_UNUSED(test); + ARG_UNUSED(data); + + /* printf.h */ + RESET_FAKE(vfnprintf); + + /* uart.h */ + RESET_FAKE(uart_tx_char_raw); + RESET_FAKE(uart_tx_start); +} + +ZTEST_RULE(fake_reset, fake_reset_rule_before, nullptr); diff --git a/zephyr/test/uart_printf/src/main.cc b/zephyr/test/uart_printf/src/main.cc new file mode 100644 index 0000000000..e84c5c76ba --- /dev/null +++ b/zephyr/test/uart_printf/src/main.cc @@ -0,0 +1,103 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <cstring> +#include <ranges> +#include <string> + +#include <zephyr/ztest.h> + +#include "common.h" +#include "printf.h" +#include "uart.h" + +ZTEST_SUITE(uart_printf, nullptr, nullptr, nullptr, nullptr, nullptr); + +ZTEST(uart_printf, test_uart_putc) +{ + int return_vals[] = { 0, -1 }; + + SET_RETURN_SEQ(uart_tx_char_raw, return_vals, 2); + + zassert_ok(uart_putc(5)); + zassert_equal(EC_ERROR_OVERFLOW, uart_putc(5)); +} + +ZTEST(uart_printf, test_uart_put_success) +{ + const std::string test_string = "test string"; + std::string output_string; + + /* Print the whole string */ + zassert_equal(test_string.size(), + static_cast<size_t>(uart_put(test_string.c_str(), + test_string.size()))); + zassert_equal(test_string.size(), uart_tx_char_raw_fake.call_count); + + /* Copy the history so it's easier to assert */ + for (auto i : + std::views::iota(static_cast<size_t>(0), test_string.size())) { + output_string += uart_tx_char_raw_fake.arg1_history[i]; + } + + /* Verify that the string was passed to uart_tx_char_raw() */ + zassert_equal(test_string, output_string); +} + +ZTEST(uart_printf, test_uart_put_fail_tx) +{ + const char test_string[] = "\n"; + + uart_tx_char_raw_fake.return_val = -1; + + /* Try printing the newline */ + zassert_equal(0, uart_put(test_string, 1)); + zassert_equal(1, uart_tx_char_raw_fake.call_count); + zassert_equal('\r', uart_tx_char_raw_fake.arg1_val); +} + +ZTEST(uart_printf, test_uart_puts_fail_tx) +{ + const char test_string[] = "\n\0"; + + uart_tx_char_raw_fake.return_val = -1; + + /* Try printing the newline */ + zassert_equal(EC_ERROR_OVERFLOW, uart_puts(test_string)); + zassert_equal(1, uart_tx_char_raw_fake.call_count); + zassert_equal('\r', uart_tx_char_raw_fake.arg1_val); +} + +ZTEST(uart_printf, test_uart_put_raw_fail_tx) +{ + const char test_string[] = "\n"; + + uart_tx_char_raw_fake.return_val = -1; + + /* Try printing the newline */ + zassert_equal(0, uart_put_raw(test_string, 1)); + zassert_equal(1, uart_tx_char_raw_fake.call_count); + zassert_equal('\n', uart_tx_char_raw_fake.arg1_val); +} + +static int vfnprintf_custom_fake_expect_int_arg; +static int vfnprintf_custom_fake(vfnprintf_addchar_t, void *, const char *, + va_list alist) +{ + zassert_equal(vfnprintf_custom_fake_expect_int_arg, va_arg(alist, int)); + return 0; +} +ZTEST(uart_printf, test_uart_printf) +{ + const char test_format[] = "d=%d"; + + vfnprintf_custom_fake_expect_int_arg = 5; + vfnprintf_fake.custom_fake = vfnprintf_custom_fake; + + zassert_ok( + uart_printf(test_format, vfnprintf_custom_fake_expect_int_arg)); + zassert_equal(1, vfnprintf_fake.call_count); + zassert_equal(test_format, vfnprintf_fake.arg2_val); +} diff --git a/zephyr/test/uart_printf/testcase.yaml b/zephyr/test/uart_printf/testcase.yaml new file mode 100644 index 0000000000..0315abab6f --- /dev/null +++ b/zephyr/test/uart_printf/testcase.yaml @@ -0,0 +1,8 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +tests: + uart.printf: + tags: uart + type: unit |