Add and modify unit tests

This commit is contained in:
mbanth
2021-07-06 16:47:58 +01:00
parent f25a765e82
commit 5910638bed
2 changed files with 175 additions and 23 deletions

View File

@@ -52,6 +52,14 @@ static USB_HID_Short_Item_t* const hidConfigurableItems[] = {
&hidUsageByte1Bit7
};
/*
* List Usage pages in the HID Report descriptor, one per byte.
*/
static const USB_HID_Short_Item_t* const hidUsagePages[] = {
&hidUsagePageConsumer,
&hidUsagePageConsumer
};
/*
* List all items in the HID Report descriptor.
*/

View File

@@ -12,6 +12,7 @@
#define HID_REPORT_ITEM_TYPE_MAIN ( 0x00 )
#define HID_REPORT_ITEM_TYPE_RESERVED ( 0x03 )
#define CONSUMER_CONTROL_PAGE ( 0x0C )
#define LOUDNESS_CONTROL ( 0xE7 )
static unsigned construct_usage_header( unsigned size )
@@ -68,6 +69,103 @@ void test_reset_prepared_hidGetReportDescriptor( void )
TEST_ASSERT_NOT_NULL( reportDescPtr );
}
// Basic item tests
void test_max_loc_hidGetReportItem( void )
{
const unsigned bit = MAX_VALID_BIT;
const unsigned byte = MAX_VALID_BYTE;
unsigned char data[ HID_REPORT_ITEM_MAX_SIZE ];
unsigned char header;
unsigned char page;
unsigned retVal = hidGetReportItem( byte, bit, &page, &header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
TEST_ASSERT_EQUAL_UINT( CONSUMER_CONTROL_PAGE, page );
TEST_ASSERT_EQUAL_UINT( 0x09, header );
TEST_ASSERT_EQUAL_UINT( 0xEA, data[ 0 ]);
TEST_ASSERT_EQUAL_UINT( 0x00, data[ 1 ]);
}
void test_min_loc_hidGetReportItem( void )
{
const unsigned bit = MIN_VALID_BIT;
const unsigned byte = MIN_VALID_BYTE;
unsigned char data[ HID_REPORT_ITEM_MAX_SIZE ];
unsigned char header;
unsigned char page;
unsigned retVal = hidGetReportItem( byte, bit, &page, &header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
TEST_ASSERT_EQUAL_UINT( CONSUMER_CONTROL_PAGE, page );
TEST_ASSERT_EQUAL_UINT( 0x09, header );
TEST_ASSERT_EQUAL_UINT( 0xE2, data[ 0 ]);
TEST_ASSERT_EQUAL_UINT( 0x00, data[ 1 ]);
}
void test_overflow_bit_hidGetReportItem( void )
{
const unsigned bit = MAX_VALID_BIT + 1;
const unsigned byte = MAX_VALID_BYTE;
unsigned char data[ HID_REPORT_ITEM_MAX_SIZE ] = { 0xBA, 0xD1 };
unsigned char header = 0xAA;
unsigned char page = 0x44;
unsigned retVal = hidGetReportItem( byte, bit, &page, &header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_LOCATION, retVal );
TEST_ASSERT_EQUAL_UINT( 0x44, page );
TEST_ASSERT_EQUAL_UINT( 0xAA, header );
TEST_ASSERT_EQUAL_UINT( 0xBA, data[ 0 ]);
TEST_ASSERT_EQUAL_UINT( 0xD1, data[ 1 ]);
}
void test_overflow_byte_hidGetReportItem( void )
{
const unsigned bit = MAX_VALID_BIT;
const unsigned byte = MAX_VALID_BYTE + 1;
unsigned char data[ HID_REPORT_ITEM_MAX_SIZE ] = { 0xBA, 0xD1 };
unsigned char header = 0xAA;
unsigned char page = 0x44;
unsigned retVal = hidGetReportItem( byte, bit, &page, &header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_LOCATION, retVal );
TEST_ASSERT_EQUAL_UINT( 0x44, page );
TEST_ASSERT_EQUAL_UINT( 0xAA, header );
TEST_ASSERT_EQUAL_UINT( 0xBA, data[ 0 ]);
TEST_ASSERT_EQUAL_UINT( 0xD1, data[ 1 ]);
}
void test_underflow_bit_hidGetReportItem( void )
{
const int bit = MIN_VALID_BIT - 1;
const unsigned byte = MIN_VALID_BYTE;
unsigned char data[ HID_REPORT_ITEM_MAX_SIZE ] = { 0xBA, 0xD1 };
unsigned char header = 0xAA;
unsigned char page = 0x44;
unsigned retVal = hidGetReportItem( byte, ( unsigned ) bit, &page, &header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_LOCATION, retVal );
TEST_ASSERT_EQUAL_UINT( 0x44, page );
TEST_ASSERT_EQUAL_UINT( 0xAA, header );
TEST_ASSERT_EQUAL_UINT( 0xBA, data[ 0 ]);
TEST_ASSERT_EQUAL_UINT( 0xD1, data[ 1 ]);
}
void test_underflow_byte_hidGetReportItem( void )
{
const unsigned bit = MIN_VALID_BIT;
const int byte = MIN_VALID_BYTE - 1;
unsigned char data[ HID_REPORT_ITEM_MAX_SIZE ] = { 0xBA, 0xD1 };
unsigned char header = 0xAA;
unsigned char page = 0x44;
unsigned retVal = hidGetReportItem(( unsigned ) byte, bit, &page, &header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_LOCATION, retVal );
TEST_ASSERT_EQUAL_UINT( 0x44, page );
TEST_ASSERT_EQUAL_UINT( 0xAA, header );
TEST_ASSERT_EQUAL_UINT( 0xBA, data[ 0 ]);
TEST_ASSERT_EQUAL_UINT( 0xD1, data[ 1 ]);
}
// Configurable and non-configurable item tests
void test_configurable_item_hidSetReportItem( void )
{
@@ -75,8 +173,9 @@ void test_configurable_item_hidSetReportItem( void )
const unsigned byte = MIN_VALID_BYTE;
const unsigned char data[ 1 ] = { LOUDNESS_CONTROL };
const unsigned char header = construct_usage_header( sizeof data / sizeof( unsigned char ));
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, data );
unsigned retVal = hidSetReportItem( byte, bit, page, header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
}
@@ -86,8 +185,9 @@ void test_nonconfigurable_item_hidSetReportItem( void )
const unsigned byte = MIN_VALID_BYTE; // hidConfigurableItems list in hid_report_descriptors.c.
const unsigned char data[ 1 ] = { LOUDNESS_CONTROL };
const unsigned char header = construct_usage_header( sizeof data / sizeof( unsigned char ));
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, data );
unsigned retVal = hidSetReportItem( byte, bit, page, header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_LOCATION, retVal );
}
@@ -97,8 +197,9 @@ void test_max_bit_hidSetReportItem( void )
const unsigned bit = MAX_VALID_BIT; // Only byte 1 has bit 7 not reserved, See the
const unsigned byte = MAX_VALID_BYTE; // hidConfigurableItems list in hid_report_descriptors.c.
const unsigned char header = construct_usage_header( 0 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
}
@@ -107,8 +208,9 @@ void test_min_bit_hidSetReportItem( void )
const unsigned bit = MIN_VALID_BIT;
const unsigned byte = MIN_VALID_BYTE;
const unsigned char header = construct_usage_header( 0 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
}
@@ -117,8 +219,9 @@ void test_overflow_bit_hidSetReportItem( void )
const unsigned bit = MAX_VALID_BIT + 1;
const unsigned byte = MIN_VALID_BYTE;
const unsigned char header = construct_usage_header( 0 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_LOCATION, retVal );
}
@@ -127,8 +230,9 @@ void test_underflow_bit_hidSetReportItem( void )
const int bit = MIN_VALID_BIT - 1;
const unsigned byte = MIN_VALID_BYTE;
const unsigned char header = construct_usage_header( 0 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, ( unsigned ) bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, ( unsigned ) bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_LOCATION, retVal );
}
@@ -138,8 +242,9 @@ void test_max_byte_hidSetReportItem( void )
const unsigned bit = MIN_VALID_BIT;
const unsigned byte = MAX_VALID_BYTE;
const unsigned char header = construct_usage_header( 0 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
}
@@ -148,8 +253,9 @@ void test_min_byte_hidSetReportItem( void )
const unsigned bit = MIN_VALID_BIT;
const unsigned byte = MIN_VALID_BYTE;
const unsigned char header = construct_usage_header( 0 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
}
@@ -158,8 +264,9 @@ void test_overflow_byte_hidSetReportItem( void )
const unsigned bit = MIN_VALID_BIT;
const unsigned byte = MAX_VALID_BYTE + 1;
const unsigned char header = construct_usage_header( 0 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_LOCATION, retVal );
}
@@ -168,8 +275,9 @@ void test_underflow_byte_hidSetReportItem( void )
const unsigned bit = MIN_VALID_BIT;
const int byte = MIN_VALID_BYTE - 1;
const unsigned char header = construct_usage_header( 0 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( ( unsigned ) byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( ( unsigned ) byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_LOCATION, retVal );
}
@@ -180,8 +288,9 @@ void test_max_size_hidSetReportItem( void )
const unsigned byte = MIN_VALID_BYTE;
const unsigned char data[ HID_REPORT_ITEM_MAX_SIZE ] = { 0x00 };
const unsigned char header = construct_usage_header( HID_REPORT_ITEM_MAX_SIZE );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, data );
unsigned retVal = hidSetReportItem( byte, bit, page, header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
}
@@ -190,8 +299,9 @@ void test_min_size_hidSetReportItem( void )
const unsigned bit = MIN_VALID_BIT;
const unsigned byte = MIN_VALID_BYTE;
const unsigned char header = construct_usage_header( 0x00 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
}
@@ -200,8 +310,9 @@ void test_unsupported_size_hidSetReportItem( void )
const unsigned bit = MIN_VALID_BIT;
const unsigned byte = MIN_VALID_BYTE;
const unsigned char header = construct_usage_header( 0x03 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_HEADER, retVal );
}
@@ -211,10 +322,11 @@ void test_bad_tag_hidSetReportItem( void )
const unsigned bit = MIN_VALID_BIT;
const unsigned byte = MIN_VALID_BYTE;
const unsigned char good_header = construct_usage_header( 0x00 );
const unsigned char page = CONSUMER_CONTROL_PAGE;
for( unsigned tag = 0x01; tag <= 0x0F; ++tag ) {
unsigned char bad_header = good_header | (( 0x0F << HID_REPORT_ITEM_HDR_TAG_SHIFT ) & HID_REPORT_ITEM_HDR_TAG_MASK );
unsigned retVal = hidSetReportItem( byte, bit, bad_header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, bad_header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_HEADER, retVal );
}
}
@@ -225,8 +337,9 @@ void test_global_type_hidSetReportItem( void )
const unsigned byte = MIN_VALID_BYTE;
const unsigned char header = ( construct_usage_header( 0x00 ) & ~HID_REPORT_ITEM_HDR_TYPE_MASK ) |
(( HID_REPORT_ITEM_TYPE_GLOBAL << HID_REPORT_ITEM_HDR_TYPE_SHIFT ) & HID_REPORT_ITEM_HDR_TYPE_MASK );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_HEADER, retVal );
}
@@ -236,8 +349,9 @@ void test_local_type_hidSetReportItem( void )
const unsigned byte = MIN_VALID_BYTE;
const unsigned char header = ( construct_usage_header( 0x00 ) & ~HID_REPORT_ITEM_HDR_TYPE_MASK ) |
(( HID_REPORT_ITEM_TYPE_LOCAL << HID_REPORT_ITEM_HDR_TYPE_SHIFT ) & HID_REPORT_ITEM_HDR_TYPE_MASK );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
}
@@ -247,8 +361,9 @@ void test_main_type_hidSetReportItem( void )
const unsigned byte = MIN_VALID_BYTE;
const unsigned char header = ( construct_usage_header( 0x00 ) & ~HID_REPORT_ITEM_HDR_TYPE_MASK ) |
(( HID_REPORT_ITEM_TYPE_MAIN << HID_REPORT_ITEM_HDR_TYPE_SHIFT ) & HID_REPORT_ITEM_HDR_TYPE_MASK );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_HEADER, retVal );
}
@@ -258,8 +373,9 @@ void test_reserved_type_hidSetReportItem( void )
const unsigned byte = MIN_VALID_BYTE;
const unsigned char header = ( construct_usage_header( 0x00 ) & ~HID_REPORT_ITEM_HDR_TYPE_MASK ) |
(( HID_REPORT_ITEM_TYPE_RESERVED << HID_REPORT_ITEM_HDR_TYPE_SHIFT ) & HID_REPORT_ITEM_HDR_TYPE_MASK );
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, NULL );
unsigned retVal = hidSetReportItem( byte, bit, page, header, NULL );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_BAD_HEADER, retVal );
}
@@ -270,8 +386,9 @@ void test_initial_modification_without_subsequent_preparation( void )
const unsigned byte = MIN_VALID_BYTE;
const unsigned char data[ 1 ] = { LOUDNESS_CONTROL };
const unsigned char header = construct_usage_header( sizeof data / sizeof( unsigned char ));
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, data );
unsigned retVal = hidSetReportItem( byte, bit, page, header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
unsigned char* reportDescPtr = hidGetReportDescriptor();
@@ -284,8 +401,9 @@ void test_initial_modification_with_subsequent_preparation( void )
const unsigned byte = MIN_VALID_BYTE;
const unsigned char data[ 1 ] = { LOUDNESS_CONTROL };
const unsigned char header = construct_usage_header( sizeof data / sizeof( unsigned char ));
const unsigned char page = CONSUMER_CONTROL_PAGE;
unsigned retVal = hidSetReportItem( byte, bit, header, data );
unsigned retVal = hidSetReportItem( byte, bit, page, header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
hidPrepareReportDescriptor();
@@ -293,6 +411,30 @@ void test_initial_modification_with_subsequent_preparation( void )
TEST_ASSERT_NOT_NULL( reportDescPtr );
}
void test_initial_modification_with_subsequent_verification( void )
{
const unsigned bit = MIN_VALID_BIT;
const unsigned byte = MIN_VALID_BYTE;
unsigned char get_data[ HID_REPORT_ITEM_MAX_SIZE ] = { 0xFF, 0xFF };
unsigned char get_header = 0xFF;
unsigned char get_page = 0xFF;
const unsigned char set_data[ 1 ] = { LOUDNESS_CONTROL };
const unsigned char set_header = construct_usage_header( sizeof set_data / sizeof( unsigned char ));
const unsigned char set_page = CONSUMER_CONTROL_PAGE;
unsigned setRetVal = hidSetReportItem( byte, bit, set_page, set_header, set_data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, setRetVal );
unsigned getRetVal = hidGetReportItem( byte, bit, &get_page, &get_header, get_data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, getRetVal );
TEST_ASSERT_EQUAL_UINT( get_page, set_page );
TEST_ASSERT_EQUAL_UINT( get_header, set_header );
TEST_ASSERT_EQUAL_UINT( get_data[ 0 ], set_data[ 0 ]);
TEST_ASSERT_EQUAL_UINT( get_data[ 1 ], set_data[ 1 ]);
}
void test_modification_without_subsequent_preparation( void )
{
hidPrepareReportDescriptor();
@@ -303,9 +445,10 @@ void test_modification_without_subsequent_preparation( void )
const unsigned byte = MIN_VALID_BYTE;
const unsigned char data[ 1 ] = { LOUDNESS_CONTROL };
const unsigned char header = construct_usage_header( sizeof data / sizeof( unsigned char ));
const unsigned char page = CONSUMER_CONTROL_PAGE;
hidResetReportDescriptor();
unsigned retVal = hidSetReportItem( byte, bit, header, data );
unsigned retVal = hidSetReportItem( byte, bit, page, header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
reportDescPtr = hidGetReportDescriptor();
@@ -322,9 +465,10 @@ void test_modification_with_subsequent_preparation( void )
const unsigned byte = MIN_VALID_BYTE;
const unsigned char data[ 1 ] = { LOUDNESS_CONTROL };
const unsigned char header = construct_usage_header( sizeof data / sizeof( unsigned char ));
const unsigned char page = CONSUMER_CONTROL_PAGE;
hidResetReportDescriptor();
unsigned retVal = hidSetReportItem( byte, bit, header, data );
unsigned retVal = hidSetReportItem( byte, bit, page, header, data );
TEST_ASSERT_EQUAL_UINT( HID_STATUS_GOOD, retVal );
hidPrepareReportDescriptor();