Converted tests into mbed-style integration tests

- TESTS/filesystem for mbed OS filesystem APIs
- TESTS/filesystem_retarget for mbed OS retargeted stdlib APIs

converted:
- test_dirs
- test_files
- test_seek
- test_parallel
pull/5538/head
Christopher Haster 2017-09-22 14:48:32 -05:00
parent 3a987334f6
commit 9408f2ba2c
20 changed files with 4493 additions and 0 deletions

View File

@ -1,2 +1,3 @@
littlefs/emubd/
littlefs/tests/
TESTS/util

View File

@ -6,6 +6,12 @@ script:
- PYTHONPATH=mbed-os python mbed-os/tools/make.py -t GCC_ARM -m K82F
--source=. --build=BUILD/K82F/GCC_ARM -j0
# Check that tests compile
- rm -rf main.cpp BUILD
- PYTHONPATH=mbed-os python mbed-os/tools/test.py -t GCC_ARM -m K82F
--source=. --build=BUILD/TESTS/K82F/GCC_ARM -j0
-n 'tests*'
# Run littlefs functional tests
- CFLAGS="-Wno-error=format" make -Clittlefs test

View File

@ -0,0 +1,658 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>
#include <errno.h>
using namespace utest::v1;
// test configuration
#ifndef MBED_TEST_FILESYSTEM
#define MBED_TEST_FILESYSTEM LittleFileSystem
#endif
#ifndef MBED_TEST_FILESYSTEM_DECL
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
#endif
#ifndef MBED_TEST_BLOCKDEVICE
#define MBED_TEST_BLOCKDEVICE SPIFBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5)
#endif
#ifndef MBED_TEST_BLOCKDEVICE_DECL
#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
#endif
#ifndef MBED_TEST_FILES
#define MBED_TEST_FILES 4
#endif
#ifndef MBED_TEST_DIRS
#define MBED_TEST_DIRS 4
#endif
#ifndef MBED_TEST_BUFFER
#define MBED_TEST_BUFFER 8192
#endif
#ifndef MBED_TEST_TIMEOUT
#define MBED_TEST_TIMEOUT 120
#endif
// declarations
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define INCLUDE(x) STRINGIZE(x.h)
#include INCLUDE(MBED_TEST_FILESYSTEM)
#include INCLUDE(MBED_TEST_BLOCKDEVICE)
MBED_TEST_FILESYSTEM_DECL;
MBED_TEST_BLOCKDEVICE_DECL;
Dir dir[MBED_TEST_DIRS];
File file[MBED_TEST_FILES];
DIR *dd[MBED_TEST_DIRS];
FILE *fd[MBED_TEST_FILES];
struct dirent ent;
struct dirent *ed;
size_t size;
uint8_t buffer[MBED_TEST_BUFFER];
uint8_t rbuffer[MBED_TEST_BUFFER];
uint8_t wbuffer[MBED_TEST_BUFFER];
// tests
void test_directory_tests() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = MBED_TEST_FILESYSTEM::format(&bd);
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_root_directory() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "/");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_directory_creation() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("potato", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_file_creation() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "burito", O_CREAT | O_WRONLY);
TEST_ASSERT_EQUAL(0, res);
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_directory_iteration() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "/");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "potato");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "burito");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_directory_failures() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("potato", 0777);
TEST_ASSERT_EQUAL(-EEXIST, res);
res = dir[0].open(&fs, "tomato");
TEST_ASSERT_EQUAL(-ENOENT, res);
res = dir[0].open(&fs, "burito");
TEST_ASSERT_EQUAL(-ENOTDIR, res);
res = file[0].open(&fs, "tomato", O_RDONLY);
TEST_ASSERT_EQUAL(-ENOENT, res);
res = file[0].open(&fs, "potato", O_RDONLY);
TEST_ASSERT_EQUAL(-EISDIR, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_nested_directories() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("potato/baked", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("potato/sweet", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("potato/fried", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "potato");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "baked");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "sweet");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "fried");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_multi_block_directory() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("cactus", 0777);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 128; i++) {
sprintf((char*)buffer, "cactus/test%d", i);
res = fs.mkdir((char*)buffer, 0777);
TEST_ASSERT_EQUAL(0, res);
}
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "cactus");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
for (int i = 0; i < 128; i++) {
sprintf((char*)buffer, "test%d", i);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
}
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_directory_remove() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.remove("potato");
TEST_ASSERT_EQUAL(-EINVAL, res);
res = fs.remove("potato/sweet");
TEST_ASSERT_EQUAL(0, res);
res = fs.remove("potato/baked");
TEST_ASSERT_EQUAL(0, res);
res = fs.remove("potato/fried");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "potato");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.remove("potato");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "/");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "burito");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "cactus");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "/");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "burito");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "cactus");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_directory_rename() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("coldpotato", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("coldpotato/baked", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("coldpotato/sweet", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("coldpotato/fried", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.rename("coldpotato", "hotpotato");
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "hotpotato");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "baked");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "sweet");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "fried");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("warmpotato", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("warmpotato/mushy", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.rename("hotpotato", "warmpotato");
TEST_ASSERT_EQUAL(-EINVAL, res);
res = fs.remove("warmpotato/mushy");
TEST_ASSERT_EQUAL(0, res);
res = fs.rename("hotpotato", "warmpotato");
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "warmpotato");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "baked");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "sweet");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "fried");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("coldpotato", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.rename("warmpotato/baked", "coldpotato/baked");
TEST_ASSERT_EQUAL(0, res);
res = fs.rename("warmpotato/sweet", "coldpotato/sweet");
TEST_ASSERT_EQUAL(0, res);
res = fs.rename("warmpotato/fried", "coldpotato/fried");
TEST_ASSERT_EQUAL(0, res);
res = fs.remove("coldpotato");
TEST_ASSERT_EQUAL(-EINVAL, res);
res = fs.remove("warmpotato");
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "coldpotato");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "baked");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "sweet");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "fried");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
// test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("Directory tests", test_directory_tests),
Case("Root directory", test_root_directory),
Case("Directory creation", test_directory_creation),
Case("File creation", test_file_creation),
Case("Directory iteration", test_directory_iteration),
Case("Directory failures", test_directory_failures),
Case("Nested directories", test_nested_directories),
Case("Multi-block directory", test_multi_block_directory),
Case("Directory remove", test_directory_remove),
Case("Directory rename", test_directory_rename),
};
Specification specification(test_setup, cases);
int main() {
return !Harness::run(specification);
}

View File

