summaryrefslogtreecommitdiff
path: root/tests/resize_failure_test.c
blob: fbfa38d8ccd454bc418d47144c32ecb6ff5c03c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#define _GNU_SOURCE
#include "term_api.h"

#include <assert.h>
#include <dlfcn.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef __has_feature
#define __has_feature(x) 0
#endif

#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
#define SANITIZED 1
#elif __has_feature(address_sanitizer) || __has_feature(memory_sanitizer) || \
      __has_feature(thread_sanitizer)
#define SANITIZED 1
#endif

#ifndef SANITIZED
static bool fail_next_calloc;
static bool fail_reallocs;

void *calloc(size_t n, size_t size)
{
    static void *(*real_calloc)(size_t, size_t);
    if (!real_calloc)
        real_calloc = (void *(*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc");
    if (fail_next_calloc) {
        fail_next_calloc = false;
        return NULL;
    }
    return real_calloc(n, size);
}

void *realloc(void *p, size_t size)
{
    static void *(*real_realloc)(void *, size_t);
    if (!real_realloc)
        real_realloc = (void *(*)(void *, size_t))dlsym(RTLD_NEXT, "realloc");
    if (fail_reallocs)
        return NULL;
    return real_realloc(p, size);
}
#endif

int main(void)
{
#ifdef SANITIZED
    puts("resize failure test skipped under sanitizer");
    return 0;
#else
    struct term *term = tt_new(10, 2);
    assert(term);
    tt_write(term, "abc", 3);

    fail_next_calloc = true;
    bool resized = tt_resize(term, 20, 2);
    /* The resize must reach the failing allocation. */
    assert(!fail_next_calloc);

    /* A failed resize must leave the old terminal geometry usable as a unit. */
    assert(!resized);
    assert(tt_cols(term) == 10);
    assert(tt_rows(term) == 2);
    assert(tt_cell(term, 0, 0)->text == 'a');
    assert(tt_cell(term, 2, 0)->text == 'c');

    tt_write(term, "\r12345678901", 12);
    assert(tt_cursor_x(term) == 1);
    assert(tt_cursor_y(term) == 1);
    assert(tt_cell(term, 0, 1)->text == '1');

    /* The failed lazy allocation must not prevent a later resize retry. */
    resized = tt_resize(term, 20, 2);
    assert(resized);
    assert(tt_cols(term) == 20);
    assert(tt_rows(term) == 2);
    assert(tt_cell(term, 0, 0)->text == '1');
    term_free(term);

    term = tt_new(10, 3);
    assert(term);
    const char margins[] = "\033[?69h\033[1;5s\033[H";
    tt_write(term, margins, sizeof(margins) - 1);
    fail_next_calloc = true;
    resized = tt_resize(term, 20, 3);
    assert(!fail_next_calloc);

    /* A failed resize keeps the margins that fit the old width. */
    assert(!resized);
    assert(tt_cols(term) == 10);
    tt_write(term, "abcdefg", 7);
    assert(tt_cell(term, 4, 0)->text == 'e');
    assert(tt_cell(term, 5, 0)->text != 'f');
    assert(tt_cell(term, 0, 1)->text == 'f');
    term_free(term);

    term = tt_new(10, 2);
    assert(term);
    tt_write(term, "normal", 6);
    fail_next_calloc = true;
    tt_write(term, "\033[?1049h", 8);
    assert(!fail_next_calloc);

    /* A failed alternate-screen allocation keeps the normal screen active. */
    assert(!term_modes(term).alternate);
    assert(tt_cell(term, 0, 0)->text == 'n');
    assert(tt_cell(term, 5, 0)->text == 'l');
    tt_write(term, "\033[?1049l", 8);
    assert(tt_cell(term, 0, 0)->text == 'n');
    term_free(term);

    /* A reflow that cannot grow its row buffers fails before it changes the
     * history, so the old geometry and text survive the failed resize. The
     * first resize sizes the buffers, so only the reflow's growth fails. */
    term = tt_new(20, 40);
    assert(term);
    for (int i = 0; i < 40; i++) {
        char line[32];
        int n = snprintf(line, sizeof line, "row %02d 0123456789\r\n", i);
        tt_write(term, line, (size_t)n);
    }
    resized = tt_resize(term, 21, 40);
    assert(resized);
    int history = tt_history(term);
    char *before = tt_history_text(term, 1000);
    assert(before);

    fail_reallocs = true;
    resized = tt_resize(term, 10, 40);
    fail_reallocs = false;
    assert(!resized);
    assert(tt_cols(term) == 21);
    assert(tt_rows(term) == 40);
    assert(tt_history(term) == history);
    char *after = tt_history_text(term, 1000);
    assert(after && !strcmp(before, after));
    free(before);
    free(after);

    resized = tt_resize(term, 10, 40);
    assert(resized);
    assert(tt_cols(term) == 10);
    term_free(term);

    /* A failed same-width height shrink must not scroll rows into history or
     * move the cursor. The same resize must succeed when allocation recovers. */
    term = tt_new(8, 4);
    assert(term);
    const char four_rows[] = "A\r\nB\r\nC\r\nD";
    tt_write(term, four_rows, sizeof four_rows - 1);
    assert(tt_cursor_y(term) == 3);
    assert(tt_history(term) == 0);

    fail_next_calloc = true;
    resized = tt_resize(term, 8, 2);
    assert(!fail_next_calloc);
    assert(!resized);
    assert(tt_cols(term) == 8 && tt_rows(term) == 4);
    assert(tt_cell(term, 0, 0)->text == 'A');
    assert(tt_cell(term, 0, 1)->text == 'B');
    assert(tt_cell(term, 0, 2)->text == 'C');
    assert(tt_cell(term, 0, 3)->text == 'D');
    assert(tt_cursor_y(term) == 3);
    assert(tt_history(term) == 0);

    resized = tt_resize(term, 8, 2);
    assert(resized);
    assert(tt_cols(term) == 8 && tt_rows(term) == 2);
    assert(tt_cell(term, 0, 0)->text == 'C');
    assert(tt_cell(term, 0, 1)->text == 'D');
    assert(tt_cursor_y(term) == 1);
    assert(tt_history(term) == 2);
    term_free(term);

    /* The alternate screen uses the same allocation-before-scroll path. */
    term = tt_new(8, 4);
    assert(term);
    tt_write(term, "\033[?1049h", 8);
    tt_write(term, four_rows, sizeof four_rows - 1);
    assert(term_modes(term).alternate);
    assert(tt_cursor_y(term) == 3);
    fail_next_calloc = true;
    resized = tt_resize(term, 8, 2);
    assert(!fail_next_calloc);
    assert(!resized);
    assert(tt_rows(term) == 4 && tt_cursor_y(term) == 3);
    assert(tt_cell(term, 0, 0)->text == 'A');
    assert(tt_cell(term, 0, 3)->text == 'D');
    resized = tt_resize(term, 8, 2);
    assert(resized);
    assert(tt_rows(term) == 2 && tt_cursor_y(term) == 1);
    assert(tt_cell(term, 0, 0)->text == 'C');
    assert(tt_cell(term, 0, 1)->text == 'D');
    term_free(term);

    puts("lazy screen allocation failures preserve observable terminal state");
    return 0;
#endif
}