Skip to content

Commit d80f63f

Browse files
fstorniolodavem330
authored andcommitted
test/vsock: add dobule bind connect test
This add bind connect test which creates a listening server socket and tries to connect a client with a bound local port to it twice. Co-developed-by: Luigi Leonardi <luigi.leonardi@outlook.com> Signed-off-by: Luigi Leonardi <luigi.leonardi@outlook.com> Signed-off-by: Filippo Storniolo <f.storniolo95@gmail.com> Reviewed-by: Stefano Garzarella <sgarzare@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 84d5fb9 commit d80f63f

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

tools/testing/vsock/util.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,48 @@ void vsock_wait_remote_close(int fd)
8585
close(epollfd);
8686
}
8787

88+
/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
89+
int 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+
88130
/* Connect to <cid, port> and return the file descriptor. */
89131
static int vsock_connect(unsigned int cid, unsigned int port, int type)
90132
{
@@ -223,6 +265,11 @@ int vsock_stream_accept(unsigned int cid, unsigned int port,
223265
return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
224266
}
225267

268+
int vsock_stream_listen(unsigned int cid, unsigned int port)
269+
{
270+
return vsock_listen(cid, port, SOCK_STREAM);
271+
}
272+
226273
int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
227274
struct sockaddr_vm *clientaddrp)
228275
{

tools/testing/vsock/util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ struct test_case {
3636
void init_signals(void);
3737
unsigned int parse_cid(const char *str);
3838
int vsock_stream_connect(unsigned int cid, unsigned int port);
39+
int vsock_bind_connect(unsigned int cid, unsigned int port,
40+
unsigned int bind_port, int type);
3941
int vsock_seqpacket_connect(unsigned int cid, unsigned int port);
4042
int vsock_stream_accept(unsigned int cid, unsigned int port,
4143
struct sockaddr_vm *clientaddrp);
44+
int vsock_stream_listen(unsigned int cid, unsigned int port);
4245
int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
4346
struct sockaddr_vm *clientaddrp);
4447
void vsock_wait_remote_close(int fd);

tools/testing/vsock/vsock_test.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,51 @@ static void test_stream_shutrd_server(const struct test_opts *opts)
11801180
close(fd);
11811181
}
11821182

1183+
static void test_double_bind_connect_server(const struct test_opts *opts)
1184+
{
1185+
int listen_fd, client_fd, i;
1186+
struct sockaddr_vm sa_client;
1187+
socklen_t socklen_client = sizeof(sa_client);
1188+
1189+
listen_fd = vsock_stream_listen(VMADDR_CID_ANY, 1234);
1190+
1191+
for (i = 0; i < 2; i++) {
1192+
control_writeln("LISTENING");
1193+
1194+
timeout_begin(TIMEOUT);
1195+
do {
1196+
client_fd = accept(listen_fd, (struct sockaddr *)&sa_client,
1197+
&socklen_client);
1198+
timeout_check("accept");
1199+
} while (client_fd < 0 && errno == EINTR);
1200+
timeout_end();
1201+
1202+
if (client_fd < 0) {
1203+
perror("accept");
1204+
exit(EXIT_FAILURE);
1205+
}
1206+
1207+
/* Waiting for remote peer to close connection */
1208+
vsock_wait_remote_close(client_fd);
1209+
}
1210+
1211+
close(listen_fd);
1212+
}
1213+
1214+
static void test_double_bind_connect_client(const struct test_opts *opts)
1215+
{
1216+
int i, client_fd;
1217+
1218+
for (i = 0; i < 2; i++) {
1219+
/* Wait until server is ready to accept a new connection */
1220+
control_expectln("LISTENING");
1221+
1222+
client_fd = vsock_bind_connect(opts->peer_cid, 1234, 4321, SOCK_STREAM);
1223+
1224+
close(client_fd);
1225+
}
1226+
}
1227+
11831228
static struct test_case test_cases[] = {
11841229
{
11851230
.name = "SOCK_STREAM connection reset",
@@ -1285,6 +1330,11 @@ static struct test_case test_cases[] = {
12851330
.run_client = test_stream_msgzcopy_empty_errq_client,
12861331
.run_server = test_stream_msgzcopy_empty_errq_server,
12871332
},
1333+
{
1334+
.name = "SOCK_STREAM double bind connect",
1335+
.run_client = test_double_bind_connect_client,
1336+
.run_server = test_double_bind_connect_server,
1337+
},
12881338
{},
12891339
};
12901340

0 commit comments

Comments
 (0)