3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2016-2017 Nest Labs, Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 * Unit tests for the Chip Persisted Storage API.
26 #ifndef __STDC_FORMAT_MACROS
27 #define __STDC_FORMAT_MACROS
35 #include <nlunit-test.h>
37 #include <CHIPVersion.h>
38 #include <platform/PersistedStorage.h>
39 #include <support/CHIPMem.h>
40 #include <support/PersistedCounter.h>
41 #include <support/UnitTestRegistration.h>
43 #include "TestPersistedStorageImplementation.h"
45 struct TestPersistedCounterContext
47 TestPersistedCounterContext();
51 TestPersistedCounterContext::TestPersistedCounterContext() : mVerbose(false) {}
53 static void InitializePersistedStorage(TestPersistedCounterContext * context)
55 sPersistentStore.clear();
58 static int TestSetup(void * inContext)
63 static int TestTeardown(void * inContext)
65 sPersistentStore.clear();
69 static void CheckOOB(nlTestSuite * inSuite, void * inContext)
71 TestPersistedCounterContext * context = static_cast<TestPersistedCounterContext *>(inContext);
72 CHIP_ERROR err = CHIP_NO_ERROR;
73 chip::PersistedCounter counter;
74 const char * testKey = "testcounter";
75 char testValue[CHIP_CONFIG_PERSISTED_STORAGE_MAX_VALUE_LENGTH];
78 memset(testValue, 0, sizeof(testValue));
80 InitializePersistedStorage(context);
82 // When initializing the first time out of the box, we should have
83 // a count of 0 and a value of 0x10000 for the next starting value
84 // in persistent storage.
86 err = counter.Init(testKey, 0x10000);
87 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
89 value = counter.GetValue();
90 NL_TEST_ASSERT(inSuite, value == 0);
93 static void CheckReboot(nlTestSuite * inSuite, void * inContext)
95 TestPersistedCounterContext * context = static_cast<TestPersistedCounterContext *>(inContext);
96 CHIP_ERROR err = CHIP_NO_ERROR;
97 chip::PersistedCounter counter, counter2;
98 const char * testKey = "testcounter";
99 char testValue[CHIP_CONFIG_PERSISTED_STORAGE_MAX_VALUE_LENGTH];
102 memset(testValue, 0, sizeof(testValue));
104 InitializePersistedStorage(context);
106 // When initializing the first time out of the box, we should have
109 err = counter.Init(testKey, 0x10000);
110 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
112 value = counter.GetValue();
113 NL_TEST_ASSERT(inSuite, value == 0);
115 // Now we "reboot", and we should get a count of 0x10000.
117 err = counter2.Init(testKey, 0x10000);
118 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
120 value = counter2.GetValue();
121 NL_TEST_ASSERT(inSuite, value == 0x10000);
124 static void CheckWriteNextCounterStart(nlTestSuite * inSuite, void * inContext)
126 TestPersistedCounterContext * context = static_cast<TestPersistedCounterContext *>(inContext);
127 CHIP_ERROR err = CHIP_NO_ERROR;
128 chip::PersistedCounter counter;
129 const char * testKey = "testcounter";
130 char testValue[CHIP_CONFIG_PERSISTED_STORAGE_MAX_VALUE_LENGTH];
133 memset(testValue, 0, sizeof(testValue));
135 InitializePersistedStorage(context);
137 // When initializing the first time out of the box, we should have
140 err = counter.Init(testKey, 0x10000);
141 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
143 value = counter.GetValue();
144 NL_TEST_ASSERT(inSuite, value == 0);
146 // Verify that we write out the next starting counter value after
147 // we've exhausted the counter's range.
149 for (int32_t i = 0; i < 0x10000; i++)
151 err = counter.Advance();
152 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
155 value = counter.GetValue();
156 NL_TEST_ASSERT(inSuite, value == 0x10000);
158 for (int32_t i = 0; i < 0x10000; i++)
160 err = counter.Advance();
161 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
164 value = counter.GetValue();
165 NL_TEST_ASSERT(inSuite, value == 0x20000);
171 * Test Suite that lists all the test functions.
173 static const nlTest sTests[] = {
174 NL_TEST_DEF("Out of box Test", CheckOOB), //
175 NL_TEST_DEF("Reboot Test", CheckReboot), //
176 NL_TEST_DEF("Write Next Counter Start Test", CheckWriteNextCounterStart), //
177 NL_TEST_SENTINEL() //
180 int TestPersistedCounter()
182 TestPersistedCounterContext context;
184 CHIP_ERROR error = chip::Platform::MemoryInit();
185 if (error != CHIP_NO_ERROR)
190 nlTestSuite theSuite = { "chip-persisted-storage", &sTests[0], TestSetup, TestTeardown };
192 // Run test suite against one context
193 nlTestRunner(&theSuite, &context);
195 int r = nlTestRunnerStats(&theSuite);
197 chip::Platform::MemoryShutdown();
202 CHIP_REGISTER_TEST_SUITE(TestPersistedCounter);