add cm_fs_test source code

Signed-off-by: vela-mib <vela-mib@xiaomi.com>
This commit is contained in:
vela-mib 2024-03-08 14:38:37 +08:00 committed by Xiang Xiao
parent 663b54e833
commit fec49af501
36 changed files with 5102 additions and 0 deletions

View File

@ -24,6 +24,11 @@ config TESTS_TESTSUITES_STACKSIZE
int "Testsuites stack size"
default 16384
config CM_FS_TEST
bool "enable fs test"
default n
depends on TESTS_TESTSUITES
config CM_SCHED_TEST
bool "enbale schedule test"
default n

View File

@ -19,6 +19,15 @@ PRIORITY = $(CONFIG_TESTS_TESTSUITES_PRIORITY)
STACKSIZE = $(CONFIG_TESTS_TESTSUITES_STACKSIZE)
MODULE = $(CONFIG_TESTS_TESTSUITES)
ifneq ($(CONFIG_CM_FS_TEST),)
CFLAGS += -I$(CURDIR)/kernel/fs/include
CFLAGS += -I$(APPDIR)/nshlib
CSRCS += $(wildcard kernel/fs/cases/*.c)
CSRCS += $(wildcard kernel/fs/common/*.c)
PROGNAME += cmocka_fs_test
MAINSRC += $(CURDIR)/kernel/fs/cmocka_fs_test.c
endif
ifneq ($(CONFIG_CM_SCHED_TEST),)
CFLAGS += -I$(CURDIR)/kernel/sched/include
CSRCS += $(wildcard kernel/sched/cases/*.c)

View File

@ -0,0 +1,101 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_append_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <errno.h>
#include <cmocka.h>
#include "fstest.h"
#define TESTFILENAME "stream01Testfile"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stream
* Example description:
* 1. open a file with "a+".
* 2. Write some strings to the file.
* 3. Check if the file pointer is offset.
* Test item: fopen() fseek() ftell()
* Expect results: TEST PASSED
****************************************************************************/
void test_nuttx_fs_append01(FAR void **state)
{
FILE *fd;
int pos;
char readstring[80];
size_t ret;
fd = fopen(TESTFILENAME, "a+");
if (fd == NULL)
{
syslog(LOG_ERR, "Unable to open file %s, errno %d\n",
TESTFILENAME, errno);
assert_true(1 == 0);
}
fprintf(fd, "This is a test of the append.\n");
pos = ftell(fd);
if (fseek(fd, 0, SEEK_END) < 0)
{
syslog(LOG_ERR, "fseek fail, errno %d\n", errno);
fclose(fd);
assert_true(1 == 0);
}
if (ftell(fd) != pos)
{
syslog(LOG_ERR, "Error opening for append ... data not at EOF\n");
fclose(fd);
assert_true(1 == 0);
}
if (fseek(fd, -30, SEEK_END) < 0)
{
syslog(LOG_ERR, "fseek fail, errno %d\n", errno);
fclose(fd);
assert_true(1 == 0);
}
readstring[30] = '\0';
ret = fread(readstring, 1, 30, fd);
readstring[ret] = 0;
fclose(fd);
if (strcmp(readstring, "This is a test of the append.\n") != 0)
{
syslog(LOG_ERR, "strcmp fail\n");
assert_true(1 == 0);
}
}

View File

@ -0,0 +1,78 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_creat_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILENAME "creatTestFile"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_creat01
****************************************************************************/
void test_nuttx_fs_creat01(FAR void **state)
{
int fd;
int ret;
char buf[20] = {
0
};
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
sprintf(test_state->filename, "%s", TESTFILENAME);
memset(buf, 'A', sizeof(buf));
/* creat a test file */
fd = creat(test_state->filename, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
/* do write */
ret = write(fd, buf, sizeof(buf));
assert_int_in_range(ret, 1, 20);
}

View File

@ -0,0 +1,112 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_dup2_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <errno.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILE "testDup2File1"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_dup201
****************************************************************************/
void test_nuttx_fs_dup201(FAR void **state)
{
char buf[16] = {
0
};
off_t currpos;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
int fd1 = open(TESTFILE, O_RDWR | O_CREAT, 0777);
assert_true(fd1 > 0);
test_state->fd1 = fd1;
/* open file */
int fd2 = open(TESTFILE, O_RDWR | O_CREAT, 0777);
assert_true(fd2 > 0);
test_state->fd2 = fd2;
/* do dup2 */
int ret = dup2(fd1, fd2);
assert_int_not_equal(ret, -1);
char *buf1 = "hello ";
char *buf2 = "world!";
/* do write */
int ret2;
ret2 = write(fd1, buf1, strlen(buf1));
assert_int_in_range(ret2, 1, strlen(buf1));
ret2 = write(fd2, buf2, strlen(buf2));
assert_int_in_range(ret2, 1, strlen(buf2));
/* refresh to storage */
assert_int_equal(fsync(fd1), 0);
/* reset file pos use fd2 */
lseek(fd2, 0, SEEK_SET);
/* check if file pos is shared */
currpos = lseek(fd1, 0, SEEK_CUR);
assert_int_equal(currpos, 0);
/* read file */
ret = read(fd1, buf, 12);
assert_int_equal(ret, 12);
/* check buf */
ret = strncmp(buf, "hello world!", 12);
assert_int_equal(ret, 0);
}

View File

@ -0,0 +1,111 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_dup_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <errno.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILENAME "testDupFile"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_dup01
****************************************************************************/
void test_nuttx_fs_dup01(FAR void **state)
{
int fd;
int newfd;
int rval;
char buf_fd[5] = "hello";
char buf_new_fd[8] = "littleFS";
char read_buf[20] = "";
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TESTFILENAME, O_RDWR | O_CREAT | O_APPEND, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
/* do write */
assert_int_in_range(write(fd, buf_fd, sizeof(buf_fd)), 1, sizeof(buf_fd));
/* refresh to storage */
assert_int_equal(fsync(fd), 0);
/* do dup */
newfd = dup(fd);
close(fd);
assert_int_not_equal(newfd, -1);
test_state->fd2 = newfd;
/* check if file pos is shared */
off_t currpos = lseek(newfd, 0, SEEK_CUR);
assert_int_equal(currpos, 5);
/* write newfd after dup */
rval = write(newfd, buf_new_fd, sizeof(buf_new_fd));
assert_int_in_range(rval, 1, sizeof(buf_new_fd));
/* refresh to storage */
assert_int_equal(fsync(newfd), 0);
/* reset file pos use newfd */
off_t ret = lseek(newfd, 0, SEEK_SET);
assert_int_equal(ret, 0);
/* do double check */
rval = read(newfd, read_buf, 20);
assert_int_in_range(rval, 1, 20);
/* check read_buf */
assert_int_equal(strncmp(read_buf, "hellolittleFS", 13), 0);
}

View File

@ -0,0 +1,92 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_eventfd_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <pthread.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <sys/eventfd.h>
#include "fstest.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: thread_func
****************************************************************************/
__attribute__((unused)) static void *thread_func(void *args)
{
eventfd_t eventfd01_buffer;
for (int i = 1; i < 6; i++)
{
read((int)args, &eventfd01_buffer, sizeof(eventfd_t));
sleep(1);
}
return 0;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_eventfd
****************************************************************************/
void test_nuttx_fs_eventfd(FAR void **state)
{
#ifdef CONFIG_EVENT_FD
eventfd_t eventfd01_buf = 1;
int eventfd01_ret;
int eventfd01_efd;
pthread_t eventfd01_tid;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
eventfd01_efd = eventfd(0, 0);
assert_int_not_equal(eventfd01_efd, -1);
test_state->fd1 = eventfd01_efd;
assert_true(pthread_create(&eventfd01_tid, NULL,
thread_func, &eventfd01_efd) >= 0);
for (int i = 1; i < 5; i++)
{
eventfd01_ret = write(eventfd01_efd, &eventfd01_buf,
sizeof(eventfd_t));
assert_int_equal(eventfd01_ret, sizeof(eventfd_t));
eventfd01_buf++;
sleep(1);
}
sleep(2);
#endif
}

View File

@ -0,0 +1,240 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_fcntl_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define BUFSIZE 512
#define TEST_FILE_1 "fcntl01_testfile"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_fcntl01
****************************************************************************/
void test_nuttx_fs_fcntl01(FAR void **state)
{
char *buf = NULL;
int oldfd;
int newfd;
int ret;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
oldfd = open(TEST_FILE_1, O_CREAT | O_RDWR, 0700);
assert_true(oldfd > 0);
test_state->fd1 = oldfd;
/* do fcntl */
newfd = fcntl(oldfd, F_DUPFD, 0);
assert_true(newfd > 0);
test_state->fd2 = newfd;
/* malloc memory */
buf = (char *)malloc(BUFSIZ);
assert_non_null(buf);
test_state->ptr = buf;
/* set memory */
memset(buf, 'A', BUFSIZ);
/* do write */
ret = write(newfd, buf, BUFSIZ);
assert_int_in_range(ret, 1, BUFSIZ);
}
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE_2 "fcntl02_testfile"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_fcntl02
****************************************************************************/
void test_nuttx_fs_fcntl02(FAR void **state)
{
int fd;
int ret;
int v;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TEST_FILE_2, O_RDWR | O_CREAT, 0700);
assert_int_in_range(fd, 0, 255);
/* do fcntl */
v = fcntl(fd, F_GETFD);
assert_int_in_range(v, 0, 255);
test_state->fd1 = fd;
v |= FD_CLOEXEC;
/* do fcntl */
ret = fcntl(fd, F_SETFD, v);
assert_int_in_range(ret, 0, 255);
test_state->fd2 = ret;
ret = (v == fcntl(fd, F_GETFD) ? 1 : 0);
assert_int_equal(ret, 1);
}
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: fs_fcntl_set_lile_status
****************************************************************************/
static int fs_fcntl_set_lile_status(int mode, int fd)
{
int flags;
int ret;
flags = fcntl(fd, F_GETFL);
if (flags == -1)
{
syslog(LOG_ERR, "get file status fail !\n");
return -1;
}
flags |= mode;
ret = fcntl(fd, F_SETFL, flags);
if (ret < 0)
{
syslog(LOG_ERR, "set file status fail !\n");
}
return 0;
}
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE_3 "fcntl03_testfile_1"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_fcntl03
****************************************************************************/
void test_nuttx_fs_fcntl03(FAR void **state)
{
int fd;
int ret;
int size;
int ret2;
char path[32] = {
0
};
char buf[10] = {
0
};
getcwd(path, sizeof(path));
/* open file */
fd = open(TEST_FILE_3, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
/* set memory */
memset(buf, 'A', 10);
/* do write */
ret2 = write(fd, buf, 10);
assert_int_in_range(ret2, 1, 10);
/* close file */
assert_int_equal(close(fd), 0);
sleep(1);
/* open the file again */
fd = open(TEST_FILE_3, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
/* F_SETFL */
ret = fs_fcntl_set_lile_status(O_APPEND, fd);
assert_int_equal(ret, 0);
/* set memory */
memset(buf, 'B', 10);
/* do write */
ret2 = write(fd, buf, 10);
assert_int_in_range(ret2, 1, 10);
/* get fd size */
size = lseek(fd, 0, SEEK_END);
/* close file */
close(fd);
/* check size */
assert_int_equal(size, 20);
}

View File

@ -0,0 +1,130 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_fstat_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <fcntl.h>
#include <syslog.h>
#include <sys/socket.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE_1 "fstat_test_file1"
#define TEST_FILE_2 "fstat_test_file2"
#define BUF_SIZE 512
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_fstat01
****************************************************************************/
void test_nuttx_fs_fstat01(FAR void **state)
{
struct stat file_s;
int fd;
int ret;
char *buf;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TEST_FILE_1, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
/* malloc memory */
buf = malloc(BUF_SIZE);
assert_non_null(buf);
test_state->ptr = buf;
/* set memory */
memset(buf, 'A', BUF_SIZE);
/* get the file size before write */
ret = fstat(fd, &file_s);
assert_int_equal(ret, 0);
/* do write */
ret = write(fd, buf, BUF_SIZE);
assert_int_in_range(ret, 1, BUF_SIZE);
/* get file size again */
ret = fstat(fd, &file_s);
assert_int_equal(ret, 0);
/* check file_s.st_size */
ret = (file_s.st_size == BUF_SIZE) ? 1 : 0;
assert_int_equal(ret, 1);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_fstat02
****************************************************************************/
void test_nuttx_fs_fstat02(FAR void **state)
{
int fd;
int ret;
struct stat file_s;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TEST_FILE_2, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
/* get the file size before write */
ret = fstat(fd, &file_s);
assert_int_equal(ret, 0);
/* close file */
assert_int_equal(close(fd), 0);
}

View File

@ -0,0 +1,104 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_fstatfs_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <stdio.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE "fstat_test_file"
#define BUF_SIZE 512
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_print_statfs
****************************************************************************/
__attribute__((unused)) static void test_nuttx_fs_print_statfs
(struct statfs *buf)
{
syslog(LOG_INFO, "statfs buffer:\n");
syslog(LOG_INFO, " f_type: %lu\n", (unsigned long)buf->f_type);
syslog(LOG_INFO, " f_namelen: %lu\n", (unsigned long)buf->f_namelen);
syslog(LOG_INFO, " f_bsize: %lu\n", (unsigned long)buf->f_bsize);
syslog(LOG_INFO, " f_blocks: %llu\n",
(unsigned long long)buf->f_blocks);
syslog(LOG_INFO, " f_bfree: %llu\n", (unsigned long long)buf->f_bfree);
syslog(LOG_INFO, " f_bavail: %llu\n",
(unsigned long long)buf->f_bavail);
syslog(LOG_INFO, " f_files: %llu\n", (unsigned long long)buf->f_files);
syslog(LOG_INFO, " f_ffree: %llu\n", (unsigned long long)buf->f_ffree);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_fstatfs01
****************************************************************************/
void test_nuttx_fs_fstatfs01(FAR void **state)
{
struct statfs statfsbuf;
int ret;
int fd;
char *buf;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* malloc memory */
buf = malloc(BUF_SIZE);
assert_non_null(buf);
test_state->ptr = buf;
/* set memory */
memset(buf, 'B', BUF_SIZE);
/* open file */
fd = open(TEST_FILE, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
/* call fstatfs() */
ret = fstatfs(fd, &statfsbuf);
assert_int_equal(ret, 0);
}

View File

@ -0,0 +1,145 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_fsync_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <time.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILE "FsyncTestFile"
#define BUF "testData123#$%*-=/ sdafasd37575sasdfasdf3563456345" \
"63456ADSFASDFASDFQWREdf4as5df4as5dfsd ###"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_fsync01
****************************************************************************/
void test_nuttx_fs_fsync01(FAR void **state)
{
int fd;
int rval;
int ret;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TESTFILE, O_RDWR | O_CREAT, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
for (int i = 0; i < 20; i++)
{
/* do write */
rval = write(fd, BUF, sizeof(BUF));
assert_int_equal(rval, sizeof(BUF));
/* refresh to storage */
ret = fsync(fd);
assert_int_equal(ret, 0);
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_fsync02
****************************************************************************/
void test_nuttx_fs_fsync02(FAR void **state)
{
int fd;
int ret;
char *buf = NULL;
int bufsize = 4096;
ssize_t writen = 0;
struct statfs statfsbuf;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* delete test file */
unlink(TESTFILE);
/* open file */
fd = open(TESTFILE, O_CREAT | O_RDWR, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
/* call fstatfs() */
ret = fstatfs(fd, &statfsbuf);
assert_int_equal(ret, 0);
bufsize = statfsbuf.f_bsize;
syslog(LOG_INFO, "the bsize = %d\n", statfsbuf.f_bsize);
/* malloc memory */
buf = malloc(bufsize * sizeof(char));
assert_non_null(buf);
test_state->ptr = buf;
/* set memory */
memset(buf, 0x66, bufsize);
/* do write */
writen = write(fd, buf, bufsize);
assert_int_in_range(writen, 1, bufsize);
/* refresh to storage */
fsync(fd);
/* call fstatfs() */
ret = fstatfs(fd, &statfsbuf);
assert_int_equal(ret, 0);
}

View File

@ -0,0 +1,106 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_getfilep_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE "fstat_test_file"
#define BUF_SIZE 512
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_getfilep01
****************************************************************************/
void test_nuttx_fs_getfilep01(FAR void **state)
{
FAR struct file *filep;
int ret;
int fd;
char *buf = NULL;
FILE *fp;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* create a file for testing */
fd = creat(TEST_FILE, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
/* fdopen file */
fp = fdopen(fd, "r+");
assert_non_null(fp);
/* get struct file */
ret = fs_getfilep(fileno(fp), &filep);
assert_int_equal(ret, 0);
/* malloc memory */
buf = malloc(BUF_SIZE);
assert_non_null(buf);
test_state->ptr = buf;
/* set memory */
memset(buf, 'A', BUF_SIZE);
/* do write */
ret = write(fileno(fp), buf, BUF_SIZE);
assert_int_equal(ret, BUF_SIZE);
/* do fflush */
fflush(fp);
/* do fsync */
fsync(fileno(fp));
/* get struct file again */
ret = fs_getfilep(fileno(fp), &filep);
assert_int_equal(ret, 0);
assert_int_equal(filep->f_pos, BUF_SIZE);
test_state->fd2 = fileno(fp);
assert_int_equal(fclose(fp), 0);
}

View File

@ -0,0 +1,84 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_mkdir_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_mkdir01
****************************************************************************/
void test_nuttx_fs_mkdir01(FAR void **state)
{
int status;
/* do mkdir */
status = mkdir("testdir1", 0700);
assert_int_equal(status, 0);
/* do rmdir */
assert_int_equal(rmdir("testdir1"), 0);
/* do mkdir */
status = mkdir("234123412341234", 0700);
assert_int_equal(status, 0);
/* do rmdir */
assert_int_equal(rmdir("234123412341234"), 0);
/* do mkdir */
status = mkdir("asdfasdfASDFASDF", 0700);
assert_int_equal(status, 0);
/* do rmdir */
assert_int_equal(rmdir("asdfasdfASDFASDF"), 0);
/* do mkdir */
status = mkdir("ASDFASD@%#%54365465654#@%#%@#", 0700);
assert_int_equal(status, 0);
/* do rmdir */
assert_int_equal(rmdir("ASDFASD@%#%54365465654#@%#%@#"), 0);
}

View File

@ -0,0 +1,84 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_open_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILE "testOpenFile"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_open01
****************************************************************************/
void test_nuttx_fs_open01(FAR void **state)
{
int fd;
int ret;
char s[] = "test data!";
char buffer[50] = {
0
};
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TESTFILE, O_WRONLY | O_CREAT, 0700);
assert_true(fd > 0);
/* do write */
int ret2 = write(fd, s, sizeof(s));
close(fd);
assert_int_in_range(ret2, 1, sizeof(s));
/* open file */
fd = open(TESTFILE, O_RDONLY);
assert_true(fd > 0);
test_state->fd1 = fd;
/* do read */
ret = read(fd, buffer, sizeof(buffer));
assert_true(ret > 0);
}

View File

@ -0,0 +1,133 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_opendir_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_opendir01
****************************************************************************/
void test_nuttx_fs_opendir01(FAR void **state)
{
DIR *dir;
struct dirent *ptr;
int ret;
ret = mkdir("testopendir1", 0777);
assert_int_equal(ret, 0);
ret = mkdir("testopendir1/dir123", 0777);
assert_int_equal(ret, 0);
/* do opendir */
dir = opendir("testopendir1");
assert_non_null(dir);
while ((ptr = readdir(dir)) != NULL)
{
if (strncmp(ptr->d_name, ".", 1) == 0)
continue;
if (strncmp(ptr->d_name, "..", 2) == 0)
continue;
if (strncmp(ptr->d_name, "dir123", 6) != 0)
{
closedir(dir);
assert_true(0);
}
}
/* close dir */
assert_int_equal(closedir(dir), 0);
}
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_NUM 1000
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_opendir02
****************************************************************************/
void test_nuttx_fs_opendir02(FAR void **state)
{
int ret;
DIR *dir;
struct dirent *ptr;
/* mkdir for test */
ret = mkdir("testopendir2", 0777);
assert_int_equal(ret, 0);
/* mkdir for test */
ret = mkdir("testopendir2/dir_test2", 0777);
assert_int_equal(ret, 0);
for (int i = 0; i < TEST_NUM; i++)
{
/* open fir for test */
dir = opendir("testopendir2");
assert_true(dir != NULL);
while ((ptr = readdir(dir)) != NULL)
{
if (strcmp(ptr->d_name, ".") == 0 ||
strcmp(ptr->d_name, "..") == 0)
{
continue;
}
else if (strcmp(ptr->d_name, "dir_test2") == 0)
{
break;
}
}
/* close dir */
assert_int_equal(closedir(dir), 0);
}
}

View File

@ -0,0 +1,81 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_poll_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <sys/epoll.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define I_FILE1 "poll_test1"
#define I_FILE2 "poll_test2"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_poll01
****************************************************************************/
void test_nuttx_fs_poll01(FAR void **state)
{
int poll01_fd1;
int poll01_fd2;
int poll01_ret;
struct pollfd poll01_fds[5];
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
poll01_fd1 = open(I_FILE1, O_RDONLY | O_CREAT);
assert_true(poll01_fd1 >= 0);
test_state->fd1 = poll01_fd1;
poll01_fds[0].fd = poll01_fd1;
poll01_fds[0].events = POLLOUT;
poll01_fd2 = open(I_FILE2, O_RDWR | O_CREAT);
assert_true(poll01_fd2 >= 0);
test_state->fd2 = poll01_fd2;
poll01_fds[1].fd = poll01_fd2;
poll01_fds[1].events = POLLIN;
poll01_ret = poll(poll01_fds, 2, 5);
assert_int_equal(poll01_ret, 2);
close(poll01_fd1);
close(poll01_fd2);
}

View File

@ -0,0 +1,100 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_pread_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdio.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE "pread_file"
#define BUF_SIZE 4
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_pread01
****************************************************************************/
void test_nuttx_fs_pread01(FAR void **state)
{
int fd;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* malloc memory */
char *buf = malloc(BUF_SIZE * sizeof(char));
assert_non_null(buf);
test_state->ptr = buf;
/* open file */
fd = open(TEST_FILE, O_RDWR | O_CREAT, 0777);
assert_int_not_equal(fd, -1);
test_state->fd1 = fd;
/* do write */
int ret = write(fd, "ABCD", BUF_SIZE);
assert_int_in_range(ret, 1, 4);
/* reset file pos use fd */
lseek(fd, 0, SEEK_SET);
/* set memory */
memset(buf, '\0', BUF_SIZE);
/* do pread */
pread(fd, buf, 2, 2);
assert_int_equal(strncmp(buf, "CD", 2), 0);
/* set memory */
memset(buf, '\0', BUF_SIZE);
/* the pread do not change file pointer */
pread(fd, buf, 3, 1);
assert_int_equal(strncmp(buf, "BCD", 3), 0);
}

View File

@ -0,0 +1,109 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_pwrite_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE "pwrite_file"
#define BUF_SIZE 4
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_pwrite01
****************************************************************************/
void test_nuttx_fs_pwrite01(FAR void **state)
{
int fd;
char *buf;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TEST_FILE, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
/* malloc memory */
buf = malloc(BUF_SIZE * sizeof(char));
assert_non_null(buf);
test_state->ptr = buf;
/* set memory */
memset(buf, 'A', BUF_SIZE);
/* do write */
int ret = write(fd, buf, BUF_SIZE);
assert_int_in_range(ret, 1, BUF_SIZE);
/* reset file pos use fd */
lseek(fd, 0, SEEK_SET);
/* set memory */
memset(buf, 'B', BUF_SIZE);
/* do pwrite */
pwrite(fd, buf, BUF_SIZE >> 1, 0);
/* set memory */
memset(buf, 'C', BUF_SIZE);
/* do pwrite */
pwrite(fd, buf, BUF_SIZE >> 2, 0);
/* do read */
assert_int_in_range(read(fd, buf, BUF_SIZE), 1, BUF_SIZE);
/* check buf */
assert_true(strncmp(buf, "CBAA", BUF_SIZE) == 0);
}

View File

@ -0,0 +1,75 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_read_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILE "testRead01File1"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_read01
****************************************************************************/
void test_nuttx_fs_read01(FAR void **state)
{
int fd;
int size;
char s[] = "Test!";
char buffer[80];
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
fd = open(TESTFILE, O_WRONLY | O_CREAT, 0777);
assert_int_in_range(fd, 0, 255);
test_state->fd1 = fd;
size = write(fd, s, sizeof(s));
assert_int_equal(size, sizeof(s));
close(fd);
fd = open(TESTFILE, O_RDONLY, 0777);
assert_int_in_range(fd, 0, 255);
test_state->fd1 = fd;
size = read(fd, buffer, sizeof(buffer));
assert_int_equal(size, sizeof(s));
}

View File

@ -0,0 +1,105 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_readdir_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <dirent.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_readdir01
****************************************************************************/
void test_nuttx_fs_readdir01(FAR void **state)
{
int fd;
int ret;
char buf[20] = {
0
};
char *filename[] = {
"testFile1",
"testFile2",
"testFile3",
"testFile4",
"testFile5",
"testFile6",
"testFile7"
};
DIR *test_dir;
struct dirent *dptr;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
for (int i = 0; i < 6; i++)
{
/* open file */
fd = open(filename[i], O_RDWR | O_CREAT, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
/* do wirte */
ret = write(fd, "hello!\n", 6);
assert_uint_in_range(ret, 1, 6);
close(fd);
}
/* do getcwd */
getcwd(buf, sizeof(buf));
/* open directory */
test_dir = opendir(buf);
assert_non_null(test_dir);
while ((dptr = readdir(test_dir)) != 0)
{
if (strcmp(dptr->d_name, ".") && strcmp(dptr->d_name, ".."))
continue;
}
/* close dir */
assert_int_equal(closedir(test_dir), 0);
}

View File

@ -0,0 +1,96 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_readlink_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE "readlink_test_file"
#define PATH_MAX_SIZE 64
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_readlink01
****************************************************************************/
void test_nuttx_fs_readlink01(FAR void **state)
{
int ret;
int fd;
/* test symlink */
char path[PATH_MAX_SIZE] = {
0
};
char buf[PATH_MAX_SIZE] = {
0
};
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* creat file */
fd = creat(TEST_FILE, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
getcwd(path, sizeof(path));
strcat(path, "/");
strcat(path, TEST_FILE);
/* creating a soft connection */
ret = symlink(path, "/file_link");
assert_int_equal(ret, 0);
/* read link */
ret = readlink("/file_link", buf, PATH_MAX_SIZE);
assert_true(ret == strlen(path));
/* delete test file */
assert_int_equal(unlink("/file_link"), 0);
}

View File

@ -0,0 +1,97 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_rename_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_rename01
****************************************************************************/
void test_nuttx_fs_rename01(FAR void **state)
{
int fd;
int status;
int ret;
char buffer[50];
char filename1[] = "testRenameFile1";
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(filename1, O_WRONLY | O_CREAT, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
/* set memory */
memset(buffer, '*', 50);
/* do write */
ret = write(fd, buffer, 50);
assert_int_in_range(ret, 1, 50);
/* do rename */
status = rename(filename1, "newNameFile1");
assert_int_equal(status, 0);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_rename02
****************************************************************************/
void test_nuttx_fs_rename02(FAR void **state)
{
int status;
/* make directory */
status = mkdir("testdir1", 0700);
assert_int_equal(status, 0);
/* rename directory */
status = rename("testdir1", "newtestdir1");
assert_int_equal(status, 0);
}

View File

@ -0,0 +1,194 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_rewinddir_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <dirent.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_PARENT_DIR "parent_dir"
#define TEST_CHILD_DIR1 "parent_dir/child_dir1"
#define TEST_CHILD_DIR2 "parent_dir/child_dir2"
#define TEST_CHILD_DIR3 "parent_dir/child_dir3"
#define TEST_CHILD_DIR4 "parent_dir/child_dir4"
#define TEST_CHILD_DIR5 "parent_dir/child_dir5"
#define TEST_CHILD_FILE1 "parent_dir/child_file1"
#define TEST_CHILD_FILE2 "parent_dir/child_file2"
#define TEST_CHILD_FILE3 "parent_dir/child_file3"
#define WRITE_BUF_SIZE 1024
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_cteat_file
****************************************************************************/
static void test_nuttx_fs_cteat_file(char *filename, size_t write_size)
{
int fd;
char w_buffer[WRITE_BUF_SIZE] = {
0
};
ssize_t size = 0;
/* open file */
fd = open(filename, O_CREAT | O_RDWR, 0777);
assert_true(fd > 0);
/* set memory */
memset(w_buffer, 0x61, WRITE_BUF_SIZE);
do
{
if (write_size <= WRITE_BUF_SIZE)
{
/* do write */
size = write(fd, w_buffer, write_size);
}
else
{
/* do write */
size = write(fd, w_buffer, WRITE_BUF_SIZE);
}
write_size = write_size - size;
}
while (write_size > (size_t)0);
/* close file */
close(fd);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_rewinddir01
****************************************************************************/
void test_nuttx_fs_rewinddir01(FAR void **state)
{
DIR *dir;
struct dirent *ptr;
int count = 0;
int r_count = 0;
int test_flag = -1;
unsigned long long size;
size = cm_get_partition_available_size();
if (size == (unsigned long long)-1)
{
fail_msg("Failed to obtain partition information !\n");
}
/* Stop the test if the available space of the partition is less than 50K */
if (size < 51200)
{
syslog(LOG_WARNING, "Partitioned free space not enough !\n");
syslog(LOG_WARNING, "Test case (%s) exits early !\n", __func__);
}
else
{
/* make directory */
assert_int_equal(mkdir(TEST_PARENT_DIR, S_IRWXU), 0);
assert_int_equal(mkdir(TEST_CHILD_DIR1, S_IRWXU), 0);
assert_int_equal(mkdir(TEST_CHILD_DIR2, S_IRWXU), 0);
assert_int_equal(mkdir(TEST_CHILD_DIR3, S_IRWXU), 0);
assert_int_equal(mkdir(TEST_CHILD_DIR4, S_IRWXU), 0);
assert_int_equal(mkdir(TEST_CHILD_DIR5, S_IRWXU), 0);
/* create */
test_nuttx_fs_cteat_file(TEST_CHILD_FILE1, 10);
test_nuttx_fs_cteat_file(TEST_CHILD_FILE2, 10);
test_nuttx_fs_cteat_file(TEST_CHILD_FILE3, 10);
/* open directory */
dir = opendir(TEST_PARENT_DIR);
while ((ptr = readdir(dir)) != NULL)
{
count++;
}
rewinddir(dir);
while ((ptr = readdir(dir)) != NULL)
{
r_count++;
}
/* close directory */
assert_int_equal(closedir(dir), 0);
if (count == r_count && count != 0)
{
test_flag = 0;
}
/* remove directory */
rmdir(TEST_CHILD_DIR1);
rmdir(TEST_CHILD_DIR2);
rmdir(TEST_CHILD_DIR3);
rmdir(TEST_CHILD_DIR4);
rmdir(TEST_CHILD_DIR5);
unlink(TEST_CHILD_FILE1);
unlink(TEST_CHILD_FILE2);
unlink(TEST_CHILD_FILE3);
rmdir(TEST_PARENT_DIR);
assert_int_equal(test_flag, 0);
}
}

View File

@ -0,0 +1,392 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_rmdir_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
#include "nsh.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define PARENTDIR1 "parentDirName"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_fmdir01
****************************************************************************/
void test_nuttx_fs_fmdir01(FAR void **state)
{
int status;
int fd;
char str[5];
char test_file_name[20] = {
0
};
char test_dir_name[20] = {
0
};
char absolute_directory[100] = {
0
};
char current_path[100] = {
0
};
char parent_directory[PATH_MAX] = {
0
};
unsigned long long size;
size = cm_get_partition_available_size();
if (size == (unsigned long long)-1)
{
fail_msg("Failed to obtain partition information !\n");
}
/* Stop the test if the available space of
* the partition is less than 160K
*/
if (size < 163840)
{
syslog(LOG_WARNING, "Partitioned free space not enough !\n");
syslog(LOG_WARNING, "Test case (%s) exits early !\n", __func__);
}
else
{
/* create directory */
status = mkdir(PARENTDIR1, 0700);
assert_int_equal(status, 0);
/* get test path */
getcwd(current_path, sizeof(current_path));
strcpy(absolute_directory, current_path);
strcat(current_path, "/");
strcat(current_path, PARENTDIR1);
strcpy(parent_directory, current_path);
chdir(current_path);
/* get test path */
getcwd(current_path, sizeof(current_path));
/* create 10 2-level subfolders */
for (int i = 0; i < 10; i++)
{
itoa(i, str, 10);
status = mkdir(str, 0700);
assert_int_equal(status, 0);
}
/* switch to directory 5 */
itoa(5, str, 10);
/* enter sub-directory */
strcat(current_path, "/");
strcat(current_path, str);
chdir(current_path);
/* get test path */
getcwd(current_path, sizeof(current_path));
/* make directory */
status = mkdir("test_3_dir_1", 0700);
assert_int_equal(status, 0);
/* make directory */
status = mkdir("test_3_dir_2", 0700);
assert_int_equal(status, 0);
/* switch to directory 8 */
itoa(8, str, 10);
/* enter sub-directory */
memset(current_path, 0, sizeof(current_path));
strcpy(current_path, parent_directory);
strcat(current_path, "/");
strcat(current_path, str);
chdir(current_path);
/* get test path */
getcwd(current_path, sizeof(current_path));
for (int j = 1; j <= 10; j++)
{
sprintf(test_file_name, "test_3_file_%d", j);
/* creat a test file */
fd = creat(test_file_name, 0700);
assert_true(fd > 0);
close(fd);
/* set memory */
memset(test_file_name, 0, sizeof(test_file_name));
}
/* switch to directory 2 */
itoa(2, str, 10);
/* enter sub-directory */
memset(current_path, 0, sizeof(current_path));
strcpy(current_path, parent_directory);
strcat(current_path, "/");
strcat(current_path, str);
chdir(current_path);
/* get test path */
getcwd(current_path, sizeof(current_path));
for (int k = 1; k <= 5; k++)
{
sprintf(test_file_name, "test_3_file_%d", k);
sprintf(test_dir_name, "test_3_dir_%d", k);
/* create a test file */
fd = creat(test_file_name, 0700);
assert_true(fd > 0);
close(fd);
/* make directory */
status = mkdir(test_dir_name, 0700);
assert_int_equal(status, 0);
/* set memory */
memset(test_file_name, 0, sizeof(test_file_name));
memset(test_dir_name, 0, sizeof(test_dir_name));
}
/* wwitch to the test absolute directory */
chdir(absolute_directory);
/* call the recursive delete interface */
cm_unlink_recursive(parent_directory);
}
}
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define PARENTDIR2 "parentDirName2"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_rmdir02
****************************************************************************/
void test_nuttx_fs_rmdir02(FAR void **state)
{
int status;
int ret;
char str[20] = {
0
};
char absolute_directory[20] = {
0
};
char parent_directory[PATH_MAX] = {
0
};
char temporary_path[300] = {
0
};
unsigned long long size;
size = cm_get_partition_available_size();
if (size == (unsigned long long)-1)
{
fail_msg("Failed to obtain partition information !\n");
}
/* Stop the test if the available space of the partition is less than 98K */
if (size < 98304)
{
syslog(LOG_WARNING, "Partitioned free space not enough !\n");
syslog(LOG_WARNING, "Test case (%s) exits early !\n", __func__);
}
else
{
getcwd(absolute_directory, sizeof(absolute_directory));
/* create directory */
status = mkdir(PARENTDIR2, 0700);
assert_int_equal(status, 0);
strcpy(parent_directory, absolute_directory);
strcat(parent_directory, "/");
strcat(parent_directory, PARENTDIR2);
/* switch to test PARENTDIR */
chdir(parent_directory);
/* create a 6-level directory in a loop */
for (int i = 0; i < 6; i++)
{
/* get current path */
getcwd(temporary_path, sizeof(temporary_path));
strcat(temporary_path, "/");
/* do snprintf */
ret = snprintf(str, 20, "test_dir_%d", i);
assert_true(ret > 0);
strcat(temporary_path, str);
/* make directory */
status = mkdir(temporary_path, 0700);
assert_int_equal(status, 0);
chdir(temporary_path);
/* set memory */
memset(temporary_path, 0, sizeof(temporary_path));
memset(str, 0, sizeof(str));
}
/* wwitch to the test absolute directory */
chdir(absolute_directory);
/* call the recursive delete interface */
cm_unlink_recursive(parent_directory);
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_rmdir03
****************************************************************************/
void test_nuttx_fs_rmdir03(FAR void **state)
{
int status;
DIR *dir = NULL;
char str[5];
char buf[20] = {
0
};
struct dirent *ptr;
unsigned long long size;
size = cm_get_partition_available_size();
if (size == (unsigned long long)-1)
{
fail_msg("Failed to obtain partition information !\n");
}
/* Stop the test if the available space of the partition is less than 80K */
if (size < 81920)
{
syslog(LOG_WARNING, "Partitioned free space not enough !\n");
syslog(LOG_WARNING, "Test case (%s) exits early !\n", __func__);
}
else
{
for (int i = 0; i < 5; i++)
{
itoa(i, str, 10);
/* make directory */
status = mkdir(str, 0700);
assert_int_equal(status, 0);
}
getcwd(buf, sizeof(buf));
/* open directory */
dir = opendir(buf);
while ((ptr = readdir(dir)) != NULL)
{
status = rmdir(ptr->d_name);
}
/* close directory flow */
assert_int_equal(closedir(dir), 0);
}
}

View File

@ -0,0 +1,278 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_seek_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILE "fileSeekTest"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_seek01
****************************************************************************/
void test_nuttx_fs_seek01(FAR void **state)
{
FILE *fp;
char c[] = "This is fseek test !";
char buffer[sizeof(c)];
int ret;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fp = fopen(TESTFILE, "w+");
assert_non_null(fp);
test_state->fd1 = fileno(fp);
/* do fwrite */
fwrite(c, strlen(c) + 1, 1, fp);
/* reset file pos use fp */
ret = fseek(fp, 8, SEEK_SET);
assert_int_equal(ret, 0);
/* do fread */
ret = fread(buffer, 1, strlen(c) + 1, fp);
buffer[ret] = 0;
assert_int_equal(strcmp(buffer, "fseek test !"), 0);
/* reset file pos use fp */
ret = fseek(fp, 5, SEEK_SET);
assert_int_equal(ret, 0);
/* do read */
ret = fread(buffer, 1, strlen(c) + 1, fp);
buffer[ret] = 0;
assert_int_equal(strcmp(buffer, "is fseek test !"), 0);
/* reset file pos use fp */
ret = fseek(fp, 14, SEEK_SET);
assert_int_equal(ret, 0);
/* do fread */
ret = fread(buffer, 1, strlen(c) + 1, fp);
buffer[ret] = 0;
assert_int_equal(strcmp(buffer, "test !"), 0);
fclose(fp);
}
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define WRITE_STR "abcdefg"
#define TFILE "tfile"
/****************************************************************************
* Private Data
****************************************************************************/
static int fd;
static struct tcase
{
off_t off;
int whence;
char *wname;
off_t exp_off;
ssize_t exp_size;
char *exp_data;
}
tcases[] =
{
{
4, SEEK_SET, "SEEK_SET", 4, 3, "efg"
},
{
-2, SEEK_CUR, "SEEK_CUR", 5, 2, "fg"
},
{
-4, SEEK_END, "SEEK_END", 3, 4, "defg"
},
{
0, SEEK_END, "SEEK_END", 7, 0, NULL
},
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_verify_lseek
****************************************************************************/
static int test_nuttx_fs_verify_lseek(unsigned int n)
{
char read_buf[64];
struct tcase *tc = &tcases[n];
int ret;
/* do read */
ssize_t sn = read(fd, read_buf, sizeof(read_buf));
if (sn < 0)
{
return -1;
}
/* set memory */
memset(read_buf, 0, sizeof(read_buf));
/* do lseek */
ret = lseek(fd, tc->off, tc->whence);
if (ret == (off_t)-1)
{
syslog(LOG_ERR, "lseek(%s, %lld, %s) failed\n", TFILE,
(long long)tc->off, tc->wname);
return -1;
}
if (ret != tc->exp_off)
{
syslog(LOG_ERR, "lseek(%s, %lld, %s) returned %d, expected %lld\n",
TFILE, (long long)tc->off, tc->wname, ret, (long long)tc->exp_off);
return -1;
}
/* do read */
sn = read(fd, read_buf, tc->exp_size);
if (sn < 0)
{
return -1;
}
if (tc->exp_data && strcmp(read_buf, tc->exp_data))
{
syslog(LOG_ERR, "lseek(%s, %lld, %s) read incorrect data\n",
TFILE, (long long)tc->off, tc->wname);
return -1;
}
else
{
return 0;
}
}
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_set
****************************************************************************/
static void test_nuttx_fs_set(void)
{
fd = open(TFILE, O_RDWR | O_CREAT, 0700);
assert_true(fd >= 0);
write(fd, WRITE_STR, sizeof(WRITE_STR) - 1);
}
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_clean
****************************************************************************/
static void test_nuttx_fs_clean(void)
{
if (fd > 0)
close(fd);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_seek02
****************************************************************************/
void test_nuttx_fs_seek02(FAR void **state)
{
int ret;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
test_nuttx_fs_set();
test_state->fd1 = fd;
/* do verify lseek */
ret = test_nuttx_fs_verify_lseek(0);
assert_int_equal(ret, 0);
/* do verify lseek */
ret = test_nuttx_fs_verify_lseek(1);
assert_int_equal(ret, 0);
/* do verify lseek */
ret = test_nuttx_fs_verify_lseek(2);
assert_int_equal(ret, 0);
/* do verify lseek */
ret = test_nuttx_fs_verify_lseek(3);
assert_int_equal(ret, 0);
/* do clean */
test_nuttx_fs_clean();
}

View File

@ -0,0 +1,201 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_sendfile_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <errno.h>
#include <cmocka.h>
#include "fstest.h"
#define O_FILE "outputFile"
#define I_FILE1 "inputFile1"
#define I_FILE2 "inputFile2"
/****************************************************************************
* Private Functions
****************************************************************************/
static void set_test(void)
{
int fd;
fd = open(O_FILE, O_CREAT | O_RDWR, 0777);
if (fd == -1)
{
syslog(LOG_ERR, "Unable to open file %s, errno %d\n", O_FILE, errno);
assert_true(1 == 0);
}
write(fd, "ABCDEFGHIJ", 10);
close(fd);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: sendfile
* Example description:
* 1.Test copy the entire file
* Expect results: TEST PASSED
****************************************************************************/
void test_nuttx_fs_sendfile01(FAR void **state)
{
int ret;
int fd_o1;
int fd_o2;
int fd_i;
int test_flag = 0;
struct stat f_stat;
set_test();
/* open file readonly */
fd_i = open(O_FILE, O_RDONLY);
if (fd_i == -1)
{
syslog(LOG_ERR, "open file fail !, errno %d\n", errno);
assert_true(1 == 0);
}
/* open file O_RDWR */
fd_o1 = open(I_FILE1, O_CREAT | O_RDWR, 0777);
if (fd_o1 == -1)
{
close(fd_i);
syslog(LOG_ERR, "open file fail !, errno %d\n", errno);
assert_true(1 == 0);
}
if (fstat(fd_i, &f_stat) < 0)
{
syslog(LOG_ERR, "fstat fail !, errno %d\n", errno);
close(fd_i);
close(fd_o1);
assert_true(1 == 0);
}
/* sendfile , copy the entire file */
ret = sendfile(fd_o1, fd_i, NULL, f_stat.st_size);
if (ret != f_stat.st_size)
{
syslog(LOG_ERR, "ret != f_stat.st_size\n");
test_flag = 1;
}
lseek(fd_i, 0, SEEK_SET);
fd_o2 = open(I_FILE2, O_CREAT | O_RDWR, 0777);
if (fd_o2 == -1)
{
syslog(LOG_ERR, "open file fail !, errno %d\n", errno);
close(fd_i);
close(fd_o1);
assert_true(1 == 0);
}
/* sendfile , Copy part of the file */
ret = sendfile(fd_o2, fd_i, NULL, 5);
if (ret != 5)
{
syslog(LOG_ERR, "ret != 5\n");
test_flag = 1;
}
close(fd_o1);
close(fd_o2);
close(fd_i);
assert_true(test_flag == 0);
}
/****************************************************************************
* Name: sendfile
* Example description:
* 1.Test copy the entire file
* Expect results: TEST PASSED
****************************************************************************/
void test_nuttx_fs_sendfile02(FAR void **state)
{
int fd_o;
int fd_i;
off_t offset;
struct stat f_stat;
set_test();
/* open file readonly */
fd_i = open(O_FILE, O_RDONLY);
if (fd_i == -1)
{
syslog(LOG_ERR, "open file fail !, errno %d\n", errno);
assert_true(1 == 0);
}
/* open file O_RDWR */
fd_o = open(I_FILE1, O_CREAT | O_RDWR, 0777);
if (fd_o == -1)
{
syslog(LOG_ERR, "open file fail !, errno %d\n", errno);
close(fd_i);
assert_true(1 == 0);
}
if (fstat(fd_i, &f_stat) < 0)
{
syslog(LOG_ERR, "fstat fail ! errno %d\n", errno);
close(fd_i);
close(fd_o);
assert_true(1 == 0);
}
offset = 5;
/* sendfile */
sendfile(fd_o, fd_i, &offset, f_stat.st_size - offset);
close(fd_o);
close(fd_i);
/* Checks whether the file pointer is offset to the specified position */
assert_int_equal(offset, f_stat.st_size);
}

View File

@ -0,0 +1,162 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_stat_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <string.h>
#include <utime.h>
#include <time.h>
#include <syslog.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE "stat_test_file"
#define BUF_SIZE 1024
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_print_time
****************************************************************************/
__attribute__((unused)) static void test_nuttx_fs_print_time(struct tm *TM)
{
syslog(LOG_INFO, " tm_year: %d\n", TM->tm_year + 1900);
syslog(LOG_INFO, " tm_mon: %d\n", TM->tm_mon);
syslog(LOG_INFO, " tm_mday: %d\n", TM->tm_mday);
syslog(LOG_INFO, " tm_hour: %d\n", TM->tm_hour);
syslog(LOG_INFO, " tm_min: %d\n", TM->tm_min);
syslog(LOG_INFO, " tm_sec: %d\n", TM->tm_sec);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_stat01
****************************************************************************/
void test_nuttx_fs_stat01(FAR void **state)
{
int fd;
int ret;
int ret2;
struct stat file_s;
char buf[BUF_SIZE] = {
0
};
struct tm *tm_1 = NULL;
struct tm *tm_2 = NULL;
int year1;
int year2;
int month1;
int month2;
int day1;
int day2;
int hour1;
int hour2;
int min1;
int min2;
time_t t_1;
time_t t_2;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* set memory */
memset(buf, 65, BUF_SIZE);
/* open file */
fd = open(TEST_FILE, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = 0;
/* do write */
ret2 = write(fd, buf, BUF_SIZE);
assert_int_in_range(ret2, 1, 1024);
close(fd);
/* get system time */
time(&t_1);
tm_1 = gmtime(&t_1);
assert_non_null(tm_1);
/* set time */
year1 = tm_1->tm_year;
month1 = tm_1->tm_mon;
day1 = tm_1->tm_mday;
hour1 = tm_1->tm_hour;
min1 = tm_1->tm_min;
/* get file info */
ret = stat(TEST_FILE, &file_s);
assert_int_equal(ret, 0);
/* output stat struct information */
t_2 = file_s.st_mtime;
tm_2 = gmtime(&t_2);
assert_non_null(tm_2);
/* set time */
year2 = tm_2->tm_year;
month2 = tm_2->tm_mon;
day2 = tm_2->tm_mday;
hour2 = tm_2->tm_hour;
min2 = tm_2->tm_min;
/* compare time and size */
assert_int_equal(year1, year2);
assert_int_equal(month1, month2);
assert_int_equal(day1, day2);
assert_int_equal(hour1, hour2);
assert_int_equal(min1, min2);
assert_int_equal(file_s.st_size, BUF_SIZE);
}

View File

@ -0,0 +1,91 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_statfs_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <dirent.h>
#include <syslog.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define _1k 1024
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_print_statfs
****************************************************************************/
__attribute__((unused)) static void test_nuttx_fs_print_statfs
(struct statfs *buf)
{
syslog(LOG_INFO, "statfs buffer:\n");
syslog(LOG_INFO, " f_type: %u \n", (unsigned)buf->f_type);
syslog(LOG_INFO, " f_bsize: %zuk\n", (size_t)buf->f_bsize / _1k);
syslog(LOG_INFO, " f_blocks: %llu \n",
(unsigned long long)buf->f_blocks);
syslog(LOG_INFO, " f_bfree: %llu \n",
(unsigned long long)buf->f_bfree);
syslog(LOG_INFO, " f_bavail: %llu \n",
(unsigned long long)buf->f_bavail);
syslog(LOG_INFO, " f_files: %llu \n",
(unsigned long long)buf->f_files);
syslog(LOG_INFO, " f_ffree: %llu \n",
(unsigned long long)buf->f_ffree);
syslog(LOG_INFO, " f_namelen: %zu \n", (size_t)buf->f_namelen);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_statfs01
****************************************************************************/
void test_nuttx_fs_statfs01(FAR void **state)
{
struct statfs disk_info;
/* call statfs() */
char *buf = getcwd(NULL, 0);
int ret = statfs(buf, &disk_info);
free(buf);
assert_int_equal(ret, 0);
}

View File

@ -0,0 +1,461 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_stream_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <errno.h>
#include <cmocka.h>
#include "fstest.h"
#define TESTFILENAME "streamTestfile"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stream
* Example description:
* 1. open a file with "a+".
* 2. Write some strings to the file.
* 3. Open it again and Write some strings.
* 4. Check the returned results.
* Test item: fopen() fwrite() freopen()
* Expect results: TEST PASSED
****************************************************************************/
void test_nuttx_fs_stream01(FAR void **state)
{
FILE *stream;
char buf[10];
int i;
size_t ret;
if ((stream = fopen(TESTFILENAME, "a+")) == NULL)
{
syslog(LOG_ERR, "open file fail !, errno %d\n", errno);
assert_true(1 == 0);
}
fwrite("a", 1, 1, stream);
if ((stream = freopen(TESTFILENAME, "a+", stream)) == NULL)
{
syslog(LOG_ERR, "reopen file fail !, errno %d\n", errno);
assert_true(1 == 0);
}
fclose(stream);
if ((stream = fopen(TESTFILENAME, "r")) == NULL)
{
syslog(LOG_ERR, "open file fail !, errno %d\n", errno);
assert_true(1 == 0);
}
else
{
for (i = 0; i < 10; i++)
buf[i] = 0;
ret = fread(buf, 1, 1, stream);
if ((buf[0] != 'a') || (buf[1] != 0) || ret == 0)
{
fclose(stream);
syslog(LOG_ERR, "bad contents in \n");
assert_true(1 == 0);
}
fclose(stream);
}
unlink(TESTFILENAME);
}
/****************************************************************************
* Name: stream
* Example description:
* 1. open a file with "a+".
* 2. Write the file multiple times.
* 3. Check that the file pointer is in the correct
* position after each write.
* 4. Reset the file pointer position. Repeat step 2-3
* 4. Check the returned results.
* Test item: fopen() fwrite() rewind() ftell() fseek() fgets()
* Expect results: TEST PASSED
****************************************************************************/
void test_nuttx_fs_stream02(FAR void **state)
{
FILE *stream;
char buf[30];
char *junk = "abcdefghijklmnopqrstuvwxyz";
long pos;
int lc;
for (lc = 0; lc < 100; lc++)
{
if ((stream = fopen(TESTFILENAME, "a+")) == NULL)
{
syslog(LOG_ERR, "open file failed !, errno %d\n", errno);
assert_true(1 == 0);
}
pos = ftell(stream);
if (pos != 0)
{
syslog(LOG_ERR, "file pointer descrepancy 1, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
if (fwrite(junk, sizeof(*junk), strlen(junk), stream) == 0)
{
syslog(LOG_ERR, "fwrite failed, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
pos = ftell(stream);
if (pos != strlen(junk))
{
syslog(LOG_ERR,
"strlen(junk)=%zi: file pointer descrepancy 2 (pos=%li)",
strlen(junk), pos);
fclose(stream);
assert_true(1 == 0);
}
rewind(stream);
pos = ftell(stream);
if (pos != 0)
{
fclose(stream);
syslog(LOG_ERR,
"file pointer descrepancy 3 (pos=%li, wanted pos=0)",
pos);
assert_true(1 == 0);
}
if (fseek(stream, strlen(junk), 1) != 0)
{
syslog(LOG_ERR, "fseek failed !, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
pos = ftell(stream);
if (pos != strlen(junk))
{
fclose(stream);
syslog(LOG_ERR,
"strlen(junk)=%zi: file pointer descrepancy 4 (pos=%li)",
strlen(junk), pos);
assert_true(1 == 0);
}
if (fseek(stream, 0, 2) != 0)
{
syslog(LOG_ERR, "fseek failed !, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
pos = ftell(stream);
if (pos != strlen(junk))
{
fclose(stream);
syslog(LOG_ERR,
"strlen(junk)=%zi: file pointer descrepancy 5 (pos=%li)",
strlen(junk), pos);
assert_true(1 == 0);
}
if (fseek(stream, 0, 0) != 0)
{
syslog(LOG_ERR, "fseek failed !, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
pos = ftell(stream);
if (pos != 0)
{
fclose(stream);
syslog(LOG_ERR,
"file pointer descrepancy 6 (pos=%li, wanted pos=0)", pos);
assert_true(1 == 0);
}
while (fgets(buf, sizeof(buf), stream))
;
pos = ftell(stream);
if (pos != strlen(junk))
{
syslog(LOG_ERR,
"strlen(junk)=%zi: file pointer descrepancy 7 (pos=%li)",
strlen(junk), pos);
assert_true(1 == 0);
}
fclose(stream);
unlink(TESTFILENAME);
}
}
/****************************************************************************
* Name: stream
* Example description:
* 1. open a file with "a+".
* 2. Write the file multiple times.
* 3. close the file.
* 4. open the file again with "r+"
* 5. Request a piece of memory and read the contents of the file.
* 6. Check that the read file class is correct.
* 7. repeat step 2-3-4-5-6 for 10 times.
* 8. Check that the test returns results.
* Test item: fopen() fwrite() fread() malloc()
* Expect results: TEST PASSED
****************************************************************************/
void test_nuttx_fs_stream03(FAR void **state)
{
FILE *stream;
char *junk = "abcdefghijklmnopqrstuvwxyz";
size_t len = strlen(junk);
char *inbuf = NULL;
int ret;
int lc;
for (lc = 0; lc < 10; lc++)
{
if ((stream = fopen(TESTFILENAME, "a+")) == NULL)
{
syslog(LOG_ERR, "fopen a+ failed, errno %d\n", errno);
assert_true(1 == 0);
}
if ((ret = fwrite(junk, sizeof(*junk), len, stream)) == 0)
{
syslog(LOG_ERR, "fwrite failed, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
if ((size_t)ret != len)
{
syslog(LOG_ERR,
"len = %zi != return value from fwrite = %zi",
len, ret);
fclose(stream);
assert_true(1 == 0);
}
fclose(stream);
if ((stream = fopen(TESTFILENAME, "r+")) == NULL)
{
syslog(LOG_ERR, "fopen r+ failed, errno %d\n", errno);
assert_true(1 == 0);
}
if ((inbuf = malloc(len)) == 0)
{
syslog(LOG_ERR,
"test failed , because of malloc fail, errno %d\n",
errno);
fclose(stream);
assert_true(1 == 0);
}
if ((ret = fread(inbuf, sizeof(*junk), len, stream)) == 0)
{
syslog(LOG_ERR, "fread failed, errno %d\n", errno);
free(inbuf);
fclose(stream);
assert_true(1 == 0);
}
if ((size_t)ret != len)
{
syslog(LOG_ERR,
"len = %zi != return value from fread = %zi",
len, ret);
free(inbuf);
fclose(stream);
assert_true(1 == 0);
}
/* Free memory */
free(inbuf);
fclose(stream);
unlink(TESTFILENAME);
}
}
/****************************************************************************
* Name: stream
* Example description:
* 1. Open the file in a different mode, such as "a+" "r+" "rb" etc.
* 2. Check that the test returns results.
* Test item: fopen() fprintf() read() fileno() fclose()
* Expect results: TEST PASSED
****************************************************************************/
void test_nuttx_fs_stream04(FAR void **state)
{
FILE *stream;
char buf[10];
int nr;
int fd;
size_t ret;
int lc;
for (lc = 0; lc < 10; lc++)
{
if ((stream = fopen(TESTFILENAME, "a+")) == NULL)
{
syslog(LOG_ERR, "fopen a+ failed, errno %d\n", errno);
assert_true(1 == 0);
}
fprintf(stream, "a");
fclose(stream);
if ((stream = fopen(TESTFILENAME, "r+")) == NULL)
{
syslog(LOG_ERR, "fopen r+ failed, errno %d\n", errno);
assert_true(1 == 0);
}
if (ferror(stream) != 0)
{
syslog(LOG_ERR,
"ferror did not return zero, return %d\n",
ferror(stream));
fclose(stream);
assert_true(1 == 0);
}
fd = fileno(stream);
if ((nr = read(fd, buf, 1)) < 0)
{
syslog(LOG_ERR, "read failed !, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
if (nr != 1)
{
syslog(LOG_ERR,
"read did not read right number, nr = %d, errno %d\n",
nr, errno);
fclose(stream);
assert_true(1 == 0);
}
if (buf[0] != 'a')
{
syslog(LOG_ERR, "read returned bad values %c\n", buf[0]);
fclose(stream);
assert_true(1 == 0);
}
fclose(stream);
if ((stream = fopen(TESTFILENAME, "r+")) == NULL)
{
syslog(LOG_ERR,
"fopen(%s) r+ failed, errno %d\n", TESTFILENAME, errno);
assert_true(1 == 0);
}
if (feof(stream) != 0)
{
syslog(LOG_ERR, "feof returned non-zero when it should not \n");
fclose(stream);
assert_true(1 == 0);
}
ret = fread(buf, 1, 2, stream);
buf[ret] = 0;
if (feof(stream) == 0)
{
syslog(LOG_ERR, "feof returned zero when it should not \n");
fclose(stream);
assert_true(1 == 0);
}
fclose(stream);
if ((stream = fopen(TESTFILENAME, "rb")) == NULL)
{
syslog(LOG_ERR, "fopen rb failed, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
fclose(stream);
if ((stream = fopen(TESTFILENAME, "wb")) == NULL)
{
syslog(LOG_ERR, "fopen wb failed, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
fclose(stream);
if ((stream = fopen(TESTFILENAME, "ab")) == NULL)
{
syslog(LOG_ERR, "fopen ab failed, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
fclose(stream);
if ((stream = fopen(TESTFILENAME, "rb+")) == NULL)
{
syslog(LOG_ERR, "fopen rb+ failed");
fclose(stream);
assert_true(1 == 0);
}
fclose(stream);
if ((stream = fopen(TESTFILENAME, "wb+")) == NULL)
{
syslog(LOG_ERR, "fopen wb+ failed, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
fclose(stream);
if ((stream = fopen(TESTFILENAME, "ab+")) == NULL)
{
syslog(LOG_ERR, "fopen ab+ failed, errno %d\n", errno);
fclose(stream);
assert_true(1 == 0);
}
fclose(stream);
unlink(TESTFILENAME);
}
}

View File

@ -0,0 +1,130 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_symlink_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE "test_file"
#define PATH_MAX_SIZE 64
/****************************************************************************
* Private Data
****************************************************************************/
static char *path;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_run_test
****************************************************************************/
static int test_nuttx_fs_run_test(void)
{
int ret;
/* test symlink */
char buf[64] = {
0
};
ret = symlink(path, "/file_link");
if (ret != 0)
{
return ERROR;
}
else
{
ret = readlink("/file_link", buf, PATH_MAX_SIZE);
unlink("/file_link");
}
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_symlink01
****************************************************************************/
void test_nuttx_fs_symlink01(FAR void **state)
{
int fd;
int ret;
char *buf;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* malloc memory */
path = malloc(PATH_MAX_SIZE * sizeof(char));
assert_non_null(path);
test_state->ptr = path;
/* set memory */
buf = getcwd(NULL, 0);
memset(path, '\0', PATH_MAX_SIZE);
sprintf(path, "%s/symlink_test_file", buf);
free(buf);
/* open file */
fd = open(path, O_WRONLY | O_CREAT, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
/* do run test */
ret = test_nuttx_fs_run_test();
/* do remove */
unlink(path);
assert_int_equal(ret, OK);
}

View File

@ -0,0 +1,117 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_truncate_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILE "truncateTestFile"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_truncate01
****************************************************************************/
void test_nuttx_fs_truncate01(FAR void **state)
{
int fd;
int ret;
int ret2;
char buf[80];
const char *s1 = "0123456789";
const char *s2 = "abcde";
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TESTFILE, O_CREAT | O_WRONLY | O_TRUNC, 0700);
#ifdef CONFIG_FDCHECK
assert_true(fd > 0);
#else
assert_int_in_range(fd, 0, 512);
#endif
test_state->fd1 = fd;
/* do write */
ret2 = write(fd, s1, strlen(s1));
assert_int_in_range(ret2, 1, strlen(s1));
/* refresh to storage */
ret = fsync(fd);
assert_int_equal(ret, 0);
/* do ftruncate */
ret = ftruncate(fd, 5);
assert_int_equal(ret, 0);
/* do lseek */
off_t pos = lseek(fd, 0, SEEK_END);
assert_int_equal(pos, 5);
/* do write */
ret2 = write(fd, s2, strlen(s2));
assert_int_in_range(ret2, 1, strlen(s2));
close(fd);
/* open file */
fd = open(TESTFILE, O_RDONLY);
assert_true(fd > 0);
test_state->fd1 = fd;
/* read file */
ret = read(fd, buf, 26);
if (ret >= 0)
{
buf[ret] = 0;
}
/* check buf */
assert_true(strncmp(buf, "01234abcde", 10) == 0);
close(fd);
}

View File

@ -0,0 +1,91 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_unlink_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <dirent.h>
#include <syslog.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MAXSIZE 1024
#define test_file "test_unlink_file01"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_unlink01
****************************************************************************/
void test_nuttx_fs_unlink01(FAR void **state)
{
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
int fd = open(test_file, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
char buf[MAXSIZE] = {
0
};
/* set memory */
memset(buf, 65, MAXSIZE);
/* do write */
int size = write(fd, buf, MAXSIZE);
assert_int_in_range(size, 1, MAXSIZE);
close(fd);
/* delete test file */
int ret = unlink(test_file);
assert_int_equal(ret, 0);
/* check if the file was deleted successfully */
fd = open(test_file, O_RDONLY);
assert_int_equal(fd, -1);
}

View File

@ -0,0 +1,170 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_write_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILE "testWriteFile"
#define MAXLEN 1024
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_write01
****************************************************************************/
void test_nuttx_fs_write01(FAR void **state)
{
int out;
int rval;
char buffer[1024];
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
out = open(TESTFILE, O_WRONLY | O_CREAT, 0700);
assert_true(out > 0);
test_state->fd1 = out;
/* set memory */
memset(buffer, '*', MAXLEN);
/* do write */
rval = write(out, buffer, MAXLEN);
assert_int_in_range(rval, 1, MAXLEN);
}
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILENAME "loopTestFile"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_write02
****************************************************************************/
void test_nuttx_fs_write02(FAR void **state)
{
int rval;
FILE *fp;
long offset;
char content[15] = "asdfgtgtrf";
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* fopen file */
fp = fopen(TESTFILENAME, "a+");
assert_non_null(fp);
test_state->fd1 = fileno(fp);
/* do fwrite */
rval = fwrite(content, 1, 10, fp);
assert_int_in_range(rval, 1, 10);
/* refresh to storage */
fsync(fileno(fp));
/* do ffkush */
fflush(fp);
/* get ftell */
offset = ftell(fp);
assert_true(offset == 10);
fclose(fp);
}
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TESTFILENAME3 "loopTestFile"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_write03
****************************************************************************/
void test_nuttx_fs_write03(FAR void **state)
{
int rval;
FILE *fp;
long offset;
char content[15] = "asdfgtgtrf";
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* fopen file */
fp = fopen(TESTFILENAME3, "a+");
assert_non_null(fp);
test_state->fd1 = fileno(fp);
/* do fwrite */
rval = fwrite(content, 1, 10, fp);
assert_int_in_range(rval, 1, 10);
/* refresh to storage */
fsync(fileno(fp));
/* get ftell */
offset = ftell(fp);
assert_true(offset == 10);
fclose(fp);
}

View File

@ -0,0 +1,193 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/cmocka_fs_test.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdio.h>
#include "fstest.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cmocka_fs_test_main
****************************************************************************/
int main(int argc, char *argv[])
{
/* Add Test Cases */
const struct CMUnitTest nuttx_fs_test_suites[] = {
cmocka_unit_test_setup_teardown(test_nuttx_fs_creat01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_dup01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_fcntl01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
#ifndef CONFIG_ARCH_SIM
cmocka_unit_test_setup_teardown(test_nuttx_fs_fcntl02,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_fcntl03,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
#endif
cmocka_unit_test_setup_teardown(test_nuttx_fs_fstat01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
#ifndef CONFIG_ARCH_SIM
cmocka_unit_test_setup_teardown(test_nuttx_fs_fstat02,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
#endif
cmocka_unit_test_setup_teardown(test_nuttx_fs_fstatfs01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_fsync01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_fsync02,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_getfilep01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_mkdir01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_open01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_opendir01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_opendir02,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_pread01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_pwrite01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
#ifndef CONFIG_ARCH_SIM
cmocka_unit_test_setup_teardown(test_nuttx_fs_read01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
#endif
cmocka_unit_test_setup_teardown(test_nuttx_fs_readdir01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_readlink01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_rename01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_rename02,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_rewinddir01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_fmdir01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_rmdir02,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_rmdir03,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_seek01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_seek02,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
#ifndef CONFIG_ARCH_SIM
cmocka_unit_test_setup_teardown(test_nuttx_fs_stat01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
#endif
cmocka_unit_test_setup_teardown(test_nuttx_fs_statfs01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_symlink01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_truncate01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_unlink01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_write01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_write02,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_write03,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_append01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_sendfile01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_sendfile02,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_stream01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_stream02,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_stream03,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_stream04,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_eventfd,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
cmocka_unit_test_setup_teardown(test_nuttx_fs_poll01,
test_nuttx_fs_fest_group_set_up,
test_nuttx_fs_test_group_tear_down),
};
/* Run Test cases */
cmocka_run_group_tests(nuttx_fs_test_suites, NULL, NULL);
return 0;
}

View File

@ -0,0 +1,215 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/common/test_fs_common.c
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <errno.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
#define CM_FSTESTDIR "CM_fs_testdir"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
unsigned long long cm_get_partition_available_size(void)
{
unsigned long long size = 0;
int ret;
struct statfs stat_info;
/* call statfs() */
ret = statfs(MOUNT_DIR, &stat_info);
if (ret == 0)
{
size = (unsigned long long)stat_info.f_bsize
* (unsigned long long)stat_info.f_bfree;
}
else
{
size = (unsigned long long)-1;
}
return size;
}
int cm_unlink_recursive(FAR char *path)
{
struct dirent *d;
struct stat stat;
size_t len;
int ret;
DIR *dp;
ret = lstat(path, &stat);
if (ret < 0)
{
return ret;
}
if (!S_ISDIR(stat.st_mode))
{
return unlink(path);
}
dp = opendir(path);
if (dp == NULL)
{
return -1;
}
len = strlen(path);
if (len > 0 && path[len - 1] == '/')
{
path[--len] = '\0';
}
while ((d = readdir(dp)) != NULL)
{
if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
{
continue;
}
snprintf(&path[len], PATH_MAX - len, "/%s", d->d_name);
ret = cm_unlink_recursive(path);
if (ret < 0)
{
closedir(dp);
return ret;
}
}
ret = closedir(dp);
if (ret >= 0)
{
path[len] = '\0';
ret = rmdir(path);
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_fest_group_set_up
****************************************************************************/
int test_nuttx_fs_fest_group_set_up(void **state)
{
int res;
struct stat buf;
struct fs_testsuites_state_s *test_state;
res = chdir(MOUNT_DIR);
if (res != 0)
{
syslog(LOG_INFO, "ERROR: Failed to switch the mount dir\n");
exit(1);
}
res = stat(CM_FSTESTDIR, &buf);
if (res == 0 && buf.st_mode == S_IFDIR)
{
res = chdir(CM_FSTESTDIR);
}
else
{
char testdir[PATH_MAX] = {
0
};
sprintf(testdir, "%s/%s", MOUNT_DIR, CM_FSTESTDIR);
/* Delete the existing test directory */
cm_unlink_recursive(testdir);
res = mkdir(CM_FSTESTDIR, 0777);
if (res != 0)
{
syslog(LOG_INFO, "ERROR: Failed to creat the test directory\n");
exit(1);
}
chdir(CM_FSTESTDIR);
}
test_state = zalloc(sizeof(struct fs_testsuites_state_s));
assert_false(test_state == NULL);
test_state->ptr = NULL;
test_state->fd1 = -1;
test_state->fd2 = -1;
memset(test_state->filename, 0, PATH_SIZE);
*state = test_state;
return res;
}
/****************************************************************************
* Name: test_nuttx_fs_test_group_tear_down
****************************************************************************/
int test_nuttx_fs_test_group_tear_down(void **state)
{
int res;
char testdir[PATH_MAX] = {
0
};
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
if (test_state->fd1 > 0)
close(test_state->fd1);
if (test_state->fd2 > 0)
close(test_state->fd2);
if (test_state->ptr != NULL)
free(test_state->ptr);
free(test_state);
sprintf(testdir, "%s/%s", MOUNT_DIR, CM_FSTESTDIR);
res = chdir(MOUNT_DIR);
if (res != 0)
{
syslog(LOG_INFO, "ERROR: Failed to switch the mount dir\n");
exit(1);
}
/* Delete the existing test directory */
cm_unlink_recursive(testdir);
return 0;
}

View File

@ -0,0 +1,210 @@
/****************************************************************************
* apps/testing/testsuites/kernel/fs/include/fstest.h
* Copyright (C) 2020 Xiaomi Corporation
*
* 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.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#ifndef __TEST_H
#define __TEST_H
#include <nuttx/config.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <libgen.h>
#include <dirent.h>
#include <sys/stat.h>
#include <syslog.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_PASS 0
#define TEST_FAIL -1
#define MAX_PATH 300
/* The test files generated during the 'fs-test'
* are stored in this directory
*/
#define FS_TEST_DIR "fs_test_dir"
#define MOUNT_DIR CONFIG_TESTS_TESTSUITES_MOUNT_DIR
#define PATH_SIZE 128
struct fs_testsuites_state_s
{
char filename[PATH_SIZE];
char *ptr;
int fd1;
int fd2;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
unsigned long long cm_get_partition_available_size(void);
int cm_unlink_recursive(FAR char *path);
int test_nuttx_fs_fest_group_set_up(FAR void **state);
int test_nuttx_fs_test_group_tear_down(FAR void **state);
/* cases/fs_creat_test.c ****************************************************/
void test_nuttx_fs_creat01(FAR void **state);
/* cases/fs_dup_test.c ******************************************************/
void test_nuttx_fs_dup01(FAR void **state);
/* cases/fs_dup2_test.c *****************************************************/
void test_nuttx_fs_dup201(FAR void **state);
/* cases/fs_fcntl_test.c ****************************************************/
void test_nuttx_fs_fcntl01(FAR void **state);
void test_nuttx_fs_fcntl02(FAR void **state);
void test_nuttx_fs_fcntl03(FAR void **state);
/* cases/fs_fstat_test.c ****************************************************/
void test_nuttx_fs_fstat01(FAR void **state);
void test_nuttx_fs_fstat02(FAR void **state);
/* cases/fs_fstatfs_test.c **************************************************/
void test_nuttx_fs_fstatfs01(FAR void **state);
/* cases/fs_fsync_test.c ****************************************************/
void test_nuttx_fs_fsync01(FAR void **state);
void test_nuttx_fs_fsync02(FAR void **state);
/* cases/fs_getfilep_test.c *************************************************/
void test_nuttx_fs_getfilep01(FAR void **state);
/* cases/fs_mkdir_test.c ****************************************************/
void test_nuttx_fs_mkdir01(FAR void **state);
/* cases/fs_open_test.c *****************************************************/
void test_nuttx_fs_open01(FAR void **state);
/* cases/fs_opendir_test.c **************************************************/
void test_nuttx_fs_opendir01(FAR void **state);
void test_nuttx_fs_opendir02(FAR void **state);
/* cases/fs_pread_test.c ****************************************************/
void test_nuttx_fs_pread01(FAR void **state);
/* cases/fs_pwrite_test.c ***************************************************/
void test_nuttx_fs_pwrite01(FAR void **state);
/* cases/fs_read_test.c *****************************************************/
void test_nuttx_fs_read01(FAR void **state);
/* cases/fs_readdir_test.c **************************************************/
void test_nuttx_fs_readdir01(FAR void **state);
/* cases/fs_readlink_test.c *************************************************/
void test_nuttx_fs_readlink01(FAR void **state);
/* cases/fs_rename_test.c ***************************************************/
void test_nuttx_fs_rename01(FAR void **state);
void test_nuttx_fs_rename02(FAR void **state);
/* cases/fs_rewinddir_test.c ************************************************/
void test_nuttx_fs_rewinddir01(FAR void **state);
/* cases/fs_rmdir_test.c ****************************************************/
void test_nuttx_fs_fmdir01(FAR void **state);
void test_nuttx_fs_rmdir02(FAR void **state);
void test_nuttx_fs_rmdir03(FAR void **state);
/* cases/fs_seek_test.c *****************************************************/
void test_nuttx_fs_seek01(FAR void **state);
void test_nuttx_fs_seek02(FAR void **state);
/* cases/fs_stat_test.c *****************************************************/
void test_nuttx_fs_stat01(FAR void **state);
/* cases/fs_statfs_test.c ***************************************************/
void test_nuttx_fs_statfs01(FAR void **state);
/* cases/fs_symlink_test.c **************************************************/
void test_nuttx_fs_symlink01(FAR void **state);
/* cases/fs_truncate_test.c *************************************************/
void test_nuttx_fs_truncate01(FAR void **state);
/* cases/fs_unlink_test.c ***************************************************/
void test_nuttx_fs_unlink01(FAR void **state);
/* cases/fs_write_test.c ****************************************************/
void test_nuttx_fs_write01(FAR void **state);
void test_nuttx_fs_write02(FAR void **state);
void test_nuttx_fs_write03(FAR void **state);
/* cases/fs_append_test.c ***************************************************/
void test_nuttx_fs_append01(FAR void **state);
/* cases/fs_sendfile_test.c *************************************************/
void test_nuttx_fs_sendfile01(FAR void **state);
void test_nuttx_fs_sendfile02(FAR void **state);
/* cases/fs_stream_test.c ***************************************************/
void test_nuttx_fs_stream01(FAR void **state);
void test_nuttx_fs_stream02(FAR void **state);
void test_nuttx_fs_stream03(FAR void **state);
void test_nuttx_fs_stream04(FAR void **state);
/* cases/fs_eventfd_test.c **************************************************/
void test_nuttx_fs_eventfd(FAR void **state);
/* fs_poll_test.c ***********************************************************/
void test_nuttx_fs_poll01(FAR void **state);
#endif