test_nvs_initialization.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 "spi_flash_emulation.h"
  15. #include "esp_partition.h"
  16. #include "nvs.h"
  17. #include "nvs_flash.h"
  18. #include <string.h>
  19. #include <iostream>
  20. TEST_CASE("nvs_flash_init_partition_ptr fails due to nullptr arg", "[nvs_custom_part]")
  21. {
  22. const uint32_t NVS_FLASH_SECTOR = 6;
  23. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  24. SpiFlashEmulator emu(10);
  25. CHECK(nvs_flash_init_partition_ptr(nullptr) == ESP_ERR_INVALID_ARG);
  26. }
  27. TEST_CASE("nvs_flash_init_partition_ptr inits one partition", "[nvs_custom_part]")
  28. {
  29. const uint32_t NVS_FLASH_SECTOR = 6;
  30. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  31. SpiFlashEmulator emu(10);
  32. nvs_handle_t out_handle;
  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. nvs_flash_deinit_partition("test");
  38. CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_ERR_NVS_PART_NOT_FOUND);
  39. CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_OK);
  40. CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_OK);
  41. CHECK(nvs_flash_deinit_partition("test") == ESP_OK);
  42. }
  43. TEST_CASE("nvs_flash_init_partition_ptr deinits one partition", "[nvs_custom_part]")
  44. {
  45. const uint32_t NVS_FLASH_SECTOR = 6;
  46. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  47. SpiFlashEmulator emu(10);
  48. nvs_handle_t out_handle;
  49. esp_partition_t partition = {};
  50. strcpy(partition.label, "test");
  51. partition.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE;
  52. partition.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE;
  53. nvs_flash_deinit_partition("test");
  54. CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_OK);
  55. CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_OK);
  56. CHECK(nvs_flash_deinit_partition("test") == ESP_OK);
  57. CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_ERR_NVS_PART_NOT_FOUND);
  58. }