@ -0,0 +1,426 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>
#include <errno.h>
using namespace utest::v1;
// test configuration
#ifndef MBED_TEST_FILESYSTEM
#define MBED_TEST_FILESYSTEM LittleFileSystem
#endif
#ifndef MBED_TEST_FILESYSTEM_DECL
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
#endif
#ifndef MBED_TEST_BLOCKDEVICE
#define MBED_TEST_BLOCKDEVICE SPIFBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5)
#endif
#ifndef MBED_TEST_BLOCKDEVICE_DECL
#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
#endif
#ifndef MBED_TEST_FILES
#define MBED_TEST_FILES 4
#endif
#ifndef MBED_TEST_DIRS
#define MBED_TEST_DIRS 4
#endif
#ifndef MBED_TEST_BUFFER
#define MBED_TEST_BUFFER 8192
#endif
#ifndef MBED_TEST_TIMEOUT
#define MBED_TEST_TIMEOUT 120
#endif
// declarations
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define INCLUDE(x) STRINGIZE(x.h)
#include INCLUDE(MBED_TEST_FILESYSTEM)
#include INCLUDE(MBED_TEST_BLOCKDEVICE)
MBED_TEST_FILESYSTEM_DECL;
MBED_TEST_BLOCKDEVICE_DECL;
Dir dir[MBED_TEST_DIRS];
File file[MBED_TEST_FILES];
DIR *dd[MBED_TEST_DIRS];
FILE *fd[MBED_TEST_FILES];
struct dirent ent;
struct dirent *ed;
size_t size;
uint8_t buffer[MBED_TEST_BUFFER];
uint8_t rbuffer[MBED_TEST_BUFFER];
uint8_t wbuffer[MBED_TEST_BUFFER];
// tests
void test_file_tests() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = MBED_TEST_FILESYSTEM::format(&bd);
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_simple_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "hello", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
size = strlen("Hello World!\n");
memcpy(wbuffer, "Hello World!\n", size);
res = file[0].write(wbuffer, size);
TEST_ASSERT_EQUAL(size, res);
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "hello", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
size = strlen("Hello World!\n");
res = file[0].read(rbuffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(rbuffer, wbuffer, size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_small_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
size_t size = 32;
size_t chunk = 31;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "smallavacado", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
for (size_t b = 0; b < chunk; b++) {
buffer[b] = rand() & 0xff;
}
res = file[0].write(buffer, chunk);
TEST_ASSERT_EQUAL(chunk, res);
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
size_t size = 32;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "smallavacado", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = file[0].read(buffer, chunk);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_medium_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
size_t size = 8192;
size_t chunk = 31;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "mediumavacado", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
for (size_t b = 0; b < chunk; b++) {
buffer[b] = rand() & 0xff;
}
res = file[0].write(buffer, chunk);
TEST_ASSERT_EQUAL(chunk, res);
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
size_t size = 8192;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "mediumavacado", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = file[0].read(buffer, chunk);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_large_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
size_t size = 262144;
size_t chunk = 31;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "largeavacado", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
for (size_t b = 0; b < chunk; b++) {
buffer[b] = rand() & 0xff;
}
res = file[0].write(buffer, chunk);
TEST_ASSERT_EQUAL(chunk, res);
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
size_t size = 262144;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "largeavacado", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = file[0].read(buffer, chunk);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_non_overlap_check() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
size_t size = 32;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "smallavacado", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = file[0].read(buffer, chunk);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
size_t size = 8192;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "mediumavacado", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = file[0].read(buffer, chunk);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
size_t size = 262144;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "largeavacado", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = file[0].read(buffer, chunk);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_dir_check() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "/");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "hello");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "smallavacado");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "mediumavacado");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "largeavacado");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
// test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("File tests", test_file_tests),
Case("Simple file test", test_simple_file_test),
Case("Small file test", test_small_file_test),
Case("Medium file test", test_medium_file_test),
Case("Large file test", test_large_file_test),
Case("Non-overlap check", test_non_overlap_check),
Case("Dir check", test_dir_check),
};
Specification specification(test_setup, cases);
int main() {
return !Harness::run(specification);
}

View File

@ -0,0 +1,387 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>
#include <errno.h>
using namespace utest::v1;
// test configuration
#ifndef MBED_TEST_FILESYSTEM
#define MBED_TEST_FILESYSTEM LittleFileSystem
#endif
#ifndef MBED_TEST_FILESYSTEM_DECL
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
#endif
#ifndef MBED_TEST_BLOCKDEVICE
#define MBED_TEST_BLOCKDEVICE SPIFBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5)
#endif
#ifndef MBED_TEST_BLOCKDEVICE_DECL
#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
#endif
#ifndef MBED_TEST_FILES
#define MBED_TEST_FILES 4
#endif
#ifndef MBED_TEST_DIRS
#define MBED_TEST_DIRS 4
#endif
#ifndef MBED_TEST_BUFFER
#define MBED_TEST_BUFFER 8192
#endif
#ifndef MBED_TEST_TIMEOUT
#define MBED_TEST_TIMEOUT 120
#endif
// declarations
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define INCLUDE(x) STRINGIZE(x.h)
#include INCLUDE(MBED_TEST_FILESYSTEM)
#include INCLUDE(MBED_TEST_BLOCKDEVICE)
MBED_TEST_FILESYSTEM_DECL;
MBED_TEST_BLOCKDEVICE_DECL;
Dir dir[MBED_TEST_DIRS];
File file[MBED_TEST_FILES];
DIR *dd[MBED_TEST_DIRS];
FILE *fd[MBED_TEST_FILES];
struct dirent ent;
struct dirent *ed;
size_t size;
uint8_t buffer[MBED_TEST_BUFFER];
uint8_t rbuffer[MBED_TEST_BUFFER];
uint8_t wbuffer[MBED_TEST_BUFFER];
// tests
void test_parallel_tests() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = MBED_TEST_FILESYSTEM::format(&bd);
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_parallel_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "a", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
res = file[1].open(&fs, "b", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
res = file[2].open(&fs, "c", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
res = file[3].open(&fs, "d", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 10; i++) {
res = file[0].write((const void*)"a", 1);
TEST_ASSERT_EQUAL(1, res);
res = file[1].write((const void*)"b", 1);
TEST_ASSERT_EQUAL(1, res);
res = file[2].write((const void*)"c", 1);
TEST_ASSERT_EQUAL(1, res);
res = file[3].write((const void*)"d", 1);
TEST_ASSERT_EQUAL(1, res);
}
file[0].close();
file[1].close();
file[2].close();
file[3].close();
res = dir[0].open(&fs, "/");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "a");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "b");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "c");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "d");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "a", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
res = file[1].open(&fs, "b", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
res = file[2].open(&fs, "c", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
res = file[3].open(&fs, "d", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 10; i++) {
res = file[0].read(buffer, 1);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('a', res);
res = file[1].read(buffer, 1);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('b', res);
res = file[2].read(buffer, 1);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('c', res);
res = file[3].read(buffer, 1);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('d', res);
}
file[0].close();
file[1].close();
file[2].close();
file[3].close();
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_parallel_remove_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "e", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 5; i++) {
res = file[0].write((const void*)"e", 1);
TEST_ASSERT_EQUAL(1, res);
}
res = fs.remove("a");
TEST_ASSERT_EQUAL(0, res);
res = fs.remove("b");
TEST_ASSERT_EQUAL(0, res);
res = fs.remove("c");
TEST_ASSERT_EQUAL(0, res);
res = fs.remove("d");
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 5; i++) {
res = file[0].write((const void*)"e", 1);
TEST_ASSERT_EQUAL(1, res);
}
file[0].close();
res = dir[0].open(&fs, "/");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "e");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "e", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 10; i++) {
res = file[0].read(buffer, 1);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('e', res);
}
file[0].close();
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_remove_inconveniently_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "e", O_WRONLY | O_TRUNC);
TEST_ASSERT_EQUAL(0, res);
res = file[1].open(&fs, "f", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
res = file[2].open(&fs, "g", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 5; i++) {
res = file[0].write((const void*)"e", 1);
TEST_ASSERT_EQUAL(1, res);
res = file[1].write((const void*)"f", 1);
TEST_ASSERT_EQUAL(1, res);
res = file[2].write((const void*)"g", 1);
TEST_ASSERT_EQUAL(1, res);
}
res = fs.remove("f");
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 5; i++) {
res = file[0].write((const void*)"e", 1);
TEST_ASSERT_EQUAL(1, res);
res = file[1].write((const void*)"f", 1);
TEST_ASSERT_EQUAL(1, res);
res = file[2].write((const void*)"g", 1);
TEST_ASSERT_EQUAL(1, res);
}
file[0].close();
file[1].close();
file[2].close();
res = dir[0].open(&fs, "/");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "e");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "g");
TEST_ASSERT_EQUAL(0, res);
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "e", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
res = file[1].open(&fs, "g", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 10; i++) {
res = file[0].read(buffer, 1);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('e', res);
res = file[1].read(buffer, 1);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('g', res);
}
file[0].close();
file[1].close();
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
// test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("Parallel tests", test_parallel_tests),
Case("Parallel file test", test_parallel_file_test),
Case("Parallel remove file test", test_parallel_remove_file_test),
Case("Remove inconveniently test", test_remove_inconveniently_test),
};
Specification specification(test_setup, cases);
int main() {
return !Harness::run(specification);
}

