Skip to content

Commit

Permalink
Fix UT coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ActoryOu committed Oct 17, 2024
1 parent 3fee861 commit 341d974
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 39 deletions.
8 changes: 6 additions & 2 deletions source/FreeRTOS_IP.c
Original file line number Diff line number Diff line change
Expand Up @@ -1798,8 +1798,7 @@ static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetwor

break;
}

if( pxEthernetHeader->usFrameType == ipIPv6_FRAME_TYPE )
else if( pxEthernetHeader->usFrameType == ipIPv6_FRAME_TYPE )
{
#if ipconfigIS_ENABLED( ipconfigUSE_IPv6 )
if( pxNDWaitingNetworkBuffer == NULL )
Expand All @@ -1820,6 +1819,11 @@ static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetwor

break;
}
else
{
/* Unknown frame type, drop the packet. */
vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
}

break;

Expand Down
2 changes: 0 additions & 2 deletions source/FreeRTOS_ND.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,6 @@
*/
void FreeRTOS_ClearND( const struct xNetworkEndPoint * pxEndPoint )
{
( void ) memset( xNDCache, 0, sizeof( xNDCache ) );

if( pxEndPoint != NULL )
{
BaseType_t x;
Expand Down
15 changes: 3 additions & 12 deletions test/unit-test/FreeRTOS_ARP/FreeRTOS_ARP_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -1104,10 +1104,7 @@ void test_eARPProcessPacket_Reply_SenderAndTargetSame( void )
xARPHadIPClash = pdFALSE;

/* Let there be no timeout. Let the EndPoint be NULL */
pxGetNetworkBufferWithDescriptor_ExpectAndReturn( sizeof( ARPPacket_t ), 0, &xNetworkBuffer );
vReleaseNetworkBufferAndDescriptor_Expect( &xNetworkBuffer );
xTaskGetTickCount_ExpectAndReturn( 0 );
vTaskSetTimeOutState_ExpectAnyArgs();
xTaskCheckForTimeOut_ExpectAnyArgsAndReturn( pdFAIL );
FreeRTOS_FindEndPointOnIP_IPv4_ExpectAnyArgsAndReturn( NULL );

eResult = eARPProcessPacket( &xNetworkBuffer );
Expand All @@ -1118,10 +1115,7 @@ void test_eARPProcessPacket_Reply_SenderAndTargetSame( void )
/* Reset the flag. */
xARPHadIPClash = pdFALSE;
/* Let there be no timeout. */
pxGetNetworkBufferWithDescriptor_ExpectAndReturn( sizeof( ARPPacket_t ), 0, &xNetworkBuffer );
vReleaseNetworkBufferAndDescriptor_Expect( &xNetworkBuffer );
xTaskGetTickCount_ExpectAndReturn( 0 );
vTaskSetTimeOutState_ExpectAnyArgs();
xTaskCheckForTimeOut_ExpectAnyArgsAndReturn( pdFAIL );
FreeRTOS_FindEndPointOnIP_IPv4_ExpectAnyArgsAndReturn( &xEndPoint );

/* Call it again and do not expect the task functions to be called. */
Expand All @@ -1135,10 +1129,7 @@ void test_eARPProcessPacket_Reply_SenderAndTargetSame( void )
xEndPoint_2.ipv4_settings.ulIPAddress = ( uint32_t ) *( pxARPFrame->xARPHeader.ucSenderProtocolAddress + 0x11 );

/* Let there be no timeout. Let the EndPoint be NULL */
pxGetNetworkBufferWithDescriptor_ExpectAndReturn( sizeof( ARPPacket_t ), 0, &xNetworkBuffer );
vReleaseNetworkBufferAndDescriptor_Expect( &xNetworkBuffer );
xTaskGetTickCount_ExpectAndReturn( 0 );
vTaskSetTimeOutState_ExpectAnyArgs();
xTaskCheckForTimeOut_ExpectAnyArgsAndReturn( pdFAIL );
FreeRTOS_FindEndPointOnIP_IPv4_ExpectAnyArgsAndReturn( &xEndPoint_2 );

eResult = eARPProcessPacket( &xNetworkBuffer );
Expand Down
2 changes: 1 addition & 1 deletion test/unit-test/FreeRTOS_ARP/ut.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ list(APPEND mock_define_list

# list the files you would like to test here
list(APPEND real_source_files
${CMAKE_BINARY_DIR}/Annexed_TCP_Sources/${project_name}.c
${MODULE_ROOT_DIR}/source/FreeRTOS_ARP.c
)
# list the directories the module under test includes
list(APPEND real_include_directories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ list(APPEND mock_define_list
# list the files you would like to test here
set(real_source_files "")
list(APPEND real_source_files
${CMAKE_BINARY_DIR}/Annexed_TCP_Sources/FreeRTOS_ARP.c
${MODULE_ROOT_DIR}/source/FreeRTOS_ARP.c
)
# list the directories the module under test includes
set(real_include_directories "")
Expand Down
116 changes: 116 additions & 0 deletions test/unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -2387,6 +2387,122 @@ void test_prvProcessEthernetPacket_InterfaceNull( void )
prvProcessEthernetPacket( pxNetworkBuffer );
}

/**
* @brief test_prvProcessEthernetPacket_IPv4FrameType_NeedARPResolution
* To validate the flow to handle IPv4 packets and it needs ARP resolution.
* But we already have one ARP packet pending so that buffer got released
* at the end.
*/
void test_prvProcessEthernetPacket_IPv4FrameType_NeedARPResolution( void )
{
NetworkBufferDescriptor_t xNetworkBuffer;
NetworkBufferDescriptor_t * pxNetworkBuffer = &xNetworkBuffer;
uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ] = { 0 };
EthernetHeader_t * pxEthernetHeader;
IPPacket_t * pxIPPacket;
IPHeader_t * pxIPHeader;
struct xNetworkInterface xInterface;
NetworkEndPoint_t xNetworkEndPoint = { 0 };

pxNetworkBuffer->xDataLength = ipconfigTCP_MSS;
pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
pxNetworkBuffer->pxInterface = &xInterface;
pxNetworkBuffer->pxEndPoint = &xNetworkEndPoint;

pxEthernetHeader = ( EthernetHeader_t * ) pxNetworkBuffer->pucEthernetBuffer;
pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE;

pxIPPacket = ( IPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
pxIPHeader = &( pxIPPacket->xIPHeader );
pxIPHeader->ucVersionHeaderLength = 0x45;

pxARPWaitingNetworkBuffer = ( NetworkBufferDescriptor_t * ) 0x1234ABCD;

prvAllowIPPacketIPv4_ExpectAndReturn( pxIPPacket, pxNetworkBuffer, ( pxIPHeader->ucVersionHeaderLength & 0x0FU ) << 2, eProcessBuffer );
xCheckRequiresResolution_ExpectAndReturn( pxNetworkBuffer, pdTRUE );
vReleaseNetworkBufferAndDescriptor_Expect( pxNetworkBuffer );

prvProcessEthernetPacket( pxNetworkBuffer );
}

/**
* @brief test_prvProcessEthernetPacket_IPv6FrameType_NeedNDResolution
* To validate the flow to handle IPv4 packets and it needs ND resolution.
* But we already have one ND packet pending so that buffer got released
* at the end.
*/
void test_prvProcessEthernetPacket_IPv6FrameType_NeedNDResolution( void )
{
NetworkBufferDescriptor_t xNetworkBuffer;
NetworkBufferDescriptor_t * pxNetworkBuffer = &xNetworkBuffer;
uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ] = { 0 };
EthernetHeader_t * pxEthernetHeader;
IPPacket_IPv6_t * pxIPv6Packet;
IPHeader_IPv6_t * pxIPv6Header;
struct xNetworkInterface xInterface;
NetworkEndPoint_t xNetworkEndPoint = { 0 };

pxNetworkBuffer->xDataLength = ipconfigTCP_MSS;
pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
pxNetworkBuffer->pxInterface = &xInterface;
pxNetworkBuffer->pxEndPoint = &xNetworkEndPoint;

pxEthernetHeader = ( EthernetHeader_t * ) pxNetworkBuffer->pucEthernetBuffer;
pxEthernetHeader->usFrameType = ipIPv6_FRAME_TYPE;

pxIPv6Packet = ( IPPacket_IPv6_t * ) pxNetworkBuffer->pucEthernetBuffer;
pxIPv6Header = &( pxIPv6Packet->xIPHeader );
pxIPv6Header->ucNextHeader = ipPROTOCOL_TCP;

pxNDWaitingNetworkBuffer = ( NetworkBufferDescriptor_t * ) 0x1234ABCD;

prvAllowIPPacketIPv6_ExpectAndReturn( pxIPv6Header, pxNetworkBuffer, ipSIZE_OF_IPv6_HEADER, eProcessBuffer );
xGetExtensionOrder_ExpectAndReturn( ipPROTOCOL_TCP, 0, -1 );
xCheckRequiresResolution_ExpectAndReturn( pxNetworkBuffer, pdTRUE );
vReleaseNetworkBufferAndDescriptor_Expect( pxNetworkBuffer );

prvProcessEthernetPacket( pxNetworkBuffer );
}

/**
* @brief test_prvProcessEthernetPacket_IPv6FrameType_NeedNDResolution2
* To validate the flow to handle IPv6 packets and it needs ND resolution.
* And we don't have any pending ND packet.
*/
void test_prvProcessEthernetPacket_IPv6FrameType_NeedNDResolution2( void )
{
NetworkBufferDescriptor_t xNetworkBuffer;
NetworkBufferDescriptor_t * pxNetworkBuffer = &xNetworkBuffer;
uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ] = { 0 };
EthernetHeader_t * pxEthernetHeader;
IPPacket_IPv6_t * pxIPv6Packet;
IPHeader_IPv6_t * pxIPv6Header;
struct xNetworkInterface xInterface;
NetworkEndPoint_t xNetworkEndPoint = { 0 };

pxNetworkBuffer->xDataLength = ipconfigTCP_MSS;
pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
pxNetworkBuffer->pxInterface = &xInterface;
pxNetworkBuffer->pxEndPoint = &xNetworkEndPoint;

pxEthernetHeader = ( EthernetHeader_t * ) pxNetworkBuffer->pucEthernetBuffer;
pxEthernetHeader->usFrameType = ipIPv6_FRAME_TYPE;

pxIPv6Packet = ( IPPacket_IPv6_t * ) pxNetworkBuffer->pucEthernetBuffer;
pxIPv6Header = &( pxIPv6Packet->xIPHeader );
pxIPv6Header->ucNextHeader = ipPROTOCOL_TCP;

pxNDWaitingNetworkBuffer = NULL;

prvAllowIPPacketIPv6_ExpectAndReturn( pxIPv6Header, pxNetworkBuffer, ipSIZE_OF_IPv6_HEADER, eProcessBuffer );
xGetExtensionOrder_ExpectAndReturn( ipPROTOCOL_TCP, 0, -1 );
xCheckRequiresResolution_ExpectAndReturn( pxNetworkBuffer, pdTRUE );
vIPTimerStartNDResolution_ExpectAnyArgs();

prvProcessEthernetPacket( pxNetworkBuffer );

TEST_ASSERT_EQUAL_PTR( pxNetworkBuffer, pxNDWaitingNetworkBuffer );
}

/**
* @brief test_prvProcessIPPacket_HeaderLengthSmaller
Expand Down
4 changes: 3 additions & 1 deletion test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOSIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@
* because the packet will already have been passed into the stack). If the
* Ethernet driver does all the necessary filtering in hardware then software
* filtering can be removed by using a value other than 1 or 0. */
#define ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES 1
#define ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES 0

/* When ipconfigETHERNET_DRIVER_FILTERS_PACKETS is enabled, the network
* interface will inspect the incoming packets to see if they can be
Expand Down Expand Up @@ -348,4 +348,6 @@
#define ipconfigIP_PASS_PACKETS_WITH_IP_OPTIONS ( 0 )
#define ipconfigZERO_COPY_TX_DRIVER ( 1 )

#define ipconfigPROCESS_CUSTOM_ETHERNET_FRAMES ( 1 )

#endif /* FREERTOS_IP_CONFIG_H */
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
#include "mock_FreeRTOS_IP_Timers.h"
#include "mock_FreeRTOS_DHCP.h"
#include "mock_FreeRTOS_DHCPv6.h"
#include "mock_NetworkBufferManagement.h"
#include "mock_FreeRTOS_Routing.h"

#include "FreeRTOS_IP.h"

Expand Down Expand Up @@ -84,6 +86,14 @@ void tearDown( void )

/* ======================== Stub Callback Functions ========================= */

eFrameProcessingResult_t eApplicationProcessCustomFrameHook( NetworkBufferDescriptor_t * const pxNetworkBuffer )
{
( void ) ( pxNetworkBuffer );

/* Force hook function to return waiting resultion for unknown Ethernet frame type. */
return eWaitingResolution;
}

/* ============================== Test Cases ============================== */

/**
Expand Down Expand Up @@ -179,3 +189,60 @@ void test_prvProcessIPEventsAndTimers_eDHCPEvent_RA( void )

prvProcessIPEventsAndTimers();
}

/**
* @brief test_prvProcessEthernetPacket_UnknownFrameType_NeedResolution
* But we release the network buffer because the frame type is unknown.
*/
void test_prvProcessEthernetPacket_UnknownFrameType_NeedResolution( void )
{
NetworkBufferDescriptor_t xNetworkBuffer;
NetworkBufferDescriptor_t * pxNetworkBuffer = &xNetworkBuffer;
uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ] = { 0 };
EthernetHeader_t * pxEthernetHeader;
IPPacket_IPv6_t * pxIPv6Packet;
IPHeader_IPv6_t * pxIPv6Header;
struct xNetworkInterface xInterface;
NetworkEndPoint_t xNetworkEndPoint = { 0 };

pxNetworkBuffer->xDataLength = ipconfigTCP_MSS;
pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
pxNetworkBuffer->pxInterface = &xInterface;
pxNetworkBuffer->pxEndPoint = &xNetworkEndPoint;

pxEthernetHeader = ( EthernetHeader_t * ) pxNetworkBuffer->pucEthernetBuffer;
pxEthernetHeader->usFrameType = 0xFFFF;

vReleaseNetworkBufferAndDescriptor_Expect( pxNetworkBuffer );

prvProcessEthernetPacket( pxNetworkBuffer );
}

/**
* @brief test_prvProcessEthernetPacket_UnknownFrameType_NeedResolution
* But we release the network buffer because the frame type is unknown.
*/
void test_prvProcessEthernetPacket_IPv4FrameType_CheckFrameFail( void )
{
NetworkBufferDescriptor_t xNetworkBuffer;
NetworkBufferDescriptor_t * pxNetworkBuffer = &xNetworkBuffer;
uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ] = { 0 };
EthernetHeader_t * pxEthernetHeader;
IPPacket_IPv6_t * pxIPv6Packet;
IPHeader_IPv6_t * pxIPv6Header;
struct xNetworkInterface xInterface;
NetworkEndPoint_t xNetworkEndPoint = { 0 };

pxNetworkBuffer->xDataLength = ipconfigTCP_MSS;
pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
pxNetworkBuffer->pxInterface = &xInterface;
pxNetworkBuffer->pxEndPoint = &xNetworkEndPoint;

pxEthernetHeader = ( EthernetHeader_t * ) pxNetworkBuffer->pucEthernetBuffer;
pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE;

FreeRTOS_FindEndPointOnMAC_ExpectAndReturn( &pxEthernetHeader->xDestinationAddress, NULL, NULL );
vReleaseNetworkBufferAndDescriptor_Expect( pxNetworkBuffer );

prvProcessEthernetPacket( pxNetworkBuffer );
}
Loading

0 comments on commit 341d974

Please sign in to comment.