fix: add fststfs api and unitest

add fststfs api and unitest

Change-Id: I5e9fedd1bc26b77f080faf2fd3f5bf2f4a1dd9f7
Signed-off-by: wcc0 <917033401@qq.com>
This commit is contained in:
wcc0 2021-07-30 16:46:50 +08:00
parent 2d8e59ccf3
commit 4c57aa26ad
10 changed files with 385 additions and 23 deletions

View File

@ -2444,4 +2444,47 @@ OUT:
return ret;
}
int SysFstatfs(int fd, struct statfs *buf)
{
int ret;
struct file *filep = NULL;
struct statfs bufRet = {0};
/* Process fd convert to system global fd */
fd = GetAssociatedSystemFd(fd);
ret = fs_getfilep(fd, &filep);
if (ret < 0) {
ret = -get_errno();
return ret;
}
ret = statfs(filep->f_path, &bufRet);
if (ret < 0) {
ret = -get_errno();
return ret;
}
ret = LOS_ArchCopyToUser(buf, &bufRet, sizeof(struct statfs));
if (ret != 0) {
ret = -EFAULT;
}
return ret;
}
int SysFstatfs64(int fd, size_t sz, struct statfs *buf)
{
int ret = 0;
if (sz != sizeof(struct statfs)) {
ret = -EINVAL;
return ret;
}
ret = SysFstatfs(fd, buf);
return ret;
}
#endif

View File

@ -257,6 +257,8 @@ extern int SysTruncate(const char *path, off_t length);
extern int SysFtruncate(int fd, off_t length);
extern int SysStatfs(const char *path, struct statfs *buf);
extern int SysStatfs64(const char *path, size_t sz, struct statfs *buf);
extern int SysFstatfs(int fd, struct statfs *buf);
extern int SysFstatfs64(int fd, size_t sz, struct statfs *buf);
extern int SysStat(const char *path, struct kstat *buf);
extern int SysLstat(const char *path, struct kstat *buffer);

View File

@ -72,6 +72,8 @@ SYSCALL_HAND_DEF(__NR_dup2, SysDup2, int, ARG_NUM_2)
SYSCALL_HAND_DEF(__NR_truncate, SysTruncate, int, ARG_NUM_7)
SYSCALL_HAND_DEF(__NR_ftruncate, SysFtruncate, int, ARG_NUM_7)
SYSCALL_HAND_DEF(__NR_statfs, SysStatfs, int, ARG_NUM_2)
SYSCALL_HAND_DEF(__NR_fstatfs, SysFstatfs, int, ARG_NUM_2)
SYSCALL_HAND_DEF(__NR_fstatfs64, SysFstatfs64, int, ARG_NUM_3)
SYSCALL_HAND_DEF(__NR_stat, SysStat, int, ARG_NUM_2)
SYSCALL_HAND_DEF(__NR_lstat, SysLstat, int, ARG_NUM_2)
SYSCALL_HAND_DEF(__NR_fstat, SysFstat, int, ARG_NUM_2)

View File

