Import Mbed OS hard-float snapshot

This commit is contained in:
Beslan
2026-06-01 20:15:04 +03:00
commit d3738e2f89
16278 changed files with 10628036 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @file fsfat_debug.h
*
* component debug header file.
*/
#ifndef __FSFAT_DEBUG
#define __FSFAT_DEBUG
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
/* Debug Support */
#define FSFAT_LOG_NONE 0
#define FSFAT_LOG_ERR 1
#define FSFAT_LOG_WARN 2
#define FSFAT_LOG_NOTICE 3
#define FSFAT_LOG_INFO 4
#define FSFAT_LOG_DEBUG 5
#define FSFAT_LOG_FENTRY 6
#define FSFAT_LOG(_fmt, ...) \
do \
{ \
printf(_fmt, __VA_ARGS__); \
}while(0);
#define noFSFAT_DEBUG
#ifdef FSFAT_DEBUG
extern uint32_t fsfat_optDebug_g;
extern uint32_t fsfat_optLogLevel_g;
/* uncomment for asserts to work */
/* #undef NDEBUG */
// todo: port to mbedOSV3++ #include <core-util/assert.h>
#define FSFAT_INLINE
// todo: port to mbedOSV3++ #define FSFAT_ASSERT CORE_UTIL_ASSERT
#define FSFAT_ASSERT(...)
#define FSFAT_DBGLOG(_fmt, ...) \
do \
{ \
if(fsfat_optDebug_g && (fsfat_optLogLevel_g >= FSFAT_LOG_DEBUG)) \
{ \
printf(_fmt, __VA_ARGS__); \
} \
}while(0);
#define FSFAT_ERRLOG(_fmt, ...) \
do \
{ \
if(fsfat_optDebug_g && (fsfat_optLogLevel_g >= FSFAT_LOG_ERR)) \
{ \
printf(_fmt, __VA_ARGS__); \
} \
}while(0);
#define FSFAT_FENTRYLOG(_fmt, ...) \
do \
{ \
if(fsfat_optDebug_g && (fsfat_optLogLevel_g >= FSFAT_LOG_FENTRY)) \
{ \
printf(_fmt, __VA_ARGS__); \
} \
}while(0);
#else
#define FSFAT_ASSERT(_x) do { } while(0)
#define FSFAT_INLINE inline
#define FSFAT_DBGLOG(_fmt, ...) do { } while(0)
#define FSFAT_ERRLOG(_fmt, ...) do { } while(0)
#define FSFAT_FENTRYLOG(_fmt, ...) do { } while(0)
#endif /* FSFAT_DEBUG */
#endif /*__FSFAT_DEBUG*/

View File