View File

@ -0,0 +1,618 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>
#include <errno.h>
using namespace utest::v1;
// test configuration
#ifndef MBED_TEST_FILESYSTEM
#define MBED_TEST_FILESYSTEM LittleFileSystem
#endif
#ifndef MBED_TEST_FILESYSTEM_DECL
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
#endif
#ifndef MBED_TEST_BLOCKDEVICE
#define MBED_TEST_BLOCKDEVICE SPIFBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5)
#endif
#ifndef MBED_TEST_BLOCKDEVICE_DECL
#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
#endif
#ifndef MBED_TEST_FILES
#define MBED_TEST_FILES 4
#endif
#ifndef MBED_TEST_DIRS
#define MBED_TEST_DIRS 4
#endif
#ifndef MBED_TEST_BUFFER
#define MBED_TEST_BUFFER 8192
#endif
#ifndef MBED_TEST_TIMEOUT
#define MBED_TEST_TIMEOUT 120
#endif
// declarations
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define INCLUDE(x) STRINGIZE(x.h)
#include INCLUDE(MBED_TEST_FILESYSTEM)
#include INCLUDE(MBED_TEST_BLOCKDEVICE)
MBED_TEST_FILESYSTEM_DECL;
MBED_TEST_BLOCKDEVICE_DECL;
Dir dir[MBED_TEST_DIRS];
File file[MBED_TEST_FILES];
DIR *dd[MBED_TEST_DIRS];
FILE *fd[MBED_TEST_FILES];
struct dirent ent;
struct dirent *ed;
size_t size;
uint8_t buffer[MBED_TEST_BUFFER];
uint8_t rbuffer[MBED_TEST_BUFFER];
uint8_t wbuffer[MBED_TEST_BUFFER];
// tests
void test_seek_tests() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = MBED_TEST_FILESYSTEM::format(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.mkdir("hello", 0777);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 132; i++) {
sprintf((char*)buffer, "hello/kitty%d", i);
res = file[0].open(&fs, (char*)buffer,
O_WRONLY | O_CREAT | O_APPEND);
TEST_ASSERT_EQUAL(0, res);
size = strlen("kittycatcat");
memcpy(buffer, "kittycatcat", size);
for (int j = 0; j < 132; j++) {
file[0].write(buffer, size);
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
}
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_simple_dir_seek() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "hello");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
off_t pos;
int i;
for (i = 0; i < 4; i++) {
sprintf((char*)buffer, "kitty%d", i);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
pos = dir[0].tell();
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
dir[0].seek(pos);
sprintf((char*)buffer, "kitty%d", i);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
dir[0].rewind();
sprintf((char*)buffer, "kitty%d", 0);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
dir[0].seek(pos);
sprintf((char*)buffer, "kitty%d", i);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_large_dir_seek() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].open(&fs, "hello");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
off_t pos;
int i;
for (i = 0; i < 128; i++) {
sprintf((char*)buffer, "kitty%d", i);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
pos = dir[0].tell();
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
dir[0].seek(pos);
sprintf((char*)buffer, "kitty%d", i);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
dir[0].rewind();
sprintf((char*)buffer, "kitty%d", 0);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
dir[0].seek(pos);
sprintf((char*)buffer, "kitty%d", i);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
res = dir[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_simple_file_seek() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "hello/kitty42", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
off_t pos;
size = strlen("kittycatcat");
for (int i = 0; i < 4; i++) {
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
pos = file[0].tell();
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
res = file[0].seek(pos, SEEK_SET);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
file[0].rewind();
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(pos, SEEK_SET);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(-size, SEEK_CUR);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(-size, SEEK_END) >= 0;
TEST_ASSERT_EQUAL(1, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
size_t size = file[0].size();
res = file[0].seek(0, SEEK_CUR);
TEST_ASSERT_EQUAL(size, res);
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_large_file_seek() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "hello/kitty42", O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
off_t pos;
size = strlen("kittycatcat");
for (int i = 0; i < 128; i++) {
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
pos = file[0].tell();
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
res = file[0].seek(pos, SEEK_SET);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
file[0].rewind();
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(pos, SEEK_SET);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(-size, SEEK_CUR);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(-size, SEEK_END) >= 0;
TEST_ASSERT_EQUAL(1, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
size_t size = file[0].size();
res = file[0].seek(0, SEEK_CUR);
TEST_ASSERT_EQUAL(size, res);
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_simple_file_seek_and_write() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "hello/kitty42", O_RDWR);
TEST_ASSERT_EQUAL(0, res);
off_t pos;
size = strlen("kittycatcat");
for (int i = 0; i < 4; i++) {
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
pos = file[0].tell();
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
memcpy(buffer, "doggodogdog", size);
res = file[0].seek(pos, SEEK_SET);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].write(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = file[0].seek(pos, SEEK_SET);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "doggodogdog", size);
TEST_ASSERT_EQUAL(0, res);
file[0].rewind();
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(pos, SEEK_SET);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "doggodogdog", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(-size, SEEK_END) >= 0;
TEST_ASSERT_EQUAL(1, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
size_t size = file[0].size();
res = file[0].seek(0, SEEK_CUR);
TEST_ASSERT_EQUAL(size, res);
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_large_file_seek_and_write() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "hello/kitty42", O_RDWR);
TEST_ASSERT_EQUAL(0, res);
off_t pos;
size = strlen("kittycatcat");
for (int i = 0; i < 128; i++) {
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
if (i != 4) {
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
}
pos = file[0].tell();
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
memcpy(buffer, "doggodogdog", size);
res = file[0].seek(pos, SEEK_SET);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].write(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = file[0].seek(pos, SEEK_SET);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "doggodogdog", size);
TEST_ASSERT_EQUAL(0, res);
file[0].rewind();
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(pos, SEEK_SET);
TEST_ASSERT_EQUAL(pos, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "doggodogdog", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(-size, SEEK_END) >= 0;
TEST_ASSERT_EQUAL(1, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
size_t size = file[0].size();
res = file[0].seek(0, SEEK_CUR);
TEST_ASSERT_EQUAL(size, res);
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_boundary_seek_and_write() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "hello/kitty42", O_RDWR);
TEST_ASSERT_EQUAL(0, res);
size = strlen("hedgehoghog");
const off_t offsets[] = {512, 1020, 513, 1021, 511, 1019};
for (int i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) {
off_t off = offsets[i];
memcpy(buffer, "hedgehoghog", size);
res = file[0].seek(off, SEEK_SET);
TEST_ASSERT_EQUAL(off, res);
res = file[0].write(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = file[0].seek(off, SEEK_SET);
TEST_ASSERT_EQUAL(off, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "hedgehoghog", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(0, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].sync();
TEST_ASSERT_EQUAL(0, res);
}
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_out_of_bounds_seek() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, "hello/kitty42", O_RDWR);
TEST_ASSERT_EQUAL(0, res);
size = strlen("kittycatcat");
res = file[0].size();
TEST_ASSERT_EQUAL(132*size, res);
res = file[0].seek((132+4)*size,
SEEK_SET);
TEST_ASSERT_EQUAL((132+4)*size, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(0, res);
memcpy(buffer, "porcupineee", size);
res = file[0].write(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = file[0].seek((132+4)*size,
SEEK_SET);
TEST_ASSERT_EQUAL((132+4)*size, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "porcupineee", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].seek(132*size,
SEEK_SET);
TEST_ASSERT_EQUAL(132*size, res);
res = file[0].read(buffer, size);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "\0\0\0\0\0\0\0\0\0\0\0", size);
TEST_ASSERT_EQUAL(0, res);
res = file[0].close();
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
// test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("Seek tests", test_seek_tests),
Case("Simple dir seek", test_simple_dir_seek),
Case("Large dir seek", test_large_dir_seek),
Case("Simple file seek", test_simple_file_seek),
Case("Large file seek", test_large_file_seek),
Case("Simple file seek and write", test_simple_file_seek_and_write),
Case("Large file seek and write", test_large_file_seek_and_write),
Case("Boundary seek and write", test_boundary_seek_and_write),
Case("Out-of-bounds seek", test_out_of_bounds_seek),
};
Specification specification(test_setup, cases);
int main() {
return !Harness::run(specification);
}

View File

@ -0,0 +1,658 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>
#include <errno.h>
using namespace utest::v1;
// test configuration
#ifndef MBED_TEST_FILESYSTEM
#define MBED_TEST_FILESYSTEM LittleFileSystem
#endif
#ifndef MBED_TEST_FILESYSTEM_DECL
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
#endif
#ifndef MBED_TEST_BLOCKDEVICE
#define MBED_TEST_BLOCKDEVICE SPIFBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5)
#endif
#ifndef MBED_TEST_BLOCKDEVICE_DECL
#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
#endif
#ifndef MBED_TEST_FILES
#define MBED_TEST_FILES 4
#endif
#ifndef MBED_TEST_DIRS
#define MBED_TEST_DIRS 4
#endif
#ifndef MBED_TEST_BUFFER
#define MBED_TEST_BUFFER 8192
#endif
#ifndef MBED_TEST_TIMEOUT
#define MBED_TEST_TIMEOUT 120
#endif
// declarations
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define INCLUDE(x) STRINGIZE(x.h)
#include INCLUDE(MBED_TEST_FILESYSTEM)
#include INCLUDE(MBED_TEST_BLOCKDEVICE)
MBED_TEST_FILESYSTEM_DECL;
MBED_TEST_BLOCKDEVICE_DECL;
Dir dir[MBED_TEST_DIRS];
File file[MBED_TEST_FILES];
DIR *dd[MBED_TEST_DIRS];
FILE *fd[MBED_TEST_FILES];
struct dirent ent;
struct dirent *ed;
size_t size;
uint8_t buffer[MBED_TEST_BUFFER];
uint8_t rbuffer[MBED_TEST_BUFFER];
uint8_t wbuffer[MBED_TEST_BUFFER];
// tests
void test_directory_tests() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = MBED_TEST_FILESYSTEM::format(&bd);
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_root_directory() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "/")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_directory_creation() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "potato", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_file_creation() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "burito", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_directory_iteration() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "/")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "potato");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "burito");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_directory_failures() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "potato", 0777);
TEST_ASSERT_EQUAL(EEXIST, errno);
res = !((dd[0] = opendir("/fs/" "tomato")) != NULL);
TEST_ASSERT_EQUAL(ENOENT, errno);
res = !((dd[0] = opendir("/fs/" "burito")) != NULL);
TEST_ASSERT_EQUAL(ENOTDIR, errno);
res = !((fd[0] = fopen("/fs/" "tomato", "rb")) != NULL);
TEST_ASSERT_EQUAL(ENOENT, errno);
res = !((fd[0] = fopen("/fs/" "potato", "rb")) != NULL);
TEST_ASSERT_EQUAL(EISDIR, errno);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_nested_directories() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "potato/baked", 0777);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "potato/sweet", 0777);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "potato/fried", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "potato")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "baked");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "sweet");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "fried");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_multi_block_directory() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "cactus", 0777);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 128; i++) {
sprintf((char*)buffer, "/fs/" "cactus/test%d", i);
res = mkdir((char*)buffer, 0777);
TEST_ASSERT_EQUAL(0, res);
}
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "cactus")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
for (int i = 0; i < 128; i++) {
sprintf((char*)buffer, "test%d", i);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
}
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_directory_remove() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = remove("/fs/" "potato");
TEST_ASSERT_EQUAL(EINVAL, errno);
res = remove("/fs/" "potato/sweet");
TEST_ASSERT_EQUAL(0, res);
res = remove("/fs/" "potato/baked");
TEST_ASSERT_EQUAL(0, res);
res = remove("/fs/" "potato/fried");
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "potato")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = remove("/fs/" "potato");
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "/")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "burito");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "cactus");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "/")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "burito");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "cactus");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_directory_rename() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "coldpotato", 0777);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "coldpotato/baked", 0777);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "coldpotato/sweet", 0777);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "coldpotato/fried", 0777);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = rename("/fs/" "coldpotato", "/fs/" "hotpotato");
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "hotpotato")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "baked");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "sweet");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "fried");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "warmpotato", 0777);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "warmpotato/mushy", 0777);
TEST_ASSERT_EQUAL(0, res);
res = rename("/fs/" "hotpotato", "/fs/" "warmpotato");
TEST_ASSERT_EQUAL(EINVAL, errno);
res = remove("/fs/" "warmpotato/mushy");
TEST_ASSERT_EQUAL(0, res);
res = rename("/fs/" "hotpotato", "/fs/" "warmpotato");
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "warmpotato")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "baked");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "sweet");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "fried");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "coldpotato", 0777);
TEST_ASSERT_EQUAL(0, res);
res = rename("/fs/" "warmpotato/baked", "/fs/" "coldpotato/baked");
TEST_ASSERT_EQUAL(0, res);
res = rename("/fs/" "warmpotato/sweet", "/fs/" "coldpotato/sweet");
TEST_ASSERT_EQUAL(0, res);
res = rename("/fs/" "warmpotato/fried", "/fs/" "coldpotato/fried");
TEST_ASSERT_EQUAL(0, res);
res = remove("/fs/" "coldpotato");
TEST_ASSERT_EQUAL(EINVAL, errno);
res = remove("/fs/" "warmpotato");
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "coldpotato")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "baked");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "sweet");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "fried");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
// test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("Directory tests", test_directory_tests),
Case("Root directory", test_root_directory),
Case("Directory creation", test_directory_creation),
Case("File creation", test_file_creation),
Case("Directory iteration", test_directory_iteration),
Case("Directory failures", test_directory_failures),
Case("Nested directories", test_nested_directories),
Case("Multi-block directory", test_multi_block_directory),
Case("Directory remove", test_directory_remove),
Case("Directory rename", test_directory_rename),
};
Specification specification(test_setup, cases);
int main() {
return !Harness::run(specification);
}

