Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * vsock test utilities
4 *
5 * Copyright (C) 2017 Red Hat, Inc.
6 *
7 * Author: Stefan Hajnoczi <stefanha@redhat.com>
8 */
9
10#include <errno.h>
11#include <stdio.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <signal.h>
15#include <unistd.h>
16#include <assert.h>
17#include <sys/epoll.h>
18
19#include "timeout.h"
20#include "control.h"
21#include "util.h"
22
23/* Install signal handlers */
24void init_signals(void)
25{
26 struct sigaction act = {
27 .sa_handler = sigalrm,
28 };
29
30 sigaction(SIGALRM, &act, NULL);
31 signal(SIGPIPE, SIG_IGN);
32}
33
34/* Parse a CID in string representation */
35unsigned int parse_cid(const char *str)
36{
37 char *endptr = NULL;
38 unsigned long n;
39
40 errno = 0;
41 n = strtoul(str, &endptr, 10);
42 if (errno || *endptr != '\0') {
43 fprintf(stderr, "malformed CID \"%s\"\n", str);
44 exit(EXIT_FAILURE);
45 }
46 return n;
47}
48
49/* Wait for the remote to close the connection */
50void vsock_wait_remote_close(int fd)
51{
52 struct epoll_event ev;
53 int epollfd, nfds;
54
55 epollfd = epoll_create1(0);
56 if (epollfd == -1) {
57 perror("epoll_create1");
58 exit(EXIT_FAILURE);
59 }
60
61 ev.events = EPOLLRDHUP | EPOLLHUP;
62 ev.data.fd = fd;
63 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
64 perror("epoll_ctl");
65 exit(EXIT_FAILURE);
66 }
67
68 nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
69 if (nfds == -1) {
70 perror("epoll_wait");
71 exit(EXIT_FAILURE);
72 }
73
74 if (nfds == 0) {
75 fprintf(stderr, "epoll_wait timed out\n");
76 exit(EXIT_FAILURE);
77 }
78
79 assert(nfds == 1);
80 assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
81 assert(ev.data.fd == fd);
82
83 close(epollfd);
84}
85
86/* Connect to <cid, port> and return the file descriptor. */
87static int vsock_connect(unsigned int cid, unsigned int port, int type)
88{
89 union {
90 struct sockaddr sa;
91 struct sockaddr_vm svm;
92 } addr = {
93 .svm = {
94 .svm_family = AF_VSOCK,
95 .svm_port = port,
96 .svm_cid = cid,
97 },
98 };
99 int ret;
100 int fd;
101
102 control_expectln("LISTENING");
103
104 fd = socket(AF_VSOCK, type, 0);
105
106 timeout_begin(TIMEOUT);
107 do {
108 ret = connect(fd, &addr.sa, sizeof(addr.svm));
109 timeout_check("connect");
110 } while (ret < 0 && errno == EINTR);
111 timeout_end();
112
113 if (ret < 0) {
114 int old_errno = errno;
115
116 close(fd);
117 fd = -1;
118 errno = old_errno;
119 }
120 return fd;
121}
122
123int vsock_stream_connect(unsigned int cid, unsigned int port)
124{
125 return vsock_connect(cid, port, SOCK_STREAM);
126}
127
128int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
129{
130 return vsock_connect(cid, port, SOCK_SEQPACKET);
131}
132
133/* Listen on <cid, port> and return the first incoming connection. The remote
134 * address is stored to clientaddrp. clientaddrp may be NULL.
135 */
136static int vsock_accept(unsigned int cid, unsigned int port,
137 struct sockaddr_vm *clientaddrp, int type)
138{
139 union {
140 struct sockaddr sa;
141 struct sockaddr_vm svm;
142 } addr = {
143 .svm = {
144 .svm_family = AF_VSOCK,
145 .svm_port = port,
146 .svm_cid = cid,
147 },
148 };
149 union {
150 struct sockaddr sa;
151 struct sockaddr_vm svm;
152 } clientaddr;
153 socklen_t clientaddr_len = sizeof(clientaddr.svm);
154 int fd;
155 int client_fd;
156 int old_errno;
157
158 fd = socket(AF_VSOCK, type, 0);
159
160 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
161 perror("bind");
162 exit(EXIT_FAILURE);
163 }
164
165 if (listen(fd, 1) < 0) {
166 perror("listen");
167 exit(EXIT_FAILURE);
168 }
169
170 control_writeln("LISTENING");
171
172 timeout_begin(TIMEOUT);
173 do {
174 client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
175 timeout_check("accept");
176 } while (client_fd < 0 && errno == EINTR);
177 timeout_end();
178
179 old_errno = errno;
180 close(fd);
181 errno = old_errno;
182
183 if (client_fd < 0)
184 return client_fd;
185
186 if (clientaddr_len != sizeof(clientaddr.svm)) {
187 fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
188 (size_t)clientaddr_len);
189 exit(EXIT_FAILURE);
190 }
191 if (clientaddr.sa.sa_family != AF_VSOCK) {
192 fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
193 clientaddr.sa.sa_family);
194 exit(EXIT_FAILURE);
195 }
196
197 if (clientaddrp)
198 *clientaddrp = clientaddr.svm;
199 return client_fd;
200}
201
202int vsock_stream_accept(unsigned int cid, unsigned int port,
203 struct sockaddr_vm *clientaddrp)
204{
205 return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
206}
207
208int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
209 struct sockaddr_vm *clientaddrp)
210{
211 return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
212}
213
214/* Transmit one byte and check the return value.
215 *
216 * expected_ret:
217 * <0 Negative errno (for testing errors)
218 * 0 End-of-file
219 * 1 Success
220 */
221void send_byte(int fd, int expected_ret, int flags)
222{
223 const uint8_t byte = 'A';
224 ssize_t nwritten;
225
226 timeout_begin(TIMEOUT);
227 do {
228 nwritten = send(fd, &byte, sizeof(byte), flags);
229 timeout_check("write");
230 } while (nwritten < 0 && errno == EINTR);
231 timeout_end();
232
233 if (expected_ret < 0) {
234 if (nwritten != -1) {
235 fprintf(stderr, "bogus send(2) return value %zd\n",
236 nwritten);
237 exit(EXIT_FAILURE);
238 }
239 if (errno != -expected_ret) {
240 perror("write");
241 exit(EXIT_FAILURE);
242 }
243 return;
244 }
245
246 if (nwritten < 0) {
247 perror("write");
248 exit(EXIT_FAILURE);
249 }
250 if (nwritten == 0) {
251 if (expected_ret == 0)
252 return;
253
254 fprintf(stderr, "unexpected EOF while sending byte\n");
255 exit(EXIT_FAILURE);
256 }
257 if (nwritten != sizeof(byte)) {
258 fprintf(stderr, "bogus send(2) return value %zd\n", nwritten);
259 exit(EXIT_FAILURE);
260 }
261}
262
263/* Receive one byte and check the return value.
264 *
265 * expected_ret:
266 * <0 Negative errno (for testing errors)
267 * 0 End-of-file
268 * 1 Success
269 */
270void recv_byte(int fd, int expected_ret, int flags)
271{
272 uint8_t byte;
273 ssize_t nread;
274
275 timeout_begin(TIMEOUT);
276 do {
277 nread = recv(fd, &byte, sizeof(byte), flags);
278 timeout_check("read");
279 } while (nread < 0 && errno == EINTR);
280 timeout_end();
281
282 if (expected_ret < 0) {
283 if (nread != -1) {
284 fprintf(stderr, "bogus recv(2) return value %zd\n",
285 nread);
286 exit(EXIT_FAILURE);
287 }
288 if (errno != -expected_ret) {
289 perror("read");
290 exit(EXIT_FAILURE);
291 }
292 return;
293 }
294
295 if (nread < 0) {
296 perror("read");
297 exit(EXIT_FAILURE);
298 }
299 if (nread == 0) {
300 if (expected_ret == 0)
301 return;
302
303 fprintf(stderr, "unexpected EOF while receiving byte\n");
304 exit(EXIT_FAILURE);
305 }
306 if (nread != sizeof(byte)) {
307 fprintf(stderr, "bogus recv(2) return value %zd\n", nread);
308 exit(EXIT_FAILURE);
309 }
310 if (byte != 'A') {
311 fprintf(stderr, "unexpected byte read %c\n", byte);
312 exit(EXIT_FAILURE);
313 }
314}
315
316/* Run test cases. The program terminates if a failure occurs. */
317void run_tests(const struct test_case *test_cases,
318 const struct test_opts *opts)
319{
320 int i;
321
322 for (i = 0; test_cases[i].name; i++) {
323 void (*run)(const struct test_opts *opts);
324 char *line;
325
326 printf("%d - %s...", i, test_cases[i].name);
327 fflush(stdout);
328
329 /* Full barrier before executing the next test. This
330 * ensures that client and server are executing the
331 * same test case. In particular, it means whoever is
332 * faster will not see the peer still executing the
333 * last test. This is important because port numbers
334 * can be used by multiple test cases.
335 */
336 if (test_cases[i].skip)
337 control_writeln("SKIP");
338 else
339 control_writeln("NEXT");
340
341 line = control_readln();
342 if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
343
344 printf("skipped\n");
345
346 free(line);
347 continue;
348 }
349
350 control_cmpln(line, "NEXT", true);
351 free(line);
352
353 if (opts->mode == TEST_MODE_CLIENT)
354 run = test_cases[i].run_client;
355 else
356 run = test_cases[i].run_server;
357
358 if (run)
359 run(opts);
360
361 printf("ok\n");
362 }
363}
364
365void list_tests(const struct test_case *test_cases)
366{
367 int i;
368
369 printf("ID\tTest name\n");
370
371 for (i = 0; test_cases[i].name; i++)
372 printf("%d\t%s\n", i, test_cases[i].name);
373
374 exit(EXIT_FAILURE);
375}
376
377void skip_test(struct test_case *test_cases, size_t test_cases_len,
378 const char *test_id_str)
379{
380 unsigned long test_id;
381 char *endptr = NULL;
382
383 errno = 0;
384 test_id = strtoul(test_id_str, &endptr, 10);
385 if (errno || *endptr != '\0') {
386 fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
387 exit(EXIT_FAILURE);
388 }
389
390 if (test_id >= test_cases_len) {
391 fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
392 test_id, test_cases_len - 1);
393 exit(EXIT_FAILURE);
394 }
395
396 test_cases[test_id].skip = true;
397}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * vsock test utilities
4 *
5 * Copyright (C) 2017 Red Hat, Inc.
6 *
7 * Author: Stefan Hajnoczi <stefanha@redhat.com>
8 */
9
10#include <errno.h>
11#include <stdio.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <string.h>
15#include <signal.h>
16#include <unistd.h>
17#include <assert.h>
18#include <sys/epoll.h>
19#include <sys/mman.h>
20
21#include "timeout.h"
22#include "control.h"
23#include "util.h"
24
25/* Install signal handlers */
26void init_signals(void)
27{
28 struct sigaction act = {
29 .sa_handler = sigalrm,
30 };
31
32 sigaction(SIGALRM, &act, NULL);
33 signal(SIGPIPE, SIG_IGN);
34}
35
36/* Parse a CID in string representation */
37unsigned int parse_cid(const char *str)
38{
39 char *endptr = NULL;
40 unsigned long n;
41
42 errno = 0;
43 n = strtoul(str, &endptr, 10);
44 if (errno || *endptr != '\0') {
45 fprintf(stderr, "malformed CID \"%s\"\n", str);
46 exit(EXIT_FAILURE);
47 }
48 return n;
49}
50
51/* Wait for the remote to close the connection */
52void vsock_wait_remote_close(int fd)
53{
54 struct epoll_event ev;
55 int epollfd, nfds;
56
57 epollfd = epoll_create1(0);
58 if (epollfd == -1) {
59 perror("epoll_create1");
60 exit(EXIT_FAILURE);
61 }
62
63 ev.events = EPOLLRDHUP | EPOLLHUP;
64 ev.data.fd = fd;
65 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
66 perror("epoll_ctl");
67 exit(EXIT_FAILURE);
68 }
69
70 nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
71 if (nfds == -1) {
72 perror("epoll_wait");
73 exit(EXIT_FAILURE);
74 }
75
76 if (nfds == 0) {
77 fprintf(stderr, "epoll_wait timed out\n");
78 exit(EXIT_FAILURE);
79 }
80
81 assert(nfds == 1);
82 assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
83 assert(ev.data.fd == fd);
84
85 close(epollfd);
86}
87
88/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
89int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
90{
91 struct sockaddr_vm sa_client = {
92 .svm_family = AF_VSOCK,
93 .svm_cid = VMADDR_CID_ANY,
94 .svm_port = bind_port,
95 };
96 struct sockaddr_vm sa_server = {
97 .svm_family = AF_VSOCK,
98 .svm_cid = cid,
99 .svm_port = port,
100 };
101
102 int client_fd, ret;
103
104 client_fd = socket(AF_VSOCK, type, 0);
105 if (client_fd < 0) {
106 perror("socket");
107 exit(EXIT_FAILURE);
108 }
109
110 if (bind(client_fd, (struct sockaddr *)&sa_client, sizeof(sa_client))) {
111 perror("bind");
112 exit(EXIT_FAILURE);
113 }
114
115 timeout_begin(TIMEOUT);
116 do {
117 ret = connect(client_fd, (struct sockaddr *)&sa_server, sizeof(sa_server));
118 timeout_check("connect");
119 } while (ret < 0 && errno == EINTR);
120 timeout_end();
121
122 if (ret < 0) {
123 perror("connect");
124 exit(EXIT_FAILURE);
125 }
126
127 return client_fd;
128}
129
130/* Connect to <cid, port> and return the file descriptor. */
131static int vsock_connect(unsigned int cid, unsigned int port, int type)
132{
133 union {
134 struct sockaddr sa;
135 struct sockaddr_vm svm;
136 } addr = {
137 .svm = {
138 .svm_family = AF_VSOCK,
139 .svm_port = port,
140 .svm_cid = cid,
141 },
142 };
143 int ret;
144 int fd;
145
146 control_expectln("LISTENING");
147
148 fd = socket(AF_VSOCK, type, 0);
149 if (fd < 0) {
150 perror("socket");
151 exit(EXIT_FAILURE);
152 }
153
154 timeout_begin(TIMEOUT);
155 do {
156 ret = connect(fd, &addr.sa, sizeof(addr.svm));
157 timeout_check("connect");
158 } while (ret < 0 && errno == EINTR);
159 timeout_end();
160
161 if (ret < 0) {
162 int old_errno = errno;
163
164 close(fd);
165 fd = -1;
166 errno = old_errno;
167 }
168 return fd;
169}
170
171int vsock_stream_connect(unsigned int cid, unsigned int port)
172{
173 return vsock_connect(cid, port, SOCK_STREAM);
174}
175
176int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
177{
178 return vsock_connect(cid, port, SOCK_SEQPACKET);
179}
180
181/* Listen on <cid, port> and return the file descriptor. */
182static int vsock_listen(unsigned int cid, unsigned int port, int type)
183{
184 union {
185 struct sockaddr sa;
186 struct sockaddr_vm svm;
187 } addr = {
188 .svm = {
189 .svm_family = AF_VSOCK,
190 .svm_port = port,
191 .svm_cid = cid,
192 },
193 };
194 int fd;
195
196 fd = socket(AF_VSOCK, type, 0);
197 if (fd < 0) {
198 perror("socket");
199 exit(EXIT_FAILURE);
200 }
201
202 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
203 perror("bind");
204 exit(EXIT_FAILURE);
205 }
206
207 if (listen(fd, 1) < 0) {
208 perror("listen");
209 exit(EXIT_FAILURE);
210 }
211
212 return fd;
213}
214
215/* Listen on <cid, port> and return the first incoming connection. The remote
216 * address is stored to clientaddrp. clientaddrp may be NULL.
217 */
218static int vsock_accept(unsigned int cid, unsigned int port,
219 struct sockaddr_vm *clientaddrp, int type)
220{
221 union {
222 struct sockaddr sa;
223 struct sockaddr_vm svm;
224 } clientaddr;
225 socklen_t clientaddr_len = sizeof(clientaddr.svm);
226 int fd, client_fd, old_errno;
227
228 fd = vsock_listen(cid, port, type);
229
230 control_writeln("LISTENING");
231
232 timeout_begin(TIMEOUT);
233 do {
234 client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
235 timeout_check("accept");
236 } while (client_fd < 0 && errno == EINTR);
237 timeout_end();
238
239 old_errno = errno;
240 close(fd);
241 errno = old_errno;
242
243 if (client_fd < 0)
244 return client_fd;
245
246 if (clientaddr_len != sizeof(clientaddr.svm)) {
247 fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
248 (size_t)clientaddr_len);
249 exit(EXIT_FAILURE);
250 }
251 if (clientaddr.sa.sa_family != AF_VSOCK) {
252 fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
253 clientaddr.sa.sa_family);
254 exit(EXIT_FAILURE);
255 }
256
257 if (clientaddrp)
258 *clientaddrp = clientaddr.svm;
259 return client_fd;
260}
261
262int vsock_stream_accept(unsigned int cid, unsigned int port,
263 struct sockaddr_vm *clientaddrp)
264{
265 return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
266}
267
268int vsock_stream_listen(unsigned int cid, unsigned int port)
269{
270 return vsock_listen(cid, port, SOCK_STREAM);
271}
272
273int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
274 struct sockaddr_vm *clientaddrp)
275{
276 return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
277}
278
279/* Transmit bytes from a buffer and check the return value.
280 *
281 * expected_ret:
282 * <0 Negative errno (for testing errors)
283 * 0 End-of-file
284 * >0 Success (bytes successfully written)
285 */
286void send_buf(int fd, const void *buf, size_t len, int flags,
287 ssize_t expected_ret)
288{
289 ssize_t nwritten = 0;
290 ssize_t ret;
291
292 timeout_begin(TIMEOUT);
293 do {
294 ret = send(fd, buf + nwritten, len - nwritten, flags);
295 timeout_check("send");
296
297 if (ret == 0 || (ret < 0 && errno != EINTR))
298 break;
299
300 nwritten += ret;
301 } while (nwritten < len);
302 timeout_end();
303
304 if (expected_ret < 0) {
305 if (ret != -1) {
306 fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n",
307 ret, expected_ret);
308 exit(EXIT_FAILURE);
309 }
310 if (errno != -expected_ret) {
311 perror("send");
312 exit(EXIT_FAILURE);
313 }
314 return;
315 }
316
317 if (ret < 0) {
318 perror("send");
319 exit(EXIT_FAILURE);
320 }
321
322 if (nwritten != expected_ret) {
323 if (ret == 0)
324 fprintf(stderr, "unexpected EOF while sending bytes\n");
325
326 fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n",
327 nwritten, expected_ret);
328 exit(EXIT_FAILURE);
329 }
330}
331
332/* Receive bytes in a buffer and check the return value.
333 *
334 * expected_ret:
335 * <0 Negative errno (for testing errors)
336 * 0 End-of-file
337 * >0 Success (bytes successfully read)
338 */
339void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
340{
341 ssize_t nread = 0;
342 ssize_t ret;
343
344 timeout_begin(TIMEOUT);
345 do {
346 ret = recv(fd, buf + nread, len - nread, flags);
347 timeout_check("recv");
348
349 if (ret == 0 || (ret < 0 && errno != EINTR))
350 break;
351
352 nread += ret;
353 } while (nread < len);
354 timeout_end();
355
356 if (expected_ret < 0) {
357 if (ret != -1) {
358 fprintf(stderr, "bogus recv(2) return value %zd (expected %zd)\n",
359 ret, expected_ret);
360 exit(EXIT_FAILURE);
361 }
362 if (errno != -expected_ret) {
363 perror("recv");
364 exit(EXIT_FAILURE);
365 }
366 return;
367 }
368
369 if (ret < 0) {
370 perror("recv");
371 exit(EXIT_FAILURE);
372 }
373
374 if (nread != expected_ret) {
375 if (ret == 0)
376 fprintf(stderr, "unexpected EOF while receiving bytes\n");
377
378 fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n",
379 nread, expected_ret);
380 exit(EXIT_FAILURE);
381 }
382}
383
384/* Transmit one byte and check the return value.
385 *
386 * expected_ret:
387 * <0 Negative errno (for testing errors)
388 * 0 End-of-file
389 * 1 Success
390 */
391void send_byte(int fd, int expected_ret, int flags)
392{
393 const uint8_t byte = 'A';
394
395 send_buf(fd, &byte, sizeof(byte), flags, expected_ret);
396}
397
398/* Receive one byte and check the return value.
399 *
400 * expected_ret:
401 * <0 Negative errno (for testing errors)
402 * 0 End-of-file
403 * 1 Success
404 */
405void recv_byte(int fd, int expected_ret, int flags)
406{
407 uint8_t byte;
408
409 recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
410
411 if (byte != 'A') {
412 fprintf(stderr, "unexpected byte read %c\n", byte);
413 exit(EXIT_FAILURE);
414 }
415}
416
417/* Run test cases. The program terminates if a failure occurs. */
418void run_tests(const struct test_case *test_cases,
419 const struct test_opts *opts)
420{
421 int i;
422
423 for (i = 0; test_cases[i].name; i++) {
424 void (*run)(const struct test_opts *opts);
425 char *line;
426
427 printf("%d - %s...", i, test_cases[i].name);
428 fflush(stdout);
429
430 /* Full barrier before executing the next test. This
431 * ensures that client and server are executing the
432 * same test case. In particular, it means whoever is
433 * faster will not see the peer still executing the
434 * last test. This is important because port numbers
435 * can be used by multiple test cases.
436 */
437 if (test_cases[i].skip)
438 control_writeln("SKIP");
439 else
440 control_writeln("NEXT");
441
442 line = control_readln();
443 if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
444
445 printf("skipped\n");
446
447 free(line);
448 continue;
449 }
450
451 control_cmpln(line, "NEXT", true);
452 free(line);
453
454 if (opts->mode == TEST_MODE_CLIENT)
455 run = test_cases[i].run_client;
456 else
457 run = test_cases[i].run_server;
458
459 if (run)
460 run(opts);
461
462 printf("ok\n");
463 }
464}
465
466void list_tests(const struct test_case *test_cases)
467{
468 int i;
469
470 printf("ID\tTest name\n");
471
472 for (i = 0; test_cases[i].name; i++)
473 printf("%d\t%s\n", i, test_cases[i].name);
474
475 exit(EXIT_FAILURE);
476}
477
478void skip_test(struct test_case *test_cases, size_t test_cases_len,
479 const char *test_id_str)
480{
481 unsigned long test_id;
482 char *endptr = NULL;
483
484 errno = 0;
485 test_id = strtoul(test_id_str, &endptr, 10);
486 if (errno || *endptr != '\0') {
487 fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
488 exit(EXIT_FAILURE);
489 }
490
491 if (test_id >= test_cases_len) {
492 fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
493 test_id, test_cases_len - 1);
494 exit(EXIT_FAILURE);
495 }
496
497 test_cases[test_id].skip = true;
498}
499
500unsigned long hash_djb2(const void *data, size_t len)
501{
502 unsigned long hash = 5381;
503 int i = 0;
504
505 while (i < len) {
506 hash = ((hash << 5) + hash) + ((unsigned char *)data)[i];
507 i++;
508 }
509
510 return hash;
511}
512
513size_t iovec_bytes(const struct iovec *iov, size_t iovnum)
514{
515 size_t bytes;
516 int i;
517
518 for (bytes = 0, i = 0; i < iovnum; i++)
519 bytes += iov[i].iov_len;
520
521 return bytes;
522}
523
524unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum)
525{
526 unsigned long hash;
527 size_t iov_bytes;
528 size_t offs;
529 void *tmp;
530 int i;
531
532 iov_bytes = iovec_bytes(iov, iovnum);
533
534 tmp = malloc(iov_bytes);
535 if (!tmp) {
536 perror("malloc");
537 exit(EXIT_FAILURE);
538 }
539
540 for (offs = 0, i = 0; i < iovnum; i++) {
541 memcpy(tmp + offs, iov[i].iov_base, iov[i].iov_len);
542 offs += iov[i].iov_len;
543 }
544
545 hash = hash_djb2(tmp, iov_bytes);
546 free(tmp);
547
548 return hash;
549}
550
551/* Allocates and returns new 'struct iovec *' according pattern
552 * in the 'test_iovec'. For each element in the 'test_iovec' it
553 * allocates new element in the resulting 'iovec'. 'iov_len'
554 * of the new element is copied from 'test_iovec'. 'iov_base' is
555 * allocated depending on the 'iov_base' of 'test_iovec':
556 *
557 * 'iov_base' == NULL -> valid buf: mmap('iov_len').
558 *
559 * 'iov_base' == MAP_FAILED -> invalid buf:
560 * mmap('iov_len'), then munmap('iov_len').
561 * 'iov_base' still contains result of
562 * mmap().
563 *
564 * 'iov_base' == number -> unaligned valid buf:
565 * mmap('iov_len') + number.
566 *
567 * 'iovnum' is number of elements in 'test_iovec'.
568 *
569 * Returns new 'iovec' or calls 'exit()' on error.
570 */
571struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum)
572{
573 struct iovec *iovec;
574 int i;
575
576 iovec = malloc(sizeof(*iovec) * iovnum);
577 if (!iovec) {
578 perror("malloc");
579 exit(EXIT_FAILURE);
580 }
581
582 for (i = 0; i < iovnum; i++) {
583 iovec[i].iov_len = test_iovec[i].iov_len;
584
585 iovec[i].iov_base = mmap(NULL, iovec[i].iov_len,
586 PROT_READ | PROT_WRITE,
587 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
588 -1, 0);
589 if (iovec[i].iov_base == MAP_FAILED) {
590 perror("mmap");
591 exit(EXIT_FAILURE);
592 }
593
594 if (test_iovec[i].iov_base != MAP_FAILED)
595 iovec[i].iov_base += (uintptr_t)test_iovec[i].iov_base;
596 }
597
598 /* Unmap "invalid" elements. */
599 for (i = 0; i < iovnum; i++) {
600 if (test_iovec[i].iov_base == MAP_FAILED) {
601 if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
602 perror("munmap");
603 exit(EXIT_FAILURE);
604 }
605 }
606 }
607
608 for (i = 0; i < iovnum; i++) {
609 int j;
610
611 if (test_iovec[i].iov_base == MAP_FAILED)
612 continue;
613
614 for (j = 0; j < iovec[i].iov_len; j++)
615 ((uint8_t *)iovec[i].iov_base)[j] = rand() & 0xff;
616 }
617
618 return iovec;
619}
620
621/* Frees 'iovec *', previously allocated by 'alloc_test_iovec()'.
622 * On error calls 'exit()'.
623 */
624void free_test_iovec(const struct iovec *test_iovec,
625 struct iovec *iovec, int iovnum)
626{
627 int i;
628
629 for (i = 0; i < iovnum; i++) {
630 if (test_iovec[i].iov_base != MAP_FAILED) {
631 if (test_iovec[i].iov_base)
632 iovec[i].iov_base -= (uintptr_t)test_iovec[i].iov_base;
633
634 if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
635 perror("munmap");
636 exit(EXIT_FAILURE);
637 }
638 }
639 }
640
641 free(iovec);
642}