From 1ff1a46a6ef7a5469fb13695ec83c7cfb178537f Mon Sep 17 00:00:00 2001 From: Pavel Emelyanov Date: Fri, 2 Mar 2012 15:46:34 +0400 Subject: [PATCH] zdtm: Test for udp sockets Create two of them, bind both and connect one end to the other. Then check that send and sendto work and recv(from) receive proper message from proper address. Queues are expected to be dropped while test according to protocol constraints, thus all sends happen after restore. Signed-off-by: Pavel Emelyanov Signed-off-by: Cyrill Gorcunov --- test/zdtm.sh | 1 + test/zdtm/live/static/Makefile | 1 + test/zdtm/live/static/socket_udp.c | 129 +++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 test/zdtm/live/static/socket_udp.c diff --git a/test/zdtm.sh b/test/zdtm.sh index bc3d6c603..85e7ec0b0 100644 --- a/test/zdtm.sh +++ b/test/zdtm.sh @@ -31,6 +31,7 @@ static/pstree static/caps00 static/cmdlinenv00 static/socket_listen +static/socket_udp static/selfexe00 streaming/socket_loop00 " diff --git a/test/zdtm/live/static/Makefile b/test/zdtm/live/static/Makefile index e434e2592..69bc3ffca 100644 --- a/test/zdtm/live/static/Makefile +++ b/test/zdtm/live/static/Makefile @@ -19,6 +19,7 @@ TST_NOFILE = \ timers \ unbound_sock \ socket_listen \ + socket_udp \ socket_aio \ msgque \ inotify_system \ diff --git a/test/zdtm/live/static/socket_udp.c b/test/zdtm/live/static/socket_udp.c new file mode 100644 index 000000000..4694fce50 --- /dev/null +++ b/test/zdtm/live/static/socket_udp.c @@ -0,0 +1,129 @@ +#include "zdtmtst.h" + +const char *test_doc = "static test for UDP socket\n"; +const char *test_author = "Pavel Emelyanov \n"; + +/* Description: + * Create two tcp socket, server send asynchronous request on + * read data and clietn write data after migration + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* for sockaddr_in and inet_ntoa() */ +#include + +static int port = 8880; +static char buf[8]; + +#define MSG1 "msg1" +#define MSG2 "msg_2" + +int main(int argc, char **argv) +{ + int ret, sk1, sk2; + socklen_t len = sizeof(struct sockaddr_in); + struct sockaddr_in addr1, addr2, addr; + + test_init(argc, argv); + + sk1 = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sk1 < 0) { + err("Can't create socket"); + return 1; + } + + memset(&addr1, 0, sizeof(addr1)); + addr1.sin_family = AF_INET; + addr1.sin_addr.s_addr = inet_addr("127.0.0.1"); + addr1.sin_port = htons(port); + + ret = bind(sk1, (struct sockaddr *)&addr1, len); + if (ret < 0) { + err("Can't bind socket"); + return 1; + } + + sk2 = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sk2 < 0) { + err("Can't create socket"); + return 1; + } + + memset(&addr2, 0, sizeof(addr1)); + addr2.sin_family = AF_INET; + addr2.sin_addr.s_addr = inet_addr("127.0.0.1"); + addr2.sin_port = htons(port + 1); + + ret = bind(sk2, (struct sockaddr *)&addr2, len); + if (ret < 0) { + err("Can't bind socket"); + return 1; + } + + ret = connect(sk2, (struct sockaddr *)&addr1, len); + if (ret < 0) { + err("Can't connect"); + return 1; + } + + test_daemon(); + test_waitsig(); + + ret = sendto(sk1, MSG1, sizeof(MSG1), 0, + (struct sockaddr *)&addr2, len); + if (ret < 0) { + fail("Can't send"); + return 1; + } + + ret = send(sk2, MSG2, sizeof(MSG2), 0); + if (ret < 0) { + fail("Can't send C"); + return 1; + } + + ret = recvfrom(sk1, buf, sizeof(buf), MSG_DONTWAIT, + (struct sockaddr *)&addr, &len); + if (ret <= 0) { + fail("Can't recv C"); + return 1; + } + + if (len != sizeof(struct sockaddr_in) || memcmp(&addr2, &addr, len)) { + fail("Wrong peer C"); + return 1; + } + + if (ret != sizeof(MSG2) || memcmp(buf, MSG2, ret)) { + fail("Wrong message C"); + return 1; + } + + ret = recvfrom(sk2, buf, sizeof(buf), MSG_DONTWAIT, + (struct sockaddr *)&addr, &len); + if (ret <= 0) { + fail("Can't recv"); + return 1; + } + + if (len != sizeof(struct sockaddr_in) || memcmp(&addr1, &addr, len)) { + fail("Wrong peer"); + return 1; + } + + if (ret != sizeof(MSG1) || memcmp(buf, MSG1, ret)) { + fail("Wrong message"); + return 1; + } + + pass(); + return 0; +}