@ -207,6 +207,10 @@ sources_pressure = [
]
sources_full = [
"jffs/full/it_test_fs_jffs_001.cpp",
"jffs/full/it_test_fs_jffs_002.cpp",
"jffs/full/it_test_fs_jffs_003.cpp",
"jffs/full/it_test_fs_jffs_004.cpp",
"jffs/full/It_jffs_001.cpp",
"jffs/full/It_jffs_002.cpp",
"jffs/full/It_jffs_003.cpp",

View File

@ -96,6 +96,7 @@ constexpr const char* JFFS_CHINESE_NAME1 = "<22><><EFBFBD>Ϻ<EFBFBD>";
constexpr const char* JFFS_BASE_DIR = "/";
#endif
#define JFFS_FILE_MODE 0755
#define JFFS_MAIN_DIR0 "/storage"
#define JFFS_MOUNT_DIR0 "/storage"
#define JFFS_MAIN_DIR1 "/storage1"
@ -266,6 +267,10 @@ VOID ItFsJffs535(VOID);
#endif
#if defined(LOSCFG_USER_TEST_FULL)
void ItTestFsJffs001(void);
void ItTestFsJffs002(void);
void ItTestFsJffs003(void);
void ItTestFsJffs004(void);
VOID ItJffs001(VOID);
VOID ItJffs002(VOID);
VOID ItJffs003(VOID);

View File

@ -28,11 +28,14 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_fs_jffs.h"
#include "It_vfs_jffs.h"
#define FILEPATH "/storage/test.txt"
#define FILEPATH "/storage/testfdopen.txt"
#define TESTFILE "/storage/testfopen"
#define MAXFD 512
#define MINFD 8
static int TestCase(void)
static int TestCase0(void)
{
FILE *fp = NULL;
int fd = -1;
@ -40,7 +43,7 @@ static int TestCase(void)
char rdbuf[10U] = {0};
int ret = -1;
fd = open(FILEPATH, O_RDWR | O_EXCL | O_CREAT, JFFS_FILE_MODE);
fd = open(FILEPATH, O_RDWR);
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_IS_ERROR, fd, EXIT);
/* r+ */
@ -58,11 +61,12 @@ static int TestCase(void)
ret = fclose(fp);
ICUNIT_GOTO_NOT_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
fp = NULL;
/* a+ /appen + rw */
fd = open(FILEPATH, O_RDWR);
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_IS_ERROR, fd, EXIT);
/* a+ */
fp = fdopen(fd, "a+");
ICUNIT_GOTO_NOT_EQUAL(fp, JFFS_TO_NULL, fp, EXIT);
@ -77,14 +81,135 @@ static int TestCase(void)
ret = fclose(fp);
ICUNIT_GOTO_NOT_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
fp = NULL;
ret = remove(FILEPATH);
/* r / only read */
fd = open(FILEPATH, O_RDWR);
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_IS_ERROR, fd, EXIT);
fp = fdopen(fd, "r");
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_TO_NULL, fd, EXIT);
ret = fwrite(buf, 10u, 1, fp);
ICUNIT_GOTO_NOT_EQUAL(ret, 1, ret, EXIT);
ret = fclose(fp);
ICUNIT_GOTO_NOT_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
fp = NULL;
/* w / only write */
fd = open(FILEPATH, O_RDWR);
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_IS_ERROR, fd, EXIT);
fp = fdopen(fd, "w");
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_TO_NULL, fd, EXIT);
ret = fread(rdbuf, 10u, 1, fp);
ICUNIT_GOTO_NOT_EQUAL(ret, 1, ret, EXIT);
ret = fclose(fp);
ICUNIT_GOTO_NOT_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
fp = NULL;
/* w+ */
fd = open(FILEPATH, O_RDWR);
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_IS_ERROR, fd, EXIT);
fp = fdopen(fd, "w+");
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_TO_NULL, fd, EXIT);
ret = fwrite(buf, 10u, 1, fp);
ICUNIT_GOTO_NOT_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
ret = fseek(fp, 0, SEEK_SET);
ICUNIT_GOTO_NOT_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
ret = fread(rdbuf, 10u, 1, fp);
ICUNIT_GOTO_NOT_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
ret = fclose(fp);
ICUNIT_GOTO_NOT_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
fp = NULL;
/* a */
fd = open(FILEPATH, O_WRONLY);
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_IS_ERROR, fd, EXIT);
fp = fdopen(fd, "a");
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_TO_NULL, fd, EXIT);
ret = fwrite(buf, 10u, 1, fp);
ICUNIT_GOTO_NOT_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
ret = fseek(fp, 0, SEEK_SET);
ICUNIT_GOTO_NOT_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
ret = fread(rdbuf, 10u, 1, fp);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = fclose(fp);
return JFFS_NO_ERROR;
EXIT:
fclose(fp);
if(fp != NULL) {
fclose(fp);
} else {
remove(FILEPATH);
}
return JFFS_IS_ERROR;
}
/* error */
static int TestCase1(void)
{
FILE *fp = NULL;
int fd = -1;
int errFd = -1;
int i = 0;
fd = open(FILEPATH, O_RDWR, JFFS_FILE_MODE);
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_IS_ERROR, fd, EXIT);
/* EIVNAL */
fp = fdopen(fd, "hello");
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_TO_NULL, fd, EXIT);
ICUNIT_GOTO_EQUAL(errno, EINVAL, errno, EXIT);
close(fd);
remove(TESTFILE);
return JFFS_NO_ERROR;
EXIT:
close(fd);
remove(TESTFILE);
return JFFS_IS_ERROR;
}
static int TestCase(void)
{
int ret = -1;
int fd = -1;
fd = open(FILEPATH, O_RDWR | O_EXCL | O_CREAT, JFFS_FILE_MODE);
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_IS_ERROR, fd, EXIT);
ret = TestCase0();
ICUNIT_GOTO_EQUAL(ret, JFFS_TO_NULL, ret, EXIT);
ret = TestCase1();
ICUNIT_GOTO_EQUAL(ret, JFFS_TO_NULL, ret, EXIT);
close(fd);
remove(FILEPATH);
return JFFS_NO_ERROR;
EXIT:
close(fd);
remove(FILEPATH);
return JFFS_IS_ERROR;

View File

