Skip to content

Commit 1ec9cc3

Browse files
author
Seppo Takalo
committed
Add moduletest for SlicingBlockDevice
This uses HeapBlockDevice for providing the underlying storage block. Check boundaries that slicingblockdevice do not overlow over to unassigned blocks.
1 parent cc14b3a commit 1ec9cc3

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include "gtest/gtest.h"
2+
#include "features/storage/blockdevice/HeapBlockDevice.h"
3+
#include "features/storage/blockdevice/SlicingBlockDevice.h"
4+
5+
#define BLOCK_SIZE (512)
6+
#define DEVICE_SIZE (BLOCK_SIZE*10)
7+
8+
class VerifyBorders_HeapBlockDevice : public mbed::HeapBlockDevice {
9+
public:
10+
mutable bool borders_crossed;
11+
mutable bd_size_t lower_limit;
12+
mutable bd_size_t upper_limit;
13+
14+
VerifyBorders_HeapBlockDevice(bd_size_t size)
15+
: HeapBlockDevice(size) {
16+
borders_crossed = false;
17+
lower_limit = 0;
18+
upper_limit = size;
19+
}
20+
21+
virtual bool is_valid_read(bd_addr_t addr, bd_size_t size) const
22+
{
23+
borders_crossed |= addr < lower_limit;
24+
borders_crossed |= addr+size > upper_limit;
25+
return BlockDevice::is_valid_read(addr, size);
26+
}
27+
28+
virtual bool is_valid_program(bd_addr_t addr, bd_size_t size) const
29+
{
30+
borders_crossed |= addr < lower_limit;
31+
borders_crossed |= addr+size > upper_limit;
32+
return BlockDevice::is_valid_program(addr, size);
33+
}
34+
35+
virtual bool is_valid_erase(bd_addr_t addr, bd_size_t size) const
36+
{
37+
borders_crossed |= addr < lower_limit;
38+
borders_crossed |= addr+size > upper_limit;
39+
return BlockDevice::is_valid_erase(addr, size);
40+
}
41+
};
42+
43+
class SlicingBlockModuleTest : public testing::Test {
44+
protected:
45+
VerifyBorders_HeapBlockDevice bd{DEVICE_SIZE};
46+
uint8_t *magic;
47+
uint8_t *buf;
48+
virtual void SetUp()
49+
{
50+
bd.init();
51+
magic = new uint8_t[BLOCK_SIZE];
52+
buf = new uint8_t[BLOCK_SIZE];
53+
// Generate simple pattern to verify against
54+
for (int i=0; i < BLOCK_SIZE; i++) {
55+
magic[i] = 0xaa + i;
56+
}
57+
}
58+
59+
virtual void TearDown()
60+
{
61+
bd.deinit();
62+
delete[] magic;
63+
delete[] buf;
64+
}
65+
};
66+
67+
TEST_F(SlicingBlockModuleTest, constructor)
68+
{
69+
mbed::SlicingBlockDevice slice(&bd, 0, bd.size());
70+
EXPECT_EQ(slice.init(), BD_ERROR_OK);
71+
EXPECT_EQ(slice.get_read_size(), bd.get_read_size());
72+
EXPECT_EQ(slice.get_program_size(), bd.get_read_size());
73+
EXPECT_EQ(slice.get_erase_size(), bd.get_read_size());
74+
EXPECT_EQ(slice.get_erase_size(0), bd.get_read_size());
75+
EXPECT_EQ(slice.deinit(), BD_ERROR_OK);
76+
}
77+
78+
TEST_F(SlicingBlockModuleTest, slice_in_middle)
79+
{
80+
uint8_t *program = new uint8_t[BLOCK_SIZE]{0xbb,0xbb,0xbb};
81+
82+
//Write magic value to heap block before and after the space for slice
83+
bd.program(magic, 0, BLOCK_SIZE);
84+
bd.program(magic, BLOCK_SIZE*3, BLOCK_SIZE);
85+
86+
bd.upper_limit = BLOCK_SIZE*3;
87+
bd.lower_limit = BLOCK_SIZE;
88+
bd.borders_crossed = false;
89+
90+
//Skip first block, then create sclicing device, with size of 2 blocks
91+
mbed::SlicingBlockDevice slice(&bd, BLOCK_SIZE, BLOCK_SIZE*3);
92+
EXPECT_EQ(slice.init(), BD_ERROR_OK);
93+
EXPECT_EQ(BLOCK_SIZE*2, slice.size());
94+
EXPECT_EQ(bd.borders_crossed, false);
95+
96+
//Program a test value
97+
EXPECT_EQ(slice.program(program, 0, BLOCK_SIZE), BD_ERROR_OK);
98+
EXPECT_EQ(slice.program(program, BLOCK_SIZE, BLOCK_SIZE), BD_ERROR_OK);
99+
EXPECT_EQ(bd.borders_crossed, false);
100+
101+
//Verify that blocks before and after the slicing blocks are not touched
102+
bd.read(buf, 0, BLOCK_SIZE);
103+
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
104+
bd.read(buf, BLOCK_SIZE*3, BLOCK_SIZE);
105+
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
106+
}
107+
108+
TEST_F(SlicingBlockModuleTest, slice_at_the_end)
109+
{
110+
uint8_t *program = new uint8_t[BLOCK_SIZE]{0xbb,0xbb,0xbb};
111+
112+
//Write magic value to heap block before the space for slice
113+
// our bd is 10*BLOCK_SIZE, so sector 7
114+
bd.program(magic, BLOCK_SIZE*7, BLOCK_SIZE);
115+
116+
//Screate sclicing device, with size of 2 blocks
117+
// Use negative index
118+
mbed::SlicingBlockDevice slice(&bd, -BLOCK_SIZE*2);
119+
EXPECT_EQ(slice.init(), BD_ERROR_OK);
120+
EXPECT_EQ(BLOCK_SIZE*2, slice.size());
121+
122+
//Program a test value
123+
EXPECT_EQ(slice.program(program, 0, BLOCK_SIZE), BD_ERROR_OK);
124+
EXPECT_EQ(slice.program(program, BLOCK_SIZE, BLOCK_SIZE), BD_ERROR_OK);
125+
126+
//Verify that blocks before and after the slicing blocks are not touched
127+
bd.read(buf, BLOCK_SIZE*7, BLOCK_SIZE);
128+
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
129+
}
130+
131+
TEST_F(SlicingBlockModuleTest, over_write)
132+
{
133+
uint8_t *program = new uint8_t[BLOCK_SIZE]{0xbb,0xbb,0xbb};
134+
135+
//Screate sclicing device, with size of 2 blocks
136+
mbed::SlicingBlockDevice slice(&bd, BLOCK_SIZE, BLOCK_SIZE*3);
137+
EXPECT_EQ(slice.init(), BD_ERROR_OK);
138+
139+
EXPECT_EQ(slice.program(program, 0, BLOCK_SIZE), BD_ERROR_OK);
140+
EXPECT_EQ(slice.program(program, BLOCK_SIZE, BLOCK_SIZE), BD_ERROR_OK);
141+
//Program a test value to address that is one pass the device size
142+
EXPECT_EQ(slice.program(program, 2*BLOCK_SIZE, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
143+
144+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
6+
set(unittest-includes ${unittest-includes}
7+
.
8+
..
9+
)
10+
11+
set(unittest-sources
12+
../features/storage/blockdevice/SlicingBlockDevice.cpp
13+
../features/storage/blockdevice/HeapBlockDevice.cpp
14+
stubs/mbed_atomic_stub.c
15+
stubs/mbed_assert_stub.c
16+
)
17+
18+
set(unittest-test-sources
19+
moduletests/storage/blockdevice/SlicingBlockDevice/moduletest.cpp
20+
)

0 commit comments

Comments
 (0)