Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <assert.h> |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <limits.h> |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 22 | #include <unistd.h> |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 23 | |
Alexander Hansen | e3f1aa1 | 2024-07-10 11:57:17 +0200 | [diff] [blame] | 24 | #include "util.h" |
| 25 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 26 | static ssize_t __read(int fd, void *buf, size_t len); |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 27 | #define read __read |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 28 | #include "config.c" |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 29 | #include "console-socket.c" |
| 30 | #define main __main |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 31 | #include "console-client.c" |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 32 | #undef read |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 33 | #undef main |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 34 | |
| 35 | struct test { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 36 | enum esc_type esc_type; |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 37 | union { |
| 38 | struct ssh_esc_state ssh; |
| 39 | struct str_esc_state str; |
| 40 | } esc_state; |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 41 | const char *in[4]; |
| 42 | size_t n_in; |
| 43 | const char *exp_out; |
| 44 | int exp_rc; |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct test_ctx { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 48 | struct console_client client; |
| 49 | struct test *test; |
| 50 | uint8_t out[4096]; |
| 51 | size_t cur_in; |
| 52 | size_t cur_out; |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 53 | }; |
| 54 | |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 55 | struct test tests[] = { |
| 56 | { |
| 57 | /* no escape code */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 58 | .esc_type = ESC_TYPE_SSH, |
| 59 | .in = { "a" }, |
| 60 | .n_in = 1, |
| 61 | .exp_out = "a", |
| 62 | .exp_rc = PROCESS_EXIT, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 63 | }, |
| 64 | { |
| 65 | /* no escape code, multiple reads */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 66 | .esc_type = ESC_TYPE_SSH, |
| 67 | .in = { "a", "b" }, |
| 68 | .n_in = 2, |
| 69 | .exp_out = "ab", |
| 70 | .exp_rc = PROCESS_EXIT, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 71 | }, |
| 72 | { |
| 73 | /* ssh escape in one read */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 74 | .esc_type = ESC_TYPE_SSH, |
| 75 | .in = { "a\r~." }, |
| 76 | .n_in = 1, |
| 77 | .exp_out = "a\r", |
| 78 | .exp_rc = PROCESS_ESC, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 79 | }, |
| 80 | { |
| 81 | /* ssh escape, partial ~ is not output. */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 82 | .esc_type = ESC_TYPE_SSH, |
| 83 | .in = { "a\r~" }, |
| 84 | .n_in = 1, |
| 85 | .exp_out = "a\r", |
| 86 | .exp_rc = PROCESS_EXIT, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 87 | }, |
| 88 | { |
| 89 | /* ssh escape split into individual reads */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 90 | .esc_type = ESC_TYPE_SSH, |
| 91 | .in = { "a", "\r", "~", "." }, |
| 92 | .n_in = 4, |
| 93 | .exp_out = "a\r", |
| 94 | .exp_rc = PROCESS_ESC, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 95 | }, |
| 96 | { |
| 97 | /* ssh escape, escaped. */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 98 | .esc_type = ESC_TYPE_SSH, |
| 99 | .in = { "a\r~~." }, |
| 100 | .n_in = 1, |
| 101 | .exp_out = "a\r~.", |
| 102 | .exp_rc = PROCESS_EXIT, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 103 | }, |
| 104 | { |
| 105 | /* ssh escape, escaped ~, and not completed. */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 106 | .esc_type = ESC_TYPE_SSH, |
| 107 | .in = { "a\r~~" }, |
| 108 | .n_in = 1, |
| 109 | .exp_out = "a\r~", |
| 110 | .exp_rc = PROCESS_EXIT, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 111 | }, |
| 112 | { |
| 113 | /* str escape, no match */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 114 | .esc_type = ESC_TYPE_STR, |
| 115 | .esc_state = { .str = { .str = (const uint8_t *)"c" } }, |
| 116 | .in = { "ab" }, |
| 117 | .n_in = 1, |
| 118 | .exp_out = "ab", |
| 119 | .exp_rc = PROCESS_EXIT, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 120 | }, |
| 121 | { |
| 122 | /* str escape, one byte, as one read */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 123 | .esc_type = ESC_TYPE_STR, |
| 124 | .esc_state = { .str = { .str = (const uint8_t *)"b" } }, |
| 125 | .in = { "abc" }, |
| 126 | .n_in = 1, |
| 127 | .exp_out = "ab", |
| 128 | .exp_rc = PROCESS_ESC, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 129 | }, |
| 130 | { |
| 131 | /* str escape, multiple bytes, as one read */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 132 | .esc_type = ESC_TYPE_STR, |
| 133 | .esc_state = { .str = { .str = (const uint8_t *)"bc" } }, |
| 134 | .in = { "abcd" }, |
| 135 | .n_in = 1, |
| 136 | .exp_out = "abc", |
| 137 | .exp_rc = PROCESS_ESC, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 138 | }, |
| 139 | { |
| 140 | /* str escape, multiple bytes, split over reads */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 141 | .esc_type = ESC_TYPE_STR, |
| 142 | .esc_state = { .str = { .str = (const uint8_t *)"bc" } }, |
| 143 | .in = { "ab", "cd" }, |
| 144 | .n_in = 2, |
| 145 | .exp_out = "abc", |
| 146 | .exp_rc = PROCESS_ESC, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 147 | }, |
| 148 | { |
| 149 | /* str escape, not matched due to intermediate data */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 150 | .esc_type = ESC_TYPE_STR, |
| 151 | .esc_state = { .str = { .str = (const uint8_t *)"ab" } }, |
| 152 | .in = { "acb" }, |
| 153 | .n_in = 1, |
| 154 | .exp_out = "acb", |
| 155 | .exp_rc = PROCESS_EXIT, |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 156 | }, |
| 157 | }; |
| 158 | |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 159 | struct test_ctx ctxs[ARRAY_SIZE(tests)]; |
| 160 | |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 161 | int write_buf_to_fd(int fd, const uint8_t *buf, size_t len) |
| 162 | { |
| 163 | struct test_ctx *ctx = &ctxs[fd]; |
| 164 | |
| 165 | assert(ctx->cur_out + len <= sizeof(ctx->out)); |
| 166 | memcpy(ctx->out + ctx->cur_out, buf, len); |
| 167 | ctx->cur_out += len; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 172 | static ssize_t __read(int fd, void *buf, size_t len) |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 173 | { |
| 174 | struct test_ctx *ctx = &ctxs[fd]; |
| 175 | const char *inbuf; |
| 176 | size_t inlen; |
| 177 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 178 | if (len > SSIZE_MAX) { |
| 179 | errno = EINVAL; |
| 180 | return -1; |
| 181 | } |
| 182 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 183 | if (ctx->cur_in >= ctx->test->n_in) { |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 184 | return 0; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 185 | } |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 186 | |
| 187 | inbuf = ctx->test->in[ctx->cur_in]; |
| 188 | inlen = strlen(inbuf); |
| 189 | assert(inlen <= len); |
| 190 | memcpy(buf, inbuf, inlen); |
| 191 | ctx->cur_in++; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 192 | return (ssize_t)inlen; |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 193 | } |
| 194 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 195 | void run_one_test(size_t idx, struct test *test, struct test_ctx *ctx) |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 196 | { |
| 197 | size_t exp_out_len; |
| 198 | int rc; |
| 199 | |
| 200 | /* we store the index into the context array as a FD, so we |
| 201 | * can refer to it through the read & write callbacks. |
| 202 | */ |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 203 | assert(idx < INT_MAX); |
| 204 | ctx->client.console_sd = (int)idx; |
| 205 | ctx->client.fd_in = (int)idx; |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 206 | ctx->client.esc_type = test->esc_type; |
| 207 | memcpy(&ctx->client.esc_state, &test->esc_state, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 208 | sizeof(test->esc_state)); |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 209 | ctx->test = test; |
| 210 | |
| 211 | for (;;) { |
| 212 | rc = process_tty(&ctx->client); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 213 | if (rc != PROCESS_OK) { |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 214 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 215 | } |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | exp_out_len = strlen(test->exp_out); |
| 219 | |
| 220 | #ifdef DEBUG |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 221 | printf("got: rc %d %s(%d), exp: rc %d %s(%ld)\n", rc, ctx->out, |
| 222 | ctx->cur_out, test->exp_rc, test->exp_out, exp_out_len); |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 223 | fflush(stdout); |
| 224 | #endif |
| 225 | assert(rc == test->exp_rc); |
| 226 | assert(exp_out_len == ctx->cur_out); |
| 227 | assert(!memcmp(ctx->out, test->exp_out, exp_out_len)); |
| 228 | } |
| 229 | |
| 230 | int main(void) |
| 231 | { |
Andrew Jeffery | 8f548f6 | 2023-04-18 11:48:49 +0930 | [diff] [blame] | 232 | size_t i; |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 233 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 234 | for (i = 0; i < ARRAY_SIZE(tests); i++) { |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 235 | run_one_test(i, &tests[i], &ctxs[i]); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 236 | } |
Jeremy Kerr | c5ce2cb | 2019-07-26 09:21:35 +0800 | [diff] [blame] | 237 | |
| 238 | return EXIT_SUCCESS; |
| 239 | } |