vtgl

terminal emulator implemented in OpenGL
git clone anongit@rnpnr.xyz:vtgl.git
Log | Files | Refs | Feed | LICENSE

Commit: 845f8fab3fc9c2622c947a26d3398cb7972ce934
Parent: 25ca9d949d615106cd04d6e3d9851f67a6132976
Author: Randy Palamar
Date:   Sun, 16 Feb 2025 21:47:21 -0700

tests: initial platform stubs

This aids in fuzz testing - particularly for OSC inputs.

Diffstat:
Mbuild.sh | 2+-
Mtests/fuzz_in/data | 3+--
Mtests/test-common.c | 32+++++++++++++++++++++++++++-----
Mtests/test-fuzz-results.c | 4+++-
Mtests/test-fuzz.c | 2+-
Mtests/test.c | 2+-
6 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/build.sh b/build.sh @@ -44,7 +44,7 @@ release) ;; fuzz) afl-clang-fast ${testcflags} -O3 -o tests/test-fuzz tests/test-fuzz.c - afl-fuzz -o fuzz_out -i tests/fuzz_in ./tests/test-fuzz + AFL_AUTORESUME=1 afl-fuzz -o fuzz_out -i tests/fuzz_in ./tests/test-fuzz exit 0 ;; fuzz_results) diff --git a/tests/fuzz_in/data b/tests/fuzz_in/data @@ -1,3 +1,3 @@  a(B>[!p a  a[4»1H[?1h[?3;4l ~ - ~ ac -\ No newline at end of file + ~ ac]2;vis: [No Name] diff --git a/tests/test-common.c b/tests/test-common.c @@ -13,6 +13,18 @@ KEYBIND_FN(zoom) { return 0; } #include "font.c" #include "terminal.c" +static PLATFORM_WINDOW_TITLE_FN(test_get_window_title) +{ + ASSERT(buffer); + stream_push_s8(buffer, s8("test_title")); +} + +static PLATFORM_WINDOW_TITLE_FN(test_set_window_title) +{ + ASSERT(buffer); + stream_push_byte(buffer, 0); +} + static size copy_into_ringbuf(RingBuf *rb, s8 raw) { @@ -47,10 +59,15 @@ launder_static_string(Term *term, s8 static_str) static Term * place_term_into_memory(MemoryBlock memory, i32 rows, i32 columns) { - Term *t = memory.memory; - t->arena_for_frame = arena_from_memory_block(memory); - t->arena_for_frame.beg += sizeof(*t); - t->size = (iv2){.w = 80, .h = 24}; + Arena tmp = arena_from_memory_block(memory); + Term *t = push_struct(&tmp, Term); + t->size = (iv2){.w = 80, .h = 24}; + + t->platform = push_struct(&tmp, typeof(*t->platform)); + t->platform->set_window_title = test_set_window_title; + t->platform->get_window_title = test_get_window_title; + + t->arena_for_frame = tmp; os_allocate_ring_buffer(&t->views[0].log, MB(2)); line_buf_alloc(&t->views[0].lines, &t->arena_for_frame, t->views[0].log.buf, t->cursor.style, @@ -70,11 +87,16 @@ place_term_into_memory(MemoryBlock memory, i32 rows, i32 columns) return t; } +/* NOTE(rnp): these work better with ASAN for testing */ +/* TODO(rnp): add appropriate poison to normal os_block_allocator when compiling with ASAN */ +void *malloc(size_t); +void free(void *); + static void release_term_memory(MemoryBlock backing) { Term *t = backing.memory; os_release_ring_buffer(&t->views[0].log); os_release_ring_buffer(&t->views[1].log); - os_release_memory_block(backing); + free(backing.memory); } diff --git a/tests/test-fuzz-results.c b/tests/test-fuzz-results.c @@ -15,7 +15,7 @@ main(i32 argc, char *argv[]) Arena file_backing = arena_from_memory_block(os_block_alloc(MB(1))); s8 file_data = os_read_file((u8 *)argv[1], &file_backing); - MemoryBlock term_backing = os_block_alloc(MB(4)); + MemoryBlock term_backing = {.memory = malloc(MB(4)), .size = MB(4)}; Term *term = place_term_into_memory(term_backing, 24, 80); term->error_stream = arena_stream(arena_from_memory_block(os_block_alloc(MB(4)))); s8 raw = launder_static_string(term, file_data); @@ -24,5 +24,7 @@ main(i32 argc, char *argv[]) if (term->error_stream.widx != 0) os_write_err_msg(stream_to_s8(&term->error_stream)); + release_term_memory(term_backing); + return 0; } diff --git a/tests/test-fuzz.c b/tests/test-fuzz.c @@ -10,7 +10,7 @@ main(void) __AFL_INIT(); u8 *buf = __AFL_FUZZ_TESTCASE_BUF; while (__AFL_LOOP(10000)) { - MemoryBlock term_backing = os_block_alloc(MB(4)); + MemoryBlock term_backing = {.memory = malloc(MB(4)), .size = MB(4)}; Term *term = place_term_into_memory(term_backing, 24, 80); i32 len = __AFL_FUZZ_TESTCASE_LEN; s8 raw = launder_static_string(term, (s8){.data = buf, .len = len}); diff --git a/tests/test.c b/tests/test.c @@ -1019,7 +1019,7 @@ main(void) u32 failure_count = 0; for (u32 i = 0; i < ARRAY_COUNT(tests); i++) { - MemoryBlock term_backing = os_block_alloc(MB(4)); + MemoryBlock term_backing = {.memory = malloc(MB(4)), .size = MB(4)}; /* TODO(rnp): term sizes as part of the tests */ Term *term = place_term_into_memory(term_backing, 24, 80); struct test_result result = tests[i](term, term->arena_for_frame);