|
| 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 | +} |
0 commit comments