Skip to content

storage: Add util classes for composing block devices #3569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
80 changes: 80 additions & 0 deletions features/TESTS/filesystem/fat_file_system/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"

#include "HeapBlockDevice.h"
#include "FATFileSystem.h"
#include <stdlib.h>

using namespace utest::v1;

// Test block device
#define BLOCK_SIZE 512
HeapBlockDevice bd(BLOCK_SIZE, 128);


void test_format() {
int err = FATFileSystem::format(&bd);
TEST_ASSERT_EQUAL(0, err);
}

template <ssize_t TEST_SIZE>
void test_read_write() {
FATFileSystem fs("fat");

int err = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, err);

uint8_t *buffer = (uint8_t *)malloc(TEST_SIZE);
TEST_ASSERT(buffer);

// Fill with random sequence
srand(1);
for (int i = 0; i < TEST_SIZE; i++) {
buffer[i] = 0xff & rand();
}

// write and read file
FileHandle *file = fs.open("test_read_write.dat", O_WRONLY | O_CREAT);
TEST_ASSERT(file);
ssize_t size = file->write(buffer, TEST_SIZE);
TEST_ASSERT_EQUAL(TEST_SIZE, size);
err = file->close();
TEST_ASSERT_EQUAL(0, err);

file = fs.open("test_read_write.dat", O_RDONLY);
TEST_ASSERT(file);
size = file->read(buffer, TEST_SIZE);
TEST_ASSERT_EQUAL(TEST_SIZE, size);
err = file->close();
TEST_ASSERT_EQUAL(0, err);

// Check that the data was unmodified
srand(1);
for (int i = 0; i < TEST_SIZE; i++) {
TEST_ASSERT_EQUAL(0xff & rand(), buffer[i]);
}

err = fs.unmount();
TEST_ASSERT_EQUAL(0, err);
}


// Test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(10, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("Testing formating", test_format),
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>),
};

Specification specification(test_setup, cases);

int main() {
return !Harness::run(specification);
}
63 changes: 63 additions & 0 deletions features/TESTS/filesystem/heap_block_device/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"

#include "HeapBlockDevice.h"
#include <stdlib.h>

using namespace utest::v1;

#define BLOCK_SIZE 512
uint8_t write_block[BLOCK_SIZE];
uint8_t read_block[BLOCK_SIZE];


void test_read_write() {
HeapBlockDevice bd(BLOCK_SIZE, 16);

int err = bd.init();
TEST_ASSERT_EQUAL(0, err);

// Fill with random sequence
srand(1);
for (int i = 0; i < BLOCK_SIZE; i++) {
write_block[i] = 0xff & rand();
}

// Write, sync, and read the block
err = bd.write(write_block, 0, 1);
TEST_ASSERT_EQUAL(1, err);

err = bd.sync();
TEST_ASSERT_EQUAL(0, err);

err = bd.read(read_block, 0, 1);
TEST_ASSERT_EQUAL(1, err);

// Check that the data was unmodified
srand(1);
for (int i = 0; i < BLOCK_SIZE; i++) {
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
}

err = bd.deinit();
TEST_ASSERT_EQUAL(0, err);
}


// Test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(10, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("Testing read write of a block", test_read_write),
};

Specification specification(test_setup, cases);

int main() {
return !Harness::run(specification);
}
Loading