Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / tests / TestPersistedCounter.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
5  *    All rights reserved.
6  *
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 /**
21  *    @file
22  *      Unit tests for the Chip Persisted Storage API.
23  *
24  */
25
26 #ifndef __STDC_FORMAT_MACROS
27 #define __STDC_FORMAT_MACROS
28 #endif
29
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include <nlunit-test.h>
36
37 #include <CHIPVersion.h>
38 #include <platform/PersistedStorage.h>
39 #include <support/CHIPMem.h>
40 #include <support/PersistedCounter.h>
41 #include <support/UnitTestRegistration.h>
42
43 #include "TestPersistedStorageImplementation.h"
44
45 struct TestPersistedCounterContext
46 {
47     TestPersistedCounterContext();
48     bool mVerbose;
49 };
50
51 TestPersistedCounterContext::TestPersistedCounterContext() : mVerbose(false) {}
52
53 static void InitializePersistedStorage(TestPersistedCounterContext * context)
54 {
55     sPersistentStore.clear();
56 }
57
58 static int TestSetup(void * inContext)
59 {
60     return SUCCESS;
61 }
62
63 static int TestTeardown(void * inContext)
64 {
65     sPersistentStore.clear();
66     return SUCCESS;
67 }
68
69 static void CheckOOB(nlTestSuite * inSuite, void * inContext)
70 {
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];
76     uint64_t value = 0;
77
78     memset(testValue, 0, sizeof(testValue));
79
80     InitializePersistedStorage(context);
81
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.
85
86     err = counter.Init(testKey, 0x10000);
87     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
88
89     value = counter.GetValue();
90     NL_TEST_ASSERT(inSuite, value == 0);
91 }
92
93 static void CheckReboot(nlTestSuite * inSuite, void * inContext)
94 {
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];
100     uint64_t value = 0;
101
102     memset(testValue, 0, sizeof(testValue));
103
104     InitializePersistedStorage(context);
105
106     // When initializing the first time out of the box, we should have
107     // a count of 0.
108
109     err = counter.Init(testKey, 0x10000);
110     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
111
112     value = counter.GetValue();
113     NL_TEST_ASSERT(inSuite, value == 0);
114
115     // Now we "reboot", and we should get a count of 0x10000.
116
117     err = counter2.Init(testKey, 0x10000);
118     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
119
120     value = counter2.GetValue();
121     NL_TEST_ASSERT(inSuite, value == 0x10000);
122 }
123
124 static void CheckWriteNextCounterStart(nlTestSuite * inSuite, void * inContext)
125 {
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];
131     uint64_t value = 0;
132
133     memset(testValue, 0, sizeof(testValue));
134
135     InitializePersistedStorage(context);
136
137     // When initializing the first time out of the box, we should have
138     // a count of 0.
139
140     err = counter.Init(testKey, 0x10000);
141     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
142
143     value = counter.GetValue();
144     NL_TEST_ASSERT(inSuite, value == 0);
145
146     // Verify that we write out the next starting counter value after
147     // we've exhausted the counter's range.
148
149     for (int32_t i = 0; i < 0x10000; i++)
150     {
151         err = counter.Advance();
152         NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
153     }
154
155     value = counter.GetValue();
156     NL_TEST_ASSERT(inSuite, value == 0x10000);
157
158     for (int32_t i = 0; i < 0x10000; i++)
159     {
160         err = counter.Advance();
161         NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
162     }
163
164     value = counter.GetValue();
165     NL_TEST_ASSERT(inSuite, value == 0x20000);
166 }
167
168 // Test Suite
169
170 /**
171  *  Test Suite that lists all the test functions.
172  */
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()                                                        //
178 };
179
180 int TestPersistedCounter()
181 {
182     TestPersistedCounterContext context;
183
184     CHIP_ERROR error = chip::Platform::MemoryInit();
185     if (error != CHIP_NO_ERROR)
186     {
187         return EXIT_FAILURE;
188     }
189
190     nlTestSuite theSuite = { "chip-persisted-storage", &sTests[0], TestSetup, TestTeardown };
191
192     // Run test suite against one context
193     nlTestRunner(&theSuite, &context);
194
195     int r = nlTestRunnerStats(&theSuite);
196
197     chip::Platform::MemoryShutdown();
198
199     return r;
200 }
201
202 CHIP_REGISTER_TEST_SUITE(TestPersistedCounter);