From c3590674e8b82b71693bb27fac27a4c9837e2a12 Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Mon, 29 Aug 2022 23:18:34 -0600 Subject: test: Add tests for panic info host command Add tests for the panic info host command including error paths for bad magic number and panic data size 0. BRANCH=none BUG=b:236074360 TEST=twister -s zephyr/test/drivers/drivers.host_cmd Signed-off-by: Yuval Peress Change-Id: I9773afab20e3236faa32bd50915e5fb505036cd4 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3863929 Code-Coverage: Zoss Reviewed-by: Tristan Honscheid --- zephyr/test/drivers/host_cmd/CMakeLists.txt | 1 + zephyr/test/drivers/host_cmd/src/get_panic_info.c | 98 +++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 zephyr/test/drivers/host_cmd/src/get_panic_info.c diff --git a/zephyr/test/drivers/host_cmd/CMakeLists.txt b/zephyr/test/drivers/host_cmd/CMakeLists.txt index d95ce4da2f..cdeb57a279 100644 --- a/zephyr/test/drivers/host_cmd/CMakeLists.txt +++ b/zephyr/test/drivers/host_cmd/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources(app PRIVATE src/battery_cut_off.c + src/get_panic_info.c src/get_pd_port_caps.c src/host_event_commands.c src/host_event_commands_deprecated.c diff --git a/zephyr/test/drivers/host_cmd/src/get_panic_info.c b/zephyr/test/drivers/host_cmd/src/get_panic_info.c new file mode 100644 index 0000000000..6dc13b463a --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/get_panic_info.c @@ -0,0 +1,98 @@ +/* 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 "host_command.h" +#include "panic.h" +#include "test/drivers/test_state.h" + +struct host_cmd_get_panic_info_fixture { + struct panic_data saved_pdata; +}; + +static void *host_cmd_get_panic_info_setup(void) +{ + static struct host_cmd_get_panic_info_fixture fixture = { 0 }; + + return &fixture; +} + +static void host_cmd_get_panic_info_before(void *f) +{ + struct host_cmd_get_panic_info_fixture *fixture = f; + struct panic_data *pdata = get_panic_data_write(); + + fixture->saved_pdata = *pdata; +} + +static void host_cmd_get_panic_info_after(void *f) +{ + struct host_cmd_get_panic_info_fixture *fixture = f; + struct panic_data *pdata = get_panic_data_write(); + + *pdata = fixture->saved_pdata; +} + +ZTEST_SUITE(host_cmd_get_panic_info, drivers_predicate_post_main, + host_cmd_get_panic_info_setup, host_cmd_get_panic_info_before, + host_cmd_get_panic_info_after, NULL); + +ZTEST_USER(host_cmd_get_panic_info, test_get_panic_info) +{ + struct panic_data *pdata = get_panic_data_write(); + struct panic_data response = { 0 }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_GET_PANIC_INFO, UINT8_C(0), response); + + pdata->arch = 0; + pdata->struct_version = 1; + pdata->flags = 2; + pdata->reserved = 3; + pdata->struct_size = sizeof(struct panic_data); + pdata->magic = PANIC_DATA_MAGIC; + + zassert_ok(host_command_process(&args), NULL); + zassert_equal(sizeof(struct panic_data), args.response_size, NULL); + zassert_equal(0, response.arch, NULL); + zassert_equal(1, response.struct_version, NULL); + zassert_equal(2, response.flags, NULL); + zassert_equal(3, response.reserved, NULL); + zassert_equal(sizeof(struct panic_data), response.struct_size, NULL); + zassert_equal(PANIC_DATA_MAGIC, response.magic, NULL); + zassert_equal(pdata->flags & PANIC_DATA_FLAG_OLD_HOSTCMD, + PANIC_DATA_FLAG_OLD_HOSTCMD, NULL); +} + +ZTEST_USER(host_cmd_get_panic_info, test_get_panic_info_bad_magic) +{ + struct panic_data *pdata = get_panic_data_write(); + struct panic_data expected = { 0 }; + struct panic_data response = { 0 }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_GET_PANIC_INFO, UINT8_C(0), response); + + pdata->magic = PANIC_DATA_MAGIC + 1; + zassert_ok(host_command_process(&args), NULL); + /* Check that nothing was written to response */ + zassert_mem_equal(&response, &expected, sizeof(struct panic_data), + NULL); +} + +ZTEST_USER(host_cmd_get_panic_info, test_get_panic_info_size_is_zero) +{ + struct panic_data *pdata = get_panic_data_write(); + struct panic_data expected = { 0 }; + struct panic_data response = { 0 }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_GET_PANIC_INFO, UINT8_C(0), response); + + pdata->magic = PANIC_DATA_MAGIC; + pdata->struct_size = 0; + zassert_ok(host_command_process(&args), NULL); + /* Check that nothing was written to response */ + zassert_mem_equal(&response, &expected, sizeof(struct panic_data), + NULL); +} -- cgit v1.2.1