Skip to content

Commit

Permalink
Merge pull request #4738 from caohaley/getters
Browse files Browse the repository at this point in the history
Added Unix Implementations for Element Extracting Functions in OMRSOCK API
  • Loading branch information
rwy7 authored Feb 21, 2020
2 parents cbbd02d + 3736838 commit 00c57af
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 25 deletions.
127 changes: 116 additions & 11 deletions fvtest/porttest/omrsockTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "omrcfg.h"
#if defined(OMR_PORT_SOCKET_SUPPORT)
#include "omrport.h"
#include "omrporterror.h"
#include "omrportsock.h"
#include "omrportsocktypes.h"
#include "testHelpers.hpp"
Expand Down Expand Up @@ -94,30 +95,134 @@ TEST(PortSockTest, library_function_pointers_not_null)
}

/**
* Test the omrsock per thread buffer.
* Test the omrsock per thread buffer using @ref omrsock_getaddrinfo_create_hints
* and all functions that extract details from the per thread buffer.
*
* In this test, per thread buffer related functions set up a buffer to store
* information such as the hints structures created in @ref omrsock_getaddrinfo_create_hints.
* information such as the hints structures created in
* @ref omrsock_getaddrinfo_create_hints. Since the per thread buffer can't be easily
* directly tested, it will be tested with @ref omrsock_getaddrinfo_create_hints.
*
* @note Errors such as invalid arguments, and/or returning the wrong family or
* socket type from hints compared to the ones passed into hints, will be reported.
*/
TEST(PortSockTest, per_thread_buffer_functionality)
TEST(PortSockTest, create_hints_and_element_extraction)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());

omrsock_addrinfo_t hints = NULL;
uint32_t length = 0;
int32_t family = 0;
int32_t sockType = 1;
int32_t protocol = 1;
int32_t flags = 0;
int32_t sockType = 0;
int32_t protocol = 0;

int32_t hintsFamily = 0;
int32_t hintsSockType = 1;
int32_t hintsProtocol = 1;
int32_t hintsFlags = 0;

OMRPORTLIB->sock_getaddrinfo_create_hints(OMRPORTLIB, &hints, family, sockType, protocol, flags);
OMRPORTLIB->sock_getaddrinfo_create_hints(OMRPORTLIB, &hints, hintsFamily, hintsSockType, hintsProtocol, hintsFlags);

ASSERT_NE(hints, (void *)NULL);

/* Testing invalid arguments: Pointer to OMRAddrInfoNode that is NULL */

int32_t rc;
rc = OMRPORTLIB->sock_getaddrinfo_length(OMRPORTLIB, NULL, &length);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

rc = OMRPORTLIB->sock_getaddrinfo_family(OMRPORTLIB, NULL, &family, 0);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

rc = OMRPORTLIB->sock_getaddrinfo_socktype(OMRPORTLIB, NULL, &sockType, 0);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

rc = OMRPORTLIB->sock_getaddrinfo_protocol(OMRPORTLIB, NULL, &protocol, 0);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

/* Testing invalid arguments: Index is bigger than the length when querying */

rc = OMRPORTLIB->sock_getaddrinfo_length(OMRPORTLIB, hints, &length);
EXPECT_EQ(rc, 0);

rc = OMRPORTLIB->sock_getaddrinfo_family(OMRPORTLIB, hints, &family, length);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

rc = OMRPORTLIB->sock_getaddrinfo_socktype(OMRPORTLIB, hints, &sockType, length);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

rc = OMRPORTLIB->sock_getaddrinfo_protocol(OMRPORTLIB, hints, &protocol, length);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

/* Testing invalid arguments: Index is smaller than 0 when querying */

rc = OMRPORTLIB->sock_getaddrinfo_family(OMRPORTLIB, hints, &family, -1);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

rc = OMRPORTLIB->sock_getaddrinfo_socktype(OMRPORTLIB, hints, &sockType, -1);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

rc = OMRPORTLIB->sock_getaddrinfo_protocol(OMRPORTLIB, hints, &protocol, -1);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

/* Get and verify elements of the newly created hints. */

rc = OMRPORTLIB->sock_getaddrinfo_length(OMRPORTLIB, hints, &length);
EXPECT_EQ(rc, 0);
EXPECT_EQ(length, 1);

rc = OMRPORTLIB->sock_getaddrinfo_family(OMRPORTLIB, hints, &family, 0);
EXPECT_EQ(rc, 0);
EXPECT_EQ(family, hintsFamily);

rc = OMRPORTLIB->sock_getaddrinfo_socktype(OMRPORTLIB, hints, &sockType, 0);
EXPECT_EQ(rc, 0);
EXPECT_EQ(sockType, hintsSockType);

rc = OMRPORTLIB->sock_getaddrinfo_protocol(OMRPORTLIB, hints, &protocol, 0);
EXPECT_EQ(rc, 0);
EXPECT_EQ(protocol, hintsProtocol);

/* Recreate hints with different parameters, see if hints elements are overwriten properly. */

