Simon Glass | 60217a2 | 2023-03-28 08:34:13 +1300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2023 Google LLC |
| 4 | */ |
| 5 | |
Simon Glass | 60217a2 | 2023-03-28 08:34:13 +1300 | [diff] [blame] | 6 | #include <cli.h> |
Tom Rini | d22d069 | 2024-04-27 08:10:58 -0600 | [diff] [blame^] | 7 | #include <time.h> |
Simon Glass | 60217a2 | 2023-03-28 08:34:13 +1300 | [diff] [blame] | 8 | #include <test/common.h> |
| 9 | #include <test/test.h> |
| 10 | #include <test/ut.h> |
| 11 | |
| 12 | static int cli_ch_test(struct unit_test_state *uts) |
| 13 | { |
| 14 | struct cli_ch_state s_cch, *cch = &s_cch; |
| 15 | |
| 16 | cli_ch_init(cch); |
| 17 | |
| 18 | /* should be nothing to return at first */ |
| 19 | ut_asserteq(0, cli_ch_process(cch, 0)); |
| 20 | |
| 21 | /* check normal entry */ |
| 22 | ut_asserteq('a', cli_ch_process(cch, 'a')); |
| 23 | ut_asserteq('b', cli_ch_process(cch, 'b')); |
| 24 | ut_asserteq('c', cli_ch_process(cch, 'c')); |
| 25 | ut_asserteq(0, cli_ch_process(cch, 0)); |
| 26 | |
| 27 | /* send an invalid escape sequence */ |
| 28 | ut_asserteq(0, cli_ch_process(cch, '\e')); |
| 29 | ut_asserteq(0, cli_ch_process(cch, '[')); |
| 30 | |
| 31 | /* |
| 32 | * with the next char it sees that the sequence is invalid, so starts |
| 33 | * emitting it |
| 34 | */ |
| 35 | ut_asserteq('\e', cli_ch_process(cch, 'X')); |
| 36 | |
| 37 | /* now we set 0 bytes to empty the buffer */ |
| 38 | ut_asserteq('[', cli_ch_process(cch, 0)); |
| 39 | ut_asserteq('X', cli_ch_process(cch, 0)); |
| 40 | ut_asserteq(0, cli_ch_process(cch, 0)); |
| 41 | |
| 42 | /* things are normal again */ |
| 43 | ut_asserteq('a', cli_ch_process(cch, 'a')); |
| 44 | ut_asserteq(0, cli_ch_process(cch, 0)); |
| 45 | |
Yurii Monakov | 6b911b6 | 2023-10-10 11:16:39 +0300 | [diff] [blame] | 46 | /* unexpected 'Esc' */ |
| 47 | ut_asserteq('a', cli_ch_process(cch, 'a')); |
| 48 | ut_asserteq(0, cli_ch_process(cch, '\e')); |
| 49 | ut_asserteq('b', cli_ch_process(cch, 'b')); |
| 50 | ut_asserteq(0, cli_ch_process(cch, 0)); |
| 51 | |
Simon Glass | 60217a2 | 2023-03-28 08:34:13 +1300 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | COMMON_TEST(cli_ch_test, 0); |
Simon Glass | 26e0cab | 2023-03-28 08:34:14 +1300 | [diff] [blame] | 55 | |
| 56 | static int cread_test(struct unit_test_state *uts) |
| 57 | { |
| 58 | int duration; |
| 59 | ulong start; |
| 60 | char buf[10]; |
| 61 | |
| 62 | /* |
| 63 | * useful for debugging |
| 64 | * |
| 65 | * gd->flags &= ~GD_FLG_RECORD; |
| 66 | * print_buffer(0, buf, 1, 7, 0); |
| 67 | */ |
| 68 | |
| 69 | console_record_reset_enable(); |
| 70 | |
| 71 | /* simple input */ |
| 72 | *buf = '\0'; |
| 73 | ut_asserteq(4, console_in_puts("abc\n")); |
| 74 | ut_asserteq(3, cli_readline_into_buffer("-> ", buf, 1)); |
| 75 | ut_asserteq_str("abc", buf); |
| 76 | |
| 77 | /* try an escape sequence (cursor left after the 'c') */ |
| 78 | *buf = '\0'; |
| 79 | ut_asserteq(8, console_in_puts("abc\e[Dx\n")); |
| 80 | ut_asserteq(4, cli_readline_into_buffer("-> ", buf, 1)); |
| 81 | ut_asserteq_str("abxc", buf); |
| 82 | |
| 83 | /* invalid escape sequence */ |
| 84 | *buf = '\0'; |
| 85 | ut_asserteq(8, console_in_puts("abc\e[Xx\n")); |
| 86 | ut_asserteq(7, cli_readline_into_buffer("-> ", buf, 1)); |
| 87 | ut_asserteq_str("abc\e[Xx", buf); |
| 88 | |
Yurii Monakov | 6b911b6 | 2023-10-10 11:16:39 +0300 | [diff] [blame] | 89 | /* unexpected 'Esc' */ |
| 90 | *buf = '\0'; |
| 91 | ut_asserteq(7, console_in_puts("abc\eXx\n")); |
| 92 | ut_asserteq(5, cli_readline_into_buffer("-> ", buf, 1)); |
| 93 | ut_asserteq_str("abcXx", buf); |
| 94 | |
Simon Glass | 26e0cab | 2023-03-28 08:34:14 +1300 | [diff] [blame] | 95 | /* check timeout, should be between 1000 and 1050ms */ |
| 96 | start = get_timer(0); |
| 97 | *buf = '\0'; |
| 98 | ut_asserteq(-2, cli_readline_into_buffer("-> ", buf, 1)); |
| 99 | duration = get_timer(start) - 1000; |
| 100 | ut_assert(duration >= 0); |
| 101 | ut_assert(duration < 50); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | COMMON_TEST(cread_test, 0); |