test: fs: Test cases for file system interfaces

Focus on file system interfaces implemented in file system core,
not specific file system.

Signed-off-by: Meng xianglin <xianglinx.meng@intel.com>
This commit is contained in:
Meng xianglin 2020-05-24 21:18:29 +08:00 committed by Anas Nashif
commit 21638f054f
8 changed files with 1342 additions and 0 deletions

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(fs_api)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,2 @@
CONFIG_FILE_SYSTEM=y
CONFIG_ZTEST=y

View file

@ -0,0 +1,94 @@
/*
* Copyright (c) 2016 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "test_fs.h"
/**
* @brief Test file system interface implemented in kernel
*
* @defgroup filesystem File System
*
* @ingroup all_test
* @{
* @}
*/
static void fs_setup(void)
{
fs_register(TEST_FS_1, &temp_fs);
}
static void dummy_teardown(void)
{
return;
}
static void dummy_setup(void)
{
return;
}
static void fs_teardown(void)
{
fs_unregister(TEST_FS_1, &temp_fs);
}
/**
* @brief Common file system operations through a general interface
*
* @details After register file system:
* - mount
* - statvfs
* - mkdir
* - opendir
* - readdir
* - closedir
* - open
* - write
* - read
* - lseek
* - tell
* - truncate
* - sync
* - close
* - rename
* - stat
* - unlink
* - unmount
* - unregister file system
* the order of test cases is critical, one case depend on ther
* case before it.
*
* @ingroup filesystem
*
*/
void test_main(void)
{
ztest_test_suite(fat_fs_basic_test,
ztest_unit_test(test_fs_register),
ztest_unit_test_setup_teardown(test_mount,
fs_setup,
dummy_teardown),
ztest_unit_test(test_file_statvfs),
ztest_unit_test(test_mkdir),
ztest_unit_test(test_opendir),
ztest_unit_test(test_closedir),
ztest_unit_test(test_lsdir),
ztest_unit_test(test_file_open),
ztest_unit_test(test_file_write),
ztest_unit_test(test_file_read),
ztest_unit_test(test_file_truncate),
ztest_unit_test(test_file_close),
ztest_unit_test(test_file_sync),
ztest_unit_test(test_file_rename),
ztest_unit_test(test_file_stat),
ztest_unit_test(test_file_unlink),
ztest_unit_test_setup_teardown(test_unmount,
dummy_setup,
fs_teardown)
);
ztest_run_test_suite(fat_fs_basic_test);
}

View file