@ -28,36 +28,86 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_fs_jffs.h"
#include "It_vfs_jffs.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/statvfs.h>
#define FILEPATH "/storage/test.txt"
#define FILEPATHLEN (strlen(FILEPATH) + 1U)
#define FILEPATH "/storage/testfstatvfs.txt"
#define FILEPATHLEN (strlen(FILEPATH) + 1U)
static int TestCase(void)
static int TestCase0(void)
{
INT32 fd = -1;
INT32 ret = 0;
CHAR pathname[FILEPATHLEN] = FILEPATH;
struct statvfs fileInfo = {0};
fd = open(pathname, O_CREAT | O_RDWR | O_EXCL, JFFS_FILE_MODE);
ICUNIT_GOTO_NOT_EQUAL(fd, -1, fd, EXIT);
fd=open(FILEPATH, O_RDWR, JFFS_FILE_MODE);
ICUNIT_GOTO_NOT_EQUAL(fd, 1, fd, EXIT);
ret = fstatvfs(fd, &fileInfo);
ret= fstatvfs(fd, &fileInfo);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
/*ICUNIT_GOTO_EQUAL(fileInfo.f_flag, 0, fileInfo.f_flag, EXIT);*/
close(fd);
remove(pathname);
return JFFS_NO_ERROR;
EXIT:
close(fd);
remove(pathname);
return JFFS_IS_ERROR;
}
static int TestCase1(void)
{
INT32 fd = -1;
INT32 ret= 0;
INT32 errFd = -1;
struct statvfs fileInfo = {0};
fd = open(FILEPATH, O_RDWR, JFFS_FILE_MODE);
ICUNIT_GOTO_NOT_EQUAL(fd, JFFS_IS_ERROR, fd, EXIT);
/* EBADF */
ret=fstatvfs(errFd, &fileInfo);
ICUNIT_GOTO_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
ICUNIT_GOTO_EQUAL(errno, EBADF, errno, EXIT);
/* EBADF(When the file does not exist, an error occurs when the file is
converted from the fd file to the system FD file.) */
close(fd);
ret= remove(FILEPATH);
ICUNIT_GOTO_EQUAL(ret, JFFS_NO_ERROR, ret, EXIT);
errno =0;
ret=fstatvfs(fd, &fileInfo);
ICUNIT_GOTO_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
ICUNIT_GOTO_EQUAL(errno, EBADF, errno, EXIT);
close(fd);
return JFFS_NO_ERROR;
EXIT:
close(fd);
return JFFS_IS_ERROR;
}
static int TestCase(void)
{
INT32 fd=-1;
int ret =-1;
fd=open(FILEPATH, O_CREAT | O_RDWR | O_EXCL, JFFS_FILE_MODE);
ICUNIT_GOTO_NOT_EQUAL(fd, 1, fd, EXIT);
close(fd);
ret= TestCase0();
ICUNIT_GOTO_EQUAL(ret, JFFS_NO_ERROR, ret, EXIT);
ret= TestCase1();
ICUNIT_GOTO_EQUAL(ret, JFFS_NO_ERROR, ret, EXIT);
remove(FILEPATH);
return JFFS_NO_ERROR;
EXIT:
remove(FILEPATH);
return JFFS_IS_ERROR;
}

View File

