diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/api_args_test.c | 140 | ||||
| -rw-r--r-- | tests/dcs_test.c | 53 | ||||
| -rw-r--r-- | tests/meson.build | 14 | ||||
| -rw-r--r-- | tests/model.c | 8 | ||||
| -rw-r--r-- | tests/model.h | 5 | ||||
| -rw-r--r-- | tests/model_row_test.c | 2 | ||||
| -rw-r--r-- | tests/model_test.c | 2 | ||||
| -rw-r--r-- | tests/osc_test.c | 32 | ||||
| -rw-r--r-- | tests/term_test.c | 99 | ||||
| -rw-r--r-- | tests/version_test.c | 27 |
10 files changed, 369 insertions, 13 deletions
diff --git a/tests/api_args_test.c b/tests/api_args_test.c new file mode 100644 index 0000000..18af728 --- /dev/null +++ b/tests/api_args_test.c @@ -0,0 +1,140 @@ +#include "term_api.h" +#include <assert.h> +#include <stdbool.h> +#include <string.h> + +/* A feed that returns false must not move the cursor or change a cell. */ +static void expect_unchanged(struct term *t, int x, int y, uint32_t cp) +{ + assert(tt_cursor_x(t) == x); + assert(tt_cursor_y(t) == y); + assert(tt_cell(t, 0, 0)->text == cp); +} + +static void test_feed_unknown_flag(void) +{ + struct term *t = tt_new(10, 3); + assert(t); + assert(term_feed(t, "abc", 3, 0)); + expect_unchanged(t, 3, 0, 'a'); + + assert(!term_feed(t, "xy", 2, 1u << 1)); + expect_unchanged(t, 3, 0, 'a'); + assert(!term_feed(t, "xy", 2, TERM_FEED_ONLCR | (1u << 4))); + expect_unchanged(t, 3, 0, 'a'); + assert(!term_feed(t, "xy", 2, 1u << 31)); + expect_unchanged(t, 3, 0, 'a'); + /* Unknown bits are rejected even when there are no bytes. */ + assert(!term_feed(t, "", 0, 1u << 1)); + + assert(term_feed(t, "\nz", 2, TERM_FEED_ONLCR)); + assert(tt_cursor_x(t) == 1); + assert(tt_cursor_y(t) == 1); + term_free(t); +} + +static void test_feed_null_buffer(void) +{ + struct term *t = tt_new(10, 3); + assert(t); + assert(term_feed(t, "abc", 3, 0)); + + assert(!term_feed(t, NULL, 1, 0)); + expect_unchanged(t, 3, 0, 'a'); + assert(!term_feed(t, NULL, 4096, TERM_FEED_ONLCR)); + expect_unchanged(t, 3, 0, 'a'); + + /* A zero length is a no-op, so a NULL buffer is fine. */ + assert(term_feed(t, NULL, 0, 0)); + assert(term_feed(t, "", 0, TERM_FEED_ONLCR)); + expect_unchanged(t, 3, 0, 'a'); + + assert(!term_feed(NULL, "a", 1, 0)); + assert(!term_feed(NULL, NULL, 0, 0)); + term_free(t); +} + +static void test_new_rejects_bad_options(void) +{ + struct term_options opt; + + memset(&opt, 0, sizeof(opt)); + opt.size.x = -1; + assert(!term_new(&opt)); + + memset(&opt, 0, sizeof(opt)); + opt.size.y = -1; + assert(!term_new(&opt)); + + memset(&opt, 0, sizeof(opt)); + opt.size.x = TERM_MAX_COLS + 1; + assert(!term_new(&opt)); + + memset(&opt, 0, sizeof(opt)); + opt.scrollback = -1; + assert(!term_new(&opt)); + + memset(&opt, 0, sizeof(opt)); + opt.cursor_style = (enum term_cursor_style)3; + assert(!term_new(&opt)); + + memset(&opt, 0, sizeof(opt)); + opt.cursor_style = (enum term_cursor_style)-1; + assert(!term_new(&opt)); +} + +static void test_new_accepts_defaults(void) +{ + struct term_options opt; + + memset(&opt, 0, sizeof(opt)); + struct term *t = term_new(&opt); + assert(t); + assert(tt_cols(t) == 80); + assert(tt_rows(t) == 24); + assert(term_cursor(t).style == TERM_CURSOR_BLOCK); + term_free(t); + + memset(&opt, 0, sizeof(opt)); + opt.size.x = TERM_MAX_COLS; + opt.size.y = 1; + opt.cursor_style = TERM_CURSOR_BAR; + t = term_new(&opt); + assert(t); + assert(tt_cols(t) == TERM_MAX_COLS); + assert(tt_rows(t) == 1); + assert(term_cursor(t).style == TERM_CURSOR_BAR); + term_free(t); +} + +static void test_resize_rejects_too_wide(void) +{ + struct term *t = tt_new(10, 3); + assert(t); + assert(term_feed(t, "abc", 3, 0)); + + assert(!tt_resize(t, TERM_MAX_COLS + 1, 3)); + assert(tt_cols(t) == 10); + assert(tt_rows(t) == 3); + expect_unchanged(t, 3, 0, 'a'); + + assert(!tt_resize(t, 10, 0)); + assert(!tt_resize(t, 0, 3)); + assert(tt_cols(t) == 10); + assert(tt_rows(t) == 3); + + assert(tt_resize(t, TERM_MAX_COLS, 3)); + assert(tt_cols(t) == TERM_MAX_COLS); + assert(tt_rows(t) == 3); + term_free(t); +} + +int main(void) +{ + test_feed_unknown_flag(); + test_feed_null_buffer(); + test_new_rejects_bad_options(); + test_new_accepts_defaults(); + test_resize_rejects_too_wide(); + return 0; +} diff --git a/tests/dcs_test.c b/tests/dcs_test.c index 0e67f78..d9455be 100644 --- a/tests/dcs_test.c +++ b/tests/dcs_test.c @@ -176,8 +176,8 @@ static void test_xtgettcap(void) { struct term *t = new_term(80, 24); assert_tcap(t, "kind", "\033[1;2B"); - assert_tcap(t, "TN", "wsap_term"); - assert_tcap(t, "name", "wsap_term"); + assert_tcap(t, "TN", "term"); + assert_tcap(t, "name", "term"); assert_tcap(t, "Co", "16777216"); assert_tcap(t, "colors", "16777216"); assert_tcap(t, "RGB", "8/8/8"); @@ -230,7 +230,7 @@ static void test_xtgettcap_multi(void) snprintf(req, sizeof(req), "\033P+q%s;%s\033\\", hk, htn); snprintf(want, sizeof(want), "\033P1+r%s=%s;%s=%s\033\\", - hk, hv, htn, "777361705f7465726d"); + hk, hv, htn, "7465726d"); response_len = 0; write_seq(t, req); expect_response(want); @@ -383,7 +383,51 @@ static void test_xtversion(void) struct term *t = new_term(80, 24); response_len = 0; write_seq(t, "\033[>0q"); - expect_response("\033P>|wsap_term\033\\"); + expect_response("\033P>|term\033\\"); + term_free(t); +} + +/* term_options.name feeds XTVERSION and the TN and name capabilities. */ +static void test_host_name(void) +{ + struct term_options opt = { + .size = { .x = 80, .y = 24 }, + .scrollback = TEST_SCROLLBACK, + .name = "myterm", + .callbacks = { .reply = respond }, + }; + struct term *t = term_new(&opt); + assert(t); + response_len = 0; + write_seq(t, "\033[>0q"); + expect_response("\033P>|myterm\033\\"); + assert_tcap(t, "TN", "myterm"); + assert_tcap(t, "name", "myterm"); + + /* A DCS +p sets the capability value. The XTVERSION name does not change. */ + response_len = 0; + write_seq(t, "\033P+p6f74686572\033\\"); + expect_no_response(); + assert_tcap(t, "TN", "other"); + response_len = 0; + write_seq(t, "\033[>0q"); + expect_response("\033P>|myterm\033\\"); + term_free(t); + + /* NULL and "" both select the default. */ + opt.name = ""; + t = term_new(&opt); + assert(t); + response_len = 0; + write_seq(t, "\033[>0q"); + expect_response("\033P>|term\033\\"); + term_free(t); + opt.name = NULL; + t = term_new(&opt); + assert(t); + response_len = 0; + write_seq(t, "\033[>0q"); + expect_response("\033P>|term\033\\"); term_free(t); } @@ -467,6 +511,7 @@ int main(void) test_synchronized_output(); test_ignored_dcs(); test_xtversion(); + test_host_name(); test_bel_terminator(); test_dcs_long_query(); test_dcs_abort_c0(); diff --git a/tests/meson.build b/tests/meson.build index 358df70..4fda075 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -30,6 +30,7 @@ term_tests = [ ['term-resize-failure-test', files('resize_failure_test.c'), [dl_dep], ['extlib']], ['dcs-test', files('dcs_test.c'), [], []], ['osc-test', files('osc_test.c'), [], []], + ['api-args-test', files('api_args_test.c'), [], []], ] test_helpers_inc = include_directories('helpers') @@ -83,7 +84,7 @@ term_amalgamation_lib = static_library( foreach entry : term_tests if entry[0] in ['term-test', 'spec-test', 'view-test', 'dcs-test', 'osc-test', 'history-text-test', 'term-input-test', 'model-test', 'model-row-test', - 'resize-property-test'] + 'resize-property-test', 'api-args-test'] test(entry[0] + '-amalgamation', executable(entry[0] + '-amalgamation', entry[1], c_args: test_c_args, @@ -117,6 +118,17 @@ if cc.has_link_argument('-Wl,--wrap=realloc') and cc.has_link_argument('-Wl,--wr suite: 'unit', ) endif + +# The project version comes in as a define, so the header macros cannot +# drift from meson.build. +version_test = executable('version-test', 'version_test.c', + c_args: test_c_args + ['-DTERM_TEST_PROJECT_VERSION="@0@"'.format(meson.project_version())], + dependencies: [term_internal_dep], +) +test('version-test', version_test, + env: test_env, + suite: 'unit', +) test('term-cross-test', bash, args: [files('cross_test.sh'), meson.project_source_root()], suite: 'portability', diff --git a/tests/model.c b/tests/model.c index 8dead6c..d212acb 100644 --- a/tests/model.c +++ b/tests/model.c @@ -72,6 +72,7 @@ enum { GROUND, ESC, CSI, STR, STR_ESC, CHARSET, HASH, DOCS }; enum { OSC, DCS, IGNORE }; struct model { + char name[64]; /* XTVERSION reply */ int cols, rows, limit; struct row **scr; struct row **hist; @@ -1290,7 +1291,7 @@ static void resize_plain(struct model *m, int cols, int rows) static bool resize(struct model *m, int cols, int rows) { - if (cols > TERM_MAX_COLS) cols = TERM_MAX_COLS; + if (cols > TERM_MAX_COLS) return false; if (cols == m->cols && rows == m->rows) return true; if (cols > MAX_TABS) return false; m->top = 0; @@ -1625,7 +1626,7 @@ static void csi_dispatch(struct model *m, unsigned char final) break; case 'q': if (m->prefix == '>' && !m->inter && (!m->nparam || !m->param[0])) { - reply(m, "\033P>|wsap_term\033\\"); + reply(m, "\033P>|%s\033\\", m->name); } else if (!m->prefix && m->inter == ' ') { int ps = m->nparam ? m->param[0] : 0; if (ps == 0) { @@ -1971,10 +1972,11 @@ static void feed_byte(struct model *m, unsigned char c) /* ---- public ---- */ -struct model *model_new(struct term_vector2i size, int history) +struct model *model_new(struct term_vector2i size, int history, const char *name) { struct model *m = calloc(1, sizeof(*m)); if (!m) abort(); + snprintf(m->name, sizeof(m->name), "%s", name && *name ? name : "term"); m->cols = size.x < 1 ? 80 : size.x; m->rows = size.y < 1 ? 24 : size.y; m->limit = history_limit(history); diff --git a/tests/model.h b/tests/model.h index 9a4316f..1707ef7 100644 --- a/tests/model.h +++ b/tests/model.h @@ -28,8 +28,9 @@ struct model_cell { struct model; -/* history is the history limit, as in term_options.scrollback. */ -struct model *model_new(struct term_vector2i size, int history); +/* history is the history limit, as in term_options.scrollback. name is + * as in term_options.name. */ +struct model *model_new(struct term_vector2i size, int history, const char *name); void model_free(struct model *m); void model_feed(struct model *m, const char *s, size_t len, unsigned flags); bool model_resize(struct model *m, struct term_vector2i size); diff --git a/tests/model_row_test.c b/tests/model_row_test.c index 7afca05..ad4ac2c 100644 --- a/tests/model_row_test.c +++ b/tests/model_row_test.c @@ -5,7 +5,7 @@ int main(void) { - struct model *m = model_new((struct term_vector2i){ .x = 10, .y = 3 }, 10); + struct model *m = model_new((struct term_vector2i){ .x = 10, .y = 3 }, 10, NULL); model_feed(m, "A\r\nB\r\nC\r\nD", strlen("A\r\nB\r\nC\r\nD"), 0); assert(model_history(m) == 1); assert(model_row(m, -1) == NULL); diff --git a/tests/model_test.c b/tests/model_test.c index 395097e..e94307c 100644 --- a/tests/model_test.c +++ b/tests/model_test.c @@ -256,7 +256,7 @@ static int run(const struct script *s, enum chunking chunks, uint64_t chunk_seed .callbacks = { .ud = &got, .reply = on_reply }, }; struct term *t = term_new(&opt); - struct model *m = model_new(s->size, s->history); + struct model *m = model_new(s->size, s->history, NULL); struct term_snapshot *snap = term_snapshot_new(); if (!t || !snap) abort(); uint64_t saved_rng = rng; diff --git a/tests/osc_test.c b/tests/osc_test.c index 41a9247..9d08157 100644 --- a/tests/osc_test.c +++ b/tests/osc_test.c @@ -104,6 +104,35 @@ static void reset_captures(void) notify_count = 0; } +/* The notification default is term_options.name. A DCS +p does not change it. */ +static void test_host_name_notify(void) +{ + struct term_options opt = { + .size = { .x = 80, .y = 24 }, + .scrollback = TEST_SCROLLBACK, + .name = "myterm", + .callbacks = { .notify = capture_notify }, + }; + struct term *t = term_new(&opt); + assert(t); + reset_captures(); + write_seq(t, "\033]9;hello\007"); + assert(notify_count == 1); + assert(!strcmp(notify_title, "myterm")); + assert(!strcmp(notify_body, "hello")); + + write_seq(t, "\033P+p6f74686572\033\\"); + reset_captures(); + write_seq(t, "\033]9;again\007"); + assert(notify_count == 1); + assert(!strcmp(notify_title, "myterm")); + + write_seq(t, "\033]777;notify;Title;Body\007"); + assert(notify_count == 2); + assert(!strcmp(notify_title, "Title")); + term_free(t); +} + static void test_osc52(void) { struct term *t = osc_test_term(); @@ -289,7 +318,7 @@ static void test_osc_misc(void) write_seq(t, "\033]9;hello there\033\\"); assert(notify_count == 1); - assert(!strcmp(notify_title, "wsap_term")); + assert(!strcmp(notify_title, "term")); assert(!strcmp(notify_body, "hello there")); write_seq(t, "\033]9;4;3;50\007"); assert(notify_count == 1); @@ -450,6 +479,7 @@ int main(void) test_osc8(); test_sgr_reset_keeps_link(); test_osc_misc(); + test_host_name_notify(); test_osc_colors(); test_osc_legacy_titles(); test_palette_and_theme(); diff --git a/tests/term_test.c b/tests/term_test.c index 9dfa082..62b827f 100644 --- a/tests/term_test.c +++ b/tests/term_test.c @@ -1441,6 +1441,55 @@ static void test_snapshot_follows_new_terminal(void) term_snapshot_free(s); } +/* The getters take a NULL snapshot and an out-of-range row like a NULL + * terminal: they return the empty value. The view keeps its address. */ +static void test_snapshot_null_and_range(void) +{ + uint32_t cps[4] = { 0xAAAA, 0xAAAA, 0xAAAA, 0xAAAA }; + assert(term_snapshot_view(NULL) == NULL); + assert(term_snapshot_row(NULL, 0) == NULL); + assert(term_snapshot_row_key(NULL, 0) == 0); + struct term_span none = term_snapshot_row_selection(NULL, 0); + assert(none.begin == 0 && none.end == 0); + assert(term_snapshot_cluster(NULL, 'a', cps, 4) == 0); + assert(term_snapshot_cluster(NULL, TERM_TEXT_CLUSTER, cps, 4) == 0); + assert(cps[0] == 0xAAAA); + + struct term *t = tt_new(4, 2); + tt_write(t, "e\xcc\x81", 3); + struct term_snapshot *s = term_snapshot_new(); + assert(term_snapshot_update(s, t)); + + const struct term_view *view = term_snapshot_view(s); + const uint32_t *palette = view->palette; + assert(term_snapshot_update(s, t)); + assert(term_snapshot_view(s) == view && view->palette == palette); + + assert(term_snapshot_row(s, -1) == NULL); + assert(term_snapshot_row(s, 2) == NULL); + assert(term_snapshot_row_key(s, -1) == 0); + assert(term_snapshot_row_key(s, 2) == 0); + struct term_span off = term_snapshot_row_selection(s, 2); + assert(off.begin == 0 && off.end == 0); + + /* A cluster cell gives its count with no buffer, and with cap 0 it + * writes nothing. */ + uint32_t text = term_snapshot_row(s, 0)[0].text; + assert(text & TERM_TEXT_CLUSTER); + assert(term_snapshot_cluster(s, text, NULL, 0) == 2); + assert(term_snapshot_cluster(s, text, cps, 0) == 2); + assert(cps[0] == 0xAAAA); + assert(term_snapshot_cluster(s, text, NULL, 4) == 2); + /* A plain code point is one code point, and cap 0 writes nothing. */ + assert(term_snapshot_cluster(s, 'a', NULL, 0) == 1); + assert(term_snapshot_cluster(s, 'a', cps, 0) == 1); + assert(cps[0] == 0xAAAA); + assert(term_snapshot_cluster(s, 'a', cps, 4) == 1 && cps[0] == 'a'); + + term_snapshot_free(s); + term_free(t); +} + static void test_string_payloads(void) { static const char *prefixes[] = {"\033]0;", "\033]7;file://host/", "\033P"}; @@ -1486,6 +1535,32 @@ static void test_string_payloads(void) } } +/* term_new() refuses a name that a DCS reply could echo unsafely. */ +static void test_host_name_rejected(void) +{ + static const char *const bad[] = { + "\033x", "a\tb", "\177", "\300", "a\nb", + }; + char longest[65]; + memset(longest, 'a', 64); + longest[64] = 0; + char fits[64]; + memset(fits, 'b', 63); + fits[63] = 0; + + for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) { + struct term_options opt = { .size = { .x = 10, .y = 3 }, .name = bad[i] }; + assert(!term_new(&opt)); + } + struct term_options opt = { .size = { .x = 10, .y = 3 }, .name = longest }; + assert(!term_new(&opt)); + + opt.name = fits; + struct term *t = term_new(&opt); + assert(t); + term_free(t); +} + static void test_newline_policy_survives_reset(void) { static const char *const resets[] = { "\033c", "\033[!p", "\033[20l" }; @@ -1862,6 +1937,27 @@ static void test_concurrent_feed_and_snapshot(void) term_free(t); } +static void test_reserved_options_must_be_zero(void) +{ + struct term_options opt = { .size = { .x = 10, .y = 2 } }; + struct term *t = term_new(&opt); + assert(t != NULL); + term_free(t); + + size_t words = sizeof(opt.reserved) / sizeof(opt.reserved[0]); + for (size_t i = 0; i < words; i++) { + opt.reserved[i] = 1; + assert(term_new(&opt) == NULL); + opt.reserved[i] = 0; + } + opt.reserved[words - 1] = 0x80000000u; + assert(term_new(&opt) == NULL); + opt.reserved[words - 1] = 0; + t = term_new(&opt); + assert(t != NULL); + term_free(t); +} + struct lock_probe { struct term *t; _Atomic bool entered; @@ -1905,6 +2001,7 @@ static void test_lock_reentrant_across_threads(void) int main(void) { + test_reserved_options_must_be_zero(); test_concurrent_feed_and_snapshot(); test_lock_reentrant_across_threads(); test_grow_past_reservation(); @@ -1931,7 +2028,9 @@ int main(void) test_alt_shrink_then_flood(); test_alt_erase_scrollback(); test_snapshot_follows_new_terminal(); + test_snapshot_null_and_range(); test_bulk_rows_survive_edits(); + test_host_name_rejected(); assert(!strcmp(UNICODE_DATA_VERSION, "18.0.0")); assert(!strcmp(term_unicode_version(), UNICODE_DATA_VERSION)); diff --git a/tests/version_test.c b/tests/version_test.c new file mode 100644 index 0000000..0eb889e --- /dev/null +++ b/tests/version_test.c @@ -0,0 +1,27 @@ +#include "term/term.h" + +#include <assert.h> +#include <stdio.h> +#include <string.h> + +/* The project version from meson.build. The build defines it. */ +#ifndef TERM_TEST_PROJECT_VERSION +#error "define TERM_TEST_PROJECT_VERSION as the project version, such as \"0.1.0\"" +#endif + +int main(void) +{ + char text[32]; + snprintf(text, sizeof(text), "%d.%d.%d", + TERM_VERSION_MAJOR, TERM_VERSION_MINOR, TERM_VERSION_PATCH); + assert(!strcmp(text, TERM_TEST_PROJECT_VERSION)); + + int major, minor, patch; + assert(sscanf(TERM_TEST_PROJECT_VERSION, "%d.%d.%d", &major, &minor, &patch) == 3); + assert(TERM_VERSION_CODE == TERM_VERSION(major, minor, patch)); + assert(term_version() == TERM_VERSION_CODE); + + assert(TERM_VERSION(0, 2, 0) > TERM_VERSION(0, 1, 255)); + assert(TERM_VERSION(1, 0, 0) > TERM_VERSION(0, 255, 255)); + return 0; +} |
