Skip to content

Commit 587ed79

Browse files
Arseniy KrasnovPaolo Abeni
authored andcommitted
vsock/test: rework MSG_PEEK test for SOCK_STREAM
This new version makes test more complicated by adding empty read, partial read and data comparisons between MSG_PEEK and normal reads. Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> Reviewed-by: Stefano Garzarella <sgarzare@redhat.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent a75f501 commit 587ed79

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

tools/testing/vsock/vsock_test.c

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,22 +255,47 @@ static void test_stream_multiconn_server(const struct test_opts *opts)
255255
close(fds[i]);
256256
}
257257

258+
#define MSG_PEEK_BUF_LEN 64
259+
258260
static void test_stream_msg_peek_client(const struct test_opts *opts)
259261
{
262+
unsigned char buf[MSG_PEEK_BUF_LEN];
263+
ssize_t send_size;
260264
int fd;
265+
int i;
261266

262267
fd = vsock_stream_connect(opts->peer_cid, 1234);
263268
if (fd < 0) {
264269
perror("connect");
265270
exit(EXIT_FAILURE);
266271
}
267272

268-
send_byte(fd, 1, 0);
273+
for (i = 0; i < sizeof(buf); i++)
274+
buf[i] = rand() & 0xFF;
275+
276+
control_expectln("SRVREADY");
277+
278+
send_size = send(fd, buf, sizeof(buf), 0);
279+
280+
if (send_size < 0) {
281+
perror("send");
282+
exit(EXIT_FAILURE);
283+
}
284+
285+
if (send_size != sizeof(buf)) {
286+
fprintf(stderr, "Invalid send size %zi\n", send_size);
287+
exit(EXIT_FAILURE);
288+
}
289+
269290
close(fd);
270291
}
271292

272293
static void test_stream_msg_peek_server(const struct test_opts *opts)
273294
{
295+
unsigned char buf_half[MSG_PEEK_BUF_LEN / 2];
296+
unsigned char buf_normal[MSG_PEEK_BUF_LEN];
297+
unsigned char buf_peek[MSG_PEEK_BUF_LEN];
298+
ssize_t res;
274299
int fd;
275300

276301
fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
@@ -279,8 +304,55 @@ static void test_stream_msg_peek_server(const struct test_opts *opts)
279304
exit(EXIT_FAILURE);
280305
}
281306

282-
recv_byte(fd, 1, MSG_PEEK);
283-
recv_byte(fd, 1, 0);
307+
/* Peek from empty socket. */
308+
res = recv(fd, buf_peek, sizeof(buf_peek), MSG_PEEK | MSG_DONTWAIT);
309+
if (res != -1) {
310+
fprintf(stderr, "expected recv(2) failure, got %zi\n", res);
311+
exit(EXIT_FAILURE);
312+
}
313+
314+
if (errno != EAGAIN) {
315+
perror("EAGAIN expected");
316+
exit(EXIT_FAILURE);
317+
}
318+
319+
control_writeln("SRVREADY");
320+
321+
/* Peek part of data. */
322+
res = recv(fd, buf_half, sizeof(buf_half), MSG_PEEK);
323+
if (res != sizeof(buf_half)) {
324+
fprintf(stderr, "recv(2) + MSG_PEEK, expected %zu, got %zi\n",
325+
sizeof(buf_half), res);
326+
exit(EXIT_FAILURE);
327+
}
328+
329+
/* Peek whole data. */
330+
res = recv(fd, buf_peek, sizeof(buf_peek), MSG_PEEK);
331+
if (res != sizeof(buf_peek)) {
332+
fprintf(stderr, "recv(2) + MSG_PEEK, expected %zu, got %zi\n",
333+
sizeof(buf_peek), res);
334+
exit(EXIT_FAILURE);
335+
}
336+
337+
/* Compare partial and full peek. */
338+
if (memcmp(buf_half, buf_peek, sizeof(buf_half))) {
339+
fprintf(stderr, "Partial peek data mismatch\n");
340+
exit(EXIT_FAILURE);
341+
}
342+
343+
res = recv(fd, buf_normal, sizeof(buf_normal), 0);
344+
if (res != sizeof(buf_normal)) {
345+
fprintf(stderr, "recv(2), expected %zu, got %zi\n",
346+
sizeof(buf_normal), res);
347+
exit(EXIT_FAILURE);
348+
}
349+
350+
/* Compare full peek and normal read. */
351+
if (memcmp(buf_peek, buf_normal, sizeof(buf_peek))) {
352+
fprintf(stderr, "Full peek data mismatch\n");
353+
exit(EXIT_FAILURE);
354+
}
355+
284356
close(fd);
285357
}
286358

0 commit comments

Comments
 (0)