test_nvs_initialization.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 inits one 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. esp_partition_t partition = {};
  34. strcpy(partition.label, "test");
  35. partition.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
  36. partition.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
  37. CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_OK);
  38. CHECK(NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr);
  39. CHECK(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
  40. }
  41. TEST_CASE("deinit partition doesn't affect other partition's open handles", "[nvs]")
  42. {
  43. const char *OTHER_PARTITION_NAME = "other_part";
  44. const uint32_t NVS_FLASH_SECTOR = 6;
  45. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  46. SpiFlashEmulator emu_default(10);
  47. emu_default.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  48. esp_partition_t part_default = {};
  49. strcpy(part_default.label, NVS_DEFAULT_PART_NAME);
  50. part_default.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
  51. part_default.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
  52. SpiFlashEmulator emu_other(10);
  53. emu_other.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  54. esp_partition_t part_other = {};
  55. strcpy(part_other.label, OTHER_PARTITION_NAME);
  56. part_other.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
  57. part_other.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
  58. const char* str = "value 0123456789abcdef0123456789abcdef";
  59. const uint8_t blob[8] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7};
  60. nvs_handle_t handle_1;
  61. TEST_ESP_OK(NVSPartitionManager::get_instance()->init_custom(NVS_DEFAULT_PART_NAME,
  62. NVS_FLASH_SECTOR,
  63. NVS_FLASH_SECTOR_COUNT_MIN));
  64. TEST_ESP_OK(NVSPartitionManager::get_instance()->init_custom(OTHER_PARTITION_NAME,
  65. NVS_FLASH_SECTOR,
  66. NVS_FLASH_SECTOR_COUNT_MIN));
  67. TEST_ESP_OK(nvs_open_from_partition(OTHER_PARTITION_NAME, "ns", NVS_READWRITE, &handle_1));
  68. // Deinitializing must not interfere with the open handle from the other partition.
  69. TEST_ESP_OK(nvs_flash_deinit_partition(NVS_DEFAULT_PART_NAME));
  70. TEST_ESP_OK(nvs_set_i32(handle_1, "foo", 0x3456789a));
  71. nvs_close(handle_1);
  72. TEST_ESP_OK(nvs_flash_deinit_partition(OTHER_PARTITION_NAME));
  73. }