hintsProtocol = 0;
hintsFlags = 6;

OMRPORTLIB->sock_getaddrinfo_create_hints(OMRPORTLIB, &hints, hintsFamily, hintsSockType, hintsProtocol, hintsFlags);

rc = OMRPORTLIB->sock_getaddrinfo_length(OMRPORTLIB, hints, &length);
EXPECT_EQ(rc, 0);
EXPECT_EQ(length, 1);

rc = OMRPORTLIB->sock_getaddrinfo_family(OMRPORTLIB, hints, &family, 0);
EXPECT_EQ(rc, 0);
EXPECT_EQ(family, hintsFamily);

rc = OMRPORTLIB->sock_getaddrinfo_socktype(OMRPORTLIB, hints, &sockType, 0);
EXPECT_EQ(rc, 0);
EXPECT_EQ(sockType, hintsSockType);

rc = OMRPORTLIB->sock_getaddrinfo_protocol(OMRPORTLIB, hints, &protocol, 0);
EXPECT_EQ(rc, 0);
EXPECT_EQ(protocol, hintsProtocol);

/* Testing invalid arguments: User exposes API and changes length, then query. */

hints->length = 5;

rc = OMRPORTLIB->sock_getaddrinfo_family(OMRPORTLIB, hints, &family, 3);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

rc = OMRPORTLIB->sock_getaddrinfo_socktype(OMRPORTLIB, hints, &sockType, 3);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);

rc = OMRPORTLIB->sock_getaddrinfo_protocol(OMRPORTLIB, hints, &protocol, 3);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
}

