diff --git a/fvtest/porttest/omrsockTest.cpp b/fvtest/porttest/omrsockTest.cpp index 7fc842891bf..8e88c6964b5 100644 --- a/fvtest/porttest/omrsockTest.cpp +++ b/fvtest/porttest/omrsockTest.cpp @@ -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) */