Skip to content

Commit ff18a52

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 22dcc5f commit ff18a52

File tree

8 files changed

+205
-23
lines changed

8 files changed

+205
-23
lines changed

.astyleignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
^TESTS/mbed_hal/trng/pithy
3232
^TESTS/mbed_hal/trng/pithy
3333
^tools
34+
^UNITTESTS

UNITTESTS/features/storage/blockdevice/HeapBlockDevice/test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "gtest/gtest.h"
1818
#include "features/storage/blockdevice/HeapBlockDevice.h"
19-
#include "string.h"
19+
#include <string.h>
2020
#include "mbed_assert.h"
2121

2222
#define BLOCK_SIZE (512)
@@ -67,7 +67,7 @@ TEST_F(HeapBlockDeviceTest, get_type)
6767

6868
TEST_F(HeapBlockDeviceTest, erase_program_read)
6969
{
70-
uint8_t *block = new uint8_t[BLOCK_SIZE]{0xaa,0xbb,0xcc};
70+
uint8_t *block = new uint8_t[BLOCK_SIZE] {0xaa,0xbb,0xcc};
7171
uint8_t *buf = new uint8_t[BLOCK_SIZE];
7272
EXPECT_EQ(bd.erase(0, BLOCK_SIZE), BD_ERROR_OK);
7373
EXPECT_EQ(bd.program(block, 0, BLOCK_SIZE), BD_ERROR_OK);
@@ -77,7 +77,7 @@ TEST_F(HeapBlockDeviceTest, erase_program_read)
7777
delete[] buf;
7878
}
7979

80-
TEST_F(HeapBlockDeviceTest, use_uninitalized)
80+
TEST_F(HeapBlockDeviceTest, use_uninitialized)
8181
{
8282
mbed::HeapBlockDevice one{DEVICE_SIZE};
8383
uint8_t *buf = new uint8_t[BLOCK_SIZE];
@@ -95,7 +95,7 @@ TEST_F(HeapBlockDeviceTest, over_read)
9595

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

UNITTESTS/stubs/mbed_atomic_stub.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,33 @@ uint32_t core_util_atomic_exchange_u32(volatile uint32_t *ptr, uint32_t desiredV
5757

5858
uint8_t core_util_atomic_incr_u8(volatile uint8_t *valuePtr, uint8_t delta)
5959
{
60-
return *valuePtr+=delta;
60+
return *valuePtr += delta;
6161
}
6262

6363
uint16_t core_util_atomic_incr_u16(volatile uint16_t *valuePtr, uint16_t delta)
6464
{
65-
return *valuePtr+=delta;
65+
return *valuePtr += delta;
6666
}
6767

6868
uint32_t core_util_atomic_incr_u32(volatile uint32_t *valuePtr, uint32_t delta)
6969
{
70-
return *valuePtr+=delta;
70+
return *valuePtr += delta;
7171
}
7272

7373

7474
uint8_t core_util_atomic_decr_u8(volatile uint8_t *valuePtr, uint8_t delta)
7575
{
76-
return *valuePtr-=delta;
76+
return *valuePtr -= delta;
7777
}
7878

7979
uint16_t core_util_atomic_decr_u16(volatile uint16_t *valuePtr, uint16_t delta)
8080
{
81-
return *valuePtr-=delta;
81+
return *valuePtr -= delta;
8282
}
8383

8484
uint32_t core_util_atomic_decr_u32(volatile uint32_t *valuePtr, uint32_t delta)
8585
{
86-
return *valuePtr-=delta;
86+
return *valuePtr -= delta;
8787
}
8888

8989

UNITTESTS/target_h/PinNames.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ typedef enum {
3434
} PinName;
3535

3636
typedef enum {
37-
PullNone,
38-
PullUp,
39-
PullDown
37+
PullNone,
38+
PullUp,
39+
PullDown
4040
} PinMode;
4141

4242
typedef enum {

UNITTESTS/target_h/gpio_object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern "C" {
2727
#endif
2828

2929
typedef struct {
30-
int unused;
30+
int unused;
3131
} gpio_t;
3232

3333
#ifdef __cplusplus

UNITTESTS/target_h/objects.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,39 @@ struct serial_s {
3434
};
3535

3636
struct dac_s {
37-
int unused;
37+
int unused;
3838
};
3939

4040
struct i2c_s {
41-
int unused;
41+
int unused;
4242
};
4343

4444
struct qspi_s {
45-
int unused;
45+
int unused;
4646
};
4747

4848
struct spi_s {
49-
int unused;
49+
int unused;
5050
};
5151

5252
struct analogin_s {
53-
int unused;
53+
int unused;
5454
};
5555

5656
struct port_s {
57-
int unused;
57+
int unused;
5858
};
5959

6060
struct pwmout_s {
61-
int unused;
61+
int unused;
6262
};
6363

6464
struct flash_s {
65-
int unused;
65+
int unused;
6666
};
6767

6868
struct can_s {
69-
int unused;
69+
int unused;
7070
};
7171

7272
#include "gpio_object.h"

0 commit comments

Comments
 (0)