From 9360796744e66d673bda0f927bc407b329b9a921 Mon Sep 17 00:00:00 2001 From: Haley Cao Date: Tue, 21 Jan 2020 04:10:46 +0000 Subject: [PATCH 1/6] Fixed Argument Name in getaddrinfo_length Function Prototype Fixed the name of the argument to match similar functions - Changed name from hints to handle for the omrsock_addrinfo_t pointer. Issue: #4745 Signed-off-by: Haley Cao --- include_core/omrport.h | 2 +- port/common/omrsock.c | 2 +- port/omrportpriv.h | 2 +- port/unix/omrsock.c | 2 +- port/win32/omrsock.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include_core/omrport.h b/include_core/omrport.h index ac671f8cee..b6acc22eb2 100644 --- a/include_core/omrport.h +++ b/include_core/omrport.h @@ -2013,7 +2013,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"*/ diff --git a/port/common/omrsock.c b/port/common/omrsock.c index d3ff75a658..bef7885016 100644 --- a/port/common/omrsock.c +++ b/port/common/omrsock.c @@ -105,7 +105,7 @@ omrsock_getaddrinfo(struct OMRPortLibrary *portLibrary, char *node, char *servic * @return 0, if no errors occurred, otherwise return an error. */ 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; } diff --git a/port/omrportpriv.h b/port/omrportpriv.h index 8b744af35d..932e15e29f 100644 --- a/port/omrportpriv.h +++ b/port/omrportpriv.h @@ -616,7 +616,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 diff --git a/port/unix/omrsock.c b/port/unix/omrsock.c index 1d447022b6..c710371c06 100644 --- a/port/unix/omrsock.c +++ b/port/unix/omrsock.c @@ -80,7 +80,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; } diff --git a/port/win32/omrsock.c b/port/win32/omrsock.c index c6d53aaf4d..2f9e103182 100644 --- a/port/win32/omrsock.c +++ b/port/win32/omrsock.c @@ -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; } From e114e570ff95cd7070b3f626440f4f55b4b39c5c Mon Sep 17 00:00:00 2001 From: Haley Cao Date: Thu, 6 Feb 2020 20:03:23 +0000 Subject: [PATCH 2/6] Fixed Function Prototype Descriptions for Error Codes Added error codes that may be returned for element extractors - Added error code OMRPORT_ERROR_INVALID_ARGUMENTS to reflect user input error and passing in non valid arguments. Issue: #4745 Signed-off-by: Haley Cao --- port/common/omrsock.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/port/common/omrsock.c b/port/common/omrsock.c index bef7885016..906c5fe4a6 100644 --- a/port/common/omrsock.c +++ b/port/common/omrsock.c @@ -102,7 +102,8 @@ 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 handle, uint32_t *length) @@ -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) @@ -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) @@ -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) From aaf999e1e795a9211abdafa0fa04fc6b50010799 Mon Sep 17 00:00:00 2001 From: Haley Cao Date: Tue, 11 Feb 2020 15:11:18 +0000 Subject: [PATCH 3/6] Fixed Create Hints So that Hints has length 1 Added a line in unix/omrsock.c to omrsock_getaddrinfo_create_hints - Hints has the length value of 0 after memset setting Hints to be 0. However, it should be 1 because Hints structure technically has 1 addrinfo node, so it is modified to be 1 now. Issue: #4745 Signed-off-by: Haley Cao --- port/unix/omrsock.c | 1 + 1 file changed, 1 insertion(+) diff --git a/port/unix/omrsock.c b/port/unix/omrsock.c index c710371c06..e160e0cb8d 100644 --- a/port/unix/omrsock.c +++ b/port/unix/omrsock.c @@ -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; } From 6e553024f9368137a853fba0f26cf91af0d93486 Mon Sep 17 00:00:00 2001 From: Haley Cao Date: Tue, 21 Jan 2020 04:29:33 +0000 Subject: [PATCH 4/6] Added Unix Implementations for Extracting Elements of addrInfo To extract length, family, socktype, protocol from addrInfo of OMRAddrInfoNode struct - The OMRAddrInfoNode has two elements, which first element, named addrInfo, is a pointer to the first addrinfo node in a linked list of addrinfo nodes. - The Unix addrinfo structs contains many elements which stores a variety of information about a socket address. - Extracts unix addrinfo elements by passing in a pointer to a OMRAddrInfoNode and the index of which one of the linked list of addrinfo nodes to extract. - Currently implemented the element extractions of what is needed to create a socket. Issue: #4745 Co-authored-by: Babneet Singh Signed-off-by: Haley Cao --- port/unix/omrsock.c | 60 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/port/unix/omrsock.c b/port/unix/omrsock.c index e160e0cb8d..d75b8be11e 100644 --- a/port/unix/omrsock.c +++ b/port/unix/omrsock.c @@ -83,25 +83,75 @@ omrsock_getaddrinfo(struct OMRPortLibrary *portLibrary, char *node, char *servic int32_t 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 From dee210b5d89d88b7aca37c76e4d675b5339ae48e Mon Sep 17 00:00:00 2001 From: Haley Cao Date: Tue, 11 Feb 2020 20:50:42 +0000 Subject: [PATCH 5/6] Modified Test Case Descriptions in omrsockTest.cpp Made changes to descriptions of what the test cases test for - Changed the first test name from per_thread_buffer_functionality to create_hints_and_element_extraction. Changed the second test from getaddrinfo_and_element_extraction to just testing getaddrinfo_and_freeaddrinfo. - Changed descriptions of what the test cases will test. Issue: #4745 Signed-off-by: Haley Cao --- fvtest/porttest/omrsockTest.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/fvtest/porttest/omrsockTest.cpp b/fvtest/porttest/omrsockTest.cpp index 10e48afd29..ccc3701367 100644 --- a/fvtest/porttest/omrsockTest.cpp +++ b/fvtest/porttest/omrsockTest.cpp @@ -94,12 +94,18 @@ 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()); @@ -115,9 +121,8 @@ TEST(PortSockTest, per_thread_buffer_functionality) } /** - * 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 @@ -130,7 +135,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. */ } From 3736838f637c979d350037a9c90a0fbab51048f8 Mon Sep 17 00:00:00 2001 From: Haley Cao Date: Tue, 21 Jan 2020 05:02:26 +0000 Subject: [PATCH 6/6] Added Test Implementation for the Getters of OMRAddrInfoNode Added test implementations for per_thread_buffer_functionality test - Added implementations to first create a hints structure, which relies on the per thread buffer structure and functions. - Added values to hints, which is stored by per thread buffer and the element getters retrieves the values and compare them to values inserted. - Test case checks for if the correct error code has been returned in cases where there are errors such as invalid arguments. - Test cases also checks for overwriting of the hints per thread buffer, and see if it overwrites correctly. Issue: #4745 Signed-off-by: Haley Cao --- fvtest/porttest/omrsockTest.cpp | 108 ++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 4 deletions(-) diff --git a/fvtest/porttest/omrsockTest.cpp b/fvtest/porttest/omrsockTest.cpp index ccc3701367..2e3e56840e 100644 --- a/fvtest/porttest/omrsockTest.cpp +++ b/fvtest/porttest/omrsockTest.cpp @@ -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" @@ -110,14 +111,113 @@ 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; - OMRPORTLIB->sock_getaddrinfo_create_hints(OMRPORTLIB, &hints, family, sockType, protocol, flags); + int32_t hintsFamily = 0; + int32_t hintsSockType = 1; + int32_t hintsProtocol = 1; + int32_t hintsFlags = 0; + + 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); } /**