diff options
author | Jeremy Bettis <jbettis@google.com> | 2022-09-20 17:24:47 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-09-21 16:54:58 +0000 |
commit | 3608bfa9759933cedb420483c0a7674c887be3ec (patch) | |
tree | a9c11ef4ec540b10fbaee03bf05309a7e5e7fd80 | |
parent | 08a849acaa4d614b3ed7250295c5d912e27b7d89 (diff) | |
download | chrome-ec-3608bfa9759933cedb420483c0a7674c887be3ec.tar.gz |
zephyr: Test 100% of reachable code in vboot/efs2
Add test cases to test 100% of reachable code in common/vboot/efs2.c.
I really tried to make uart_shell_stop() fail, but all possible error
conditions also either make logging raise an assert or cause the test
to hang forever.
BRANCH=None
BUG=None
TEST=./twister
Signed-off-by: Jeremy Bettis <jbettis@google.com>
Change-Id: I259acb13d541dd41c7d2c9f5a1080bd4e77c200c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3906618
Tested-by: Jeremy Bettis <jbettis@chromium.org>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
Reviewed-by: Tomasz Michalec <tmichalec@google.com>
Auto-Submit: Jeremy Bettis <jbettis@chromium.org>
Commit-Queue: Jeremy Bettis <jbettis@chromium.org>
-rw-r--r-- | common/vboot/efs2.c | 5 | ||||
-rw-r--r-- | zephyr/test/vboot_efs2/src/main.c | 107 |
2 files changed, 101 insertions, 11 deletions
diff --git a/common/vboot/efs2.c b/common/vboot/efs2.c index 7cb8608daf..b45109029d 100644 --- a/common/vboot/efs2.c +++ b/common/vboot/efs2.c @@ -78,8 +78,13 @@ static enum cr50_comm_err send_to_cr50(const uint8_t *data, size_t size) if (uart_shell_stop()) { /* Failed to stop the shell. */ + /* LCOV_EXCL_START - At least on posix systems, uart_shell_stop + * will never fail, it will crash the binary or hang forever on + * error. + */ board_enable_packet_mode(false); return CR50_COMM_ERR_UNKNOWN; + /* LCOV_EXCL_STOP */ } /* diff --git a/zephyr/test/vboot_efs2/src/main.c b/zephyr/test/vboot_efs2/src/main.c index 53c77ea32e..1558fb75f0 100644 --- a/zephyr/test/vboot_efs2/src/main.c +++ b/zephyr/test/vboot_efs2/src/main.c @@ -15,6 +15,7 @@ #include "usbc_ppc.h" #include "vboot.h" +#include "zephyr/devicetree.h" #include <stdint.h> #include <zephyr/drivers/gpio/gpio_emul.h> @@ -24,6 +25,8 @@ #include <zephyr/ztest_assert.h> #include <zephyr/ztest_test_new.h> +#define SERIAL_BUFFER_SIZE DT_PROP(DT_NODELABEL(test_uart), buffer_size) + static int show_power_shortage_called; void show_power_shortage(void) { @@ -160,21 +163,22 @@ static void reply_cr50_payload(const struct device *dev, void *user_data) * with whatever is in user_data which holds a cr50 * reply. */ - if (req.size <= + if (req.size + sizeof(req) <= serial_vnd_out_data_size_get(uart_shell_dev)) { serial_vnd_read_out_data(uart_shell_dev, NULL, - req.size); + req.size + + sizeof(req)); serial_vnd_queue_in_data( uart_shell_dev, user_data, sizeof(struct cr50_comm_response)); } } } else { - /* Packet mode is off, so just consume some bytes from the out - * buffer. The buffer is only 200 in the dts, so 1000 will - * consume it all. + /* Packet mode is off, so just consume enough bytes from the out + * buffer to clear it. */ - serial_vnd_read_out_data(uart_shell_dev, NULL, 1000); + serial_vnd_read_out_data(uart_shell_dev, NULL, + SERIAL_BUFFER_SIZE); } } @@ -196,8 +200,6 @@ ZTEST(vboot_efs2, test_vboot_main_jump_bad_payload) zassert_true(strstr(outbuffer, "VB Ping Cr50") != NULL, "Expected msg not in %s", outbuffer); - zassert_true(strstr(outbuffer, "VB Hash mismatch") != NULL, - "Expected msg not in %s", outbuffer); zassert_true(vboot_allow_usb_pd(), NULL); zassert_equal(show_power_shortage_called, 0, NULL); zassert_equal(show_critical_error_called, 0, NULL); @@ -230,6 +232,37 @@ ZTEST(vboot_efs2, test_vboot_main_jump_bad_crc) zassert_equal(show_critical_error_called, 1, NULL); } +ZTEST(vboot_efs2, test_vboot_main_vboot_get_rw_hash_fail) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + struct ec_response_vboot_hash response; + struct ec_params_vboot_hash hash_start_params = { + .cmd = EC_VBOOT_HASH_START, + .hash_type = EC_VBOOT_HASH_TYPE_SHA256, + .offset = 0, + .size = 0x12345, + }; + struct host_cmd_handler_args hash_start_args = BUILD_HOST_COMMAND( + EC_CMD_VBOOT_HASH, 0, response, hash_start_params); + + shell_backend_dummy_clear_output(shell_zephyr); + + zassert_ok(host_command_process(&hash_start_args), NULL); + vboot_main(); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(strstr(outbuffer, "VB Ping Cr50") != NULL, + "Expected msg not in %s", outbuffer); + zassert_true(strstr(outbuffer, "VB Failed to verify RW (0x6)") != NULL, + "Expected msg not in %s", outbuffer); + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_power_shortage_called, 0, NULL); + zassert_equal(show_critical_error_called, 1, NULL); +} + ZTEST(vboot_efs2, test_vboot_main_jump_success) { const struct shell *shell_zephyr = get_ec_shell(); @@ -248,14 +281,64 @@ ZTEST(vboot_efs2, test_vboot_main_jump_success) zassert_true(strstr(outbuffer, "VB Ping Cr50") != NULL, "Expected msg not in %s", outbuffer); - zassert_true(strstr(outbuffer, "VB Failed to jump") != NULL, - "Expected msg not in %s", outbuffer); zassert_false(vboot_allow_usb_pd(), NULL); zassert_equal(show_power_shortage_called, 0, NULL); zassert_equal(show_critical_error_called, 1, NULL); zassert_equal(system_get_reset_flags(), 0, NULL); } +ZTEST(vboot_efs2, test_shutdown_hook_in_rw) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + /* Set system_is_in_rw */ + system_set_shrspi_image_copy(EC_IMAGE_RW); + + shell_backend_dummy_clear_output(shell_zephyr); + hook_notify(HOOK_CHIPSET_SHUTDOWN_COMPLETE); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(strstr(outbuffer, "VB hook_shutdown") != NULL, + "Expected msg not in %s", outbuffer); + zassert_equal(system_get_reset_flags(), 0, NULL); + + /* Verify some things we don't expect also. */ + zassert_true(strstr(outbuffer, "VB Ping Cr50") == NULL, + "Unexpected msg in %s", outbuffer); + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_critical_error_called, 0, NULL); + zassert_equal(show_power_shortage_called, 0, NULL); +} + +ZTEST(vboot_efs2, test_shutdown_hook_in_ro) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + /* Set system_is_in_rw */ + system_set_shrspi_image_copy(EC_IMAGE_RO); + + shell_backend_dummy_clear_output(shell_zephyr); + hook_notify(HOOK_CHIPSET_SHUTDOWN_COMPLETE); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(strstr(outbuffer, "VB hook_shutdown") != NULL, + "Expected msg not in %s", outbuffer); + zassert_true(strstr(outbuffer, "VB Ping Cr50") != NULL, + "Expected msg not in %s", outbuffer); + zassert_equal(system_get_reset_flags(), EC_RESET_FLAG_AP_IDLE, NULL); + zassert_equal(show_critical_error_called, 1, NULL); + + /* Verify some things we don't expect also. */ + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_power_shortage_called, 0, NULL); +} + void *vboot_efs2_setup(void) { /* Wait for the shell to start. */ @@ -275,9 +358,11 @@ void vboot_efs2_cleanup(void *fixture) show_power_shortage_called = 0; show_critical_error_called = 0; system_exit_manual_recovery(); - system_clear_reset_flags(EC_RESET_FLAG_STAY_IN_RO | EC_RESET_FLAG_EFS); + system_clear_reset_flags(EC_RESET_FLAG_STAY_IN_RO | EC_RESET_FLAG_EFS | + EC_RESET_FLAG_AP_IDLE); vboot_disable_pd(); serial_vnd_set_callback(uart_shell_dev, NULL, NULL); + serial_vnd_read_out_data(uart_shell_dev, NULL, SERIAL_BUFFER_SIZE); } ZTEST_SUITE(vboot_efs2, NULL, vboot_efs2_setup, NULL, vboot_efs2_cleanup, NULL); |