From 9aabe0159d4fe7726df5bf7b1714892292e87756 Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Mon, 17 Oct 2022 11:28:46 -0600 Subject: common/button.c: Add tests for failed paths through ACTIVE Add tests for several failed paths when a simulated recovery button is used. These include: - Pressing the volume up/down buttons for too short of a time - Pressing only one volume button - Transitioning to ACTIVE then timing out BRANCH=none BUG=none TEST=twister -s zephyr/test/drivers/drivers.button Signed-off-by: Yuval Peress Change-Id: Ibbeb99985a59be03f48a2d158a77d8edc46153c9 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3960558 Reviewed-by: Jeremy Bettis Code-Coverage: Zoss --- common/button.c | 21 +++--- include/button.h | 16 +++++ zephyr/test/drivers/CMakeLists.txt | 1 + zephyr/test/drivers/Kconfig | 3 + zephyr/test/drivers/button/CMakeLists.txt | 5 ++ zephyr/test/drivers/button/src/main.c | 108 ++++++++++++++++++++++++++++++ zephyr/test/drivers/testcase.yaml | 6 ++ 7 files changed, 149 insertions(+), 11 deletions(-) create mode 100644 zephyr/test/drivers/button/CMakeLists.txt create mode 100644 zephyr/test/drivers/button/src/main.c diff --git a/common/button.c b/common/button.c index 53745adaef..aa7bcf6b4c 100644 --- a/common/button.c +++ b/common/button.c @@ -532,17 +532,6 @@ static void debug_mode_handle(void) #else /* CONFIG_DEDICATED_RECOVERY_BUTTON */ -enum debug_state { - STATE_DEBUG_NONE, - STATE_DEBUG_CHECK, - STATE_STAGING, - STATE_DEBUG_MODE_ACTIVE, - STATE_SYSRQ_PATH, - STATE_WARM_RESET_PATH, - STATE_SYSRQ_EXEC, - STATE_WARM_RESET_EXEC, -}; - #define DEBUG_BTN_POWER BIT(0) #define DEBUG_BTN_VOL_UP BIT(1) #define DEBUG_BTN_VOL_DN BIT(2) @@ -669,6 +658,16 @@ static void debug_mode_transition(enum debug_state next_state) #endif } +__test_only void reset_button_debug_state(void) +{ + debug_mode_transition(STATE_DEBUG_NONE); +} + +__test_only enum debug_state get_button_debug_state(void) +{ + return curr_debug_state; +} + static void debug_mode_handle(void) { int mask; diff --git a/include/button.h b/include/button.h index 937b280876..5ba2f8579d 100644 --- a/include/button.h +++ b/include/button.h @@ -102,4 +102,20 @@ int button_is_adc_detected(enum gpio_signal gpio); */ int adc_to_physical_value(enum gpio_signal gpio); +/* Public for testing purposes only, undocumented. */ +enum debug_state { + STATE_DEBUG_NONE, + STATE_DEBUG_CHECK, + STATE_STAGING, + STATE_DEBUG_MODE_ACTIVE, + STATE_SYSRQ_PATH, + STATE_WARM_RESET_PATH, + STATE_SYSRQ_EXEC, + STATE_WARM_RESET_EXEC, +}; + +__test_only void reset_button_debug_state(void); + +__test_only enum debug_state get_button_debug_state(void); + #endif /* __CROS_EC_BUTTON_H */ diff --git a/zephyr/test/drivers/CMakeLists.txt b/zephyr/test/drivers/CMakeLists.txt index 47d029bb79..d1145653f1 100644 --- a/zephyr/test/drivers/CMakeLists.txt +++ b/zephyr/test/drivers/CMakeLists.txt @@ -40,6 +40,7 @@ add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_USBC_PPC usbc_ppc) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_HOST_COMMANDS host_cmd) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_SYSTEM system) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_LOCATE_CHIP_ALTS locate_chip) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_BUTTON button) get_target_property(TEST_SOURCES_NEW app SOURCES) diff --git a/zephyr/test/drivers/Kconfig b/zephyr/test/drivers/Kconfig index 09ef3e9ebf..17cc06055d 100644 --- a/zephyr/test/drivers/Kconfig +++ b/zephyr/test/drivers/Kconfig @@ -94,4 +94,7 @@ config LINK_TEST_SUITE_LOCATE_CHIP_ALTS Compile a binary that allows the non herobrine paths of the locate chip command to execute +config LINK_TEST_SUITE_BUTTON + bool "Link tests for common/button.c" + source "Kconfig.zephyr" diff --git a/zephyr/test/drivers/button/CMakeLists.txt b/zephyr/test/drivers/button/CMakeLists.txt new file mode 100644 index 0000000000..0070d595b1 --- /dev/null +++ b/zephyr/test/drivers/button/CMakeLists.txt @@ -0,0 +1,5 @@ +# Copyright 2021 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +zephyr_library_sources(src/main.c) diff --git a/zephyr/test/drivers/button/src/main.c b/zephyr/test/drivers/button/src/main.c new file mode 100644 index 0000000000..16591b7189 --- /dev/null +++ b/zephyr/test/drivers/button/src/main.c @@ -0,0 +1,108 @@ +/* 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 +#include +#include + +#include "button.h" +#include "console.h" +#include "hooks.h" +#include "test/drivers/test_state.h" +#include "timer.h" + +static char *button_debug_state_strings[] = { + "STATE_DEBUG_NONE", "STATE_DEBUG_CHECK", + "STATE_STAGING", "STATE_DEBUG_MODE_ACTIVE", + "STATE_SYSRQ_PATH", "STATE_WARM_RESET_PATH", + "STATE_SYSRQ_EXEC", "STATE_WARM_RESET_EXEC", +}; + +#define ASSERT_DEBUG_STATE(expected) \ + do { \ + enum debug_state state = get_button_debug_state(); \ + zassert_equal(expected, state, \ + "Button debug state expected to be %d(%s)," \ + " but was %d(%s)", \ + expected, button_debug_state_strings[expected], \ + state, button_debug_state_strings[state]); \ + } while (false) + +struct button_fixture { + timestamp_t fake_time; +}; + +static void *button_setup(void) +{ + static struct button_fixture fixture; + + /* Set the mock clock */ + get_time_mock = &fixture.fake_time; + return &fixture; +} + +static void button_before(void *f) +{ + ((struct button_fixture *)f)->fake_time.val = 0; + reset_button_debug_state(); + button_init(); +} + +ZTEST_SUITE(button, drivers_predicate_post_main, button_setup, button_before, + NULL, NULL); + +static inline void pass_time(uint64_t duration_ms) +{ + get_time_mock->val += duration_ms * 1000; + k_msleep(duration_ms); +} + +ZTEST(button, test_press_one_button_no_change) +{ + /* Press the volume-up button for 1/2 a second */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500")); + + /* Wait for the timeout */ + pass_time(11000); + ASSERT_DEBUG_STATE(STATE_DEBUG_NONE); +} + +ZTEST(button, test_press_vup_vdown_too_short) +{ + /* Press both volume-up and volume-down for 1/2 second */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500")); + zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 500")); + + /* Let the deferred calls get run (800ms) */ + pass_time(800); + ASSERT_DEBUG_STATE(STATE_DEBUG_CHECK); + + /* Wait for the timeout */ + pass_time(11000); + ASSERT_DEBUG_STATE(STATE_DEBUG_NONE); +} + +ZTEST(button, test_fail_check_button_released_too_soon) +{ + /* Press both volume-up and volume-down for 0.9 seconds */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 9000")); + zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 9000")); + + /* Let the deferred calls get run (800ms) */ + pass_time(800); + ASSERT_DEBUG_STATE(STATE_DEBUG_CHECK); + + /* Wait for the timeout, should put us in staging */ + pass_time(11000); + ASSERT_DEBUG_STATE(STATE_STAGING); + + /* Wait for the handler to be called and set us to ACTIVE mode */ + pass_time(7000); + ASSERT_DEBUG_STATE(STATE_DEBUG_MODE_ACTIVE); + + /* Wait for the deadline to pass, putting us back in NONE */ + pass_time(11000); + ASSERT_DEBUG_STATE(STATE_DEBUG_NONE); +} diff --git a/zephyr/test/drivers/testcase.yaml b/zephyr/test/drivers/testcase.yaml index 46e52941ee..4518aa13cf 100644 --- a/zephyr/test/drivers/testcase.yaml +++ b/zephyr/test/drivers/testcase.yaml @@ -20,6 +20,12 @@ tests: - CONFIG_PLATFORM_EC_LID_ANGLE_UPDATE=y - CONFIG_POWER_SEQUENCE_MOCK=y - CONFIG_PLATFORM_EC_CBI_EEPROM=y + drivers.button: + extra_configs: + - CONFIG_LINK_TEST_SUITE_BUTTON=y + - CONFIG_PLATFORM_EC_CMD_BUTTON=y + - CONFIG_PLATFORM_EC_VOLUME_BUTTONS=y + - CONFIG_PLATFORM_EC_EMULATED_SYSRQ=y drivers.anx7447: extra_args: CONF_FILE="prj.conf;anx7447/prj.conf" DTC_OVERLAY_FILE="./boards/native_posix.overlay;./anx7447/tcpc_policy.dts" extra_configs: -- cgit v1.2.1