test_nvs_initialization.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "catch.hpp"
  14. #include "nvs_partition_manager.hpp"
  15. #include "spi_flash_emulation.h"
  16. #include "esp_partition.h"
  17. #include "nvs.h"
  18. #include <string.h>
  19. using namespace nvs;
  20. #define TEST_ESP_OK(rc) CHECK((rc) == ESP_OK)
  21. TEST_CASE("nvs_flash_init_partition_ptr fails due to nullptr arg", "[nvs_custom_part]")
  22. {
  23. const uint32_t NVS_FLASH_SECTOR = 6;
  24. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  25. SpiFlashEmulator emu(10);
  26. CHECK(nvs_flash_init_partition_ptr(nullptr) == ESP_ERR_INVALID_ARG);
  27. }
  28. TEST_CASE("nvs_flash_init_partition_ptr fails due to external partition", "[nvs_custom_part]")
  29. {
  30. const uint32_t NVS_FLASH_SECTOR = 6;
  31. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  32. SpiFlashEmulator emu(10);
  33. struct esp_flash_t spi_flash = {};
  34. esp_partition_t partition = {};
  35. strcpy(partition.label, "test");
  36. partition.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
  37. partition.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
  38. partition.flash_chip = &spi_flash;
  39. CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_ERR_NOT_SUPPORTED);
  40. }
  41. TEST_CASE("nvs_flash_init_partition_ptr inits one partition", "[nvs_custom_part]")
  42. {
  43. const uint32_t NVS_FLASH_SECTOR = 6;
  44. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  45. SpiFlashEmulator emu(10);
  46. esp_partition_t partition = {};
  47. strcpy(partition.label, "test");
  48. partition.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
  49. partition.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
  50. partition.flash_chip = esp_flash_default_chip;
  51. CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_OK);
  52. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
  53. CHECK(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  54. }
  55. TEST_CASE("deinit partition doesn't affect other partition's open handles", "[nvs]")
  56. {
  57. const char *OTHER_PARTITION_NAME = "other_part";
  58. const uint32_t NVS_FLASH_SECTOR = 6;
  59. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  60. SpiFlashEmulator emu_default(10);
  61. emu_default.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  62. esp_partition_t part_default = {};
  63. strcpy(part_default.label, NVS_DEFAULT_PART_NAME);
  64. part_default.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
  65. part_default.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
  66. SpiFlashEmulator emu_other(10);
  67. emu_other.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  68. esp_partition_t part_other = {};
  69. strcpy(part_other.label, OTHER_PARTITION_NAME);
  70. part_other.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
  71. part_other.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
  72. const char* str = "value 0123456789abcdef0123456789abcdef";
  73. const uint8_t blob[8] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7};
  74. nvs_handle_t handle_1;
  75. TEST_ESP_OK(NVSPartitionManager::get_instance()->init_custom(NVS_DEFAULT_PART_NAME,
  76. NVS_FLASH_SECTOR,
  77. NVS_FLASH_SECTOR_COUNT_MIN));
  78. TEST_ESP_OK(NVSPartitionManager::get_instance()->init_custom(OTHER_PARTITION_NAME,
  79. NVS_FLASH_SECTOR,
  80. NVS_FLASH_SECTOR_COUNT_MIN));
  81. TEST_ESP_OK(nvs_open_from_partition(OTHER_PARTITION_NAME, "ns", NVS_READWRITE, &handle_1));
  82. // Deinitializing must not interfere with the open handle from the other partition.
  83. TEST_ESP_OK(nvs_flash_deinit_partition(NVS_DEFAULT_PART_NAME));
  84. TEST_ESP_OK(nvs_set_i32(handle_1, "foo", 0x3456789a));
  85. nvs_close(handle_1);
  86. TEST_ESP_OK(nvs_flash_deinit_partition(OTHER_PARTITION_NAME));
  87. }