View File

@ -0,0 +1,426 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>
#include <errno.h>
using namespace utest::v1;
// test configuration
#ifndef MBED_TEST_FILESYSTEM
#define MBED_TEST_FILESYSTEM LittleFileSystem
#endif
#ifndef MBED_TEST_FILESYSTEM_DECL
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
#endif
#ifndef MBED_TEST_BLOCKDEVICE
#define MBED_TEST_BLOCKDEVICE SPIFBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5)
#endif
#ifndef MBED_TEST_BLOCKDEVICE_DECL
#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
#endif
#ifndef MBED_TEST_FILES
#define MBED_TEST_FILES 4
#endif
#ifndef MBED_TEST_DIRS
#define MBED_TEST_DIRS 4
#endif
#ifndef MBED_TEST_BUFFER
#define MBED_TEST_BUFFER 8192
#endif
#ifndef MBED_TEST_TIMEOUT
#define MBED_TEST_TIMEOUT 120
#endif
// declarations
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define INCLUDE(x) STRINGIZE(x.h)
#include INCLUDE(MBED_TEST_FILESYSTEM)
#include INCLUDE(MBED_TEST_BLOCKDEVICE)
MBED_TEST_FILESYSTEM_DECL;
MBED_TEST_BLOCKDEVICE_DECL;
Dir dir[MBED_TEST_DIRS];
File file[MBED_TEST_FILES];
DIR *dd[MBED_TEST_DIRS];
FILE *fd[MBED_TEST_FILES];
struct dirent ent;
struct dirent *ed;
size_t size;
uint8_t buffer[MBED_TEST_BUFFER];
uint8_t rbuffer[MBED_TEST_BUFFER];
uint8_t wbuffer[MBED_TEST_BUFFER];
// tests
void test_file_tests() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = MBED_TEST_FILESYSTEM::format(&bd);
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_simple_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "hello", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
size = strlen("Hello World!\n");
memcpy(wbuffer, "Hello World!\n", size);
res = fwrite(wbuffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "hello", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
size = strlen("Hello World!\n");
res = fread(rbuffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(rbuffer, wbuffer, size);
TEST_ASSERT_EQUAL(0, res);
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_small_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
size_t size = 32;
size_t chunk = 31;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "smallavacado", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
for (size_t b = 0; b < chunk; b++) {
buffer[b] = rand() & 0xff;
}
res = fwrite(buffer, 1, chunk, fd[0]);
TEST_ASSERT_EQUAL(chunk, res);
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
size_t size = 32;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "smallavacado", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = fread(buffer, 1, chunk, fd[0]);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_medium_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
size_t size = 8192;
size_t chunk = 31;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "mediumavacado", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
for (size_t b = 0; b < chunk; b++) {
buffer[b] = rand() & 0xff;
}
res = fwrite(buffer, 1, chunk, fd[0]);
TEST_ASSERT_EQUAL(chunk, res);
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
size_t size = 8192;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "mediumavacado", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = fread(buffer, 1, chunk, fd[0]);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_large_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
size_t size = 262144;
size_t chunk = 31;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "largeavacado", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
for (size_t b = 0; b < chunk; b++) {
buffer[b] = rand() & 0xff;
}
res = fwrite(buffer, 1, chunk, fd[0]);
TEST_ASSERT_EQUAL(chunk, res);
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
size_t size = 262144;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "largeavacado", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = fread(buffer, 1, chunk, fd[0]);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_non_overlap_check() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
size_t size = 32;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "smallavacado", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = fread(buffer, 1, chunk, fd[0]);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
size_t size = 8192;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "mediumavacado", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = fread(buffer, 1, chunk, fd[0]);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
{
size_t size = 262144;
size_t chunk = 29;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "largeavacado", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (size_t i = 0; i < size; i += chunk) {
chunk = (chunk < size - i) ? chunk : size - i;
res = fread(buffer, 1, chunk, fd[0]);
TEST_ASSERT_EQUAL(chunk, res);
for (size_t b = 0; b < chunk && i+b < size; b++) {
res = buffer[b];
TEST_ASSERT_EQUAL(rand() & 0xff, res);
}
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_dir_check() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "/")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "hello");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "smallavacado");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "mediumavacado");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "largeavacado");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
// test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("File tests", test_file_tests),
Case("Simple file test", test_simple_file_test),
Case("Small file test", test_small_file_test),
Case("Medium file test", test_medium_file_test),
Case("Large file test", test_large_file_test),
Case("Non-overlap check", test_non_overlap_check),
Case("Dir check", test_dir_check),
};
Specification specification(test_setup, cases);
int main() {
return !Harness::run(specification);
}