@@ -0,0 +1,117 @@
/* @file fsfat_test.c
*
* mbed Microcontroller Library
* Copyright (c) 2006-2016 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* test support code implementation file.
*/
#include "fsfat_debug.h"
#include "fsfat_test.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include <ctype.h>
#ifdef FSFAT_DEBUG
uint32_t fsfat_optDebug_g = 1;
uint32_t fsfat_optLogLevel_g = FSFAT_LOG_NONE; /*FSFAT_LOG_NONE|FSFAT_LOG_ERR|FSFAT_LOG_DEBUG|FSFAT_LOG_FENTRY; */
#endif
/* ruler for measuring text strings */
/* 1 1 1 1 1 1 1 1 1 1 2 2 2 */
/* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 */
/* 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 */
const uint8_t fsfat_test_byte_data_table[FSFAT_TEST_BYTE_DATA_TABLE_SIZE] = {
0x2d, 0xf3, 0x31, 0x4c, 0x11, 0x4f, 0xde, 0x0d, 0xbd, 0xbc, 0xa6, 0x78, 0x36, 0x5c, 0x1d, 0x28,
0x5f, 0xa9, 0x10, 0x65, 0x54, 0x45, 0x21, 0x1a, 0x88, 0xfe, 0x76, 0x45, 0xb9, 0xac, 0x65, 0x9a,
0x34, 0x9d, 0x73, 0x10, 0xb4, 0xa9, 0x2e, 0x90, 0x95, 0x68, 0xac, 0xfe, 0xc5, 0x2d, 0x15, 0x03,
0x34, 0x70, 0xf1, 0x1d, 0x48, 0xa1, 0xa0, 0xed, 0x5c, 0x2f, 0xf5, 0x2b, 0xb9, 0x84, 0xbb, 0x45,
0x32, 0xdd, 0xb1, 0x33, 0x95, 0x2a, 0xbc, 0x26, 0xf0, 0x89, 0xba, 0xf4, 0xbd, 0xf9, 0x5d, 0x2e,
0x6e, 0x11, 0xc6, 0xa7, 0x78, 0xfc, 0xc9, 0x0e, 0x6b, 0x38, 0xba, 0x14, 0x1b, 0xab, 0x4c, 0x20,
0x91, 0xe4, 0xb0, 0xf1, 0x2b, 0x14, 0x07, 0x6b, 0xb5, 0xcd, 0xe3, 0x49, 0x75, 0xac, 0xe8, 0x98,
0xf1, 0x58, 0x8f, 0xd9, 0xc4, 0x8f, 0x00, 0x17, 0xb5, 0x06, 0x6a, 0x33, 0xbd, 0xa7, 0x40, 0x5a,
0xbf, 0x49, 0xf7, 0x27, 0x1b, 0x4c, 0x3e, 0x6f, 0xe3, 0x08, 0x1f, 0xfd, 0xa6, 0xd4, 0xc7, 0x5f,
0xa4, 0xa6, 0x82, 0xad, 0x19, 0xd5, 0x5c, 0xd8, 0x3a, 0x49, 0x85, 0xc9, 0x21, 0x83, 0xf6, 0xc6,
0x84, 0xf9, 0x76, 0x89, 0xf3, 0x2d, 0x17, 0x50, 0x97, 0x38, 0x48, 0x9a, 0xe1, 0x82, 0xcd, 0xac,
0xa8, 0x1d, 0xd7, 0x96, 0x5e, 0xb3, 0x08, 0xa8, 0x3a, 0xc7, 0x2b, 0x05, 0xaf, 0xdc, 0x16, 0xdf,
0x48, 0x0f, 0x2a, 0x7e, 0x3a, 0x82, 0xd7, 0x80, 0xd6, 0x49, 0x27, 0x5d, 0xe3, 0x07, 0x62, 0xb3,
0xc3, 0x6c, 0xba, 0xb2, 0xaa, 0x9f, 0xd9, 0x03, 0x0d, 0x27, 0xa8, 0xe0, 0xd6, 0xee, 0x79, 0x4b,
0xd6, 0x97, 0x99, 0xb7, 0x11, 0xd6, 0x0d, 0x34, 0xae, 0x99, 0x4a, 0x93, 0x95, 0xd0, 0x5a, 0x34,
0x19, 0xa2, 0x69, 0x57, 0xcf, 0x7c, 0x3d, 0x98, 0x88, 0x5d, 0x04, 0xf2, 0xd7, 0xac, 0xa5, 0x63
};
/* @brief test utility function to delete the file identified by filename
*/
int32_t fsfat_test_delete(const char *filename)
{
FSFAT_FENTRYLOG("%s:entered.\r\n", __func__);
return remove(filename);
}
/* @brief test utility function to create a file
*
* @param filename name of the file including path
* @param data data to store in file
* @param len number of bytes of data present in the data buffer.
*/
int32_t fsfat_test_create(const char *filename, const char *data, size_t len)
{
int32_t ret = -1;
FILE *fp = NULL;
FSFAT_FENTRYLOG("%s:entered (filename=%s, len=%d).\n", __func__, filename, (int) len);
fp = fopen(filename, "w+");
if (fp == NULL) {
return ret;
}
ret = fwrite((const void *) data, len, 1, fp);
if (ret < 0) {
fclose(fp);
return ret;
}
fclose(fp);
return ret;
}
/* @brief support function for generating a kv_name
* @param name buffer to hold kv name
* @param len length of kv name to generate
*
*/
int32_t fsfat_test_filename_gen(char *name, const size_t len)
{
size_t i;
uint32_t pos = 0;
const char *buf = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!$-_@";
const int buf_len = strlen(buf);
FSFAT_FENTRYLOG("%s:entered\n", __func__);
for (i = 0; i < len; i++) {
pos = rand() % (buf_len);
name[i] = buf[pos];
}
return 0;
}

View File

