summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorPatryk Duda <pdk@semihalf.com>2022-03-16 11:59:04 +0100
committerCommit Bot <commit-bot@chromium.org>2022-03-22 12:22:35 +0000
commit66c4507ec5c6ef1d3ffac0f23a56f4a3827a61d2 (patch)
treec6038d82add6a24835088630878d18f35bd2ddda /test
parent4844236c145597b7bc40122376da6cf099ac7e7b (diff)
downloadchrome-ec-66c4507ec5c6ef1d3ffac0f23a56f4a3827a61d2.tar.gz
test: Introduce system_is_locked unit test
The test is intended to check system_is_locked() behaviour in the following conditions: - Hardware write protect is off - Hardware write protect is on but software write protect is off - Hardware write protect is on but software write protect is on BUG=b:217946520 BRANCH=none TEST=run_device_test.py --board dartmonkey \ --tests system_is_locked_wp_on TEST=run_device_test.py --board dartmonkey \ --tests system_is_locked_wp_off TEST=run_device_test.py --board bloonchipper \ --tests system_is_locked_wp_on TEST=run_device_test.py --board bloonchipper \ --tests system_is_locked_wp_off Signed-off-by: Patryk Duda <pdk@semihalf.com> Change-Id: Ibf90829c4a36fb5e5c8e790c598c26297fbba5b0 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3528399 Reviewed-by: Andrea Grandi <agrandi@google.com> Reviewed-by: Tom Hughes <tomhughes@chromium.org> Tested-by: Patryk Duda <patrykd@google.com> Commit-Queue: Patryk Duda <patrykd@google.com>
Diffstat (limited to 'test')
-rw-r--r--test/build.mk1
-rwxr-xr-xtest/run_device_tests.py6
-rw-r--r--test/system_is_locked.c129
-rw-r--r--test/system_is_locked.tasklist10
4 files changed, 146 insertions, 0 deletions
diff --git a/test/build.mk b/test/build.mk
index e57f6e5c8e..02ae6503f8 100644
--- a/test/build.mk
+++ b/test/build.mk
@@ -218,6 +218,7 @@ static_if-y=static_if.o
stm32f_rtc-y=stm32f_rtc.o
stress-y=stress.o
system-y=system.o
+system_is_locked-y=system_is_locked.o
thermal-y=thermal.o
timer_calib-y=timer_calib.o
timer_dos-y=timer_dos.o
diff --git a/test/run_device_tests.py b/test/run_device_tests.py
index de3e61bca3..69ce6b9d15 100755
--- a/test/run_device_tests.py
+++ b/test/run_device_tests.py
@@ -178,6 +178,12 @@ class AllTests:
TestConfig(name='sha256_unrolled'),
'static_if':
TestConfig(name='static_if'),
+ 'system_is_locked_wp_on':
+ TestConfig(name='system_is_locked', test_args=['wp_on'],
+ toggle_power=True, enable_hw_write_protect=True),
+ 'system_is_locked_wp_off':
+ TestConfig(name='system_is_locked', test_args=['wp_off'],
+ toggle_power=True, enable_hw_write_protect=False),
'timer_dos':
TestConfig(name='timer_dos'),
'utils':
diff --git a/test/system_is_locked.c b/test/system_is_locked.c
new file mode 100644
index 0000000000..9870f77ebc
--- /dev/null
+++ b/test/system_is_locked.c
@@ -0,0 +1,129 @@
+/* Copyright 2022 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "test_util.h"
+
+#include "flash.h"
+#include "string.h"
+#include "system.h"
+#include "task.h"
+#include "write_protect.h"
+
+static bool write_protect_enabled;
+
+test_static int test_write_protect(void)
+{
+ TEST_EQ(write_protect_is_asserted(), write_protect_enabled, "%d");
+
+ return EC_SUCCESS;
+}
+
+test_static int test_ro_protection_enabled(void)
+{
+ TEST_BITS_SET(crec_flash_get_protect(), EC_FLASH_PROTECT_RO_NOW);
+
+ return EC_SUCCESS;
+}
+
+test_static int test_system_is_locked(void)
+{
+ if (!write_protect_is_asserted() ||
+ (~crec_flash_get_protect() & EC_FLASH_PROTECT_RO_NOW))
+ TEST_EQ(system_is_locked(), 0, "%d");
+ else
+ TEST_EQ(system_is_locked(), 1, "%d");
+
+ return EC_SUCCESS;
+}
+
+static void print_usage(void)
+{
+ ccprintf("usage: runtest [wp_on|wp_off]\n");
+}
+
+void test_run_step(uint32_t state)
+{
+ /*
+ * Step 1: Check if reported write protect and system_is_locked()
+ * output is correct. Since RO protection is not enabled at this point
+ * we expect system_is_locked() to return 0. If write protect is
+ * enabled then attempt to enable RO protection.
+ */
+ if (state & TEST_STATE_MASK(TEST_STATE_STEP_1)) {
+ RUN_TEST(test_write_protect);
+ RUN_TEST(test_system_is_locked);
+
+ if (test_get_error_count())
+ test_reboot_to_next_step(TEST_STATE_FAILED);
+ else if (write_protect_enabled) {
+ ccprintf("Request RO protection at boot\n");
+ crec_flash_set_protect(EC_FLASH_PROTECT_RO_AT_BOOT,
+ EC_FLASH_PROTECT_RO_AT_BOOT);
+ test_reboot_to_next_step(TEST_STATE_STEP_2);
+ } else {
+ /* Write protect is disabled, nothing else to do */
+ test_reboot_to_next_step(TEST_STATE_PASSED);
+ }
+ }
+ /*
+ * Step 2: Check if hardware write protect is enabled, RO protection
+ * is enabled and system_is_locked() returns 1.
+ */
+ else if (state & TEST_STATE_MASK(TEST_STATE_STEP_2)) {
+ /* Expect hardware write protect to be enabled */
+ write_protect_enabled = true;
+ RUN_TEST(test_write_protect);
+ RUN_TEST(test_ro_protection_enabled);
+ RUN_TEST(test_system_is_locked);
+ if (test_get_error_count())
+ test_reboot_to_next_step(TEST_STATE_FAILED);
+ else
+ test_reboot_to_next_step(TEST_STATE_PASSED);
+ }
+}
+
+int task_test(void *unused)
+{
+ test_run_multistep();
+ return EC_SUCCESS;
+}
+
+void run_test(int argc, char **argv)
+{
+ test_reset();
+
+ if (IS_ENABLED(CONFIG_SYSTEM_UNLOCKED)) {
+ ccprintf("Please disable CONFIG_SYSTEM_UNLOCKED before "
+ "running this test\n");
+ test_fail();
+ return;
+ }
+
+ if (argc < 2) {
+ print_usage();
+ test_fail();
+ return;
+ }
+
+ if (strncmp(argv[1], "wp_on", 5) == 0)
+ write_protect_enabled = true;
+ else if (strncmp(argv[1], "wp_off", 6) == 0) {
+ write_protect_enabled = false;
+ if (IS_ENABLED(CONFIG_WP_ALWAYS)) {
+ ccprintf("Hardware write protect always enabled. "
+ "Please disable CONFIG_WP_ALWAYS before "
+ "running this test\n");
+ test_fail();
+ return;
+ }
+ } else {
+ print_usage();
+ test_fail();
+ return;
+ }
+
+ msleep(30); /* Wait for TASK_ID_TEST to initialize */
+ task_wake(TASK_ID_TEST);
+}
diff --git a/test/system_is_locked.tasklist b/test/system_is_locked.tasklist
new file mode 100644
index 0000000000..6a2f1834ca
--- /dev/null
+++ b/test/system_is_locked.tasklist
@@ -0,0 +1,10 @@
+/* Copyright 2022 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/**
+ * See CONFIG_TASK_LIST in config.h for details.
+ */
+#define CONFIG_TEST_TASK_LIST \
+ TASK_TEST(TEST, task_test, NULL, TASK_STACK_SIZE)