View File

@ -0,0 +1,387 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>
#include <errno.h>
using namespace utest::v1;
// test configuration
#ifndef MBED_TEST_FILESYSTEM
#define MBED_TEST_FILESYSTEM LittleFileSystem
#endif
#ifndef MBED_TEST_FILESYSTEM_DECL
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
#endif
#ifndef MBED_TEST_BLOCKDEVICE
#define MBED_TEST_BLOCKDEVICE SPIFBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5)
#endif
#ifndef MBED_TEST_BLOCKDEVICE_DECL
#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
#endif
#ifndef MBED_TEST_FILES
#define MBED_TEST_FILES 4
#endif
#ifndef MBED_TEST_DIRS
#define MBED_TEST_DIRS 4
#endif
#ifndef MBED_TEST_BUFFER
#define MBED_TEST_BUFFER 8192
#endif
#ifndef MBED_TEST_TIMEOUT
#define MBED_TEST_TIMEOUT 120
#endif
// declarations
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define INCLUDE(x) STRINGIZE(x.h)
#include INCLUDE(MBED_TEST_FILESYSTEM)
#include INCLUDE(MBED_TEST_BLOCKDEVICE)
MBED_TEST_FILESYSTEM_DECL;
MBED_TEST_BLOCKDEVICE_DECL;
Dir dir[MBED_TEST_DIRS];
File file[MBED_TEST_FILES];
DIR *dd[MBED_TEST_DIRS];
FILE *fd[MBED_TEST_FILES];
struct dirent ent;
struct dirent *ed;
size_t size;
uint8_t buffer[MBED_TEST_BUFFER];
uint8_t rbuffer[MBED_TEST_BUFFER];
uint8_t wbuffer[MBED_TEST_BUFFER];
// tests
void test_parallel_tests() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = MBED_TEST_FILESYSTEM::format(&bd);
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_parallel_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "a", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[1] = fopen("/fs/" "b", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[2] = fopen("/fs/" "c", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[3] = fopen("/fs/" "d", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 10; i++) {
res = fwrite((const void*)"a", 1, 1, fd[0]);
TEST_ASSERT_EQUAL(1, res);
res = fwrite((const void*)"b", 1, 1, fd[1]);
TEST_ASSERT_EQUAL(1, res);
res = fwrite((const void*)"c", 1, 1, fd[2]);
TEST_ASSERT_EQUAL(1, res);
res = fwrite((const void*)"d", 1, 1, fd[3]);
TEST_ASSERT_EQUAL(1, res);
}
fclose(fd[0]);
fclose(fd[1]);
fclose(fd[2]);
fclose(fd[3]);
res = !((dd[0] = opendir("/fs/" "/")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "a");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "b");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "c");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "d");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "a", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[1] = fopen("/fs/" "b", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[2] = fopen("/fs/" "c", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[3] = fopen("/fs/" "d", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 10; i++) {
res = fread(buffer, 1, 1, fd[0]);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('a', res);
res = fread(buffer, 1, 1, fd[1]);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('b', res);
res = fread(buffer, 1, 1, fd[2]);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('c', res);
res = fread(buffer, 1, 1, fd[3]);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('d', res);
}
fclose(fd[0]);
fclose(fd[1]);
fclose(fd[2]);
fclose(fd[3]);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_parallel_remove_file_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "e", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 5; i++) {
res = fwrite((const void*)"e", 1, 1, fd[0]);
TEST_ASSERT_EQUAL(1, res);
}
res = remove("/fs/" "a");
TEST_ASSERT_EQUAL(0, res);
res = remove("/fs/" "b");
TEST_ASSERT_EQUAL(0, res);
res = remove("/fs/" "c");
TEST_ASSERT_EQUAL(0, res);
res = remove("/fs/" "d");
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 5; i++) {
res = fwrite((const void*)"e", 1, 1, fd[0]);
TEST_ASSERT_EQUAL(1, res);
}
fclose(fd[0]);
res = !((dd[0] = opendir("/fs/" "/")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "e");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "e", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 10; i++) {
res = fread(buffer, 1, 1, fd[0]);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('e', res);
}
fclose(fd[0]);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_remove_inconveniently_test() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "e", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[1] = fopen("/fs/" "f", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[2] = fopen("/fs/" "g", "wb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 5; i++) {
res = fwrite((const void*)"e", 1, 1, fd[0]);
TEST_ASSERT_EQUAL(1, res);
res = fwrite((const void*)"f", 1, 1, fd[1]);
TEST_ASSERT_EQUAL(1, res);
res = fwrite((const void*)"g", 1, 1, fd[2]);
TEST_ASSERT_EQUAL(1, res);
}
res = remove("/fs/" "f");
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 5; i++) {
res = fwrite((const void*)"e", 1, 1, fd[0]);
TEST_ASSERT_EQUAL(1, res);
res = fwrite((const void*)"f", 1, 1, fd[1]);
TEST_ASSERT_EQUAL(1, res);
res = fwrite((const void*)"g", 1, 1, fd[2]);
TEST_ASSERT_EQUAL(1, res);
}
fclose(fd[0]);
fclose(fd[1]);
fclose(fd[2]);
res = !((dd[0] = opendir("/fs/" "/")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "e");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "g");
TEST_ASSERT_EQUAL(0, res);
res = ed->d_type;
TEST_ASSERT_EQUAL(DT_REG, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "e", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[1] = fopen("/fs/" "g", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 10; i++) {
res = fread(buffer, 1, 1, fd[0]);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('e', res);
res = fread(buffer, 1, 1, fd[1]);
TEST_ASSERT_EQUAL(1, res);
res = buffer[0];
TEST_ASSERT_EQUAL('g', res);
}
fclose(fd[0]);
fclose(fd[1]);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
// test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("Parallel tests", test_parallel_tests),
Case("Parallel file test", test_parallel_file_test),
Case("Parallel remove file test", test_parallel_remove_file_test),
Case("Remove inconveniently test", test_remove_inconveniently_test),
};
Specification specification(test_setup, cases);
int main() {
return !Harness::run(specification);
}

