Skip to content

Commit 2083995

Browse files
author
Seppo Takalo
committed
Add module tests for TDBStore
Also add option for developer to use broken disk-image on TDBStore module tests. This disk-image was used for verifying IOTSTOR-978 bug.
1 parent fb53687 commit 2083995

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Binary file not shown.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
#include "stubs/EmulatedSD.h"
23+
24+
#define BLOCK_SIZE (512)
25+
#define DEVICE_SIZE (BLOCK_SIZE*20)
26+
27+
using namespace mbed;
28+
// For testing purposes, block device should keep its data between test runs
29+
// to ensure that we can cope with dirty-images
30+
HeapBlockDevice sd{DEVICE_SIZE};
31+
32+
// For debugging purpose, we could use file to store the BlockDevice content
33+
//EmulatedSD sd{"../UNITTESTS/moduletests/storage/kvstore/TDBStore/broken_image.img"};
34+
35+
class TDBStoreModuleTest : public testing::Test {
36+
protected:
37+
FlashSimBlockDevice flash{&sd};
38+
TDBStore tdb{&flash};
39+
40+
virtual void SetUp()
41+
{
42+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
43+
EXPECT_EQ(tdb.reset(), MBED_SUCCESS);
44+
}
45+
46+
virtual void TearDown()
47+
{
48+
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
49+
}
50+
};
51+
52+
TEST_F(TDBStoreModuleTest, init)
53+
{
54+
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
55+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
56+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
57+
}
58+
59+
TEST_F(TDBStoreModuleTest, set_get)
60+
{
61+
char buf[100];
62+
size_t size;
63+
EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS);
64+
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
65+
EXPECT_EQ(size, 5);
66+
EXPECT_STREQ("data", buf);
67+
}
68+
69+
TEST_F(TDBStoreModuleTest, reset)
70+
{
71+
char buf[100];
72+
size_t size, read;
73+
// Write so much, that we are sure that garbage collection have kicked up
74+
for (int i = 0; i < 100; ++i) {
75+
size = sprintf(buf, "data%d", i);
76+
EXPECT_EQ(tdb.set("key", buf, size, 0), MBED_SUCCESS);
77+
}
78+
EXPECT_EQ(tdb.reset(), MBED_SUCCESS);
79+
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
80+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
81+
// After reset->deinit()->init() there should be no data
82+
EXPECT_NE(tdb.get("key", buf, 100, &read), MBED_SUCCESS);
83+
}
84+
85+
TEST_F(TDBStoreModuleTest, set_deinit_init_get)
86+
{
87+
char buf[100];
88+
size_t size;
89+
for (int i = 0; i < 10; ++i) {
90+
EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS);
91+
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
92+
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
93+
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
94+
EXPECT_EQ(size, 5);
95+
EXPECT_STREQ("data", buf);
96+
EXPECT_EQ(tdb.remove("key"), MBED_SUCCESS);
97+
}
98+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.c
20+
stubs/mbed_error.c
21+
stubs/EmulatedSD.cpp
22+
)
23+
24+
set(unittest-test-sources
25+
moduletests/storage/kvstore/TDBStore/moduletest.cpp
26+
)
27+
28+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBYPASS_NVSTORE_CHECK")

0 commit comments

Comments
 (0)