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 24, 2020
1 parent 0f20428 commit c94eef3
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions fvtest/porttest/omrsockTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,107 @@ TEST(PortSockTest, two_socket_datagram_communication)
EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &serverSocket), 0);
}

/**
* Equal operator to compare contents of timeval struct socket options.
*/
bool operator==(const OMRTimeval& lhs, const OMRTimeval& rhs)
{
if ((lhs.data.tv_sec != rhs.data.tv_sec) || (lhs.data.tv_usec != rhs.data.tv_usec)) {
return false;
}
return true;
}

/**
* Equal operator to compare contents of linger struct socket options.
*/
bool operator==(const OMRLinger& lhs, const OMRLinger& rhs)
{
if ((lhs.data.l_onoff != rhs.data.l_onoff) || (lhs.data.l_linger != rhs.data.l_linger)) {
return false;
}
return true;
}

namespace OMRSock
{
typedef OMRTimeval OMRTimevalTest;
typedef OMRLinger OMRLingerTest;

/**
* Print contents of OMRTimeval struct when comparing contents of linger struct socket options.
*/
void
printTo(const OMRTimevalTest& x, std::ostream *os)
{
*os << "{" << x.data.tv_sec << ", " << x.data.tv_usec << "}";

}

/**
* Print contents of OMRLinger struct when comparing contents of linger struct socket options.
*/
void
printTo(const OMRLingerTest& x, std::ostream *os)
{
*os << "{" << x.data.l_onoff << ", " << x.data.l_linger << "}";
}

/**
* 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());
OMRSock::OMRTimevalTest timeVal;
OMRSock::OMRLingerTest lingerVal;
omrsock_socket_t sock = NULL;

OMRSock::OMRTimevalTest timeTest;
OMRSock::OMRLingerTest lingerTest;
EXPECT_EQ(OMRPORTLIB->sock_timeval_init(OMRPORTLIB, &timeTest, 3, 0), 0);
EXPECT_EQ(OMRPORTLIB->sock_linger_init(OMRPORTLIB, &lingerTest, 1, 3), 0);

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);

EXPECT_EQ(lingerTest, lingerVal);
EXPECT_EQ(timeTest, timeVal);

/* 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. */
OMRSock::OMRTimevalTest timeResult;
OMRSock::OMRLingerTest lingerResult;
int32_t flagResult;

EXPECT_EQ(OMRPORTLIB->sock_getsockopt_int(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_KEEPALIVE, &flagResult), 0);
EXPECT_GE(flagResult, 0);

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

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

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

EXPECT_EQ(OMRPORTLIB->sock_getsockopt_int(OMRPORTLIB, sock, OMRSOCK_IPPROTO_TCP, OMRSOCK_TCP_NODELAY, &flagResult), 0);
EXPECT_GE(flagResult, 0);
}
}

#endif /* defined(OMR_PORT_SOCKET_SUPPORT) */

0 comments on commit c94eef3

Please sign in to comment.