View File

@ -0,0 +1,616 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>
#include <errno.h>
using namespace utest::v1;
// test configuration
#ifndef MBED_TEST_FILESYSTEM
#define MBED_TEST_FILESYSTEM LittleFileSystem
#endif
#ifndef MBED_TEST_FILESYSTEM_DECL
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
#endif
#ifndef MBED_TEST_BLOCKDEVICE
#define MBED_TEST_BLOCKDEVICE SPIFBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5)
#endif
#ifndef MBED_TEST_BLOCKDEVICE_DECL
#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
#endif
#ifndef MBED_TEST_FILES
#define MBED_TEST_FILES 4
#endif
#ifndef MBED_TEST_DIRS
#define MBED_TEST_DIRS 4
#endif
#ifndef MBED_TEST_BUFFER
#define MBED_TEST_BUFFER 8192
#endif
#ifndef MBED_TEST_TIMEOUT
#define MBED_TEST_TIMEOUT 120
#endif
// declarations
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define INCLUDE(x) STRINGIZE(x.h)
#include INCLUDE(MBED_TEST_FILESYSTEM)
#include INCLUDE(MBED_TEST_BLOCKDEVICE)
MBED_TEST_FILESYSTEM_DECL;
MBED_TEST_BLOCKDEVICE_DECL;
Dir dir[MBED_TEST_DIRS];
File file[MBED_TEST_FILES];
DIR *dd[MBED_TEST_DIRS];
FILE *fd[MBED_TEST_FILES];
struct dirent ent;
struct dirent *ed;
size_t size;
uint8_t buffer[MBED_TEST_BUFFER];
uint8_t rbuffer[MBED_TEST_BUFFER];
uint8_t wbuffer[MBED_TEST_BUFFER];
// tests
void test_seek_tests() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = MBED_TEST_FILESYSTEM::format(&bd);
TEST_ASSERT_EQUAL(0, res);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = mkdir("/fs/" "hello", 0777);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 132; i++) {
sprintf((char*)buffer, "/fs/" "hello/kitty%d", i);
res = !((fd[0] = fopen((char*)buffer,
"ab")) != NULL);
TEST_ASSERT_EQUAL(0, res);
size = strlen("kittycatcat");
memcpy(buffer, "kittycatcat", size);
for (int j = 0; j < 132; j++) {
fwrite(buffer, 1, size, fd[0]);
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
}
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_simple_dir_seek() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "hello")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
off_t pos;
int i;
for (i = 0; i < 4; i++) {
sprintf((char*)buffer, "kitty%d", i);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
pos = telldir(dd[0]);
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
seekdir(dd[0], pos);
sprintf((char*)buffer, "kitty%d", i);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
rewinddir(dd[0]);
sprintf((char*)buffer, "kitty%d", 0);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
seekdir(dd[0], pos);
sprintf((char*)buffer, "kitty%d", i);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_large_dir_seek() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((dd[0] = opendir("/fs/" "hello")) != NULL);
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
off_t pos;
int i;
for (i = 0; i < 128; i++) {
sprintf((char*)buffer, "kitty%d", i);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
pos = telldir(dd[0]);
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
seekdir(dd[0], pos);
sprintf((char*)buffer, "kitty%d", i);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
rewinddir(dd[0]);
sprintf((char*)buffer, "kitty%d", 0);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, ".");
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, "..");
TEST_ASSERT_EQUAL(0, res);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
seekdir(dd[0], pos);
sprintf((char*)buffer, "kitty%d", i);
res = ((ed = readdir(dd[0])) != NULL);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ed->d_name, (char*)buffer);
TEST_ASSERT_EQUAL(0, res);
res = closedir(dd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_simple_file_seek() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "hello/kitty42", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
off_t pos;
size = strlen("kittycatcat");
for (int i = 0; i < 4; i++) {
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
pos = ftell(fd[0]);
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
res = fseek(fd[0], pos, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
rewind(fd[0]);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], pos, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], -size, SEEK_CUR);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], -size, SEEK_END);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], 0, SEEK_CUR);
TEST_ASSERT_EQUAL(0, res);
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_large_file_seek() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "hello/kitty42", "rb")) != NULL);
TEST_ASSERT_EQUAL(0, res);
off_t pos;
size = strlen("kittycatcat");
for (int i = 0; i < 128; i++) {
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
pos = ftell(fd[0]);
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
res = fseek(fd[0], pos, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
rewind(fd[0]);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], pos, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], -size, SEEK_CUR);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], -size, SEEK_END);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], 0, SEEK_CUR);
TEST_ASSERT_EQUAL(0, res);
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_simple_file_seek_and_write() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "hello/kitty42", "r+b")) != NULL);
TEST_ASSERT_EQUAL(0, res);
off_t pos;
size = strlen("kittycatcat");
for (int i = 0; i < 4; i++) {
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
pos = ftell(fd[0]);
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
memcpy(buffer, "doggodogdog", size);
res = fseek(fd[0], pos, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fwrite(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = fseek(fd[0], pos, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "doggodogdog", size);
TEST_ASSERT_EQUAL(0, res);
rewind(fd[0]);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], pos, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "doggodogdog", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], -size, SEEK_END);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], 0, SEEK_CUR);
TEST_ASSERT_EQUAL(0, res);
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_large_file_seek_and_write() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "hello/kitty42", "r+b")) != NULL);
TEST_ASSERT_EQUAL(0, res);
off_t pos;
size = strlen("kittycatcat");
for (int i = 0; i < 128; i++) {
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
if (i != 4) {
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
}
pos = ftell(fd[0]);
}
res = pos >= 0;
TEST_ASSERT_EQUAL(1, res);
memcpy(buffer, "doggodogdog", size);
res = fseek(fd[0], pos, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fwrite(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = fseek(fd[0], pos, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "doggodogdog", size);
TEST_ASSERT_EQUAL(0, res);
rewind(fd[0]);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], pos, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "doggodogdog", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], -size, SEEK_END);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], 0, SEEK_CUR);
TEST_ASSERT_EQUAL(0, res);
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_boundary_seek_and_write() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "hello/kitty42", "r+b")) != NULL);
TEST_ASSERT_EQUAL(0, res);
size = strlen("hedgehoghog");
const off_t offsets[] = {512, 1020, 513, 1021, 511, 1019};
for (int i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) {
off_t off = offsets[i];
memcpy(buffer, "hedgehoghog", size);
res = fseek(fd[0], off, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fwrite(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = fseek(fd[0], off, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "hedgehoghog", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], 0, SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "kittycatcat", size);
TEST_ASSERT_EQUAL(0, res);
res = fflush(fd[0]);
TEST_ASSERT_EQUAL(0, res);
}
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
void test_out_of_bounds_seek() {
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = !((fd[0] = fopen("/fs/" "hello/kitty42", "r+b")) != NULL);
TEST_ASSERT_EQUAL(0, res);
size = strlen("kittycatcat");
res = fseek(fd[0], 0, SEEK_END);
TEST_ASSERT_EQUAL(0, res);
res = ftell(fd[0]);
TEST_ASSERT_EQUAL(132*size, res);
res = fseek(fd[0], (132+4)*size,
SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(0, res);
memcpy(buffer, "porcupineee", size);
res = fwrite(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = fseek(fd[0], (132+4)*size,
SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "porcupineee", size);
TEST_ASSERT_EQUAL(0, res);
res = fseek(fd[0], 132*size,
SEEK_SET);
TEST_ASSERT_EQUAL(0, res);
res = fread(buffer, 1, size, fd[0]);
TEST_ASSERT_EQUAL(size, res);
res = memcmp(buffer, "\0\0\0\0\0\0\0\0\0\0\0", size);
TEST_ASSERT_EQUAL(0, res);
res = fclose(fd[0]);
TEST_ASSERT_EQUAL(0, res);
res = fs.unmount();
TEST_ASSERT_EQUAL(0, res);
}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}
// test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("Seek tests", test_seek_tests),
Case("Simple dir seek", test_simple_dir_seek),
Case("Large dir seek", test_large_dir_seek),
Case("Simple file seek", test_simple_file_seek),
Case("Large file seek", test_large_file_seek),
Case("Simple file seek and write", test_simple_file_seek_and_write),
Case("Large file seek and write", test_large_file_seek_and_write),
Case("Boundary seek and write", test_boundary_seek_and_write),
Case("Out-of-bounds seek", test_out_of_bounds_seek),
};
Specification specification(test_setup, cases);
int main() {
return !Harness::run(specification);
}

21
TESTS/util/Makefile Normal file
View File

@ -0,0 +1,21 @@
all: test_dirs test_files test_seek test_parallel
test_%: ../../littlefs/tests/test_%.sh
cp $< $(notdir $<)
sed -i -e 's/tests\//.\//' -e 's/echo/.\/echo.py/' $(notdir $<)
./clean.sh
ln -f -s replacements_mbed.yml replacements.yml
./$(notdir $<)
mkdir -p ../filesystem/$(patsubst test_%,%,$@)
cp main.cpp ../filesystem/$(patsubst test_%,%,$@)/main.cpp
./clean.sh
ln -f -s replacements_retarget.yml replacements.yml
./$(notdir $<)
mkdir -p ../filesystem_retarget/$(patsubst test_%,%,$@)
cp main.cpp ../filesystem_retarget/$(patsubst test_%,%,$@)/main.cpp
clean:
./clean.sh

4
TESTS/util/clean.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
rm -f main.cpp
rm -f template_all_names.txt

34
TESTS/util/echo.py Executable file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env python
import re
import sys
import subprocess
import os
def main(*args):
desc = ' '.join(args).strip('-= ')
name = 'test_' + desc.lower().replace(' ', '_').replace('-', '_')
exists = os.path.isfile('template_all_names.txt')
with open('template_all_names.txt', 'a') as file:
file.write(name + '\n')
file.write(desc + '\n')
with open('template_unit.fmt') as file:
template = file.read()
template_header, template_footer = template.split('{test}')
if exists:
with open('main.cpp', 'a') as file:
file.write(template_footer.format(
test_name=name))
if name != 'test_results':
with open('main.cpp', 'a') as file:
file.write(template_header.format(
test_name=name))
if __name__ == "__main__":
main(*sys.argv[1:])

View File

@ -0,0 +1,34 @@
- ['lfs_format\(&lfs, &cfg\)', 'MBED_TEST_FILESYSTEM::format(&bd)']
- ['lfs_mount\(&lfs, &cfg\)', 'fs.mount(&bd)']
- ['lfs_unmount\(&lfs\)', 'fs.unmount()']
- ['lfs_mkdir\(&lfs, (.*)\)', 'fs.mkdir(\1, 0777)']
- ['lfs_remove\(&lfs, (.*)\)', 'fs.remove(\1)']
- ['lfs_rename\(&lfs, (.*), ?(.*)\)', 'fs.rename(\1, \2)']
- ['lfs_dir_open\(&lfs, &dir\[(.*)\], ?(.*)\)', 'dir[\1].open(&fs, \2)']
- ['lfs_dir_close\(&lfs, &dir\[(.*)\]\)', 'dir[\1].close()']
- ['lfs_dir_read\(&lfs, &dir\[(.*)\], &info\)', 'dir[\1].read(&ent)']
- ['lfs_dir_seek\(&lfs, &dir\[(.*)\], ?(.*)\).*;', 'dir[\1].seek(\2);'] # no dir errors
- ['lfs_dir_rewind\(&lfs, &dir\[(.*)\]\).*;', 'dir[\1].rewind();'] # no dir errors
- ['lfs_dir_tell\(&lfs, &dir\[(.*)\]\)', 'dir[\1].tell()']
- ['lfs_file_open\(&lfs, &file\[(.*)\], ?(.*)\)', 'file[\1].open(&fs, \2)']
- ['lfs_file_close\(&lfs, &file\[(.*)\]\)', 'file[\1].close()']
- ['lfs_file_sync\(&lfs, &file\[(.*)\]\)', 'file[\1].sync()']
- ['lfs_file_write\(&lfs, &file\[(.*)\], ?(.*), (.*)\)', 'file[\1].write(\2, \3)']
- ['lfs_file_read\(&lfs, &file\[(.*)\], ?(.*), (.*)\)', 'file[\1].read(\2, \3)']
- ['lfs_file_seek\(&lfs, &file\[(.*)\], ?(.*)\)', 'file[\1].seek(\2)']
- ['lfs_file_tell\(&lfs, &file\[(.*)\]\)', 'file[\1].tell()']
- ['lfs_file_rewind\(&lfs, &file\[(.*)\]\).*;', 'file[\1].rewind();'] # no errors
- ['lfs_file_size\(&lfs, &file\[(.*)\]\)', 'file[\1].size()']
- ['LFS_TYPE_([A-Z]+)', 'DT_\1']
- ['LFS_O_([A-Z]+)', 'O_\1']
- ['LFS_SEEK_([A-Z]+)', 'SEEK_\1']
- ['LFS_ERR_EXISTS', '-EEXIST']
- ['LFS_ERR_([A-Z]+)', '-E\1']
- ['lfs_(s?)size_t', '\1size_t']
- ['lfs_soff_t', 'off_t']
- ['info\.name', 'ent.d_name']
- ['info\.type', 'ent.d_type']
- ['^.*info\.size.*$', ''] # dirent sizes not supported

View File

@ -0,0 +1,38 @@
- ['lfs_format\(&lfs, &cfg\)', 'MBED_TEST_FILESYSTEM::format(&bd)']
- ['lfs_mount\(&lfs, &cfg\)', 'fs.mount(&bd)']
- ['lfs_unmount\(&lfs\)', 'fs.unmount()']
- ['lfs_mkdir\(&lfs, (.*)\)', 'mkdir("/fs/" \1, 0777)']
- ['lfs_remove\(&lfs, (.*)\)', 'remove("/fs/" \1)']
- ['lfs_rename\(&lfs, (.*), ?(.*)\)', 'rename("/fs/" \1, "/fs/" \2)']
- ['lfs_dir_open\(&lfs, &dir\[(.*)\], ?(.*)\)', '!((dd[\1] = opendir("/fs/" \2)) != NULL)']
- ['lfs_dir_close\(&lfs, &dir\[(.*)\]\)', 'closedir(dd[\1])']
- ['lfs_dir_read\(&lfs, &dir\[(.*)\], &info\)', '((ed = readdir(dd[\1])) != NULL)']
- ['lfs_dir_seek\(&lfs, &dir\[(.*)\], ?(.*)\).*;', 'seekdir(dd[\1], \2);'] # no dir errors
- ['lfs_dir_rewind\(&lfs, &dir\[(.*)\]\).*;', 'rewinddir(dd[\1]);'] # no dir errors
- ['lfs_dir_tell\(&lfs, &dir\[(.*)\]\)', 'telldir(dd[\1])']
- ['lfs_file_open\(&lfs, &file\[(.*)\], ?(.*)\)', '!((fd[\1] = fopen("/fs/" \2)) != NULL)']
- ['lfs_file_close\(&lfs, &file\[(.*)\]\)', 'fclose(fd[\1])']
- ['lfs_file_sync\(&lfs, &file\[(.*)\]\)', 'fflush(fd[\1])']
- ['lfs_file_write\(&lfs, &file\[(.*)\], ?(.*), (.*)\)', 'fwrite(\2, 1, \3, fd[\1])']
- ['lfs_file_read\(&lfs, &file\[(.*)\], ?(.*), (.*)\)', 'fread(\2, 1, \3, fd[\1])']
- ['lfs_file_tell\(&lfs, &file\[(.*)\]\)', 'ftell(fd[\1])']
- ['lfs_file_rewind\(&lfs, &file\[(.*)\]\).*;', 'rewind(fd[\1]);'] # no errors
- ['LFS_TYPE_([A-Z]+)', 'DT_\1']
- ['LFS_SEEK_([A-Z]+)', 'SEEK_\1']
- ['LFS_ERR_EXISTS', '-EEXIST']
- ['LFS_ERR_([A-Z]+)', '-E\1']
- ['lfs_(s?)size_t', '\1size_t']
- ['lfs_soff_t', 'off_t']
- ['info\.name', 'ed->d_name']
- ['info\.type', 'ed->d_type']
- ['^.*info\.size.*$', ''] # dirent sizes not supported
- ['LFS_O_WRONLY \| LFS_O_CREAT \| LFS_O_APPEND', '"ab"']
- ['LFS_O_WRONLY \| LFS_O_TRUNC', '"wb"']
- ['LFS_O_CREAT \| LFS_O_WRONLY', '"wb"']
- ['LFS_O_WRONLY \| LFS_O_CREAT', '"wb"']
- ['LFS_O_RDONLY', '"rb"']
- ['LFS_O_RDWR', '"r+b"']

33
TESTS/util/stats.py Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env python
import re
import sys
import subprocess
import os
def main(*args):
with open('main.cpp') as file:
tests = file.read()
cases = []
with open('template_all_names.txt') as file:
while True:
name = file.readline().strip('\n')
desc = file.readline().strip('\n')
if name == 'test_results':
break
cases.append((name, desc))
with open('template_wrapper.fmt') as file:
template = file.read()
with open('main.cpp', 'w') as file:
file.write(template.format(
tests=tests,
test_cases='\n'.join(
4*' '+'Case("{desc}", {name}),'.format(
name=name, desc=desc) for name, desc in cases)))
if __name__ == "__main__":
main(*sys.argv[1:])

View File

@ -0,0 +1,4 @@
{{
{test}
}}

View File

@ -0,0 +1,8 @@
void {test_name}() {{
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{test}
res = bd.deinit();
TEST_ASSERT_EQUAL(0, res);
}}

View File

@ -0,0 +1,86 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>
#include <errno.h>
using namespace utest::v1;
// test configuration
#ifndef MBED_TEST_FILESYSTEM
#define MBED_TEST_FILESYSTEM LittleFileSystem
#endif
#ifndef MBED_TEST_FILESYSTEM_DECL
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
#endif
#ifndef MBED_TEST_BLOCKDEVICE
#define MBED_TEST_BLOCKDEVICE SPIFBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5)
#endif
#ifndef MBED_TEST_BLOCKDEVICE_DECL
#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
#endif
#ifndef MBED_TEST_FILES
#define MBED_TEST_FILES 4
#endif
#ifndef MBED_TEST_DIRS
#define MBED_TEST_DIRS 4
#endif
#ifndef MBED_TEST_BUFFER
#define MBED_TEST_BUFFER 8192
#endif
#ifndef MBED_TEST_TIMEOUT
#define MBED_TEST_TIMEOUT 120
#endif
// declarations
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define INCLUDE(x) STRINGIZE(x.h)
#include INCLUDE(MBED_TEST_FILESYSTEM)
#include INCLUDE(MBED_TEST_BLOCKDEVICE)
MBED_TEST_FILESYSTEM_DECL;
MBED_TEST_BLOCKDEVICE_DECL;
Dir dir[MBED_TEST_DIRS];
File file[MBED_TEST_FILES];
DIR *dd[MBED_TEST_DIRS];
FILE *fd[MBED_TEST_FILES];
struct dirent ent;
struct dirent *ed;
size_t size;
uint8_t buffer[MBED_TEST_BUFFER];
uint8_t rbuffer[MBED_TEST_BUFFER];
uint8_t wbuffer[MBED_TEST_BUFFER];
// tests
{tests}
// test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {{
GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}}
Case cases[] = {{
{test_cases}
}};
Specification specification(test_setup, cases);
int main() {{
return !Harness::run(specification);
}}

