Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

VSOCK: add send_byte()/recv_byte() test utilities

Test cases will want to transfer data. This patch adds utility
functions to do this.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Stefan Hajnoczi and committed by
David S. Miller
092f32ae 2f65b44e

+105
+103
tools/testing/vsock/util.c
··· 9 9 10 10 #include <errno.h> 11 11 #include <stdio.h> 12 + #include <stdint.h> 12 13 #include <stdlib.h> 13 14 #include <signal.h> 14 15 #include <unistd.h> ··· 148 147 if (clientaddrp) 149 148 *clientaddrp = clientaddr.svm; 150 149 return client_fd; 150 + } 151 + 152 + /* Transmit one byte and check the return value. 153 + * 154 + * expected_ret: 155 + * <0 Negative errno (for testing errors) 156 + * 0 End-of-file 157 + * 1 Success 158 + */ 159 + void send_byte(int fd, int expected_ret, int flags) 160 + { 161 + const uint8_t byte = 'A'; 162 + ssize_t nwritten; 163 + 164 + timeout_begin(TIMEOUT); 165 + do { 166 + nwritten = send(fd, &byte, sizeof(byte), flags); 167 + timeout_check("write"); 168 + } while (nwritten < 0 && errno == EINTR); 169 + timeout_end(); 170 + 171 + if (expected_ret < 0) { 172 + if (nwritten != -1) { 173 + fprintf(stderr, "bogus send(2) return value %zd\n", 174 + nwritten); 175 + exit(EXIT_FAILURE); 176 + } 177 + if (errno != -expected_ret) { 178 + perror("write"); 179 + exit(EXIT_FAILURE); 180 + } 181 + return; 182 + } 183 + 184 + if (nwritten < 0) { 185 + perror("write"); 186 + exit(EXIT_FAILURE); 187 + } 188 + if (nwritten == 0) { 189 + if (expected_ret == 0) 190 + return; 191 + 192 + fprintf(stderr, "unexpected EOF while sending byte\n"); 193 + exit(EXIT_FAILURE); 194 + } 195 + if (nwritten != sizeof(byte)) { 196 + fprintf(stderr, "bogus send(2) return value %zd\n", nwritten); 197 + exit(EXIT_FAILURE); 198 + } 199 + } 200 + 201 + /* Receive one byte and check the return value. 202 + * 203 + * expected_ret: 204 + * <0 Negative errno (for testing errors) 205 + * 0 End-of-file 206 + * 1 Success 207 + */ 208 + void recv_byte(int fd, int expected_ret, int flags) 209 + { 210 + uint8_t byte; 211 + ssize_t nread; 212 + 213 + timeout_begin(TIMEOUT); 214 + do { 215 + nread = recv(fd, &byte, sizeof(byte), flags); 216 + timeout_check("read"); 217 + } while (nread < 0 && errno == EINTR); 218 + timeout_end(); 219 + 220 + if (expected_ret < 0) { 221 + if (nread != -1) { 222 + fprintf(stderr, "bogus recv(2) return value %zd\n", 223 + nread); 224 + exit(EXIT_FAILURE); 225 + } 226 + if (errno != -expected_ret) { 227 + perror("read"); 228 + exit(EXIT_FAILURE); 229 + } 230 + return; 231 + } 232 + 233 + if (nread < 0) { 234 + perror("read"); 235 + exit(EXIT_FAILURE); 236 + } 237 + if (nread == 0) { 238 + if (expected_ret == 0) 239 + return; 240 + 241 + fprintf(stderr, "unexpected EOF while receiving byte\n"); 242 + exit(EXIT_FAILURE); 243 + } 244 + if (nread != sizeof(byte)) { 245 + fprintf(stderr, "bogus recv(2) return value %zd\n", nread); 246 + exit(EXIT_FAILURE); 247 + } 248 + if (byte != 'A') { 249 + fprintf(stderr, "unexpected byte read %c\n", byte); 250 + exit(EXIT_FAILURE); 251 + } 151 252 } 152 253 153 254 /* Run test cases. The program terminates if a failure occurs. */
+2
tools/testing/vsock/util.h
··· 36 36 int vsock_stream_connect(unsigned int cid, unsigned int port); 37 37 int vsock_stream_accept(unsigned int cid, unsigned int port, 38 38 struct sockaddr_vm *clientaddrp); 39 + void send_byte(int fd, int expected_ret, int flags); 40 + void recv_byte(int fd, int expected_ret, int flags); 39 41 void run_tests(const struct test_case *test_cases, 40 42 const struct test_opts *opts); 41 43