From 341d974539961683e4d41b64206e262c6d0bb499 Mon Sep 17 00:00:00 2001 From: ActoryOu Date: Thu, 17 Oct 2024 10:01:44 +0000 Subject: [PATCH] Fix UT coverage. --- source/FreeRTOS_IP.c | 8 +- source/FreeRTOS_ND.c | 2 - .../FreeRTOS_ARP/FreeRTOS_ARP_utest.c | 15 +- test/unit-test/FreeRTOS_ARP/ut.cmake | 2 +- .../ut.cmake | 2 +- .../unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c | 116 ++++++++ .../FreeRTOSIPConfig.h | 4 +- .../FreeRTOS_IP_DiffConfig3_utest.c | 67 +++++ .../FreeRTOS_IP_Timers_utest.c | 249 +++++++++++++++++- .../unit-test/FreeRTOS_ND/FreeRTOS_ND_utest.c | 73 +++-- 10 files changed, 499 insertions(+), 39 deletions(-) diff --git a/source/FreeRTOS_IP.c b/source/FreeRTOS_IP.c index fded6315d..fded8c851 100644 --- a/source/FreeRTOS_IP.c +++ b/source/FreeRTOS_IP.c @@ -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 ) @@ -1820,6 +1819,11 @@ static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetwor break; } + else + { + /* Unknown frame type, drop the packet. */ + vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer ); + } break; diff --git a/source/FreeRTOS_ND.c b/source/FreeRTOS_ND.c index 8c3458e2a..c09310322 100644 --- a/source/FreeRTOS_ND.c +++ b/source/FreeRTOS_ND.c @@ -423,8 +423,6 @@ */ void FreeRTOS_ClearND( const struct xNetworkEndPoint * pxEndPoint ) { - ( void ) memset( xNDCache, 0, sizeof( xNDCache ) ); - if( pxEndPoint != NULL ) { BaseType_t x; diff --git a/test/unit-test/FreeRTOS_ARP/FreeRTOS_ARP_utest.c b/test/unit-test/FreeRTOS_ARP/FreeRTOS_ARP_utest.c index bdf85aba6..c460c0c77 100644 --- a/test/unit-test/FreeRTOS_ARP/FreeRTOS_ARP_utest.c +++ b/test/unit-test/FreeRTOS_ARP/FreeRTOS_ARP_utest.c @@ -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 ); @@ -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. */ @@ -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 ); diff --git a/test/unit-test/FreeRTOS_ARP/ut.cmake b/test/unit-test/FreeRTOS_ARP/ut.cmake index 5df8b8a09..a8757579e 100644 --- a/test/unit-test/FreeRTOS_ARP/ut.cmake +++ b/test/unit-test/FreeRTOS_ARP/ut.cmake @@ -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 diff --git a/test/unit-test/FreeRTOS_ARP_DataLenLessThanMinPacket/ut.cmake b/test/unit-test/FreeRTOS_ARP_DataLenLessThanMinPacket/ut.cmake index 949036f43..2c25b6b68 100644 --- a/test/unit-test/FreeRTOS_ARP_DataLenLessThanMinPacket/ut.cmake +++ b/test/unit-test/FreeRTOS_ARP_DataLenLessThanMinPacket/ut.cmake @@ -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 "") diff --git a/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c b/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c index 8c7cd2b94..34c8839ac 100644 --- a/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c +++ b/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c @@ -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 diff --git a/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOSIPConfig.h b/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOSIPConfig.h index a95e7d992..9454ff172 100644 --- a/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOSIPConfig.h +++ b/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOSIPConfig.h @@ -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 @@ -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 */ diff --git a/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOS_IP_DiffConfig3_utest.c b/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOS_IP_DiffConfig3_utest.c index 8fac597cf..492831170 100644 --- a/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOS_IP_DiffConfig3_utest.c +++ b/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOS_IP_DiffConfig3_utest.c @@ -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" @@ -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 ============================== */ /** @@ -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 ); +} diff --git a/test/unit-test/FreeRTOS_IP_Timers/FreeRTOS_IP_Timers_utest.c b/test/unit-test/FreeRTOS_IP_Timers/FreeRTOS_IP_Timers_utest.c index 789cf3fc3..367fbee9b 100644 --- a/test/unit-test/FreeRTOS_IP_Timers/FreeRTOS_IP_Timers_utest.c +++ b/test/unit-test/FreeRTOS_IP_Timers/FreeRTOS_IP_Timers_utest.c @@ -65,6 +65,7 @@ void prvIPTimerStart( IPTimer_t * pxTimer, BaseType_t prvIPTimerCheck( IPTimer_t * pxTimer ); extern IPTimer_t xARPTimer; +extern IPTimer_t xNDTimer; #if ( ipconfigUSE_TCP != 0 ) /** @brief TCP timer, to check for timeouts, resends. */ extern IPTimer_t xTCPTimer; @@ -75,6 +76,7 @@ extern IPTimer_t xARPTimer; #endif extern IPTimer_t xARPResolutionTimer; +extern IPTimer_t xNDResolutionTimer; extern BaseType_t xAllNetworksUp; extern IPTimer_t xNetworkTimer; @@ -96,9 +98,11 @@ void setUp( void ) /* Reset all timers. */ memset( &xARPTimer, 0, sizeof( IPTimer_t ) ); + memset( &xNDTimer, 0, sizeof( IPTimer_t ) ); memset( &xDNSTimer, 0, sizeof( IPTimer_t ) ); memset( &xTCPTimer, 0, sizeof( IPTimer_t ) ); memset( &xARPResolutionTimer, 0, sizeof( IPTimer_t ) ); + memset( &xNDResolutionTimer, 0, sizeof( IPTimer_t ) ); memset( &xNetworkTimer, 0, sizeof( IPTimer_t ) ); } @@ -119,6 +123,7 @@ void test_xCalculateSleepTime_AllTimersInactive( void ) TickType_t uxTicks; xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; xDNSTimer.bActive = pdFALSE; xTCPTimer.bActive = pdFALSE; @@ -138,14 +143,16 @@ void test_xCalculateSleepTime_AllTimersActive_AllTimesGreater( void ) TickType_t uxTicks; xARPTimer.bActive = pdTRUE; + xNDTimer.bActive = pdTRUE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdTRUE; xDNSTimer.bActive = pdTRUE; xTCPTimer.bActive = pdTRUE; xARPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; - pxNetworkEndPoints->xDHCP_RATimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME + 1; - xTCPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME + 2; - xDNSTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME + 3; + xNDTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME + 1; + pxNetworkEndPoints->xDHCP_RATimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME + 2; + xTCPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME + 3; + xDNSTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME + 4; uxTicks = xCalculateSleepTime(); @@ -162,11 +169,39 @@ void test_xCalculateSleepTime_AllTimersActive_AllTimesGreaterExceptARP( void ) TickType_t uxTicks; xARPTimer.bActive = pdTRUE; + xNDTimer.bActive = pdTRUE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdTRUE; xDNSTimer.bActive = pdTRUE; xTCPTimer.bActive = pdTRUE; xARPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME - 10; + xNDTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; + pxNetworkEndPoints->xDHCP_RATimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; + xTCPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; + xDNSTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; + + uxTicks = xCalculateSleepTime(); + + TEST_ASSERT_EQUAL( ipconfigMAX_IP_TASK_SLEEP_TIME - 10, uxTicks ); +} + +/** + * @brief test_xCalculateSleepTime_AllTimersActive_AllTimesGreaterExceptND + * To validate if xCalculateSleepTime() returns the shortest remaining time + * of all active timers. In this case, ND timer has the shortest remaining time. + */ +void test_xCalculateSleepTime_AllTimersActive_AllTimesGreaterExceptND( void ) +{ + TickType_t uxTicks; + + xARPTimer.bActive = pdTRUE; + xNDTimer.bActive = pdTRUE; + pxNetworkEndPoints->xDHCP_RATimer.bActive = pdTRUE; + xDNSTimer.bActive = pdTRUE; + xTCPTimer.bActive = pdTRUE; + + xARPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; + xNDTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME - 10; pxNetworkEndPoints->xDHCP_RATimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; xTCPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; xDNSTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; @@ -191,11 +226,13 @@ void test_xCalculateSleepTime_AllTimersActive_AllTimesGreaterExceptDHCP( void ) pxNetworkEndPoints = pxEndpoint; xARPTimer.bActive = pdTRUE; + xNDTimer.bActive = pdTRUE; pxEndpoint->xDHCP_RATimer.bActive = pdTRUE; xDNSTimer.bActive = pdTRUE; xTCPTimer.bActive = pdTRUE; xARPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; + xNDTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; pxEndpoint->xDHCP_RATimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME - 10; xTCPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; xDNSTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; @@ -215,11 +252,13 @@ void test_xCalculateSleepTime_AllTimersActive_AllTimesGreaterExceptTCP( void ) TickType_t uxTicks; xARPTimer.bActive = pdTRUE; + xNDTimer.bActive = pdTRUE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdTRUE; xDNSTimer.bActive = pdTRUE; xTCPTimer.bActive = pdTRUE; xARPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; + xNDTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; pxNetworkEndPoints->xDHCP_RATimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; xTCPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME - 10; xDNSTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; @@ -239,11 +278,13 @@ void test_xCalculateSleepTime_AllTimersActive_AllTimesGreaterExceptDNS( void ) TickType_t uxTicks; xARPTimer.bActive = pdTRUE; + xNDTimer.bActive = pdTRUE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdTRUE; xDNSTimer.bActive = pdTRUE; xTCPTimer.bActive = pdTRUE; xARPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; + xNDTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; pxNetworkEndPoints->xDHCP_RATimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; xTCPTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME; xDNSTimer.ulRemainingTime = ipconfigMAX_IP_TASK_SLEEP_TIME - 10; @@ -295,6 +336,7 @@ void test_xCalculateSleepTime_MultipleDHCPTimers( void ) void test_vCheckNetworkTimers_AllTimersDisabled( void ) { xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; xDNSTimer.bActive = pdFALSE; xTCPTimer.bActive = pdFALSE; @@ -315,6 +357,7 @@ void test_vCheckNetworkTimers_AllTimersDisabled( void ) void test_vCheckNetworkTimers_ARPTimerActiveAndExpired( void ) { xARPTimer.bActive = pdTRUE; + xNDTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; xDNSTimer.bActive = pdFALSE; xTCPTimer.bActive = pdFALSE; @@ -342,10 +385,12 @@ void test_vCheckNetworkTimers_ARPTimerActiveAndExpired( void ) void test_vCheckNetworkTimers_ARPResolutionTimerActiveAndExpiredNullBuffer( void ) { xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; xDNSTimer.bActive = pdFALSE; xTCPTimer.bActive = pdFALSE; xARPResolutionTimer.bActive = pdTRUE; + xNDResolutionTimer.bActive = pdFALSE; xARPResolutionTimer.bExpired = pdTRUE; @@ -372,10 +417,12 @@ void test_vCheckNetworkTimers_ARPResolutionTimerActiveAndExpiredNullBuffer( void void test_vCheckNetworkTimers_ARPResolutionTimerActiveAndExpired( void ) { xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; xDNSTimer.bActive = pdFALSE; xTCPTimer.bActive = pdFALSE; xARPResolutionTimer.bActive = pdTRUE; + xNDResolutionTimer.bActive = pdFALSE; xARPResolutionTimer.bExpired = pdTRUE; @@ -397,6 +444,100 @@ void test_vCheckNetworkTimers_ARPResolutionTimerActiveAndExpired( void ) TEST_ASSERT_EQUAL_PTR( NULL, pxARPWaitingNetworkBuffer ); } +/** + * @brief test_vCheckNetworkTimers_NDTimerActiveAndExpired + * To validate if vCheckNetworkTimers() handles ND timer expired event as expected. + */ +void test_vCheckNetworkTimers_NDTimerActiveAndExpired( void ) +{ + xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdTRUE; + pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; + xDNSTimer.bActive = pdFALSE; + xTCPTimer.bActive = pdFALSE; + + xNDTimer.bExpired = pdTRUE; + + vTaskSetTimeOutState_Expect( &( xNDTimer.xTimeOut ) ); + + xSendEventToIPTask_ExpectAndReturn( eNDTimerEvent, pdTRUE ); + + uxQueueMessagesWaiting_ExpectAnyArgsAndReturn( pdTRUE ); + + vSocketCloseNextTime_Expect( NULL ); + + vSocketListenNextTime_Expect( NULL ); + + vCheckNetworkTimers(); +} + +/** + * @brief test_vCheckNetworkTimers_NDesolutionTimerActiveAndExpiredNullBuffer + * To validate if vCheckNetworkTimers() handles ND resolution timer expired event as expected. + * And there is no buffer waiting for ND reply. + */ +void test_vCheckNetworkTimers_NDesolutionTimerActiveAndExpiredNullBuffer( void ) +{ + xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; + pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; + xDNSTimer.bActive = pdFALSE; + xTCPTimer.bActive = pdFALSE; + xARPResolutionTimer.bActive = pdFALSE; + xNDResolutionTimer.bActive = pdTRUE; + + xNDResolutionTimer.bExpired = pdTRUE; + + pxNDWaitingNetworkBuffer = NULL; + + vTaskSetTimeOutState_Expect( &( xNDResolutionTimer.xTimeOut ) ); + + uxQueueMessagesWaiting_ExpectAnyArgsAndReturn( pdTRUE ); + + vSocketCloseNextTime_Expect( NULL ); + + vSocketListenNextTime_Expect( NULL ); + + vCheckNetworkTimers(); + + TEST_ASSERT_EQUAL( pdTRUE, xNDResolutionTimer.bExpired ); +} + +/** + * @brief test_vCheckNetworkTimers_NDResolutionTimerActiveAndExpired + * To validate if vCheckNetworkTimers() handles ND resolution timer expired event as expected. + * And there is a buffer waiting for ND reply. + */ +void test_vCheckNetworkTimers_NDResolutionTimerActiveAndExpired( void ) +{ + xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; + pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; + xDNSTimer.bActive = pdFALSE; + xTCPTimer.bActive = pdFALSE; + xARPResolutionTimer.bActive = pdFALSE; + xNDResolutionTimer.bActive = pdTRUE; + + xNDResolutionTimer.bExpired = pdTRUE; + + pxNDWaitingNetworkBuffer = ( NetworkBufferDescriptor_t * ) 0x1234ABCD; + + vTaskSetTimeOutState_Expect( &( xNDResolutionTimer.xTimeOut ) ); + + vReleaseNetworkBufferAndDescriptor_Expect( pxNDWaitingNetworkBuffer ); + + uxQueueMessagesWaiting_ExpectAnyArgsAndReturn( pdTRUE ); + + vSocketCloseNextTime_Expect( NULL ); + + vSocketListenNextTime_Expect( NULL ); + + vCheckNetworkTimers(); + + TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xNDResolutionTimer.bActive ); + TEST_ASSERT_EQUAL_PTR( NULL, pxNDWaitingNetworkBuffer ); +} + /** * @brief test_vCheckNetworkTimers_DHCPTimerActiveAndExpired * To validate if vCheckNetworkTimers() handles DHCP timer expired event as expected. @@ -404,10 +545,12 @@ void test_vCheckNetworkTimers_ARPResolutionTimerActiveAndExpired( void ) void test_vCheckNetworkTimers_DHCPTimerActiveAndExpired( void ) { xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdTRUE; xDNSTimer.bActive = pdFALSE; xTCPTimer.bActive = pdFALSE; xARPResolutionTimer.bActive = pdFALSE; + xNDResolutionTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bExpired = pdTRUE; @@ -454,10 +597,12 @@ void test_vCheckNetworkTimers_DHCPv6TimerActiveAndExpired( void ) void test_vCheckNetworkTimers_RATimerActiveAndExpired( void ) { xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdTRUE; xDNSTimer.bActive = pdFALSE; xTCPTimer.bActive = pdFALSE; xARPResolutionTimer.bActive = pdFALSE; + xNDResolutionTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bExpired = pdTRUE; pxNetworkEndPoints->bits.bIPv6 = pdTRUE; @@ -486,10 +631,12 @@ void test_vCheckNetworkTimers_DNSTimerActiveAndExpired( void ) NetworkEndPoint_t xEndPoint = { 0 }; xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; xDNSTimer.bActive = pdTRUE; xTCPTimer.bActive = pdFALSE; xARPResolutionTimer.bActive = pdFALSE; + xNDResolutionTimer.bActive = pdFALSE; xDNSTimer.bExpired = pdTRUE; @@ -624,10 +771,12 @@ void test_vCheckNetworkTimers_NetworkInterfacesAlreadyUp( void ) void test_vCheckNetworkTimers_AllTimersInactivePendingMessages( void ) { xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; xDNSTimer.bActive = pdFALSE; xTCPTimer.bActive = pdFALSE; xARPResolutionTimer.bActive = pdFALSE; + xNDResolutionTimer.bActive = pdFALSE; uxQueueMessagesWaiting_ExpectAnyArgsAndReturn( pdTRUE ); @@ -646,10 +795,12 @@ void test_vCheckNetworkTimers_AllTimersInactivePendingMessages( void ) void test_vCheckNetworkTimers_AllTimersInactive_2( void ) { xARPTimer.bActive = pdFALSE; + xNDTimer.bActive = pdFALSE; pxNetworkEndPoints->xDHCP_RATimer.bActive = pdFALSE; xDNSTimer.bActive = pdFALSE; xTCPTimer.bActive = pdFALSE; xARPResolutionTimer.bActive = pdFALSE; + xNDResolutionTimer.bActive = pdFALSE; uxQueueMessagesWaiting_ExpectAnyArgsAndReturn( pdFALSE ); @@ -724,7 +875,25 @@ void test_vIPTimerStartARPResolution( void ) } /** - * @brief test_vIPTimerStartARPResolution + * @brief test_vIPTimerStartNDResolution + * To validate if vIPTimerStartNDResolution() activate the ND resolution timer + * with zero time. Timer must be expired after calling. + */ +void test_vIPTimerStartNDResolution( void ) +{ + TickType_t xTime = 0x00; + + vTaskSetTimeOutState_Expect( &xNDResolutionTimer.xTimeOut ); + + vIPTimerStartNDResolution( xTime ); + + TEST_ASSERT_EQUAL( xTime, xNDResolutionTimer.ulRemainingTime ); + TEST_ASSERT_EQUAL( pdTRUE, xNDResolutionTimer.bActive ); + TEST_ASSERT_EQUAL( pdTRUE, xNDResolutionTimer.bExpired ); +} + +/** + * @brief test_vTCPTimerReload * To validate if vTCPTimerReload() activate the TCP timer with non-zero time. */ void test_vTCPTimerReload( void ) @@ -759,6 +928,24 @@ void test_vARPTimerReload( void ) TEST_ASSERT_EQUAL( pdFALSE, xARPTimer.bExpired ); } +/** + * @brief test_vNDTimerReload + * To validate if vNDTimerReload() activate the ND timer with non-zero time. + */ +void test_vNDTimerReload( void ) +{ + TickType_t xTime = 0x12A; + + vTaskSetTimeOutState_Expect( &xNDTimer.xTimeOut ); + + vNDTimerReload( xTime ); + + TEST_ASSERT_EQUAL( 0x12A, xNDTimer.ulReloadTime ); + TEST_ASSERT_EQUAL( xTime, xNDTimer.ulRemainingTime ); + TEST_ASSERT_EQUAL( pdTRUE, xNDTimer.bActive ); + TEST_ASSERT_EQUAL( pdFALSE, xNDTimer.bExpired ); +} + /** * @brief test_vDHCP_RATimerReload * To validate if vTCPTimerReload() activate the DHCP timer with non-zero time. @@ -986,6 +1173,60 @@ void test_vIPSetARPResolutionTimerEnableState_True( void ) TEST_ASSERT_EQUAL( xEnableState, xARPResolutionTimer.bActive ); } +/** + * @brief test_vIPSetNDTimerEnableState_False + * To validate if vIPSetNDTimerEnableState() sets ND timer to non expired state. + */ +void test_vIPSetNDTimerEnableState_False( void ) +{ + BaseType_t xEnableState = pdFALSE; + + vIPSetNDTimerEnableState( xEnableState ); + + TEST_ASSERT_EQUAL( xEnableState, xNDTimer.bActive ); +} + +/** + * @brief test_vIPSetNDTimerEnableState_True + * To validate if vIPSetNDTimerEnableState() sets ND timer to expired state. + */ +void test_vIPSetNDTimerEnableState_True( void ) +{ + BaseType_t xEnableState = pdTRUE; + + vIPSetNDTimerEnableState( xEnableState ); + + TEST_ASSERT_EQUAL( xEnableState, xNDTimer.bActive ); +} + +/** + * @brief test_vIPSetNDResolutionTimerEnableState_False + * To validate if vIPSetNDResolutionTimerEnableState() sets ND resolution timer + * to non expired state. + */ +void test_vIPSetNDResolutionTimerEnableState_False( void ) +{ + BaseType_t xEnableState = pdFALSE; + + vIPSetNDResolutionTimerEnableState( xEnableState ); + + TEST_ASSERT_EQUAL( xEnableState, xNDResolutionTimer.bActive ); +} + +/** + * @brief test_vIPSetNDResolutionTimerEnableState_True + * To validate if vIPSetNDResolutionTimerEnableState() sets ND resolution timer + * to expired state. + */ +void test_vIPSetNDResolutionTimerEnableState_True( void ) +{ + BaseType_t xEnableState = pdTRUE; + + vIPSetNDResolutionTimerEnableState( xEnableState ); + + TEST_ASSERT_EQUAL( xEnableState, xNDResolutionTimer.bActive ); +} + /** * @brief test_vIPSetDHCP_RATimerEnableState_False * To validate if vIPSetDHCP_RATimerEnableState() sets DHCP timer diff --git a/test/unit-test/FreeRTOS_ND/FreeRTOS_ND_utest.c b/test/unit-test/FreeRTOS_ND/FreeRTOS_ND_utest.c index 672bfac25..afb69714e 100644 --- a/test/unit-test/FreeRTOS_ND/FreeRTOS_ND_utest.c +++ b/test/unit-test/FreeRTOS_ND/FreeRTOS_ND_utest.c @@ -789,6 +789,40 @@ void test_FreeRTOS_ClearND( void ) TEST_ASSERT_EQUAL_MEMORY( xNDCache, xTempNDCache, sizeof( xNDCache ) ); } +/** + * @brief Clear the Neighbour Discovery cache with specific endpoint. + */ +void test_FreeRTOS_ClearND_WithEndPoint( void ) +{ + NDCacheRow_t xTempNDCache[ ipconfigND_CACHE_ENTRIES ]; + struct xNetworkEndPoint xEndPoint; + + /* Set xNDCache to non zero entries*/ + ( void ) memset( xNDCache, 1, sizeof( xNDCache ) ); + ( void ) memset( xTempNDCache, 0, sizeof( xTempNDCache ) ); + xNDCache[ 1 ].pxEndPoint = &xEndPoint; + FreeRTOS_ClearND( &xEndPoint ); + + TEST_ASSERT_EQUAL_MEMORY( &xNDCache[ 1 ], xTempNDCache, sizeof( NDCacheRow_t ) ); +} + +/** + * @brief Clear the Neighbour Discovery cache with endpoint. + * But the endpoint doesn't match any in cache. + */ +void test_FreeRTOS_ClearND_EndPointNotFound( void ) +{ + NDCacheRow_t xTempNDCache[ ipconfigND_CACHE_ENTRIES ]; + struct xNetworkEndPoint xEndPoint; + + /* Set xNDCache to non zero entries*/ + ( void ) memset( xNDCache, 1, sizeof( xNDCache ) ); + ( void ) memset( xTempNDCache, 1, sizeof( xTempNDCache ) ); + FreeRTOS_ClearND( &xEndPoint ); + + TEST_ASSERT_EQUAL_MEMORY( xTempNDCache, xNDCache, sizeof( xNDCache ) ); +} + /** * @brief Toggle happy path. */ @@ -1886,22 +1920,6 @@ void test_FreeRTOS_CreateIPv6Address_Pass2( void ) TEST_ASSERT_EQUAL( xReturn, pdPASS ); } -void test_FreeRTOS_CreateIPv6Address_Pass3( void ) /*CHECK if needed */ -{ - IPv6_Address_t xIPAddress, xPrefix; - size_t uxPrefixLength = 127; - BaseType_t xDoRandom = pdTRUE, xReturn, xIndex; - - for( xIndex = 0; xIndex < 4; xIndex++ ) - { - xApplicationGetRandomNumber_ExpectAnyArgsAndReturn( pdTRUE ); - } - - xReturn = FreeRTOS_CreateIPv6Address( &xIPAddress, &xPrefix, uxPrefixLength, xDoRandom ); - - TEST_ASSERT_EQUAL( xReturn, pdPASS ); -} - /** * @brief Cover all the pcMessageType print * scenario. @@ -1944,6 +1962,9 @@ void test_pcMessageType_All( void ) ( void ) pcMessageType( xType ); } +/** + * @brief heck if the network buffer requires resolution for different protocols. + */ void test_xCheckIPv6RequiresResolution_Protocols( void ) { struct xNetworkEndPoint xEndPoint = { 0 }; @@ -1978,6 +1999,10 @@ void test_xCheckIPv6RequiresResolution_Protocols( void ) TEST_ASSERT_EQUAL( pdFALSE, xResult ); } +/** + * @brief Check if the network buffer requires resolution for addresses + * not on the local network. + */ void test_xCheckRequiresNDResolution_TCPNotOnLocalNetwork( void ) { struct xNetworkEndPoint xEndPoint = { 0 }; @@ -2000,6 +2025,9 @@ void test_xCheckRequiresNDResolution_TCPNotOnLocalNetwork( void ) TEST_ASSERT_EQUAL( pdFALSE, xResult ); } +/** + * @brief Cache hit occurs with an IP address in the multicast case. + */ void test_xCheckRequiresNDResolution_Hit( void ) { struct xNetworkEndPoint xEndPoint = { 0 }; @@ -2028,6 +2056,9 @@ void test_xCheckRequiresNDResolution_Hit( void ) TEST_ASSERT_EQUAL( pdFALSE, xResult ); } +/** + * @brief ND cache miss scenarios. + */ void test_xCheckRequiresNDResolution_Miss( void ) { struct xNetworkEndPoint xEndPoint, * pxEndPoint = &xEndPoint; @@ -2067,3 +2098,13 @@ void test_xCheckRequiresNDResolution_Miss( void ) TEST_ASSERT_EQUAL( pdTRUE, xResult ); } + +/** + * @brief Toggle vNDSendUnsolicited. + */ +void test_vNDSendUnsolicited( void ) +{ + xSendEventToIPTask_ExpectAndReturn( eNDTimerEvent, 0 ); + + vNDSendUnsolicited(); +}