cread.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2023 Google LLC
  4. */
  5. #include <common.h>
  6. #include <cli.h>
  7. #include <test/common.h>
  8. #include <test/test.h>
  9. #include <test/ut.h>
  10. static int cli_ch_test(struct unit_test_state *uts)
  11. {
  12. struct cli_ch_state s_cch, *cch = &s_cch;
  13. cli_ch_init(cch);
  14. /* should be nothing to return at first */
  15. ut_asserteq(0, cli_ch_process(cch, 0));
  16. /* check normal entry */
  17. ut_asserteq('a', cli_ch_process(cch, 'a'));
  18. ut_asserteq('b', cli_ch_process(cch, 'b'));
  19. ut_asserteq('c', cli_ch_process(cch, 'c'));
  20. ut_asserteq(0, cli_ch_process(cch, 0));
  21. /* send an invalid escape sequence */
  22. ut_asserteq(0, cli_ch_process(cch, '\e'));
  23. ut_asserteq(0, cli_ch_process(cch, '['));
  24. /*
  25. * with the next char it sees that the sequence is invalid, so starts
  26. * emitting it
  27. */
  28. ut_asserteq('\e', cli_ch_process(cch, 'X'));
  29. /* now we set 0 bytes to empty the buffer */
  30. ut_asserteq('[', cli_ch_process(cch, 0));
  31. ut_asserteq('X', cli_ch_process(cch, 0));
  32. ut_asserteq(0, cli_ch_process(cch, 0));
  33. /* things are normal again */
  34. ut_asserteq('a', cli_ch_process(cch, 'a'));
  35. ut_asserteq(0, cli_ch_process(cch, 0));
  36. return 0;
  37. }
  38. COMMON_TEST(cli_ch_test, 0);
  39. static int cread_test(struct unit_test_state *uts)
  40. {
  41. int duration;
  42. ulong start;
  43. char buf[10];
  44. /*
  45. * useful for debugging
  46. *
  47. * gd->flags &= ~GD_FLG_RECORD;
  48. * print_buffer(0, buf, 1, 7, 0);
  49. */
  50. console_record_reset_enable();
  51. /* simple input */
  52. *buf = '\0';
  53. ut_asserteq(4, console_in_puts("abc\n"));
  54. ut_asserteq(3, cli_readline_into_buffer("-> ", buf, 1));
  55. ut_asserteq_str("abc", buf);
  56. /* try an escape sequence (cursor left after the 'c') */
  57. *buf = '\0';
  58. ut_asserteq(8, console_in_puts("abc\e[Dx\n"));
  59. ut_asserteq(4, cli_readline_into_buffer("-> ", buf, 1));
  60. ut_asserteq_str("abxc", buf);
  61. /* invalid escape sequence */
  62. *buf = '\0';
  63. ut_asserteq(8, console_in_puts("abc\e[Xx\n"));
  64. ut_asserteq(7, cli_readline_into_buffer("-> ", buf, 1));
  65. ut_asserteq_str("abc\e[Xx", buf);
  66. /* check timeout, should be between 1000 and 1050ms */
  67. start = get_timer(0);
  68. *buf = '\0';
  69. ut_asserteq(-2, cli_readline_into_buffer("-> ", buf, 1));
  70. duration = get_timer(start) - 1000;
  71. ut_assert(duration >= 0);
  72. ut_assert(duration < 50);
  73. return 0;
  74. }
  75. COMMON_TEST(cread_test, 0);