/**
* Test @ref omrsock_getaddrinfo_create_hints, @ref omrsock_getaddrinfo,
* @ref omrsock_freeaddrinfo and all functions that extract details from
* @ref omrsock_getaddrinfo results.
* Test @ref omrsock_getaddrinfo, @ref omrsock_freeaddrinfo and all functions
* that extract details from @ref omrsock_getaddrinfo results.
*
* In this test, the relevant variables are passed into
* @ref omrsock_getaddrinfo_create_hints. The generated hints are passed into
Expand All @@ -130,7 +235,7 @@ TEST(PortSockTest, per_thread_buffer_functionality)
* socket type from @ref omrsock_getaddrinfo compared to the ones passed into hints,
* will be reported.
*/
TEST(PortSockTest, getaddrinfo_creation_and_extraction)
TEST(PortSockTest, getaddrinfo_and_freeaddrinfo)
{
/* Unimplemented. */
}
Expand Down
2 changes: 1 addition & 1 deletion include_core/omrport.h
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,7 @@ typedef struct OMRPortLibrary {
/** see @ref omrsock.c::omrsock_getaddrinfo "omrsock_getaddrinfo"*/
int32_t (*sock_getaddrinfo)(struct OMRPortLibrary *portLibrary, char *node, char *service, omrsock_addrinfo_t hints, omrsock_addrinfo_t result) ;
/** see @ref omrsock.c::omrsock_getaddrinfo_length "omrsock_getaddrinfo_length"*/
int32_t (*sock_getaddrinfo_length)(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t hints, uint32_t *length) ;
int32_t (*sock_getaddrinfo_length)(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, uint32_t *length) ;
/** see @ref omrsock.c::omrsock_getaddrinfo_family "omrsock_getaddrinfo_family"*/
int32_t (*sock_getaddrinfo_family)(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, int32_t *family, int32_t index ) ;
/** see @ref omrsock.c::omrsock_getaddrinfo_socktype "omrsock_getaddrinfo_socktype"*/
Expand Down
14 changes: 9 additions & 5 deletions port/common/omrsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ omrsock_getaddrinfo(struct OMRPortLibrary *portLibrary, char *node, char *servic
* @param[in] handle The result structure returned by @ref omrsock_getaddrinfo.
* @param[out] length The number of results.
*
* @return 0, if no errors occurred, otherwise return an error.
* @return 0, if no errors occurred, otherwise return an error. Error code values returned are
* \arg OMRPORT_ERROR_INVALID_ARGUMENTS when handle or index arguments are invalid.
*/
int32_t
omrsock_getaddrinfo_length(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t hints, uint32_t *length)
omrsock_getaddrinfo_length(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, uint32_t *length)
{
return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM;
}
Expand All @@ -119,7 +120,8 @@ omrsock_getaddrinfo_length(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_
* @param[out] family The family at "index".
* @param[in] index The index into the structure returned by @ref omrsock_getaddrinfo.
*
* @return 0, if no errors occurred, otherwise return an error.
* @return 0, if no errors occurred, otherwise return an error. Error code values returned are
* \arg OMRPORT_ERROR_INVALID_ARGUMENTS when handle or index arguments are invalid.
*/
int32_t
omrsock_getaddrinfo_family(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, int32_t *family, int32_t index)
Expand All @@ -136,7 +138,8 @@ omrsock_getaddrinfo_family(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_
* @param[out] socktype The socket type at "index".
* @param[in] index The index into the structure returned by @ref omrsock_getaddrinfo.
*
* @return 0, if no errors occurred, otherwise return an error.
* @return 0, if no errors occurred, otherwise return an error. Error code values returned are
* \arg OMRPORT_ERROR_INVALID_ARGUMENTS when handle or index arguments are invalid.
*/
int32_t
omrsock_getaddrinfo_socktype(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, int32_t *socktype, int32_t index)
Expand All @@ -153,7 +156,8 @@ omrsock_getaddrinfo_socktype(struct OMRPortLibrary *portLibrary, omrsock_addrinf
* @param[out] protocol The protocol family at "index".
* @param[in] index The index into the structure returned by @ref omrsock_getaddrinfo.
*
* @return 0, if no errors occurred, otherwise return an error.
* @return 0, if no errors occurred, otherwise return an error. Error code values returned are
* \arg OMRPORT_ERROR_INVALID_ARGUMENTS when handle or index arguments are invalid.
*/
int32_t
omrsock_getaddrinfo_protocol(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, int32_t *protocol, int32_t index)
Expand Down
2 changes: 1 addition & 1 deletion port/omrportpriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ omrsock_getaddrinfo_create_hints(struct OMRPortLibrary *portLibrary, omrsock_add
extern J9_CFUNC int32_t
omrsock_getaddrinfo(struct OMRPortLibrary *portLibrary, char *node, char *service, omrsock_addrinfo_t hints, omrsock_addrinfo_t result);
extern J9_CFUNC int32_t
omrsock_getaddrinfo_length(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t hints, uint32_t *length);
omrsock_getaddrinfo_length(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, uint32_t *length);
extern J9_CFUNC int32_t
omrsock_getaddrinfo_family(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, int32_t *family, int32_t index);
extern J9_CFUNC int32_t
Expand Down
63 changes: 57 additions & 6 deletions port/unix/omrsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ omrsock_getaddrinfo_create_hints(struct OMRPortLibrary *portLibrary, omrsock_add
ptbHints->ai_protocol = protocol;

(ptBuffer->addrInfoHints).addrInfo = ptbHints;
(ptBuffer->addrInfoHints).length = 1;
*hints = &ptBuffer->addrInfoHints;
return 0;
}
Expand All @@ -80,27 +81,77 @@ omrsock_getaddrinfo(struct OMRPortLibrary *portLibrary, char *node, char *servic
}

int32_t
omrsock_getaddrinfo_length(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t hints, uint32_t *length)
omrsock_getaddrinfo_length(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, uint32_t *length)
{
return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM;
if (NULL == handle) {
return OMRPORT_ERROR_INVALID_ARGUMENTS;
}

*length = handle->length;
return 0;
}

int32_t
omrsock_getaddrinfo_family(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, int32_t *family, int32_t index)
{
return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM;
{
if ((NULL == handle) || (NULL == handle->addrInfo) || (index < 0) || (index >= handle->length)) {
return OMRPORT_ERROR_INVALID_ARGUMENTS;
}

omr_os_addrinfo *info = handle->addrInfo;
int32_t i = 0;

for (i = 0; i < index; i++) {
info = info->ai_next;
if (NULL == info) {
return OMRPORT_ERROR_INVALID_ARGUMENTS;
}
}

*family = info->ai_family;
return 0;
}

int32_t
omrsock_getaddrinfo_socktype(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, int32_t *socktype, int32_t index)
{
return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM;
if ((NULL == handle) || (NULL == handle->addrInfo) || (index < 0) || (index >= handle->length)) {
return OMRPORT_ERROR_INVALID_ARGUMENTS;
}

omr_os_addrinfo *info = handle->addrInfo;
int32_t i = 0;

for (i = 0; i < index; i++) {
info = info->ai_next;
if (NULL == info) {
return OMRPORT_ERROR_INVALID_ARGUMENTS;
}
}

*socktype = info->ai_socktype;
return 0;
}

int32_t
omrsock_getaddrinfo_protocol(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, int32_t *protocol, int32_t index)
{
return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM;
if ((NULL == handle) || (NULL == handle->addrInfo) || (index < 0) || (index >= handle->length)) {
return OMRPORT_ERROR_INVALID_ARGUMENTS;
}

omr_os_addrinfo *info = handle->addrInfo;
int32_t i = 0;

for (i = 0; i < index; i++) {
info = info->ai_next;
if (NULL == info) {
return OMRPORT_ERROR_INVALID_ARGUMENTS;
}
}

*protocol = info->ai_protocol;
return 0;
}

int32_t
Expand Down
2 changes: 1 addition & 1 deletion port/win32/omrsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ omrsock_getaddrinfo(struct OMRPortLibrary *portLibrary, char *node, char *servic
}

int32_t
omrsock_getaddrinfo_length(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t hints, uint32_t *length)
omrsock_getaddrinfo_length(struct OMRPortLibrary *portLibrary, omrsock_addrinfo_t handle, uint32_t *length)
{
return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM;
}
Expand Down

0 comments on commit 00c57af

Please sign in to comment.