diff --git a/test/unit-test/FreeRTOS_DHCP/FreeRTOS_DHCP_utest.c b/test/unit-test/FreeRTOS_DHCP/FreeRTOS_DHCP_utest.c index 0a6e24b4d..9451ce66a 100644 --- a/test/unit-test/FreeRTOS_DHCP/FreeRTOS_DHCP_utest.c +++ b/test/unit-test/FreeRTOS_DHCP/FreeRTOS_DHCP_utest.c @@ -53,6 +53,38 @@ extern uint8_t pucUDPBuffer[]; extern uint8_t DHCP_header[]; +/*-------------------------------------Helpers--------------------------------*/ + +static void prvWriteDHCPOption( uint8_t ** ppucBuf, + uint8_t ucOp, + const void * pvPayload, + uint8_t ucLen ) +{ + uint8_t * pucBuf = *ppucBuf; + + *pucBuf++ = ucOp; + *pucBuf++ = ucLen; + memcpy( pucBuf, pvPayload, ucLen ); + pucBuf += ucLen; + + *ppucBuf = pucBuf; +} + +static void prvWriteDHCPOptionU8( uint8_t ** ppucBuf, + uint8_t ucOp, + uint8_t ucPayload ) +{ + prvWriteDHCPOption( ppucBuf, ucOp, &ucPayload, sizeof( ucPayload ) ); +} + +static void prvWriteDHCPOptionU32( uint8_t ** ppucBuf, + uint8_t ucOp, + uint32_t ulPayload ) +{ + /* TBD: htonl(ulPayload)? */ + prvWriteDHCPOption( ppucBuf, ucOp, &ulPayload, sizeof( ulPayload ) ); +} + /*---------------------------------------Test Cases--------------------------*/ void test_xIsDHCPSocket( void ) { @@ -4538,9 +4570,6 @@ void test_vDHCPProcess_eWaitingAcknowledge_IncorrectDNSServerAddress( void ) NetworkEndPoint_t xEndPoint = { 0 }, * pxEndPoint = &xEndPoint; IPV4Parameters_t * xIPv4Addressing = &( pxEndPoint->ipv4_settings ); - DHCPMsg[ xTotalLength - 1U ] = 0xFF; - - /* Set the header - or at least the start of DHCP message. */ memset( DHCPMsg, 0, sizeof( DHCPMsg ) ); /* Copy the header here. */ @@ -4556,53 +4585,16 @@ void test_vDHCPProcess_eWaitingAcknowledge_IncorrectDNSServerAddress( void ) /* Leave one byte for the padding. */ uint8_t * DHCPOption = &DHCPMsg[ sizeof( struct xDHCPMessage_IPv4 ) + 1 ]; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_MESSAGE_TYPE_OPTION_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 1; - /* Add the offer byte. */ - DHCPOption[ 2 ] = dhcpMESSAGE_TYPE_ACK; - DHCPOption += 4; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_SERVER_IP_ADDRESS_OPTION_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 4; - /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = DHCPServerAddress; - - DHCPOption += 6; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_SUBNET_MASK_OPTION_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 4; - /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = ulSubnetMask; - - DHCPOption += 6; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_GATEWAY_OPTION_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 4; - /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = ulGateway; - - DHCPOption += 6; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_LEASE_TIME_OPTION_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 4; - /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = ulLeaseTime; - - DHCPOption += 6; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_DNS_SERVER_OPTIONS_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 4; - /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = FREERTOS_INADDR_ANY; + prvWriteDHCPOptionU8( &DHCPOption, dhcpIPv4_MESSAGE_TYPE_OPTION_CODE, dhcpMESSAGE_TYPE_ACK ); + prvWriteDHCPOptionU32( &DHCPOption, dhcpIPv4_SERVER_IP_ADDRESS_OPTION_CODE, DHCPServerAddress ); + prvWriteDHCPOptionU32( &DHCPOption, dhcpIPv4_SUBNET_MASK_OPTION_CODE, ulSubnetMask ); + prvWriteDHCPOptionU32( &DHCPOption, dhcpIPv4_GATEWAY_OPTION_CODE, ulGateway ); + prvWriteDHCPOptionU32( &DHCPOption, dhcpIPv4_LEASE_TIME_OPTION_CODE, ulLeaseTime ); + prvWriteDHCPOptionU32( &DHCPOption, dhcpIPv4_DNS_SERVER_OPTIONS_CODE, FREERTOS_INADDR_ANY ); + *DHCPOption++ = 0xFF; + TEST_ASSERT_EQUAL( DHCPOption - DHCPMsg, xTotalLength ); /* Put the information in global variables to be returned by * the FreeRTOS_recvrom. */ @@ -4684,9 +4676,6 @@ void test_vDHCPProcess_eWaitingAcknowledge_IncorrectDNSServerAddress2( void ) NetworkEndPoint_t xEndPoint = { 0 }, * pxEndPoint = &xEndPoint; IPV4Parameters_t * xIPv4Addressing = &( pxEndPoint->ipv4_settings ); - DHCPMsg[ xTotalLength - 1U ] = 0xFF; - - /* Set the header - or at least the start of DHCP message. */ memset( DHCPMsg, 0, sizeof( DHCPMsg ) ); /* Copy the header here. */ @@ -4702,53 +4691,16 @@ void test_vDHCPProcess_eWaitingAcknowledge_IncorrectDNSServerAddress2( void ) /* Leave one byte for the padding. */ uint8_t * DHCPOption = &DHCPMsg[ sizeof( struct xDHCPMessage_IPv4 ) + 1 ]; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_MESSAGE_TYPE_OPTION_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 1; - /* Add the offer byte. */ - DHCPOption[ 2 ] = dhcpMESSAGE_TYPE_ACK; - - DHCPOption += 4; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_SERVER_IP_ADDRESS_OPTION_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 4; - /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = DHCPServerAddress; - - DHCPOption += 6; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_SUBNET_MASK_OPTION_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 4; - /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = ulSubnetMask; - - DHCPOption += 6; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_GATEWAY_OPTION_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 4; - /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = ulGateway; - - DHCPOption += 6; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_LEASE_TIME_OPTION_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 4; - /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = ulLeaseTime; - DHCPOption += 6; - /* Add Message type code. */ - DHCPOption[ 0 ] = dhcpIPv4_DNS_SERVER_OPTIONS_CODE; - /* Add length. */ - DHCPOption[ 1 ] = 4; - /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = ipBROADCAST_IP_ADDRESS; + prvWriteDHCPOptionU8( &DHCPOption, dhcpIPv4_MESSAGE_TYPE_OPTION_CODE, dhcpMESSAGE_TYPE_ACK ); + prvWriteDHCPOptionU32( &DHCPOption, dhcpIPv4_SERVER_IP_ADDRESS_OPTION_CODE, DHCPServerAddress ); + prvWriteDHCPOptionU32( &DHCPOption, dhcpIPv4_SUBNET_MASK_OPTION_CODE, ulSubnetMask ); + prvWriteDHCPOptionU32( &DHCPOption, dhcpIPv4_GATEWAY_OPTION_CODE, ulGateway ); + prvWriteDHCPOptionU32( &DHCPOption, dhcpIPv4_LEASE_TIME_OPTION_CODE, ulLeaseTime ); + prvWriteDHCPOptionU32( &DHCPOption, dhcpIPv4_DNS_SERVER_OPTIONS_CODE, ipBROADCAST_IP_ADDRESS ); + *DHCPOption++ = 0xFF; + TEST_ASSERT_EQUAL( DHCPOption - DHCPMsg, xTotalLength ); /* Put the information in global variables to be returned by * the FreeRTOS_recvrom. */ @@ -4821,9 +4773,6 @@ void test_vDHCPProcess_eWaitingAcknowledge_IPv4ServerIncorrectLength( void ) uint8_t DHCPMsg[ xTotalLength ]; uint32_t DHCPServerAddress = 0xC0A80001; /* 192.168.0.1 */ uint32_t ulClientIPAddress = 0xC0A8000A; /* 192.168.0.10 */ - uint32_t ulSubnetMask = 0xFFFFF100; /* 255.255.241.0 */ - uint32_t ulGateway = 0xC0A80001; /* 192.168.0.1 */ - uint32_t ulLeaseTime = 0x00000096; /* 150 seconds */ DHCPMessage_IPv4_t * pxDHCPMessage = ( DHCPMessage_IPv4_t * ) DHCPMsg; NetworkEndPoint_t xEndPoint = { 0 }, * pxEndPoint = &xEndPoint; @@ -4858,7 +4807,7 @@ void test_vDHCPProcess_eWaitingAcknowledge_IPv4ServerIncorrectLength( void ) /* Add length. */ DHCPOption[ 1 ] = 4; /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = DHCPServerAddress; + memcpy( &DHCPOption[ 2 ], &DHCPServerAddress, sizeof( DHCPServerAddress ) ); DHCPOption += 6; /* Add Message type code. */ @@ -4866,7 +4815,7 @@ void test_vDHCPProcess_eWaitingAcknowledge_IPv4ServerIncorrectLength( void ) /* Add length. */ DHCPOption[ 1 ] = 3; /* Add the offer byte. */ - *( ( uint32_t * ) &DHCPOption[ 2 ] ) = DHCPServerAddress; + memcpy( &DHCPOption[ 2 ], &DHCPServerAddress, sizeof( DHCPServerAddress ) ); /* Put the information in global variables to be returned by * the FreeRTOS_recvrom. */