1 #include <string.h>
2 
3 #include "unity.h"
4 #include "esp_spi_flash.h"
5 #include "esp_ota_ops.h"
6 
7 #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS || CONFIG_SPI_FLASH_DANGEROUS_WRITE_FAILS
8 
9 static const char *data = "blah blah blah";
10 
11 #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_FAILS
12 #define TEST_TAGS "[spi_flash][esp_flash]"
13 #else // ABORTS
14 #define TEST_TAGS "[spi_flash][esp_flash][ignore]"
15 #endif
16 
17 TEST_CASE("can't overwrite bootloader", TEST_TAGS)
18 {
19     TEST_ESP_ERR(ESP_ERR_INVALID_ARG, spi_flash_write(0x1000, data, strlen(data)));
20     TEST_ESP_ERR(ESP_ERR_INVALID_ARG, spi_flash_write(0x0FF8, data, strlen(data)));
21     TEST_ESP_ERR(ESP_ERR_INVALID_ARG, spi_flash_write(0x1400, data, strlen(data)));
22     TEST_ESP_ERR(ESP_ERR_INVALID_ARG, spi_flash_erase_range(0x8000, 0x2000));
23     TEST_ESP_ERR(ESP_ERR_INVALID_ARG, spi_flash_erase_range(0x7000, 0x2000));
24 }
25 
26 TEST_CASE("can't overwrite current running app", TEST_TAGS)
27 {
28     const esp_partition_t *p = esp_ota_get_running_partition();
29     TEST_ESP_ERR(ESP_ERR_INVALID_ARG, spi_flash_write(p->address + 1024, data, strlen(data)));
30     TEST_ESP_ERR(ESP_ERR_INVALID_ARG, spi_flash_erase_range(p->address + 4096, 8192));
31 }
32 
33 #endif // FAILS || ABORTS
34