Skip to content

Commit 8a0697f

Browse files
Arseniy KrasnovPaolo Abeni
authored andcommitted
vsock/test: MSG_PEEK test for SOCK_SEQPACKET
This adds MSG_PEEK test for SOCK_SEQPACKET. It works in the same way as SOCK_STREAM test, except it also tests MSG_TRUNC flag. 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 587ed79 commit 8a0697f

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

tools/testing/vsock/vsock_test.c

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,19 @@ static void test_stream_multiconn_server(const struct test_opts *opts)
257257

258258
#define MSG_PEEK_BUF_LEN 64
259259

260-
static void test_stream_msg_peek_client(const struct test_opts *opts)
260+
static void test_msg_peek_client(const struct test_opts *opts,
261+
bool seqpacket)
261262
{
262263
unsigned char buf[MSG_PEEK_BUF_LEN];
263264
ssize_t send_size;
264265
int fd;
265266
int i;
266267

267-
fd = vsock_stream_connect(opts->peer_cid, 1234);
268+
if (seqpacket)
269+
fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
270+
else
271+
fd = vsock_stream_connect(opts->peer_cid, 1234);
272+
268273
if (fd < 0) {
269274
perror("connect");
270275
exit(EXIT_FAILURE);
@@ -290,15 +295,20 @@ static void test_stream_msg_peek_client(const struct test_opts *opts)
290295
close(fd);
291296
}
292297

293-
static void test_stream_msg_peek_server(const struct test_opts *opts)
298+
static void test_msg_peek_server(const struct test_opts *opts,
299+
bool seqpacket)
294300
{
295301
unsigned char buf_half[MSG_PEEK_BUF_LEN / 2];
296302
unsigned char buf_normal[MSG_PEEK_BUF_LEN];
297303
unsigned char buf_peek[MSG_PEEK_BUF_LEN];
298304
ssize_t res;
299305
int fd;
300306

301-
fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
307+
if (seqpacket)
308+
fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
309+
else
310+
fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
311+
302312
if (fd < 0) {
303313
perror("accept");
304314
exit(EXIT_FAILURE);
@@ -340,6 +350,21 @@ static void test_stream_msg_peek_server(const struct test_opts *opts)
340350
exit(EXIT_FAILURE);
341351
}
342352

353+
if (seqpacket) {
354+
/* This type of socket supports MSG_TRUNC flag,
355+
* so check it with MSG_PEEK. We must get length
356+
* of the message.
357+
*/
358+
res = recv(fd, buf_half, sizeof(buf_half), MSG_PEEK |
359+
MSG_TRUNC);
360+
if (res != sizeof(buf_peek)) {
361+
fprintf(stderr,
362+
"recv(2) + MSG_PEEK | MSG_TRUNC, exp %zu, got %zi\n",
363+
sizeof(buf_half), res);
364+
exit(EXIT_FAILURE);
365+
}
366+
}
367+
343368
res = recv(fd, buf_normal, sizeof(buf_normal), 0);
344369
if (res != sizeof(buf_normal)) {
345370
fprintf(stderr, "recv(2), expected %zu, got %zi\n",
@@ -356,6 +381,16 @@ static void test_stream_msg_peek_server(const struct test_opts *opts)
356381
close(fd);
357382
}
358383

384+
static void test_stream_msg_peek_client(const struct test_opts *opts)
385+
{
386+
return test_msg_peek_client(opts, false);
387+
}
388+
389+
static void test_stream_msg_peek_server(const struct test_opts *opts)
390+
{
391+
return test_msg_peek_server(opts, false);
392+
}
393+
359394
#define SOCK_BUF_SIZE (2 * 1024 * 1024)
360395
#define MAX_MSG_SIZE (32 * 1024)
361396

@@ -1125,6 +1160,16 @@ static void test_stream_virtio_skb_merge_server(const struct test_opts *opts)
11251160
close(fd);
11261161
}
11271162

1163+
static void test_seqpacket_msg_peek_client(const struct test_opts *opts)
1164+
{
1165+
return test_msg_peek_client(opts, true);
1166+
}
1167+
1168+
static void test_seqpacket_msg_peek_server(const struct test_opts *opts)
1169+
{
1170+
return test_msg_peek_server(opts, true);
1171+
}
1172+
11281173
static struct test_case test_cases[] = {
11291174
{
11301175
.name = "SOCK_STREAM connection reset",
@@ -1200,6 +1245,11 @@ static struct test_case test_cases[] = {
12001245
.run_client = test_stream_virtio_skb_merge_client,
12011246
.run_server = test_stream_virtio_skb_merge_server,
12021247
},
1248+
{
1249+
.name = "SOCK_SEQPACKET MSG_PEEK",
1250+
.run_client = test_seqpacket_msg_peek_client,
1251+
.run_server = test_seqpacket_msg_peek_server,
1252+
},
12031253
{},
12041254
};
12051255

0 commit comments

Comments
 (0)