Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/doxygen/include/size_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
</tr>
<tr>
<td>cellular_common_api.c</td>
<td><center>0.7K</center></td>
<td><center>0.7K</center></td>
<td><center>0.9K</center></td>
<td><center>0.8K</center></td>
</tr>
<tr>
<td>cellular_common.c</td>
Expand All @@ -44,7 +44,7 @@
</tr>
<tr>
<td><b>Total estimates</b></td>
<td><b><center>15.4K</center></b></td>
<td><b><center>13.9K</center></b></td>
<td><b><center>15.6K</center></b></td>
<td><b><center>14.0K</center></b></td>
</tr>
</table>
60 changes: 54 additions & 6 deletions source/cellular_common_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,24 @@ CellularError_t Cellular_CommonSocketRegisterDataReadyCallback( CellularHandle_t
}
else
{
socketHandle->dataReadyCallback = dataReadyCallback;
socketHandle->pDataReadyCallbackContext = pCallbackContext;
uint8_t socketId;
cellularStatus = CELLULAR_INVALID_HANDLE;

taskENTER_CRITICAL();

/* Verify the socket handle is still alive. */
for( socketId = 0; socketId < CELLULAR_NUM_SOCKET_MAX; socketId++ )
{
if( pContext->pSocketData[ socketId ] == socketHandle )
Comment thread
kstribrnAmzn marked this conversation as resolved.
Resolved
{
socketHandle->dataReadyCallback = dataReadyCallback;
socketHandle->pDataReadyCallbackContext = pCallbackContext;
cellularStatus = CELLULAR_SUCCESS;
break;
}
}

taskEXIT_CRITICAL();
}

return cellularStatus;
Expand Down Expand Up @@ -526,8 +542,24 @@ CellularError_t Cellular_CommonSocketRegisterSocketOpenCallback( CellularHandle_
}
else
{
socketHandle->openCallback = socketOpenCallback;
socketHandle->pOpenCallbackContext = pCallbackContext;
uint8_t socketId;
cellularStatus = CELLULAR_INVALID_HANDLE;

taskENTER_CRITICAL();

/* Verify the socket handle is still alive. */
for( socketId = 0; socketId < CELLULAR_NUM_SOCKET_MAX; socketId++ )
{
if( pContext->pSocketData[ socketId ] == socketHandle )
{
socketHandle->openCallback = socketOpenCallback;
socketHandle->pOpenCallbackContext = pCallbackContext;
cellularStatus = CELLULAR_SUCCESS;
break;
}
}

taskEXIT_CRITICAL();
}

return cellularStatus;
Expand Down Expand Up @@ -556,8 +588,24 @@ CellularError_t Cellular_CommonSocketRegisterClosedCallback( CellularHandle_t ce
}
else
{
socketHandle->closedCallback = closedCallback;
socketHandle->pClosedCallbackContext = pCallbackContext;
uint8_t socketId;
cellularStatus = CELLULAR_INVALID_HANDLE;

taskENTER_CRITICAL();

/* Verify the socket handle is still alive. */
for( socketId = 0; socketId < CELLULAR_NUM_SOCKET_MAX; socketId++ )
{
if( pContext->pSocketData[ socketId ] == socketHandle )
{
socketHandle->closedCallback = closedCallback;
socketHandle->pClosedCallbackContext = pCallbackContext;
cellularStatus = CELLULAR_SUCCESS;
break;
}
}

taskEXIT_CRITICAL();
}

return cellularStatus;
Expand Down
221 changes: 221 additions & 0 deletions test/unit-test/cellular_common_api_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ int suiteTearDown( int numFailures )

/* ========================================================================== */

void dummyTaskENTER_CRITICAL( void )
{
}

void dummyTaskEXIT_CRITICAL( void )
{
}

void MockPlatformMutex_Unlock( PlatformMutex_t * pMutex )
{
( void ) pMutex;
Expand Down Expand Up @@ -1132,6 +1140,38 @@ void test_Cellular_CommonSocketRegisterDataReadyCallback_Happy_Path( void )
memset( &context, 0, sizeof( CellularContext_t ) );
struct CellularSocketContext socketHandle;

/* The callback setter only writes through a handle which is still in the
* socket table. Place the handle in the table to represent a live socket. */
context.pSocketData[ 0 ] = &socketHandle;

_Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS );

cellularStatus = Cellular_CommonSocketRegisterDataReadyCallback( &context, &socketHandle,
cellularSocketDataReadyCallback,
testCallback );

TEST_ASSERT_EQUAL( CELLULAR_SUCCESS, cellularStatus );
TEST_ASSERT_EQUAL( socketHandle.dataReadyCallback, cellularSocketDataReadyCallback );
TEST_ASSERT_EQUAL( socketHandle.pDataReadyCallbackContext, testCallback );
}

