diff options
Diffstat (limited to 'tests/t_batch.c')
| -rw-r--r-- | tests/t_batch.c | 346 |
1 files changed, 346 insertions, 0 deletions
diff --git a/tests/t_batch.c b/tests/t_batch.c new file mode 100644 index 0000000..5a7ac1e --- /dev/null +++ b/tests/t_batch.c | |||
| @@ -0,0 +1,346 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. */ | ||
| 3 | |||
| 4 | /* System */ | ||
| 5 | #include <fcntl.h> | ||
| 6 | #include <poll.h> | ||
| 7 | #include <stdlib.h> | ||
| 8 | #include <string.h> | ||
| 9 | #include <sys/socket.h> | ||
| 10 | #include <unistd.h> | ||
| 11 | |||
| 12 | /* Opentracker */ | ||
| 13 | #include "ot_batch.h" | ||
| 14 | #include "test.h" | ||
| 15 | |||
| 16 | /* The batch writes to a real socket pair, because what is worth pinning down | ||
| 17 | is what it does when the kernel takes only part of what it was offered. */ | ||
| 18 | |||
| 19 | static char *dup_of(const char *text) { | ||
| 20 | size_t len = strlen(text); | ||
| 21 | char *copy = malloc(len); | ||
| 22 | |||
| 23 | memcpy(copy, text, len); | ||
| 24 | return copy; | ||
| 25 | } | ||
| 26 | |||
| 27 | static void drain(int s, char *into, size_t len) { | ||
| 28 | size_t got = 0; | ||
| 29 | |||
| 30 | while (got < len) { | ||
| 31 | ssize_t r = read(s, into + got, len - got); | ||
| 32 | |||
| 33 | if (r <= 0) | ||
| 34 | break; | ||
| 35 | got += (size_t)r; | ||
| 36 | } | ||
| 37 | into[got] = 0; | ||
| 38 | } | ||
| 39 | |||
| 40 | static void case_queue_accounting(void) { | ||
| 41 | ot_batch batch; | ||
| 42 | |||
| 43 | memset(&batch, 0, sizeof(batch)); | ||
| 44 | |||
| 45 | test_case("a zeroed batch is empty and sends nothing"); | ||
| 46 | CHECK_INT(batch_pending(&batch), 0); | ||
| 47 | CHECK_INT(batch_send(-1, &batch), BATCH_DONE); | ||
| 48 | |||
| 49 | test_case("anything queued makes it pending"); | ||
| 50 | CHECK_INT(batch_add(&batch, dup_of("hello "), 6), 0); | ||
| 51 | CHECK_INT(batch_add_copy(&batch, "world", 5), 0); | ||
| 52 | CHECK_INT(batch_pending(&batch), 1); | ||
| 53 | CHECK_INT(batch.count, 2); | ||
| 54 | |||
| 55 | batch_reset(&batch); | ||
| 56 | test_case("and a reset empties it"); | ||
| 57 | CHECK_INT(batch_pending(&batch), 0); | ||
| 58 | CHECK_PTR(batch.entries, NULL); | ||
| 59 | } | ||
| 60 | |||
| 61 | static void case_send_in_one_go(void) { | ||
| 62 | ot_batch batch; | ||
| 63 | int sv[2]; | ||
| 64 | char got[32]; | ||
| 65 | |||
| 66 | memset(&batch, 0, sizeof(batch)); | ||
| 67 | CHECK_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); | ||
| 68 | |||
| 69 | test_case("several buffers arrive as one stream, in order"); | ||
| 70 | CHECK_INT(batch_add(&batch, dup_of("opentracker"), 11), 0); | ||
| 71 | CHECK_INT(batch_add_copy(&batch, " says ", 6), 0); | ||
| 72 | CHECK_INT(batch_add(&batch, dup_of("hello"), 5), 0); | ||
| 73 | CHECK_INT(batch_send(sv[0], &batch), 22); | ||
| 74 | CHECK_INT(batch_pending(&batch), 0); | ||
| 75 | |||
| 76 | drain(sv[1], got, 22); | ||
| 77 | CHECK_STR(got, "opentracker says hello"); | ||
| 78 | |||
| 79 | test_case("and an empty batch reports it has nothing left"); | ||
| 80 | CHECK_INT(batch_send(sv[0], &batch), BATCH_DONE); | ||
| 81 | |||
| 82 | batch_reset(&batch); | ||
| 83 | close(sv[0]); | ||
| 84 | close(sv[1]); | ||
| 85 | } | ||
| 86 | |||
| 87 | /* Send everything the batch holds, reading the other end as we go so the | ||
| 88 | socket never stays full, and return what arrived. */ | ||
| 89 | static size_t pump(int writer, int reader, ot_batch *batch, char *into, size_t room) { | ||
| 90 | size_t got = 0; | ||
| 91 | |||
| 92 | while (batch_pending(batch) || got < room) { | ||
| 93 | ssize_t r; | ||
| 94 | |||
| 95 | if (batch_pending(batch)) { | ||
| 96 | ssize_t res = batch_send(writer, batch); | ||
| 97 | |||
| 98 | if (res == BATCH_FAILED) | ||
| 99 | break; | ||
| 100 | if (res > 0) | ||
| 101 | continue; | ||
| 102 | } | ||
| 103 | |||
| 104 | r = read(reader, into + got, room - got); | ||
| 105 | if (r <= 0) | ||
| 106 | break; | ||
| 107 | got += (size_t)r; | ||
| 108 | } | ||
| 109 | return got; | ||
| 110 | } | ||
| 111 | |||
| 112 | static void case_partial_write_resumes(void) { | ||
| 113 | const size_t size = 200000; | ||
| 114 | ot_batch batch; | ||
| 115 | int sv[2], sndbuf = 4096; | ||
| 116 | char *sent, *got; | ||
| 117 | size_t i, arrived; | ||
| 118 | |||
| 119 | memset(&batch, 0, sizeof(batch)); | ||
| 120 | CHECK_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); | ||
| 121 | setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)); | ||
| 122 | fcntl(sv[0], F_SETFL, fcntl(sv[0], F_GETFL, 0) | O_NONBLOCK); | ||
| 123 | |||
| 124 | sent = malloc(size); | ||
| 125 | got = malloc(size); | ||
| 126 | for (i = 0; i < size; ++i) | ||
| 127 | sent[i] = (char)(i % 251); | ||
| 128 | |||
| 129 | test_case("a buffer the kernel takes in pieces arrives whole and in order"); | ||
| 130 | CHECK_INT(batch_add(&batch, memcpy(malloc(size), sent, size), size), 0); | ||
| 131 | arrived = pump(sv[0], sv[1], &batch, got, size); | ||
| 132 | CHECK_INT(arrived, size); | ||
| 133 | CHECK_INT(batch_pending(&batch), 0); | ||
| 134 | CHECK_MEM(got, sent, size); | ||
| 135 | |||
| 136 | free(sent); | ||
| 137 | free(got); | ||
| 138 | batch_reset(&batch); | ||
| 139 | close(sv[0]); | ||
| 140 | close(sv[1]); | ||
| 141 | } | ||
| 142 | |||
| 143 | /* writev refuses more than IOV_MAX entries at once, so a long batch has to be | ||
| 144 | sent across several calls. */ | ||
| 145 | static void case_more_entries_than_one_writev(void) { | ||
| 146 | const size_t entries = 3000, each = 4; | ||
| 147 | ot_batch batch; | ||
| 148 | int sv[2], sndbuf = 4096; | ||
| 149 | char *sent, *got; | ||
| 150 | size_t i, arrived, refused = 0; | ||
| 151 | |||
| 152 | memset(&batch, 0, sizeof(batch)); | ||
| 153 | CHECK_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); | ||
| 154 | setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)); | ||
| 155 | fcntl(sv[0], F_SETFL, fcntl(sv[0], F_GETFL, 0) | O_NONBLOCK); | ||
| 156 | |||
| 157 | sent = malloc(entries * each); | ||
| 158 | got = malloc(entries * each); | ||
| 159 | |||
| 160 | test_case("a batch of more entries than writev accepts still arrives whole"); | ||
| 161 | for (i = 0; i < entries; ++i) { | ||
| 162 | char piece[4]; | ||
| 163 | |||
| 164 | piece[0] = (char)(i), piece[1] = (char)(i >> 8), piece[2] = 'x', piece[3] = 'y'; | ||
| 165 | memcpy(sent + i * each, piece, each); | ||
| 166 | if (batch_add(&batch, memcpy(malloc(each), piece, each), each)) | ||
| 167 | ++refused; | ||
| 168 | } | ||
| 169 | CHECK_INT(refused, 0); | ||
| 170 | CHECK_INT(batch.count, entries); | ||
| 171 | |||
| 172 | arrived = pump(sv[0], sv[1], &batch, got, entries * each); | ||
| 173 | CHECK_INT(arrived, entries * each); | ||
| 174 | CHECK_MEM(got, sent, entries * each); | ||
| 175 | |||
| 176 | free(sent); | ||
| 177 | free(got); | ||
| 178 | batch_reset(&batch); | ||
| 179 | close(sv[0]); | ||
| 180 | close(sv[1]); | ||
| 181 | } | ||
| 182 | |||
| 183 | static void case_reset_frees_the_unsent(void) { | ||
| 184 | ot_batch batch; | ||
| 185 | int sv[2]; | ||
| 186 | |||
| 187 | memset(&batch, 0, sizeof(batch)); | ||
| 188 | CHECK_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); | ||
| 189 | |||
| 190 | test_case("a batch torn down mid flight keeps no bytes outstanding"); | ||
| 191 | CHECK_INT(batch_add(&batch, dup_of("first"), 5), 0); | ||
| 192 | CHECK_INT(batch_add(&batch, dup_of("second"), 6), 0); | ||
| 193 | CHECK_INT(batch_send(sv[0], &batch), 11); | ||
| 194 | CHECK_INT(batch_add(&batch, dup_of("never sent"), 10), 0); | ||
| 195 | CHECK_INT(batch_pending(&batch), 1); | ||
| 196 | |||
| 197 | /* Whether the unsent buffer is freed is for the sanitizer to say */ | ||
| 198 | batch_reset(&batch); | ||
| 199 | CHECK_INT(batch_pending(&batch), 0); | ||
| 200 | CHECK_INT(batch.count, 0); | ||
| 201 | |||
| 202 | close(sv[0]); | ||
| 203 | close(sv[1]); | ||
| 204 | } | ||
| 205 | |||
| 206 | static void case_closed_socket_is_fatal(void) { | ||
| 207 | ot_batch batch; | ||
| 208 | int sv[2]; | ||
| 209 | |||
| 210 | memset(&batch, 0, sizeof(batch)); | ||
| 211 | CHECK_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); | ||
| 212 | close(sv[1]); | ||
| 213 | |||
| 214 | test_case("writing to a socket whose peer is gone reports a dead connection"); | ||
| 215 | CHECK_INT(batch_add(&batch, dup_of("nobody is listening"), 19), 0); | ||
| 216 | CHECK_INT(batch_send(sv[0], &batch), BATCH_FAILED); | ||
| 217 | |||
| 218 | batch_reset(&batch); | ||
| 219 | close(sv[0]); | ||
| 220 | } | ||
| 221 | |||
| 222 | static void case_vector_is_taken_whole(void) { | ||
| 223 | ot_batch batch; | ||
| 224 | struct iovec *vector = malloc(3 * sizeof(struct iovec)); | ||
| 225 | int sv[2]; | ||
| 226 | char got[32]; | ||
| 227 | |||
| 228 | memset(&batch, 0, sizeof(batch)); | ||
| 229 | CHECK_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); | ||
| 230 | |||
| 231 | test_case("a whole vector is queued in one go, in order"); | ||
| 232 | vector[0].iov_base = dup_of("one "), vector[0].iov_len = 4; | ||
| 233 | vector[1].iov_base = dup_of("two "), vector[1].iov_len = 4; | ||
| 234 | vector[2].iov_base = dup_of("three"), vector[2].iov_len = 5; | ||
| 235 | CHECK_INT(batch_add_vector(&batch, vector, 3), 0); | ||
| 236 | free(vector); | ||
| 237 | CHECK_INT(batch.count, 3); | ||
| 238 | CHECK_INT(batch_send(sv[0], &batch), 13); | ||
| 239 | |||
| 240 | drain(sv[1], got, 13); | ||
| 241 | CHECK_STR(got, "one two three"); | ||
| 242 | |||
| 243 | batch_reset(&batch); | ||
| 244 | close(sv[0]); | ||
| 245 | close(sv[1]); | ||
| 246 | } | ||
| 247 | |||
| 248 | /* The entry array starts small and doubles, so crossing that boundary one | ||
| 249 | buffer at a time is worth its own case. */ | ||
| 250 | static void case_growing_one_entry_at_a_time(void) { | ||
| 251 | const size_t entries = 100; | ||
| 252 | ot_batch batch; | ||
| 253 | int sv[2]; | ||
| 254 | char sent[400], got[401]; | ||
| 255 | size_t i, refused = 0; | ||
| 256 | |||
| 257 | memset(&batch, 0, sizeof(batch)); | ||
| 258 | CHECK_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); | ||
| 259 | |||
| 260 | test_case("a batch grown one buffer at a time keeps them all, in order"); | ||
| 261 | for (i = 0; i < entries; ++i) { | ||
| 262 | char piece[4]; | ||
| 263 | |||
| 264 | piece[0] = (char)('a' + i % 26), piece[1] = (char)('0' + i % 10); | ||
| 265 | piece[2] = (char)('A' + i % 26), piece[3] = '.'; | ||
| 266 | memcpy(sent + i * 4, piece, 4); | ||
| 267 | if (batch_add(&batch, memcpy(malloc(4), piece, 4), 4)) | ||
| 268 | ++refused; | ||
| 269 | } | ||
| 270 | CHECK_INT(refused, 0); | ||
| 271 | CHECK_INT(batch.count, entries); | ||
| 272 | CHECK_INT(batch_send(sv[0], &batch), (ssize_t)(entries * 4)); | ||
| 273 | |||
| 274 | drain(sv[1], got, entries * 4); | ||
| 275 | CHECK_MEM(got, sent, entries * 4); | ||
| 276 | |||
| 277 | batch_reset(&batch); | ||
| 278 | close(sv[0]); | ||
| 279 | close(sv[1]); | ||
| 280 | } | ||
| 281 | |||
| 282 | /* Appending while earlier entries are still on their way is what a chunked | ||
| 283 | transfer does. Rather than depend on when a kernel decides to block, this | ||
| 284 | takes entries off the front the way a completed send would and checks that | ||
| 285 | the array reuses the space instead of growing. */ | ||
| 286 | static void case_append_while_sending(void) { | ||
| 287 | const size_t per_round = 12, each = 8; | ||
| 288 | ot_batch batch; | ||
| 289 | int sv[2]; | ||
| 290 | char expected[16], got[17]; | ||
| 291 | size_t i, round, refused = 0, space_after_first = 0; | ||
| 292 | |||
| 293 | memset(&batch, 0, sizeof(batch)); | ||
| 294 | CHECK_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); | ||
| 295 | |||
| 296 | test_case("an array whose front has gone out is reused, not grown"); | ||
| 297 | for (round = 0; round < 8; ++round) { | ||
| 298 | for (i = 0; i < per_round; ++i) { | ||
| 299 | /* Distinct per entry, so a move from the wrong index shows up */ | ||
| 300 | char *piece = malloc(each); | ||
| 301 | |||
| 302 | memset(piece, (int)('a' + (round * per_round + i) % 26), each); | ||
| 303 | piece[0] = (char)('0' + i); | ||
| 304 | piece[1] = (char)('0' + round); | ||
| 305 | if (batch_add(&batch, piece, each)) | ||
| 306 | ++refused; | ||
| 307 | } | ||
| 308 | |||
| 309 | /* All but the last entry have left and been freed */ | ||
| 310 | while (batch.count - batch.first > 1) | ||
| 311 | free(batch.entries[batch.first++].iov_base); | ||
| 312 | |||
| 313 | if (!round) | ||
| 314 | space_after_first = batch.space; | ||
| 315 | } | ||
| 316 | CHECK_INT(refused, 0); | ||
| 317 | CHECK_INT(batch.space, space_after_first); | ||
| 318 | |||
| 319 | test_case("and what is still queued survives the move, in order"); | ||
| 320 | CHECK_INT(batch.count - batch.first, 1); | ||
| 321 | CHECK_INT(batch_add(&batch, memset(malloc(each), 'Z', each), each), 0); | ||
| 322 | CHECK_INT(batch_send(sv[0], &batch), (ssize_t)(2 * each)); | ||
| 323 | |||
| 324 | memset(expected, (int)('a' + (7 * per_round + per_round - 1) % 26), each); | ||
| 325 | expected[0] = (char)('0' + per_round - 1); | ||
| 326 | expected[1] = '7'; | ||
| 327 | memset(expected + each, 'Z', each); | ||
| 328 | drain(sv[1], got, 2 * each); | ||
| 329 | CHECK_MEM(got, expected, 2 * each); | ||
| 330 | |||
| 331 | batch_reset(&batch); | ||
| 332 | close(sv[0]); | ||
| 333 | close(sv[1]); | ||
| 334 | } | ||
| 335 | |||
| 336 | void test_batch(void) { | ||
| 337 | case_queue_accounting(); | ||
| 338 | case_send_in_one_go(); | ||
| 339 | case_vector_is_taken_whole(); | ||
| 340 | case_growing_one_entry_at_a_time(); | ||
| 341 | case_append_while_sending(); | ||
| 342 | case_partial_write_resumes(); | ||
| 343 | case_more_entries_than_one_writev(); | ||
| 344 | case_reset_frees_the_unsent(); | ||
| 345 | case_closed_socket_is_fatal(); | ||
| 346 | } | ||