48
TESTS/util/test.py Executable file
View File

@ -0,0 +1,48 @@
#!/usr/bin/env python
import re
import sys
import subprocess
import os
import yaml
def generate(test):
with open('replacements.yml') as file:
replacements = yaml.load(file)
lines = []
for line in re.split('(?<=[;{}])\n', test.read()):
for pattern, replacement in replacements:
line = re.sub(pattern, replacement, line, 0, re.DOTALL | re.MULTILINE)
match = re.match('(?: *\n)*( *)(.*)=>(.*);', line, re.DOTALL | re.MULTILINE)
if match:
tab, test, expect = match.groups()
lines.append(tab+'res = {test};'.format(test=test.strip()))
lines.append(tab+'TEST_ASSERT_EQUAL({expect}, res);'.format(
name=re.match('\w*', test.strip()).group(),
expect=expect.strip()))
else:
lines.append(line)
lines = lines[:-1]
with open('template_subunit.fmt') as file:
template = file.read()
with open('main.cpp', 'a') as file:
file.write(template.format(
test=('\n'.join(
4*' '+line.replace('\n', '\n'+4*' ')
for line in lines))))
def main(test=None):
if test and not test.startswith('-'):
with open(test) as file:
generate(file)
else:
generate(sys.stdin)
if __name__ == "__main__":
main(*sys.argv[1:])