summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2022-08-26 05:46:00 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-08-26 21:18:47 +0000
commitf100d8d6fd13c10e86610802201750616d9e1621 (patch)
tree9ff65f523dfa6cd3dcfa7b3185a18e8d87e2c3a8
parent5e29be445ab5555ad8f951310179fa23cda2b66b (diff)
downloadchrome-ec-f100d8d6fd13c10e86610802201750616d9e1621.tar.gz
zephyr: vstore: Add tests for vstore host command
Add coverage for the three host commands. This does not cover the init and sysjump behaviour. Update to use two slots so we can test non-trivial behaviour. BUG=b:236075794,b:236160558,b:236160563 BRANCH=none TEST=./twister -T zephyr/test/drivers/ -s drivers.default Signed-off-by: Simon Glass <sjg@chromium.org> Change-Id: I3aa9146b459033f32fb12d0d86ec44c214f0db63 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3858390 Reviewed-by: Jeremy Bettis <jbettis@chromium.org> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r--common/vstore.c2
-rw-r--r--include/vstore.h16
-rw-r--r--zephyr/test/drivers/default/CMakeLists.txt1
-rw-r--r--zephyr/test/drivers/default/src/vstore.c169
-rw-r--r--zephyr/test/drivers/prj.conf1
5 files changed, 188 insertions, 1 deletions
diff --git a/common/vstore.c b/common/vstore.c
index 085c7b81c9..0cbf761eef 100644
--- a/common/vstore.c
+++ b/common/vstore.c
@@ -97,7 +97,7 @@ static enum ec_status vstore_write(struct host_cmd_handler_args *args)
}
DECLARE_HOST_COMMAND(EC_CMD_VSTORE_WRITE, vstore_write, EC_VER_MASK(0));
-static void vstore_clear_lock(void)
+test_export_static void vstore_clear_lock(void)
{
int i;
diff --git a/include/vstore.h b/include/vstore.h
new file mode 100644
index 0000000000..00b268652d
--- /dev/null
+++ b/include/vstore.h
@@ -0,0 +1,16 @@
+/* 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 __CROS_EC_VSTORE_H
+#define __CROS_EC_VSTORE_H
+
+#ifdef TEST_BUILD
+
+/* Clear all vstore locks */
+void vstore_clear_lock(void);
+
+#endif /* TEST_BUILD */
+
+#endif /* __CROS_EC_VSTORE_H */
diff --git a/zephyr/test/drivers/default/CMakeLists.txt b/zephyr/test/drivers/default/CMakeLists.txt
index 00249ca6e6..add3723c4f 100644
--- a/zephyr/test/drivers/default/CMakeLists.txt
+++ b/zephyr/test/drivers/default/CMakeLists.txt
@@ -72,5 +72,6 @@ target_sources(app PRIVATE
src/usb_pd_host_cmd.c
src/vboot_hash.c
src/virtual_battery.c
+ src/vstore.c
src/watchdog.c
)
diff --git a/zephyr/test/drivers/default/src/vstore.c b/zephyr/test/drivers/default/src/vstore.c
new file mode 100644
index 0000000000..cbdbe65ccc
--- /dev/null
+++ b/zephyr/test/drivers/default/src/vstore.c
@@ -0,0 +1,169 @@
+/* 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 "ec_commands.h"
+#include "host_command.h"
+#include "vstore.h"
+#include "test/drivers/test_state.h"
+
+ZTEST_SUITE(vstore, drivers_predicate_post_main, NULL, NULL, NULL, NULL);
+
+ZTEST_USER(vstore, test_vstore_info)
+{
+ struct ec_response_vstore_info response;
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND_RESPONSE(EC_CMD_VSTORE_INFO, 0, response);
+
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_ok(args.result, NULL);
+ zassert_equal(args.response_size, sizeof(response), NULL);
+ zassert_equal(response.slot_count, CONFIG_VSTORE_SLOT_COUNT,
+ "response.slot_count = %d", response.slot_count);
+ zassert_equal(response.slot_locked, 0, "response.slot_locked = %#x",
+ response.slot_locked);
+}
+
+ZTEST_USER(vstore, test_vstore_read)
+{
+ struct ec_params_vstore_read params = {
+ .slot = 0,
+ };
+ struct ec_response_vstore_read response;
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_VSTORE_READ, 0, response, params);
+ uint8_t expect[EC_VSTORE_SLOT_SIZE] = {}; /* data should start as 0 */
+
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_ok(args.result, NULL);
+ zassert_equal(args.response_size, sizeof(response), NULL);
+ zassert_mem_equal(expect, response.data, EC_VSTORE_SLOT_SIZE,
+ "response.data did not match");
+}
+
+ZTEST_USER(vstore, test_vstore_read_bad_slot)
+{
+ struct ec_params_vstore_read params = {
+ .slot = CONFIG_VSTORE_SLOT_COUNT,
+ };
+ struct ec_response_vstore_read response;
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_VSTORE_READ, 0, response, params);
+
+ zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM,
+ "Failed to fail on invalid slot %d", params.slot);
+}
+
+ZTEST_USER(vstore, test_vstore_write_bad_slot)
+{
+ struct ec_params_vstore_write params = {
+ .slot = CONFIG_VSTORE_SLOT_COUNT,
+ .data = {},
+ };
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND_PARAMS(EC_CMD_VSTORE_WRITE, 0, params);
+
+ zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM,
+ "Failed to fail on invalid slot %d", params.slot);
+}
+
+static void do_vstore_write_read(unsigned int slot)
+{
+ struct ec_params_vstore_write write_params = {
+ .slot = slot,
+ /* .data is set up below */
+ };
+ struct host_cmd_handler_args write_args =
+ BUILD_HOST_COMMAND_PARAMS(EC_CMD_VSTORE_WRITE, 0, write_params);
+ struct ec_params_vstore_read read_params = {
+ .slot = slot,
+ };
+ struct ec_response_vstore_read read_response;
+ struct host_cmd_handler_args read_args = BUILD_HOST_COMMAND(
+ EC_CMD_VSTORE_READ, 0, read_response, read_params);
+ struct ec_response_vstore_info info_response;
+ struct host_cmd_handler_args info_args = BUILD_HOST_COMMAND_RESPONSE(
+ EC_CMD_VSTORE_INFO, 0, info_response);
+ int i;
+
+ for (i = 0; i < EC_VSTORE_SLOT_SIZE; i++)
+ write_params.data[i] = i + 1;
+
+ /* Write to a slot */
+ zassert_ok(host_command_process(&write_args), NULL);
+ zassert_ok(write_args.result, NULL);
+
+ /* Check that it is now locked */
+ zassert_ok(host_command_process(&info_args), NULL);
+ zassert_ok(info_args.result, NULL);
+ zassert_equal(info_args.response_size, sizeof(info_response), NULL);
+ zassert_equal(info_response.slot_count, CONFIG_VSTORE_SLOT_COUNT,
+ "response.slot_count = %d", info_response.slot_count);
+ zassert_equal(info_response.slot_locked, 1 << slot,
+ "response.slot_locked = %#x", info_response.slot_locked);
+
+ /* Read to check data */
+ zassert_ok(host_command_process(&read_args), NULL);
+ zassert_ok(read_args.result, NULL);
+ zassert_equal(read_args.response_size, sizeof(read_response), NULL);
+ zassert_mem_equal(write_params.data, read_response.data,
+ EC_VSTORE_SLOT_SIZE, "response.data did not match");
+
+ /* Try to write to it again */
+ zassert_equal(host_command_process(&write_args), EC_RES_ACCESS_DENIED,
+ "Failed to fail on writing locked slot %d",
+ write_params.slot);
+
+ /* Check that it is still locked after that attempt */
+ zassert_ok(host_command_process(&info_args), NULL);
+ zassert_ok(info_args.result, NULL);
+ zassert_equal(info_args.response_size, sizeof(info_response), NULL);
+ zassert_equal(info_response.slot_count, CONFIG_VSTORE_SLOT_COUNT,
+ "response.slot_count = %d", info_response.slot_count);
+ zassert_equal(info_response.slot_locked, 1 << slot,
+ "response.slot_locked = %#x", info_response.slot_locked);
+
+ /* Read to check the data didn't change */
+ zassert_ok(host_command_process(&read_args), NULL);
+ zassert_ok(read_args.result, NULL);
+ zassert_equal(read_args.response_size, sizeof(read_response), NULL);
+ zassert_mem_equal(write_params.data, read_response.data,
+ EC_VSTORE_SLOT_SIZE, "response.data did not match");
+
+ /* Clear locks and try the write again, this time with zero bytes */
+ vstore_clear_lock();
+ memset(write_params.data, '\0', EC_VSTORE_SLOT_SIZE);
+ zassert_ok(host_command_process(&write_args), NULL);
+ zassert_ok(write_args.result, NULL);
+
+ /* Check that it is now locked */
+ zassert_ok(host_command_process(&info_args), NULL);
+ zassert_ok(info_args.result, NULL);
+ zassert_equal(info_args.response_size, sizeof(info_response), NULL);
+ zassert_equal(info_response.slot_count, CONFIG_VSTORE_SLOT_COUNT,
+ "response.slot_count = %d", info_response.slot_count);
+ zassert_equal(info_response.slot_locked, 1 << slot,
+ "response.slot_locked = %#x", info_response.slot_locked);
+
+ /* Read to check the data changed */
+ zassert_ok(host_command_process(&read_args), NULL);
+ zassert_ok(read_args.result, NULL);
+ zassert_equal(read_args.response_size, sizeof(read_response), NULL);
+ zassert_mem_equal(write_params.data, read_response.data,
+ EC_VSTORE_SLOT_SIZE, "response.data did not match");
+
+ /* Clear locks to put things into a normal state */
+ vstore_clear_lock();
+}
+
+ZTEST_USER(vstore, test_vstore_write_read)
+{
+ /* Try on two different slots */
+ zassert_true(CONFIG_VSTORE_SLOT_COUNT >= 2,
+ "Please set CONFIG_VSTORE_SLOT_COUNT to >= 2");
+ do_vstore_write_read(0);
+ do_vstore_write_read(1);
+}
diff --git a/zephyr/test/drivers/prj.conf b/zephyr/test/drivers/prj.conf
index 36eab9ea3b..4ba842b933 100644
--- a/zephyr/test/drivers/prj.conf
+++ b/zephyr/test/drivers/prj.conf
@@ -151,6 +151,7 @@ CONFIG_PLATFORM_EC_LID_SWITCH=y
CONFIG_PLATFORM_EC_POWER_BUTTON=y
CONFIG_PLATFORM_EC_HOST_INTERFACE_ESPI=y
CONFIG_PLATFORM_EC_PORT80=y
+CONFIG_PLATFORM_EC_VSTORE_SLOT_COUNT=2
CONFIG_WATCHDOG=y
CONFIG_WDT_DISABLE_AT_BOOT=y