summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2022-09-20 10:54:31 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-09-23 22:16:16 +0000
commitb9caf4a588579e1d8e623a27db2862cf65a319ff (patch)
treea8210baa833e2e872c338387a81515bc4266fd45
parent9bfa3fbf1065c8a65acf3e33e28d6e61063d0de5 (diff)
downloadchrome-ec-b9caf4a588579e1d8e623a27db2862cf65a319ff.tar.gz
zephyr: tests: Test timer_init() in common/timer.c
Test the timer initialization function. Add some mocks for the board-level functions and the jump tag API. BRANCH=None BUG=b:247608485 TEST=./twister Signed-off-by: Tristan Honscheid <honscheid@google.com> Change-Id: I7edeca26e93f18c467618691967299f411507166 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3910977 Reviewed-by: Tomasz Michalec <tmichalec@google.com> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r--common/system.c3
-rw-r--r--zephyr/test/drivers/CMakeLists.txt1
-rw-r--r--zephyr/test/drivers/Kconfig3
-rw-r--r--zephyr/test/drivers/testcase.yaml3
-rw-r--r--zephyr/test/drivers/timer/CMakeLists.txt7
-rw-r--r--zephyr/test/drivers/timer/src/timer.c91
6 files changed, 107 insertions, 1 deletions
diff --git a/common/system.c b/common/system.c
index b4387f5257..8302ae8885 100644
--- a/common/system.c
+++ b/common/system.c
@@ -369,7 +369,8 @@ int system_add_jump_tag(uint16_t tag, int version, int size, const void *data)
return EC_SUCCESS;
}
-const uint8_t *system_get_jump_tag(uint16_t tag, int *version, int *size)
+test_mockable const uint8_t *system_get_jump_tag(uint16_t tag, int *version,
+ int *size)
{
const struct jump_tag *t;
int used = 0;
diff --git a/zephyr/test/drivers/CMakeLists.txt b/zephyr/test/drivers/CMakeLists.txt
index f436fa42f9..ca154dba79 100644
--- a/zephyr/test/drivers/CMakeLists.txt
+++ b/zephyr/test/drivers/CMakeLists.txt
@@ -51,6 +51,7 @@ add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_KEYBOARD_SCAN keyboard_scan)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_LED_DRIVER led_driver)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_MKBP mkbp)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_RT9490 rt9490)
+add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_TIMER timer)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_USB_MALFUNCTION_SINK usb_malfunction_sink)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_USB_PD_DPS dps)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_USB_PORT_POWER_DUMB usb_port_power_dumb)
diff --git a/zephyr/test/drivers/Kconfig b/zephyr/test/drivers/Kconfig
index 9ebfa0cdfd..e1c028572e 100644
--- a/zephyr/test/drivers/Kconfig
+++ b/zephyr/test/drivers/Kconfig
@@ -32,6 +32,9 @@ config LINK_TEST_SUITE_RT9490
config LINK_TEST_SUITE_SYSTEM
bool "Link tests for common system code"
+config LINK_TEST_SUITE_TIMER
+ bool "Link and test the timer tests"
+
config LINK_TEST_SUITE_USB_MALFUNCTION_SINK
bool "Link and test the usb_malfunction_sink tests"
diff --git a/zephyr/test/drivers/testcase.yaml b/zephyr/test/drivers/testcase.yaml
index b06d40acba..281b804f7a 100644
--- a/zephyr/test/drivers/testcase.yaml
+++ b/zephyr/test/drivers/testcase.yaml
@@ -73,6 +73,9 @@ tests:
- CONFIG_LINK_TEST_SUITE_SYSTEM=y
- CONFIG_PLATFORM_EC_BOARD_VERSION_CBI=y
- CONFIG_POWER_SEQUENCE_MOCK=y
+ drivers.timer:
+ extra_configs:
+ - CONFIG_LINK_TEST_SUITE_TIMER=y
drivers.usb_port_power_dumb:
extra_args: DTC_OVERLAY_FILE="./boards/native_posix.overlay;./usb_port_power_dumb/usba.dts"
extra_configs:
diff --git a/zephyr/test/drivers/timer/CMakeLists.txt b/zephyr/test/drivers/timer/CMakeLists.txt
new file mode 100644
index 0000000000..8327ccdafb
--- /dev/null
+++ b/zephyr/test/drivers/timer/CMakeLists.txt
@@ -0,0 +1,7 @@
+# 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.
+
+target_sources(app PRIVATE
+ src/timer.c
+)
diff --git a/zephyr/test/drivers/timer/src/timer.c b/zephyr/test/drivers/timer/src/timer.c
new file mode 100644
index 0000000000..f5055904b1
--- /dev/null
+++ b/zephyr/test/drivers/timer/src/timer.c
@@ -0,0 +1,91 @@
+/* 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/ztest.h>
+#include <zephyr/fff.h>
+
+#include "host_command.h"
+#include "test/drivers/test_state.h"
+#include "timer.h"
+
+BUILD_ASSERT(IS_ENABLED(CONFIG_HWTIMER_64BIT),
+ "Tests expect the 64-bit HW timer");
+
+/**
+ * @brief Sets the timestamp returned by the system_get_jump_tag mock. Set to
+ * UNIT64_MAX to make the mock return NULL.
+ */
+static uint64_t jump_tag_time;
+
+/* Sets the initial timer value */
+FAKE_VALUE_FUNC(int, __hw_clock_source_init64, uint64_t);
+
+FAKE_VALUE_FUNC(uint8_t *, system_get_jump_tag, uint16_t, int *, int *);
+
+ZTEST(timer, init_from_jump_tag)
+{
+ /* When initializing after a system jump, the timer should get set to
+ * the time before the jump (stored in a jump tag)
+ */
+
+ /* Set up mock to return this time */
+ jump_tag_time = 0x0123456789abcdef;
+
+ timer_init();
+
+ zassert_equal(1, system_get_jump_tag_fake.call_count, NULL);
+ zassert_equal(1, __hw_clock_source_init64_fake.call_count, NULL);
+ zassert_equal(jump_tag_time,
+ __hw_clock_source_init64_fake.arg0_history[0], NULL);
+}
+
+ZTEST(timer, init_from_zero)
+{
+ /* When there is no jump tag, the timer should initialize to zero. */
+
+ /* Simulate no jump tag stored */
+ jump_tag_time = UINT64_MAX;
+
+ timer_init();
+
+ zassert_equal(1, system_get_jump_tag_fake.call_count, NULL);
+ zassert_equal(1, __hw_clock_source_init64_fake.call_count, NULL);
+ zassert_equal(0, __hw_clock_source_init64_fake.arg0_history[0], NULL);
+}
+
+/**
+ * @brief Custom fake for system_get_jump_tag
+ *
+ * @param tag Which jump tag to retrieve, ignored in this application.
+ * @param version Output param to write jump tag version
+ * @param size Output param to write tag size
+ * @return uint8_t* Pointer to tag data
+ */
+static uint8_t *system_get_jump_tag_custom_fake(uint16_t tag, int *version,
+ int *size)
+{
+ ARG_UNUSED(tag);
+
+ /* Pretend the tag doesn't exist if set to this value */
+ if (jump_tag_time == UINT64_MAX) {
+ return NULL;
+ }
+
+ if (version)
+ *version = 1;
+ if (size)
+ *size = sizeof(jump_tag_time);
+ return (uint8_t *)&jump_tag_time;
+}
+
+static void reset(void *data)
+{
+ ARG_UNUSED(data);
+
+ RESET_FAKE(__hw_clock_source_init64);
+ RESET_FAKE(system_get_jump_tag)
+ system_get_jump_tag_fake.custom_fake = system_get_jump_tag_custom_fake;
+}
+
+ZTEST_SUITE(timer, drivers_predicate_post_main, NULL, reset, reset, NULL);