summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@google.com>2022-11-14 01:25:29 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-11-14 22:11:50 +0000
commit9e7107aeb41834a2d40f845f5644cf9c48fc3d1e (patch)
tree24ae54f9a3d7fa9ad873c95c06843cd6b461145a
parentb6716df9c14ad71f3b49b7e05d20bce5c851850b (diff)
downloadchrome-ec-9e7107aeb41834a2d40f845f5644cf9c48fc3d1e.tar.gz
test: system shim
Add tests for all error conditions that can be emulated at runtime for system.c's shim layer. BRANCH=none BUG=none TEST=twister Signed-off-by: Yuval Peress <peress@google.com> Change-Id: Id90dbe0c046e379f5bee5fe958a58e0928cfd157 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4025333 Reviewed-by: Abe Levkoy <alevkoy@chromium.org> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r--zephyr/shim/src/system.c2
-rw-r--r--zephyr/test/system_shim/CMakeLists.txt15
-rw-r--r--zephyr/test/system_shim/boards/native_posix.overlay5
-rw-r--r--zephyr/test/system_shim/default.overlay12
-rw-r--r--zephyr/test/system_shim/include/fakes.h29
-rw-r--r--zephyr/test/system_shim/prj.conf12
-rw-r--r--zephyr/test/system_shim/src/no_chosen.c24
-rw-r--r--zephyr/test/system_shim/src/suite.c50
-rw-r--r--zephyr/test/system_shim/src/test_system.c (renamed from zephyr/test/system_shim/test_system.c)77
-rw-r--r--zephyr/test/system_shim/testcase.yaml15
10 files changed, 205 insertions, 36 deletions
diff --git a/zephyr/shim/src/system.c b/zephyr/shim/src/system.c
index 53e651ec03..99f4f4c3e1 100644
--- a/zephyr/shim/src/system.c
+++ b/zephyr/shim/src/system.c
@@ -342,7 +342,7 @@ static int check_reset_cause(void)
return 0;
}
-static int system_preinitialize(const struct device *unused)
+test_export_static int system_preinitialize(const struct device *unused)
{
ARG_UNUSED(unused);
diff --git a/zephyr/test/system_shim/CMakeLists.txt b/zephyr/test/system_shim/CMakeLists.txt
index 5db9cb285d..92b55a47e6 100644
--- a/zephyr/test/system_shim/CMakeLists.txt
+++ b/zephyr/test/system_shim/CMakeLists.txt
@@ -9,5 +9,16 @@ project(system_shim_test)
# Include FFF fakes
add_subdirectory(${PLATFORM_EC}/zephyr/test/test_utils test_utils)
-target_sources(app PRIVATE test_system.c
- ${PLATFORM_EC}/zephyr/shim/src/system.c)
+target_sources(app
+ PRIVATE
+ src/suite.c
+ ${PLATFORM_EC}/zephyr/shim/src/system.c
+)
+target_include_directories(app PRIVATE include)
+
+dt_has_chosen(has_bbram PROPERTY "cros-ec,bbram")
+if(has_bbram)
+ target_sources(app PRIVATE src/test_system.c)
+else()
+ target_sources(app PRIVATE src/no_chosen.c)
+endif()
diff --git a/zephyr/test/system_shim/boards/native_posix.overlay b/zephyr/test/system_shim/boards/native_posix.overlay
index 0bcda0f513..b08ea4bf7a 100644
--- a/zephyr/test/system_shim/boards/native_posix.overlay
+++ b/zephyr/test/system_shim/boards/native_posix.overlay
@@ -6,9 +6,6 @@
#include <board-overlays/native_posix.dts>
/ {
- chosen {
- cros-ec,bbram = &bbram;
- };
bbram: test-bbram-dev {
compatible = "zephyr,bbram-emul";
@@ -33,7 +30,7 @@
offset = <0x07>;
size = <0x05>;
};
- scratchpad {
+ scratchpad: scratchpad {
offset = <0x0c>;
size = <0x04>;
};
diff --git a/zephyr/test/system_shim/default.overlay b/zephyr/test/system_shim/default.overlay
new file mode 100644
index 0000000000..13ceee9c19
--- /dev/null
+++ b/zephyr/test/system_shim/default.overlay
@@ -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.
+ */
+
+#include "boards/native_posix.overlay"
+
+/ {
+ chosen {
+ cros-ec,bbram = &bbram;
+ };
+};
diff --git a/zephyr/test/system_shim/include/fakes.h b/zephyr/test/system_shim/include/fakes.h
new file mode 100644
index 0000000000..80da06a313
--- /dev/null
+++ b/zephyr/test/system_shim/include/fakes.h
@@ -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.
+ */
+
+#ifndef ZEPHYR_TEST_SYSTEM_SHIM_INCLUDE_FAKES_H_
+#define ZEPHYR_TEST_SYSTEM_SHIM_INCLUDE_FAKES_H_
+
+#include <zephyr/fff.h>
+
+DECLARE_FAKE_VALUE_FUNC(int, cros_system_native_posix_get_reset_cause,
+ const struct device *);
+DECLARE_FAKE_VALUE_FUNC(uint64_t, cros_system_native_posix_deep_sleep_ticks,
+ const struct device *);
+DECLARE_FAKE_VALUE_FUNC(int, cros_system_native_posix_hibernate,
+ const struct device *, uint32_t, uint32_t);
+DECLARE_FAKE_VALUE_FUNC(const char *, cros_system_native_posix_get_chip_vendor,
+ const struct device *);
+DECLARE_FAKE_VALUE_FUNC(const char *, cros_system_native_posix_get_chip_name,
+ const struct device *);
+DECLARE_FAKE_VALUE_FUNC(const char *,
+ cros_system_native_posix_get_chip_revision,
+ const struct device *);
+DECLARE_FAKE_VALUE_FUNC(int, cros_system_native_posix_soc_reset,
+ const struct device *);
+DECLARE_FAKE_VOID_FUNC(watchdog_reload);
+DECLARE_FAKE_VOID_FUNC(board_hibernate);
+
+#endif /* ZEPHYR_TEST_SYSTEM_SHIM_INCLUDE_FAKES_H_ */
diff --git a/zephyr/test/system_shim/prj.conf b/zephyr/test/system_shim/prj.conf
index fa7bd9fc04..98e133f5ff 100644
--- a/zephyr/test/system_shim/prj.conf
+++ b/zephyr/test/system_shim/prj.conf
@@ -9,3 +9,15 @@ CONFIG_CROS_EC=y
CONFIG_LOG=y
CONFIG_BBRAM=y
CONFIG_BBRAM_EMUL=y
+
+CONFIG_PLATFORM_EC_BOARD_RESET_AFTER_POWER_ON=y
+
+# Make console work
+CONFIG_SERIAL=y
+CONFIG_SHELL_BACKEND_DUMMY=y
+CONFIG_SHELL_BACKEND_DUMMY_BUF_SIZE=1000
+CONFIG_SHELL_BACKEND_SERIAL=n
+
+# Make cros_system driver work
+CONFIG_PM=y
+CONFIG_CROS_SYSTEM_NATIVE_POSIX=y
diff --git a/zephyr/test/system_shim/src/no_chosen.c b/zephyr/test/system_shim/src/no_chosen.c
new file mode 100644
index 0000000000..55504691d1
--- /dev/null
+++ b/zephyr/test/system_shim/src/no_chosen.c
@@ -0,0 +1,24 @@
+/* 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 "fakes.h"
+#include "system.h"
+
+ZTEST(system, test_fail_get_bbram_no_device)
+{
+ zassert_equal(EC_ERROR_INVAL, system_get_bbram(0, NULL));
+}
+
+ZTEST(system, test_fail_set_scratchpad)
+{
+ zassert_equal(-EC_ERROR_INVAL, system_set_scratchpad(0));
+}
+
+ZTEST(system, test_fail_get_scratchpad)
+{
+ zassert_equal(-EC_ERROR_INVAL, system_get_scratchpad(NULL));
+}
diff --git a/zephyr/test/system_shim/src/suite.c b/zephyr/test/system_shim/src/suite.c
new file mode 100644
index 0000000000..0d825ad9d7
--- /dev/null
+++ b/zephyr/test/system_shim/src/suite.c
@@ -0,0 +1,50 @@
+/* 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/device.h>
+#include <zephyr/drivers/bbram.h>
+#include <zephyr/fff.h>
+#include <zephyr/ztest.h>
+
+#include "fakes.h"
+
+DEFINE_FAKE_VALUE_FUNC(int, cros_system_native_posix_get_reset_cause,
+ const struct device *);
+DEFINE_FAKE_VALUE_FUNC(uint64_t, cros_system_native_posix_deep_sleep_ticks,
+ const struct device *);
+DEFINE_FAKE_VALUE_FUNC(int, cros_system_native_posix_hibernate,
+ const struct device *, uint32_t, uint32_t);
+DEFINE_FAKE_VALUE_FUNC(const char *, cros_system_native_posix_get_chip_vendor,
+ const struct device *);
+DEFINE_FAKE_VALUE_FUNC(const char *, cros_system_native_posix_get_chip_name,
+ const struct device *);
+DEFINE_FAKE_VALUE_FUNC(const char *, cros_system_native_posix_get_chip_revision,
+ const struct device *);
+DEFINE_FAKE_VALUE_FUNC(int, cros_system_native_posix_soc_reset,
+ const struct device *);
+DEFINE_FAKE_VOID_FUNC(watchdog_reload);
+DEFINE_FAKE_VOID_FUNC(board_hibernate);
+
+static void system_before_after(void *test_data)
+{
+ const struct device *bbram_dev =
+ DEVICE_DT_GET_OR_NULL(DT_CHOSEN(cros_ec_bbram));
+
+ RESET_FAKE(cros_system_native_posix_get_reset_cause);
+ RESET_FAKE(cros_system_native_posix_deep_sleep_ticks);
+ RESET_FAKE(cros_system_native_posix_hibernate);
+ RESET_FAKE(cros_system_native_posix_get_chip_vendor);
+ RESET_FAKE(cros_system_native_posix_get_chip_name);
+ RESET_FAKE(cros_system_native_posix_get_chip_revision);
+ RESET_FAKE(cros_system_native_posix_soc_reset);
+ RESET_FAKE(watchdog_reload);
+ RESET_FAKE(board_hibernate);
+
+ if (bbram_dev != NULL) {
+ bbram_emul_set_invalid(bbram_dev, false);
+ }
+}
+
+ZTEST_SUITE(system, NULL, NULL, system_before_after, system_before_after, NULL);
diff --git a/zephyr/test/system_shim/test_system.c b/zephyr/test/system_shim/src/test_system.c
index 3bf43d6669..e6e7077540 100644
--- a/zephyr/test/system_shim/test_system.c
+++ b/zephyr/test/system_shim/src/test_system.c
@@ -12,6 +12,8 @@
#include <zephyr/ztest_assert.h>
#include <zephyr/ztest_test_new.h>
+#include "drivers/cros_system.h"
+#include "fakes.h"
#include "system.h"
LOG_MODULE_REGISTER(test);
@@ -24,27 +26,14 @@ LOG_MODULE_REGISTER(test);
static char mock_data[64] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@";
-FAKE_VALUE_FUNC(uint64_t, cros_system_native_posix_deep_sleep_ticks,
- const struct device *);
-FAKE_VALUE_FUNC(int, cros_system_native_posix_hibernate, const struct device *,
- uint32_t, uint32_t);
-FAKE_VALUE_FUNC(const char *, cros_system_native_posix_get_chip_vendor,
- const struct device *);
-FAKE_VALUE_FUNC(const char *, cros_system_native_posix_get_chip_name,
- const struct device *);
-FAKE_VALUE_FUNC(const char *, cros_system_native_posix_get_chip_revision,
- const struct device *);
-FAKE_VALUE_FUNC(int, cros_system_native_posix_soc_reset, const struct device *);
-FAKE_VOID_FUNC(watchdog_reload);
-
-static void system_before_after(void *test_data)
+int system_preinitialize(const struct device *unused);
+
+ZTEST(system, test_invalid_bbram_index)
{
- RESET_FAKE(cros_system_native_posix_deep_sleep_ticks);
- RESET_FAKE(cros_system_native_posix_hibernate);
+ zassert_equal(EC_ERROR_INVAL,
+ system_get_bbram(SYSTEM_BBRAM_IDX_TRY_SLOT + 1, NULL));
}
-ZTEST_SUITE(system, NULL, NULL, system_before_after, system_before_after, NULL);
-
ZTEST(system, test_bbram_get)
{
const struct device *const bbram_dev =
@@ -97,6 +86,15 @@ ZTEST(system, test_system_set_get_scratchpad)
zassert_equal(scratch_read, scratch_set);
}
+ZTEST(system, test_system_get_scratchpad_fail)
+{
+ const struct device *bbram_dev =
+ DEVICE_DT_GET(DT_CHOSEN(cros_ec_bbram));
+
+ zassert_ok(bbram_emul_set_invalid(bbram_dev, true));
+ zassert_equal(-EC_ERROR_INVAL, system_get_scratchpad(NULL));
+}
+
static jmp_buf jmp_hibernate;
static int _test_cros_system_native_posix_hibernate(const struct device *dev,
@@ -136,6 +134,7 @@ ZTEST(system, test_system_hibernate)
sys_dev);
zassert_equal(cros_system_native_posix_hibernate_fake.arg1_val, secs);
zassert_equal(cros_system_native_posix_hibernate_fake.arg2_val, msecs);
+ zassert_equal(board_hibernate_fake.call_count, 1);
}
ZTEST(system, test_system_hibernate__failure)
@@ -255,3 +254,45 @@ ZTEST_USER(system, test_system_console_cmd__idlestats)
zassert_equal(cros_system_native_posix_deep_sleep_ticks_fake.call_count,
1);
}
+
+ZTEST(system, test_init_invalid_reset_cause)
+{
+ cros_system_native_posix_get_reset_cause_fake.return_val = -1;
+ zassert_equal(-1, system_preinitialize(NULL));
+}
+
+ZTEST(system, test_init_cause_vcc1_rst_pin)
+{
+ cros_system_native_posix_get_reset_cause_fake.return_val = VCC1_RST_PIN;
+ chip_save_reset_flags(0);
+ system_clear_reset_flags(0xffffffff);
+
+ zassert_ok(system_preinitialize(NULL));
+ zassert_equal(EC_RESET_FLAG_RESET_PIN, system_get_reset_flags());
+
+ chip_save_reset_flags(EC_RESET_FLAG_INITIAL_PWR);
+ zassert_ok(system_preinitialize(NULL));
+ zassert_equal(EC_RESET_FLAG_RESET_PIN | EC_RESET_FLAG_POWER_ON |
+ EC_RESET_FLAG_POWER_ON,
+ system_get_reset_flags());
+}
+
+ZTEST(system, test_init_cause_debug_rst)
+{
+ cros_system_native_posix_get_reset_cause_fake.return_val = DEBUG_RST;
+ chip_save_reset_flags(0);
+ system_clear_reset_flags(0xffffffff);
+
+ zassert_ok(system_preinitialize(NULL));
+ zassert_equal(EC_RESET_FLAG_SOFT, system_get_reset_flags());
+}
+
+ZTEST(system, test_init_cause_watchdog_rst)
+{
+ cros_system_native_posix_get_reset_cause_fake.return_val = WATCHDOG_RST;
+ chip_save_reset_flags(0);
+ system_clear_reset_flags(0xffffffff);
+
+ zassert_ok(system_preinitialize(NULL));
+ zassert_equal(EC_RESET_FLAG_WATCHDOG, system_get_reset_flags());
+}
diff --git a/zephyr/test/system_shim/testcase.yaml b/zephyr/test/system_shim/testcase.yaml
index 3374c7f5f3..29b5fa55c0 100644
--- a/zephyr/test/system_shim/testcase.yaml
+++ b/zephyr/test/system_shim/testcase.yaml
@@ -1,15 +1,8 @@
common:
platform_allow: native_posix
+ tags:
+ system
tests:
system_shim.default:
- tags:
- system
- extra_configs:
- # Make console work
- - CONFIG_SERIAL=y
- - CONFIG_SHELL_BACKEND_DUMMY=y
- - CONFIG_SHELL_BACKEND_DUMMY_BUF_SIZE=1000
- - CONFIG_SHELL_BACKEND_SERIAL=n
- # Make cros_system driver work
- - CONFIG_PM=y
- - CONFIG_CROS_SYSTEM_NATIVE_POSIX=y
+ extra_args: DTC_OVERLAY_FILE="./default.overlay"
+ system_shim.no_chosen: {}