init

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "greentea-client/test_env.h"
00018 #include "unity.h"
00019 #include "utest.h"
00020 
00021 #include "HeapBlockDevice.h"
00022 #include "MBRBlockDevice.h"
00023 #include <stdlib.h>
00024 
00025 using namespace utest::v1;
00026 
00027 // TODO HACK, replace with available ram/heap property
00028 #if defined(TARGET_MTB_MTS_XDOT)
00029     #error [NOT_SUPPORTED] Insufficient heap for heap block device tests
00030 #endif
00031 
00032 #define BLOCK_COUNT 16
00033 #define BLOCK_SIZE 512
00034 
00035 HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
00036 
00037 // Testing formatting of master boot record
00038 void test_mbr_format()
00039 {
00040     // Create two partitions splitting device in ~half
00041     int err = MBRBlockDevice::partition(&bd, 1, 0x83, 0, (BLOCK_COUNT/2)*BLOCK_SIZE);
00042     TEST_ASSERT_EQUAL(0, err);
00043 
00044     err = MBRBlockDevice::partition(&bd, 2, 0x83, -(BLOCK_COUNT/2)*BLOCK_SIZE);
00045     TEST_ASSERT_EQUAL(0, err);
00046 
00047     // Load both partitions, as well as a third to check for invalid partitions
00048     MBRBlockDevice part1(&bd, 1);
00049     err = part1.init();
00050     TEST_ASSERT_EQUAL(0, err);
00051 
00052     MBRBlockDevice part2(&bd, 2);
00053     err = part2.init();
00054     TEST_ASSERT_EQUAL(0, err);
00055 
00056     MBRBlockDevice part3(&bd, 3);
00057     err = part3.init();
00058     TEST_ASSERT_EQUAL(BD_ERROR_INVALID_PARTITION, err);
00059 
00060     // Deinit partitions
00061     err = part1.deinit();
00062     TEST_ASSERT_EQUAL(0, err);
00063 
00064     err = part2.deinit();
00065     TEST_ASSERT_EQUAL(0, err);
00066 }
00067 
00068 // Testing mbr attributes
00069 void test_mbr_attr()
00070 {
00071     // Load partitions
00072     MBRBlockDevice part1(&bd, 1);
00073     int err = part1.init();
00074     TEST_ASSERT_EQUAL(0, err);
00075 
00076     MBRBlockDevice part2(&bd, 2);
00077     err = part2.init();
00078     TEST_ASSERT_EQUAL(0, err);
00079 
00080     // Test attributes on partitions
00081     printf("partition 1 partition number: %d\n",        part1.get_partition_number());
00082     printf("partition 1 partition start: 0x%llx\n",     part1.get_partition_start());
00083     printf("partition 1 partition stop: 0x%llx\n",      part1.get_partition_stop());
00084     printf("partition 1 partition type: 0x%02x\n",      part1.get_partition_type());
00085     printf("partition 1 read size: %llu bytes\n",       part1.get_read_size());
00086     printf("partition 1 program size: %llu bytes\n",    part1.get_program_size());
00087     printf("partition 1 erase size: %llu bytes\n",      part1.get_erase_size());
00088     printf("partition 1 size: %llu bytes\n",            part1.size());
00089     TEST_ASSERT_EQUAL(1,                                part1.get_partition_number());
00090     TEST_ASSERT_EQUAL(1*BLOCK_SIZE,                     part1.get_partition_start());
00091     TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE,       part1.get_partition_stop());
00092     TEST_ASSERT_EQUAL(0x83,                             part1.get_partition_type());
00093     TEST_ASSERT_EQUAL(BLOCK_SIZE,                       part1.get_read_size());
00094     TEST_ASSERT_EQUAL(BLOCK_SIZE,                       part1.get_program_size());
00095     TEST_ASSERT_EQUAL(BLOCK_SIZE,                       part1.get_erase_size());
00096     TEST_ASSERT_EQUAL(((BLOCK_COUNT/2)-1)*BLOCK_SIZE,   part1.size());
00097 
00098     printf("partition 2 partition number: %d\n",        part2.get_partition_number());
00099     printf("partition 2 partition start: 0x%llx\n",     part2.get_partition_start());
00100     printf("partition 2 partition stop: 0x%llx\n",      part2.get_partition_stop());
00101     printf("partition 2 partition type: 0x%02x\n",      part2.get_partition_type());
00102     printf("partition 2 read size: %llu bytes\n",       part2.get_read_size());
00103     printf("partition 2 program size: %llu bytes\n",    part2.get_program_size());
00104     printf("partition 2 erase size: %llu bytes\n",      part2.get_erase_size());
00105     printf("partition 2 size: %llu bytes\n",            part2.size());
00106     TEST_ASSERT_EQUAL(2,                                part2.get_partition_number());
00107     TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE,       part2.get_partition_start());
00108     TEST_ASSERT_EQUAL(BLOCK_COUNT*BLOCK_SIZE,           part2.get_partition_stop());
00109     TEST_ASSERT_EQUAL(0x83,                             part2.get_partition_type());
00110     TEST_ASSERT_EQUAL(BLOCK_SIZE,                       part2.get_read_size());
00111     TEST_ASSERT_EQUAL(BLOCK_SIZE,                       part2.get_program_size());
00112     TEST_ASSERT_EQUAL(BLOCK_SIZE,                       part2.get_erase_size());
00113     TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE,       part2.size());
00114 
00115     // Deinit partitions
00116     err = part1.deinit();
00117     TEST_ASSERT_EQUAL(0, err);
00118 
00119     err = part2.deinit();
00120     TEST_ASSERT_EQUAL(0, err);
00121 }
00122 
00123 // Testing mbr read write
00124 void test_mbr_read_write()
00125 {
00126     // Load partitions
00127     MBRBlockDevice part1(&bd, 1);
00128     int err = part1.init();
00129     TEST_ASSERT_EQUAL(0, err);
00130 
00131     MBRBlockDevice part2(&bd, 2);
00132     err = part2.init();
00133     TEST_ASSERT_EQUAL(0, err);
00134 
00135     // Test reading/writing the partitions
00136     uint8_t *write_block = new uint8_t[BLOCK_SIZE];
00137     uint8_t *read_block = new uint8_t[BLOCK_SIZE];
00138 
00139     // Fill with random sequence
00140     srand(1);
00141     for (int i = 0; i < BLOCK_SIZE; i++) {
00142         write_block[i] = 0xff & rand();
00143     }
00144 
00145     // Write, sync, and read the block
00146     err = part1.erase(0, BLOCK_SIZE);
00147     TEST_ASSERT_EQUAL(0, err);
00148 
00149     err = part1.program(write_block, 0, BLOCK_SIZE);
00150     TEST_ASSERT_EQUAL(0, err);
00151 
00152     err = part1.read(read_block, 0, BLOCK_SIZE);
00153     TEST_ASSERT_EQUAL(0, err);
00154 
00155     // Check that the data was unmodified
00156     srand(1);
00157     for (int i = 0; i < BLOCK_SIZE; i++) {
00158         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00159     }
00160 
00161     // Check with original block device
00162     err = bd.read(read_block, 1*BLOCK_SIZE, BLOCK_SIZE);
00163     TEST_ASSERT_EQUAL(0, err);
00164 
00165     // Check that the data was unmodified
00166     srand(1);
00167     for (int i = 0; i < BLOCK_SIZE; i++) {
00168         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00169     }
00170 
00171     // Test with second slice of block device
00172     srand(1);
00173     for (int i = 0; i < BLOCK_SIZE; i++) {
00174         write_block[i] = 0xff & rand();
00175     }
00176 
00177     // Write, sync, and read the block
00178     err = part2.erase(0, BLOCK_SIZE);
00179     TEST_ASSERT_EQUAL(0, err);
00180 
00181     err = part2.program(write_block, 0, BLOCK_SIZE);
00182     TEST_ASSERT_EQUAL(0, err);
00183 
00184     err = part2.read(read_block, 0, BLOCK_SIZE);
00185     TEST_ASSERT_EQUAL(0, err);
00186 
00187     // Check that the data was unmodified
00188     srand(1);
00189     for (int i = 0; i < BLOCK_SIZE; i++) {
00190         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00191     }
00192 
00193     // Check with original block device
00194     err = bd.read(read_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE);
00195     TEST_ASSERT_EQUAL(0, err);
00196 
00197     // Check that the data was unmodified
00198     srand(1);
00199     for (int i = 0; i < BLOCK_SIZE; i++) {
00200         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00201     }
00202 
00203     // Clean up
00204     delete[] write_block;
00205     delete[] read_block;
00206 
00207     err = part1.deinit();
00208     TEST_ASSERT_EQUAL(0, err);
00209 
00210     err = part2.deinit();
00211     TEST_ASSERT_EQUAL(0, err);
00212 }
00213 
00214 
00215 // Test setup
00216 utest::v1::status_t test_setup(const size_t number_of_cases) {
00217     GREENTEA_SETUP(10, "default_auto");
00218     return verbose_test_setup_handler(number_of_cases);
00219 }
00220 
00221 Case cases[] = {
00222     Case("Testing formatting of master boot record", test_mbr_format),
00223     Case("Testing mbr attributes", test_mbr_attr),
00224     Case("Testing mbr read write", test_mbr_read_write),
00225 };
00226 
00227 Specification specification(test_setup, cases);
00228 
00229 int main() {
00230     return !Harness::run(specification);
00231 }