@ -0,0 +1,341 @@
/*
* Copyright (c) 2020 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
#include <kernel.h>
#include <zephyr/types.h>
#include <errno.h>
#include <init.h>
#include <fs/fs.h>
#include <sys/__assert.h>
#include "test_fs.h"
#define BUF_LEN 128
static char buffer[BUF_LEN];
static char *read_pos = buffer;
static char *cur = buffer;
static int file_length;
static struct fs_mount_t *mp[FS_TYPE_END];
static bool nospace;
static
int temp_open(struct fs_file_t *zfp, const char *file_name, fs_mode_t flags)
{
if (zfp == NULL || file_name == NULL) {
return -EINVAL;
}
if (zfp->filep) {
if (strcmp(zfp->filep, file_name) == 0) {
/* file has been opened */
return -EEXIST;
}
}
if (!(flags & FS_O_MASK)) {
return -EINVAL;
}
zfp->filep = (char *)file_name;
return 0;
}
static int temp_close(struct fs_file_t *zfp)
{
if (zfp == NULL) {
return -EINVAL;
}
if (zfp->filep) {
zfp->filep = NULL;
} else {
return -EIO;
}
return 0;
}
static int temp_unlink(struct fs_mount_t *mountp, const char *path)
{
if (mountp == NULL || path == NULL) {
return -EINVAL;
}
if (strcmp(mountp->mnt_point, path) == 0) {
return -EPERM;
}
return 0;
}
static int temp_rename(struct fs_mount_t *mountp, const char *from,
const char *to)
{
if (mountp == NULL || from == NULL || to == NULL) {
return -EINVAL;
}
if (strcmp(to, TEST_FILE_EX) == 0) {
return -EINVAL;
}
return 0;
}
static ssize_t temp_read(struct fs_file_t *zfp, void *ptr, size_t size)
{
unsigned int br;
if (zfp == NULL || ptr == NULL) {
return -EINVAL;
}
br = size;
if (read_pos - buffer + br > file_length) {
br = file_length - (read_pos - buffer);
}
memcpy(ptr, read_pos, br);
read_pos += br;
cur = read_pos;
return br;
}
static ssize_t temp_write(struct fs_file_t *zfp, const void *ptr, size_t size)
{
unsigned int bw;
if (zfp == NULL || ptr == NULL) {
return -EINVAL;
}
if (nospace) {
return -ENOSPC;
}
bw = size;
if (file_length + bw > BUF_LEN) {
bw = BUF_LEN - file_length;
nospace = true;
}
memcpy(buffer + file_length, ptr, bw);
file_length += bw;
cur = buffer + file_length;
return bw;
}
static int temp_seek(struct fs_file_t *zfp, off_t offset, int whence)
{
if (!zfp) {
return -EINVAL;
}
switch (whence) {
case FS_SEEK_SET:
cur = buffer + offset;
break;
case FS_SEEK_CUR:
cur += offset;
break;
case FS_SEEK_END:
cur = buffer + file_length + offset;
break;
default:
return -EINVAL;
}
if ((cur < buffer) || (cur > buffer + file_length)) {
return -EINVAL;
}
return 0;
}
static off_t temp_tell(struct fs_file_t *zfp)
{
if (!zfp) {
return -EINVAL;
}
if (nospace) {
return -ENOSPC;
}
return cur - buffer;
}
static int temp_truncate(struct fs_file_t *zfp, off_t length)
{
if (!zfp) {
return -EINVAL;
}
if (length > BUF_LEN) {
return -EINVAL;
}
file_length = length;
return 0;
}
static int temp_sync(struct fs_file_t *zfp)
{
if (!zfp) {
return -EINVAL;
}
if (nospace) {
return -ENOSPC;
}
return 0;
}
static int temp_mkdir(struct fs_mount_t *mountp, const char *path)
{
if (mountp == NULL || path == NULL) {
return -EINVAL;
}
if (strcmp(mountp->mnt_point, path) == 0) {
return -EPERM;
}
return 0;
}
static int temp_opendir(struct fs_dir_t *zdp, const char *path)
{
if (zdp == NULL || path == NULL) {
return -EINVAL;
}
if (zdp->dirp) {
if (strcmp(zdp->dirp, path) == 0) {
return -EIO;
}
}
zdp->dirp = (char *)path;
return 0;
}
static int i;
static int temp_readdir(struct fs_dir_t *zdp, struct fs_dirent *entry)
{
if (!zdp) {
return -EINVAL;
}
if (!entry) {
return -ENOENT;
}
switch (i) {
case 0:
strcpy(entry->name, ".");
entry->type = FS_DIR_ENTRY_DIR;
i++;
break;
case 1:
strcpy(entry->name, "testdir");
entry->type = FS_DIR_ENTRY_DIR;
i++;
break;
case 2:
strcpy(entry->name, "test.txt");
entry->type = FS_DIR_ENTRY_FILE;
i++;
break;
default:
strcpy(entry->name, "\0");
i = 0;
break;
}
return 0;
}
static int temp_closedir(struct fs_dir_t *zdp)
{
if (!zdp) {
return -EINVAL;
}
if (!(zdp->dirp)) {
return -EIO;
}
zdp->dirp = NULL;
return 0;
}
static int temp_stat(struct fs_mount_t *mountp,
const char *path, struct fs_dirent *entry)
{
if (mountp == NULL || path == NULL || entry == NULL) {
return -EINVAL;
}
return 0;
}
static int temp_statvfs(struct fs_mount_t *mountp,
const char *path, struct fs_statvfs *stat)
{
if (mountp == NULL || path == NULL || stat == NULL) {
return -EINVAL;
}
memset(stat, 0, sizeof(struct fs_statvfs));
stat->f_bsize = 512;
return 0;
}
static int temp_mount(struct fs_mount_t *mountp)
{
if (mountp == NULL) {
return -EINVAL;
}
if (mountp->mnt_point[mountp->mountp_len - 1] != ':') {
return -EINVAL;
}
mp[mountp->type] = mountp;
return 0;
}
static int temp_unmount(struct fs_mount_t *mountp)
{
if (mountp == NULL) {
return -EINVAL;
}
if (mp[mountp->type] == NULL) {
return -EINVAL;
}
mp[mountp->type] = NULL;
return 0;
}
/* File system interface */
struct fs_file_system_t temp_fs = {
.open = temp_open,
.close = temp_close,
.read = temp_read,
.write = temp_write,
.lseek = temp_seek,
.tell = temp_tell,
.truncate = temp_truncate,
.sync = temp_sync,
.opendir = temp_opendir,
.readdir = temp_readdir,
.closedir = temp_closedir,
.mount = temp_mount,
.unmount = temp_unmount,
.unlink = temp_unlink,
.rename = temp_rename,
.mkdir = temp_mkdir,
.stat = temp_stat,
.statvfs = temp_statvfs,
};

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2020 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __TEST_FS_H__
#define __TEST_FS_H__
#include <zephyr.h>
#include <ztest.h>
#include <fs/fs.h>
#define TEST_FS_MNTP "/NAND:"
#define TEST_FILE TEST_FS_MNTP"/testfile.txt"
#define TEST_FILE_RN TEST_FS_MNTP"/testfile_renamed.txt"
#define TEST_FILE_EX TEST_FS_MNTP"/testfile_exist.txt"
#define TEST_DIR TEST_FS_MNTP"/testdir"
#define TEST_DIR_FILE TEST_FS_MNTP"/testdir/testfile.txt"
/* kenel only reserve two slots for specific file system.
* By disable that two file systems, test cases can make
* use of that slots to register a file systems for test
*/
#define TEST_FS_1 FS_FATFS
#define TEST_FS_2 FS_LITTLEFS
extern struct fs_file_system_t temp_fs;
struct test_fs_data {
int reserve;
};
void test_fs_register(void);
void test_mount(void);
void test_file_statvfs(void);
void test_mkdir(void);
void test_opendir(void);
void test_closedir(void);
void test_lsdir(void);
void test_file_open(void);
void test_file_write(void);
void test_file_read(void);
void test_file_truncate(void);
void test_file_close(void);
void test_file_sync(void);
void test_file_rename(void);
void test_file_stat(void);
void test_file_unlink(void);
void test_unmount(void);
#endif

