diff --git a/testsuites/unittest/posix/BUILD.gn b/testsuites/unittest/posix/BUILD.gn index 26ac5a97..d02f8153 100644 --- a/testsuites/unittest/posix/BUILD.gn +++ b/testsuites/unittest/posix/BUILD.gn @@ -60,13 +60,15 @@ static_library("posix_test") { "src/time/time_func_test_01.c", ] + if (defined(LOSCFG_SUPPORT_FATFS) || defined(LOSCFG_SUPPORT_LITTLEFS)) { + deps = [ "src/fs" ] + } + include_dirs = [ "//test/xts/tools/hctest/include", "src", ] - if (defined(LOSCFG_SUPPORT_FATFS) || defined(LOSCFG_SUPPORT_LITTLEFS)) { - sources += [ "src/fs/posix_fs_func_test.c" ] - } + if (!defined(LOSCFG_COMPILER_ICCARM)) { cflags = [ "-Wno-error" ] } else { diff --git a/testsuites/unittest/posix/src/fs/BUILD.gn b/testsuites/unittest/posix/src/fs/BUILD.gn new file mode 100644 index 00000000..9b316164 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +static_library("fs") { + sources = [ + "api/posix_fs_access_test.c", + "api/posix_fs_close_test.c", + "api/posix_fs_closedir_test.c", + "api/posix_fs_fstat_test.c", + "api/posix_fs_fsync_test.c", + "api/posix_fs_ftruncate_test.c", + "api/posix_fs_lseek_test.c", + "api/posix_fs_mkdir_test.c", + "api/posix_fs_open_test.c", + "api/posix_fs_opendir_test.c", + "api/posix_fs_pread_test.c", + "api/posix_fs_pwrite_test.c", + "api/posix_fs_read_test.c", + "api/posix_fs_readdir_test.c", + "api/posix_fs_rename_test.c", + "api/posix_fs_rmdir_test.c", + "api/posix_fs_stat_test.c", + "api/posix_fs_statfs_test.c", + "api/posix_fs_unlink_test.c", + "api/posix_fs_write_test.c", + "full/posix_fs_full_test.c", + "posix_fs_func_test.c", + "pressure/posix_fs_pressure_test.c", + ] + + include_dirs = [ + ".", + "../", + "../../../../include", + ] +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_access_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_access_test.c new file mode 100644 index 00000000..4d1e7906 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_access_test.c @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_ACCESS_OK + * @tc.name access + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsAccessOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = access(tmpFileName, F_OK); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +#if (LOSCFG_SUPPORT_FATFS == 1) + ret = access(tmpFileName, R_OK); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + ret = access(tmpFileName, W_OK); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + ret = access(tmpFileName, X_OK); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); +#endif + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_ACCESS_EINVAL + * @tc.name access + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsAccessEINVAL, Function | MediumTest | Level1) +{ + char *tmpFileName = NULL; + + int32_t ret = access(tmpFileName, F_OK); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsAccessTest(void) +{ + RUN_ONE_TESTCASE(TestFsAccessOK); + RUN_ONE_TESTCASE(TestFsAccessEINVAL); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_close_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_close_test.c new file mode 100644 index 00000000..cf903764 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_close_test.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_CLOSE_OK + * @tc.name close + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsCloseOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsCloseTest(void) +{ + RUN_ONE_TESTCASE(TestFsCloseOK); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_closedir_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_closedir_test.c new file mode 100644 index 00000000..614a8b19 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_closedir_test.c @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_CLOSEDIR_OK + * @tc.name closedir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsClosedirOK, Function | MediumTest | Level1) +{ + int32_t ret; + + DIR *dir = opendir(TEST_ROOT); + ICUNIT_ASSERT_NOT_EQUAL(dir, NULL, dir); + + ret = closedir(dir); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_CLOSEDIR_EBADF + * @tc.name closedir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsClosedirEBADF, Function | MediumTest | Level1) +{ + int32_t ret; + + DIR *dir = opendir(TEST_ROOT); + ICUNIT_ASSERT_NOT_EQUAL(dir, NULL, dir); + + ret = closedir(NULL); + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + ret = closedir(dir); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + +EXIT: + return POSIX_FS_NO_ERROR; +} + +void PosixFsClosedirTest(void) +{ + RUN_ONE_TESTCASE(TestFsClosedirOK); + RUN_ONE_TESTCASE(TestFsClosedirEBADF); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_fstat_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_fstat_test.c new file mode 100644 index 00000000..690dedcf --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_fstat_test.c @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" +#include "vfs_config.h" + +/* * + * @tc.number SUB_KERNEL_FS_FSTAT_OK + * @tc.name fstat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFstatOK, Function | MediumTest | Level1) +{ + int32_t ret; + struct stat buf = { 0 }; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = fstat(fd, &buf); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FSTAT_EBADF001 + * @tc.name fstat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFstatEBADF001, Function | MediumTest | Level1) +{ + struct stat buf = { 0 }; + + int32_t ret = fstat(-1, &buf); /* -1, bad fd */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FSTAT_EBADF002 + * @tc.name fstat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFstatEBADF002, Function | MediumTest | Level1) +{ + struct stat buf = { 0 }; + + int32_t ret = fstat(ERROR_CONFIG_NFILE_DESCRIPTORS, &buf); + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FSTAT_EBADF003 + * @tc.name fstat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFstatEBADF003, Function | MediumTest | Level1) +{ + struct stat buf = { 0 }; + + int32_t ret = fstat(0, &buf); /* 0, used for stdin */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = fstat(1, &buf); /* 1, used for stdout */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = fstat(2, &buf); /* 2, used for stderr */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FSTAT_EINVAL + * @tc.name fstat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFstatEINVAL, Function | MediumTest | Level1) +{ + int32_t ret; + struct stat *buf = NULL; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = fstat(fd, buf); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FSTAT_ENOENT + * @tc.name fstat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFstatENOENT, Function | MediumTest | Level1) +{ + int32_t ret; + struct stat buf = { 0 }; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + struct MountPoint *mountBak = g_mountPoints; + g_mountPoints = NULL; + ret = fstat(fd, &buf); + g_mountPoints = mountBak; + ICUNIT_ASSERT_EQUAL(errno, ENOENT, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsFstatTest(void) +{ + RUN_ONE_TESTCASE(TestFsFstatOK); + RUN_ONE_TESTCASE(TestFsFstatEBADF001); + RUN_ONE_TESTCASE(TestFsFstatEBADF002); + RUN_ONE_TESTCASE(TestFsFstatEBADF003); + RUN_ONE_TESTCASE(TestFsFstatEINVAL); + RUN_ONE_TESTCASE(TestFsFstatENOENT); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_fsync_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_fsync_test.c new file mode 100644 index 00000000..a254a206 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_fsync_test.c @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" +#include "vfs_config.h" + +/* * + * @tc.number SUB_KERNEL_FS_FSYNC_OK + * @tc.name fsync + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFsyncOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = fsync(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FSYNC_EBADF001 + * @tc.name fsync + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFsyncEBADF001, Function | MediumTest | Level1) +{ + int32_t ret = fsync(-1); /* -1, bad fd */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FSYNC_EBADF002 + * @tc.name fsync + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFsyncEBADF002, Function | MediumTest | Level1) +{ + int32_t ret = fsync(ERROR_CONFIG_NFILE_DESCRIPTORS); + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FSYNC_EBADF003 + * @tc.name fsync + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFsyncEBADF003, Function | MediumTest | Level1) +{ + int32_t ret = fsync(0); /* 0, used for stdin */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = fsync(1); /* 1, used for stdout */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = fsync(2); /* 2, used for stderr */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsFsyncTest(void) +{ + RUN_ONE_TESTCASE(TestFsFsyncOK); + RUN_ONE_TESTCASE(TestFsFsyncEBADF001); + RUN_ONE_TESTCASE(TestFsFsyncEBADF002); + RUN_ONE_TESTCASE(TestFsFsyncEBADF003); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_ftruncate_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_ftruncate_test.c new file mode 100644 index 00000000..cfbe25d5 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_ftruncate_test.c @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_FTRUNCATE_OK + * @tc.name ftruncate + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFtruncateOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = FILE2; + const char writeBuf[TEST_BUF_SIZE] = "hello"; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = write(fd, writeBuf, TEST_BUF_SIZE); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + ret = ftruncate(fd, MODIFIED_FILE_SIZE); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FTRUNCATE_EBADF001 + * @tc.name ftruncate + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFtruncateEBADF001, Function | MediumTest | Level1) +{ + int32_t ret = ftruncate(-1, MODIFIED_FILE_SIZE); /* -1, bad fd */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = ftruncate(ERROR_CONFIG_NFILE_DESCRIPTORS, MODIFIED_FILE_SIZE); + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FTRUNCATE_EBADF002 + * @tc.name ftruncate + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFtruncateEBADF002, Function | MediumTest | Level1) +{ + int32_t ret = ftruncate(0, MODIFIED_FILE_SIZE); /* 0, used for stdin */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = ftruncate(1, MODIFIED_FILE_SIZE); /* 1, used for stdout */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = ftruncate(2, MODIFIED_FILE_SIZE); /* 2, used for stderr */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FTRUNCATE_EINVAL + * @tc.name ftruncate + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFtruncateEINVAL, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = ftruncate(fd, -1); /* -1, length after modification */ + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FTRUNCATE_EACCES + * @tc.name ftruncate + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFtruncateEACCES, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDONLY, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = ftruncate(fd, MODIFIED_FILE_SIZE); + ICUNIT_ASSERT_EQUAL(errno, EACCES, POSIX_FS_IS_ERROR); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsFtruncateTest(void) +{ +#if (LOSCFG_SUPPORT_FATFS == 1) + RUN_ONE_TESTCASE(TestFsFtruncateOK); +#endif + RUN_ONE_TESTCASE(TestFsFtruncateEBADF001); + RUN_ONE_TESTCASE(TestFsFtruncateEBADF002); + RUN_ONE_TESTCASE(TestFsFtruncateEINVAL); + RUN_ONE_TESTCASE(TestFsFtruncateEACCES); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_lseek_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_lseek_test.c new file mode 100644 index 00000000..da4e9389 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_lseek_test.c @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" +#include "vfs_config.h" + +#define TEST_SEEK_WRITE_BUF "AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDD" + +/* * + * @tc.number SUB_KERNEL_FS_LSEEK_OK001 + * @tc.name lseek + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekOK001, Function | MediumTest | Level1) +{ + off_t off; + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + off = lseek(fd, 0, SEEK_SET); /* 0, offset distance */ + ICUNIT_ASSERT_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_LSEEK_OK002 + * @tc.name lseek + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekOK002, Function | MediumTest | Level1) +{ + off_t off; + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + off = lseek(fd, 0, SEEK_CUR); /* 0, offset distance */ + ICUNIT_ASSERT_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_LSEEK_OK003 + * @tc.name lseek + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekOK003, Function | MediumTest | Level1) +{ + off_t off; + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + off = lseek(fd, 0, SEEK_END); /* 0, offset distance */ + ICUNIT_ASSERT_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_LSEEK_EBADF001 + * @tc.name lseek + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekEBADF001, Function | MediumTest | Level1) +{ + off_t off = lseek(-1, 0, SEEK_SET); /* -1, bad fd 0, offset distance */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL((int32_t)off, POSIX_FS_IS_ERROR, (int32_t)off); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_LSEEK_EBADF002 + * @tc.name lseek + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekEBADF002, Function | MediumTest | Level1) +{ + off_t off = lseek(ERROR_CONFIG_NFILE_DESCRIPTORS, 0, SEEK_SET); /* 0, offset distance */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL((int32_t)off, POSIX_FS_IS_ERROR, (int32_t)off); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_LSEEK_EBADF003 + * @tc.name lseek + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekEBADF003, Function | MediumTest | Level1) +{ + off_t off = lseek(0, 0, SEEK_SET); /* 0, used for stdin 0, offet distance */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL((int32_t)off, POSIX_FS_IS_ERROR, (int32_t)off); + + off = lseek(1, 0, SEEK_SET); /* 1, used for stdout 0, offet distance */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL((int32_t)off, POSIX_FS_IS_ERROR, (int32_t)off); + + off = lseek(2, 0, SEEK_SET); /* 2 used for stderr 0, offet distance */ + ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL((int32_t)off, POSIX_FS_IS_ERROR, (int32_t)off); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_LSEEK_standard + * @tc.name lseek + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekStandard, Function | MediumTest | Level1) +{ + off_t off; + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char writeBuf[TEST_BUF_SIZE] = { TEST_SEEK_WRITE_BUF }; + char readBuf[TEST_BUF_SIZE] = { 0 }; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = write(fd, writeBuf, TEST_BUF_SIZE); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + off = lseek(fd, 0, SEEK_SET); /* 0, offet distance */ + ICUNIT_ASSERT_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off); + + ret = read(fd, readBuf, 10); /* 10, common data for test, no special meaning */ + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + ret = strcmp(readBuf, "AAAAAAAAAA"); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_NO_ERROR, ret, EXIT); + + off = lseek(fd, 10, SEEK_CUR); /* 10, common data for test, no special meaning */ + ICUNIT_GOTO_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off, EXIT); + + ret = read(fd, readBuf, 10); /* 10, common data for test, no special meaning */ + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + ret = strcmp(readBuf, "CCCCCCCCCC"); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_NO_ERROR, ret, EXIT); + + off = lseek(fd, -10, SEEK_END); /* -10, common data for test, no special meaning */ + ICUNIT_GOTO_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off, EXIT); + + ret = read(fd, readBuf, 10); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + ret = strcmp(readBuf, "DDDDDDDDDD"); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_NO_ERROR, ret, EXIT); + + off = lseek(fd, 10, SEEK_END); /* 10, common data for test, no special meaning */ + ICUNIT_GOTO_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off, EXIT); + + ret = write(fd, "E", 1); /* 1, common data for test, no special meaning */ + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + off = lseek(fd, 0, SEEK_END); /* 0, offet distance */ + ICUNIT_GOTO_EQUAL(off, 51, off, EXIT); /* 51, common data for test, no special meaning */ + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsLseekTest(void) +{ + RUN_ONE_TESTCASE(TestFsLseekOK001); + RUN_ONE_TESTCASE(TestFsLseekOK002); + RUN_ONE_TESTCASE(TestFsLseekOK003); + RUN_ONE_TESTCASE(TestFsLseekEBADF001); + RUN_ONE_TESTCASE(TestFsLseekEBADF002); + RUN_ONE_TESTCASE(TestFsLseekEBADF003); + RUN_ONE_TESTCASE(TestFsLseekStandard); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_mkdir_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_mkdir_test.c new file mode 100644 index 00000000..85d0a0a2 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_mkdir_test.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_MKDIR_OK + * @tc.name mkdir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsMkdirOK, Function | MediumTest | Level1) +{ + char pathF[50] = { DIRB }; + + int32_t ret = mkdir(pathF, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + ret = rmdir(pathF); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_MKDIR_EINVAL + * @tc.name mkdir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsMkdirEINVAL, Function | MediumTest | Level1) +{ + int32_t ret = mkdir(NULL, TEST_MODE_HIGH); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_MKDIR_ENOENT + * @tc.name mkdir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsMkdirENOENT, Function | MediumTest | Level1) +{ + int32_t ret; + char tmpDir[TEST_BUF_SIZE] = { DIRC }; + struct MountPoint *mountBak = g_mountPoints; + + g_mountPoints = NULL; + ret = mkdir(tmpDir, TEST_MODE_HIGH); + g_mountPoints = mountBak; + ICUNIT_ASSERT_EQUAL(errno, ENOENT, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsMkdirTest(void) +{ + RUN_ONE_TESTCASE(TestFsMkdirOK); + RUN_ONE_TESTCASE(TestFsMkdirEINVAL); + RUN_ONE_TESTCASE(TestFsMkdirENOENT); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_open_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_open_test.c new file mode 100644 index 00000000..c139bf18 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_open_test.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_OPEN_OK + * @tc.name open + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsOpenOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_OPEN_EINVAL + * @tc.name open + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsOpenEINVAL, Function | MediumTest | Level1) +{ + const char *tmpFileName1 = NULL; + const char *tmpFileName2 = "/"; + + int32_t fd = open(tmpFileName1, O_CREAT | O_RDWR); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + fd = open(tmpFileName2, O_CREAT | O_RDWR); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_OPEN_ENOENT + * @tc.name open + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsOpenENOENT, Function | MediumTest | Level1) +{ + int32_t fd = -1; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + struct MountPoint *mountBak = g_mountPoints; + + g_mountPoints = NULL; + fd = open(tmpFileName, O_CREAT | O_RDWR); + g_mountPoints = mountBak; + ICUNIT_ASSERT_EQUAL(errno, ENOENT, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsOpenTest(void) +{ + RUN_ONE_TESTCASE(TestFsOpenOK); + RUN_ONE_TESTCASE(TestFsOpenEINVAL); + RUN_ONE_TESTCASE(TestFsOpenENOENT); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_opendir_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_opendir_test.c new file mode 100644 index 00000000..78cd71fa --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_opendir_test.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_OPENDIR_OK + * @tc.name opendir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsOpendirOK, Function | MediumTest | Level1) +{ + int32_t ret; + + DIR *dir = opendir(TEST_ROOT); + ICUNIT_ASSERT_NOT_EQUAL_VOID(dir, NULL, dir); + + ret = closedir(dir); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_OPENDIR_EINVAL + * @tc.name opendir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsOpendirEINVAL, Function | MediumTest | Level1) +{ + DIR *dir = opendir(NULL); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL_VOID(dir, NULL, dir); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_OPENDIR_ENOENT + * @tc.name opendir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsOpendirENOENT, Function | MediumTest | Level1) +{ + DIR *dir = NULL; + struct MountPoint *mountBak = g_mountPoints; + + g_mountPoints = NULL; + dir = opendir(TEST_ROOT); + g_mountPoints = mountBak; + ICUNIT_ASSERT_EQUAL(errno, ENOENT, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL_VOID(dir, NULL, dir); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsOpendirTest(void) +{ + RUN_ONE_TESTCASE(TestFsOpendirOK); + RUN_ONE_TESTCASE(TestFsOpendirEINVAL); + RUN_ONE_TESTCASE(TestFsOpendirENOENT); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_pread_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_pread_test.c new file mode 100644 index 00000000..b82b4610 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_pread_test.c @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_PREAD_OK + * @tc.name pread + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPreadOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char readBuf[TEST_BUF_SIZE] = "fsPreadTest"; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = pread(fd, readBuf, TEST_BUF_SIZE, 0); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_PREAD_EFAULT + * @tc.name pread + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPreadEFAULT, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char *readBuf = NULL; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = pread(fd, readBuf, TEST_BUF_SIZE, 0); + ICUNIT_ASSERT_EQUAL(errno, EFAULT, POSIX_FS_IS_ERROR); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_PREAD_EACCES + * @tc.name pread + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPreadEACCES, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + char readBuf[TEST_BUF_SIZE] = "fsPreadTest"; + + int32_t fd = open(tmpFileName, O_CREAT | O_WRONLY, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = pread(fd, readBuf, TEST_BUF_SIZE, 0); + ICUNIT_ASSERT_EQUAL(errno, EACCES, POSIX_FS_IS_ERROR); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsPreadTest(void) +{ + RUN_ONE_TESTCASE(TestFsPreadOK); + RUN_ONE_TESTCASE(TestFsPreadEFAULT); + RUN_ONE_TESTCASE(TestFsPreadEACCES); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_pwrite_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_pwrite_test.c new file mode 100644 index 00000000..85308477 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_pwrite_test.c @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_PRWRITE_OK + * @tc.name pwrite + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPwriteOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char writeBuf[TEST_BUF_SIZE] = "fsPwrtiteTest"; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = pwrite(fd, writeBuf, TEST_BUF_SIZE, 0); /* 0, offset distance */ + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_PRWRITE_EFAULT + * @tc.name pwrite + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPwriteEFAULT, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char *writeBuf = NULL; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = pwrite(fd, writeBuf, TEST_BUF_SIZE, 0); /* 0, offset distance */ + ICUNIT_ASSERT_EQUAL(errno, EFAULT, POSIX_FS_IS_ERROR); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_PRWRITE_EACCES + * @tc.name pwrite + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPwriteEACCES, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char writeBuf[TEST_BUF_SIZE] = "fsPwriteTest"; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDONLY, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = pwrite(fd, writeBuf, TEST_BUF_SIZE, 0); /* 0, offset distance */ + ICUNIT_ASSERT_EQUAL(errno, EACCES, POSIX_FS_IS_ERROR); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsPwriteTest(void) +{ + RUN_ONE_TESTCASE(TestFsPwriteOK); + RUN_ONE_TESTCASE(TestFsPwriteEFAULT); + RUN_ONE_TESTCASE(TestFsPwriteEACCES); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_read_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_read_test.c new file mode 100644 index 00000000..899b8baf --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_read_test.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_READ_OK + * @tc.name read + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsReadOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char readBuf[TEST_BUF_SIZE] = "hello"; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = read(fd, readBuf, sizeof(readBuf)); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_READ_EFAULT + * @tc.name read + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsReadEFAULT, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char *readBuf = NULL; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = read(fd, readBuf, sizeof(readBuf)); + ICUNIT_ASSERT_EQUAL(errno, EFAULT, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_READ_EACCES + * @tc.name read + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsReadEACCES, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char readBuf[TEST_BUF_SIZE] = "hello"; + + int32_t fd = open(tmpFileName, O_CREAT | O_WRONLY); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = read(fd, readBuf, sizeof(readBuf)); + ICUNIT_ASSERT_EQUAL(errno, EACCES, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsReadTest(void) +{ + RUN_ONE_TESTCASE(TestFsReadOK); + RUN_ONE_TESTCASE(TestFsReadEFAULT); + RUN_ONE_TESTCASE(TestFsReadEACCES); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_readdir_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_readdir_test.c new file mode 100644 index 00000000..b3bc2185 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_readdir_test.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_READDIR_OK + * @tc.name readdir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsReaddirOK, Function | MediumTest | Level1) +{ + int32_t ret; + struct dirent *dResult = NULL; + + DIR *dir = opendir(TEST_ROOT); + ICUNIT_ASSERT_NOT_EQUAL(dir, NULL, POSIX_FS_IS_ERROR); + + dResult = readdir(dir); + ICUNIT_ASSERT_NOT_EQUAL(dResult, NULL, POSIX_FS_IS_ERROR); + + ret = closedir(dir); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsReaddirTest(void) +{ + RUN_ONE_TESTCASE(TestFsReaddirOK); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_rename_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_rename_test.c new file mode 100644 index 00000000..9359e1f6 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_rename_test.c @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" +#include "vfs_config.h" + +#define TEST_FILE1 TEST_ROOT"/file1.txt" +#define TEST_FILE2 TEST_ROOT"/file2.txt" +#define TEST_FILE3 TEST_ROOT"/file3.txt" +#define TEST_FILE4 TEST_ROOT"/file4.txt" + +/* * + * @tc.number SUB_KERNEL_FS_RENAME_OK + * @tc.name rename + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsRenameOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char fileNameOld[TEST_BUF_SIZE] = { TEST_FILE1 }; + const char fileNameNew[TEST_BUF_SIZE] = { TEST_FILE2 }; + + int32_t fd = open(fileNameOld, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = rename(fileNameOld, fileNameNew); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(fileNameNew); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_RENAME_EINVAL001 + * @tc.name rename + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsRenameEINVAL001, Function | MediumTest | Level1) +{ + const char *fileNameOld = NULL; + const char fileNameNew[TEST_BUF_SIZE] = { TEST_FILE3 }; + + int32_t ret = rename(fileNameOld, fileNameNew); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_RENAME_EINVAL002 + * @tc.name rename + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsRenameEINVAL002, Function | MediumTest | Level1) +{ + const char fileNameOld[TEST_BUF_SIZE] = { TEST_FILE4 }; + const char *fileNameNew = NULL; + + int32_t ret = rename(fileNameOld, fileNameNew); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_RENAME_EINVAL003 + * @tc.name rename + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsRenameEINVAL003, Function | MediumTest | Level1) +{ + int32_t ret; + const char fileNameOld[TEST_BUF_SIZE] = { TEST_FILE3 }; + const char fileNameNew[TEST_BUF_SIZE] = { TEST_FILE4 }; + + struct MountPoint *mountBak = g_mountPoints; + g_mountPoints = NULL; + ret = rename(fileNameOld, fileNameNew); + g_mountPoints = mountBak; + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsRenameTest(void) +{ + RUN_ONE_TESTCASE(TestFsRenameOK); + RUN_ONE_TESTCASE(TestFsRenameEINVAL001); + RUN_ONE_TESTCASE(TestFsRenameEINVAL002); + RUN_ONE_TESTCASE(TestFsRenameEINVAL003); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_rmdir_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_rmdir_test.c new file mode 100644 index 00000000..5a1e6b12 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_rmdir_test.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_RMDIR_OK + * @tc.name rmdir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsRmdirOK, Function | MediumTest | Level1) +{ + char pathH[TEST_BUF_SIZE] = { DIRD }; + + int32_t ret = mkdir(pathH, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = rmdir(pathH); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_RMDIR_EINVAL + * @tc.name rmdir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsRmdirEINVAL, Function | MediumTest | Level1) +{ + int32_t ret = rmdir(NULL); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsRmdirTest(void) +{ + RUN_ONE_TESTCASE(TestFsRmdirOK); + RUN_ONE_TESTCASE(TestFsRmdirEINVAL); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_stat_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_stat_test.c new file mode 100644 index 00000000..b7fd0366 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_stat_test.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_STAT_OK + * @tc.name stat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsStatOK, Function | MediumTest | Level1) +{ + struct stat buf = { 0 }; + + int32_t ret = stat(TEST_ROOT, &buf); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_sSTAT_EINVAL001 + * @tc.name stat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsStatEINVAL001, Function | MediumTest | Level1) +{ + struct stat buf = { 0 }; + const char *tmpFileName = NULL; + + int32_t ret = stat(tmpFileName, &buf); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_sSTAT_EINVAL002 + * @tc.name stat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsStatEINVAL002, Function | MediumTest | Level1) +{ + struct stat *buf = NULL; + + int32_t ret = stat(TEST_ROOT, buf); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_STAT_ENOENT + * @tc.name stat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsStatENOENT, Function | MediumTest | Level1) +{ + int32_t ret; + struct stat buf = { 0 }; + + struct MountPoint *mountBak = g_mountPoints; + g_mountPoints = NULL; + ret = stat(TEST_ROOT, &buf); + g_mountPoints = mountBak; + ICUNIT_ASSERT_EQUAL(errno, ENOENT, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsStatTest(void) +{ + RUN_ONE_TESTCASE(TestFsStatOK); + RUN_ONE_TESTCASE(TestFsStatEINVAL001); + RUN_ONE_TESTCASE(TestFsStatEINVAL002); + RUN_ONE_TESTCASE(TestFsStatENOENT); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_statfs_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_statfs_test.c new file mode 100644 index 00000000..5067fffb --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_statfs_test.c @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_STATFS_OK + * @tc.name statfs + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsStatfsOK, Function | MediumTest | Level1) +{ + struct statfs buf = { 0 }; + + int32_t ret = statfs(TEST_ROOT, &buf); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_STATFS_EINVAL + * @tc.name statfs + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsStatfsEINVAL, Function | MediumTest | Level1) +{ + struct statfs buf = { 0 }; + + int32_t ret = statfs(NULL, &buf); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = statfs(TEST_ROOT, NULL); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} +/* * + * @tc.number SUB_KERNEL_FS_STATFS_ENOENT + * @tc.name statfs + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsStatfsENOENT, Function | MediumTest | Level1) +{ + int32_t ret; + struct statfs buf = { 0 }; + + struct MountPoint *mountBak = g_mountPoints; + g_mountPoints = NULL; + ret = statfs(TEST_ROOT, &buf); + g_mountPoints = mountBak; + ICUNIT_ASSERT_EQUAL(errno, ENOENT, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsStatfsTest(void) +{ + RUN_ONE_TESTCASE(TestFsStatfsOK); + RUN_ONE_TESTCASE(TestFsStatfsEINVAL); + RUN_ONE_TESTCASE(TestFsStatfsENOENT); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_unlink_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_unlink_test.c new file mode 100644 index 00000000..29a98753 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_unlink_test.c @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_UNLINK_OK + * @tc.name unlink + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsUnlinkOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE4 }; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_UNLINK_ENOENT + * @tc.name unlink + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsUnlinkENOENT, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE]= { FILE1 }; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + struct MountPoint *mountBak = g_mountPoints; + g_mountPoints = NULL; + ret = unlink(tmpFileName); + g_mountPoints = mountBak; + ICUNIT_ASSERT_EQUAL(errno, ENOENT, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsUnlinkTest(void) +{ + RUN_ONE_TESTCASE(TestFsUnlinkOK); + RUN_ONE_TESTCASE(TestFsUnlinkENOENT); +} diff --git a/testsuites/unittest/posix/src/fs/api/posix_fs_write_test.c b/testsuites/unittest/posix/src/fs/api/posix_fs_write_test.c new file mode 100644 index 00000000..fdc0c634 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/api/posix_fs_write_test.c @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +/* * + * @tc.number SUB_KERNEL_FS_WRITE_OK + * @tc.name write + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsWriteOK, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + const char writeBuf[TEST_BUF_SIZE] = "hello"; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = write(fd, writeBuf, TEST_BUF_SIZE); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_WRITE_EINVAL + * @tc.name write + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsWriteEINVAL, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + const char *writeBuf1 = NULL; + const char writeBuf2[TEST_BUF_SIZE] = "hello"; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = write(fd, writeBuf1, TEST_BUF_SIZE); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = write(fd, writeBuf2, 0); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_WRITE_EACCES + * @tc.name write + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsWriteEACCES, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + const char writeBuf[TEST_BUF_SIZE] = "hello"; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDONLY); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = write(fd, writeBuf, TEST_BUF_SIZE); + ICUNIT_ASSERT_EQUAL(errno, EACCES, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsWriteTest(void) +{ + RUN_ONE_TESTCASE(TestFsWriteOK); + RUN_ONE_TESTCASE(TestFsWriteEINVAL); + RUN_ONE_TESTCASE(TestFsWriteEACCES); +} diff --git a/testsuites/unittest/posix/src/fs/full/posix_fs_full_test.c b/testsuites/unittest/posix/src/fs/full/posix_fs_full_test.c new file mode 100644 index 00000000..1f018732 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/full/posix_fs_full_test.c @@ -0,0 +1,463 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" + +#define TEST_OPEN_DIR_NUM 16 +#define TEST_OPEN_FILE_NUM 16 +#define WRITE_BUF_SIZE 128 +#define READ_WRITE_BUF_SIZE 1024 +#define TEMP_DIRE TEST_ROOT"/e" +#define TEMP_DIRE_FILE TEST_ROOT"/e/eFile" +#define TEMP_DIRF TEST_ROOT"/f" +#define TEMP_DIRF_DIR TEST_ROOT"/f/fDir" +#define TEST_DIRG TEST_ROOT"/g" + +/* * + * @tc.number SUB_KERNEL_FS_FULL001 + * @tc.name open close unlink + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFull001, Function | MediumTest | Level1) +{ + int32_t ret; + int32_t fd1 = -1; + int32_t fd2 = -1; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + + fd1 = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd1, POSIX_FS_IS_ERROR, fd1); + +#if (LOSCFG_SUPPORT_FATFS == 1) + fd2 = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_EQUAL(fd2, POSIX_FS_IS_ERROR, fd2); +#endif + + ret = close(fd1); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + fd2 = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd2, POSIX_FS_IS_ERROR, fd2); + + ret = close(fd2); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FULL002 + * @tc.name open write read lseek read close unlink + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFull002, Function | MediumTest | Level1) +{ + off_t off; + int32_t ret; + int32_t fd = -1; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char *writeBuf = (char *)malloc(READ_WRITE_BUF_SIZE * sizeof(char)); + char *readBuf = (char *)malloc(READ_WRITE_BUF_SIZE * sizeof(char)); + + (void)memset_s(writeBuf, READ_WRITE_BUF_SIZE, 'w', READ_WRITE_BUF_SIZE); + (void)memset_s(readBuf, READ_WRITE_BUF_SIZE, 'r', READ_WRITE_BUF_SIZE); + + fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = write(fd, writeBuf, READ_WRITE_BUF_SIZE / 2); /* 2, number of writes */ + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + ret = write(fd, writeBuf, READ_WRITE_BUF_SIZE / 2); /* 2, number of writes */ + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + off = lseek(fd, 0, SEEK_SET); /* 0, offset distance */ + ICUNIT_GOTO_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off, EXIT); + + for (int32_t i = 0; i < 8; i++) { /* 8, number of reads */ + ret = read(fd, readBuf + i * 128, 128); /* 128, 128, length per read */ + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + } + + for (int32_t j = 0; j < READ_WRITE_BUF_SIZE; j++) { + ICUNIT_GOTO_EQUAL(writeBuf[j], readBuf[j], POSIX_FS_IS_ERROR, EXIT); + } + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FULL003 + * @tc.name mkdir opendir closedir rmdir fstat + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFull003, Function | MediumTest | Level1) +{ + int res; + DIR *dir = NULL; + int32_t index = 0; + bool flag = true; + struct dirent *dirmsg = NULL; + struct stat buf = { 0 }; + int32_t i, fd[TEST_OPEN_FILE_NUM]; + char tmpFileName[TEST_BUF_SIZE] = { 0 }; + char fileName[TEST_BUF_SIZE] = { 0 }; + + int32_t ret = mkdir(TEMP_DIRE, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + for (i = 0; i < TEST_OPEN_FILE_NUM; i++) { + res = sprintf_s(tmpFileName, TEST_BUF_SIZE, "%s%02d", TEMP_DIRE_FILE, i); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + goto EXIT1; + } + fd[i] = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + flag = fd[i] == -1 ? false : true; + ICUNIT_GOTO_NOT_EQUAL(fd[i], POSIX_FS_IS_ERROR, fd[i], EXIT1); + } + dir = opendir(TEMP_DIRE); + flag = dir == NULL ? false : true; + ICUNIT_GOTO_NOT_EQUAL(dir, NULL, POSIX_FS_IS_ERROR, EXIT1); + while ((dirmsg = readdir(dir)) != NULL) { + res = sprintf_s(fileName, TEST_BUF_SIZE, "%s%02d", "eFile", index); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + goto EXIT1; + } + ret = strcmp(dirmsg->d_name, fileName); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_NO_ERROR, ret, EXIT1); + ret = fstat(fd[index], &buf); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + ICUNIT_GOTO_EQUAL(buf.st_mode & S_IFMT, S_IFREG, POSIX_FS_IS_ERROR, EXIT1); + index++; + } + ICUNIT_GOTO_EQUAL(index, TEST_OPEN_FILE_NUM, POSIX_FS_IS_ERROR, EXIT1); +EXIT1: + for (int32_t j = 0; j < i; j++) { + res = sprintf_s(tmpFileName, TEST_BUF_SIZE, "%s%02d", TEMP_DIRE_FILE, j); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + return POSIX_FS_IS_ERROR; + } + ret = close(fd[j]); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + } + if (flag == false) { + goto EXIT; + } + ret = closedir(dir); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); +EXIT: + ret = rmdir(TEMP_DIRE); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FULL004 + * @tc.name mkdir readdir opendir stat closedir rmdir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFull004, Function | MediumTest | Level1) +{ + int res; + int32_t i; + int32_t index = 0; + DIR *dir = NULL; + struct dirent *dirmsg = NULL; + struct stat buf = { 0 }; + char dirName[TEST_BUF_SIZE] = { 0 }; + char tmpDirName[TEST_BUF_SIZE] = { 0 }; + + int32_t ret = mkdir(TEMP_DIRF, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + for (i = 0; i < TEST_OPEN_DIR_NUM; i++) { + res = sprintf_s(tmpDirName, TEST_BUF_SIZE, "%s%02d", TEMP_DIRF_DIR, i); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + goto EXIT; + } + ret = mkdir(tmpDirName, TEST_MODE_HIGH); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + } + + dir = opendir(TEMP_DIRF); + ICUNIT_GOTO_NOT_EQUAL(dir, NULL, POSIX_FS_IS_ERROR, EXIT1); + while ((dirmsg = readdir(dir)) != NULL) { + res = sprintf_s(dirName, TEST_BUF_SIZE, "%s%02d", "fDir", index); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + goto EXIT1; + } + ret = strcmp(dirmsg->d_name, dirName); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_NO_ERROR, ret, EXIT1); + res = sprintf_s(tmpDirName, TEST_BUF_SIZE, "%s%02d", TEMP_DIRF_DIR, index); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + goto EXIT1; + } + ret = stat(tmpDirName, &buf); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + ICUNIT_GOTO_EQUAL(buf.st_mode & S_IFMT, S_IFDIR, POSIX_FS_IS_ERROR, EXIT1); + index++; + } + ICUNIT_GOTO_EQUAL(index, TEST_OPEN_DIR_NUM, POSIX_FS_IS_ERROR, EXIT1); +EXIT1: + ret = closedir(dir); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); +EXIT: + for (int32_t j = 0; j < i; j++) { + res = sprintf_s(tmpDirName, TEST_BUF_SIZE, "%s%02d", TEMP_DIRF_DIR, j); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + return POSIX_FS_IS_ERROR; + } + ret = rmdir(tmpDirName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + } + ret = rmdir(TEMP_DIRF); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FULL005 + * @tc.name read write lseek close unlink + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFull005, Function | MediumTest | Level1) +{ + off_t off; + int32_t ret; + int32_t fd = -1; + bool flag = true; + char readBuf = 'r'; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + char *writeBuf = (char*)malloc(WRITE_BUF_SIZE * sizeof(char)); + + (void)memset_s(writeBuf, WRITE_BUF_SIZE, 'w', WRITE_BUF_SIZE); + + fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = write(fd, writeBuf, WRITE_BUF_SIZE); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + off = lseek(fd, 0, SEEK_SET); + ICUNIT_GOTO_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off, EXIT); + + for (int i = 1; i <= TEST_OPEN_FILE_NUM; i++) { + ret = read(fd, &readBuf, 1); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + if (readBuf != 'w') { + flag = false; + break; + } + readBuf = 'r'; + off = lseek(fd, 7, SEEK_CUR); /* 7, offset distance */ + ICUNIT_GOTO_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off, EXIT); + } + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FULL006 + * @tc.name open fstat close unlink + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFull006, Function | MediumTest | Level1) +{ + int32_t ret; + struct stat buf = { 0 }; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = fstat(fd, &buf); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + ICUNIT_GOTO_EQUAL(buf.st_mode & S_IFMT, S_IFREG, POSIX_FS_IS_ERROR, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FULL007 + * @tc.name open stat close unlink + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFull007, Function | MediumTest | Level1) +{ + int32_t ret; + struct stat buf = { 0 }; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = stat(tmpFileName, &buf); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + ICUNIT_GOTO_EQUAL(buf.st_mode & S_IFMT, S_IFREG, POSIX_FS_IS_ERROR, EXIT); + +EXIT: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = stat(tmpFileName, &buf); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FULL008 + * @tc.name mkdir stat rmdir + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFull008, Function | MediumTest | Level1) +{ + char dirName[TEST_BUF_SIZE] = { TEST_DIRG }; + struct stat buf = { 0 }; + + int32_t ret = mkdir(dirName, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = stat(dirName, &buf); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + ICUNIT_GOTO_EQUAL(buf.st_mode & S_IFMT, S_IFDIR, POSIX_FS_IS_ERROR, EXIT1); + + ret = rmdir(dirName); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + + ret = stat(dirName, &buf); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + return POSIX_FS_NO_ERROR; + +EXIT1: + ret = rmdir(dirName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); +EXIT: + return POSIX_FS_IS_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_FULL009 + * @tc.name open close stat rename unlink + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsFull009, Function | MediumTest | Level1) +{ + int32_t ret; + struct stat buf = { 0 }; + char tmpFileName1[TEST_BUF_SIZE] = { FILE5 }; + char tmpFileName2[TEST_BUF_SIZE] = { FILE6 }; + + int32_t fd = open(tmpFileName1, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + + ret = stat(tmpFileName1, &buf); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + ICUNIT_GOTO_EQUAL(buf.st_mode & S_IFMT, S_IFREG, POSIX_FS_IS_ERROR, EXIT); + + ret = rename(tmpFileName1, tmpFileName2); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + ret = stat(tmpFileName1, &buf); + ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + + ret = stat(tmpFileName2, &buf); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + ICUNIT_GOTO_EQUAL(buf.st_mode & S_IFMT, S_IFREG, POSIX_FS_IS_ERROR, EXIT); + + ret = unlink(tmpFileName1); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName2); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; + +EXIT1: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); +EXIT: + ret = unlink(tmpFileName1); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +void PosixFsFullTest(void) +{ + RUN_ONE_TESTCASE(TestFsFull001); + RUN_ONE_TESTCASE(TestFsFull002); +#if (LOSCFG_SUPPORT_FATFS == 1) + RUN_ONE_TESTCASE(TestFsFull003); + RUN_ONE_TESTCASE(TestFsFull004); +#endif + RUN_ONE_TESTCASE(TestFsFull005); + RUN_ONE_TESTCASE(TestFsFull006); + RUN_ONE_TESTCASE(TestFsFull007); + RUN_ONE_TESTCASE(TestFsFull008); + RUN_ONE_TESTCASE(TestFsFull009); +} diff --git a/testsuites/unittest/posix/src/fs/posix_fs_func_test.c b/testsuites/unittest/posix/src/fs/posix_fs_func_test.c index d980df5b..69b9a470 100644 --- a/testsuites/unittest/posix/src/fs/posix_fs_func_test.c +++ b/testsuites/unittest/posix/src/fs/posix_fs_func_test.c @@ -33,18 +33,7 @@ #define __NEED_mode_t #endif -#include -#include -#include -#include "ohos_types.h" -#include "posix_test.h" -#include "los_config.h" -#include "kernel_test.h" -#include "log.h" -#include -#include -#include -#include +#include "posix_fs_test.h" /* * * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency @@ -54,31 +43,6 @@ */ LITE_TEST_SUIT(Posix, PosixFs, PosixFsFuncTestSuite); -#if (LOSCFG_SUPPORT_FATFS == 1) -#define TEST_ROOT "system" -#endif - -#if (LOSCFG_SUPPORT_LITTLEFS == 1) -#define TEST_ROOT "/littlefs" -#endif - -#define TEST_FILE_PTAH_RIGHT TEST_ROOT"/FILE0" /* file path, to open/rd/close */ -#define FILE0 "FILE0" /* common file name used for testing */ -#define FILE1 TEST_ROOT"/FILE1" /* common file under test root path name used for testing */ -#define FILE2 TEST_ROOT"/FILE2" /* common file under test root path name used for testing */ -#define DIRA TEST_ROOT"/a" /* common file under test root path name used for testing */ -#define FILE_IN_DIRA TEST_ROOT"/a/FILE0" /* common file under test root path name used for testing */ -#define DIRAB TEST_ROOT"/a/b" /* common file under test root path name used for testing */ -#define DIRAC TEST_ROOT"/a/c" /* common file under test root path name used for testing */ - -#define TEST_BUF_SIZE 40 /* 40, common data for test, no special meaning */ -#define TEST_SEEK_SIZE 10 /* 10, common data for test, no special meaning */ -#define TEST_RW_SIZE 20 /* 20, common data for test, no special meaning */ -#define TEST_LOOPUP_TIME 20 /* 100, common data for test, no special meaning */ - -#define TEST_MODE_NORMAL 0666 -#define TEST_MODE_HIGH 0777 - /* * * @tc.setup : setup for all testcases * @return : setup result, TRUE is success, FALSE is fail @@ -2012,9 +1976,8 @@ LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFcntl002, Function | MediumTest | Lev RUN_TEST_SUITE(PosixFsFuncTestSuite); -void PosixFsFuncTest() +void PosixFsAPITest(void) { - LOG("begin PosixFsFuncTest....\r\n"); RUN_ONE_TESTCASE(testFsFopenFclose001); RUN_ONE_TESTCASE(testFsFopenFclose002); RUN_ONE_TESTCASE(testFsFopenFclose003); @@ -2103,6 +2066,34 @@ void PosixFsFuncTest() RUN_ONE_TESTCASE(testFsFcntl001); RUN_ONE_TESTCASE(testFsFcntl002); #endif +} +void PosixFsFuncTest() +{ + PosixFsAPITest(); + PosixFsOpenTest(); + PosixFsCloseTest(); + PosixFsOpendirTest(); + PosixFsClosedirTest(); + PosixFsReadTest(); + PosixFsWriteTest(); + PosixFsReaddirTest(); + PosixFsMkdirTest(); + PosixFsRmdirTest(); + PosixFsLseekTest(); + PosixFsUnlinkTest(); + PosixFsStatTest(); + PosixFsFstatTest(); + PosixFsFsyncTest(); + PosixFsRenameTest(); +#if (LOSCFG_SUPPORT_FATFS == 1) + PosixFsStatfsTest(); +#endif + PosixFsFtruncateTest(); + PosixFsPreadTest(); + PosixFsPwriteTest(); + PosixFsAccessTest(); + PosixFsFullTest(); + PosixFsPressureTest(); return; } diff --git a/testsuites/unittest/posix/src/fs/posix_fs_test.h b/testsuites/unittest/posix/src/fs/posix_fs_test.h new file mode 100644 index 00000000..6f5c5ddb --- /dev/null +++ b/testsuites/unittest/posix/src/fs/posix_fs_test.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __POSIX_FS_TEST_H +#define __POSIX_FS_TEST_H + +#include "posix_test.h" +#include +#include +#include +#include "ohos_types.h" +#include "los_config.h" +#include "vfs_files.h" +#include "vfs_mount.h" +#include "vfs_maps.h" +#include "kernel_test.h" +#include "log.h" +#include +#include +#include +#include +#include "limits.h" + +#if (LOSCFG_SUPPORT_FATFS == 1) +#define TEST_ROOT "system" +#endif + +#if (LOSCFG_SUPPORT_LITTLEFS == 1) +#define TEST_ROOT "/littlefs" +#endif + +#define TEST_FILE_PTAH_RIGHT TEST_ROOT"/FILE0" /* file path, to open/rd/close */ +#define FILE0 "FILE0" /* common file name used for testing */ +#define FILE1 TEST_ROOT"/FILE1" /* common file under test root path name used for testing */ +#define FILE2 TEST_ROOT"/FILE2" /* common file under test root path name used for testing */ +#define FILE3 TEST_ROOT"/FILE3" /* common file under test root path name used for testing */ +#define FILE4 TEST_ROOT"/FILE4" /* common file under test root path name used for testing */ +#define FILE5 TEST_ROOT"/FILE5" /* common file under test root path name used for testing */ +#define FILE6 TEST_ROOT"/FILE6" /* common file under test root path name used for testing */ +#define DIRA TEST_ROOT"/a" /* common file under test root path name used for testing */ +#define DIRB TEST_ROOT"/b" /* common file under test root path name used for testing */ +#define DIRC TEST_ROOT"/c" /* common file under test root path name used for testing */ +#define DIRD TEST_ROOT"/d" /* common file under test root path name used for testing */ + +#define FILE_IN_DIRA TEST_ROOT"/a/FILE0" /* common file under test root path name used for testing */ +#define DIRAB TEST_ROOT"/a/b" /* common file under test root path name used for testing */ +#define DIRAC TEST_ROOT"/a/c" /* common file under test root path name used for testing */ + +#define TEST_BUF_SIZE 40 /* 40, common data for test, no special meaning */ +#define TEST_SEEK_SIZE 10 /* 10, common data for test, no special meaning */ +#define TEST_RW_SIZE 20 /* 20, common data for test, no special meaning */ +#define TEST_LOOPUP_TIME 20 /* 100, common data for test, no special meaning */ + +#define ERROR_CONFIG_NFILE_DESCRIPTORS 88888 /* 88888, common data for test, no special meaning */ +#define MODIFIED_FILE_SIZE 1024 /* 1024, common data for test, no special meaning */ + +#define TEST_MODE_NORMAL 0666 /* 0666, common data for test, no special meaning */ +#define TEST_MODE_HIGH 0777 /* 0777, common data for test, no special meaning */ + +#define POSIX_FS_IS_ERROR (-1) /* -1, common data for test, no special meaning */ +#define POSIX_FS_NO_ERROR 0 /* 0, common data for test, no special meaning */ + +#endif /* __POSIX_FS_TEST_H */ diff --git a/testsuites/unittest/posix/src/fs/pressure/posix_fs_pressure_test.c b/testsuites/unittest/posix/src/fs/pressure/posix_fs_pressure_test.c new file mode 100644 index 00000000..df6900c5 --- /dev/null +++ b/testsuites/unittest/posix/src/fs/pressure/posix_fs_pressure_test.c @@ -0,0 +1,292 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "posix_fs_test.h" +#include "vfs_config.h" + +#define MAX_OPEN_FILE_NUM 16 +#define PRESSURE_RUN_TIMES 200 +#define PRESSURE_RUN_TIMES003 150 +#define READ_WRITE_BUF_SIZE 1024 +#define PATH_MAX_NAME_LEN PATH_MAX +#define MAX_OPEN_DIRS_NUM LOSCFG_MAX_OPEN_DIRS +#define TEMP_FILE_PATH TEST_ROOT"/FILE7" +#define TEMP_DIR_PATH TEST_ROOT"/DIRE" + +/* * + * @tc.number SUB_KERNEL_FS_FRESSURE001 + * @tc.name open close unlink + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPressure001, Function | MediumTest | Level1) +{ + int32_t ret; + int32_t fd = -1; + char tmpFileName[PATH_MAX_NAME_LEN + 1] = TEST_ROOT"/"; + + int32_t len = strlen(TEST_ROOT); + for (int32_t i = len + 1; i < PATH_MAX_NAME_LEN - 1; i++) { + tmpFileName[i] = 'F'; + } + tmpFileName[PATH_MAX_NAME_LEN - 1] = '\0'; + + for (int32_t times = 0; times < PRESSURE_RUN_TIMES; times++) { + fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + + ret = unlink(tmpFileName); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + } + + tmpFileName[PATH_MAX_NAME_LEN - 1] = 'E'; + tmpFileName[PATH_MAX_NAME_LEN] = '\0'; + fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_EQUAL(errno, ENAMETOOLONG, POSIX_FS_IS_ERROR); + ICUNIT_ASSERT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + return POSIX_FS_NO_ERROR; + +EXIT1: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); +EXIT: + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_PRESSURE002 + * @tc.name open write lseek read close unlink + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPressure002, Function | MediumTest | Level1) +{ + off_t off; + int32_t ret; + int32_t fd = -1; + bool flag = true; + const char tmpFileName[TEST_BUF_SIZE] = FILE1; + char *writeBuf = (char *)malloc(READ_WRITE_BUF_SIZE * sizeof(char)); + char *readBuf = (char *)malloc(READ_WRITE_BUF_SIZE * sizeof(char)); + + (void)memset_s(writeBuf, READ_WRITE_BUF_SIZE, 'w', READ_WRITE_BUF_SIZE); + + for (int32_t times = 0; times < PRESSURE_RUN_TIMES; times++) { + (void)memset_s(readBuf, READ_WRITE_BUF_SIZE, 'r', READ_WRITE_BUF_SIZE); + fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = write(fd, writeBuf, READ_WRITE_BUF_SIZE / 2); /* 2, common data for test, no special meaning */ + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + ret = write(fd, writeBuf, READ_WRITE_BUF_SIZE / 2); /* 2, common data for test, no special meaning */ + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + + off = lseek(fd, 0, SEEK_SET); /* 0, offset distance */ + ICUNIT_GOTO_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off, EXIT1); + + for (int32_t i = 0; i < 8; i++) { /* 8, number of reads */ + ret = read(fd, readBuf + i * 128, 128); /* 128, length per read */ + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + } + + for (int32_t i = 0; i < READ_WRITE_BUF_SIZE; i++) { + ICUNIT_GOTO_EQUAL(writeBuf[i], readBuf[i], POSIX_FS_IS_ERROR, EXIT1); + } + + ret = close(fd); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + + ret = unlink(tmpFileName); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT); + } + + return POSIX_FS_NO_ERROR; + +EXIT1: + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); +EXIT: + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_ORESSURE003 + * @tc.name open fstat stat mkdir unlink rmdir close + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPressure003, Function | MediumTest | Level1) +{ + int res; + int32_t ret; + bool flagMkdir = true; + struct stat buf = { 0 }; + int32_t j, k, fd[MAX_OPEN_FILE_NUM]; + char tmpDirName[TEST_BUF_SIZE] = { 0 }; + char tmpFileName[TEST_BUF_SIZE] = { 0 }; + + for (int32_t times = 0; times < PRESSURE_RUN_TIMES003; times++) { + for (j = 0; j < MAX_OPEN_FILE_NUM; j++) { + res = sprintf_s(tmpFileName, TEST_BUF_SIZE, "%s%02d", TEMP_FILE_PATH, j); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + goto EXIT3; + } + fd[j] = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_GOTO_NOT_EQUAL(fd[j], POSIX_FS_IS_ERROR, fd[j], EXIT3); + } + for (int32_t i = 0; i < MAX_OPEN_FILE_NUM; i++) { + ret = fstat(fd[i], &buf); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT3); + ICUNIT_GOTO_EQUAL(buf.st_mode & S_IFMT, S_IFREG, POSIX_FS_IS_ERROR, EXIT3); + } + for (int32_t i = 0; i < MAX_OPEN_FILE_NUM; i++) { + ret = close(fd[i]); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT2); + } + for (int32_t i = 0; i < MAX_OPEN_FILE_NUM; i++) { + res = sprintf_s(tmpFileName, TEST_BUF_SIZE, "%s%02d", TEMP_FILE_PATH, i); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + goto EXIT1; + } + ret = stat(tmpFileName, &buf); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + ICUNIT_GOTO_EQUAL(buf.st_mode & S_IFMT, S_IFREG, POSIX_FS_IS_ERROR, EXIT1); + } + for (k = 0; k < MAX_OPEN_DIRS_NUM; k++) { + res = sprintf_s(tmpDirName, TEST_BUF_SIZE, "%s%02d", TEMP_DIR_PATH, k); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + goto EXIT1; + } + ret = mkdir(tmpDirName, TEST_MODE_HIGH); + if (ret == POSIX_FS_IS_ERROR) { + flagMkdir = false; + } + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + } + for (int32_t i = 0; i < MAX_OPEN_DIRS_NUM; i++) { + res = sprintf_s(tmpDirName, TEST_BUF_SIZE, "%s%02d", TEMP_DIR_PATH, i); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + goto EXIT1; + } + ret = stat(tmpDirName, &buf); + ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT1); + ICUNIT_GOTO_EQUAL(buf.st_mode & S_IFMT, S_IFDIR, POSIX_FS_IS_ERROR, EXIT1); + } + for (int32_t i = 0; i < MAX_OPEN_FILE_NUM; i++) { + res = sprintf_s(tmpFileName, TEST_BUF_SIZE, "%s%02d", TEMP_FILE_PATH, i); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + return POSIX_FS_IS_ERROR; + } + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + } + for (int32_t i = 0; i < MAX_OPEN_DIRS_NUM; i++) { + res = sprintf_s(tmpDirName, TEST_BUF_SIZE, "%s%02d", TEMP_DIR_PATH, i); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + return POSIX_FS_IS_ERROR; + } + ret = rmdir(tmpDirName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + } + for (int32_t i = 0; i < MAX_OPEN_FILE_NUM; i++) { + res = sprintf_s(tmpFileName, TEST_BUF_SIZE, "%s%02d", TEMP_FILE_PATH, i); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + return POSIX_FS_IS_ERROR; + } + ret = stat(tmpFileName, &buf); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + } + for (int32_t i = 0; i < MAX_OPEN_DIRS_NUM; i++) { + res = sprintf_s(tmpDirName, TEST_BUF_SIZE, "%s%02d", TEMP_DIR_PATH, i); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + return POSIX_FS_IS_ERROR; + } + ret = stat(tmpDirName, &buf); + ICUNIT_ASSERT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + } + } + return POSIX_FS_NO_ERROR; +EXIT3: + for (int32_t m = 0; m < j; m++) { + ret = close(fd[m]); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + } + goto EXIT1; +EXIT2: + for (int32_t m = j; m < MAX_OPEN_FILE_NUM; m++) { + ret = close(fd[m]); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + } +EXIT1: + for (int32_t m = 0; m < j; m++) { + res = sprintf_s(tmpFileName, TEST_BUF_SIZE, "%s%02d", TEMP_FILE_PATH, m); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + return POSIX_FS_IS_ERROR; + } + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + } + if (flagMkdir == false) { + goto EXIT; + } + return POSIX_FS_NO_ERROR; +EXIT: + for (int32_t m = 0; m < k; m++) { + res = sprintf_s(tmpDirName, TEST_BUF_SIZE, "%s%02d", TEMP_DIR_PATH, m); + if (res < 0) { + printf("[%s:%d] sprintf_s failed\n", __func__, __LINE__); + return POSIX_FS_IS_ERROR; + } + ret = rmdir(tmpDirName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + } + return POSIX_FS_NO_ERROR; +} + +void PosixFsPressureTest(void) +{ + RUN_ONE_TESTCASE(TestFsPressure001); + RUN_ONE_TESTCASE(TestFsPressure002); + RUN_ONE_TESTCASE(TestFsPressure003); +} diff --git a/testsuites/unittest/posix/src/posix_test.c b/testsuites/unittest/posix/src/posix_test.c index 30b16314..1494d7ba 100644 --- a/testsuites/unittest/posix/src/posix_test.c +++ b/testsuites/unittest/posix/src/posix_test.c @@ -62,4 +62,7 @@ void ItSuitePosix(void) PosixSemaphoreFuncTest(); PosixTimeFuncTest(); #endif +#if (LOSCFG_SUPPORT_LITTLEFS == 1 || LOSCFG_SUPPORT_FATFS == 1) + PosixFsFuncTest(); +#endif }