Skip to content

Commit

Permalink
Added socket options related tests to omrsockTest.cpp
Browse files Browse the repository at this point in the history
Added a new test socket_options that calls set/get sockopt

- Added test that initializes timeval and linger structs.
- Created a socket and set options including keepalive, linger,
  rcvtimeo, sndtimeo, tcp_nodelay.
- Get the same options and compare with values inputted into
  set options, and make sure they are the same.

Issue: eclipse-omr#5158

Signed-off-by: Haley Cao <haleycao88@hotmail.com>
  • Loading branch information
Haley Cao committed Jun 16, 2020
1 parent 822760e commit 6b895d3
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions fvtest/porttest/omrsockTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,84 @@ TEST(PortSockTest, two_socket_datagram_communication)
EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &serverSocket), 0);
}

/**
* Compare contents of timeval struct socket options.
*
* @return 0 on success, return OMRPORT_ERROR_OPFAILED otherwise.
*/
int32_t compare_timeval(omrsock_timeval_t A, omrsock_timeval_t B)
{
if ((A->data.tv_sec != B->data.tv_sec) || (A->data.tv_usec != B->data.tv_usec)) {
return OMRPORT_ERROR_OPFAILED;
}
return 0;
}

/**
* Compare contents of linger struct socket options.
*
* @return 0 on success, return OMRPORT_ERROR_OPFAILED otherwise.
*/
int32_t compare_linger(omrsock_linger_t A, omrsock_linger_t B)
{
if ((A->data.l_onoff != B->data.l_onoff) || (A->data.l_linger != B->data.l_linger)) {
return OMRPORT_ERROR_OPFAILED;
}
return 0;
}

/**
* Test socket options functions by test setting and getting the socket options.
*
* First, the linger and timeval structs will be allocated and the corresponding
* init functions will be called to set up those structs. Then, an arbitrary IPv4
* stream socket will be opened and the socket options will be set and get. The
* test will check if the set and get option values are the same.
*/
TEST(PortSockTest, socket_options)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRTimeval timeVal;
OMRLinger lingerVal;
omrsock_socket_t sock = NULL;

EXPECT_EQ(OMRPORTLIB->sock_timeval_init(OMRPORTLIB, &timeVal, 2, 0), 0);
EXPECT_EQ(OMRPORTLIB->sock_linger_init(OMRPORTLIB, &lingerVal, 1, 2), 0);
ASSERT_EQ(OMRPORTLIB->sock_socket(OMRPORTLIB, &sock, OMRSOCK_AF_INET, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT), 0);

/* Call omrsock_setsockopt to set socket options. */
int32_t flag = 1;
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_KEEPALIVE, &flag), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_linger(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_LINGER, &lingerVal), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeVal), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeVal), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, sock, OMRSOCK_IPPROTO_TCP, OMRSOCK_TCP_NODELAY, &flag), 0);

/* Call omrsock_getsockopt to check if the results matches the inputs earlier. */
OMRTimeval timeResult;
OMRLinger lingerResult;
int32_t flagResult;

EXPECT_EQ(OMRPORTLIB->sock_getsockopt_int(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_KEEPALIVE, &flagResult), 0);
if (flagResult > 0)
SUCCEED();
else
FAIL();

EXPECT_EQ(OMRPORTLIB->sock_getsockopt_linger(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_LINGER, &lingerResult), 0);
EXPECT_EQ(compare_linger(&lingerResult, &lingerVal), 0);

EXPECT_EQ(OMRPORTLIB->sock_getsockopt_timeval(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeResult), 0);
EXPECT_EQ(compare_timeval(&timeResult, &timeVal), 0);

EXPECT_EQ(OMRPORTLIB->sock_getsockopt_timeval(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeResult), 0);
EXPECT_EQ(compare_timeval(&timeResult, &timeVal), 0);

EXPECT_EQ(OMRPORTLIB->sock_getsockopt_int(OMRPORTLIB, sock, OMRSOCK_IPPROTO_TCP, OMRSOCK_TCP_NODELAY, &flagResult), 0);
if (flagResult > 0)
SUCCEED();
else
FAIL();
}

#endif /* defined(OMR_PORT_SOCKET_SUPPORT) */

0 comments on commit 6b895d3

Please sign in to comment.