diff --git a/src/os/inc/osapi-sockets.h b/src/os/inc/osapi-sockets.h index 450fa561d..9f4572da7 100644 --- a/src/os/inc/osapi-sockets.h +++ b/src/os/inc/osapi-sockets.h @@ -145,11 +145,13 @@ typedef struct * * The address is set to a suitable default value for the family. * - * @param[out] Addr The address buffer to initialize + * @param[out] Addr The address buffer to initialize @nonnull * @param[in] Domain The address family * * @return Execution status, see @ref OSReturnCodes + * @retval #OS_SUCCESS @copybrief OS_SUCCESS * @retval #OS_INVALID_POINTER if Addr argument is NULL + * @retval #OS_ERR_NOT_IMPLEMENTED if the system does not implement the requested domain */ int32 OS_SocketAddrInit(OS_SockAddr_t *Addr, OS_SocketDomain_t Domain); @@ -165,13 +167,15 @@ int32 OS_SocketAddrInit(OS_SockAddr_t *Addr, OS_SocketDomain_t Domain); * * @note For IPv4, this would typically be the dotted-decimal format (X.X.X.X). * - * @param[out] buffer Buffer to hold the output string - * @param[in] buflen Maximum length of the output string - * @param[in] Addr The network address buffer to convert + * @param[out] buffer Buffer to hold the output string @nonnull + * @param[in] buflen Maximum length of the output string @nonzero + * @param[in] Addr The network address buffer to convert @nonnull * * @return Execution status, see @ref OSReturnCodes + * @retval #OS_SUCCESS @copybrief OS_SUCCESS * @retval #OS_INVALID_POINTER if argument is NULL * @retval #OS_ERR_INVALID_SIZE if passed-in buflen is not valid + * @retval #OS_ERROR if the address cannot be converted to string, or string buffer too small */ int32 OS_SocketAddrToString(char *buffer, size_t buflen, const OS_SockAddr_t *Addr); @@ -190,11 +194,13 @@ int32 OS_SocketAddrToString(char *buffer, size_t buflen, const OS_SockAddr_t *Ad * Since many embedded deployments do not have name services, this should * not be relied upon. * - * @param[out] Addr The address buffer to initialize - * @param[in] string The string to initialize the address from. + * @param[out] Addr The address buffer to initialize @nonnull + * @param[in] string The string to initialize the address from @nonnull * * @return Execution status, see @ref OSReturnCodes + * @retval #OS_SUCCESS @copybrief OS_SUCCESS * @retval #OS_INVALID_POINTER if argument is NULL + * @retval #OS_ERROR if the string cannot be converted to an address */ int32 OS_SocketAddrFromString(OS_SockAddr_t *Addr, const char *string); @@ -206,11 +212,13 @@ int32 OS_SocketAddrFromString(OS_SockAddr_t *Addr, const char *string); * as TCP/IP and UDP/IP) this function gets the port number from the * address structure. * - * @param[out] PortNum Buffer to store the port number - * @param[in] Addr The network address buffer + * @param[out] PortNum Buffer to store the port number @nonnull + * @param[in] Addr The network address buffer @nonnull * * @return Execution status, see @ref OSReturnCodes + * @retval #OS_SUCCESS @copybrief OS_SUCCESS * @retval #OS_INVALID_POINTER if argument is NULL + * @retval #OS_ERR_BAD_ADDRESS if the address domain is not compatible */ int32 OS_SocketAddrGetPort(uint16 *PortNum, const OS_SockAddr_t *Addr); @@ -222,11 +230,13 @@ int32 OS_SocketAddrGetPort(uint16 *PortNum, const OS_SockAddr_t *Addr); * as TCP/IP and UDP/IP) this function sets the port number from the * address structure. * + * @param[out] Addr The network address buffer @nonnull * @param[in] PortNum The port number to set - * @param[out] Addr The network address buffer * * @return Execution status, see @ref OSReturnCodes + * @retval #OS_SUCCESS @copybrief OS_SUCCESS * @retval #OS_INVALID_POINTER if argument is NULL + * @retval #OS_ERR_BAD_ADDRESS if the address domain is not compatible */ int32 OS_SocketAddrSetPort(OS_SockAddr_t *Addr, uint16 PortNum); /**@}*/ @@ -253,12 +263,14 @@ int32 OS_SocketAddrSetPort(OS_SockAddr_t *Addr, uint16 PortNum); * * A new, unconnected and unbound socket is allocated of the given domain and type. * - * @param[out] sock_id Buffer to hold the non-zero OSAL ID + * @param[out] sock_id Buffer to hold the non-zero OSAL ID @nonnull * @param[in] Domain The domain / address family of the socket (INET or INET6, etc) * @param[in] Type The type of the socket (STREAM or DATAGRAM) * * @return Execution status, see @ref OSReturnCodes + * @retval #OS_SUCCESS @copybrief OS_SUCCESS * @retval #OS_INVALID_POINTER if argument is NULL + * @retval #OS_ERR_NOT_IMPLEMENTED if the system does not implement the requested socket/address domain */ int32 OS_SocketOpen(osal_id_t *sock_id, OS_SocketDomain_t Domain, OS_SocketType_t Type); @@ -274,10 +286,14 @@ int32 OS_SocketOpen(osal_id_t *sock_id, OS_SocketDomain_t Domain, OS_SocketType_ * socket into a listening state for incoming connections at the local address. * * @param[in] sock_id The socket ID - * @param[in] Addr The local address to bind to + * @param[in] Addr The local address to bind to @nonnull * * @return Execution status, see @ref OSReturnCodes + * @retval #OS_SUCCESS @copybrief OS_SUCCESS + * @retval #OS_ERR_INVALID_ID if the sock_id parameter is not valid * @retval #OS_INVALID_POINTER if argument is NULL + * @retval #OS_ERR_INCORRECT_OBJ_STATE if the socket is already bound + * @retval #OS_ERR_INCORRECT_OBJ_TYPE if the handle is not a socket */ int32 OS_SocketBind(osal_id_t sock_id, const OS_SockAddr_t *Addr); @@ -290,10 +306,14 @@ int32 OS_SocketBind(osal_id_t sock_id, const OS_SockAddr_t *Addr); * socket will return an error (these sockets should use SendTo/RecvFrom). * * @param[in] sock_id The socket ID - * @param[in] Addr The remote address to connect to + * @param[in] Addr The remote address to connect to @nonnull * @param[in] timeout The maximum amount of time to wait, or OS_PEND to wait forever * * @return Execution status, see @ref OSReturnCodes + * @retval #OS_SUCCESS @copybrief OS_SUCCESS + * @retval #OS_ERR_INCORRECT_OBJ_STATE if the socket is already connected + * @retval #OS_ERR_INVALID_ID if the sock_id parameter is not valid + * @retval #OS_ERR_INCORRECT_OBJ_TYPE if the handle is not a socket * @retval #OS_INVALID_POINTER if Addr argument is NULL */ int32 OS_SocketConnect(osal_id_t sock_id, const OS_SockAddr_t *Addr, int32 timeout); @@ -309,6 +329,11 @@ int32 OS_SocketConnect(osal_id_t sock_id, const OS_SockAddr_t *Addr, int32 timeo * @param[in] Mode Whether to shutdown reading, writing, or both. * * @return Execution status, see @ref OSReturnCodes + * @retval #OS_SUCCESS @copybrief OS_SUCCESS + * @retval #OS_ERR_INVALID_ID if the sock_id parameter is not valid + * @retval #OS_ERR_INVALID_ARGUMENT if the Mode argument is not one of the valid options + * @retval #OS_ERR_INCORRECT_OBJ_TYPE if the handle is not a socket + * @retval #OS_ERR_INCORRECT_OBJ_STATE if the socket is not connected */ int32 OS_SocketShutdown(osal_id_t sock_id, OS_SocketShutdownMode_t Mode); @@ -325,12 +350,16 @@ int32 OS_SocketShutdown(osal_id_t sock_id, OS_SocketShutdownMode_t Mode); * server socket ID can be reused for the next connection. * * @param[in] sock_id The server socket ID, previously bound using OS_SocketBind() - * @param[out] connsock_id The connection socket, a new ID that can be read/written - * @param[in] Addr The remote address of the incoming connection + * @param[out] connsock_id The connection socket, a new ID that can be read/written @nonnull + * @param[in] Addr The remote address of the incoming connection @nonnull * @param[in] timeout The maximum amount of time to wait, or OS_PEND to wait forever * * @return Execution status, see @ref OSReturnCodes + * @retval #OS_SUCCESS @copybrief OS_SUCCESS * @retval #OS_INVALID_POINTER if argument is NULL + * @retval #OS_ERR_INVALID_ID if the sock_id parameter is not valid + * @retval #OS_ERR_INCORRECT_OBJ_TYPE if the handle is not a socket + * @retval #OS_ERR_INCORRECT_OBJ_STATE if the socket is not bound or already connected */ int32 OS_SocketAccept(osal_id_t sock_id, osal_id_t *connsock_id, OS_SockAddr_t *Addr, int32 timeout); @@ -342,14 +371,16 @@ int32 OS_SocketAccept(osal_id_t sock_id, osal_id_t *connsock_id, OS_SockAddr_t * * that data without blocking. Otherwise, it may block up to the given timeout. * * @param[in] sock_id The socket ID, previously bound using OS_SocketBind() - * @param[out] buffer Pointer to message data receive buffer - * @param[in] buflen The maximum length of the message data to receive + * @param[out] buffer Pointer to message data receive buffer @nonnull + * @param[in] buflen The maximum length of the message data to receive @nonzero * @param[out] RemoteAddr Buffer to store the remote network address (may be NULL) * @param[in] timeout The maximum amount of time to wait, or OS_PEND to wait forever * * @return Count of actual bytes received or error status, see @ref OSReturnCodes * @retval #OS_INVALID_POINTER if argument is NULL * @retval #OS_ERR_INVALID_SIZE if passed-in buflen is not valid + * @retval #OS_ERR_INVALID_ID if the sock_id parameter is not valid + * @retval #OS_ERR_INCORRECT_OBJ_TYPE if the handle is not a socket */ int32 OS_SocketRecvFrom(osal_id_t sock_id, void *buffer, size_t buflen, OS_SockAddr_t *RemoteAddr, int32 timeout); @@ -362,13 +393,15 @@ int32 OS_SocketRecvFrom(osal_id_t sock_id, void *buffer, size_t buflen, OS_SockA * an error code. * * @param[in] sock_id The socket ID, which must be of the datagram type - * @param[in] buffer Pointer to message data to send - * @param[in] buflen The length of the message data to send + * @param[in] buffer Pointer to message data to send @nonnull + * @param[in] buflen The length of the message data to send @nonzero * @param[in] RemoteAddr Buffer containing the remote network address to send to * * @return Count of actual bytes sent or error status, see @ref OSReturnCodes * @retval #OS_INVALID_POINTER if argument is NULL * @retval #OS_ERR_INVALID_SIZE if passed-in buflen is not valid + * @retval #OS_ERR_INVALID_ID if the sock_id parameter is not valid + * @retval #OS_ERR_INCORRECT_OBJ_TYPE if the handle is not a socket */ int32 OS_SocketSendTo(osal_id_t sock_id, const void *buffer, size_t buflen, const OS_SockAddr_t *RemoteAddr); @@ -380,8 +413,8 @@ int32 OS_SocketSendTo(osal_id_t sock_id, const void *buffer, size_t buflen, cons * * @sa OS_SocketGetInfo() * - * @param[out] sock_id Buffer to hold result - * @param[in] sock_name Name of socket to find + * @param[out] sock_id Buffer to hold result @nonnull + * @param[in] sock_name Name of socket to find @nonnull * * @return Execution status, see @ref OSReturnCodes * @retval #OS_SUCCESS @copybrief OS_SUCCESS @@ -399,7 +432,7 @@ int32 OS_SocketGetIdByName(osal_id_t *sock_id, const char *sock_name); * This allows applications to find the name of a given socket. * * @param[in] sock_id The socket ID - * @param[out] sock_prop Buffer to hold socket information + * @param[out] sock_prop Buffer to hold socket information @nonnull * * @return Execution status, see @ref OSReturnCodes * @retval #OS_SUCCESS @copybrief OS_SUCCESS diff --git a/src/os/portable/os-impl-bsd-sockets.c b/src/os/portable/os-impl-bsd-sockets.c index 39ae4fa6b..e7da995cb 100644 --- a/src/os/portable/os-impl-bsd-sockets.c +++ b/src/os/portable/os-impl-bsd-sockets.c @@ -733,7 +733,8 @@ int32 OS_SocketAddrFromString_Impl(OS_SockAddr_t *Addr, const char *string) break; } - if (inet_pton(Accessor->sa.sa_family, string, addrbuffer) < 0) + /* This function is defined as returning 1 on success, not 0 */ + if (inet_pton(Accessor->sa.sa_family, string, addrbuffer) != 1) { return OS_ERROR; } diff --git a/src/tests/network-api-test/network-api-test.c b/src/tests/network-api-test/network-api-test.c index 2eaaccc39..846679088 100644 --- a/src/tests/network-api-test/network-api-test.c +++ b/src/tests/network-api-test/network-api-test.c @@ -59,6 +59,8 @@ enum UT_STREAM_CONNECTION_MAX }; +osal_id_t fs_id; +osal_id_t regular_file_id; osal_id_t s_task_id; osal_id_t p1_socket_id; osal_id_t p2_socket_id; @@ -70,7 +72,9 @@ OS_SockAddr_t s_addr; OS_SockAddr_t c_addr; bool networkImplemented = true; -char ServerFn_ErrorString[128]; +int32 ServerFn_AcceptStatus; +char ServerFn_ErrorString[128]; +char LongName[OS_MAX_API_NAME + 5]; /***************************************************************************** * @@ -78,13 +82,78 @@ char ServerFn_ErrorString[128]; * *****************************************************************************/ -void TestDatagramNetworkApi_Setup(void) +void TestNetworkApiBadArgs(void) +{ + OS_SockAddr_t addr; + osal_id_t socket_id; + uint16 port; + char addr_string[20]; + + memset(&addr, 0, sizeof(addr)); + + UtAssert_INT32_EQ(OS_SocketOpen(NULL, OS_SocketDomain_INVALID, OS_SocketType_INVALID), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketOpen(&socket_id, OS_SocketDomain_MAX, OS_SocketType_MAX), OS_ERR_NOT_IMPLEMENTED); + + UtAssert_INT32_EQ(OS_SocketAddrInit(&addr, OS_SocketDomain_INVALID), OS_ERR_NOT_IMPLEMENTED); + UtAssert_INT32_EQ(OS_SocketAddrInit(NULL, OS_SocketDomain_INVALID), OS_INVALID_POINTER); + + /* OS_SocketAddrSetPort */ + UtAssert_INT32_EQ(OS_SocketAddrSetPort(&addr, 0xFFFF), OS_ERR_BAD_ADDRESS); + UtAssert_INT32_EQ(OS_SocketAddrSetPort(NULL, 1234), OS_INVALID_POINTER); + + /* OS_SocketAddrGetPort */ + UtAssert_INT32_EQ(OS_SocketAddrGetPort(&port, &addr), OS_ERR_BAD_ADDRESS); + UtAssert_INT32_EQ(OS_SocketAddrGetPort(NULL, &addr), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketAddrGetPort(&port, NULL), OS_INVALID_POINTER); + + /* init the addr value to be INET for to/from string tests */ + UtAssert_INT32_EQ(OS_SocketAddrInit(&addr, OS_SocketDomain_INET), OS_SUCCESS); + + /* OS_SocketAddrFromString */ + UtAssert_INT32_EQ(OS_SocketAddrFromString(NULL, "127.0.0.1"), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketAddrFromString(&addr, NULL), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketAddrFromString(&addr, "invalid"), OS_ERROR); + + /* OS_SocketAddrToString */ + UtAssert_INT32_EQ(OS_SocketAddrToString(addr_string, 0, &addr), OS_ERR_INVALID_SIZE); + UtAssert_INT32_EQ(OS_SocketAddrToString(NULL, sizeof(addr_string), &addr), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketAddrToString(addr_string, 1, &addr), OS_ERROR); +} + +void TestNetworkApiInet6(void) { - int32 expected; - int32 actual; osal_id_t socket_id; OS_SockAddr_t addr; - OS_SockAddr_t inv_addr; + int32 actual; + + /* + * The INET6 protocol is an optional component - + * Check for general support of this protocol by trying to init a + * socket address of that type + */ + actual = OS_SocketAddrInit(&addr, OS_SocketDomain_INET6); + if (actual == OS_ERR_NOT_IMPLEMENTED) + { + UtAssert_NA("OS_SocketAddrInit(&addr, OS_SocketDomain_INET6) - INET6 not supported"); + } + else + { + UtAssert_True(actual == OS_SUCCESS, "OS_SocketAddrInit(&addr, OS_SocketDomain_INET6) (%ld) == OS_SUCCESS", + (long)actual); + + /* Try OS_SocketOpen - should work if INET6 is supported per above test */ + UtAssert_INT32_EQ(OS_SocketOpen(&socket_id, OS_SocketDomain_INET6, OS_SocketType_DATAGRAM), OS_SUCCESS); + UtAssert_INT32_EQ(OS_close(socket_id), OS_SUCCESS); + } +} + +void TestDatagramNetworkApi_Setup(void) +{ + int32 expected; + int32 actual; + + memset(LongName, 'x', sizeof(LongName) - 1); + LongName[sizeof(LongName) - 1] = 0; /* Open a peer1 socket */ expected = OS_SUCCESS; @@ -140,85 +209,11 @@ void TestDatagramNetworkApi_Setup(void) actual = OS_SocketBind(p2_socket_id, &p2_addr); UtAssert_True(actual == expected, "OS_SocketBind() (%ld) == OS_SUCCESS", (long)actual); - /* - * Test for invalid and other nominal input parameters - * to the network functions being called above - */ - - /* OS_SocketOpen */ - actual = OS_SocketOpen(&socket_id, OS_SocketDomain_INET6, OS_SocketType_DATAGRAM); - if (actual == OS_ERR_NOT_IMPLEMENTED) - { - UtAssert_NA("INET6 not supported"); - } - else - { - UtAssert_True(actual == OS_SUCCESS, "OS_SocketOpen() (%ld) Passed", (long)actual); - } - OS_close(socket_id); - - expected = OS_INVALID_POINTER; - actual = OS_SocketOpen(NULL, OS_SocketDomain_INVALID, OS_SocketType_INVALID); - UtAssert_True(actual == expected, "OS_SocketOpen() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_ERR_NOT_IMPLEMENTED; - actual = OS_SocketOpen(&socket_id, OS_SocketDomain_MAX, OS_SocketType_MAX); - UtAssert_True(actual == expected, "OS_SocketOpen() (%ld) == OS_ERR_NOT_IMPLEMENTED", (long)actual); - - /* OS_SocketAddrInit */ - actual = OS_SocketAddrInit(&addr, OS_SocketDomain_INET6); - if (actual == OS_ERR_NOT_IMPLEMENTED) - { - UtAssert_NA("INET6 not supported"); - } - else - { - UtAssert_True(actual == OS_SUCCESS, "OS_SocketAddrInit() (%ld) == OS_SUCCESS", (long)actual); - } - - actual = OS_SocketAddrInit(NULL, OS_SocketDomain_INET6); - if (actual == OS_ERR_NOT_IMPLEMENTED) - { - UtAssert_NA("INET6 not supported"); - } - else - { - UtAssert_True(actual == OS_INVALID_POINTER, "OS_SocketAddrInit() (%ld) == OS_INVALID_POINTER", - (long)actual); - } - - expected = OS_ERR_NOT_IMPLEMENTED; - actual = OS_SocketAddrInit(&addr, OS_SocketDomain_INVALID); - UtAssert_True(actual == expected, "OS_SocketAddrInit() (%ld) == OS_ERR_NOT_IMPLEMENTED", (long)actual); - - expected = OS_INVALID_POINTER; - actual = OS_SocketAddrInit(NULL, OS_SocketDomain_INVALID); - UtAssert_True(actual == expected, "OS_SocketAddrInit() (%ld) == OS_INVALID_POINTER", (long)actual); - - /* OS_SocketAddrSetPort */ - expected = OS_ERR_BAD_ADDRESS; - actual = OS_SocketAddrSetPort(&addr, 0xFFFF); - UtAssert_True(actual == expected, "OS_SocketAddrSetPort() (%ld) == OS_ERR_BAD_ADDRESS", (long)actual); - - expected = OS_INVALID_POINTER; - actual = OS_SocketAddrSetPort(NULL, 1234); - UtAssert_True(actual == expected, "OS_SocketAddrSetPort() (%ld) == OS_INVALID_POINTER", (long)actual); - - /* OS_SocketAddrFromString */ - expected = OS_INVALID_POINTER; - actual = OS_SocketAddrFromString(NULL, "127.0.0.1"); - UtAssert_True(actual == expected, "OS_SocketAddrFromString() (%ld) == OS_SUCCESS", (long)actual); - - /* OS_SocketBind */ - expected = OS_ERR_INVALID_ID; - socket_id = OS_ObjectIdFromInteger(1); - actual = OS_SocketBind(socket_id, &addr); - UtAssert_True(actual == expected, "OS_SocketBind() (%ld) == OS_ERR_INVALID_ID", (long)actual); - - expected = OS_ERR_INCORRECT_OBJ_STATE; - memset(&inv_addr, 0, sizeof(inv_addr)); - actual = OS_SocketBind(p2_socket_id, &inv_addr); - UtAssert_True(actual == expected, "OS_SocketBind() (%ld) == OS_ERR_INCORRECT_OBJ_STATE", (long)actual); + /* Finally, open a regular file handle to test rejection if passed to a socket API call */ + UtAssert_INT32_EQ(OS_FileSysAddFixedMap(&fs_id, "./test", "/test"), OS_SUCCESS); + UtAssert_INT32_EQ(OS_OpenCreate(®ular_file_id, "/test/test.txt", OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, + OS_READ_WRITE), + OS_SUCCESS); } } /* end TestDatagramNetworkApi_Setup */ @@ -230,183 +225,114 @@ void TestDatagramNetworkApi_Setup(void) *****************************************************************************/ void TestDatagramNetworkApi(void) { - if (networkImplemented) + char AddrBuffer1[32]; + char AddrBuffer2[32]; + char AddrBuffer3[32]; + char AddrBuffer4[32]; + uint32 Buf1 = 111; + uint32 Buf2 = 000; + uint32 Buf3 = 222; + uint32 Buf4 = 000; + osal_id_t objid; + uint16 PortNum; + OS_socket_prop_t prop; + OS_SockAddr_t l_addr; + + if (!networkImplemented) { - char AddrBuffer1[32]; - char AddrBuffer2[32]; - char AddrBuffer3[32]; - char AddrBuffer4[32]; - uint32 Buf1 = 111; - uint32 Buf2 = 000; - uint32 Buf3 = 222; - uint32 Buf4 = 000; - osal_id_t objid; - uint16 PortNum; - OS_socket_prop_t prop; - OS_SockAddr_t l_addr; - int32 expected; - int32 actual; - - /* - * Send data from peer1 to peer2 and verify - */ - - /* Send data from peer1 to peer2 */ - expected = sizeof(Buf1); - actual = OS_SocketSendTo(p1_socket_id, &Buf1, sizeof(Buf1), &p2_addr); - UtAssert_True(actual == expected, "OS_SocketSendTo() Passed. sizeof(Buf1) (%ld) == 1", (long)actual); - - /* Recieve data from peer1 to peer2 */ - expected = sizeof(Buf2); - actual = OS_SocketRecvFrom(p2_socket_id, &Buf2, sizeof(Buf2), &l_addr, UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_SocketRecvFrom() Passed. sizeof(Buf2) (%ld) == 1", (long)actual); - UtAssert_True(Buf1 == Buf2, "Buf1 (%ld) == Buf2 (%ld)", (long)Buf1, (long)Buf2); - - /* Convert addresses to string and verify data is being sent from the correct address */ - expected = OS_SUCCESS; - - actual = OS_SocketAddrToString(AddrBuffer1, sizeof(AddrBuffer1), &p1_addr); - UtAssert_True(actual == expected, "OS_SocketAddrToString() (%ld) == OS_SUCCESS", (long)actual); + UtAssert_NA("Network API not implemented"); + return; + } - actual = OS_SocketAddrToString(AddrBuffer2, sizeof(AddrBuffer2), &l_addr); - UtAssert_True(actual == expected, "OS_SocketAddrToString() (%ld) == OS_SUCCESS", (long)actual); + /* + * Check various error inputs + * This set must be done after setting up a valid ID. + */ - UtAssert_True(strcmp(AddrBuffer1, AddrBuffer2) == 0, "AddrBuffer1 (%s) == AddrBuffer2 (%s)", AddrBuffer1, - AddrBuffer2); + /* make a bad object ID by flipping the bits of a good object ID */ + objid = OS_ObjectIdFromInteger(OS_ObjectIdToInteger(p2_socket_id) ^ 0xFFFFFFFF); + + /* OS_SocketBind */ + UtAssert_INT32_EQ(OS_SocketBind(OS_OBJECT_ID_UNDEFINED, &p2_addr), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketBind(objid, &p2_addr), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketBind(regular_file_id, &p2_addr), OS_ERR_INCORRECT_OBJ_TYPE); + UtAssert_INT32_EQ(OS_SocketBind(p2_socket_id, &p2_addr), OS_ERR_INCORRECT_OBJ_STATE); + UtAssert_INT32_EQ(OS_SocketBind(p2_socket_id, NULL), OS_INVALID_POINTER); + + /* OS_SocketRecvFrom */ + UtAssert_INT32_EQ(OS_SocketRecvFrom(OS_OBJECT_ID_UNDEFINED, &Buf2, sizeof(Buf2), &l_addr, UT_TIMEOUT), + OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketRecvFrom(objid, &Buf2, sizeof(Buf2), &l_addr, UT_TIMEOUT), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketRecvFrom(regular_file_id, &Buf2, sizeof(Buf2), &l_addr, UT_TIMEOUT), + OS_ERR_INCORRECT_OBJ_TYPE); + UtAssert_INT32_EQ(OS_SocketRecvFrom(p2_socket_id, NULL, sizeof(Buf2), &l_addr, UT_TIMEOUT), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketRecvFrom(p2_socket_id, &Buf2, 0, &l_addr, UT_TIMEOUT), OS_ERR_INVALID_SIZE); + + /* OS_SocketSendTo */ + UtAssert_INT32_EQ(OS_SocketSendTo(OS_OBJECT_ID_UNDEFINED, &Buf2, sizeof(Buf2), &l_addr), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketSendTo(objid, &Buf2, sizeof(Buf2), &l_addr), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketSendTo(regular_file_id, &Buf2, sizeof(Buf2), &l_addr), OS_ERR_INCORRECT_OBJ_TYPE); + UtAssert_INT32_EQ(OS_SocketSendTo(p2_socket_id, NULL, sizeof(Buf2), &l_addr), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketSendTo(p2_socket_id, &Buf2, 0, &l_addr), OS_ERR_INVALID_SIZE); + UtAssert_INT32_EQ(OS_SocketSendTo(p2_socket_id, &Buf2, sizeof(Buf2), NULL), OS_INVALID_POINTER); + + /* OS_SocketGetInfo */ + UtAssert_INT32_EQ(OS_SocketGetInfo(OS_OBJECT_ID_UNDEFINED, &prop), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketGetInfo(objid, &prop), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketGetInfo(p2_socket_id, NULL), OS_INVALID_POINTER); + + /* OS_SocketGetIdByName */ + UtAssert_INT32_EQ(OS_SocketGetIdByName(NULL, "NF"), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketGetIdByName(&objid, NULL), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketGetIdByName(&objid, "NF"), OS_ERR_NAME_NOT_FOUND); + UtAssert_INT32_EQ(OS_SocketGetIdByName(&objid, LongName), OS_ERR_NAME_TOO_LONG); - /* - * Send data from peer2 to peer1 and verify - */ + /* + * Send data from peer1 to peer2 and verify + */ - /* Send data from peer2 to peer1 */ - expected = sizeof(Buf3); - actual = OS_SocketSendTo(p2_socket_id, &Buf3, sizeof(Buf3), &p1_addr); - UtAssert_True(actual == expected, "OS_SocketSendTo() Passed. sizeof(Buf1) (%ld) == 1", (long)actual); + /* Send data from peer1 to peer2 */ + UtAssert_INT32_EQ(OS_SocketSendTo(p1_socket_id, &Buf1, sizeof(Buf1), &p2_addr), sizeof(Buf1)); - /* Recieve data from peer2 to peer1 */ - expected = sizeof(Buf4); - actual = OS_SocketRecvFrom(p1_socket_id, &Buf4, sizeof(Buf4), &l_addr, UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_SocketRecvFrom() Passed. sizeof(Buf3) (%ld) == 1", (long)actual); - UtAssert_True(Buf3 == Buf4, "Buf3 (%ld) == Buf4 (%ld)", (long)Buf3, (long)Buf4); + /* Recieve data from peer1 to peer2 */ + UtAssert_INT32_EQ(OS_SocketRecvFrom(p2_socket_id, &Buf2, sizeof(Buf2), &l_addr, UT_TIMEOUT), sizeof(Buf2)); + UtAssert_True(Buf1 == Buf2, "Buf1 (%ld) == Buf2 (%ld)", (long)Buf1, (long)Buf2); - /* Convert addresses to string and verify data is being sent from the correct address */ - expected = OS_SUCCESS; + /* Convert addresses to string and verify data is being sent from the correct address */ + UtAssert_INT32_EQ(OS_SocketAddrToString(AddrBuffer1, sizeof(AddrBuffer1), &p1_addr), OS_SUCCESS); + UtAssert_INT32_EQ(OS_SocketAddrToString(AddrBuffer2, sizeof(AddrBuffer2), &l_addr), OS_SUCCESS); - actual = OS_SocketAddrToString(AddrBuffer3, sizeof(AddrBuffer3), &p2_addr); - UtAssert_True(actual == expected, "OS_SocketAddrToString() (%ld) == OS_SUCCESS", (long)actual); + UtAssert_True(strcmp(AddrBuffer1, AddrBuffer2) == 0, "AddrBuffer1 (%s) == AddrBuffer2 (%s)", AddrBuffer1, + AddrBuffer2); - actual = OS_SocketAddrToString(AddrBuffer4, sizeof(AddrBuffer4), &l_addr); - UtAssert_True(actual == expected, "OS_SocketAddrToString() (%ld) == OS_SUCCESS", (long)actual); + /* + * Send data from peer2 to peer1 and verify + */ - UtAssert_True(strcmp(AddrBuffer3, AddrBuffer4) == 0, "AddrBuffer3 (%s) == AddrBuffer4 (%s)", AddrBuffer3, - AddrBuffer4); + /* Send data from peer2 to peer1 */ + UtAssert_INT32_EQ(OS_SocketSendTo(p2_socket_id, &Buf3, sizeof(Buf3), &p1_addr), sizeof(Buf3)); - /* Get port from incoming address and verify */ - actual = OS_SocketAddrGetPort(&PortNum, &p2_addr); - UtAssert_True(actual == expected, "OS_SocketAddrGetPort() (%ld) == OS_SUCCESS", (long)actual); - UtAssert_True(PortNum == 9998, "PortNum (%ld) == 9998", (long)actual); + /* Recieve data from peer2 to peer1 */ + UtAssert_INT32_EQ(OS_SocketRecvFrom(p1_socket_id, &Buf4, sizeof(Buf4), &l_addr, UT_TIMEOUT), sizeof(Buf4)); + UtAssert_True(Buf3 == Buf4, "Buf3 (%ld) == Buf4 (%ld)", (long)Buf3, (long)Buf4); - /* Get socket info and verify */ - actual = OS_SocketGetInfo(p1_socket_id, &prop); - UtAssert_True(actual == expected, "OS_SocketGetInfo() (%ld) == OS_SUCCESS", (long)actual); - UtAssert_True(!OS_ObjectIdDefined(prop.creator), "prop.creator (%lu) == 0", OS_ObjectIdToInteger(prop.creator)); - UtAssert_True(strcmp(prop.name, "127.0.0.1:9999") == 0, "prop.name (%s) == 127.0.0.1:9999", prop.name); + /* Convert addresses to string and verify data is being sent from the correct address */ + UtAssert_INT32_EQ(OS_SocketAddrToString(AddrBuffer3, sizeof(AddrBuffer3), &p2_addr), OS_SUCCESS); + UtAssert_INT32_EQ(OS_SocketAddrToString(AddrBuffer4, sizeof(AddrBuffer4), &l_addr), OS_SUCCESS); + UtAssert_True(strcmp(AddrBuffer3, AddrBuffer4) == 0, "AddrBuffer3 (%s) == AddrBuffer4 (%s)", AddrBuffer3, + AddrBuffer4); - actual = OS_SocketGetIdByName(&objid, "127.0.0.1:9999"); - UtAssert_True(actual == expected, "OS_SocketGetIdByName() (%ld) == OS_SUCCESS", (long)actual); - UtAssert_True(OS_ObjectIdEqual(objid, p1_socket_id), "objid (%lu) == p1_socket_id", - OS_ObjectIdToInteger(objid)); + /* Get port from incoming address and verify */ + UtAssert_INT32_EQ(OS_SocketAddrGetPort(&PortNum, &p2_addr), OS_SUCCESS); + UtAssert_True(PortNum == 9998, "PortNum (%u) == 9998", (unsigned int)PortNum); - /* - * Test for invalid input parameters - * to the network functions being called above - */ + /* Get socket info and verify */ + UtAssert_INT32_EQ(OS_SocketGetInfo(p1_socket_id, &prop), OS_SUCCESS); + UtAssert_True(!OS_ObjectIdDefined(prop.creator), "prop.creator (%lu) == 0", OS_ObjectIdToInteger(prop.creator)); + UtAssert_True(strcmp(prop.name, "127.0.0.1:9999") == 0, "prop.name (%s) == 127.0.0.1:9999", prop.name); - /* OS_SocketSendTo */ - expected = OS_INVALID_POINTER; - actual = OS_SocketSendTo(p1_socket_id, NULL, OSAL_SIZE_C(0), NULL); - UtAssert_True(actual == expected, "OS_SocketSendTo(NULL) (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_INVALID_POINTER; - actual = OS_SocketSendTo(p1_socket_id, NULL, OSAL_SIZE_C(1), &p2_addr); - UtAssert_True(actual == expected, "OS_SocketSendTo() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_ERR_INVALID_ID; - objid = OS_ObjectIdFromInteger(0xFFFFFFFF); - actual = OS_SocketSendTo(objid, &Buf1, sizeof(Buf1), &p2_addr); - UtAssert_True(actual == expected, "OS_SocketSendTo() (%ld) == OS_ERR_INVALID_ID", (long)actual); - - /* OS_SocketRecvFrom */ - expected = OS_INVALID_POINTER; - actual = OS_SocketRecvFrom(p2_socket_id, NULL, OSAL_SIZE_C(1), NULL, UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_SocketRecvFrom() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_INVALID_POINTER; - actual = OS_SocketRecvFrom(p2_socket_id, NULL, OSAL_SIZE_C(0), NULL, 0); - UtAssert_True(actual == expected, "OS_SocketRecvFrom(NULL) (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_ERR_INVALID_ID; - objid = OS_ObjectIdFromInteger(0xFFFFFFFF); - actual = OS_SocketRecvFrom(objid, &Buf2, sizeof(Buf2), &l_addr, UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_SocketRecvFrom() (%ld) == OS_ERR_INVALID_ID", (long)actual); - - expected = OS_ERR_INVALID_SIZE; - actual = OS_SocketRecvFrom(p2_socket_id, &Buf2, OSAL_SIZE_C(0), &l_addr, UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_SocketRecvFrom() (%ld) == OS_ERR_INVALID_SIZE", (long)actual); - - expected = OS_ERR_INVALID_SIZE; - actual = OS_SocketRecvFrom(p2_socket_id, &Buf2, OSAL_SIZE_C(0), NULL, UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_SocketRecvFrom() (%ld) == OS_ERR_INVALID_SIZE", (long)actual); - - /* OS_SocketAddrToString */ - expected = OS_INVALID_POINTER; - actual = OS_SocketAddrToString(NULL, OSAL_SIZE_C(0), NULL); - UtAssert_True(actual == expected, "OS_SocketAddrToString() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_INVALID_POINTER; - actual = OS_SocketAddrToString(AddrBuffer1, sizeof(AddrBuffer1), NULL); - UtAssert_True(actual == expected, "OS_SocketAddrToString() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_INVALID_POINTER; - actual = OS_SocketAddrToString(NULL, OSAL_SIZE_C(0), &p2_addr); - UtAssert_True(actual == expected, "OS_SocketAddrToString() (%ld) == OS_INVALID_POINTER", (long)actual); - - /* OS_SocketAddrGetPort */ - expected = OS_INVALID_POINTER; - actual = OS_SocketAddrGetPort(NULL, NULL); - UtAssert_True(actual == expected, "OS_SocketAddrGetPort() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_INVALID_POINTER; - actual = OS_SocketAddrGetPort(NULL, &l_addr); - UtAssert_True(actual == expected, "OS_SocketAddrGetPort() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_INVALID_POINTER; - actual = OS_SocketAddrGetPort(&PortNum, NULL); - UtAssert_True(actual == expected, "OS_SocketAddrGetPort() (%ld) == OS_INVALID_POINTER", (long)actual); - - /* OS_SocketGetIdByName */ - expected = OS_INVALID_POINTER; - actual = OS_SocketGetIdByName(NULL, NULL); - UtAssert_True(actual == expected, "OS_SocketGetIdByName() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_ERR_NAME_NOT_FOUND; - actual = OS_SocketGetIdByName(&objid, "NF"); - UtAssert_True(actual == expected, "OS_SocketGetIdByName() (%ld) == OS_ERR_NAME_NOT_FOUND", (long)actual); - - /* OS_SocketGetInfo */ - expected = OS_INVALID_POINTER; - actual = OS_SocketGetInfo(p2_socket_id, NULL); - UtAssert_True(actual == expected, "OS_SocketGetInfo() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_ERR_INVALID_ID; - objid = OS_OBJECT_ID_UNDEFINED; - actual = OS_SocketGetInfo(objid, &prop); - UtAssert_True(actual == expected, "OS_SocketGetInfo() (%ld) == OS_ERR_INVALID_ID", (long)actual); - } - else - { - UtAssert_NA("Network API not implemented"); - } + UtAssert_INT32_EQ(OS_SocketGetIdByName(&objid, "127.0.0.1:9999"), OS_SUCCESS); + UtAssert_True(OS_ObjectIdEqual(objid, p1_socket_id), "objid (%lu) == p1_socket_id", OS_ObjectIdToInteger(objid)); } /* end TestDatagramNetworkApi */ @@ -446,11 +372,9 @@ void Server_Fn(void) UtPrintf("SERVER: accepting connection %u", (unsigned int)iter); /* Accept incoming connections */ - Status = OS_SocketAccept(s_socket_id, &connsock_id, &addr, OS_PEND); - if (Status != OS_SUCCESS) + ServerFn_AcceptStatus = OS_SocketAccept(s_socket_id, &connsock_id, &addr, OS_PEND); + if (ServerFn_AcceptStatus != OS_SUCCESS) { - snprintf(ServerFn_ErrorString, sizeof(ServerFn_ErrorString), "OS_SocketAccept() iter=%u, return code=%d", - (unsigned int)iter, (int)Status); break; } @@ -571,6 +495,16 @@ void TestStreamNetworkApi(void) actual = OS_SocketAddrFromString(&s_addr, "127.0.0.1"); UtAssert_True(actual == expected, "OS_SocketAddrFromString() (%ld) == OS_SUCCESS", (long)actual); + /* OS_SocketAccept error conditions - check before binding */ + /* create a bad ID by flipping the bits of a good ID */ + temp_id = OS_ObjectIdFromInteger(OS_ObjectIdToInteger(s_socket_id) ^ 0xFFFFFFFF); + UtAssert_INT32_EQ(OS_SocketAccept(temp_id, &temp_id, &temp_addr, 0), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketAccept(s_socket_id, NULL, &temp_addr, UT_TIMEOUT), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketAccept(s_socket_id, &temp_id, NULL, UT_TIMEOUT), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketAccept(s_socket_id, &temp_id, &temp_addr, UT_TIMEOUT), OS_ERR_INCORRECT_OBJ_STATE); + UtAssert_INT32_EQ(OS_SocketAccept(regular_file_id, &temp_id, &temp_addr, UT_TIMEOUT), + OS_ERR_INCORRECT_OBJ_TYPE); + /* Bind server socket to server address */ actual = OS_SocketBind(s_socket_id, &s_addr); UtAssert_True(actual == expected, "OS_SocketBind() (%ld) == OS_SUCCESS", (long)actual); @@ -619,67 +553,56 @@ void TestStreamNetworkApi(void) UtAssert_True(actual == expected, "OS_SocketOpen() (%ld) == OS_SUCCESS", (long)actual); UtAssert_True(OS_ObjectIdDefined(c_socket_id), "c_socket_id (%lu) != 0", OS_ObjectIdToInteger(c_socket_id)); + /* + * Test for invalid input parameters - BEFORE connection + * This is done before valid connection when the c_socket_id is created but not connected. + * only needs to be done once, so only do this on the first pass. + */ + if (iter == UT_STREAM_CONNECTION_INITIAL) + { + /* create a bad ID by flipping the bits of a good ID */ + temp_id = OS_ObjectIdFromInteger(OS_ObjectIdToInteger(c_socket_id) ^ 0xFFFFFFFF); + + /* OS_SocketShutdown */ + UtAssert_INT32_EQ(OS_SocketShutdown(temp_id, OS_SocketShutdownMode_SHUT_READ), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketShutdown(regular_file_id, OS_SocketShutdownMode_SHUT_READ), + OS_ERR_INCORRECT_OBJ_TYPE); + UtAssert_INT32_EQ(OS_SocketShutdown(c_socket_id, OS_SocketShutdownMode_SHUT_READ), + OS_ERR_INCORRECT_OBJ_STATE); + } + actual = OS_SocketConnect(c_socket_id, &s_addr, UT_TIMEOUT); UtAssert_True(actual == expected, "OS_SocketConnect() (%ld) == OS_SUCCESS", (long)actual); /* - * Test for invalid input parameters - - * This is done after valid connection when the c_socket_id is valid, - * but it only needs to be done once, so only do this on the first pass. + * Test for invalid input parameters - AFTER connection + * This is done after valid connection when the c_socket_id is fully connected. + * only needs to be done once, so only do this on the first pass. */ if (iter == UT_STREAM_CONNECTION_INITIAL) { - /* OS_TimedRead */ - expected = OS_ERR_INVALID_ID; - temp_id = OS_ObjectIdFromInteger(0xFFFFFFFF); - actual = OS_TimedRead(temp_id, Buf_rcv_c, sizeof(Buf_rcv_c), UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_TimedRead() (%ld) == %ld", (long)actual, (long)expected); + /* create a bad ID by flipping the bits of a good ID */ + temp_id = OS_ObjectIdFromInteger(OS_ObjectIdToInteger(c_socket_id) ^ 0xFFFFFFFF); - expected = OS_INVALID_POINTER; - actual = OS_TimedRead(c_socket_id, NULL, sizeof(Buf_rcv_c), UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_TimedRead() (%ld) == %ld", (long)actual, (long)expected); + /* OS_SocketShutdown */ + UtAssert_INT32_EQ(OS_SocketShutdown(c_socket_id, OS_SocketShutdownMode_NONE), OS_ERR_INVALID_ARGUMENT); - expected = OS_ERROR_TIMEOUT; - actual = OS_TimedRead(c_socket_id, Buf_rcv_c, sizeof(Buf_rcv_c), 0); - UtAssert_True(actual == expected, "OS_TimedRead() (%ld) == %ld", (long)actual, (long)expected); + /* OS_TimedRead */ + UtAssert_INT32_EQ(OS_TimedRead(temp_id, Buf_rcv_c, sizeof(Buf_rcv_c), UT_TIMEOUT), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_TimedRead(c_socket_id, NULL, sizeof(Buf_rcv_c), UT_TIMEOUT), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_TimedRead(c_socket_id, Buf_rcv_c, 0, UT_TIMEOUT), OS_ERR_INVALID_SIZE); + UtAssert_INT32_EQ(OS_TimedRead(c_socket_id, Buf_rcv_c, sizeof(Buf_rcv_c), 0), OS_ERROR_TIMEOUT); /* OS_TimedWrite */ - expected = OS_ERR_INVALID_ID; - temp_id = OS_ObjectIdFromInteger(0xFFFFFFFF); - actual = OS_TimedWrite(temp_id, Buf_rcv_c, sizeof(Buf_rcv_c), UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_TimedWrite() (%ld) == %ld", (long)actual, (long)expected); - - expected = OS_INVALID_POINTER; - actual = OS_TimedWrite(c_socket_id, NULL, sizeof(Buf_rcv_c), UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_TimedWrite() (%ld) == %ld", (long)actual, (long)expected); - - /* OS_SocketAccept */ - expected = OS_INVALID_POINTER; - actual = OS_SocketAccept(s_socket_id, NULL, NULL, 0); - UtAssert_True(actual == expected, "OS_SocketAccept() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_INVALID_POINTER; - actual = OS_SocketAccept(s_socket_id, NULL, &temp_addr, UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_SocketAccept() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_INVALID_POINTER; - actual = OS_SocketAccept(s_socket_id, &temp_id, NULL, UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_SocketAccept() (%ld) == OS_INVALID_POINTER", (long)actual); + UtAssert_INT32_EQ(OS_TimedWrite(temp_id, Buf_rcv_c, sizeof(Buf_rcv_c), UT_TIMEOUT), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_TimedWrite(c_socket_id, NULL, sizeof(Buf_rcv_c), UT_TIMEOUT), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_TimedWrite(c_socket_id, Buf_rcv_c, 0, UT_TIMEOUT), OS_ERR_INVALID_SIZE); /* OS_SocketConnect */ - expected = OS_INVALID_POINTER; - actual = OS_SocketConnect(c_socket_id, NULL, UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_SocketConnect() (%ld) == OS_INVALID_POINTER", (long)actual); - - expected = OS_ERR_INCORRECT_OBJ_STATE; - actual = OS_SocketConnect(c_socket_id, &s_addr, 0); - UtAssert_True(actual == expected, "OS_SocketConnect() (%ld) == OS_ERR_INCORRECT_OBJ_STATE", - (long)actual); - - expected = OS_ERR_INVALID_ID; - temp_id = OS_ObjectIdFromInteger(0xFFFFFFFF); - actual = OS_SocketConnect(temp_id, &s_addr, UT_TIMEOUT); - UtAssert_True(actual == expected, "OS_SocketConnect() (%ld) == OS_ERR_INVALID_ID", (long)actual); + UtAssert_INT32_EQ(OS_SocketConnect(c_socket_id, NULL, UT_TIMEOUT), OS_INVALID_POINTER); + UtAssert_INT32_EQ(OS_SocketConnect(temp_id, &s_addr, UT_TIMEOUT), OS_ERR_INVALID_ID); + UtAssert_INT32_EQ(OS_SocketConnect(regular_file_id, &s_addr, UT_TIMEOUT), OS_ERR_INCORRECT_OBJ_TYPE); + UtAssert_INT32_EQ(OS_SocketConnect(c_socket_id, &s_addr, 0), OS_ERR_INCORRECT_OBJ_STATE); } /* @@ -693,18 +616,12 @@ void TestStreamNetworkApi(void) */ if (iter == UT_STREAM_CONNECTION_READ_SHUTDOWN) { - expected = OS_SUCCESS; - actual = OS_SocketShutdown(c_socket_id, OS_SocketShutdownMode_SHUT_READ); - UtAssert_True(actual == expected, "OS_SocketShutdown(SHUT_READ) (%ld) == %ld", (long)actual, - (long)expected); + UtAssert_INT32_EQ(OS_SocketShutdown(c_socket_id, OS_SocketShutdownMode_SHUT_READ), OS_SUCCESS); } if (iter == UT_STREAM_CONNECTION_RDWR_SHUTDOWN) { - expected = OS_SUCCESS; - actual = OS_SocketShutdown(c_socket_id, OS_SocketShutdownMode_SHUT_READWRITE); - UtAssert_True(actual == expected, "OS_SocketShutdown(SHUT_READWRITE) (%ld) == %ld", (long)actual, - (long)expected); + UtAssert_INT32_EQ(OS_SocketShutdown(c_socket_id, OS_SocketShutdownMode_SHUT_READWRITE), OS_SUCCESS); } if (iter == UT_STREAM_CONNECTION_READ_SHUTDOWN || iter == UT_STREAM_CONNECTION_RDWR_SHUTDOWN) @@ -728,10 +645,7 @@ void TestStreamNetworkApi(void) /* On the designated iteration, use shutdown to indicate this is the end of the written data */ if (iter == UT_STREAM_CONNECTION_WRITE_SHUTDOWN) { - expected = OS_SUCCESS; - actual = OS_SocketShutdown(c_socket_id, OS_SocketShutdownMode_SHUT_WRITE); - UtAssert_True(actual == expected, "OS_SocketShutdown(SHUT_WRITE) (%ld) == %ld", (long)actual, - (long)expected); + UtAssert_INT32_EQ(OS_SocketShutdown(c_socket_id, OS_SocketShutdownMode_SHUT_WRITE), OS_SUCCESS); } if (iter == UT_STREAM_CONNECTION_WRITE_SHUTDOWN || iter == UT_STREAM_CONNECTION_RDWR_SHUTDOWN) @@ -773,6 +687,15 @@ void TestStreamNetworkApi(void) } OS_close(c_socket_id); + + /* Check that the server function did NOT Report any errors */ + UtAssert_True(ServerFn_AcceptStatus == OS_SUCCESS, + "OS_SocketAccept(s_socket_id, &connsock_id, &addr, OS_PEND) (%d) == OS_SUCCESS", + (int)ServerFn_AcceptStatus); + if (ServerFn_ErrorString[0] != 0) + { + UtAssert_Failed("Server_Fn(): %s", ServerFn_ErrorString); + } } /* @@ -789,12 +712,6 @@ void TestStreamNetworkApi(void) loopcnt++; } UtAssert_True(loopcnt < UT_EXIT_LOOP_MAX, "Task exited after %ld iterations", (long)loopcnt); - - /* Check that the server function did NOT Report any errors */ - if (ServerFn_ErrorString[0] != 0) - { - UtAssert_Failed("Server_Fn(): %s", ServerFn_ErrorString); - } } } /* end TestStreamNetworkApi */ @@ -824,6 +741,8 @@ void UtTest_Setup(void) /* * Register the test setup and check routines in UT assert */ + UtTest_Add(TestNetworkApiBadArgs, NULL, NULL, "NetworkApiBadArgs"); + UtTest_Add(TestNetworkApiInet6, NULL, NULL, "TestNetworkApiInet6"); UtTest_Add(TestDatagramNetworkApi, TestDatagramNetworkApi_Setup, TestDatagramNetworkApi_Teardown, "TestDatagramNetworkApi"); UtTest_Add(TestStreamNetworkApi, NULL, TestStreamNetworkApi_Teardown, "TestStreamNetworkApi"); diff --git a/src/unit-test-coverage/portable/src/coveragetest-bsd-sockets.c b/src/unit-test-coverage/portable/src/coveragetest-bsd-sockets.c index e2e41af96..bacdab00a 100644 --- a/src/unit-test-coverage/portable/src/coveragetest-bsd-sockets.c +++ b/src/unit-test-coverage/portable/src/coveragetest-bsd-sockets.c @@ -407,8 +407,13 @@ void Test_OS_SocketAddrFromString_Impl(void) UT_SetDeferredRetcode(UT_KEY(OCS_inet_pton), 1, -1); OSAPI_TEST_FUNCTION_RC(OS_SocketAddrFromString_Impl, (&addr, buffer), OS_ERROR); - /* AF_INET, success */ + /* AF_INET, unable to convert (note inet_pton returns 0 if it failed) */ sa->sa_family = OCS_AF_INET; + UT_SetDeferredRetcode(UT_KEY(OCS_inet_pton), 1, 0); + OSAPI_TEST_FUNCTION_RC(OS_SocketAddrFromString_Impl, (&addr, buffer), OS_ERROR); + + /* AF_INET, success */ + UT_SetDeferredRetcode(UT_KEY(OCS_inet_pton), 1, 1); OSAPI_TEST_FUNCTION_RC(OS_SocketAddrFromString_Impl, (&addr, buffer), OS_SUCCESS); }