treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / testing / vsock / util.c
blob93cbd6f603f976ef874cd40a02ec6ac0cffab03e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * vsock test utilities
5 * Copyright (C) 2017 Red Hat, Inc.
7 * Author: Stefan Hajnoczi <stefanha@redhat.com>
8 */
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>
19 #include "timeout.h"
20 #include "control.h"
21 #include "util.h"
23 /* Install signal handlers */
24 void init_signals(void)
26 struct sigaction act = {
27 .sa_handler = sigalrm,
30 sigaction(SIGALRM, &act, NULL);
31 signal(SIGPIPE, SIG_IGN);
34 /* Parse a CID in string representation */
35 unsigned int parse_cid(const char *str)
37 char *endptr = NULL;
38 unsigned long n;
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);
46 return n;
49 /* Wait for the remote to close the connection */
50 void vsock_wait_remote_close(int fd)
52 struct epoll_event ev;
53 int epollfd, nfds;
55 epollfd = epoll_create1(0);
56 if (epollfd == -1) {
57 perror("epoll_create1");
58 exit(EXIT_FAILURE);
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);
68 nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
69 if (nfds == -1) {
70 perror("epoll_wait");
71 exit(EXIT_FAILURE);
74 if (nfds == 0) {
75 fprintf(stderr, "epoll_wait timed out\n");
76 exit(EXIT_FAILURE);
79 assert(nfds == 1);
80 assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
81 assert(ev.data.fd == fd);
83 close(epollfd);
86 /* Connect to <cid, port> and return the file descriptor. */
87 int vsock_stream_connect(unsigned int cid, unsigned int port)
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,
99 int ret;
100 int fd;
102 control_expectln("LISTENING");
104 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
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();
113 if (ret < 0) {
114 int old_errno = errno;
116 close(fd);
117 fd = -1;
118 errno = old_errno;
120 return fd;
123 /* Listen on <cid, port> and return the first incoming connection. The remote
124 * address is stored to clientaddrp. clientaddrp may be NULL.
126 int vsock_stream_accept(unsigned int cid, unsigned int port,
127 struct sockaddr_vm *clientaddrp)
129 union {
130 struct sockaddr sa;
131 struct sockaddr_vm svm;
132 } addr = {
133 .svm = {
134 .svm_family = AF_VSOCK,
135 .svm_port = port,
136 .svm_cid = cid,
139 union {
140 struct sockaddr sa;
141 struct sockaddr_vm svm;
142 } clientaddr;
143 socklen_t clientaddr_len = sizeof(clientaddr.svm);
144 int fd;
145 int client_fd;
146 int old_errno;
148 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
150 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
151 perror("bind");
152 exit(EXIT_FAILURE);
155 if (listen(fd, 1) < 0) {
156 perror("listen");
157 exit(EXIT_FAILURE);
160 control_writeln("LISTENING");
162 timeout_begin(TIMEOUT);
163 do {
164 client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
165 timeout_check("accept");
166 } while (client_fd < 0 && errno == EINTR);
167 timeout_end();
169 old_errno = errno;
170 close(fd);
171 errno = old_errno;
173 if (client_fd < 0)
174 return client_fd;
176 if (clientaddr_len != sizeof(clientaddr.svm)) {
177 fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
178 (size_t)clientaddr_len);
179 exit(EXIT_FAILURE);
181 if (clientaddr.sa.sa_family != AF_VSOCK) {
182 fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
183 clientaddr.sa.sa_family);
184 exit(EXIT_FAILURE);
187 if (clientaddrp)
188 *clientaddrp = clientaddr.svm;
189 return client_fd;
192 /* Transmit one byte and check the return value.
194 * expected_ret:
195 * <0 Negative errno (for testing errors)
196 * 0 End-of-file
197 * 1 Success
199 void send_byte(int fd, int expected_ret, int flags)
201 const uint8_t byte = 'A';
202 ssize_t nwritten;
204 timeout_begin(TIMEOUT);
205 do {
206 nwritten = send(fd, &byte, sizeof(byte), flags);
207 timeout_check("write");
208 } while (nwritten < 0 && errno == EINTR);
209 timeout_end();
211 if (expected_ret < 0) {
212 if (nwritten != -1) {
213 fprintf(stderr, "bogus send(2) return value %zd\n",
214 nwritten);
215 exit(EXIT_FAILURE);
217 if (errno != -expected_ret) {
218 perror("write");
219 exit(EXIT_FAILURE);
221 return;
224 if (nwritten < 0) {
225 perror("write");
226 exit(EXIT_FAILURE);
228 if (nwritten == 0) {
229 if (expected_ret == 0)
230 return;
232 fprintf(stderr, "unexpected EOF while sending byte\n");
233 exit(EXIT_FAILURE);
235 if (nwritten != sizeof(byte)) {
236 fprintf(stderr, "bogus send(2) return value %zd\n", nwritten);
237 exit(EXIT_FAILURE);
241 /* Receive one byte and check the return value.
243 * expected_ret:
244 * <0 Negative errno (for testing errors)
245 * 0 End-of-file
246 * 1 Success
248 void recv_byte(int fd, int expected_ret, int flags)
250 uint8_t byte;
251 ssize_t nread;
253 timeout_begin(TIMEOUT);
254 do {
255 nread = recv(fd, &byte, sizeof(byte), flags);
256 timeout_check("read");
257 } while (nread < 0 && errno == EINTR);
258 timeout_end();
260 if (expected_ret < 0) {
261 if (nread != -1) {
262 fprintf(stderr, "bogus recv(2) return value %zd\n",
263 nread);
264 exit(EXIT_FAILURE);
266 if (errno != -expected_ret) {
267 perror("read");
268 exit(EXIT_FAILURE);
270 return;
273 if (nread < 0) {
274 perror("read");
275 exit(EXIT_FAILURE);
277 if (nread == 0) {
278 if (expected_ret == 0)
279 return;
281 fprintf(stderr, "unexpected EOF while receiving byte\n");
282 exit(EXIT_FAILURE);
284 if (nread != sizeof(byte)) {
285 fprintf(stderr, "bogus recv(2) return value %zd\n", nread);
286 exit(EXIT_FAILURE);
288 if (byte != 'A') {
289 fprintf(stderr, "unexpected byte read %c\n", byte);
290 exit(EXIT_FAILURE);
294 /* Run test cases. The program terminates if a failure occurs. */
295 void run_tests(const struct test_case *test_cases,
296 const struct test_opts *opts)
298 int i;
300 for (i = 0; test_cases[i].name; i++) {
301 void (*run)(const struct test_opts *opts);
302 char *line;
304 printf("%d - %s...", i, test_cases[i].name);
305 fflush(stdout);
307 /* Full barrier before executing the next test. This
308 * ensures that client and server are executing the
309 * same test case. In particular, it means whoever is
310 * faster will not see the peer still executing the
311 * last test. This is important because port numbers
312 * can be used by multiple test cases.
314 if (test_cases[i].skip)
315 control_writeln("SKIP");
316 else
317 control_writeln("NEXT");
319 line = control_readln();
320 if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
322 printf("skipped\n");
324 free(line);
325 continue;
328 control_cmpln(line, "NEXT", true);
329 free(line);
331 if (opts->mode == TEST_MODE_CLIENT)
332 run = test_cases[i].run_client;
333 else
334 run = test_cases[i].run_server;
336 if (run)
337 run(opts);
339 printf("ok\n");
343 void list_tests(const struct test_case *test_cases)
345 int i;
347 printf("ID\tTest name\n");
349 for (i = 0; test_cases[i].name; i++)
350 printf("%d\t%s\n", i, test_cases[i].name);
352 exit(EXIT_FAILURE);
355 void skip_test(struct test_case *test_cases, size_t test_cases_len,
356 const char *test_id_str)
358 unsigned long test_id;
359 char *endptr = NULL;
361 errno = 0;
362 test_id = strtoul(test_id_str, &endptr, 10);
363 if (errno || *endptr != '\0') {
364 fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
365 exit(EXIT_FAILURE);
368 if (test_id >= test_cases_len) {
369 fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
370 test_id, test_cases_len - 1);
371 exit(EXIT_FAILURE);
374 test_cases[test_id].skip = true;