|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 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 | +#include "mbed.h" |
| 17 | +#include "greentea-client/test_env.h" |
| 18 | +#include "unity.h" |
| 19 | +#include "utest.h" |
| 20 | + |
| 21 | +#include "HeapBlockDevice.h" |
| 22 | +#include "SlicingBlockDevice.h" |
| 23 | +#include "ChainingBlockDevice.h" |
| 24 | +#include <stdlib.h> |
| 25 | + |
| 26 | +using namespace utest::v1; |
| 27 | + |
| 28 | +/* It is not possible to build a KL25Z image with IAR including the file system if |
| 29 | + * stack tracking statistics are enabled. If this is the case, build dummy |
| 30 | + * tests. |
| 31 | + */ |
| 32 | +#if ! defined(TOOLCHAIN_IAR) && ! defined(TARGET_KL25Z) && ! defined(MBED_STACK_STATS_ENABLED) |
| 33 | + |
| 34 | +#define BLOCK_COUNT 16 |
| 35 | +#define BLOCK_SIZE 512 |
| 36 | +#define UTIL_BLOCK_DEVICE_TEST_01 test_slicing |
| 37 | +#define UTIL_BLOCK_DEVICE_TEST_02 test_chaining |
| 38 | +uint8_t write_block[BLOCK_SIZE]; |
| 39 | +uint8_t read_block[BLOCK_SIZE]; |
| 40 | + |
| 41 | + |
| 42 | +// Simple test which read/writes blocks on a sliced block device |
| 43 | +void test_slicing() { |
| 44 | + HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE); |
| 45 | + |
| 46 | + // Test with first slice of block device |
| 47 | + SlicingBlockDevice slice1(&bd, 0, (BLOCK_COUNT/2)*BLOCK_SIZE); |
| 48 | + |
| 49 | + int err = slice1.init(); |
| 50 | + TEST_ASSERT_EQUAL(0, err); |
| 51 | + |
| 52 | + TEST_ASSERT_EQUAL(BLOCK_SIZE, slice1.get_program_size()); |
| 53 | + TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, slice1.size()); |
| 54 | + |
| 55 | + // Fill with random sequence |
| 56 | + srand(1); |
| 57 | + for (int i = 0; i < BLOCK_SIZE; i++) { |
| 58 | + write_block[i] = 0xff & rand(); |
| 59 | + } |
| 60 | + |
| 61 | + // Write, sync, and read the block |
| 62 | + err = slice1.program(write_block, 0, BLOCK_SIZE); |
| 63 | + TEST_ASSERT_EQUAL(0, err); |
| 64 | + |
| 65 | + err = slice1.read(read_block, 0, BLOCK_SIZE); |
| 66 | + TEST_ASSERT_EQUAL(0, err); |
| 67 | + |
| 68 | + // Check that the data was unmodified |
| 69 | + srand(1); |
| 70 | + for (int i = 0; i < BLOCK_SIZE; i++) { |
| 71 | + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); |
| 72 | + } |
| 73 | + |
| 74 | + // Check with original block device |
| 75 | + err = bd.read(read_block, 0, BLOCK_SIZE); |
| 76 | + TEST_ASSERT_EQUAL(0, err); |
| 77 | + |
| 78 | + // Check that the data was unmodified |
| 79 | + srand(1); |
| 80 | + for (int i = 0; i < BLOCK_SIZE; i++) { |
| 81 | + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); |
| 82 | + } |
| 83 | + |
| 84 | + err = slice1.deinit(); |
| 85 | + TEST_ASSERT_EQUAL(0, err); |
| 86 | + |
| 87 | + |
| 88 | + // Test with second slice of block device |
| 89 | + SlicingBlockDevice slice2(&bd, -(BLOCK_COUNT/2)*BLOCK_SIZE); |
| 90 | + |
| 91 | + err = slice2.init(); |
| 92 | + TEST_ASSERT_EQUAL(0, err); |
| 93 | + |
| 94 | + TEST_ASSERT_EQUAL(BLOCK_SIZE, slice2.get_program_size()); |
| 95 | + TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, slice2.size()); |
| 96 | + |
| 97 | + // Fill with random sequence |
| 98 | + srand(1); |
| 99 | + for (int i = 0; i < BLOCK_SIZE; i++) { |
| 100 | + write_block[i] = 0xff & rand(); |
| 101 | + } |
| 102 | + |
| 103 | + // Write, sync, and read the block |
| 104 | + err = slice2.program(write_block, 0, BLOCK_SIZE); |
| 105 | + TEST_ASSERT_EQUAL(0, err); |
| 106 | + |
| 107 | + err = slice2.read(read_block, 0, BLOCK_SIZE); |
| 108 | + TEST_ASSERT_EQUAL(0, err); |
| 109 | + |
| 110 | + // Check that the data was unmodified |
| 111 | + srand(1); |
| 112 | + for (int i = 0; i < BLOCK_SIZE; i++) { |
| 113 | + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); |
| 114 | + } |
| 115 | + |
| 116 | + // Check with original block device |
| 117 | + err = bd.read(read_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); |
| 118 | + TEST_ASSERT_EQUAL(0, err); |
| 119 | + |
| 120 | + // Check that the data was unmodified |
| 121 | + srand(1); |
| 122 | + for (int i = 0; i < BLOCK_SIZE; i++) { |
| 123 | + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); |
| 124 | + } |
| 125 | + |
| 126 | + err = slice2.deinit(); |
| 127 | + TEST_ASSERT_EQUAL(0, err); |
| 128 | +} |
| 129 | + |
| 130 | +// Simple test which read/writes blocks on a chain of block devices |
| 131 | +void test_chaining() { |
| 132 | + HeapBlockDevice bd1((BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); |
| 133 | + HeapBlockDevice bd2((BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); |
| 134 | + |
| 135 | + // Test with chain of block device |
| 136 | + BlockDevice *bds[] = {&bd1, &bd2}; |
| 137 | + ChainingBlockDevice chain(bds); |
| 138 | + |
| 139 | + int err = chain.init(); |
| 140 | + TEST_ASSERT_EQUAL(0, err); |
| 141 | + |
| 142 | + TEST_ASSERT_EQUAL(BLOCK_SIZE, chain.get_program_size()); |
| 143 | + TEST_ASSERT_EQUAL(BLOCK_COUNT*BLOCK_SIZE, chain.size()); |
| 144 | + |
| 145 | + // Fill with random sequence |
| 146 | + srand(1); |
| 147 | + for (int i = 0; i < BLOCK_SIZE; i++) { |
| 148 | + write_block[i] = 0xff & rand(); |
| 149 | + } |
| 150 | + |
| 151 | + // Write, sync, and read the block |
| 152 | + err = chain.program(write_block, 0, BLOCK_SIZE); |
| 153 | + TEST_ASSERT_EQUAL(0, err); |
| 154 | + |
| 155 | + err = chain.read(read_block, 0, BLOCK_SIZE); |
| 156 | + TEST_ASSERT_EQUAL(0, err); |
| 157 | + |
| 158 | + // Check that the data was unmodified |
| 159 | + srand(1); |
| 160 | + for (int i = 0; i < BLOCK_SIZE; i++) { |
| 161 | + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); |
| 162 | + } |
| 163 | + |
| 164 | + // Write, sync, and read the block |
| 165 | + err = chain.program(write_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); |
| 166 | + TEST_ASSERT_EQUAL(0, err); |
| 167 | + |
| 168 | + err = chain.read(read_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); |
| 169 | + TEST_ASSERT_EQUAL(0, err); |
| 170 | + |
| 171 | + // Check that the data was unmodified |
| 172 | + srand(1); |
| 173 | + for (int i = 0; i < BLOCK_SIZE; i++) { |
| 174 | + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); |
| 175 | + } |
| 176 | + |
| 177 | + err = chain.deinit(); |
| 178 | + TEST_ASSERT_EQUAL(0, err); |
| 179 | +} |
| 180 | + |
| 181 | +#else /* ! defined(TOOLCHAIN_IAR) && ! defined(TARGET_KL25Z) && ! defined(MBED_STACK_STATS_ENABLED) */ |
| 182 | + |
| 183 | +#define UTIL_BLOCK_DEVICE_TEST_01 util_block_device_test_dummy |
| 184 | +#define UTIL_BLOCK_DEVICE_TEST_02 util_block_device_test_dummy |
| 185 | + |
| 186 | +/** @brief util_block_device_test_dummy Dummy test case for testing when KL25Z being built with stack statistics enabled. |
| 187 | + * |
| 188 | + * @return success always |
| 189 | + */ |
| 190 | +static control_t util_block_device_test_dummy() |
| 191 | +{ |
| 192 | + printf("Null test\n"); |
| 193 | + return CaseNext; |
| 194 | +} |
| 195 | + |
| 196 | +#endif /* ! defined(TOOLCHAIN_IAR) && ! defined(TARGET_KL25Z) && ! defined(MBED_STACK_STATS_ENABLED) */ |
| 197 | + |
| 198 | +// Test setup |
| 199 | +utest::v1::status_t test_setup(const size_t number_of_cases) { |
| 200 | + GREENTEA_SETUP(10, "default_auto"); |
| 201 | + return verbose_test_setup_handler(number_of_cases); |
| 202 | +} |
| 203 | + |
| 204 | +Case cases[] = { |
| 205 | + Case("Testing slicing of a block device", UTIL_BLOCK_DEVICE_TEST_01), |
| 206 | + Case("Testing chaining of block devices", UTIL_BLOCK_DEVICE_TEST_02), |
| 207 | +}; |
| 208 | + |
| 209 | +Specification specification(test_setup, cases); |
| 210 | + |
| 211 | +int main() { |
| 212 | + return !Harness::run(specification); |
| 213 | +} |
0 commit comments