diff options
| author | hathach <[email protected]> | 2022-12-08 13:10:24 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2022-12-08 13:10:24 +0700 |
| commit | bae7fe5be7f8a29a4b0951c7270718f20110a394 (patch) | |
| tree | f2fbf8fa7e4a3232af867c6e6cda8ca64abf168c | |
| parent | d4620d99d31b5566248d2f3a182d7c84633601af (diff) | |
add test fifo overwritable + overflow, that cause buffer overflow
| -rw-r--r-- | test/unit-test/test/test_fifo.c | 111 |
1 files changed, 69 insertions, 42 deletions
diff --git a/test/unit-test/test/test_fifo.c b/test/unit-test/test/test_fifo.c index 0a5f4d3b9..5e639401d 100644 --- a/test/unit-test/test/test_fifo.c +++ b/test/unit-test/test/test_fifo.c @@ -28,15 +28,21 @@ #include "unity.h" #include "tusb_fifo.h" -#define FIFO_SIZE 10 +#define FIFO_SIZE 64 TU_FIFO_DEF(tu_ff, FIFO_SIZE, uint8_t, false); tu_fifo_t* ff = &tu_ff; tu_fifo_buffer_info_t info; +uint8_t test_data[4096]; +uint8_t rd_buf[FIFO_SIZE]; + void setUp(void) { tu_fifo_clear(ff); memset(&info, 0, sizeof(tu_fifo_buffer_info_t)); + + for(int i=0; i<sizeof(test_data); i++) test_data[i] = i; + memset(rd_buf, 0, sizeof(rd_buf)); } void tearDown(void) @@ -61,85 +67,106 @@ void test_normal(void) void test_item_size(void) { TU_FIFO_DEF(ff4, FIFO_SIZE, uint32_t, false); - tu_fifo_clear(&ff4); - uint32_t data[20]; - for(uint32_t i=0; i<sizeof(data)/4; i++) data[i] = i; + uint32_t data4[2*FIFO_SIZE]; + for(uint32_t i=0; i<sizeof(data4)/4; i++) data4[i] = i; - tu_fifo_write_n(&ff4, data, 10); + // fill up fifo + tu_fifo_write_n(&ff4, data4, FIFO_SIZE); - uint32_t rd[10]; + uint32_t rd_buf4[FIFO_SIZE]; uint16_t rd_count; // read 0 -> 4 - rd_count = tu_fifo_read_n(&ff4, rd, 5); + rd_count = tu_fifo_read_n(&ff4, rd_buf4, 5); TEST_ASSERT_EQUAL( 5, rd_count ); - TEST_ASSERT_EQUAL_UINT32_ARRAY( data, rd, rd_count ); // 0 -> 4 + TEST_ASSERT_EQUAL_UINT32_ARRAY( data4, rd_buf4, rd_count ); // 0 -> 4 - tu_fifo_write_n(&ff4, data+10, 5); + tu_fifo_write_n(&ff4, data4+FIFO_SIZE, 5); - // read 5 -> 14 - rd_count = tu_fifo_read_n(&ff4, rd, 10); - TEST_ASSERT_EQUAL( 10, rd_count ); - TEST_ASSERT_EQUAL_UINT32_ARRAY( data+5, rd, rd_count ); // 5 -> 14 + // read all 5 -> 68 + rd_count = tu_fifo_read_n(&ff4, rd_buf4, FIFO_SIZE); + TEST_ASSERT_EQUAL( FIFO_SIZE, rd_count ); + TEST_ASSERT_EQUAL_UINT32_ARRAY( data4+5, rd_buf4, rd_count ); // 5 -> 68 } void test_read_n(void) { - // prepare data - uint8_t data[20]; - for(int i=0; i<sizeof(data); i++) data[i] = i; - - for(uint8_t i=0; i < FIFO_SIZE; i++) tu_fifo_write(ff, data+i); - - uint8_t rd[10]; uint16_t rd_count; + // fill up fifo + for(uint8_t i=0; i < FIFO_SIZE; i++) tu_fifo_write(ff, test_data+i); + // case 1: Read index + count < depth // read 0 -> 4 - rd_count = tu_fifo_read_n(ff, rd, 5); + rd_count = tu_fifo_read_n(ff, rd_buf, 5); TEST_ASSERT_EQUAL( 5, rd_count ); - TEST_ASSERT_EQUAL_MEMORY( data, rd, rd_count ); // 0 -> 4 + TEST_ASSERT_EQUAL_MEMORY( test_data, rd_buf, rd_count ); // 0 -> 4 // case 2: Read index + count > depth // write 10, 11, 12 - tu_fifo_write(ff, data+10); - tu_fifo_write(ff, data+11); - tu_fifo_write(ff, data+12); + tu_fifo_write(ff, test_data+FIFO_SIZE); + tu_fifo_write(ff, test_data+FIFO_SIZE+1); + tu_fifo_write(ff, test_data+FIFO_SIZE+2); - rd_count = tu_fifo_read_n(ff, rd, 7); + rd_count = tu_fifo_read_n(ff, rd_buf, 7); TEST_ASSERT_EQUAL( 7, rd_count ); - TEST_ASSERT_EQUAL_MEMORY( data+5, rd, rd_count ); // 5 -> 11 + TEST_ASSERT_EQUAL_MEMORY( test_data+5, rd_buf, rd_count ); // 5 -> 11 // Should only read until empty - TEST_ASSERT_EQUAL( 1, tu_fifo_read_n(ff, rd, 100) ); + TEST_ASSERT_EQUAL( FIFO_SIZE-5+3-7, tu_fifo_read_n(ff, rd_buf, 100) ); } void test_write_n(void) { - // prepare data - uint8_t data[20]; - for(int i=0; i<sizeof(data); i++) data[i] = i; - // case 1: wr + count < depth - tu_fifo_write_n(ff, data, 8); // wr = 8, count = 8 + tu_fifo_write_n(ff, test_data, 32); // wr = 32, count = 32 - uint8_t rd[10]; uint16_t rd_count; - rd_count = tu_fifo_read_n(ff, rd, 5); // wr = 8, count = 3 - TEST_ASSERT_EQUAL( 5, rd_count ); - TEST_ASSERT_EQUAL_MEMORY( data, rd, rd_count ); // 0 -> 4 + rd_count = tu_fifo_read_n(ff, rd_buf, 16); // wr = 32, count = 16 + TEST_ASSERT_EQUAL( 16, rd_count ); + TEST_ASSERT_EQUAL_MEMORY( test_data, rd_buf, rd_count ); // case 2: wr + count > depth - tu_fifo_write_n(ff, data+8, 6); // wr = 3, count = 9 + tu_fifo_write_n(ff, test_data+32, 40); // wr = 72 -> 8, count = 56 - for(rd_count=0; rd_count<7; rd_count++) tu_fifo_read(ff, rd+rd_count); // wr = 3, count = 2 + tu_fifo_read_n(ff, rd_buf, 32); // count = 24 + TEST_ASSERT_EQUAL_MEMORY( test_data+16, rd_buf, rd_count); - TEST_ASSERT_EQUAL_MEMORY( data+5, rd, rd_count); // 5 -> 11 + TEST_ASSERT_EQUAL(24, tu_fifo_count(ff)); +} - TEST_ASSERT_EQUAL(2, tu_fifo_count(ff)); +static uint16_t help_write(uint16_t total, uint16_t n) +{ + tu_fifo_write_n(ff, test_data, n); + total = tu_min16(FIFO_SIZE, total + n); + + TEST_ASSERT_EQUAL(total, tu_fifo_count(ff)); + TEST_ASSERT_EQUAL(FIFO_SIZE - total, tu_fifo_remaining(ff)); + + return total; +} + +void test_write_overwritable(void) +{ + tu_fifo_set_overwritable(ff, true); + + // based on actual crash tests detected by fuzzing + uint16_t total = 0; + + total = help_write(total, 12); + total = help_write(total, 55); + total = help_write(total, 73); + total = help_write(total, 55); + total = help_write(total, 75); + total = help_write(total, 84); + total = help_write(total, 1); + total = help_write(total, 10); + total = help_write(total, 12); + total = help_write(total, 25); + total = help_write(total, 192); } void test_peek(void) @@ -315,4 +342,4 @@ void test_rd_idx_wrap() n = tu_fifo_read_n(&ff10, dst, 4); TEST_ASSERT_EQUAL(n, 2); TEST_ASSERT_EQUAL(ff10.rd_idx, 6); -}
\ No newline at end of file +} |