View file

@ -0,0 +1,719 @@
/*
* Copyright (c) 2020 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @brief Test cases for filesystem api
*
* @defgroup filesystem_api File system API
* @ingroup filesystem
* @{
* @}
*/
#include "test_fs.h"
#include <stdio.h>
#include <string.h>
struct fs_file_system_t null_fs = {NULL};
static struct test_fs_data test_data;
static struct fs_mount_t test_fs_mnt_1 = {
.type = TEST_FS_1,
.mnt_point = TEST_FS_MNTP,
.fs_data = &test_data,
};
static struct fs_mount_t test_fs_mnt_unsupported_fs = {
.type = FS_TYPE_END,
.mnt_point = "/MMCBLOCK:",
.fs_data = &test_data,
};
static struct fs_mount_t test_fs_mnt_invalid_root = {
.type = TEST_FS_2,
.mnt_point = "SDA:",
.fs_data = &test_data,
};
static struct fs_mount_t test_fs_mnt_already_mounted = {
.type = TEST_FS_2,
.mnt_point = TEST_FS_MNTP,
.fs_data = &test_data,
};
static struct fs_mount_t test_fs_mnt_invalid_parm = {
.type = TEST_FS_2,
.mnt_point = "/SDA",
.fs_data = &test_data,
};
static struct fs_mount_t test_fs_mnt_no_op = {
.type = TEST_FS_2,
.mnt_point = "/SDA:",
.fs_data = &test_data,
};
static struct fs_file_t filep;
static const char test_str[] = "hello world!";
/**
* @brief Test mount interface of filesystem
*
* @ingroup filesystem_api
*/
void test_mount(void)
{
int ret;
TC_PRINT("\nmount tests:\n");
TC_PRINT("Mount to a NULL directory\n");
ret = fs_mount(NULL);
zassert_not_equal(ret, 0, "Mount a NULL fs");
TC_PRINT("Mount to a unsupported directory\n");
ret = fs_mount(&test_fs_mnt_unsupported_fs);
zassert_not_equal(ret, 0, "Mount a unsupported fs");
fs_register(TEST_FS_2, &temp_fs);
TC_PRINT("Mount to an invalid directory\n");
ret = fs_mount(&test_fs_mnt_invalid_root);
zassert_not_equal(ret, 0, "Mount to an invalid dir");
TC_PRINT("Invalid parameter pass to file system operation interface\n");
ret = fs_mount(&test_fs_mnt_invalid_parm);
zassert_not_equal(ret, 0, "Mount with invalid parm");
ret = fs_mount(&test_fs_mnt_1);
zassert_equal(ret, 0, "Error mounting fs");
TC_PRINT("Mount to a directory that has file system mounted already\n");
ret = fs_mount(&test_fs_mnt_already_mounted);
zassert_not_equal(ret, 0, "Mount to a mounted dir");
fs_unregister(TEST_FS_2, &temp_fs);
fs_register(TEST_FS_2, &null_fs);
TC_PRINT("Mount a file system has no interface implemented\n");
ret = fs_mount(&test_fs_mnt_no_op);
zassert_not_equal(ret, 0, "Mount to a fs without op interface");
fs_unregister(TEST_FS_2, &null_fs);
}
/**
* @brief Test unmount interface of filesystem
*
* @ingroup filesystem_api
*/
void test_unmount(void)
{
int ret;
TC_PRINT("\nunmount tests:\n");
TC_PRINT("\nunmount nothing:\n");
ret = fs_unmount(NULL);
zassert_not_equal(ret, 0, "Unmount a NULL fs");
TC_PRINT("\nunmount file system that has never been mounted:\n");
ret = fs_unmount(&test_fs_mnt_unsupported_fs);
zassert_not_equal(ret, 0, "Unmount a never mounted fs");
TC_PRINT("\nunmount file system multiple times:\n");
ret = fs_unmount(&test_fs_mnt_1);
zassert_true(ret >= 0, "Fail to unmount fs");
test_fs_mnt_1.fs = &temp_fs;
ret = fs_unmount(&test_fs_mnt_1);
zassert_not_equal(ret, 0, "Unmount a unmounted fs");
}
/**
* @brief Test statvfs interface of filesystem
*
* @ingroup filesystem_api
*/
void test_file_statvfs(void)
{
struct fs_statvfs stat;
int ret;
ret = fs_statvfs(NULL, &stat);
zassert_not_equal(ret, 0, "Get valume without path");
ret = fs_statvfs("/SDCARD:", &stat);
zassert_not_equal(ret, 0, "Get valume by no-exist path");
ret = fs_statvfs(TEST_FS_MNTP, NULL);
zassert_not_equal(ret, 0, "Get valume without stat structure");
ret = fs_statvfs(TEST_FS_MNTP, &stat);
zassert_equal(ret, 0, "Error getting voluem stats");
TC_PRINT("\n");
TC_PRINT("Optimal transfer block size = %lu\n", stat.f_bsize);
TC_PRINT("Allocation unit size = %lu\n", stat.f_frsize);
TC_PRINT("Volume size in f_frsize units = %lu\n", stat.f_blocks);
TC_PRINT("Free space in f_frsize units = %lu\n", stat.f_bfree);
}
/**
* @brief Test make directory interface of filesystem
*
* @ingroup filesystem_api
*/
void test_mkdir(void)
{
int ret;
TC_PRINT("\nmkdir tests:\n");
ret = fs_mkdir(NULL);
zassert_not_equal(ret, 0, "Create a NULL directory");
ret = fs_mkdir("/SDCARD:/testdir");
zassert_not_equal(ret, 0, "Create dir in no fs mounted dir");
ret = fs_mkdir(TEST_FS_MNTP);
zassert_not_equal(ret, 0, "Shoult not create root dir");
ret = fs_mkdir(TEST_DIR);
zassert_equal(ret, 0, "Error creating dir");
TC_PRINT("Created dir %s!\n", TEST_DIR);
}
/**
* @brief Test open directory interface of filesystem
*
* @ingroup filesystem_api
*/
void test_opendir(void)
{
int ret;
struct fs_dir_t dirp;
TC_PRINT("\nopendir tests:\n");
memset(&dirp, 0, sizeof(dirp));
TC_PRINT("Test null path\n");
ret = fs_opendir(NULL, NULL);
zassert_not_equal(ret, 0, "Open NULL dir");
TC_PRINT("Test root directory\n");
ret = fs_opendir(&dirp, "/");
zassert_equal(ret, 0, "Fail to open root dir");
TC_PRINT("Test non-exist mount point\n");
ret = fs_opendir(&dirp, "/SDCARD:/test_dir");
zassert_not_equal(ret, 0, "Open dir in a unmounted fs");
ret = fs_opendir(&dirp, TEST_DIR);
zassert_equal(ret, 0, "Fail to open dir");
TC_PRINT("Open same directory multi times\n");
ret = fs_opendir(&dirp, TEST_DIR);
zassert_not_equal(ret, 0, "Can't reopen an opened dir");
TC_PRINT("Opening dir successfully\n");
}
/**
* @brief Test close directory interface of filesystem
*
* @ingroup filesystem_api
*/
void test_closedir(void)
{
int ret;
struct fs_dir_t dirp;
TC_PRINT("\nclosedir tests: %s\n", TEST_DIR);
memset(&dirp, 0, sizeof(dirp));
ret = fs_opendir(&dirp, TEST_DIR);
zassert_equal(ret, 0, "Fail to open dir");
ret = fs_closedir(&dirp);
zassert_equal(ret, 0, "Fail to close dir");
dirp.mp = &test_fs_mnt_1;
ret = fs_closedir(&dirp);
zassert_not_equal(ret, 0, "Should no close a closed dir");
}
static int _test_lsdir(const char *path)
{
int ret;
struct fs_dir_t dirp;
struct fs_dirent entry;
TC_PRINT("\nlsdir tests:\n");
memset(&dirp, 0, sizeof(dirp));
memset(&entry, 0, sizeof(entry));
TC_PRINT("read an unopened dir\n");
dirp.dirp = "somepath";
ret = fs_readdir(&dirp, &entry);
if (!ret) {
return TC_FAIL;
}
dirp.mp = &test_fs_mnt_1;
ret = fs_readdir(&dirp, NULL);
if (!ret) {
return TC_FAIL;
}
TC_PRINT("read an opened dir\n");
ret = fs_opendir(&dirp, path);
if (ret) {
if (path) {
TC_PRINT("Error opening dir %s [%d]\n", path, ret);
}
return TC_FAIL;
}
TC_PRINT("\nListing dir %s:\n", path);
for (;;) {
ret = fs_readdir(&dirp, &entry);
/* entry.name[0] == 0 means end-of-dir */
if (ret || entry.name[0] == 0) {
break;
}
if (entry.type == FS_DIR_ENTRY_DIR) {
TC_PRINT("[DIR ] %s\n", entry.name);
} else {
TC_PRINT("[FILE] %s (size = %zu)\n",
entry.name, entry.size);
}
}
ret = fs_closedir(&dirp);
if (ret) {
TC_PRINT("Error close a directory\n");
return TC_FAIL;
}
return ret;
}
/**
* @brief Test lsdir interface include opendir, readdir, closedir
*
* @ingroup filesystem_api
*/
void test_lsdir(void)
{
zassert_true(_test_lsdir(NULL) == TC_FAIL, NULL);
zassert_true(_test_lsdir("/") == TC_PASS, NULL);
zassert_true(_test_lsdir("/test") == TC_FAIL, NULL);
zassert_true(_test_lsdir(TEST_DIR) == TC_PASS, NULL);
}
/**
* @brief Open a existing file or create a new file
*
* @ingroup filesystem_api
*/
void test_file_open(void)
{
int ret;
TC_PRINT("\nOpen tests:\n");
TC_PRINT("\nOpen a file without a path\n");
ret = fs_open(&filep, NULL, FS_O_READ);
zassert_not_equal(ret, 0, "Open a NULL file");
TC_PRINT("\nOpen a file with wrong abs path\n");
ret = fs_open(&filep, "/test_file.txt", FS_O_READ);
zassert_not_equal(ret, 0, "Open a file with wrong path");
ret = fs_open(&filep, TEST_FILE, FS_O_READ);
zassert_equal(ret, 0, "Fail to open file");
TC_PRINT("\nReopen the same file");
ret = fs_open(&filep, TEST_FILE, FS_O_READ);
zassert_not_equal(ret, 0, "Reopen an opend file");
TC_PRINT("Opened file %s\n", TEST_FILE);
}
static int _test_file_write(void)
{
ssize_t brw;
int ret;
TC_PRINT("\nWrite tests:\n");
ret = fs_seek(&filep, 0, FS_SEEK_SET);
if (ret) {
TC_PRINT("fs_seek failed [%d]\n", ret);
fs_close(&filep);
return ret;
}
TC_PRINT("Write to file from a invalid source\n");
brw = fs_write(&filep, NULL, strlen(test_str));
if (brw >= 0) {
return TC_FAIL;
}
TC_PRINT("Data written:\"%s\"\n\n", test_str);
brw = fs_write(&filep, (char *)test_str, strlen(test_str));
if (brw < 0) {
TC_PRINT("Failed writing to file [%zd]\n", brw);
fs_close(&filep);
return brw;
}
if (brw < strlen(test_str)) {
TC_PRINT("Unable to complete write. Volume full.\n");
TC_PRINT("Number of bytes written: [%zd]\n", brw);
fs_close(&filep);
return TC_FAIL;
}
TC_PRINT("Data successfully written!\n");
return ret;
}
/**
* @brief Write items of data of size bytes long
*
* @ingroup filesystem_api
*
*/
void test_file_write(void)
{
zassert_true(_test_file_write() == TC_PASS, NULL);
}
static int _test_file_sync(void)
{
int ret;
ssize_t brw;
TC_PRINT("\nSync tests:\n");
ret = fs_open(&filep, TEST_FILE, FS_O_RDWR);
for (;;) {
brw = fs_write(&filep, (char *)test_str, strlen(test_str));
if (brw < strlen(test_str)) {
break;
}
ret = fs_sync(&filep);
if (ret) {
TC_PRINT("Error syncing file [%d]\n", ret);
fs_close(&filep);
return ret;
}
ret = fs_tell(&filep);
if (ret < 0) {
TC_PRINT("Error tell file [%d]\n", ret);
fs_close(&filep);
return ret;
}
}
TC_PRINT("Sync a overflowed file\n");
ret = fs_sync(&filep);
if (!ret) {
fs_close(&filep);
return TC_FAIL;
}
TC_PRINT("Tell a overflowed file\n");
ret = fs_tell(&filep);
if (!ret) {
fs_close(&filep);
return TC_FAIL;
}
fs_close(&filep);
return TC_PASS;
}
/**
* @brief Flush the cache of an open file
*
* @ingroup filesystem_api
*/
void test_file_sync(void)
{
zassert_true(_test_file_sync() == TC_PASS, NULL);
}
/**
* @brief Read items of data of size bytes long
*
* @ingroup filesystem_api
*/
void test_file_read(void)
{
ssize_t brw;
char read_buff[80];
size_t sz = strlen(test_str);
TC_PRINT("\nRead tests:\n");
TC_PRINT("Read to a invalid buffer\n");
brw = fs_read(&filep, NULL, sz);
zassert_false(brw >= 0, "Read data to a invalid buffer");
brw = fs_read(&filep, read_buff, sz);
zassert_true(brw >= 0, "Fail to read file");
read_buff[brw] = 0;
TC_PRINT("Data read:\"%s\"\n\n", read_buff);
zassert_true(strcmp(test_str, read_buff) == 0,
"Error - Data read does not match data written");
TC_PRINT("Data read matches data written\n");
}
static int _test_file_truncate(void)
{
int ret;
off_t orig_pos;
char read_buff[80];
ssize_t brw;
TC_PRINT("\nTruncate tests: max file size is 128byte\n");
TC_PRINT("Truncating to size larger than 128byte\n");
ret = fs_truncate(&filep, 256);
if (!ret) {
fs_close(&filep);
return TC_FAIL;
}
/* Test truncating to 0 size */
TC_PRINT("\nTesting shrink to 0 size\n");
ret = fs_truncate(&filep, 0);
if (ret) {
TC_PRINT("fs_truncate failed [%d]\n", ret);
fs_close(&filep);
return ret;
}
TC_PRINT("File seek from invalid whence\n");
ret = fs_seek(&filep, 0, 100);
if (!ret) {
fs_close(&filep);
return TC_FAIL;
}
fs_seek(&filep, 0, FS_SEEK_END);
if (fs_tell(&filep) > 0) {
TC_PRINT("Failed truncating to size 0\n");
fs_close(&filep);
return TC_FAIL;
}
TC_PRINT("Testing write after truncating\n");
ret = _test_file_write();
if (ret) {
TC_PRINT("Write failed after truncating\n");
return ret;
}
fs_seek(&filep, 0, FS_SEEK_END);
orig_pos = fs_tell(&filep);
TC_PRINT("Original size of file = %d\n", (int)orig_pos);
/* Test shrinking file */
TC_PRINT("\nTesting shrinking\n");
ret = fs_truncate(&filep, orig_pos - 5);
if (ret) {
TC_PRINT("fs_truncate failed [%d]\n", ret);
fs_close(&filep);
return ret;
}
fs_seek(&filep, 0, FS_SEEK_END);
TC_PRINT("File size after shrinking by 5 bytes = %d\n",
(int)fs_tell(&filep));
if (fs_tell(&filep) != (orig_pos - 5)) {
TC_PRINT("File size after fs_truncate not as expected\n");
fs_close(&filep);
return TC_FAIL;
}
/* Test expanding file */
TC_PRINT("\nTesting expanding\n");
fs_seek(&filep, 0, FS_SEEK_END);
orig_pos = fs_tell(&filep);
ret = fs_truncate(&filep, orig_pos + 10);
if (ret) {
TC_PRINT("fs_truncate failed [%d]\n", ret);
fs_close(&filep);
return ret;
}
fs_seek(&filep, 0, FS_SEEK_END);
TC_PRINT("File size after expanding by 10 bytes = %d\n",
(int)fs_tell(&filep));
if (fs_tell(&filep) != (orig_pos + 10)) {
TC_PRINT("File size after fs_truncate not as expected\n");
fs_close(&filep);
return TC_FAIL;
}
/* Check if expanded regions are zeroed */
TC_PRINT("Testing for zeroes in expanded region\n");
fs_seek(&filep, -5, FS_SEEK_END);
brw = fs_read(&filep, read_buff, 5);
if (brw < 5) {
TC_PRINT("Read failed after truncating\n");
fs_close(&filep);
return -1;
}
for (int i = 0; i < 5; i++) {
if (read_buff[i]) {
TC_PRINT("Expanded regions are not zeroed\n");
fs_close(&filep);
return TC_FAIL;
}
}
return TC_PASS;
}
/**
* @brief Truncate the file to the new length
*
* @details This test include three cases:
* - fs_seek, locate the position to truncate
* - fs_truncate
* - fs_tell, retrieve the current position
*
* @ingroup filesystem_api
*/
void test_file_truncate(void)
{
zassert_true(_test_file_truncate() == TC_PASS, NULL);
}
/**
* @brief Flush associated stream and close the file
*
* @ingroup filesystem_api
*
*/
void test_file_close(void)
{
int ret;
TC_PRINT("\nClose tests:\n");
ret = fs_close(&filep);
zassert_equal(ret, 0, "Fail to close file");
TC_PRINT("\nClose a closed file:\n");
filep.mp = &test_fs_mnt_1;
ret = fs_close(&filep);
zassert_not_equal(ret, 0, "Should not reclose a closed file");
TC_PRINT("Closed file %s\n", TEST_FILE);
}
/**
* @brief Rename a file or directory
*
* @ingroup filesystem_api
*/
void test_file_rename(void)
{
int ret = TC_FAIL;
TC_PRINT("\nRename file tests:\n");
ret = fs_rename(NULL, NULL);
zassert_not_equal(ret, 0, "Rename a NULL file");
ret = fs_rename("/SDCARD:/testfile.txt", TEST_FILE_RN);
zassert_not_equal(ret, 0, "Rename a non-exist file");
ret = fs_rename(TEST_FILE, "/SDCARD:/testfile_renamed.txt");
zassert_not_equal(ret, 0, "Rename file to different mount point");
ret = fs_rename(TEST_FILE, TEST_FILE_EX);
zassert_not_equal(ret, 0, "Rename file to an exist file");
ret = fs_rename(TEST_FILE, TEST_FILE_RN);
zassert_equal(ret, 0, "Fail to rename a file");
}
/**
* @brief Check the status of a file or directory specified by the path
*
* @ingroup filesystem_api
*/
void test_file_stat(void)
{
int ret;
struct fs_dirent entry;
TC_PRINT("\nStat file tests:\n");
ret = fs_stat(NULL, &entry);
zassert_not_equal(ret, 0, "Stat a NULL dir");
ret = fs_stat("/SDCARD", &entry);
zassert_not_equal(ret, 0, "Stat a non-exist dir");
ret = fs_stat(TEST_DIR, NULL);
zassert_not_equal(ret, 0, "Stat a dir without entry");
ret = fs_stat(TEST_DIR, &entry);
zassert_equal(ret, 0, "Fail to stat a dir");
ret = fs_stat(TEST_DIR_FILE, &entry);
zassert_equal(ret, 0, "Fail to stat a file");
}
/**
* @brief Delete the specified file or directory
*
* @ingroup filesystem_api
*
*/
void test_file_unlink(void)
{
int ret;
TC_PRINT("\nDelete tests:\n");
ret = fs_unlink(NULL);
zassert_not_equal(ret, 0, "Delete a NULL file");
ret = fs_unlink("/SDCARD:/test_file.txt");
zassert_not_equal(ret, 0, "Delete a non-exist file");
ret = fs_unlink(TEST_FS_MNTP);
zassert_not_equal(ret, 0, "Delete a root dir");
ret = fs_unlink(TEST_FILE_RN);
zassert_equal(ret, 0, "Fail to delete file");
TC_PRINT("File (%s) deleted successfully!\n", TEST_FILE_RN);
}

