Răsfoiți Sursa

refactor(nvs): using define for namespace len instead of magic number

Closes https://github.com/espressif/esp-idf/issues/9671
Jakob Hasse 3 ani în urmă
părinte
comite
674fa9377f

+ 4 - 3
components/nvs_flash/include/nvs.h

@@ -59,6 +59,7 @@ typedef nvs_handle_t nvs_handle IDF_DEPRECATED("Replace with nvs_handle_t");
 
 #define NVS_PART_NAME_MAX_SIZE              16   /*!< maximum length of partition name (excluding null terminator) */
 #define NVS_KEY_NAME_MAX_SIZE               16   /*!< Maximum length of NVS key name (including null terminator) */
+#define NVS_NS_NAME_MAX_SIZE                NVS_KEY_NAME_MAX_SIZE /*!< Maximum length of NVS namespace name (including null terminator) */
 
 /**
  * @brief Mode of opening the non-volatile storage
@@ -96,9 +97,9 @@ typedef enum {
  * @brief information about entry obtained from nvs_entry_info function
  */
 typedef struct {
-    char namespace_name[16];    /*!< Namespace to which key-value belong */
-    char key[NVS_KEY_NAME_MAX_SIZE];               /*!< Key of stored key-value pair */
-    nvs_type_t type;            /*!< Type of stored key-value pair */
+    char namespace_name[NVS_NS_NAME_MAX_SIZE];  /*!< Namespace to which key-value belong */
+    char key[NVS_KEY_NAME_MAX_SIZE];            /*!< Key of stored key-value pair */
+    nvs_type_t type;                            /*!< Type of stored key-value pair */
 } nvs_entry_info_t;
 
 /**

+ 46 - 13
components/nvs_flash/test_nvs_host/test_nvs_handle.cpp

@@ -1,16 +1,8 @@
-// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
+/*
+ * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
 #include "catch.hpp"
 #include <algorithm>
 #include <cstring>
@@ -27,6 +19,47 @@
 using namespace std;
 using namespace nvs;
 
+TEST_CASE("open_nvs_handle too long namespace name", "[partition_mgr]")
+{
+    const uint32_t NVS_FLASH_SECTOR = 6;
+    const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
+    const char *TOO_LONG_NS_NAME = "0123456789abcdef";
+    char test_e = 'a';
+    NVSHandleSimple *handle;
+    PartitionEmulationFixture f(0, 10, "test");
+    CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
+    REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
+            == ESP_OK);
+
+    REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", TOO_LONG_NS_NAME, NVS_READWRITE, &handle) == ESP_ERR_NVS_KEY_TOO_LONG);
+
+    CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
+    REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
+
+}
+
+TEST_CASE("open_nvs_handle longest namespace name", "[partition_mgr]")
+{
+    const uint32_t NVS_FLASH_SECTOR = 6;
+    const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
+    const char *LONGEST_NS_NAME = "0123456789abcde";
+    char test_e = 'a';
+    NVSHandleSimple *handle;
+    PartitionEmulationFixture f(0, 10, "test");
+    CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 0);
+    REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
+            == ESP_OK);
+
+    REQUIRE(NVSPartitionManager::get_instance()->open_handle("test", LONGEST_NS_NAME, NVS_READWRITE, &handle) == ESP_OK);
+
+    CHECK(NVSPartitionManager::get_instance()->open_handles_size() == 1);
+
+    delete handle;
+
+    REQUIRE(NVSPartitionManager::get_instance()->deinit_partition("test") == ESP_OK);
+
+}
+
 TEST_CASE("NVSHandleSimple closes its reference in PartitionManager", "[partition_mgr]")
 {
     const uint32_t NVS_FLASH_SECTOR = 6;

+ 0 - 1
tools/ci/check_copyright_ignore.txt

@@ -885,7 +885,6 @@ components/nvs_flash/test_nvs_host/spi_flash_emulation.cpp
 components/nvs_flash/test_nvs_host/test_fixtures.hpp
 components/nvs_flash/test_nvs_host/test_intrusive_list.cpp
 components/nvs_flash/test_nvs_host/test_nvs_cxx_api.cpp
-components/nvs_flash/test_nvs_host/test_nvs_handle.cpp
 components/nvs_flash/test_nvs_host/test_nvs_initialization.cpp
 components/nvs_flash/test_nvs_host/test_nvs_partition.cpp
 components/nvs_flash/test_nvs_host/test_nvs_storage.cpp