@@ -0,0 +1,75 @@
/** @file fsfat_test.h
*
* mbed Microcontroller Library
* Copyright (c) 2006-2016 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Header file for test support data structures and function API.
*/
#ifndef __FSFAT_TEST_H
#define __FSFAT_TEST_H
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Defines */
//#define FSFAT_INIT_1_TABLE_HEAD { "a", ""}
#define FSFAT_INIT_1_TABLE_MID_NODE { "/sd/01234567.txt", "abcdefghijklmnopqrstuvwxyz"}
//#define FSFAT_INIT_1_TABLE_TAIL { "/sd/fopentst/hello/world/animal/wobbly/dog/foot/backrght.txt", "present"}
#define FSFAT_TEST_RW_TABLE_SENTINEL 0xffffffff
#define FSFAT_TEST_BYTE_DATA_TABLE_SIZE 256
#define FSFAT_UTEST_MSG_BUF_SIZE 256
#define FSFAT_UTEST_DEFAULT_TIMEOUT_MS 10000
#define FSFAT_MBED_HOSTTEST_TIMEOUT 60
#define FSFAT_MAX_FILE_BASENAME 8
#define FSFAT_MAX_FILE_EXTNAME 3
#define FSFAT_BUF_MAX_LENGTH 64
#define FSFAT_FILENAME_MAX_LENGTH 255
/* support macro for make string for utest _MESSAGE macros, which dont support formatted output */
#define FSFAT_TEST_UTEST_MESSAGE(_buf, _max_len, _fmt, ...) \
do \
{ \
snprintf((_buf), (_max_len), (_fmt), __VA_ARGS__); \
}while(0);
/*
* Structures
*/
/* kv data for test */
typedef struct fsfat_kv_data_t {
const char *filename;
const char *value;
} fsfat_kv_data_t;
extern const uint8_t fsfat_test_byte_data_table[FSFAT_TEST_BYTE_DATA_TABLE_SIZE];
int32_t fsfat_test_create(const char *filename, const char *data, size_t len);
int32_t fsfat_test_delete(const char *key_name);
int32_t fsfat_test_filename_gen(char *name, const size_t len);
#ifdef __cplusplus
}
#endif
#endif /* __FSFAT_TEST_H */

View File

@@ -0,0 +1,495 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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 FATFileSystem
#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 SDBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SDBlockDevice bd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
#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_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_CONF_SD_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 dir_file_check(char *list[], uint32_t elements)
{
int res;
while (1) {
res = dir[0].read(&ent);
if (0 == res) {
break;
}
for (int i = 0; i < elements ; i++) {
res = strcmp(ent.d_name, list[i]);
if (0 == res) {
res = ent.d_type;
if ((DT_DIR != res) && (DT_REG != res)) {
TEST_ASSERT(1);
}
break;
} else if (i == elements) {
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);
char *dir_list[] = {"potato", "burito", ".", ".."};
dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0])));
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(-ENOTDIR, res);
res = dir[0].open(&fs, "burito");
TEST_ASSERT_NOT_EQUAL(0, res);
res = file[0].open(&fs, "tomato", O_RDONLY);
TEST_ASSERT_EQUAL(-ENOENT, res);
res = file[0].open(&fs, "potato", O_RDONLY);
TEST_ASSERT_NOT_EQUAL(0, 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, "/");
TEST_ASSERT_EQUAL(0, res);
char *dir_list[] = {"potato", "baked", "sweet", "fried", ".", ".."};
dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0])));
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);
#if (MBED_TEST_FILESYSTEM != FATFileSystem)
char *dir_list[] = {".", ".."};
dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0])));
#endif
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_NOT_EQUAL(0, 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);
#if (MBED_TEST_FILESYSTEM != FATFileSystem)
char *dir_list[] = {".", ".."};
dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0])));
#endif
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 = 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);
char *dir_list[] = {"burito", "cactus", ".", ".."};
dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0])));
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);
char *dir_list[] = {"baked", "sweet", "fried", ".", ".."};
dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0])));
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_NOT_EQUAL(0, res);
res = fs.remove("warmpotato/mushy");
TEST_ASSERT_EQUAL(0, res);
res = fs.remove("warmpotato");
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);
char *dir_list[] = {"baked", "sweet", "fried", ".", ".."};
dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0])));
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_NOT_EQUAL(0, 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);
char *dir_list[] = {"baked", "sweet", "fried", ".", ".."};
dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0])));
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,338 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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 FATFileSystem
#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 SDBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SDBlockDevice bd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
#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;
uint8_t buffer[MBED_TEST_BUFFER];
uint8_t rbuffer[MBED_TEST_BUFFER];
uint8_t wbuffer[MBED_TEST_BUFFER];
static char file_counter = 0;
const char *filenames[] = {"smallavacado", "mediumavacado", "largeavacado",
"blockfile", "bigblockfile", "hello", ".", ".."
};
// 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);
{
size_t size;
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);
}
template <int file_size, int write_size, int read_size>
void test_write_file_test()
{
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
size_t size = file_size;
size_t chunk = write_size;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, filenames[file_counter], 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 = file_size;
size_t chunk = read_size;
srand(0);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
res = file[0].open(&fs, filenames[file_counter], 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);
}
file_counter++;
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);
int numFiles = sizeof(filenames) / sizeof(filenames[0]);
// Check the filenames in directory
while (1) {
res = dir[0].read(&ent);
if (0 == res) {
break;
}
for (int i = 0; i < numFiles ; i++) {
res = strcmp(ent.d_name, filenames[i]);
if (0 == res) {
res = ent.d_type;
if ((DT_REG != res) && (DT_DIR != res)) {
TEST_ASSERT(1);
}
break;
} else if (i == numFiles) {
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_write_file_test<32, 31, 29>),
Case("Medium file test", test_write_file_test<8192, 31, 29>),
Case("Large file test", test_write_file_test<262144, 31, 29>),
Case("Block Size file test", test_write_file_test<9000, 512, 512>),
Case("Multiple block size file test", test_write_file_test<26215, MBED_TEST_BUFFER, MBED_TEST_BUFFER>),
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);
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,214 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if !defined(MBED_CONF_RTOS_PRESENT)
#error [NOT_SUPPORTED] filesystem test cases require a RTOS to run
#else
#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 FATFileSystem
#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 SDBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SDBlockDevice bd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
#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 512
#endif
#ifndef MBED_TEST_TIMEOUT
#define MBED_TEST_TIMEOUT 120
#endif
#ifndef MBED_THREAD_COUNT
#define MBED_THREAD_COUNT MBED_TEST_FILES
#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;
volatile bool count_done = 0;
// 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 write_file_data(char count)
{
char filename[10];
uint8_t wbuffer[MBED_TEST_BUFFER];
int res;
sprintf(filename, "%s%d", "data", count);
res = file[count].open(&fs, filename, O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, res);
char letter = 'A' + count;
for (uint32_t i = 0; i < MBED_TEST_BUFFER; i++) {
wbuffer[i] = letter++;
if ('z' == letter) {
letter = 'A' + count;
}
}
for (uint32_t i = 0; i < 5; i++) {
res = file[count].write(wbuffer, MBED_TEST_BUFFER);
TEST_ASSERT_EQUAL(MBED_TEST_BUFFER, res);
}
res = file[count].close();
TEST_ASSERT_EQUAL(0, res);
}
void read_file_data(char count)
{
char filename[10];
uint8_t rbuffer[MBED_TEST_BUFFER];
int res;
sprintf(filename, "%s%d", "data", count);
res = file[count].open(&fs, filename, O_RDONLY);
TEST_ASSERT_EQUAL(0, res);
for (uint32_t i = 0; i < 5; i++) {
res = file[count].read(rbuffer, MBED_TEST_BUFFER);
TEST_ASSERT_EQUAL(MBED_TEST_BUFFER, res);
char letter = 'A' + count;
for (uint32_t i = 0; i < MBED_TEST_BUFFER; i++) {
res = rbuffer[i];
TEST_ASSERT_EQUAL(letter++, res);
if ('z' == letter) {
letter = 'A' + count;
}
}
}
res = file[count].close();
TEST_ASSERT_EQUAL(0, res);
}
void test_thread_access_test()
{
char *dummy = new (std::nothrow) char[OS_STACK_SIZE * MBED_THREAD_COUNT];
delete[] dummy;
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
Thread *data[MBED_THREAD_COUNT];
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
res = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, res);
// Write threads in parallel
for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) {
data[thread_count] = new Thread(osPriorityNormal);
data[thread_count]->start(callback((void(*)(void *))write_file_data, (void *)thread_count));
}
// Wait for write thread to join before creating read thread
for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) {
data[thread_count]->join();
delete data[thread_count];
data[thread_count] = new Thread(osPriorityNormal);
data[thread_count]->start(callback((void(*)(void *))read_file_data, (void *)thread_count));
}
// Wait for read threads to join
for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) {
data[thread_count]->join();
delete data[thread_count];
}
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("Filesystem access from multiple threads", test_thread_access_test),
};
Specification specification(test_setup, cases);
int main()
{
return !Harness::run(specification);
}
#endif // !defined(MBED_CONF_RTOS_PRESENT)

