1 #include "esp_flash.h"
2 #include "esp_partition.h"
3 #include "unity.h"
4 
5 TEST_CASE("Basic handling of a partition in external flash", "[partition]")
6 {
7     esp_flash_t flash = {
8             .size = 1 * 1024 * 1024,
9     };
10 
11     const esp_partition_type_t t = ESP_PARTITION_TYPE_DATA;
12     const esp_partition_subtype_t st = ESP_PARTITION_SUBTYPE_DATA_FAT;
13     const char* label = "ext_fat";
14 
15     /* can register */
16     const esp_partition_t* ext_partition;
17     TEST_ESP_OK(esp_partition_register_external(&flash, 0, flash.size, label, t, st, &ext_partition));
18 
19     /* can find the registered partition */
20     const esp_partition_t* found = esp_partition_find_first(t, st, label);
21     TEST_ASSERT_EQUAL_HEX(ext_partition, found);
22     TEST_ASSERT_EQUAL(found->size, flash.size);
23     TEST_ASSERT_EQUAL(found->address, 0);
24 
25     /* can deregister */
26     TEST_ESP_OK(esp_partition_deregister_external(ext_partition));
27 
28     /* can not deregister one of the partitions on the main flash chip */
29     const esp_partition_t* nvs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
30     TEST_ASSERT_NOT_NULL(nvs_partition);
31     TEST_ESP_ERR(ESP_ERR_INVALID_ARG, esp_partition_deregister_external(nvs_partition));
32 
33     /* can not deregister an unknown partition */
34     esp_partition_t dummy_partition = {};
35     TEST_ESP_ERR(ESP_ERR_NOT_FOUND, esp_partition_deregister_external(&dummy_partition));
36 
37     /* can not register a partition larger than the flash size */
38     TEST_ESP_ERR(ESP_ERR_INVALID_SIZE,
39             esp_partition_register_external(&flash, 0, 2 * flash.size, "ext_fat", t, st, &ext_partition));
40 
41     /* can not register an overlapping partition */
42     TEST_ESP_OK(esp_partition_register_external(&flash, 0, 2 * SPI_FLASH_SEC_SIZE, "p1", t, st, &ext_partition));
43     TEST_ESP_ERR(ESP_ERR_INVALID_ARG, esp_partition_register_external(&flash, SPI_FLASH_SEC_SIZE, 2 * SPI_FLASH_SEC_SIZE,
44             "p2", t, st, NULL));
45     TEST_ESP_OK(esp_partition_deregister_external(ext_partition));
46 }
47