@ -28,12 +28,13 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_fs_jffs.h"
#include "It_vfs_jffs.h"
#define DIRPATH1 "/storage/test1"
#define DIRPATH2 "/storage/test2"
#define TEST_MAXPATHLEN 4098
static int TestCase(void)
static int TestCase0(void)
{
INT32 ret = -1;
INT32 dirFd = -1;
@ -90,6 +91,46 @@ EXIT:
return JFFS_NO_ERROR;
}
static int TestCase1(void)
{
int ret = -1;
char pathName[TEST_MAXPATHLEN] = {0};
int errFd = -1;
/* ENAMETOOLONG */
ret = memset_s(pathName, TEST_MAXPATHLEN, 1, TEST_MAXPATHLEN);
ICUNIT_GOTO_EQUAL(ret, JFFS_NO_ERROR, ret, EXIT);
ret = renameat(AT_FDCWD, pathName, AT_FDCWD, pathName);
ICUNIT_GOTO_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
ICUNIT_GOTO_EQUAL(errno, ENAMETOOLONG, ret, EXIT);
/* EINVAL */
ret = renameat(AT_FDCWD, "\0", AT_FDCWD, "\0");
ICUNIT_GOTO_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
ICUNIT_GOTO_EQUAL(errno, EINVAL, errno, EXIT);
/* ENOENT */
ret = renameat(errFd, "/storage/test.txt", errFd, "/storage/test1.txt");
ICUNIT_GOTO_EQUAL(ret, JFFS_IS_ERROR, ret, EXIT);
ICUNIT_GOTO_EQUAL(errno, ENOENT, ret, EXIT);
return JFFS_NO_ERROR;
EXIT:
return JFFS_IS_ERROR;
}
static int TestCase(void)
{
int ret = -1;
ret = TestCase0();
ICUNIT_GOTO_EQUAL(ret, JFFS_NO_ERROR, ret, EXIT);
ret = TestCase1();
ICUNIT_GOTO_EQUAL(ret, JFFS_NO_ERROR, ret, EXIT);
return JFFS_NO_ERROR;
EXIT:
return JFFS_IS_ERROR;
}
void ItTestFsJffs003(void)
{
TEST_ADD_CASE("It_Test_Fs_Jffs_003", TestCase, TEST_VFS, TEST_JFFS, TEST_LEVEL0, TEST_FUNCTION);

View File

@ -28,11 +28,14 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_fs_jffs.h"
#include "It_vfs_jffs.h"
#define MOUNT_FILEPATH "/storage/mounts"
#define TESTFILE "/storage/hellomnt"
#define MAXFD 512
#define MINFD 8
static int TestCase(void)
static int TestCase0(void)
{
CHAR pathname1[JFFS_STANDARD_NAME_LENGTH] = MOUNT_FILEPATH;
struct mntent *mnt = NULL;
@ -65,11 +68,54 @@ static int TestCase(void)
ICUNIT_GOTO_EQUAL(mnt->mnt_freq, mountsData.mnt_freq, mnt.mnt_freq, EXIT);
ICUNIT_GOTO_EQUAL(mnt->mnt_passno, mountsData.mnt_passno, mnt.mnt_passno, EXIT);
endmntent(fp);
return JFFS_NO_ERROR;
EXIT:
endmntent(fp);
return JFFS_IS_ERROR;
}
static int TestCase1(void)
{
int ret = -1;
FILE *fp = NULL;
int i = 0;
int fd =-1;
/* EINVAL */
fp = setmntent(MOUNT_FILEPATH, "+r");
ICUNIT_GOTO_EQUAL(fp, NULL, fp, EXIT);
ICUNIT_GOTO_EQUAL(errno, EINVAL, fp, EXIT);
/* EISDIR */
fp = setmntent("./", "r+");
ICUNIT_GOTO_EQUAL(fp, NULL, fp, EXIT);
ICUNIT_GOTO_EQUAL(errno, EISDIR, fp, EXIT);
/* ENOENT */
fp = setmntent("/storage/testmnt", "r+");
ICUNIT_GOTO_EQUAL(fp, NULL, fp, EXIT);
ICUNIT_GOTO_EQUAL(errno, ENOENT, fp, EXIT);
remove(TESTFILE);
return JFFS_NO_ERROR;
EXIT:
remove(TESTFILE);
return JFFS_IS_ERROR;
}
static int TestCase(void)
{
int ret = -1;
ret = TestCase0();
ICUNIT_GOTO_EQUAL(ret, JFFS_NO_ERROR, ret, EXIT);
ret = TestCase1();
ICUNIT_GOTO_EQUAL(ret, JFFS_NO_ERROR, ret, EXIT);
return JFFS_NO_ERROR;
EXIT:
return JFFS_IS_ERROR;
}
void ItTestFsJffs004(void)
{
TEST_ADD_CASE("It_Test_Fs_Jffs_004", TestCase, TEST_VFS, TEST_JFFS, TEST_LEVEL0, TEST_FUNCTION);

View File

@ -999,6 +999,50 @@ public:
static void TearDownTestCase(void) {}
};
#if defined(LOSCFG_USER_TEST_FULL)
/* *
* @tc.name: ItTestFsJffs001
* @tc.desc: function for VfsJffsTest
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(VfsJffsTest, ItTestFsJffs001, TestSize.Level0)
{
ItTestFsJffs001();
}
/* *
* @tc.name: ItTestFsJffs002
* @tc.desc: function for VfsJffsTest
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(VfsJffsTest, ItTestFsJffs002, TestSize.Level0)
{
ItTestFsJffs002();
}
/* *
* @tc.name: ItTestFsJffs003
* @tc.desc: function for VfsJffsTest
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(VfsJffsTest, ItTestFsJffs003, TestSize.Level0)
{
ItTestFsJffs003();
}
/* *
* @tc.name: ItTestFsJffs004
* @tc.desc: function for VfsJffsTest
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(VfsJffsTest, ItTestFsJffs004, TestSize.Level0)
{
ItTestFsJffs004();
}
/* *
* @tc.name: IT_JFFS_002
* @tc.desc: function for VfsJffsTest