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 <sjg@chromium.org>
diff --git a/include/shell/shell_dummy.h b/include/shell/shell_dummy.h
index 675d4cd..c320caa 100644
--- a/include/shell/shell_dummy.h
+++ b/include/shell/shell_dummy.h
@@ -24,7 +24,7 @@
 	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 0000000..25291eb
--- /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 0000000..c78ce7b
--- /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 0000000..4bbeba5
--- /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 <zephyr.h>
+#include <ztest.h>
+#include <device.h>
+
+#include <drivers/flash.h>
+#include <shell/shell.h>
+#include <shell/shell_dummy.h>
+
+/* 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 0000000..bbe0ac7
--- /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