From 325fe82cdfc63699c254030f3a187ccf6413e6d2 Mon Sep 17 00:00:00 2001 From: Haley Cao Date: Wed, 29 Apr 2020 00:25:03 +0000 Subject: [PATCH] Added new test case using sendto and recvfrom functions Added to two_socket_communication test in omrsockTest.cpp - Added test case that involves datagram communication. - Set timeout for sendto and recvfrom to be 2 seconds. - However, because of the inconsistency in receiving the message for datagram communication, the message is sent 50 times and received using recvfrom for up to 50 times until it receives the same number of bytes as it sent. Issue: #5128 Signed-off-by: Haley Cao --- fvtest/porttest/omrsockTest.cpp | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/fvtest/porttest/omrsockTest.cpp b/fvtest/porttest/omrsockTest.cpp index 33df65e9d45..b8305fac537 100644 --- a/fvtest/porttest/omrsockTest.cpp +++ b/fvtest/porttest/omrsockTest.cpp @@ -589,6 +589,40 @@ TEST(PortSockTest, two_socket_datagram_communication) /* Connect is optional for datagram clients */ EXPECT_EQ(connect_client_to_server(OMRPORTLIB, (char *)"localhost", NULL, OMRSOCK_AF_INET, OMRSOCK_DGRAM, &clientSocket, &clientSockAddr, &serverSockAddr), 0); +/* Datagram Sendto and Recvfrom test. Sendto and Recvfrom are called multiple tests to ensure it has been + * sent and received. + */ + OMRTimeval timeVal; + EXPECT_EQ(OMRPORTLIB->sock_timeval_init(OMRPORTLIB, &timeVal, 2, 0), 0); + EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, clientSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeVal), 0); + EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, clientSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeVal), 0); + EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, serverSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeVal), 0); + EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, serverSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeVal), 0); + + /* Send 50 times. This should be enough for the message to be sent. */ + char msgDgram[100] = "This is an omrsock test for datagram communications."; + int32_t bytesLeft = strlen(msgDgram) + 1; + + for (int32_t i = 0; i < 100; i++) { + OMRPORTLIB->sock_sendto(OMRPORTLIB, clientSocket, (uint8_t *)msgDgram, bytesLeft, 0, &serverSockAddr); + } + + /* Receive */ + char bufDgram[100] = ""; + int32_t bytesRecv = 0; + + for (int32_t i = 0; i < 100; i++) { + bytesRecv = OMRPORTLIB->sock_recvfrom(OMRPORTLIB, serverSocket, (uint8_t *)bufDgram, bytesLeft, 0, &clientSockAddr); + if (bytesRecv == bytesLeft) { + break; + } + } + + printf("sent: %s \n", msgDgram); + printf("received: %s \n", bufDgram); + + EXPECT_EQ(strcmp(msgDgram, bufDgram), 0); + EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &clientSocket), 0); EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &serverSocket), 0); }