From 0e9d829e913a28f148a191bb2664a91e2888d985 Mon Sep 17 00:00:00 2001 From: Vic Yang Date: Wed, 7 Aug 2013 06:06:44 +0800 Subject: Add test for console command history This tests that command history is as expected. Also fix a bug that some checks in console_edit test are skipped. BUG=chrome-os-partner:19236 TEST=Pass console_edit test. BRANCH=None Change-Id: Ifbd3d1690f25b35bf5efe523e656b013aa534d26 Signed-off-by: Vic Yang Reviewed-on: https://gerrit.chromium.org/gerrit/64837 --- board/host/board.h | 3 +++ chip/host/uart.c | 33 ++++++++++++++++++++++++++++++ include/test_util.h | 6 ++++++ test/console_edit.c | 58 +++++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 92 insertions(+), 8 deletions(-) diff --git a/board/host/board.h b/board/host/board.h index 3707e2a1f4..b80ac27fdb 100644 --- a/board/host/board.h +++ b/board/host/board.h @@ -15,6 +15,9 @@ #define CONFIG_TEMP_SENSOR #undef CONFIG_WATCHDOG +#undef CONFIG_CONSOLE_HISTORY +#define CONFIG_CONSOLE_HISTORY 4 + /* Host test config */ #ifdef TEST_SMART_BATTERY_CHARGER #define CONFIG_BATTERY_MOCK diff --git a/chip/host/uart.c b/chip/host/uart.c index de2129dbf3..b6369df426 100644 --- a/chip/host/uart.c +++ b/chip/host/uart.c @@ -32,6 +32,37 @@ static struct queue cached_char = { .buf = cached_char_buf, }; +#define CONSOLE_CAPTURE_SIZE 2048 +static char capture_buf[CONSOLE_CAPTURE_SIZE]; +static int capture_size; +static int capture_enabled; + +void test_capture_console(int enabled) +{ + if (enabled == capture_enabled) + return; + + if (enabled) + capture_size = 0; + else + capture_buf[capture_size] = '\0'; + + capture_enabled = enabled; +} + +static void test_capture_char(char c) +{ + if (capture_size == CONSOLE_CAPTURE_SIZE) + return; + capture_buf[capture_size++] = c; +} + + +const char *test_get_captured_console(void) +{ + return (const char *)capture_buf; +} + static void trigger_interrupt(void) { /* @@ -80,6 +111,8 @@ int uart_rx_available(void) void uart_write_char(char c) { + if (capture_enabled) + test_capture_char(c); printf("%c", c); fflush(stdout); } diff --git a/include/test_util.h b/include/test_util.h index ae237809c1..160d1cab68 100644 --- a/include/test_util.h +++ b/include/test_util.h @@ -101,4 +101,10 @@ void test_chipset_on(void); /* Simulates chipset power off */ void test_chipset_off(void); +/* Start/stop capturing console output */ +void test_capture_console(int enabled); + +/* Get captured console output */ +const char *test_get_captured_console(void); + #endif /* __CROS_EC_TEST_UTIL_H */ diff --git a/test/console_edit.c b/test/console_edit.c index 1649102477..5b4cc7944c 100644 --- a/test/console_edit.c +++ b/test/console_edit.c @@ -68,6 +68,26 @@ static void ctrl_key(char c) UART_INJECT(seq); } +/* + * Helper function to compare multiline strings. When comparing, CR's are + * ignored. + */ +static int compare_multiline_string(const char *s1, const char *s2) +{ + do { + while (*s1 == '\r') + ++s1; + while (*s2 == '\r') + ++s2; + if (*s1 != *s2) + return 1; + ++s1; + ++s2; + } while (*s1 || *s2); + + return 0; +} + /*****************************************************************************/ /* Tests */ @@ -156,8 +176,7 @@ static int test_history_up_up(void) arrow_key(ARROW_UP, 2); UART_INJECT("\n"); msleep(30); - TEST_CHECK(cmd_1_call_cnt == 2); - TEST_CHECK(cmd_2_call_cnt == 1); + TEST_CHECK(cmd_1_call_cnt == 2 && cmd_2_call_cnt == 1); } static int test_history_up_up_down(void) @@ -172,8 +191,7 @@ static int test_history_up_up_down(void) arrow_key(ARROW_DOWN, 1); UART_INJECT("\n"); msleep(30); - TEST_CHECK(cmd_1_call_cnt == 1); - TEST_CHECK(cmd_2_call_cnt == 2); + TEST_CHECK(cmd_1_call_cnt == 1 && cmd_2_call_cnt == 2); } static int test_history_edit(void) @@ -185,8 +203,7 @@ static int test_history_edit(void) arrow_key(ARROW_UP, 1); UART_INJECT("\b2\n"); msleep(30); - TEST_CHECK(cmd_1_call_cnt == 1); - TEST_CHECK(cmd_2_call_cnt == 1); + TEST_CHECK(cmd_1_call_cnt == 1 && cmd_2_call_cnt == 1); } static int test_history_stash(void) @@ -200,8 +217,32 @@ static int test_history_stash(void) arrow_key(ARROW_DOWN, 1); UART_INJECT("2\n"); msleep(30); - TEST_CHECK(cmd_1_call_cnt == 1); - TEST_CHECK(cmd_2_call_cnt == 1); + TEST_CHECK(cmd_1_call_cnt == 1 && cmd_2_call_cnt == 1); +} + +static int test_history_list(void) +{ + const char *exp_output = "history\n" /* Input command */ + "test3\n" /* Output 4 last commands */ + "test4\n" + "test5\n" + "history\n" + "> "; + + UART_INJECT("test1\n"); + UART_INJECT("test2\n"); + UART_INJECT("test3\n"); + UART_INJECT("test4\n"); + UART_INJECT("test5\n"); + msleep(30); + test_capture_console(1); + UART_INJECT("history\n"); + msleep(30); + test_capture_console(0); + TEST_ASSERT(compare_multiline_string(test_get_captured_console(), + exp_output) == 0); + + return EC_SUCCESS; } void run_test(void) @@ -219,6 +260,7 @@ void run_test(void) RUN_TEST(test_history_up_up_down); RUN_TEST(test_history_edit); RUN_TEST(test_history_stash); + RUN_TEST(test_history_list); test_print_result(); } -- cgit v1.2.1