View file

@ -0,0 +1,124 @@
/*
* Copyright (c) 2020 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "test_fs.h"
/* amount of file system */
#define NUM_FS 2
#define TEST_FS_NAND1 "/NAND:"
#define TEST_FS_NAND2 "/MMCBLOCK:"
static struct test_fs_data test_data;
static struct fs_mount_t test_fs_mnt_1 = {
.type = TEST_FS_1,
.mnt_point = TEST_FS_NAND1,
.fs_data = &test_data,
};
static struct fs_mount_t test_fs_mnt_2 = {
.type = TEST_FS_2,
.mnt_point = TEST_FS_NAND2,
.fs_data = &test_data,
};
static int test_fs_init(void)
{
if (fs_register(TEST_FS_1, &temp_fs)) {
return -EINVAL;
}
if (fs_mount(&test_fs_mnt_1)) {
return -EINVAL;
}
if (fs_register(TEST_FS_2, &temp_fs)) {
return -EINVAL;
}
if (fs_mount(&test_fs_mnt_2)) {
return -EINVAL;
}
return 0;
}
static int test_fs_readmount(void)
{
int ret;
int mnt_nbr = 0;
const char *mnt_name;
do {
ret = fs_readmount(&mnt_nbr, &mnt_name);
if (ret < 0) {
break;
}
} while (true);
if (mnt_nbr == NUM_FS) {
return 0;
}
return TC_FAIL;
}
static int test_fs_deinit(void)
{
if (fs_unregister(TEST_FS_1, &temp_fs)) {
return -EINVAL;
}
if (fs_unmount(&test_fs_mnt_1)) {
return -EINVAL;
}
if (fs_unregister(TEST_FS_2, &temp_fs)) {
return -EINVAL;
}
if (fs_unmount(&test_fs_mnt_2)) {
return -EINVAL;
}
return 0;
}
static int test_fs_unsupported(void)
{
if (fs_register(FS_TYPE_END, &temp_fs) == 0) {
return TC_FAIL;
}
if (fs_unregister(FS_TYPE_END, &temp_fs) == 0) {
return TC_FAIL;
}
return 0;
}
/**
* @brief Multi file systems register and unregister
*
* @details register and unregister two file systems to test
* the system support multiple file system simultanously
*
* @addtogroup filesystem_api
*
* @{
*/
void test_fs_register(void)
{
zassert_true(test_fs_init() == 0, "Failed to register filesystems");
zassert_true(test_fs_readmount() == 0, "Failed to readmount");
zassert_true(test_fs_deinit() == 0, "Failed to unregister filesystems");
zassert_true(test_fs_unsupported() == 0, "Supported other file system");
}
/**
* @}
*/

View file

@ -0,0 +1,3 @@
tests:
filesystem.api:
tags: filesystem