From b39bdaaa0160ac9545a16d88d363731adbdc8b73 Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Thu, 6 Jan 2022 19:31:58 -0500 Subject: [PATCH] add test for f715214 --- termbox.h | 11 +++++-- tests/test_large_input/expected.ansi | 24 +++++++++++++++ tests/test_large_input/test.php | 46 ++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 tests/test_large_input/expected.ansi create mode 100755 tests/test_large_input/test.php diff --git a/termbox.h b/termbox.h index 0e4a294..033bd6f 100644 --- a/termbox.h +++ b/termbox.h @@ -256,6 +256,13 @@ extern "C" { // __ffi_strip #define TB_OPT_PRINTF_BUF 4096 #endif +/* Define this to set the size of the read buffer used when reading + * from the tty + */ +#ifndef TB_OPT_READ_BUF +#define TB_OPT_READ_BUF 64 +#endif + /* Define this for limited back compat with termbox v1 */ #ifdef TB_OPT_V1_COMPAT #define tb_change_cell tb_set_cell @@ -2058,7 +2065,7 @@ static int update_term_size_via_esc() { return TB_ERR_RESIZE_POLL; } - char buf[64]; + char buf[TB_OPT_READ_BUF]; ssize_t read_rv = read(global.rfd, buf, sizeof(buf) - 1); if (read_rv < 1) { global.last_errno = errno; @@ -2337,7 +2344,7 @@ static const char *get_terminfo_string(int16_t str_offsets_pos, static int wait_event(struct tb_event *event, int timeout) { int rv; - char buf[64]; + char buf[TB_OPT_READ_BUF]; memset(event, 0, sizeof(*event)); if_ok_return(rv, extract_event(event)); diff --git a/tests/test_large_input/expected.ansi b/tests/test_large_input/expected.ansi new file mode 100644 index 0000000..7a50277 --- /dev/null +++ b/tests/test_large_input/expected.ansi @@ -0,0 +1,24 @@ +#5up_arrow_count=22 + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/test_large_input/test.php b/tests/test_large_input/test.php new file mode 100755 index 0000000..38dd898 --- /dev/null +++ b/tests/test_large_input/test.php @@ -0,0 +1,46 @@ +memfd_create('ttyin', 0); +$ttyout = $libc->memfd_create('ttyout', 0); +$test->ffi->tb_init_rwfd($ttyin, $ttyout); + +// we're going to send a bunch of up-arrow escape sequences to termbox via our +// fake tty, and then ensure that termbox emits the expected up-arrow events +// via `tb_peek_event`. we will send enough data to fill up the read buffer +// (TB_OPT_READ_BUF). +$up_arrow = "\x1bOA"; +$read_buf_size = $test->defines['TB_OPT_READ_BUF']; +$num_up_arrows = (int)ceil($read_buf_size / strlen($up_arrow)); +$input_data = str_repeat($up_arrow, $num_up_arrows); +$fttyin = fopen("php://fd/$ttyin", 'w'); +$nbytes = fwrite($fttyin, $input_data); +fseek($fttyin, strlen($input_data) * -1, SEEK_CUR); + +// count how many up arrow events termbox emits +$up_arrow_count = 0; +$test->ffi->tb_set_input_mode($test->defines['TB_INPUT_ALT']); +$event = $test->ffi->new('struct tb_event'); +do { + $rv = $test->ffi->tb_peek_event(FFI::addr($event), 1000); + if ($rv == 0 && $event->key === $test->defines['TB_KEY_ARROW_UP']) { + $up_arrow_count += 1; + } +} while ($rv == 0); + +// close fake termbox setup +fclose($fttyin); +$libc->close($ttyin); +$libc->close($ttyout); +$test->ffi->tb_shutdown(); + +// display up_arrow_count +$test->ffi->tb_init(); +$test->ffi->tb_printf(0, 0, 0, 0, "up_arrow_count=%d", $up_arrow_count); +$test->ffi->tb_present(); +$test->screencap(); -- 2.39.5