Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_nvs_host: fix asan reported bugs (IDFGH-9860) #11117

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions components/nvs_flash/test_nvs_host/test_nvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ void dumpBytes(const uint8_t *data, size_t count)
}
}

bool memeq(void *a, size_t a_len, void *b, size_t b_len)
{
if (a_len != b_len) {
return false;
}
return memcmp(a, b, a_len) == 0;
}

TEST_CASE("Page handles invalid CRC of variable length items", "[nvs][cur]")
{
Expand Down Expand Up @@ -793,12 +800,12 @@ TEST_CASE("Check that NVS supports old blob format without blob index", "[nvs]")
size_t buflen = 64;
uint8_t hexdata[] = {0x01, 0x02, 0x03, 0xab, 0xcd, 0xef};
TEST_ESP_OK( nvs_get_blob(handle, "dummyHex2BinKey", buf, &buflen));
CHECK(memcmp(buf, hexdata, buflen) == 0);
CHECK(memeq(buf, buflen, hexdata, sizeof(hexdata)));

buflen = 64;
uint8_t base64data[] = {'1', '2', '3', 'a', 'b', 'c'};
TEST_ESP_OK( nvs_get_blob(handle, "dummyBase64Key", buf, &buflen));
CHECK(memcmp(buf, base64data, buflen) == 0);
CHECK(memeq(buf, buflen, base64data, sizeof(base64data)));

nvs::Page p;
p.load(&part, 0);
Expand All @@ -822,7 +829,7 @@ TEST_CASE("Check that NVS supports old blob format without blob index", "[nvs]")
/* Read the blob in new format and check the contents*/
buflen = 64;
TEST_ESP_OK( nvs_get_blob(handle, "dummyBase64Key", buf, &buflen));
CHECK(memcmp(buf, base64data, buflen) == 0);
CHECK(memeq(buf, buflen, base64data, sizeof(base64data)));

TEST_ESP_OK(nvs_flash_deinit_partition(part.get_partition_name()));
}
Expand Down Expand Up @@ -886,21 +893,21 @@ static void check_nvs_part_gen_args(SpiFlashEmulator *spi_flash_emulator,
size_t buflen = 64;
int j;
TEST_ESP_OK( nvs_get_blob(handle, "dummyHex2BinKey", buf, &buflen));
CHECK(memcmp(buf, hexdata, buflen) == 0);
CHECK(memeq(buf, buflen, hexdata, sizeof(hexdata)));

uint8_t base64data[] = {'1', '2', '3', 'a', 'b', 'c'};
TEST_ESP_OK( nvs_get_blob(handle, "dummyBase64Key", buf, &buflen));
CHECK(memcmp(buf, base64data, buflen) == 0);
CHECK(memeq(buf, buflen, base64data, sizeof(base64data)));

buflen = 64;
uint8_t hexfiledata[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
TEST_ESP_OK( nvs_get_blob(handle, "hexFileKey", buf, &buflen));
CHECK(memcmp(buf, hexfiledata, buflen) == 0);
CHECK(memeq(buf, buflen, hexfiledata, sizeof(hexfiledata)));

buflen = 64;
uint8_t strfiledata[64] = "abcdefghijklmnopqrstuvwxyz\0";
TEST_ESP_OK( nvs_get_str(handle, "stringFileKey", buf, &buflen));
CHECK(memcmp(buf, strfiledata, buflen) == 0);
CHECK(memeq(buf, buflen, strfiledata, sizeof(strfiledata)));

char bin_data[5200];
size_t bin_len = sizeof(bin_data);
Expand All @@ -909,7 +916,7 @@ static void check_nvs_part_gen_args(SpiFlashEmulator *spi_flash_emulator,
file.open(filename);
file.read(binfiledata,5200);
TEST_ESP_OK( nvs_get_blob(handle, "binFileKey", bin_data, &bin_len));
CHECK(memcmp(bin_data, binfiledata, bin_len) == 0);
CHECK(memeq(bin_data, bin_len, binfiledata, sizeof(binfiledata)));

file.close();

Expand Down Expand Up @@ -971,21 +978,21 @@ static void check_nvs_part_gen_args_mfg(SpiFlashEmulator *spi_flash_emulator,
buflen = 64;
int j;
TEST_ESP_OK( nvs_get_blob(handle, "dummyHex2BinKey", buf, &buflen));
CHECK(memcmp(buf, hexdata, buflen) == 0);
CHECK(memeq(buf, buflen, hexdata, sizeof(hexdata)));

uint8_t base64data[] = {'1', '2', '3', 'a', 'b', 'c'};
TEST_ESP_OK( nvs_get_blob(handle, "dummyBase64Key", buf, &buflen));
CHECK(memcmp(buf, base64data, buflen) == 0);
CHECK(memeq(buf, buflen, base64data, sizeof(base64data)));

buflen = 64;
uint8_t hexfiledata[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
TEST_ESP_OK( nvs_get_blob(handle, "hexFileKey", buf, &buflen));
CHECK(memcmp(buf, hexfiledata, buflen) == 0);
CHECK(memeq(buf, buflen, hexfiledata, sizeof(hexfiledata)));

buflen = 64;
uint8_t strfiledata[64] = "abcdefghijklmnopqrstuvwxyz\0";
TEST_ESP_OK( nvs_get_str(handle, "stringFileKey", buf, &buflen));
CHECK(memcmp(buf, strfiledata, buflen) == 0);
CHECK(memeq(buf, buflen, strfiledata, sizeof(strfiledata)));

char bin_data[5200];
size_t bin_len = sizeof(bin_data);
Expand All @@ -994,7 +1001,7 @@ static void check_nvs_part_gen_args_mfg(SpiFlashEmulator *spi_flash_emulator,
file.open(filename);
file.read(binfiledata,5200);
TEST_ESP_OK( nvs_get_blob(handle, "binFileKey", bin_data, &bin_len));
CHECK(memcmp(bin_data, binfiledata, bin_len) == 0);
CHECK(memeq(bin_data, bin_len, binfiledata, sizeof(binfiledata)));

file.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ TEST_CASE("EMU raw read function works", "[spi_flash_emu]")
uint32_t read_value = 0;
CHECK(esp_partition_write(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK);

CHECK(esp_partition_read_raw(&f.esp_part, 0, &read_value, sizeof(&read_value)) == ESP_OK);
CHECK(esp_partition_read_raw(&f.esp_part, 0, &read_value, sizeof(read_value)) == ESP_OK);

CHECK(read_value == 0xdeadbeef);
}
Expand All @@ -97,7 +97,7 @@ TEST_CASE("EMU raw write function works", "[spi_flash_emu]")
uint32_t read_value = 0;
CHECK(esp_partition_write_raw(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK);

CHECK(esp_partition_read(&f.esp_part, 0, &read_value, sizeof(&read_value)) == ESP_OK);
CHECK(esp_partition_read(&f.esp_part, 0, &read_value, sizeof(read_value)) == ESP_OK);

CHECK(read_value == 0xdeadbeef);
}
Expand Down