From 740b99c937734dec535886a83f8ca6941d568d09 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Thu, 7 May 2020 16:08:36 -0600 Subject: [PATCH] tests: shell: shell_flash: Add a test for 'flash read' Add a simple test for this command to check that the output is as expected. This requires a larger output buffer in shell_dummy, so update that too. Signed-off-by: Simon Glass --- include/shell/shell_dummy.h | 2 +- tests/subsys/shell/shell_flash/CMakeLists.txt | 8 ++ tests/subsys/shell/shell_flash/prj.conf | 11 +++ .../shell/shell_flash/src/shell_flash_test.c | 80 +++++++++++++++++++ tests/subsys/shell/shell_flash/testcase.yaml | 7 ++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 tests/subsys/shell/shell_flash/CMakeLists.txt create mode 100644 tests/subsys/shell/shell_flash/prj.conf create mode 100644 tests/subsys/shell/shell_flash/src/shell_flash_test.c create mode 100644 tests/subsys/shell/shell_flash/testcase.yaml diff --git a/include/shell/shell_dummy.h b/include/shell/shell_dummy.h index 675d4cddfaa..c320caa04a9 100644 --- a/include/shell/shell_dummy.h +++ b/include/shell/shell_dummy.h @@ -24,7 +24,7 @@ struct shell_dummy { size_t len; /** output buffer to collect shell output */ - char buf[100]; + char buf[300]; }; #define SHELL_DUMMY_DEFINE(_name) \ diff --git a/tests/subsys/shell/shell_flash/CMakeLists.txt b/tests/subsys/shell/shell_flash/CMakeLists.txt new file mode 100644 index 00000000000..25291ebc49e --- /dev/null +++ b/tests/subsys/shell/shell_flash/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) +project(shell) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/subsys/shell/shell_flash/prj.conf b/tests/subsys/shell/shell_flash/prj.conf new file mode 100644 index 00000000000..c78ce7b6143 --- /dev/null +++ b/tests/subsys/shell/shell_flash/prj.conf @@ -0,0 +1,11 @@ +CONFIG_FLASH=y +CONFIG_FLASH_SIMULATOR=y +CONFIG_FLASH_SHELL=y +CONFIG_SHELL=y +CONFIG_SHELL_BACKEND_SERIAL=n +CONFIG_SHELL_BACKEND_DUMMY=y +CONFIG_SHELL_CMD_BUFF_SIZE=90 +CONFIG_SHELL_PRINTF_BUFF_SIZE=15 +CONFIG_SHELL_METAKEYS=n +CONFIG_LOG=n +CONFIG_ZTEST=y diff --git a/tests/subsys/shell/shell_flash/src/shell_flash_test.c b/tests/subsys/shell/shell_flash/src/shell_flash_test.c new file mode 100644 index 00000000000..4bbeba5cc13 --- /dev/null +++ b/tests/subsys/shell/shell_flash/src/shell_flash_test.c @@ -0,0 +1,80 @@ +/* + * Copyright 2020 Google LLC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** @file + * @brief Interactive shell test suite for 'flash' command + * + */ + +#include +#include +#include + +#include +#include +#include + +/* configuration derived from DT */ +#ifdef CONFIG_ARCH_POSIX +#define SOC_NV_FLASH_NODE DT_CHILD(DT_INST(0, zephyr_sim_flash), flash_0) +#else +#define SOC_NV_FLASH_NODE DT_CHILD(DT_INST(0, zephyr_sim_flash), flash_sim_0) +#endif /* CONFIG_ARCH_POSIX */ +#define FLASH_SIMULATOR_BASE_OFFSET DT_REG_ADDR(SOC_NV_FLASH_NODE) + +/* Test 'flash read' shell command */ +static void test_flash_read(void) +{ + /* To keep the test simple, just compare against known data */ + char *const lines[] = { + "00000000: 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 |ABCDEFGH IJKLMNOP|", + "00000010: 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 |QRSTUVWX YZ[\\]^_`|", + "00000020: 61 62 63 |abc |", + }; + const struct shell *shell = shell_backend_dummy_get_ptr(); + static struct device *flash_dev; + const char *buf, *ptr; + const int test_base = FLASH_SIMULATOR_BASE_OFFSET; + const int test_size = 0x24; /* 32-alignment required */ + uint8_t data[test_size]; + size_t size; + int ret; + int i; + + for (i = 0; i < test_size; i++) { + data[i] = 'A' + i; + } + flash_dev = device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL); + + zassert_true(flash_dev != NULL, + "Simulated flash driver was not found!"); + + ret = flash_write_protection_set(flash_dev, false); + zassert_equal(0, ret, NULL); + + ret = flash_write(flash_dev, test_base, data, test_size); + zassert_equal(0, ret, "flash_write() failed: %d", ret); + + ret = shell_execute_cmd(NULL, "flash read 0 23"); + zassert_equal(0, ret, "flash read failed: %d", ret); + + buf = shell_backend_dummy_get_output(shell, &size); + for (i = 0, ptr = buf; i < ARRAY_SIZE(lines); i++) { + ptr += 2; /* Skip \r\n */ + zassert_false(strncmp(ptr, lines[i], strlen(lines[i])), + "Incorrect line %d: %s", i, ptr); + ptr += strlen(lines[i]); /* Skip to next line */ + } +} + +void test_main(void) +{ + ztest_test_suite(shell_flash_test_suite, + ztest_unit_test(test_flash_read) + ); + + ztest_run_test_suite(shell_flash_test_suite); +} diff --git a/tests/subsys/shell/shell_flash/testcase.yaml b/tests/subsys/shell/shell_flash/testcase.yaml new file mode 100644 index 00000000000..bbe0ac79456 --- /dev/null +++ b/tests/subsys/shell/shell_flash/testcase.yaml @@ -0,0 +1,7 @@ +tests: + shell.history: + min_flash: 64 + min_ram: 32 + filter: ( CONFIG_FLASH_SHELL ) + tags: flash shell + platform_whitelist: qemu_x86 native_posix