/**
* @brief Test that a handle held in the last socket table slot is accepted by
* Cellular_CommonSocketRegisterDataReadyCallback.
*
* This walks the whole socket table before the match, which exercises the
* liveness scan beyond its first iteration.
*/
void test_Cellular_CommonSocketRegisterDataReadyCallback_Last_Socket_Slot_Happy_Path( void )
{
CellularError_t cellularStatus = CELLULAR_SUCCESS;
CellularContext_t context;

memset( &context, 0, sizeof( CellularContext_t ) );
struct CellularSocketContext socketHandle;

context.pSocketData[ CELLULAR_NUM_SOCKET_MAX - 1 ] = &socketHandle;

_Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS );

cellularStatus = Cellular_CommonSocketRegisterDataReadyCallback( &context, &socketHandle,
Expand All @@ -1143,6 +1183,45 @@ void test_Cellular_CommonSocketRegisterDataReadyCallback_Happy_Path( void )
TEST_ASSERT_EQUAL( socketHandle.pDataReadyCallbackContext, testCallback );
}

/**
* @brief Test that a socket handle which is no longer in the socket table is
* rejected by Cellular_CommonSocketRegisterDataReadyCallback.
*
* _Cellular_RemoveSocketData clears the socket's slot in pSocketData before
* freeing it, so a handle which is absent from the table is either stale or
* never belonged to this context. The setter must return
* CELLULAR_INVALID_HANDLE and must not write through the handle.
*/
void test_Cellular_CommonSocketRegisterDataReadyCallback_Stale_socketHandle( void )
{
CellularError_t cellularStatus = CELLULAR_SUCCESS;
CellularContext_t context;
uint8_t socketId;

memset( &context, 0, sizeof( CellularContext_t ) );
struct CellularSocketContext socketHandle;
struct CellularSocketContext liveSocketHandle;

memset( &socketHandle, 0, sizeof( struct CellularSocketContext ) );

/* Populate every slot with a different live socket so that the scan has to
* reject the handle on table membership rather than on an empty table. */
for( socketId = 0; socketId < CELLULAR_NUM_SOCKET_MAX; socketId++ )
{
context.pSocketData[ socketId ] = &liveSocketHandle;
}

_Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS );

cellularStatus = Cellular_CommonSocketRegisterDataReadyCallback( &context, &socketHandle,
cellularSocketDataReadyCallback,
testCallback );

TEST_ASSERT_EQUAL( CELLULAR_INVALID_HANDLE, cellularStatus );
TEST_ASSERT_EQUAL( NULL, socketHandle.dataReadyCallback );
TEST_ASSERT_EQUAL( NULL, socketHandle.pDataReadyCallbackContext );
}

/**
* @brief Test that null handler case for Cellular_CommonSocketRegisterSocketOpenCallback.
*/
Expand Down Expand Up @@ -1187,6 +1266,38 @@ void test_Cellular_CommonSocketRegisterSocketOpenCallback_Happy_Path( void )
memset( &context, 0, sizeof( CellularContext_t ) );
struct CellularSocketContext socketHandle;

/* The callback setter only writes through a handle which is still in the
* socket table. Place the handle in the table to represent a live socket. */
context.pSocketData[ 0 ] = &socketHandle;

_Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS );

cellularStatus = Cellular_CommonSocketRegisterSocketOpenCallback( &context, &socketHandle,
cellularSocketOpenCallback,
testCallback );

TEST_ASSERT_EQUAL( CELLULAR_SUCCESS, cellularStatus );
TEST_ASSERT_EQUAL( socketHandle.openCallback, cellularSocketOpenCallback );
TEST_ASSERT_EQUAL( socketHandle.pOpenCallbackContext, testCallback );
}

/**
* @brief Test that a handle held in the last socket table slot is accepted by
* Cellular_CommonSocketRegisterSocketOpenCallback.
*
* This walks the whole socket table before the match, which exercises the
* liveness scan beyond its first iteration.
*/
void test_Cellular_CommonSocketRegisterSocketOpenCallback_Last_Socket_Slot_Happy_Path( void )
{
CellularError_t cellularStatus = CELLULAR_SUCCESS;
CellularContext_t context;

memset( &context, 0, sizeof( CellularContext_t ) );
struct CellularSocketContext socketHandle;

context.pSocketData[ CELLULAR_NUM_SOCKET_MAX - 1 ] = &socketHandle;

_Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS );

cellularStatus = Cellular_CommonSocketRegisterSocketOpenCallback( &context, &socketHandle,
Expand All @@ -1198,6 +1309,45 @@ void test_Cellular_CommonSocketRegisterSocketOpenCallback_Happy_Path( void )
TEST_ASSERT_EQUAL( socketHandle.pOpenCallbackContext, testCallback );
}

