Skip to content

Commit 35410a9

Browse files
author
Seppo Takalo
committed
Add module tests for TDBStore
1 parent 94bb831 commit 35410a9

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/* Copyright (c) 2019 ARM Limited
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "gtest/gtest.h"
18+
#include "features/storage/blockdevice/HeapBlockDevice.h"
19+
#include "features/storage/blockdevice/FlashSimBlockDevice.h"
20+
#include "features/storage/kvstore/tdbstore/TDBStore.h"
21+
#include <stdlib.h>
22+
23+
#define BLOCK_SIZE (512)
24+
#define DEVICE_SIZE (BLOCK_SIZE*200)
25+
26+
using namespace mbed;
27+
28+
class TDBStoreModuleTest : public testing::Test {
29+
protected:
30+
HeapBlockDevice heap{DEVICE_SIZE};
31+
FlashSimBlockDevice flash{&heap};
32+
TDBStore tdb{&flash};
33+
34+
virtual void SetUp()
35+
{
36+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
37+
EXPECT_EQ(tdb.reset(), MBED_SUCCESS);
38+
}
39+
40+
virtual void TearDown()
41+
{
42+
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
43+
}
44+
};
45+
46+
TEST_F(TDBStoreModuleTest, init)
47+
{
48+
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
49+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
50+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
51+
}
52+
53+
TEST_F(TDBStoreModuleTest, set_get)
54+
{
55+
char buf[100];
56+
size_t size;
57+
EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS);
58+
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
59+
EXPECT_EQ(size, 5);
60+
EXPECT_STREQ("data", buf);
61+
}
62+
63+
TEST_F(TDBStoreModuleTest, erased_get_set)
64+
{
65+
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
66+
EXPECT_EQ(flash.init(), MBED_SUCCESS);
67+
EXPECT_EQ(flash.erase(0, flash.size()), MBED_SUCCESS);
68+
EXPECT_EQ(flash.deinit(), MBED_SUCCESS);
69+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
70+
char buf[100];
71+
size_t size;
72+
EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS);
73+
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
74+
EXPECT_EQ(size, 5);
75+
EXPECT_STREQ("data", buf);
76+
}
77+
78+
TEST_F(TDBStoreModuleTest, set_deinit_init_get)
79+
{
80+
char buf[100];
81+
size_t size;
82+
for (int i = 0; i < 100; ++i) {
83+
EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS);
84+
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
85+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
86+
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
87+
EXPECT_EQ(size, 5);
88+
EXPECT_STREQ("data", buf);
89+
EXPECT_EQ(tdb.remove("key"), MBED_SUCCESS);
90+
}
91+
}
92+
93+
TEST_F(TDBStoreModuleTest, corrupted_set_deinit_init_get)
94+
{
95+
char buf[100];
96+
char *block = new char[BLOCK_SIZE];
97+
size_t size;
98+
EXPECT_EQ(heap.init(), MBED_SUCCESS); // Extra init, so the heap will not be deinitialized
99+
100+
srand(0); // Prefer to have always the same pattern
101+
102+
for (int i = 0; i < 100; ++i) {
103+
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
104+
// Corrupt the first part of the storage
105+
for (int i = 0; i < heap.size()/BLOCK_SIZE/2; i++) {
106+
for (int j = 0; j < BLOCK_SIZE; j++) {
107+
block[j] = rand();
108+
}
109+
EXPECT_EQ(heap.program(block, BLOCK_SIZE * i, BLOCK_SIZE), MBED_SUCCESS);
110+
}
111+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
112+
for (int j = 0; j < 100; ++j) {
113+
// Use random data, so the data has to be updated
114+
EXPECT_EQ(tdb.set("key", block+j, 50, 0), MBED_SUCCESS);
115+
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
116+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
117+
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
118+
EXPECT_EQ(size, 50);
119+
EXPECT_EQ(0, memcmp(buf, block+j, size));
120+
}
121+
EXPECT_EQ(tdb.remove("key"), MBED_SUCCESS);
122+
}
123+
124+
EXPECT_EQ(heap.deinit(), MBED_SUCCESS);
125+
delete[] block;
126+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
6+
set(unittest-includes ${unittest-includes}
7+
.
8+
..
9+
../features/frameworks/mbed-trace/mbed-trace
10+
)
11+
12+
set(unittest-sources
13+
../features/storage/blockdevice/FlashSimBlockDevice.cpp
14+
../features/storage/blockdevice/HeapBlockDevice.cpp
15+
../features/storage/blockdevice/BufferedBlockDevice.cpp
16+
../features/storage/kvstore/tdbstore/TDBStore.cpp
17+
../features/frameworks/mbed-trace/source/mbed_trace.c
18+
stubs/mbed_atomic_stub.c
19+
stubs/mbed_assert_stub.cpp
20+
stubs/mbed_error.c
21+
)
22+
23+
set(unittest-test-sources
24+
moduletests/storage/kvstore/TDBStore/moduletest.cpp
25+
)
26+
27+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBYPASS_NVSTORE_CHECK")

0 commit comments

Comments
 (0)