From b1f3cc0024f18b86ecb747b76a277eafe17a5b61 Mon Sep 17 00:00:00 2001 From: mbanth Date: Tue, 25 May 2021 11:23:18 +0100 Subject: [PATCH] Add size range tests. Fix misspelled function name. --- lib_xua/src/hid/hid_report_descriptor.c | 9 +++-- lib_xua/src/hid/xua_hid_report_descriptor.h | 4 +++ tests/xua_unit_tests/src/test_hid.c | 39 +++++++++++++++++++-- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/lib_xua/src/hid/hid_report_descriptor.c b/lib_xua/src/hid/hid_report_descriptor.c index 0d43b9fb..2bddc6cc 100644 --- a/lib_xua/src/hid/hid_report_descriptor.c +++ b/lib_xua/src/hid/hid_report_descriptor.c @@ -239,7 +239,6 @@ static unsigned hidGetItemByteLocation( const unsigned char location ) static unsigned hidGetItemSize( const unsigned char header ) { unsigned bSize = ( header & HID_REPORT_ITEM_HDR_SIZE_MASK ) >> HID_REPORT_ITEM_HDR_SIZE_SHIFT; - assert( bSize <= HID_REPORT_ITEM_MAX_SIZE ); return bSize; } @@ -266,7 +265,7 @@ unsigned char* hidGetReportDescriptor( void ) return retVal; } -void hidPrerpareReportDescriptor( void ) +void hidPrepareReportDescriptor( void ) { unsigned char* ptr = hidReportDescriptor; for( unsigned idx = 0; idx < sizeof hidReportDescriptorItems / sizeof( USB_HID_Short_Item_t ); ++idx ) { @@ -283,9 +282,9 @@ unsigned hidSetReportItem( const unsigned byte, const unsigned bit, const unsign unsigned bTag = hidGetItemTag ( header ); unsigned bType = hidGetItemType( header ); - if(( HID_REPORT_ITEM_MAX_SIZE < bSize ) && - ( HID_REPORT_ITEM_USAGE_TAG == bTag ) && - ( HID_REPORT_ITEM_USAGE_TAG == bType )) { + if(( HID_REPORT_ITEM_MAX_SIZE < bSize ) || + ( HID_REPORT_ITEM_USAGE_TAG != bTag ) || + ( HID_REPORT_ITEM_USAGE_TYPE != bType )) { retVal = HID_STATUS_BAD_HEADER; } else { for( unsigned itemIdx = 0; itemIdx < sizeof hidConfigurableItems / sizeof( USB_HID_Short_Item_t ); ++itemIdx ) { diff --git a/lib_xua/src/hid/xua_hid_report_descriptor.h b/lib_xua/src/hid/xua_hid_report_descriptor.h index 6198ce16..811eb230 100644 --- a/lib_xua/src/hid/xua_hid_report_descriptor.h +++ b/lib_xua/src/hid/xua_hid_report_descriptor.h @@ -75,6 +75,10 @@ void hidPrepareReportDescriptor( void ); /** * @brief Modify a HID Report descriptor item * + * @warning This function does not check that the length of the \a data array matches the value of + * the bSize field in the \a header. For safe operation use a \a data array of at least + * \c HID_REPORT_ITEM_MAX_SIZE bytes in length. + * * Parameters: * * @param[in] byte The byte position of the control within the HID Report diff --git a/tests/xua_unit_tests/src/test_hid.c b/tests/xua_unit_tests/src/test_hid.c index bdac3205..73fa95d3 100644 --- a/tests/xua_unit_tests/src/test_hid.c +++ b/tests/xua_unit_tests/src/test_hid.c @@ -1,6 +1,7 @@ // Copyright 2021 XMOS LIMITED. // This Software is subject to the terms of the XMOS Public Licence: Version 1. #include +#include #include "xua_unit_tests.h" #include "xua_hid_report_descriptor.h" @@ -18,15 +19,15 @@ static unsigned construct_usage_header( unsigned size ) } // Basic report descriptor tests -void test_uninitialised_hidGetReportDescriptor( void ) +void test_unprepared_hidGetReportDescriptor( void ) { unsigned char* reportDescPtr = hidGetReportDescriptor(); TEST_ASSERT_NULL( reportDescPtr ); } -void test_initialised_hidGetReportDescriptor( void ) +void test_prepared_hidGetReportDescriptor( void ) { - hidInitReportDescriptor(); + hidPrepareReportDescriptor(); unsigned char* reportDescPtr = hidGetReportDescriptor(); TEST_ASSERT_NOT_NULL( reportDescPtr ); } @@ -112,3 +113,35 @@ void test_underflow_byte_hidSetReportItem( void ) unsigned retVal = hidSetReportItem( ( unsigned ) byte, bit, header, NULL ); TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_LOCATION, retVal ); } + +// Size range tests +void test_max_size_hidSetReportItem( void ) +{ + const unsigned bit = 0; + const unsigned byte = 0; + const unsigned char data[ HID_REPORT_ITEM_MAX_SIZE ] = { 0x00 }; + const unsigned char header = construct_usage_header( HID_REPORT_ITEM_MAX_SIZE ); + + unsigned retVal = hidSetReportItem( byte, bit, header, data ); + TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal ); +} + +void test_min_size_hidSetReportItem( void ) +{ + const unsigned bit = 0; + const unsigned byte = 0; + const unsigned char header = construct_usage_header( 0x00 ); + + unsigned retVal = hidSetReportItem( byte, bit, header, NULL ); + TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal ); +} + +void test_unsupported_size_hidSetReportItem( void ) +{ + const unsigned bit = 0; + const unsigned byte = 0; + const unsigned char header = construct_usage_header( 0x03 ); + + unsigned retVal = hidSetReportItem( byte, bit, header, NULL ); + TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_HEADER, retVal ); +}