From 6455c80659f24a71dc7eccae639ac9655ca3d050 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 2 Nov 2021 18:08:11 +0800 Subject: [PATCH 01/10] Update plaintext_poix transport interface * EAGAIN or EWOULDBLOCK should return zero to indicate this operation can be retried. --- .../posix/transport/src/plaintext_posix.c | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/platform/posix/transport/src/plaintext_posix.c b/platform/posix/transport/src/plaintext_posix.c index 8f703e7291..a56c95ef51 100644 --- a/platform/posix/transport/src/plaintext_posix.c +++ b/platform/posix/transport/src/plaintext_posix.c @@ -170,7 +170,16 @@ int32_t Plaintext_Recv( NetworkContext_t * pNetworkContext, } else if( bytesReceived < 0 ) { - logTransportError( errno ); + /* The timeout and no data is received situaition. Return zero to indicate + * this operation can be retried. */ + if( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) + { + bytesReceived = 0; + } + else + { + logTransportError( errno ); + } } else { @@ -237,7 +246,16 @@ int32_t Plaintext_Send( NetworkContext_t * pNetworkContext, } else if( bytesSent < 0 ) { - logTransportError( errno ); + /* The timeout and no data is sent situaition. Return zero to indicate + * this operation can be retried. */ + if( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) + { + bytesSent = 0; + } + else + { + logTransportError( errno ); + } } else { From 76dbb4b81d64139b13718c39cefa8d8097611046 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 2 Nov 2021 23:03:17 +0800 Subject: [PATCH 02/10] Fix typo --- platform/posix/transport/src/plaintext_posix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform/posix/transport/src/plaintext_posix.c b/platform/posix/transport/src/plaintext_posix.c index a56c95ef51..1dfbf81bb3 100644 --- a/platform/posix/transport/src/plaintext_posix.c +++ b/platform/posix/transport/src/plaintext_posix.c @@ -170,7 +170,7 @@ int32_t Plaintext_Recv( NetworkContext_t * pNetworkContext, } else if( bytesReceived < 0 ) { - /* The timeout and no data is received situaition. Return zero to indicate + /* The timeout and no data is received situation. Return zero to indicate * this operation can be retried. */ if( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) { @@ -246,7 +246,7 @@ int32_t Plaintext_Send( NetworkContext_t * pNetworkContext, } else if( bytesSent < 0 ) { - /* The timeout and no data is sent situaition. Return zero to indicate + /* The timeout and no data is sent situation. Return zero to indicate * this operation can be retried. */ if( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) { From 2c0b560627cd5c43488f0aaf11606598a3905bb2 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 2 Nov 2021 23:21:44 +0800 Subject: [PATCH 03/10] Fix plaintext utest --- platform/posix/transport/src/plaintext_posix.c | 4 ++-- platform/posix/transport/utest/plaintext_utest.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/platform/posix/transport/src/plaintext_posix.c b/platform/posix/transport/src/plaintext_posix.c index 1dfbf81bb3..c8157685a1 100644 --- a/platform/posix/transport/src/plaintext_posix.c +++ b/platform/posix/transport/src/plaintext_posix.c @@ -172,7 +172,7 @@ int32_t Plaintext_Recv( NetworkContext_t * pNetworkContext, { /* The timeout and no data is received situation. Return zero to indicate * this operation can be retried. */ - if( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) + if( ( pollStatus > 0 ) && ( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) ) { bytesReceived = 0; } @@ -248,7 +248,7 @@ int32_t Plaintext_Send( NetworkContext_t * pNetworkContext, { /* The timeout and no data is sent situation. Return zero to indicate * this operation can be retried. */ - if( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) + if( ( pollStatus > 0 ) && ( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) ) { bytesSent = 0; } diff --git a/platform/posix/transport/utest/plaintext_utest.c b/platform/posix/transport/utest/plaintext_utest.c index 8ef5aba253..561564ebe1 100644 --- a/platform/posix/transport/utest/plaintext_utest.c +++ b/platform/posix/transport/utest/plaintext_utest.c @@ -73,7 +73,7 @@ static uint8_t errorNumbers[] = EBADF, ECONNRESET, EDESTADDRREQ, EINTR, EINVAL, ENOTCONN, ENOTSOCK, EOPNOTSUPP, ETIMEDOUT, EMSGSIZE, EPIPE, - EAGAIN, EWOULDBLOCK, UNKNOWN_ERRNO + UNKNOWN_ERRNO }; /* ============================ UNITY FIXTURES ============================ */ From a540c79fb23aa76df765c12b4499e0161697ca7e Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Wed, 3 Nov 2021 08:43:43 +0800 Subject: [PATCH 04/10] Add test cases to cover plaintext_posix blocked timeout --- .../posix/transport/src/plaintext_posix.c | 8 +-- .../posix/transport/utest/plaintext_utest.c | 56 ++++++++++++++++++- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/platform/posix/transport/src/plaintext_posix.c b/platform/posix/transport/src/plaintext_posix.c index c8157685a1..0ace0a66fd 100644 --- a/platform/posix/transport/src/plaintext_posix.c +++ b/platform/posix/transport/src/plaintext_posix.c @@ -170,8 +170,8 @@ int32_t Plaintext_Recv( NetworkContext_t * pNetworkContext, } else if( bytesReceived < 0 ) { - /* The timeout and no data is received situation. Return zero to indicate - * this operation can be retried. */ + /* The socket blocked for timeout and no data is received situation. + * Return zero to indicate this operation can be retried. */ if( ( pollStatus > 0 ) && ( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) ) { bytesReceived = 0; @@ -246,8 +246,8 @@ int32_t Plaintext_Send( NetworkContext_t * pNetworkContext, } else if( bytesSent < 0 ) { - /* The timeout and no data is sent situation. Return zero to indicate - * this operation can be retried. */ + /* The socket blocked for timeout and no data is sent situation. + * Return zero to indicate this operation can be retried. */ if( ( pollStatus > 0 ) && ( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) ) { bytesSent = 0; diff --git a/platform/posix/transport/utest/plaintext_utest.c b/platform/posix/transport/utest/plaintext_utest.c index 561564ebe1..14d75fb9c9 100644 --- a/platform/posix/transport/utest/plaintext_utest.c +++ b/platform/posix/transport/utest/plaintext_utest.c @@ -70,9 +70,9 @@ static uint8_t plaintextBuffer[ BUFFER_LEN ] = { 0 }; * one is used for the default case. */ static uint8_t errorNumbers[] = { - EBADF, ECONNRESET, EDESTADDRREQ, EINTR, - EINVAL, ENOTCONN, ENOTSOCK, EOPNOTSUPP, - ETIMEDOUT, EMSGSIZE, EPIPE, + EBADF, ECONNRESET, EDESTADDRREQ, EINTR, + EINVAL, ENOTCONN, ENOTSOCK, EOPNOTSUPP, + ETIMEDOUT, EMSGSIZE, EPIPE, UNKNOWN_ERRNO }; @@ -255,6 +255,31 @@ void test_Plaintext_Recv_Network_Error( void ) } } +/** + * @brief Test that #Plaintext_Recv returns 0 bytes when the socket has blocked for + * timeout. + */ +void test_Plaintext_Recv_Socket_Timeout_Blocked( void ) +{ + int32_t bytesReceived; + + poll_ExpectAnyArgsAndReturn( 1 ); + recv_ExpectAnyArgsAndReturn( SEND_RECV_ERROR ); + errno = EAGAIN; + bytesReceived = Plaintext_Recv( &networkContext, + plaintextBuffer, + 1U ); + TEST_ASSERT_EQUAL( 0, bytesReceived ); + + poll_ExpectAnyArgsAndReturn( 1 ); + recv_ExpectAnyArgsAndReturn( SEND_RECV_ERROR ); + errno = EWOULDBLOCK; + bytesReceived = Plaintext_Recv( &networkContext, + plaintextBuffer, + 1U ); + TEST_ASSERT_EQUAL( 0, bytesReceived ); +} + /** * @brief Test the happy path case when #Plaintext_Send is able to send all bytes * over the network stack successfully. @@ -337,3 +362,28 @@ void test_Plaintext_Send_Poll_Error( void ) BYTES_TO_SEND ); TEST_ASSERT_EQUAL( SEND_RECV_ERROR, bytesSent ); } + +/** + * @brief Test that #Plaintext_Send returns 0 bytes when the socket has blocked for + * timeout. + */ +void test_Plaintext_Send_Socket_Timeout_Blocked( void ) +{ + int32_t bytesSent; + + poll_ExpectAnyArgsAndReturn( 1 ); + send_ExpectAnyArgsAndReturn( SEND_RECV_ERROR ); + errno = EAGAIN; + bytesSent = Plaintext_Send( &networkContext, + plaintextBuffer, + BYTES_TO_SEND ); + TEST_ASSERT_EQUAL( 0, bytesSent ); + + poll_ExpectAnyArgsAndReturn( 1 ); + send_ExpectAnyArgsAndReturn( SEND_RECV_ERROR ); + errno = EWOULDBLOCK; + bytesSent = Plaintext_Send( &networkContext, + plaintextBuffer, + BYTES_TO_SEND ); + TEST_ASSERT_EQUAL( 0, bytesSent ); +} From a8b37be5945d78b426776c7486b9cd86795deb32 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Wed, 3 Nov 2021 11:05:24 +0800 Subject: [PATCH 05/10] Fix EAGAIN and EWOULDBLOCK coverage problem --- .../posix/transport/src/plaintext_posix.c | 72 +++++++++++++------ 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/platform/posix/transport/src/plaintext_posix.c b/platform/posix/transport/src/plaintext_posix.c index 0ace0a66fd..38d5426350 100644 --- a/platform/posix/transport/src/plaintext_posix.c +++ b/platform/posix/transport/src/plaintext_posix.c @@ -163,24 +163,38 @@ int32_t Plaintext_Recv( NetworkContext_t * pNetworkContext, /* Note: A zero value return from recv() represents * closure of TCP connection by the peer. */ - if( ( pollStatus > 0 ) && ( bytesReceived == 0 ) ) - { - /* Peer has closed the connection. Treat as an error. */ - bytesReceived = -1; - } - else if( bytesReceived < 0 ) + if( pollStatus > 0 ) { - /* The socket blocked for timeout and no data is received situation. - * Return zero to indicate this operation can be retried. */ - if( ( pollStatus > 0 ) && ( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) ) + if( bytesReceived == 0 ) { - bytesReceived = 0; + /* Peer has closed the connection. Treat as an error. */ + bytesReceived = -1; + } + else if( bytesReceived < 0 ) + { + /* The socket blocked for timeout and no data is received situation. + * Return zero to indicate this operation can be retried. */ + #if EAGAIN == EWOULDBLOCK + if( errno == EAGAIN ) + { + bytesReceived = 0; + } + #else + if( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) + { + bytesReceived = 0; + } + #endif } else { - logTransportError( errno ); + /* Empty else MISRA 15.7 */ } } + else if( bytesReceived < 0 ) + { + logTransportError( errno ); + } else { /* Empty else MISRA 15.7 */ @@ -239,24 +253,38 @@ int32_t Plaintext_Send( NetworkContext_t * pNetworkContext, bytesSent = 0; } - if( ( pollStatus > 0 ) && ( bytesSent == 0 ) ) - { - /* Peer has closed the connection. Treat as an error. */ - bytesSent = -1; - } - else if( bytesSent < 0 ) + if( pollStatus > 0 ) { - /* The socket blocked for timeout and no data is sent situation. - * Return zero to indicate this operation can be retried. */ - if( ( pollStatus > 0 ) && ( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) ) + if( bytesSent == 0 ) { - bytesSent = 0; + /* Peer has closed the connection. Treat as an error. */ + bytesSent = -1; + } + else if( bytesSent < 0 ) + { + /* The socket blocked for timeout and no data is received situation. + * Return zero to indicate this operation can be retried. */ + #if EAGAIN == EWOULDBLOCK + if( errno == EAGAIN ) + { + bytesSent = 0; + } + #else + if( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) + { + bytesSent = 0; + } + #endif } else { - logTransportError( errno ); + /* Empty else MISRA 15.7 */ } } + else if( bytesSent < 0 ) + { + logTransportError( errno ); + } else { /* Empty else MISRA 15.7 */ From 129564a061ffad4f60aeb27107b2051867667e40 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 19 Nov 2021 15:44:50 +0800 Subject: [PATCH 06/10] Update plaintext_posix with invalid parameter test --- .../posix/transport/src/plaintext_posix.c | 196 ++++++++++-------- .../posix/transport/utest/plaintext_utest.c | 47 +++++ 2 files changed, 156 insertions(+), 87 deletions(-) diff --git a/platform/posix/transport/src/plaintext_posix.c b/platform/posix/transport/src/plaintext_posix.c index 38d5426350..86017be1ef 100644 --- a/platform/posix/transport/src/plaintext_posix.c +++ b/platform/posix/transport/src/plaintext_posix.c @@ -118,59 +118,74 @@ int32_t Plaintext_Recv( NetworkContext_t * pNetworkContext, int32_t bytesReceived = -1, pollStatus = 1; struct pollfd pollFds; - assert( pNetworkContext != NULL && pNetworkContext->pParams != NULL ); - assert( pBuffer != NULL ); - assert( bytesToRecv > 0 ); - - /* Get receive timeout from the socket to use as the timeout for #select. */ - pPlaintextParams = pNetworkContext->pParams; - - /* Initialize the file descriptor. - * #POLLPRI corresponds to high-priority data while #POLLIN corresponds - * to any other data that may be read. */ - pollFds.events = POLLIN | POLLPRI; - pollFds.revents = 0; - /* Set the file descriptor for poll. */ - pollFds.fd = pPlaintextParams->socketDescriptor; - - /* Speculative read for the start of a payload. - * Note: This is done to avoid blocking when - * no data is available to be read from the socket. */ - if( bytesToRecv == 1U ) + if( pNetworkContext == NULL ) { - /* Check if there is data to read (without blocking) from the socket. */ - pollStatus = poll( &pollFds, 1, 0 ); + LogError( ( "Parameter check failed: pNetworkContext is NULL." ) ); + bytesReceived = -1; } - - if( pollStatus > 0 ) + else if( pNetworkContext->pParams == NULL ) { - /* The socket is available for receiving data. */ - bytesReceived = ( int32_t ) recv( pPlaintextParams->socketDescriptor, - pBuffer, - bytesToRecv, - 0 ); + LogError( ( "Parameter check failed: pNetworkContext->pParams is NULL." ) ); + bytesReceived = -1; } - else if( pollStatus < 0 ) + else if( pBuffer == NULL ) { - /* An error occurred while polling. */ + LogError( ( "Parameter check failed: pBuffer is NULL." ) ); bytesReceived = -1; } - else + else if( bytesToRecv == 0 ) { - /* No data available to receive. */ bytesReceived = 0; } - - /* Note: A zero value return from recv() represents - * closure of TCP connection by the peer. */ - if( pollStatus > 0 ) + else { - if( bytesReceived == 0 ) + /* Get receive timeout from the socket to use as the timeout for #select. */ + pPlaintextParams = pNetworkContext->pParams; + + /* Initialize the file descriptor. + * #POLLPRI corresponds to high-priority data while #POLLIN corresponds + * to any other data that may be read. */ + pollFds.events = POLLIN | POLLPRI; + pollFds.revents = 0; + /* Set the file descriptor for poll. */ + pollFds.fd = pPlaintextParams->socketDescriptor; + + /* Speculative read for the start of a payload. + * Note: This is done to avoid blocking when + * no data is available to be read from the socket. */ + if( bytesToRecv == 1U ) + { + /* Check if there is data to read (without blocking) from the socket. */ + pollStatus = poll( &pollFds, 1, 0 ); + } + + if( pollStatus > 0 ) + { + /* The socket is available for receiving data. */ + bytesReceived = ( int32_t ) recv( pPlaintextParams->socketDescriptor, + pBuffer, + bytesToRecv, + 0 ); + } + else if( pollStatus < 0 ) + { + /* An error occurred while polling. */ + bytesReceived = -1; + } + else + { + /* No data available to receive. */ + bytesReceived = 0; + } + + /* Note: A zero value return from recv() represents + * closure of TCP connection by the peer. */ + if( ( pollStatus > 0 ) && ( bytesReceived == 0 ) ) { /* Peer has closed the connection. Treat as an error. */ bytesReceived = -1; } - else if( bytesReceived < 0 ) + else if( ( pollStatus > 0 ) && ( bytesReceived < 0 ) ) { /* The socket blocked for timeout and no data is received situation. * Return zero to indicate this operation can be retried. */ @@ -186,19 +201,15 @@ int32_t Plaintext_Recv( NetworkContext_t * pNetworkContext, } #endif } + else if( bytesReceived < 0 ) + { + logTransportError( errno ); + } else { /* Empty else MISRA 15.7 */ } } - else if( bytesReceived < 0 ) - { - logTransportError( errno ); - } - else - { - /* Empty else MISRA 15.7 */ - } return bytesReceived; } @@ -215,52 +226,67 @@ int32_t Plaintext_Send( NetworkContext_t * pNetworkContext, int32_t bytesSent = -1, pollStatus = -1; struct pollfd pollFds; - assert( pNetworkContext != NULL && pNetworkContext->pParams != NULL ); - assert( pBuffer != NULL ); - assert( bytesToSend > 0 ); - - /* Get send timeout from the socket to use as the timeout for #select. */ - pPlaintextParams = pNetworkContext->pParams; - - /* Initialize the file descriptor. */ - pollFds.events = POLLOUT; - pollFds.revents = 0; - /* Set the file descriptor for poll. */ - pollFds.fd = pPlaintextParams->socketDescriptor; - - /* Check if data can be written to the socket. - * Note: This is done to avoid blocking on send() when - * the socket is not ready to accept more data for network - * transmission (possibly due to a full TX buffer). */ - pollStatus = poll( &pollFds, 1, 0 ); - - if( pollStatus > 0 ) + if( pNetworkContext == NULL ) { - /* The socket is available for sending data. */ - bytesSent = ( int32_t ) send( pPlaintextParams->socketDescriptor, - pBuffer, - bytesToSend, - 0 ); + LogError( ( "Parameter check failed: pNetworkContext is NULL." ) ); + bytesSent = -1; } - else if( pollStatus < 0 ) + else if( pNetworkContext->pParams == NULL ) { - /* An error occurred while polling. */ + LogError( ( "Parameter check failed: pNetworkContext->pParams is NULL." ) ); bytesSent = -1; } - else + else if( pBuffer == NULL ) + { + LogError( ( "Parameter check failed: pBuffer is NULL." ) ); + bytesSent = -1; + } + else if( bytesToSend == 0 ) { - /* Socket is not available for sending data. */ bytesSent = 0; } - - if( pollStatus > 0 ) + else { - if( bytesSent == 0 ) + /* Get send timeout from the socket to use as the timeout for #select. */ + pPlaintextParams = pNetworkContext->pParams; + + /* Initialize the file descriptor. */ + pollFds.events = POLLOUT; + pollFds.revents = 0; + /* Set the file descriptor for poll. */ + pollFds.fd = pPlaintextParams->socketDescriptor; + + /* Check if data can be written to the socket. + * Note: This is done to avoid blocking on send() when + * the socket is not ready to accept more data for network + * transmission (possibly due to a full TX buffer). */ + pollStatus = poll( &pollFds, 1, 0 ); + + if( pollStatus > 0 ) + { + /* The socket is available for sending data. */ + bytesSent = ( int32_t ) send( pPlaintextParams->socketDescriptor, + pBuffer, + bytesToSend, + 0 ); + } + else if( pollStatus < 0 ) + { + /* An error occurred while polling. */ + bytesSent = -1; + } + else + { + /* Socket is not available for sending data. */ + bytesSent = 0; + } + + if( ( pollStatus > 0 ) && ( bytesSent == 0 ) ) { /* Peer has closed the connection. Treat as an error. */ bytesSent = -1; } - else if( bytesSent < 0 ) + else if( ( pollStatus > 0 ) && ( bytesSent < 0 ) ) { /* The socket blocked for timeout and no data is received situation. * Return zero to indicate this operation can be retried. */ @@ -276,19 +302,15 @@ int32_t Plaintext_Send( NetworkContext_t * pNetworkContext, } #endif } + else if( bytesSent < 0 ) + { + logTransportError( errno ); + } else { /* Empty else MISRA 15.7 */ } } - else if( bytesSent < 0 ) - { - logTransportError( errno ); - } - else - { - /* Empty else MISRA 15.7 */ - } return bytesSent; } diff --git a/platform/posix/transport/utest/plaintext_utest.c b/platform/posix/transport/utest/plaintext_utest.c index 14d75fb9c9..94cae47caf 100644 --- a/platform/posix/transport/utest/plaintext_utest.c +++ b/platform/posix/transport/utest/plaintext_utest.c @@ -175,6 +175,30 @@ void test_Plaintext_Disconnect_Invalid_Params( void ) TEST_ASSERT_EQUAL( SOCKETS_INVALID_PARAMETER, socketStatus ); } +/** + * @brief Test that invalid parameters returns an error. + */ +void test_Plaintext_Recv_Invalid_Params( void ) +{ + int32_t bytesReceived; + NetworkContext_t networkContext = { 0 }; + + bytesReceived = Plaintext_Recv( NULL, plaintextBuffer, BUFFER_LEN ); + TEST_ASSERT_EQUAL( -1, bytesReceived ); + + networkContext.pParams = NULL; + bytesReceived = Plaintext_Recv( &networkContext, plaintextBuffer, BUFFER_LEN ); + TEST_ASSERT_EQUAL( -1, bytesReceived ); + + networkContext.pParams = &plaintextParams; + bytesReceived = Plaintext_Recv( &networkContext, NULL, BUFFER_LEN ); + TEST_ASSERT_EQUAL( -1, bytesReceived ); + + networkContext.pParams = &plaintextParams; + bytesReceived = Plaintext_Recv( &networkContext, plaintextBuffer, 0 ); + TEST_ASSERT_EQUAL( 0, bytesReceived ); +} + /** * @brief Test that #Plaintext_Recv returns an error when #recv fails to * receive data over the network stack. @@ -280,6 +304,29 @@ void test_Plaintext_Recv_Socket_Timeout_Blocked( void ) TEST_ASSERT_EQUAL( 0, bytesReceived ); } +/** + * @brief Test that invalid parameters returns an error. + */ +void test_Plaintext_Send_Invalid_Params( void ) +{ + int32_t bytesSent; + NetworkContext_t networkContext = { 0 }; + + bytesSent = Plaintext_Send( NULL, plaintextBuffer, BUFFER_LEN ); + TEST_ASSERT_EQUAL( -1, bytesSent ); + + networkContext.pParams = NULL; + bytesSent = Plaintext_Send( &networkContext, plaintextBuffer, BUFFER_LEN ); + TEST_ASSERT_EQUAL( -1, bytesSent ); + + networkContext.pParams = &plaintextParams; + bytesSent = Plaintext_Send( &networkContext, NULL, BUFFER_LEN ); + TEST_ASSERT_EQUAL( -1, bytesSent ); + + bytesSent = Plaintext_Send( &networkContext, plaintextBuffer, 0 ); + TEST_ASSERT_EQUAL( 0, bytesSent ); +} + /** * @brief Test the happy path case when #Plaintext_Send is able to send all bytes * over the network stack successfully. From 47116ae952699001b3643b0a864bcb8cf3bd21d3 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 14 Jan 2022 11:02:41 +0800 Subject: [PATCH 07/10] Update zero byte to send/recv error --- platform/posix/transport/src/plaintext_posix.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/platform/posix/transport/src/plaintext_posix.c b/platform/posix/transport/src/plaintext_posix.c index 86017be1ef..a74fe91f9f 100644 --- a/platform/posix/transport/src/plaintext_posix.c +++ b/platform/posix/transport/src/plaintext_posix.c @@ -135,7 +135,8 @@ int32_t Plaintext_Recv( NetworkContext_t * pNetworkContext, } else if( bytesToRecv == 0 ) { - bytesReceived = 0; + LogError( ( "Parameter check failed: bytesReceived is zero." ) ); + bytesReceived = -1; } else { @@ -243,7 +244,8 @@ int32_t Plaintext_Send( NetworkContext_t * pNetworkContext, } else if( bytesToSend == 0 ) { - bytesSent = 0; + LogError( ( "Parameter check failed: bytesReceived is zero." ) ); + bytesSent = -1; } else { From db7bdfb87ef42f90531e449013621c67fbb97cb5 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 14 Jan 2022 11:20:46 +0800 Subject: [PATCH 08/10] Update zero byte to send/recv return value --- platform/posix/transport/utest/plaintext_utest.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform/posix/transport/utest/plaintext_utest.c b/platform/posix/transport/utest/plaintext_utest.c index 94cae47caf..8825b832c2 100644 --- a/platform/posix/transport/utest/plaintext_utest.c +++ b/platform/posix/transport/utest/plaintext_utest.c @@ -196,7 +196,7 @@ void test_Plaintext_Recv_Invalid_Params( void ) networkContext.pParams = &plaintextParams; bytesReceived = Plaintext_Recv( &networkContext, plaintextBuffer, 0 ); - TEST_ASSERT_EQUAL( 0, bytesReceived ); + TEST_ASSERT_EQUAL( -1, bytesReceived ); } /** @@ -324,7 +324,7 @@ void test_Plaintext_Send_Invalid_Params( void ) TEST_ASSERT_EQUAL( -1, bytesSent ); bytesSent = Plaintext_Send( &networkContext, plaintextBuffer, 0 ); - TEST_ASSERT_EQUAL( 0, bytesSent ); + TEST_ASSERT_EQUAL( -1, bytesSent ); } /** From cea8faa2361452429a24693e829a6145403ffd0e Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 14 Jan 2022 11:26:07 +0800 Subject: [PATCH 09/10] Updat log typo --- platform/posix/transport/src/plaintext_posix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform/posix/transport/src/plaintext_posix.c b/platform/posix/transport/src/plaintext_posix.c index a74fe91f9f..0cacf460dd 100644 --- a/platform/posix/transport/src/plaintext_posix.c +++ b/platform/posix/transport/src/plaintext_posix.c @@ -135,7 +135,7 @@ int32_t Plaintext_Recv( NetworkContext_t * pNetworkContext, } else if( bytesToRecv == 0 ) { - LogError( ( "Parameter check failed: bytesReceived is zero." ) ); + LogError( ( "Parameter check failed: bytesToRecv is zero." ) ); bytesReceived = -1; } else @@ -244,7 +244,7 @@ int32_t Plaintext_Send( NetworkContext_t * pNetworkContext, } else if( bytesToSend == 0 ) { - LogError( ( "Parameter check failed: bytesReceived is zero." ) ); + LogError( ( "Parameter check failed: bytesToSend is zero." ) ); bytesSent = -1; } else From bd5c206d71e71c79585707ec794df0ece2e08f1d Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 14 Jan 2022 11:28:50 +0800 Subject: [PATCH 10/10] Update typo in comment --- platform/posix/transport/src/plaintext_posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/posix/transport/src/plaintext_posix.c b/platform/posix/transport/src/plaintext_posix.c index 0cacf460dd..711e4559aa 100644 --- a/platform/posix/transport/src/plaintext_posix.c +++ b/platform/posix/transport/src/plaintext_posix.c @@ -290,7 +290,7 @@ int32_t Plaintext_Send( NetworkContext_t * pNetworkContext, } else if( ( pollStatus > 0 ) && ( bytesSent < 0 ) ) { - /* The socket blocked for timeout and no data is received situation. + /* The socket blocked for timeout and no data is sent situation. * Return zero to indicate this operation can be retried. */ #if EAGAIN == EWOULDBLOCK if( errno == EAGAIN )