/**
* @brief Test that a socket handle which is no longer in the socket table is
* rejected by Cellular_CommonSocketRegisterSocketOpenCallback.
*
* _Cellular_RemoveSocketData clears the socket's slot in pSocketData before
* freeing it, so a handle which is absent from the table is either stale or
* never belonged to this context. The setter must return
* CELLULAR_INVALID_HANDLE and must not write through the handle.
*/
void test_Cellular_CommonSocketRegisterSocketOpenCallback_Stale_socketHandle( void )
{
CellularError_t cellularStatus = CELLULAR_SUCCESS;
CellularContext_t context;
uint8_t socketId;

memset( &context, 0, sizeof( CellularContext_t ) );
struct CellularSocketContext socketHandle;
struct CellularSocketContext liveSocketHandle;

memset( &socketHandle, 0, sizeof( struct CellularSocketContext ) );

/* Populate every slot with a different live socket so that the scan has to
* reject the handle on table membership rather than on an empty table. */
for( socketId = 0; socketId < CELLULAR_NUM_SOCKET_MAX; socketId++ )
{
context.pSocketData[ socketId ] = &liveSocketHandle;
}

_Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS );

cellularStatus = Cellular_CommonSocketRegisterSocketOpenCallback( &context, &socketHandle,
cellularSocketOpenCallback,
testCallback );

TEST_ASSERT_EQUAL( CELLULAR_INVALID_HANDLE, cellularStatus );
TEST_ASSERT_EQUAL( NULL, socketHandle.openCallback );
TEST_ASSERT_EQUAL( NULL, socketHandle.pOpenCallbackContext );
}

/**
* @brief Test that null handler case for Cellular_CommonSocketRegisterClosedCallback.
*/
Expand Down Expand Up @@ -1242,6 +1392,38 @@ void test_Cellular_CommonSocketRegisterClosedCallback_Happy_Path( void )
memset( &context, 0, sizeof( CellularContext_t ) );
struct CellularSocketContext socketHandle;

/* The callback setter only writes through a handle which is still in the
* socket table. Place the handle in the table to represent a live socket. */
context.pSocketData[ 0 ] = &socketHandle;

_Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS );

cellularStatus = Cellular_CommonSocketRegisterClosedCallback( &context, &socketHandle,
cellularSocketClosedCallback,
testCallback );

TEST_ASSERT_EQUAL( CELLULAR_SUCCESS, cellularStatus );
TEST_ASSERT_EQUAL( socketHandle.closedCallback, cellularSocketClosedCallback );
TEST_ASSERT_EQUAL( socketHandle.pClosedCallbackContext, testCallback );
}

/**
* @brief Test that a handle held in the last socket table slot is accepted by
* Cellular_CommonSocketRegisterClosedCallback.
*
* This walks the whole socket table before the match, which exercises the
* liveness scan beyond its first iteration.
*/
void test_Cellular_CommonSocketRegisterClosedCallback_Last_Socket_Slot_Happy_Path( void )
{
CellularError_t cellularStatus = CELLULAR_SUCCESS;
CellularContext_t context;

memset( &context, 0, sizeof( CellularContext_t ) );
struct CellularSocketContext socketHandle;

context.pSocketData[ CELLULAR_NUM_SOCKET_MAX - 1 ] = &socketHandle;

_Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS );

cellularStatus = Cellular_CommonSocketRegisterClosedCallback( &context, &socketHandle,
Expand All @@ -1253,6 +1435,45 @@ void test_Cellular_CommonSocketRegisterClosedCallback_Happy_Path( void )
TEST_ASSERT_EQUAL( socketHandle.pClosedCallbackContext, testCallback );
}

/**
* @brief Test that a socket handle which is no longer in the socket table is
* rejected by Cellular_CommonSocketRegisterClosedCallback.
*
* _Cellular_RemoveSocketData clears the socket's slot in pSocketData before
* freeing it, so a handle which is absent from the table is either stale or
* never belonged to this context. The setter must return
* CELLULAR_INVALID_HANDLE and must not write through the handle.
*/
void test_Cellular_CommonSocketRegisterClosedCallback_Stale_socketHandle( void )
{
CellularError_t cellularStatus = CELLULAR_SUCCESS;
CellularContext_t context;
uint8_t socketId;

memset( &context, 0, sizeof( CellularContext_t ) );
struct CellularSocketContext socketHandle;
struct CellularSocketContext liveSocketHandle;

memset( &socketHandle, 0, sizeof( struct CellularSocketContext ) );

/* Populate every slot with a different live socket so that the scan has to
* reject the handle on table membership rather than on an empty table. */
for( socketId = 0; socketId < CELLULAR_NUM_SOCKET_MAX; socketId++ )
{
context.pSocketData[ socketId ] = &liveSocketHandle;
}

_Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS );

cellularStatus = Cellular_CommonSocketRegisterClosedCallback( &context, &socketHandle,
cellularSocketClosedCallback,
testCallback );

TEST_ASSERT_EQUAL( CELLULAR_INVALID_HANDLE, cellularStatus );
TEST_ASSERT_EQUAL( NULL, socketHandle.closedCallback );
TEST_ASSERT_EQUAL( NULL, socketHandle.pClosedCallbackContext );
}

/**
* @brief Test that option set local port happy path case for Cellular_CommonSocketSetSockOpt.
*/
Expand Down
Loading