View File

@@ -0,0 +1,657 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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 FATFileSystem
#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 SDBlockDevice
#define MBED_TEST_BLOCKDEVICE_DECL SDBlockDevice bd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
#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_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;
uint8_t buffer[MBED_CONF_SD_TEST_BUFFER];
// tests
void test_seek_tests()
{
int res = bd.init();
TEST_ASSERT_EQUAL(0, res);
{
size_t size;
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);
#if (MBED_TEST_FILESYSTEM != FATFileSystem)
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);
#endif
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);
#if (MBED_TEST_FILESYSTEM != FATFileSystem)
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);
#endif
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);
#if (MBED_TEST_FILESYSTEM != FATFileSystem)
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);
#endif
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);
#if (MBED_TEST_FILESYSTEM != FATFileSystem)
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);
#endif
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);
{
size_t size;
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 = 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);
{
size_t size;
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 = 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);
{
size_t size;
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 = 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);
{
size_t size;
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 = 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);
{
size_t size;
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);
{
size_t size;
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);
#if (MBED_TEST_FILESYSTEM != FATFileSystem)
// FatFs does not guarantee empty expanded buffer
res = memcmp(buffer, "\0\0\0\0\0\0\0\0\0\0\0", size);
TEST_ASSERT_EQUAL(0, res);
#endif
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);
}