Skip to content

Commit

Permalink
Fix Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Sep 19, 2024
1 parent d5474ee commit dd1f80b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
5 changes: 2 additions & 3 deletions source/FreeRTOS_ARP.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@
const IPHeader_t * pxIPHeader = &( pxIPPacket->xIPHeader );
const IPV4Parameters_t * pxIPv4Settings = &( pxNetworkBuffer->pxEndPoint->ipv4_settings );

/* configASSERT( ( pxIPPacket->xEthernetHeader.usFrameType == ipIPv4_FRAME_TYPE ) || ( pxIPPacket->xEthernetHeader.usFrameType == ipARP_FRAME_TYPE ) ); */

if( ( pxIPHeader->ulSourceIPAddress & pxIPv4Settings->ulNetMask ) == ( pxIPv4Settings->ulIPAddress & pxIPv4Settings->ulNetMask ) )
{
/* If the IP is on the same subnet and we do not have an ARP entry already,
Expand Down Expand Up @@ -908,7 +910,6 @@
/* Get the lowest 23 bits of the IP-address. */
vSetMultiCastIPv4MacAddress( ulAddressToLookup, pxMACAddress );

eReturn = eResolutionFailed;
pxEndPoint = FreeRTOS_FirstEndPoint( NULL );

for( ;
Expand Down Expand Up @@ -1329,8 +1330,6 @@
}
/*-----------------------------------------------------------*/



/**
* @brief Generate an ARP request packet by copying various constant details to
* the buffer.
Expand Down
2 changes: 1 addition & 1 deletion source/FreeRTOS_IP.c
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetwor
case eWaitingResolution:

#if ipconfigIS_ENABLED( ipconfigUSE_IPv4 )
if( pxEthernetHeader->usFrameType == ipIPv4_FRAME_TYPE )
if( ( pxEthernetHeader->usFrameType == ipIPv4_FRAME_TYPE ) || ( pxEthernetHeader->usFrameType == ipARP_FRAME_TYPE ) )
{
if( pxARPWaitingNetworkBuffer == NULL )
{
Expand Down
2 changes: 2 additions & 0 deletions source/FreeRTOS_ND.c
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,8 @@
IPv6_Address_t * pxIPAddress = &( pxIPHeader->xSourceAddress );
uint8_t ucNextHeader = pxIPHeader->ucNextHeader;

configASSERT( pxIPPacket->xEthernetHeader.usFrameType == ipIPv6_FRAME_TYPE );

if( ( ucNextHeader == ipPROTOCOL_TCP ) ||
( ucNextHeader == ipPROTOCOL_UDP ) )
{
Expand Down
28 changes: 28 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
# This script should be run from the root directory of the FreeRTOS+TCP repo.

if [[ ! -d source ]]; then
echo "Please run this script from the root directory of the FreeRTOS+TCP repo."
exit 1
fi

clear

UNIT_TEST_DIR="test/unit-test"
BUILD_DIR="${UNIT_TEST_DIR}/build/"

# Create the build directory using CMake:
rm -rf ${BUILD_DIR}
cmake -S ${UNIT_TEST_DIR} -B ${BUILD_DIR} -G Ninja

# Create the executables:
ninja -C ${BUILD_DIR} all

pushd ${BUILD_DIR}
# Run the tests for all units
ctest -E system --output-on-failure
popd

# Calculate the coverage
# ninja -C ${BUILD_DIR} coverage
# lcov --list --rc lcov_branch_coverage=1 ${BUILD_DIR}coverage.info
33 changes: 31 additions & 2 deletions test/unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ void test_prvIPTask( void )
/* In prvIPTask_Initialise. */
vTCPTimerReload_ExpectAnyArgs();
vIPSetARPResolutionTimerEnableState_Expect( pdFALSE );
vIPSetNDResolutionTimerEnableState_Expect( pdFALSE );
vDNSInitialise_Ignore();
FreeRTOS_dnsclear_Ignore();

Expand Down Expand Up @@ -533,6 +534,7 @@ void test_prvIPTask_NetworkDown( void )
/* In prvIPTask_Initialise. */
vTCPTimerReload_ExpectAnyArgs();
vIPSetARPResolutionTimerEnableState_Expect( pdFALSE );
vIPSetNDResolutionTimerEnableState_Expect( pdFALSE );
vDNSInitialise_Ignore();
FreeRTOS_dnsclear_Ignore();

Expand Down Expand Up @@ -731,7 +733,7 @@ void test_prvProcessIPEventsAndTimers_eNetworkRxEvent_NullEndPoint( void )

/**
* @brief test_prvProcessIPEventsAndTimers_eARPTimerEvent
* Check if prvProcessIPEventsAndTimers() updates the cache for ARP/ND when timeout event triggered.
* Check if prvProcessIPEventsAndTimers() updates the cache for ARP when timeout event triggered.
*/
void test_prvProcessIPEventsAndTimers_eARPTimerEvent( void )
{
Expand All @@ -746,6 +748,26 @@ void test_prvProcessIPEventsAndTimers_eARPTimerEvent( void )
xQueueReceive_ExpectAnyArgsAndReturn( pdTRUE );
xQueueReceive_ReturnMemThruPtr_pvBuffer( &xReceivedEvent, sizeof( xReceivedEvent ) );
vARPAgeCache_Expect();

prvProcessIPEventsAndTimers();
}

/**
* @brief test_prvProcessIPEventsAndTimers_eNDTimerEvent
* Check if prvProcessIPEventsAndTimers() updates the cache for ND when timeout event triggered.
*/
void test_prvProcessIPEventsAndTimers_eNDTimerEvent( void )
{
IPStackEvent_t xReceivedEvent;

xReceivedEvent.eEventType = eNDTimerEvent;
xReceivedEvent.pvData = NULL;

/* prvProcessIPEventsAndTimers */
vCheckNetworkTimers_Expect();
xCalculateSleepTime_ExpectAndReturn( 0 );
xQueueReceive_ExpectAnyArgsAndReturn( pdTRUE );
xQueueReceive_ReturnMemThruPtr_pvBuffer( &xReceivedEvent, sizeof( xReceivedEvent ) );
vNDAgeCache_Expect();

prvProcessIPEventsAndTimers();
Expand Down Expand Up @@ -4232,7 +4254,14 @@ static void prvIPNetworkUpCalls_Generic( const uint8_t * pucAddress,

vApplicationIPNetworkEventHook_Multi_Expect( eNetworkUp, &xEndPoint );
vDNSInitialise_Expect();
vARPTimerReload_Expect( pdMS_TO_TICKS( 10000 ) );
if( xEndPoint.bits.bIPv6 == pdTRUE_UNSIGNED )
{
vNDTimerReload_Expect( pdMS_TO_TICKS( 10000 ) );
}
else
{
vARPTimerReload_Expect( pdMS_TO_TICKS( 10000 ) );
}

vIPNetworkUpCalls( &xEndPoint );

Expand Down
17 changes: 17 additions & 0 deletions test/unit-test/FreeRTOS_IP_Utils/FreeRTOS_IP_Utils_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "mock_FreeRTOS_IP_Private.h"
#include "mock_FreeRTOS_IP_Timers.h"
#include "mock_FreeRTOS_ARP.h"
#include "mock_FreeRTOS_ND.h"
#include "mock_FreeRTOS_DHCP.h"
#include "mock_FreeRTOS_DHCPv6.h"
#include "mock_FreeRTOS_Routing.h"
Expand Down Expand Up @@ -489,11 +490,13 @@ void test_prvProcessNetworkDownEvent_Pass( void )
xEndPoint.bits.bCallDownHook = pdFALSE_UNSIGNED;

vIPSetARPTimerEnableState_Expect( pdFALSE );
vIPSetNDTimerEnableState_Expect( pdFALSE );

FreeRTOS_FirstEndPoint_IgnoreAndReturn( &xEndPoint );
FreeRTOS_NextEndPoint_IgnoreAndReturn( NULL );

FreeRTOS_ClearARP_ExpectAnyArgs();
FreeRTOS_ClearND_ExpectAnyArgs();

vDHCPStop_Expect( &xEndPoint );

Expand All @@ -504,13 +507,15 @@ void test_prvProcessNetworkDownEvent_Pass( void )
/* Run again to trigger a different path in the code. */

vIPSetARPTimerEnableState_Expect( pdFALSE );
vIPSetNDTimerEnableState_Expect( pdFALSE );

FreeRTOS_FirstEndPoint_IgnoreAndReturn( &xEndPoint );
FreeRTOS_NextEndPoint_IgnoreAndReturn( NULL );

vApplicationIPNetworkEventHook_Multi_Expect( eNetworkDown, &xEndPoint );

FreeRTOS_ClearARP_Expect( &xEndPoint );
FreeRTOS_ClearND_Expect( &xEndPoint );

vDHCPStop_Expect( &xEndPoint );

Expand All @@ -535,11 +540,13 @@ void test_prvProcessNetworkDownEvent_Fail( void )
xEndPoint.bits.bWantDHCP = pdFALSE_UNSIGNED;

vIPSetARPTimerEnableState_Expect( pdFALSE );
vIPSetNDTimerEnableState_Expect( pdFALSE );

FreeRTOS_FirstEndPoint_IgnoreAndReturn( &xEndPoint );
FreeRTOS_NextEndPoint_IgnoreAndReturn( NULL );

FreeRTOS_ClearARP_Expect( &xEndPoint );
FreeRTOS_ClearND_Expect( &xEndPoint );

vIPNetworkUpCalls_Expect( &xEndPoint );

Expand Down Expand Up @@ -586,11 +593,13 @@ void test_prvProcessNetworkDownEvent_InterfaceInitFail( void )
xEndPoint.bits.bCallDownHook = pdFALSE_UNSIGNED;

vIPSetARPTimerEnableState_Expect( pdFALSE );
vIPSetNDTimerEnableState_Expect( pdFALSE );

FreeRTOS_FirstEndPoint_IgnoreAndReturn( &xEndPoint );
FreeRTOS_NextEndPoint_IgnoreAndReturn( NULL );

FreeRTOS_ClearARP_ExpectAnyArgs();
FreeRTOS_ClearND_ExpectAnyArgs();

vDHCPStop_Expect( &xEndPoint );

Expand All @@ -616,6 +625,7 @@ void test_prvProcessNetworkDownEvent_PassDHCPv6( void )
xEndPoint.bits.bCallDownHook = pdFALSE_UNSIGNED;

vIPSetARPTimerEnableState_Expect( pdFALSE );
vIPSetNDTimerEnableState_Expect( pdFALSE );

FreeRTOS_FirstEndPoint_IgnoreAndReturn( &xEndPoint );

Expand All @@ -628,6 +638,7 @@ void test_prvProcessNetworkDownEvent_PassDHCPv6( void )
FreeRTOS_NextEndPoint_IgnoreAndReturn( NULL );

FreeRTOS_ClearARP_ExpectAnyArgs();
FreeRTOS_ClearND_ExpectAnyArgs();

vDHCPv6Stop_Expect( &xEndPoint );

Expand All @@ -653,6 +664,7 @@ void test_prvProcessNetworkDownEvent_PassRA( void )
xEndPoint.bits.bCallDownHook = pdFALSE_UNSIGNED;

vIPSetARPTimerEnableState_Expect( pdFALSE );
vIPSetNDTimerEnableState_Expect( pdFALSE );

FreeRTOS_FirstEndPoint_IgnoreAndReturn( &xEndPoint );

Expand All @@ -665,6 +677,7 @@ void test_prvProcessNetworkDownEvent_PassRA( void )
FreeRTOS_NextEndPoint_IgnoreAndReturn( NULL );

FreeRTOS_ClearARP_ExpectAnyArgs();
FreeRTOS_ClearND_ExpectAnyArgs();

vIPSetDHCP_RATimerEnableState_Expect( &xEndPoint, pdFALSE );

Expand Down Expand Up @@ -692,6 +705,7 @@ void test_prvProcessNetworkDownEvent_PassStaticIP( void )
memcpy( xEndPoint.ipv6_defaults.xIPAddress.ucBytes, xIPv6Address.ucBytes, ipSIZE_OF_IPv6_ADDRESS );

vIPSetARPTimerEnableState_Expect( pdFALSE );
vIPSetNDTimerEnableState_Expect( pdFALSE );

FreeRTOS_FirstEndPoint_IgnoreAndReturn( &xEndPoint );

Expand All @@ -704,6 +718,7 @@ void test_prvProcessNetworkDownEvent_PassStaticIP( void )
FreeRTOS_NextEndPoint_IgnoreAndReturn( NULL );

FreeRTOS_ClearARP_ExpectAnyArgs();
FreeRTOS_ClearND_ExpectAnyArgs();

vIPNetworkUpCalls_Expect( &xEndPoint );

Expand Down Expand Up @@ -3128,6 +3143,7 @@ static void prvProcessNetworkDownEvent_Generic( const uint8_t * pucAddress,
}

vIPSetARPTimerEnableState_Expect( pdFALSE );
vIPSetNDTimerEnableState_Expect( pdFALSE );

FreeRTOS_FirstEndPoint_IgnoreAndReturn( &xEndPoint );

Expand All @@ -3141,6 +3157,7 @@ static void prvProcessNetworkDownEvent_Generic( const uint8_t * pucAddress,


FreeRTOS_ClearARP_Expect( &xEndPoint );
FreeRTOS_ClearND_Expect( &xEndPoint );

vIPNetworkUpCalls_Expect( &xEndPoint );

Expand Down

0 comments on commit dd1f80b

Please sign in to comment.