From f82bca6b3940fdff47d7bab6eae34173dcee122d Mon Sep 17 00:00:00 2001 From: liujia178 Date: Thu, 21 Dec 2023 16:31:58 +0800 Subject: [PATCH] Supplement NDK use case Issue: https://gitee.com/openharmony/third_party_musl/issues/I8PYVS?from=project-issue Test: NDK gtest Signed-off-by: liujia178 --- libc-test/README-HowtoRun-gtest.md | 156 ++++ .../src/functionalext/supplement/BUILD.gn | 61 ++ .../dirent_gtest/dirent_readdir64_r_test.cpp | 96 +++ .../dirent_gtest/dirent_scandir64_test.cpp | 84 +++ .../exit_gtest/exit_aeabi_atexit_test.cpp | 64 ++ .../fcntl_open_by_handle_at_test.cpp | 53 ++ .../fenv/fenv_gtest/fenv_except_test.cpp | 686 ++++++++++++++++++ .../fenv/fenv_gtest/fenv_fegetenv_test.cpp | 22 + .../fenv/fenv_gtest/fenv_fesetenv_test.cpp | 19 + .../fenv/fenv_gtest/fenv_fesetround_test.cpp | 43 ++ .../fortify_gtest/fortify_fread_chk_test.cpp | 49 ++ .../fortify_gtest/fortify_getcwd_chk_test.cpp | 33 + .../fortify_memmove_chk_test.cpp | 57 ++ .../fortify_gtest/fortify_memset_chk_test.cpp | 25 + .../fortify_gtest/fortify_open64_chk_test.cpp | 9 +- .../fortify_readlink_chk_test.cpp | 46 ++ .../fortify_stpncpy_chk_test.cpp | 28 + .../fortify_strncat_chk_test.cpp | 25 + .../ipc/ipc_gtest/ipc_semctl_test.cpp | 85 +++ .../ipc/ipc_gtest/ipc_semget_test.cpp | 25 + .../ipc/ipc_gtest/ipc_semop_test.cpp | 67 ++ .../legacy/legacy_gtest/legacy_err_test.cpp | 77 ++ .../legacy/legacy_gtest/legacy_errx_test.cpp | 75 ++ .../legacy/legacy_gtest/legacy_ftw64_test.cpp | 83 +++ .../legacy_gtest/legacy_valloc_test.cpp | 23 + .../linux_gtest/linux_fallocate64_test.cpp | 51 ++ .../linux_gtest/linux_getdents64_test.cpp | 51 ++ .../linux_gtest/linux_sendfile64_test.cpp | 33 + .../misc/misc_gtest/misc_wordexp_test.cpp | 23 + .../network_gtest/network_sethostent_test.cpp | 23 + .../process_gtest/process_execle_test.cpp | 42 ++ .../process_gtest/process_fork_test.cpp | 58 ++ .../regex/regex_gtest/regex_regcomp_test.cpp | 42 ++ .../regex/regex_gtest/regex_regerror_test.cpp | 45 ++ .../regex/regex_gtest/regex_regfree_test.cpp | 24 + .../sched_sched_getscheduler_test.cpp | 29 + .../search_gtest/search_remque_test.cpp | 34 + .../signal/signal_gtest/signal_raise_test.cpp | 89 +++ .../stat/stat_gtest/stat_fstatfs64_test.cpp | 8 +- .../stdio/stdio_gtest/stdio_fdopen_test.cpp | 48 ++ .../stdio_fflush_unlocked_test.cpp | 1 + .../stdio_gtest/stdio_fgetc_unlocked_test.cpp | 55 ++ .../stdio/stdio_gtest/stdio_fileno_test.cpp | 75 ++ .../stdio/stdio_gtest/stdio_fopen64_test.cpp | 60 ++ .../stdio/stdio_gtest/stdio_fpurge_test.cpp | 1 + .../stdio_gtest/stdio_fputs_unlocked_test.cpp | 26 + .../stdio_gtest/stdio_fread_unlocked_test.cpp | 56 ++ .../stdio_gtest/stdio_freopen64_test.cpp | 31 + .../stdio/stdio_gtest/stdio_fseeko64_test.cpp | 105 +++ .../stdio_gtest/stdio_fsetpos64_test.cpp | 36 + .../stdio/stdio_gtest/stdio_ftello64_test.cpp | 46 ++ .../stdio/stdio_gtest/stdio_ftello_test.cpp | 137 ++++ .../stdio_gtest/stdio_funlockfile_test.cpp | 56 ++ .../stdio_fwrite_unlocked_test.cpp | 49 ++ .../stdio/stdio_gtest/stdio_fwscanf_test.cpp | 38 + .../stdio/stdio_gtest/stdio_printf_test.cpp | 120 +++ .../stdio/stdio_gtest/stdio_scanf_test.cpp | 45 ++ .../stdio/stdio_gtest/stdio_wscanf_test.cpp | 47 ++ .../string_gtest/string_strcasecmp_l_test.cpp | 24 + .../string_strncasecmp_l_test.cpp | 29 + .../sys/sys_gtest/sys_preadv64_test.cpp | 93 +++ .../sys/sys_gtest/sys_prlimit64_test.cpp | 51 ++ .../sys_gtest/sys_process_vm_writev_test.cpp | 71 ++ .../sys/sys_gtest/sys_pwritev64_test.cpp | 38 + .../termios_gtest/termios_tcflush_test.cpp | 29 + .../unistd/unistd_gtest/unistd_sync_test.cpp | 39 + 66 files changed, 3941 insertions(+), 8 deletions(-) create mode 100644 libc-test/README-HowtoRun-gtest.md create mode 100644 libc-test/src/functionalext/supplement/dirent/dirent_gtest/dirent_readdir64_r_test.cpp create mode 100644 libc-test/src/functionalext/supplement/dirent/dirent_gtest/dirent_scandir64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/exit/exit_gtest/exit_aeabi_atexit_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fcntl/fcntl_gtest/fcntl_open_by_handle_at_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_except_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fegetenv_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fesetenv_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fesetround_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_fread_chk_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_getcwd_chk_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_memmove_chk_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_memset_chk_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_readlink_chk_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_stpncpy_chk_test.cpp create mode 100644 libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_strncat_chk_test.cpp create mode 100644 libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semctl_test.cpp create mode 100644 libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semget_test.cpp create mode 100644 libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semop_test.cpp create mode 100644 libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_err_test.cpp create mode 100644 libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_errx_test.cpp create mode 100644 libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_ftw64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_valloc_test.cpp create mode 100644 libc-test/src/functionalext/supplement/linux/linux_gtest/linux_fallocate64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/linux/linux_gtest/linux_getdents64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/linux/linux_gtest/linux_sendfile64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/misc/misc_gtest/misc_wordexp_test.cpp create mode 100644 libc-test/src/functionalext/supplement/network/network_gtest/network_sethostent_test.cpp create mode 100644 libc-test/src/functionalext/supplement/process/process_gtest/process_execle_test.cpp create mode 100644 libc-test/src/functionalext/supplement/process/process_gtest/process_fork_test.cpp create mode 100644 libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regcomp_test.cpp create mode 100644 libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regerror_test.cpp create mode 100644 libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regfree_test.cpp create mode 100644 libc-test/src/functionalext/supplement/sched/sched_gtest/sched_sched_getscheduler_test.cpp create mode 100644 libc-test/src/functionalext/supplement/search/search_gtest/search_remque_test.cpp create mode 100644 libc-test/src/functionalext/supplement/signal/signal_gtest/signal_raise_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fdopen_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fgetc_unlocked_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fileno_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fopen64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fputs_unlocked_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fread_unlocked_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_freopen64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fseeko64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fsetpos64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_ftello64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_ftello_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_funlockfile_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fwrite_unlocked_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fwscanf_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_printf_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_scanf_test.cpp create mode 100644 libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_wscanf_test.cpp create mode 100644 libc-test/src/functionalext/supplement/string/string_gtest/string_strcasecmp_l_test.cpp create mode 100644 libc-test/src/functionalext/supplement/string/string_gtest/string_strncasecmp_l_test.cpp create mode 100644 libc-test/src/functionalext/supplement/sys/sys_gtest/sys_preadv64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/sys/sys_gtest/sys_prlimit64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/sys/sys_gtest/sys_process_vm_writev_test.cpp create mode 100644 libc-test/src/functionalext/supplement/sys/sys_gtest/sys_pwritev64_test.cpp create mode 100644 libc-test/src/functionalext/supplement/termios/termios_gtest/termios_tcflush_test.cpp create mode 100644 libc-test/src/functionalext/supplement/unistd/unistd_gtest/unistd_sync_test.cpp diff --git a/libc-test/README-HowtoRun-gtest.md b/libc-test/README-HowtoRun-gtest.md new file mode 100644 index 000000000..34d698c44 --- /dev/null +++ b/libc-test/README-HowtoRun-gtest.md @@ -0,0 +1,156 @@ +## libc-gtest脚本使用说明 + +### 一、编译 + +编译命令,仅编译libc-test测试用例 + +``` +./build.sh --product-name rk3568 --build-target libc_gtest +``` + +用例可执行文件生成的目录位于 `out/rk3568/musl/libc_gtest` +以及相关的动态库位于 `out/rk3568/musl/libc-gtest-lib` + +### 二、执行测试 + +1. 将HDC(Windows版)工具hdc_std.exe加入到windows环境变量。连接上开发板,保证HDC工具可以连接。 +2. 将out/rk3568/musl/libc-gtest-lib/中的所有so拷贝到windows测试环境的某个目录下。 +3. 进入musl/scripts,将run_libcgtest_windows.bat脚本复制到windows环境中,并修改脚本中的LOCAL_DYNLIB,将其设定为步骤2中保存so的目录(必要)。 + + ```bat + @REM Local storage directory of dynamic link library + set LOCAL_DYNLIB="your_local_dir" + ``` +4. windows端双击运行run_libcgtest_windows.bat,将测试用so文件推送到开发板中。 +5. 参考[https://gitee.com/openharmony/testfwk_developer_test](https://gitee.com/openharmony/testfwk_developer_test)的“搭建执行环境"一节,在windows环境下创建测试框架目录Test,从Linux环境拷贝测试框架test/testfwk/developer_test和test/testfwk/xdevice到创建的Test目录下。 +6. 在windows的Test目录下,创建unittest目录,在unittest目录下,创建musl目录。将out/rk3568/musl/libc-gtest/中的musl_unittest测试用例可执行文件拷贝到musl目录中。 +7. 修改developer_test/config/中的user_config.xml。 + + ``` + + + false + + + + + + + + + + D:\Test\testcase\tests + + ``` +8. 在windows的developer_test目录下,双击执行start.bat。输入run -t UT -tp musl命令,执行musl的gtest测试用例。 +9. 用例执行结束后,在developer_test/report目录下会生成测试报告。 + +### 三、测试用zip文件的生成方式 + +编译libc_gtest后,在out/rk3568/musl/libc-gtest-lib/路径下会存在用于测试加载zip包中链接库所使用的zip文件。这些文件zip文件是事先做好,并放到源码路径中的。然后在编译libc_gtest时,从源码路径/third_party/musl/libc-test/src/functionalext/supplement/ldso/ldso_gtest/libs/ziplibs_32(编译arm时)或/third_party/musl/libc-test/src/functionalext/supplement/ldso/ldso_gtest/libs/ziplibs_64(编译aarch64时)拷贝到out/rk3568/musl/libc-gtest-lib/中的。 + +*※可被用于加载的zip文件,要满足不被压缩,且对齐两个条件。* + +下面是加载功能的gtest测试用例中使用到的zip文件的生成方法。 + +前提条件:安装zipalign工具。 + +1. ziplibs_32/libzipalign.zip生成方法。 + + ``` + ①使用./build.sh --product-name rk3568 --build-target libc_gtest命令编译libc_gtest,编译后out/rk3568/musl/libc-gtest-lib/中会生成libdlopen_common.so。 + ②生成未压缩的zip文件。将编译出来的libdlopen_common.so使用zip -0 tmp.zip libdlopen_common.so命令生成未压缩的tmp.zip文件。 + ③生成对齐的zip文件。使用zipalign -p -f -v 4 tmp.zip libzipalign.zip命令生成对齐的libzipalign.zip。 + ``` +2. ziplibs_32/libzipalign_rpath.zip生成方法。 + + ``` + ①使用./build.sh --product-name rk3568 --build-target libc_gtest命令编译libc_gtest,编译后out/rk3568/musl/libc-gtest-lib/中会生成libdlopen_rpath.so。 + ②生成未压缩的zip文件。将编译出来的libdlopen_rpath.so使用zip -0 tmp.zip libdlopen_rpath.so命令生成未压缩的tmp.zip文件。 + ③生成对齐的zip文件。使用zipalign -p -f -v 4 tmp.zip libzipalign_rpath.zip命令生成对齐的libzipalign_rpath.zip。 + ``` +3. ziplibs_32/libzipalign_lspath.zip生成方法。 + + ``` + ①本地修改/third_party/musl/libc-test/src/functionalext/supplement/ldso/ldso_gtest/libs/BUILD.gn文件,将dlopen_zip_test添加到group(ldso_libs)的deps中,使最终能够生成libdlopen_zip_test.so。使用./build.sh --product-name rk3568 --build-target libc_gtest命令编译libc_gtest,编译后out/rk3568/musl/libc-gtest-lib/中会生成libdlopen_zip_test.so。 + ②生成未压缩的zip文件。新建libso文件夹,将编译出来的libdlopen_zip_test.so放到libso中,使用zip -0 tmp.zip libso/libdlopen_zip_test.so命令生成未压缩的tmp.zip文件。 + ③生成对齐的zip文件。使用zipalign -p -f -v 4 tmp.zip libzipalign_lspath.zip命令生成对齐的libzipalign_lspath.zip。 + ``` +4. ziplibs_64/libzipalign.zip生成方法。 + + ``` + ①使用./build.sh --product-name rk3568 --target-cpu arm64 --build-target libc_gtest命令编译libc_gtest,编译后out/rk3568/musl/libc-gtest-lib/中会生成libdlopen_common.so。 + ②生成未压缩的zip文件。将编译出来的libdlopen_common.so使用zip -0 tmp.zip libdlopen_common.so命令生成未压缩的tmp.zip文件。 + ③生成对齐的zip文件。使用zipalign -p -f -v 4 tmp.zip libzipalign.zip命令生成对齐的libzipalign.zip。 + ``` +5. ziplibs_64/libzipalign_rpath.zip生成方法。 + + ``` + ①使用./build.sh --product-name rk3568 --target-cpu arm64 --build-target libc_gtest命令编译libc_gtest,编译后out/rk3568/musl/libc-gtest-lib/中会生成libdlopen_rpath.so。 + ②生成未压缩的zip文件。将编译出来的libdlopen_rpath.so使用zip -0 tmp.zip libdlopen_rpath.so命令生成未压缩的tmp.zip文件。 + ③生成对齐的zip文件。使用zipalign -p -f -v 4 tmp.zip libzipalign_rpath.zip命令生成对齐的libzipalign_rpath.zip。 + ``` +6. ziplibs_64/libzipalign_lspath.zip生成方法。 + + ``` + ①本地修改/third_party/musl/libc-test/src/functionalext/supplement/ldso/ldso_gtest/libs/BUILD.gn文件,将dlopen_zip_test添加到group(ldso_libs)的deps中,使最终能够生成libdlopen_zip_test.so。使用./build.sh --product-name rk3568 --target-cpu arm64 --build-target libc_gtest命令编译libc_gtest,编译后out/rk3568/musl/libc-gtest-lib/中会生成libdlopen_zip_test.so。 + ②生成未压缩的zip文件。新建libso文件夹,将编译出来的libdlopen_zip_test.so放到libso中,使用zip -0 tmp.zip libso/libdlopen_zip_test.so命令生成未压缩的tmp.zip文件。 + ③生成对齐的zip文件。使用zipalign -p -f -v 4 tmp.zip libzipalign_lspath.zip命令生成对齐的libzipalign_lspath.zip。 + ``` + +## 四、增加用例 + + 编写gtest测试用例的步骤如下(以dlopen为例): + +1. 添加测试文件 + + 在libc-test/src/functionalext/supplement/对应的模块下创建目录xx_gtest,存放测试文件。 + + 测试接口dlopen,添加对应的测试文件:libc-test\src\functionalext\supplement\ldso\ldso_gtest\ldso_dlopen_test.cpp + +2. 引用测试框架头文件和命名空间 + + ``` + #include + + using namespace testing::ext; + ``` + +3. 添加被测试类的头文件 + + ``` + #include + ``` + +4. 定义测试类 + + ``` + class LdsoDlopenTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} + }; + ``` + +5. 测试用例实现,包含用例注释和逻辑实现 + + ``` + /** + * @tc.name: dlopen_001 + * @tc.desc: Test the loading order of dynamic link libraries. + * libdlopen_relocation.so(RelocationTest, calls RelocationTestOrder) + * |-----1.so(RelocationTestOrder)->expect value + * |-----2.so(RelocationTestOrder) + * @tc.type: FUNC + */ + HWTEST_F(LdsoDlopenTest, dlopen_001, TestSize.Level1) + { + void* handle = dlopen("libdlopen_relocation.so", RTLD_NOW); + ASSERT_NE(handle, nullptr); + + FuncTypeRetInt fn = reinterpret_cast(dlsym(handle, "RelocationTest")); + ASSERT_NE(fn, nullptr); + EXPECT_EQ(EXPECT_RETURN_VALUE_15, fn()); + + dlclose(handle); + } + ``` diff --git a/libc-test/src/functionalext/supplement/BUILD.gn b/libc-test/src/functionalext/supplement/BUILD.gn index 93463c146..9064822a4 100644 --- a/libc-test/src/functionalext/supplement/BUILD.gn +++ b/libc-test/src/functionalext/supplement/BUILD.gn @@ -147,6 +147,28 @@ ohos_executable("musl_unittest") { "malloc/malloc_gtest/malloc_malloc_usable_size_test.cpp", # math unittest + "dirent/dirent_gtest/dirent_readdir64_r_test.cpp", + "dirent/dirent_gtest/dirent_scandir64_test.cpp", + "fcntl/fcntl_gtest/fcntl_open_by_handle_at_test.cpp", + "fenv/fenv_gtest/fenv_except_test.cpp", + "fenv/fenv_gtest/fenv_fegetenv_test.cpp", + "fenv/fenv_gtest/fenv_fesetenv_test.cpp", + "fenv/fenv_gtest/fenv_fesetround_test.cpp", + "fortify/fortify_gtest/fortify_fread_chk_test.cpp", + "fortify/fortify_gtest/fortify_getcwd_chk_test.cpp", + "fortify/fortify_gtest/fortify_memmove_chk_test.cpp", + "fortify/fortify_gtest/fortify_memset_chk_test.cpp", + "fortify/fortify_gtest/fortify_readlink_chk_test.cpp", + "fortify/fortify_gtest/fortify_stpncpy_chk_test.cpp", + "fortify/fortify_gtest/fortify_strncat_chk_test.cpp", + "ipc/ipc_gtest/ipc_semctl_test.cpp", + "ipc/ipc_gtest/ipc_semget_test.cpp", + "ipc/ipc_gtest/ipc_semop_test.cpp", + "legacy/legacy_gtest/legacy_err_test.cpp", + "legacy/legacy_gtest/legacy_errx_test.cpp", + "legacy/legacy_gtest/legacy_ftw64_test.cpp", + "legacy/legacy_gtest/legacy_valloc_test.cpp", + "linux/linux_gtest/linux_getdents64_test.cpp", "math/math_gtest/complex_test.cpp", "math/math_gtest/math_acos_test.cpp", "math/math_gtest/math_acosh_test.cpp", @@ -227,6 +249,7 @@ ohos_executable("musl_unittest") { "misc/misc_gtest/misc_setpriority_test.cpp", "misc/misc_gtest/misc_syscall_test.cpp", "misc/misc_gtest/misc_uname_test.cpp", + "misc/misc_gtest/misc_wordexp_test.cpp", "mman/mman_gtest/mman_madvise_test.cpp", "mman/mman_gtest/mman_mmap_test.cpp", "mman/mman_gtest/mman_mprotect_test.cpp", @@ -240,18 +263,27 @@ ohos_executable("musl_unittest") { "network/network_gtest/netinet_ether_test.cpp", "network/network_gtest/network_herror_test.cpp", "network/network_gtest/network_res_search_test.cpp", + "network/network_gtest/network_sethostent_test.cpp", "network/network_gtest/resolv_test.cpp", "network/network_gtest/socket_test.cpp", "passwd/passwd_gtest/passwd_endgrent_test.cpp", "prng/prng_gtest/prng_rand_r_test.cpp", "prng/prng_gtest/prng_rand_test.cpp", + "process/process_gtest/process_execle_test.cpp", + "process/process_gtest/process_fork_test.cpp", "process/process_gtest/process_unsetenv_test.cpp", "regex/regex_gtest/regex_fnmatch_test.cpp", + "regex/regex_gtest/regex_regcomp_test.cpp", + "regex/regex_gtest/regex_regerror_test.cpp", + "regex/regex_gtest/regex_regfree_test.cpp", + "sched/sched_gtest/sched_sched_getscheduler_test.cpp", + "search/search_gtest/search_remque_test.cpp", "search/search_gtest/search_tdelete_test.cpp", "search/search_gtest/search_tfind_test.cpp", "select/select_gtest/select_poll_test.cpp", "setjmp/setjmp_gtest/setjmp_setjmp_test.cpp", "sigchain/sigchain_gtest/sigchain_add_special_signal_handler_test.cpp", + "signal/signal_gtest/signal_raise_test.cpp", "signal/signal_gtest/signal_sigaddset_test.cpp", "signal/signal_gtest/signal_sigemptyset_test.cpp", "signal/signal_gtest/signal_sigtimedwait_test.cpp", @@ -268,18 +300,35 @@ ohos_executable("musl_unittest") { "stat/stat_gtest/stat_statvfs64_test.cpp", "stat/stat_gtest/stat_umask_test.cpp", "stdio/stdio_gtest/stdio_clearerr_unlocked_test.cpp", + "stdio/stdio_gtest/stdio_fdopen_test.cpp", "stdio/stdio_gtest/stdio_feof_unlocked_test.cpp", "stdio/stdio_gtest/stdio_ferror_test.cpp", "stdio/stdio_gtest/stdio_ferror_unlocked_test.cpp", "stdio/stdio_gtest/stdio_fflush_unlocked_test.cpp", + "stdio/stdio_gtest/stdio_fgetc_unlocked_test.cpp", "stdio/stdio_gtest/stdio_fgetpos64_test.cpp", + "stdio/stdio_gtest/stdio_fileno_test.cpp", "stdio/stdio_gtest/stdio_fileno_unlocked_test.cpp", + "stdio/stdio_gtest/stdio_fopen64_test.cpp", "stdio/stdio_gtest/stdio_fpurge_test.cpp", "stdio/stdio_gtest/stdio_fputc_unlocked_test.cpp", + "stdio/stdio_gtest/stdio_fputs_unlocked_test.cpp", + "stdio/stdio_gtest/stdio_fread_unlocked_test.cpp", + "stdio/stdio_gtest/stdio_freopen64_test.cpp", + "stdio/stdio_gtest/stdio_fseeko64_test.cpp", "stdio/stdio_gtest/stdio_fsetlocking_test.cpp", + "stdio/stdio_gtest/stdio_fsetpos64_test.cpp", + "stdio/stdio_gtest/stdio_ftello64_test.cpp", + "stdio/stdio_gtest/stdio_ftello_test.cpp", + "stdio/stdio_gtest/stdio_funlockfile_test.cpp", + "stdio/stdio_gtest/stdio_fwrite_unlocked_test.cpp", + "stdio/stdio_gtest/stdio_fwscanf_test.cpp", "stdio/stdio_gtest/stdio_gets_test.cpp", "stdio/stdio_gtest/stdio_getwchar_test.cpp", "stdio/stdio_gtest/stdio_perror_test.cpp", + "stdio/stdio_gtest/stdio_printf_test.cpp", + "stdio/stdio_gtest/stdio_scanf_test.cpp", + "stdio/stdio_gtest/stdio_wscanf_test.cpp", "stdlib/stdlib_gtest/stdlib_arc4random_test.cpp", "stdlib/stdlib_gtest/stdlib_div_test.cpp", "string/string_gtest/string_bcmp_test.cpp", @@ -289,6 +338,7 @@ ohos_executable("musl_unittest") { "string/string_gtest/string_memcpy_test.cpp", "string/string_gtest/string_memrchr_test.cpp", "string/string_gtest/string_memset_test.cpp", + "string/string_gtest/string_strcasecmp_l_test.cpp", "string/string_gtest/string_strcasecmp_test.cpp", "string/string_gtest/string_strcat_test.cpp", "string/string_gtest/string_strchr_test.cpp", @@ -297,6 +347,7 @@ ohos_executable("musl_unittest") { "string/string_gtest/string_strcspn_test.cpp", "string/string_gtest/string_strdup_test.cpp", "string/string_gtest/string_strlen_test.cpp", + "string/string_gtest/string_strncasecmp_l_test.cpp", "string/string_gtest/string_strncasecmp_test.cpp", "string/string_gtest/string_strncmp_test.cpp", "string/string_gtest/string_strncpy_test.cpp", @@ -307,9 +358,14 @@ ohos_executable("musl_unittest") { "string/string_gtest/string_strstr_test.cpp", "string/string_gtest/string_wmemchr_test.cpp", "string/string_gtest/string_wmemcmp_test.cpp", + "sys/sys_gtest/sys_preadv64_test.cpp", + "sys/sys_gtest/sys_prlimit64_test.cpp", + "sys/sys_gtest/sys_process_vm_writev_test.cpp", + "sys/sys_gtest/sys_pwritev64_test.cpp", "temp/temp_gtest/temp_mkostemp64_test.cpp", "temp/temp_gtest/temp_mkstemp64_test.cpp", "temp/temp_gtest/temp_mkstemps64_test.cpp", + "termios/termios_gtest/termios_tcflush_test.cpp", "thread/thread_gtest/pthread_attr_test.cpp", "thread/thread_gtest/pthread_barrier_test.cpp", "thread/thread_gtest/pthread_cleanup_test.cpp", @@ -349,12 +405,17 @@ ohos_executable("musl_unittest") { "unistd/unistd_gtest/unistd_read_test.cpp", "unistd/unistd_gtest/unistd_readlink_test.cpp", "unistd/unistd_gtest/unistd_rmdir_test.cpp", + "unistd/unistd_gtest/unistd_sync_test.cpp", "unistd/unistd_gtest/unistd_unlink_test.cpp", "unistd/unistd_gtest/unistd_usleep_test.cpp", "unistd/unistd_gtest/unistd_write_test.cpp", "unistd/unistd_gtest/unistd_writev_test.cpp", ] + if (musl_arch == "arm") { + sources += [ "exit/exit_gtest/exit_aeabi_atexit_test.cpp" ] + } + ldflags = [ "-Wl,-rpath=/data/tmp/libcgtest/libs:/data/tmp/libcgtest/libs/rpath-test/", ] diff --git a/libc-test/src/functionalext/supplement/dirent/dirent_gtest/dirent_readdir64_r_test.cpp b/libc-test/src/functionalext/supplement/dirent/dirent_gtest/dirent_readdir64_r_test.cpp new file mode 100644 index 000000000..de8693683 --- /dev/null +++ b/libc-test/src/functionalext/supplement/dirent/dirent_gtest/dirent_readdir64_r_test.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include + +using namespace testing::ext; + +constexpr int PATH_SIZE = 1024; + +class DirentReaddir64RTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: readdir64_r_001 + * @tc.desc: Verify whether the readdir64_r interface correctly returns the directory and file entries within a folder + * that contains subdirectories when reading it, while excluding the special directory entries "." and "..". + * @tc.type: FUNC + */ +HWTEST_F(DirentReaddir64RTest, readdir64_r_001, TestSize.Level1) +{ + const char* dirPath = "test-dir"; + const char* subDirPath = "test-dir/subdir"; + mkdir(dirPath, 0700); + mkdir(subDirPath, 0700); + FILE* file = fopen("test-dir/testFile.txt", "w"); + DIR* dir = opendir(dirPath); + ASSERT_NE(dir, nullptr); + struct dirent64 entry; + struct dirent64* result; + std::vector fileNames; + while (readdir64_r(dir, &entry, &result) == 0 && result != nullptr) { + if (strcmp(result->d_name, ".") != 0 && strcmp(result->d_name, "..") != 0) { + fileNames.push_back(result->d_name); + } + } + ASSERT_NE(std::find(fileNames.begin(), fileNames.end(), "testFile.txt"), fileNames.end()); + ASSERT_NE(std::find(fileNames.begin(), fileNames.end(), "subdir"), fileNames.end()); + fclose(file); + EXPECT_EQ(0, rmdir(subDirPath)); + EXPECT_EQ(0, remove("test-dir/testFile.txt")); + closedir(dir); + EXPECT_EQ(0, rmdir(dirPath)); +} + +/** + * @tc.name: readdir64_r_002 + * @tc.desc: Verify whether the readdir64_r interface correctly returns the directory entry ".." for the parent + * directory when reading an empty folder. + * @tc.type: FUNC + */ +HWTEST_F(DirentReaddir64RTest, readdir64_r_002, TestSize.Level1) +{ + char tempPath[PATH_SIZE] = "test-dir"; + mkdir(tempPath, 0700); + DIR* dir = opendir(tempPath); + ASSERT_NE(dir, nullptr); + struct dirent64 entry; + struct dirent64* result; + int res = readdir64_r(dir, &entry, &result); + EXPECT_EQ(res, 0); + EXPECT_TRUE(strcmp(result->d_name, "..") == 0 || strcmp(result->d_name, ".") == 0); + closedir(dir); + EXPECT_EQ(0, rmdir(tempPath)); +} + +/** + * @tc.name: readdir64_r_003 + * @tc.desc: Verify whether the readdir64_r interface correctly returns the file entries within a non-empty folder when + * reading it, while excluding the special directory entries "." and "..". + * @tc.type: FUNC + */ +HWTEST_F(DirentReaddir64RTest, readdir64_r_003, TestSize.Level1) +{ + const char* dirPath = "test-dir"; + mkdir(dirPath, 0700); + FILE* file = fopen("test-dir/testFile.txt", "w"); + DIR* dir = opendir(dirPath); + ASSERT_NE(dir, nullptr); + struct dirent64 entry; + struct dirent64* result = nullptr; + std::vector fileNames; + while (readdir64_r(dir, &entry, &result) == 0 && result != nullptr) { + if (strcmp(result->d_name, ".") != 0 && strcmp(result->d_name, "..") != 0) { + fileNames.push_back(result->d_name); + } + } + EXPECT_TRUE(fileNames.size() > 0); + ASSERT_NE(std::find(fileNames.begin(), fileNames.end(), "testFile.txt"), fileNames.end()); + fclose(file); + EXPECT_EQ(0, remove("test-dir/testFile.txt")); + closedir(dir); + EXPECT_EQ(0, rmdir(dirPath)); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/dirent/dirent_gtest/dirent_scandir64_test.cpp b/libc-test/src/functionalext/supplement/dirent/dirent_gtest/dirent_scandir64_test.cpp new file mode 100644 index 000000000..81f9c116a --- /dev/null +++ b/libc-test/src/functionalext/supplement/dirent/dirent_gtest/dirent_scandir64_test.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include + +using namespace testing::ext; +using namespace std; + +class DirentScandir64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: scandir64_001 + * @tc.desc: Simulate the scenario of a non-existent directory, and verify that when an non-existent directory is passed + * in, the scandir64 function correctly returns -1, sets the error code to ENOENT, and returns a null result + * pointer. + * @tc.type: FUNC + */ +HWTEST_F(DirentScandir64Test, scandir64_001, TestSize.Level1) +{ + dirent64** result = nullptr; + errno = 0; + int dirResult64 = scandir64("/fake-dir", &result, nullptr, nullptr); + EXPECT_TRUE(dirResult64 == -1); + EXPECT_TRUE(result == nullptr); + EXPECT_EQ(errno, ENOENT); +} + +/** + * @tc.name: scandir64_002 + * @tc.desc: Verify the behavior of the scandir64 interface when reading files in a specified directory, and checks for + * the existence of specific files. The expected outcome is that the file named testFile.txt is present. + * @tc.type: FUNC + */ +HWTEST_F(DirentScandir64Test, scandir64_002, TestSize.Level1) +{ + const char* testDirectory = "test_directory"; + mkdir(testDirectory, 0777); + FILE* file = fopen("test_directory/testFile.txt", "w"); + fclose(file); + std::vector directoryContents; + struct dirent** fileList; + int fileNum = scandir64(testDirectory, &fileList, nullptr, alphasort); + ASSERT_NE(fileList, nullptr); + EXPECT_GE(fileNum, 0); + for (int i = 0; i < fileNum; i++) { + directoryContents.push_back(fileList[i]->d_name); + free(fileList[i]); + } + free(fileList); + ASSERT_NE(find(directoryContents.begin(), directoryContents.end(), "testFile.txt"), directoryContents.end()); + remove("test_directory/testFile.txt"); + rmdir(testDirectory); +} + +/** + * @tc.name: scandir64_003 + * @tc.desc: Verify the behavior of the scandir64 interface when reading an empty directory. The expected outcome is + * that the container should not contain any files. + * @tc.type: FUNC + */ +HWTEST_F(DirentScandir64Test, scandir64_003, TestSize.Level1) +{ + const char* testDirectory = "empty_directory"; + mkdir(testDirectory, 0777); + std::vector directoryContents; + struct dirent** fileList; + int fileNum = scandir64(testDirectory, &fileList, nullptr, alphasort); + ASSERT_NE(fileList, nullptr); + EXPECT_GE(fileNum, 0); + for (int i = 0; i < fileNum; i++) { + if (strcmp(fileList[i]->d_name, ".") != 0 && strcmp(fileList[i]->d_name, "..") != 0) { + directoryContents.push_back(fileList[i]->d_name); + } + free(fileList[i]); + } + free(fileList); + EXPECT_TRUE(directoryContents.empty()); + rmdir(testDirectory); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/exit/exit_gtest/exit_aeabi_atexit_test.cpp b/libc-test/src/functionalext/supplement/exit/exit_gtest/exit_aeabi_atexit_test.cpp new file mode 100644 index 000000000..e003a3f2d --- /dev/null +++ b/libc-test/src/functionalext/supplement/exit/exit_gtest/exit_aeabi_atexit_test.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +using namespace testing::ext; + +constexpr int NUM = 123; + +class ExitAeabiAtexitTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +extern "C" int __aeabi_atexit(void* obj, void (*func)(void*), void* d); + +void CleanUp(void* arg) +{ + testing::internal::CaptureStdout(); + std::cout << "Hello, world!"; + std::string output = testing::internal::GetCapturedStdout(); + EXPECT_STREQ("Hello, world!", output.c_str()); +} + +void Exit(void* arg) +{ + EXPECT_EQ(NUM, *reinterpret_cast(arg)); +} + +/** + * @tc.name: __aeabi_atexit_001 + * @tc.desc: Test __aeabi_atexit by determining whether the registration function has been called. + * @tc.type: FUNC + */ +HWTEST_F(ExitAeabiAtexitTest, __aeabi_atexit_001, TestSize.Level1) +{ + pid_t pid = fork(); + if (pid == 0) { + EXPECT_EQ(0, __aeabi_atexit(nullptr, &CleanUp, nullptr)); + exit(0); + } else if (pid > 0) { + waitpid(pid, nullptr, 0); + } else { + FAIL() << "fork() failed!"; + } +} + +/** + * @tc.name: __aeabi_atexit_002 + * @tc.desc: Test __aeabi_atexit by determining whether the registration function has been called. + * @tc.type: FUNC + */ +HWTEST_F(ExitAeabiAtexitTest, __aeabi_atexit_002, TestSize.Level1) +{ + pid_t pid = fork(); + if (pid == 0) { + int num = NUM; + EXPECT_EQ(0, __aeabi_atexit(&num, &Exit, nullptr)); + exit(0); + } else if (pid > 0) { + waitpid(pid, nullptr, 0); + } else { + FAIL() << "fork() failed!"; + } +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fcntl/fcntl_gtest/fcntl_open_by_handle_at_test.cpp b/libc-test/src/functionalext/supplement/fcntl/fcntl_gtest/fcntl_open_by_handle_at_test.cpp new file mode 100644 index 000000000..f7a13e11b --- /dev/null +++ b/libc-test/src/functionalext/supplement/fcntl/fcntl_gtest/fcntl_open_by_handle_at_test.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +using namespace testing::ext; + +class FcntlByhandleatTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: name_to_handle_at_001 + * @tc.desc: Verify that when passing a null pointer as a file handle, name_to_handle_at can correctly return -1. + * @tc.type: FUNC + * */ +HWTEST_F(FcntlByhandleatTest, name_to_handle_at_001, TestSize.Level1) +{ + DIR* dir = opendir("/proc"); + int dirFd = dirfd(dir); + ASSERT_NE(dirFd, -1); + struct file_handle handle; + memset(&handle, 0, sizeof(handle)); + EXPECT_EQ(-1, name_to_handle_at(dirFd, "", &handle, nullptr, AT_FDCWD)); + close(dirFd); +} + +/** + * @tc.name: open_by_handle_at_001 + * @tc.desc: Verify that when passing a null pointer as a file handle,open_by_handle_at can correctly return -1. + * @tc.type: FUNC + * */ +HWTEST_F(FcntlByhandleatTest, open_by_handle_at_001, TestSize.Level1) +{ + int newFd = open_by_handle_at(AT_FDCWD, nullptr, O_RDONLY); + EXPECT_EQ(-1, newFd); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_except_test.cpp b/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_except_test.cpp new file mode 100644 index 000000000..53d83e628 --- /dev/null +++ b/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_except_test.cpp @@ -0,0 +1,686 @@ +#include +#include + +using namespace testing::ext; + +class FenvExceptTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: feclearexcept_001 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feclearexcept_001, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(FE_ALL_EXCEPT, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +} + +/** + * @tc.name: feclearexcept_002 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feclearexcept_002, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_DIVBYZERO + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(FE_ALL_EXCEPT, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feclearexcept(FE_DIVBYZERO)); + EXPECT_EQ(FE_ALL_EXCEPT & ~FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: feclearexcept_003 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feclearexcept_003, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_INEXACT + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(FE_ALL_EXCEPT, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feclearexcept(FE_INEXACT)); + EXPECT_EQ(FE_ALL_EXCEPT & ~FE_INEXACT, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: feclearexcept_004 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feclearexcept_004, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_INVALID + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(FE_ALL_EXCEPT, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feclearexcept(FE_INVALID)); + EXPECT_EQ(FE_ALL_EXCEPT & ~FE_INVALID, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: feclearexcept_005 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feclearexcept_005, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_OVERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(FE_ALL_EXCEPT, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feclearexcept(FE_OVERFLOW)); + EXPECT_EQ(FE_ALL_EXCEPT & ~FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: feclearexcept_006 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feclearexcept_006, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_UNDERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(FE_ALL_EXCEPT, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feclearexcept(FE_UNDERFLOW)); + EXPECT_EQ(FE_ALL_EXCEPT & ~FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: feraiseexcept_001 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feraiseexcept_001, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_DIVBYZERO + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feraiseexcept(FE_DIVBYZERO)); + EXPECT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: feraiseexcept_002 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feraiseexcept_002, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_INEXACT + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feraiseexcept(FE_INEXACT)); + EXPECT_EQ(FE_INEXACT, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: feraiseexcept_003 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feraiseexcept_003, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_INVALID + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feraiseexcept(FE_INVALID)); + EXPECT_EQ(FE_INVALID, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: feraiseexcept_004 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feraiseexcept_004, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_OVERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feraiseexcept(FE_OVERFLOW)); + EXPECT_EQ(FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: feraiseexcept_005 + * @tc.desc: Verify that the return value of the function is correctly. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, feraiseexcept_005, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_UNDERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feraiseexcept(FE_UNDERFLOW)); + EXPECT_EQ(FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fetestexcept_001 + * @tc.desc: Verify that the return value of the function is the same as the exception. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fetestexcept_001, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_DIVBYZERO +#ifdef FE_UNDERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feraiseexcept(FE_DIVBYZERO | FE_UNDERFLOW)); + EXPECT_EQ(FE_DIVBYZERO | FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +#endif +} + +/** + * @tc.name: fetestexcept_002 + * @tc.desc: Verify that the return value of the function is the same as the exception. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fetestexcept_002, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_DIVBYZERO +#ifdef FE_INEXACT + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feraiseexcept(FE_DIVBYZERO | FE_INEXACT)); + EXPECT_EQ(FE_DIVBYZERO | FE_INEXACT, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +#endif +} + +/** + * @tc.name: fetestexcept_003 + * @tc.desc: Verify that the return value of the function is the same as the exception. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fetestexcept_003, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_UNDERFLOW +#ifdef FE_OVERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feraiseexcept(FE_UNDERFLOW | FE_OVERFLOW)); + EXPECT_EQ(FE_UNDERFLOW | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +#endif +} + +/** + * @tc.name: fetestexcept_004 + * @tc.desc: Verify that the return value of the function is the same as the exception. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fetestexcept_004, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_UNDERFLOW +#ifdef FE_OVERFLOW +#ifdef FE_INVALID + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + EXPECT_EQ(0, feraiseexcept(FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID)); + EXPECT_EQ(FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +#endif +#endif +} + +/** + * @tc.name: fetestexcept_005 + * @tc.desc: Verify that the return value of the function is the same as the exception. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fetestexcept_005, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_INEXACT +#ifdef FE_UNDERFLOW +#ifdef FE_OVERFLOW +#ifdef FE_INVALID + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + int mask = FE_INEXACT | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID; + EXPECT_EQ(0, feraiseexcept(mask)); + EXPECT_EQ(mask, fetestexcept(mask)); +#else + GTEST_SKIP(); +#endif +#endif +#endif +#endif +#endif +} + +/** + * @tc.name: fegetexceptflag_001 + * @tc.desc: Verify that the expection is correctly saved to the variable. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fegetexceptflag_001, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_ALL_EXCEPT)); + + fexcept_t flagp = 0; + EXPECT_EQ(0, fegetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(flagp, FE_ALL_EXCEPT); +#else + GTEST_SKIP(); +#endif +} + +/** + * @tc.name: fegetexceptflag_002 + * @tc.desc: Verify that the expection is correctly saved to the variable. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fegetexceptflag_002, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_DIVBYZERO + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_DIVBYZERO)); + + fexcept_t flagp = 0; + EXPECT_EQ(0, fegetexceptflag(&flagp, FE_DIVBYZERO)); + EXPECT_EQ(flagp, FE_DIVBYZERO); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fegetexceptflag_003 + * @tc.desc: Verify that the expection is correctly saved to the variable. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fegetexceptflag_003, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_INEXACT + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_INEXACT)); + + fexcept_t flagp = 0; + EXPECT_EQ(0, fegetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(flagp, FE_INEXACT); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fegetexceptflag_004 + * @tc.desc: Verify that the expection is correctly saved to the variable. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fegetexceptflag_004, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_INVALID + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_INVALID)); + + fexcept_t flagp = 0; + EXPECT_EQ(0, fegetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(flagp, FE_INVALID); + EXPECT_EQ(0, fegetexceptflag(&flagp, FE_INVALID)); + EXPECT_EQ(flagp, FE_INVALID); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fegetexceptflag_005 + * @tc.desc: Verify that the expection is correctly saved to the variable. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fegetexceptflag_005, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_OVERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_OVERFLOW)); + + fexcept_t flagp = 0; + EXPECT_EQ(0, fegetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(flagp, FE_OVERFLOW); + EXPECT_EQ(0, fegetexceptflag(&flagp, FE_OVERFLOW)); + EXPECT_EQ(flagp, FE_OVERFLOW); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fegetexceptflag_006 + * @tc.desc: Verify that the expection is correctly saved to the variable. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fegetexceptflag_006, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_UNDERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, feraiseexcept(FE_UNDERFLOW)); + + fexcept_t flagp = 0; + EXPECT_EQ(0, fegetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(flagp, FE_UNDERFLOW); + EXPECT_EQ(0, fegetexceptflag(&flagp, FE_UNDERFLOW)); + EXPECT_EQ(flagp, FE_UNDERFLOW); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fesetexceptflag_001 + * @tc.desc: Verify allows the function to set exception from variables. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fesetexceptflag_001, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + fexcept_t flagp = FE_ALL_EXCEPT; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(FE_ALL_EXCEPT, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +} + +/** + * @tc.name: fesetexceptflag_002 + * @tc.desc: Verify allows the function to set exception from variables. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fesetexceptflag_002, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_DIVBYZERO + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + fexcept_t flagp = FE_DIVBYZERO; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT)); + + flagp = FE_ALL_EXCEPT; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_DIVBYZERO)); + EXPECT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fesetexceptflag_003 + * @tc.desc: Verify allows the function to set exception from variables. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fesetexceptflag_003, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_INEXACT + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + fexcept_t flagp = FE_INEXACT; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(FE_INEXACT, fetestexcept(FE_ALL_EXCEPT)); + + flagp = FE_ALL_EXCEPT; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_INEXACT)); + EXPECT_EQ(FE_INEXACT, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fesetexceptflag_004 + * @tc.desc: Verify allows the function to set exception from variables. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fesetexceptflag_004, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_INVALID + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + fexcept_t flagp = FE_INVALID; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(FE_INVALID, fetestexcept(FE_ALL_EXCEPT)); + + flagp = FE_ALL_EXCEPT; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_INVALID)); + EXPECT_EQ(FE_INVALID, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fesetexceptflag_005 + * @tc.desc: Verify allows the function to set exception from variables. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fesetexceptflag_005, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_OVERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + fexcept_t flagp = FE_OVERFLOW; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT)); + + flagp = FE_ALL_EXCEPT; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_OVERFLOW)); + EXPECT_EQ(FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fesetexceptflag_006 + * @tc.desc: Verify allows the function to set exception from variables. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fesetexceptflag_006, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_UNDERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + fexcept_t flagp = FE_UNDERFLOW; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT)); + + flagp = FE_ALL_EXCEPT; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_UNDERFLOW)); + EXPECT_EQ(FE_UNDERFLOW, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +} + +/** + * @tc.name: fesetexceptflag_007 + * @tc.desc: Verify allows the function to set exception from variables. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fesetexceptflag_007, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_UNDERFLOW +#ifdef FE_DIVBYZERO + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + fexcept_t flagp = FE_UNDERFLOW | FE_DIVBYZERO; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_ALL_EXCEPT)); + EXPECT_EQ(FE_UNDERFLOW | FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT)); + + flagp = FE_ALL_EXCEPT; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_UNDERFLOW | FE_DIVBYZERO)); + EXPECT_EQ(FE_UNDERFLOW | FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +#endif +} + +/** + * @tc.name: fesetexceptflag_008 + * @tc.desc: Verify allows the function to set exception from variables. + * @tc.type: FUNC + */ +HWTEST_F(FenvExceptTest, fesetexceptflag_008, TestSize.Level1) +{ +#ifdef FE_ALL_EXCEPT +#ifdef FE_UNDERFLOW +#ifdef FE_DIVBYZERO +#ifdef FE_OVERFLOW + EXPECT_EQ(0, feclearexcept(FE_ALL_EXCEPT)); + EXPECT_EQ(0, fetestexcept(FE_ALL_EXCEPT)); + + fexcept_t flagp = FE_UNDERFLOW | FE_DIVBYZERO; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_OVERFLOW | FE_DIVBYZERO)); + EXPECT_EQ(FE_DIVBYZERO, fetestexcept(FE_ALL_EXCEPT)); + + flagp = FE_ALL_EXCEPT & ~FE_DIVBYZERO; + EXPECT_EQ(0, fesetexceptflag(&flagp, FE_UNDERFLOW | FE_DIVBYZERO | FE_OVERFLOW)); + EXPECT_EQ(FE_UNDERFLOW | FE_OVERFLOW, fetestexcept(FE_ALL_EXCEPT)); +#else + GTEST_SKIP(); +#endif +#endif +#endif +#endif +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fegetenv_test.cpp b/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fegetenv_test.cpp new file mode 100644 index 000000000..edc04eddb --- /dev/null +++ b/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fegetenv_test.cpp @@ -0,0 +1,22 @@ +#include + +#include + +using namespace testing::ext; + +class FegetenvTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** +* @tc.name: fegetenv_001 +* @tc.desc: Gets the floating-point environment +* @tc.type: FUNC +*/ +HWTEST_F(FegetenvTest, fegetenv_001, TestSize.Level1) +{ + fenv_t envp; + + EXPECT_EQ(0, fegetenv(&envp)); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fesetenv_test.cpp b/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fesetenv_test.cpp new file mode 100644 index 000000000..da6a84c2d --- /dev/null +++ b/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fesetenv_test.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace testing::ext; + +class FesetenvTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fesetenv_001 + * @tc.desc: Set up the floating-point environment + * @tc.type: FUNC + */ +HWTEST_F(FesetenvTest, fesetenv_001, TestSize.Level1) +{ + EXPECT_EQ(0, fesetenv(FE_DFL_ENV)); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fesetround_test.cpp b/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fesetround_test.cpp new file mode 100644 index 000000000..a956c173c --- /dev/null +++ b/libc-test/src/functionalext/supplement/fenv/fenv_gtest/fenv_fesetround_test.cpp @@ -0,0 +1,43 @@ +#include +#include + +using namespace testing::ext; + +class FenvFesetroundTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fesetround_001 + * @tc.desc: Verify whether the floating-point calculation results meet the expectations after setting the rounding mode + * to "round to nearest" (FE_TONEAREST). + * @tc.type: FUNC + */ +HWTEST_F(FenvFesetroundTest, fesetround_001, TestSize.Level1) +{ + fesetround(FE_TONEAREST); + float a = 3.25f; + float b = 2.75f; + volatile float x = a + b; + ASSERT_FLOAT_EQ(6.0f, x); + x = x - b; + ASSERT_FLOAT_EQ(3.25f, x); +} + +/** + * @tc.name: fesetround_002 + * @tc.desc: Verify whether the floating-point calculation results meet the expectations after setting the rounding mode + * to "round up" (FE_UPWARD). + * @tc.type: FUNC + */ +HWTEST_F(FenvFesetroundTest, fesetround_002, TestSize.Level1) +{ + fesetround(FE_UPWARD); + float a = 2.0f; + float b = 8388608.0f; + volatile float x = a + b; + ASSERT_FLOAT_EQ(8388610.0f, x); + x = x - b; + ASSERT_FLOAT_EQ(2.0f, x); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_fread_chk_test.cpp b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_fread_chk_test.cpp new file mode 100644 index 000000000..1d377611d --- /dev/null +++ b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_fread_chk_test.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#if !defined(__FORTIFY_COMPILATION) +#define UNDEF_FORTIFY +#define __FORTIFY_COMPILATION +#endif +#include + +constexpr int BUF_SIZE = 6; + +using namespace testing::ext; + +class FreadChkTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: __fread_chk_001 + * @tc.desc: Read the five characters of the version file for comparison + * @tc.type: FUNC + */ +HWTEST_F(FreadChkTest, __fread_chk_001, TestSize.Level1) +{ + const char* input = "/tmp/FortifyFreadChkTest.__fread_chk_001.tmp"; + char buf[BUF_SIZE] = { 0 }; + const char* src = "Linux\0"; + + int fd = open(input, O_WRONLY | O_CREAT | O_TRUNC, 0644); + ASSERT_NE(-1, fd); + EXPECT_LT(0, write(fd, src, sizeof(char) * strlen(src))); + ASSERT_EQ(0, close(fd)); + + FILE* fp = fopen(input, "r"); + ASSERT_NE(nullptr, fp); + EXPECT_EQ(strlen(src), __fread_chk(buf, sizeof(char), strlen(src), fp, BUF_SIZE)); + EXPECT_STREQ(src, buf); + ASSERT_EQ(0, fclose(fp)); + + ASSERT_EQ(0, remove(input)); +} + +#ifdef UNDEF_FORTIFY +#undef UNDEF_FORTIFY +#undef __FORTIFY_COMPILATION +#endif \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_getcwd_chk_test.cpp b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_getcwd_chk_test.cpp new file mode 100644 index 000000000..3f9b9a568 --- /dev/null +++ b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_getcwd_chk_test.cpp @@ -0,0 +1,33 @@ +#include +#include +#if !defined(__FORTIFY_COMPILATION) +#define UNDEF_FORTIFY +#define __FORTIFY_COMPILATION +#endif +#include + +constexpr int BUF_SIZE = 100; + +using namespace testing::ext; + +class FortifyGetcwdChkTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: __getcwd_chk_001 + * @tc.desc: Gets the program run path. + * @tc.type: FUNC + */ +HWTEST_F(FortifyGetcwdChkTest, __getcwd_chk_001, TestSize.Level1) +{ + char buf[BUF_SIZE] = { 0 }; + EXPECT_LT(0, strlen(__getcwd_chk(buf, sizeof(buf), BUF_SIZE))); + EXPECT_LT(0, strlen(buf)); +} + +#ifdef UNDEF_FORTIFY +#undef UNDEF_FORTIFY +#undef __FORTIFY_COMPILATION +#endif \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_memmove_chk_test.cpp b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_memmove_chk_test.cpp new file mode 100644 index 000000000..118e503ca --- /dev/null +++ b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_memmove_chk_test.cpp @@ -0,0 +1,57 @@ +#include +#include + +constexpr int BUF_SIZE = 100; + +using namespace testing::ext; + +class FortifyMemmoveChkTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +class FortifyMemmoveChkDeathTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +extern "C" void* __memmove_chk(void* dest, const void* src, size_t len, size_t dst_len); + +/** + * @tc.name: __memmove_chk_001 + * @tc.desc: Compare two strings for consistency. + * @tc.type: FUNC + */ +HWTEST_F(FortifyMemmoveChkTest, __memmove_chk_001, TestSize.Level1) +{ + const char* src = "hello, world"; + char dst[BUF_SIZE] = { 0 }; + __memmove_chk(dst, src, strlen(src), BUF_SIZE); + EXPECT_STREQ(src, dst); +} + +/** +* @tc.name: __memmove_chk_002 +* @tc.desc: Verify that the __memmove_chk function performs the copy operation correctly given the parameters and + prevents buffer overflow problems. +* @tc.type: FUNC +*/ +HWTEST_F(FortifyMemmoveChkTest, __memmove_chk_002, TestSize.Level1) +{ + const char* src = "hello, world"; + char dst[BUF_SIZE] = { 0 }; + __memmove_chk(dst, src, strlen(src), static_cast(-1)); + EXPECT_STREQ(src, dst); +} + +/** + * @tc.name: __memmove_chk_003 + * @tc.desc: The function does not execute when buf_size == 0 and it outputs an error message in stderr. + * @tc.type: FUNC + */ +HWTEST_F(FortifyMemmoveChkDeathTest, __memmove_chk_003, TestSize.Level1) +{ + const char* src = "hello, world"; + char dst[BUF_SIZE] = { 0 }; + EXPECT_DEATH(__memmove_chk(dst, src, strlen(src), 0), "Musl Fortify runtime error*"); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_memset_chk_test.cpp b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_memset_chk_test.cpp new file mode 100644 index 000000000..683f889c9 --- /dev/null +++ b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_memset_chk_test.cpp @@ -0,0 +1,25 @@ +#include +#include + +constexpr int BUF_SIZE = 6; + +using namespace testing::ext; + +class MemsetChkTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +extern "C" void* __memset_chk(void* dest, int byte, size_t count, size_t dst_len); + +/** + * @tc.name: memsetchk_001 + * @tc.desc: Use characters to fill memory areas + * @tc.type: FUNC + */ +HWTEST_F(MemsetChkTest, memsetchk_001, TestSize.Level1) +{ + char dst[BUF_SIZE] = { 0 }; + EXPECT_STREQ("AAAAA", reinterpret_cast(__memset_chk(dst, 'A', BUF_SIZE - 1, BUF_SIZE))); + EXPECT_STREQ("AAAAA", dst); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_open64_chk_test.cpp b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_open64_chk_test.cpp index 58bf48ceb..966ea36ca 100644 --- a/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_open64_chk_test.cpp +++ b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_open64_chk_test.cpp @@ -17,9 +17,10 @@ class FortifyOpen64chkTest : public testing::Test { * */ HWTEST_F(FortifyOpen64chkTest, __open64_chk_001, TestSize.Level1) { - int fd = __open64_chk("/proc/version", O_RDWR); - EXPECT_NE(-1, fd); - fd = open64("/proc/version", O_RDWR | O_CREAT, 0777); - EXPECT_NE(-1, fd); + int fd = open("test.txt", O_RDONLY | O_CREAT, 0644); close(fd); + fd = __open64_chk("./test.txt", O_RDONLY); + ASSERT_NE(-1, fd); + close(fd); + remove("./test.txt"); } \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_readlink_chk_test.cpp b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_readlink_chk_test.cpp new file mode 100644 index 000000000..fb18c17ac --- /dev/null +++ b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_readlink_chk_test.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#if !defined(__FORTIFY_COMPILATION) +#define UNDEF_FORTIFY +#define __FORTIFY_COMPILATION +#endif +#include + +constexpr int BUF_SIZE = 100; + +using namespace testing::ext; + +class FortifyReadLinkTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: __readlink_chk_001 + * @tc.desc: Gets the soft connection pointer. + * @tc.type: FUNC + */ +HWTEST_F(FortifyReadLinkTest, __readlink_chk_001, TestSize.Level1) +{ + const char* tmpFile = "/data/FortifyReadLinkTest.__readlink_chk_001.tmp"; + const char* linkFile = "/data/FortifyReadLinkTest.__readlink_chk_001.lnk"; + int fd = open(tmpFile, O_CREAT | O_TRUNC, 0644); + ASSERT_NE(-1, fd); + close(fd); + + ASSERT_EQ(0, symlink(tmpFile, linkFile)); + + char buf[BUF_SIZE] = { 0 }; + EXPECT_LT(0, __readlink_chk(linkFile, buf, sizeof(buf), BUF_SIZE)); + EXPECT_STREQ(tmpFile, buf); + + ASSERT_EQ(0, unlink(linkFile)); + ASSERT_EQ(0, remove(tmpFile)); +} + +#ifdef UNDEF_FORTIFY +#undef UNDEF_FORTIFY +#undef __FORTIFY_COMPILATION +#endif \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_stpncpy_chk_test.cpp b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_stpncpy_chk_test.cpp new file mode 100644 index 000000000..e1b149b19 --- /dev/null +++ b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_stpncpy_chk_test.cpp @@ -0,0 +1,28 @@ +#include +#include + +constexpr int BUF_SIZE = 36; + +using namespace testing::ext; + +class StpncpyChkTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +extern "C" char* __stpncpy_chk(char* dest, const char* src, size_t len, size_t buf_size); + +/** + * @tc.name: stpncpychk_001 + * @tc.desc: Copy strings for comparison + * @tc.type: FUNC + */ +HWTEST_F(StpncpyChkTest, __stpncpy_chk_001, TestSize.Level1) +{ + const char* src = "Hello\0"; + char dst[BUF_SIZE] = { 0 }; + char* dst2 = __stpncpy_chk(dst, src, strlen(src), BUF_SIZE); + EXPECT_STREQ(src, dst); + EXPECT_EQ('o', *(dst2 - 1)); + EXPECT_EQ('\0', *dst2); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_strncat_chk_test.cpp b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_strncat_chk_test.cpp new file mode 100644 index 000000000..fbf2b4b31 --- /dev/null +++ b/libc-test/src/functionalext/supplement/fortify/fortify_gtest/fortify_strncat_chk_test.cpp @@ -0,0 +1,25 @@ +#include +#include + +constexpr int BUF_SIZE = 36; +using namespace testing::ext; + +class StrncatChkTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +extern "C" char* __strncat_chk(char* dest, const char* src, size_t len, size_t buf_size); + +/** + * @tc.name: __strncat_chk_001 + * @tc.desc: Append a string to an array. + * @tc.type: FUNC + */ +HWTEST_F(StrncatChkTest, __strncat_chk_001, TestSize.Level1) +{ + char buf[BUF_SIZE] = { 0 }; + const char* src = "hello\0"; + EXPECT_STREQ(src, __strncat_chk(buf, src, strlen(src), BUF_SIZE)); + EXPECT_STREQ(src, buf); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semctl_test.cpp b/libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semctl_test.cpp new file mode 100644 index 000000000..dab67da93 --- /dev/null +++ b/libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semctl_test.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include + +using namespace testing::ext; + +constexpr int PATH_SIZE = 1024; +constexpr int SEMAPHORE_COUNT = 3; +constexpr int SIGNAL_VALUE = 5; + +class IpcSemctlTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: semctl_001 + * @tc.desc: Verify the semctl interface by checking the successful retrieval of semaphore information through the + * IPC_STAT command, ensuring its functionality. + * @tc.type: FUNC + */ +HWTEST_F(IpcSemctlTest, semctl_001, TestSize.Level1) +{ + int semId = semget(IPC_PRIVATE, SEMAPHORE_COUNT, IPC_CREAT | 0666); + struct semid_ds semInfo; + int result = semctl(semId, 0, IPC_STAT, &semInfo); + ASSERT_NE(result, -1); + int deleteResult = semctl(semId, 0, IPC_RMID); + ASSERT_NE(deleteResult, -1); +} + +/** + * @tc.name: semctl_002 + * @tc.desc: Verify whether the semctl function correctly returns the expected error code when called with invalid + * parameters. + * @tc.type: FUNC + */ +HWTEST_F(IpcSemctlTest, semctl_002, TestSize.Level1) +{ + errno = 0; + int result = semctl(-1, 0, IPC_RMID); + EXPECT_TRUE(result == -1); + EXPECT_TRUE(errno == EINVAL || errno == ENOSYS); +} + +/** + * @tc.name: semctl_003 + * @tc.desc: Verify whether the functionality of parameter setting and removal operations is working correctly after + * creating a semaphore. + * @tc.type: FUNC + */ +HWTEST_F(IpcSemctlTest, semctl_003, TestSize.Level1) +{ + int semid = semget(IPC_PRIVATE, 1, 0666 | IPC_CREAT); + ASSERT_NE(semid, -1); + int setValResult = semctl(semid, 0, SETVAL, SIGNAL_VALUE); + ASSERT_NE(setValResult, -1); + int value = semctl(semid, 0, GETVAL); + EXPECT_EQ(SIGNAL_VALUE, value); + int removeResult = semctl(semid, 0, IPC_RMID); + EXPECT_EQ(0, removeResult); +} + +/** + * @tc.name: semctl_004 + * @tc.desc: Verify the functionality of the semctl interface by creating a semaphore, retrieving its information, and + * verifying that the number of semaphores is 1. Finally, the semaphore is removed. + * @tc.type: FUNC + */ +HWTEST_F(IpcSemctlTest, semctl_004, TestSize.Level1) +{ + char tempPath[PATH_SIZE] = "test-dir"; + key_t semKey = ftok(tempPath, 1); + int semId = semget(semKey, 1, IPC_CREAT | 0666); + ASSERT_NE(semId, -1); + semid_ds semInfo; + memset(&semInfo, 0, sizeof(semInfo)); + EXPECT_TRUE(semctl(semId, 0, IPC_STAT, &semInfo) == 0); + EXPECT_TRUE(semInfo.sem_nsems == 1U); + int deleteResult = semctl(semId, 0, IPC_RMID); + ASSERT_NE(deleteResult, -1); + rmdir(tempPath); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semget_test.cpp b/libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semget_test.cpp new file mode 100644 index 000000000..5d90d8b32 --- /dev/null +++ b/libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semget_test.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +class IpcSemgetTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: semget_001 + * @tc.desc: Verify whether the semget interface can correctly return the expected error code when invalid parameters + * are passed in. + * @tc.type: FUNC + */ +HWTEST_F(IpcSemgetTest, semget_001, TestSize.Level1) +{ + errno = 0; + int result = semget(-1, -1, 0); + EXPECT_TRUE(result == -1); + EXPECT_TRUE(errno == EINVAL || errno == ENOSYS); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semop_test.cpp b/libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semop_test.cpp new file mode 100644 index 000000000..3425e0045 --- /dev/null +++ b/libc-test/src/functionalext/supplement/ipc/ipc_gtest/ipc_semop_test.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include + +using namespace testing::ext; + +class IpcSemopTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: semop_001 + * @tc.desc: Create a semaphore set, set an initial value, perform a wait operation, and verify if the value of the + * semaphore after the wait operation matches the expected result. + * @tc.type: FUNC + */ +HWTEST_F(IpcSemopTest, semop_001, TestSize.Level1) +{ + int semId = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666); + int result = semctl(semId, 0, SETVAL, 1); + EXPECT_EQ(result, 0); + sembuf waitOperation[] = { { .sem_num = 0, .sem_op = -1, .sem_flg = 0 } }; + result = semop(semId, waitOperation, 1); + EXPECT_EQ(result, 0); + int value = semctl(semId, 0, GETVAL); + EXPECT_EQ(value, 0); + result = semctl(semId, 0, IPC_RMID); + EXPECT_EQ(result, 0); +} + +/** + * @tc.name: semop_002 + * @tc.desc: Verify the semop interface. It prepares by creating a semaphore set and setting an initial value. Then it + * performs a signal operation once and checks if the value of the semaphore after the operation matches the + * expected result. + * @tc.type: FUNC + */ +HWTEST_F(IpcSemopTest, semop_002, TestSize.Level1) +{ + int semId = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666); + int result = semctl(semId, 0, SETVAL, 0); + EXPECT_EQ(result, 0); + sembuf signalOperation[] = { { .sem_num = 0, .sem_op = 1, .sem_flg = 0 } }; + result = semop(semId, signalOperation, 1); + EXPECT_EQ(result, 0); + int value = semctl(semId, 0, GETVAL); + EXPECT_EQ(value, 1); + result = semctl(semId, 0, IPC_RMID); + EXPECT_EQ(result, 0); +} + +/** + * @tc.name: semop_003 + * @tc.desc: Examine the behavior of the semop interface when invalid parameters are passed, including the handling of + * error codes. + * @tc.type: FUNC + */ +HWTEST_F(IpcSemopTest, semop_003, TestSize.Level1) +{ + errno = 0; + int result = semop(-1, nullptr, 0); + EXPECT_TRUE(result == -1); + EXPECT_TRUE(errno == EINVAL || errno == ENOSYS); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_err_test.cpp b/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_err_test.cpp new file mode 100644 index 000000000..a3175635c --- /dev/null +++ b/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_err_test.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include + +#define EXITNO(X) (((X) & 0xff00) >> 8) + +using namespace testing::ext; + +class LegacyErrTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: err_001 + * @tc.desc: Gets the error code after the function call + * @tc.type: FUNC + */ +HWTEST_F(LegacyErrTest, err_001, TestSize.Level1) +{ + pid_t pid = fork(); + if (pid == 0) { + err(EXITNO(123), "err_001"); + } else if (pid > 0) { + int status; + waitpid(pid, &status, 0); + EXPECT_EQ(1, WIFEXITED(status)); + EXPECT_EQ(EXITNO(123), WEXITSTATUS(status)); + } else { + FAIL() << "fork() failed!"; + } +} + +/** + * @tc.name: err_002 + * @tc.desc: Gets the error code after the function call + * @tc.type: FUNC + */ +HWTEST_F(LegacyErrTest, err_002, TestSize.Level1) +{ + pid_t pid = fork(); + if (pid == 0) { + err(EXITNO(0), "err_002"); + } else if (pid > 0) { + int status; + waitpid(pid, &status, 0); + EXPECT_EQ(1, WIFEXITED(status)); + EXPECT_EQ(EXITNO(0), WEXITSTATUS(status)); + } else { + FAIL() << "fork() failed!"; + } +} + +/** + * @tc.name: err_003 + * @tc.desc: Gets the error code after the function call + * @tc.type: FUNC + */ +HWTEST_F(LegacyErrTest, err_003, TestSize.Level1) +{ + pid_t pid; + + pid = fork(); + if (pid == 0) { + err(EXITNO(-1), "err_003"); + } else if (pid > 0) { + int status; + waitpid(pid, &status, 0); + EXPECT_EQ(1, WIFEXITED(status)); + EXPECT_EQ(EXITNO(-1), WEXITSTATUS(status)); + } else { + FAIL() << "fork() failed!"; + } +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_errx_test.cpp b/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_errx_test.cpp new file mode 100644 index 000000000..1899ac69c --- /dev/null +++ b/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_errx_test.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include + +#define EXITNO(X) (((X) & 0xff00) >> 8) + +using namespace testing::ext; + +class LegacyErrxTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: errx_001 + * @tc.desc: Gets the error code after the function call + * @tc.type: FUNC + */ +HWTEST_F(LegacyErrxTest, errx_001, TestSize.Level1) +{ + pid_t pid = fork(); + if (pid == 0) { + errx(EXITNO(123), "errx_001"); + } else if (pid > 0) { + int status; + waitpid(pid, &status, 0); + EXPECT_EQ(1, WIFEXITED(status)); + EXPECT_EQ(EXITNO(123), WEXITSTATUS(status)); + } else { + FAIL() << "fork() failed!"; + } +} + +/** + * @tc.name: errx_002 + * @tc.desc: Gets the error code after the function call + * @tc.type: FUNC + */ +HWTEST_F(LegacyErrxTest, errx_002, TestSize.Level1) +{ + pid_t pid = fork(); + if (pid == 0) { + errx(EXITNO(0), "errx_002"); + } else if (pid > 0) { + int status; + waitpid(pid, &status, 0); + EXPECT_EQ(1, WIFEXITED(status)); + EXPECT_EQ(EXITNO(0), WEXITSTATUS(status)); + } else { + FAIL() << "fork() failed!"; + } +} + +/** + * @tc.name: errx_003 + * @tc.desc: Gets the error code after the function call + * @tc.type: FUNC + */ +HWTEST_F(LegacyErrxTest, errx_003, TestSize.Level1) +{ + pid_t pid = fork(); + if (pid == 0) { + errx(EXITNO(-1), "errx_003"); + } else if (pid > 0) { + int status; + waitpid(pid, &status, 0); + EXPECT_EQ(1, WIFEXITED(status)); + EXPECT_EQ(EXITNO(-1), WEXITSTATUS(status)); + } else { + FAIL() << "fork() failed!"; + } +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_ftw64_test.cpp b/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_ftw64_test.cpp new file mode 100644 index 000000000..8eae35858 --- /dev/null +++ b/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_ftw64_test.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include + +using namespace testing::ext; + +class LegacyFtw64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +constexpr int DEPTH = 256; +constexpr int SIZE = 10; + +int FtwCallback(const char* fpath, const struct stat* sb, int typeFlag) +{ + if (typeFlag == FTW_F) { + printf("File: %s, Size: %lld bytes\n", fpath, (long long)sb->st_size); + } else if (typeFlag == FTW_D) { + printf("Directory: %s\n", fpath); + } + return 0; +} + +#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) +/** + * @tc.name: ftw64_001 + * @tc.desc: This test verifies that when calling the ftw64 function to traverse the/musl directory, when the directory + * does not exist, the ftw64 function will return -1 and set errno to ENOENT. + * @tc.type: FUNC + */ +HWTEST_F(LegacyFtw64Test, ftw64_001, TestSize.Level1) +{ + errno = 0; + auto nullFtwCallback = [](const char*, const struct stat64*, int) { return 0; }; + EXPECT_EQ(-1, ftw64("/musl", nullFtwCallback, DEPTH)); + EXPECT_EQ(ENOENT, errno); +} + +/** + * @tc.name: ftw64_002 + * @tc.desc: The testing viewpoint of this test case is to verify that when calling the ftw64 function to traverse an + * empty string directory, it returns -1 and sets errno to ENOENT. + * @tc.type: FUNC + */ +HWTEST_F(LegacyFtw64Test, ftw64_002, TestSize.Level1) +{ + errno = 0; + auto nullFtwCallback = [](const char*, const struct stat64*, int) { return 0; }; + EXPECT_EQ(-1, ftw64("", nullFtwCallback, DEPTH)); + EXPECT_EQ(ENOENT, errno); +} + +/** + * @tc.name: ftw64_003 + * @tc.desc: This test verifies that when using the ftw64 function to traverse a directory, when the directory structure + * contains regular files and subdirectories, the function can function properly and successfully return 0, + * and after the traversal is completed, all files and directories can be correctly deleted. + * @tc.type: FUNC + */ +HWTEST_F(LegacyFtw64Test, ftw64_003, TestSize.Level1) +{ + const char* directory = "ftw64"; + ASSERT_EQ(mkdir(directory, 0777), 0); + FILE* file1 = fopen("ftw64/file1.txt", "w"); + fclose(file1); + FILE* file2 = fopen("ftw64/file2.txt", "w"); + fclose(file2); + ASSERT_EQ(mkdir("ftw64/subdir", 0777), 0); + FILE* file3 = fopen("ftw64/subdir/file3.txt", "w"); + fclose(file3); + int result = ftw64(directory, FtwCallback, SIZE); + EXPECT_EQ(0, result); + EXPECT_TRUE(remove("ftw64/file1.txt") == 0); + EXPECT_TRUE(remove("ftw64/file2.txt") == 0); + EXPECT_TRUE(remove("ftw64/subdir/file3.txt") == 0); + EXPECT_TRUE(rmdir("ftw64/subdir") == 0); + EXPECT_TRUE(rmdir("ftw64") == 0); +} +#endif \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_valloc_test.cpp b/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_valloc_test.cpp new file mode 100644 index 000000000..916b1bb8f --- /dev/null +++ b/libc-test/src/functionalext/supplement/legacy/legacy_gtest/legacy_valloc_test.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace testing::ext; + +constexpr int PAGESIZE = 4096; + +class LegacyVallocTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: valloc_001 + * @tc.desc: Allocate heap size by page size. + * @tc.type: FUNC + */ +HWTEST_F(LegacyVallocTest, valloc_001, TestSize.Level1) +{ + void* ptr = valloc(PAGESIZE); + ASSERT_NE(nullptr, ptr); + free(ptr); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/linux/linux_gtest/linux_fallocate64_test.cpp b/libc-test/src/functionalext/supplement/linux/linux_gtest/linux_fallocate64_test.cpp new file mode 100644 index 000000000..6918b360a --- /dev/null +++ b/libc-test/src/functionalext/supplement/linux/linux_gtest/linux_fallocate64_test.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace testing::ext; + +constexpr int SIZE_50MB = 52428800; + +class LinuxFallocate64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fallocate64_001 + * @tc.desc: Create a large file and determine the file offset location + * @tc.type: FUNC + */ +HWTEST_F(LinuxFallocate64Test, fallocate64_001, TestSize.Level1) +{ + struct stat buf; + const char* tmpFile = "/data/LinuxFallocate64Test.fallocate64_001.tmp"; + + int fd = open(tmpFile, O_RDWR | O_CREAT | O_TRUNC, 0644); + ASSERT_NE(-1, fd); + EXPECT_EQ(0, fallocate64(fd, 0, 0, SIZE_50MB)); + close(fd); + + fd = open(tmpFile, O_RDONLY); + ASSERT_NE(-1, fd); + ASSERT_EQ(0, fstat(fd, &buf)); + EXPECT_EQ(SIZE_50MB, buf.st_size); + close(fd); + remove(tmpFile); +} + +/** + * @tc.name: fallocate64_002 + * @tc.desc: Create a file of the invalid size. + * @tc.type: FUNC + */ +HWTEST_F(LinuxFallocate64Test, fallocate64_002, TestSize.Level1) +{ + const char* tmpFile = "/data/LinuxFallocate64Test.fallocate64_002.tmp"; + + int fd = open(tmpFile, O_RDWR | O_CREAT | O_TRUNC, 0644); + ASSERT_NE(-1, fd); + EXPECT_EQ(-1, fallocate64(fd, 0, 0, -1)); + close(fd); + + remove(tmpFile); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/linux/linux_gtest/linux_getdents64_test.cpp b/libc-test/src/functionalext/supplement/linux/linux_gtest/linux_getdents64_test.cpp new file mode 100644 index 000000000..955bb4fa5 --- /dev/null +++ b/libc-test/src/functionalext/supplement/linux/linux_gtest/linux_getdents64_test.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace testing::ext; + +class LinuxGetdents64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +constexpr int BUFFERSIZE = 1024; + +#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) +/** + * @tc.name: getdents64_001 + * @tc.desc: This test verifies that in Linux systems, using the getdents64 function can successfully read directory + * entry information in the specified directory, and ensures that the function return value is not -1, + * indicating a successful read operation. + * @tc.type: FUNC + */ +HWTEST_F(LinuxGetdents64Test, getdents64_001, TestSize.Level1) +{ + int fd = open(".", O_RDONLY | O_DIRECTORY); + ASSERT_NE(fd, -1); + char buf[BUFFERSIZE]; + int result = getdents64(fd, reinterpret_cast(buf), sizeof(buf)); + ASSERT_NE(result, -1); + close(fd); +} + +/** + * @tc.name: getdents64_002 + * @tc.desc: The testing viewpoint of this test case is to verify whether the function returns -1 when the buffer size + * passed in exceeds the system limit when calling the getdents64 interface, indicating that an exception has + * occurred. + * @tc.type: FUNC + */ +HWTEST_F(LinuxGetdents64Test, getdents64_002, TestSize.Level1) +{ + struct dirent buffer; + int fd = open("/getdents64", O_RDONLY); + int result = getdents64(fd, &buffer, INT_MAX); + EXPECT_EQ(result, -1); + close(fd); +} +#endif \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/linux/linux_gtest/linux_sendfile64_test.cpp b/libc-test/src/functionalext/supplement/linux/linux_gtest/linux_sendfile64_test.cpp new file mode 100644 index 000000000..882111baf --- /dev/null +++ b/libc-test/src/functionalext/supplement/linux/linux_gtest/linux_sendfile64_test.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace testing::ext; + +class LinuxSendfile64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: sendfile64_001 + * @tc.desc: Verify the functionality of the sendfile64 interface for transferring data between files. + * @tc.type: FUNC + */ +HWTEST_F(LinuxSendfile64Test, sendfile64_001, TestSize.Level1) +{ + int fdIn = open("test.txt", O_RDWR | O_CREAT, 0644); + ASSERT_NE(fdIn, -1); + const char* inputData = "hello world"; + ssize_t writeRet = write(fdIn, inputData, strlen(inputData)); + EXPECT_EQ(writeRet, strlen(inputData)); + int fdOut = open("output_file", O_WRONLY | O_CREAT, 0644); + ASSERT_NE(fdOut, -1); + off_t offset = 0; + ssize_t sendfileRet = sendfile64(fdOut, fdIn, &offset, strlen(inputData)); + EXPECT_GT(sendfileRet, 0); + close(fdIn); + close(fdOut); + EXPECT_EQ(0, remove("test.txt")); + EXPECT_EQ(0, remove("output_file")); +} diff --git a/libc-test/src/functionalext/supplement/misc/misc_gtest/misc_wordexp_test.cpp b/libc-test/src/functionalext/supplement/misc/misc_gtest/misc_wordexp_test.cpp new file mode 100644 index 000000000..494314a00 --- /dev/null +++ b/libc-test/src/functionalext/supplement/misc/misc_gtest/misc_wordexp_test.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace testing::ext; + +class WordexpTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: wordexp_001 + * @tc.desc: Wildcard expansion by default. + * @tc.type: FUNC + */ +HWTEST_F(WordexpTest, wordexp_001, TestSize.Level1) +{ + wordexp_t exp; + + EXPECT_EQ(0, wordexp("/proc/ver*", &exp, 0)); + EXPECT_STREQ("/proc/version", exp.we_wordv[0]); + wordfree(&exp); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/network/network_gtest/network_sethostent_test.cpp b/libc-test/src/functionalext/supplement/network/network_gtest/network_sethostent_test.cpp new file mode 100644 index 000000000..caeab0411 --- /dev/null +++ b/libc-test/src/functionalext/supplement/network/network_gtest/network_sethostent_test.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace testing::ext; + +class ProcessForkTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: sethostent_001 + * @tc.desc: Verify whether the sethostent interface can return empty host record when setting the initial position of + * the HOST database stream. + * @tc.type: FUNC + */ +HWTEST_F(ProcessForkTest, sethostent_001, TestSize.Level1) +{ + sethostent(1); + struct hostent* host = gethostent(); + EXPECT_EQ(host, nullptr); + endhostent(); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/process/process_gtest/process_execle_test.cpp b/libc-test/src/functionalext/supplement/process/process_gtest/process_execle_test.cpp new file mode 100644 index 000000000..75c4e3537 --- /dev/null +++ b/libc-test/src/functionalext/supplement/process/process_gtest/process_execle_test.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +using namespace testing::ext; + +class ProcessExecleTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: execle_001 + * @tc.desc: Run execle() to check the exit status of the child process + * @tc.type: FUNC + */ +HWTEST_F(ProcessExecleTest, execle_001, TestSize.Level1) +{ + testing::internal::CaptureStdout(); + pid_t pid = fork(); + if (pid == 0) { + const char* envp[] = { nullptr }; + EXPECT_EQ(0, execle("/bin/ls", "ls", "-l", nullptr, envp)); + exit(0); + } else if (pid > 0) { + waitpid(pid, nullptr, 0); + std::string out = testing::internal::GetCapturedStdout(); + EXPECT_LE(0, out.length()); + } else { + FAIL() << "fork() failed!"; + } +} + +/** + * @tc.name: execle_002 + * @tc.desc: Verify the return value of a failed run. + * @tc.type: FUNC + */ +HWTEST_F(ProcessExecleTest, execle_002, TestSize.Level1) +{ + EXPECT_EQ(-1, execle("/", "execle_002", nullptr, nullptr, nullptr)); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/process/process_gtest/process_fork_test.cpp b/libc-test/src/functionalext/supplement/process/process_gtest/process_fork_test.cpp new file mode 100644 index 000000000..09e501801 --- /dev/null +++ b/libc-test/src/functionalext/supplement/process/process_gtest/process_fork_test.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + +using namespace testing::ext; + +class ProcessForkTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fork_001 + * @tc.desc: Verify that getpid function can return the correct process ID in the child process, and waitpid function + * can correctly wait for the child process to exit in the parent process. + * @tc.type: FUNC + */ +HWTEST_F(ProcessForkTest, fork_001, TestSize.Level1) +{ + pid_t id = getpid(); + pid_t pid = fork(); + ASSERT_NE(-1, pid); + if (pid > 0) { + EXPECT_EQ(id, getpid()); + int status; + EXPECT_EQ(pid, TEMP_FAILURE_RETRY(waitpid(pid, &status, 0))); + EXPECT_TRUE(WIFEXITED(status)); + EXPECT_EQ(WEXITSTATUS(status), 0); + } else { + EXPECT_EQ(id, getppid()); + exit(0); + } +} + +/** + * @tc.name: fork_002 + * @tc.desc: Verify the functionality of the fork interface for creating child processes and to test the signal passing + * between parent and child processes. + * @tc.type: FUNC + */ +HWTEST_F(ProcessForkTest, fork_002, TestSize.Level1) +{ + pid_t pid = fork(); + ASSERT_NE(-1, pid); + if (pid > 0) { + sleep(1); + kill(pid, SIGUSR1); + int status; + waitpid(pid, &status, 0); + EXPECT_TRUE(WIFEXITED(status)); + EXPECT_EQ(WEXITSTATUS(status), 0); + } else { + signal(SIGUSR1, [](int) { _exit(0); }); + pause(); + } +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regcomp_test.cpp b/libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regcomp_test.cpp new file mode 100644 index 000000000..fc4cab2ed --- /dev/null +++ b/libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regcomp_test.cpp @@ -0,0 +1,42 @@ +#include +#include + +using namespace testing::ext; + +class RegexRegcompTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: regcomp_001 + * @tc.desc: Verify whether the regcomp interface can successfully compile the valid regular expression "abc" and ensure + * that it returns 0, thus confirming its correct behavior in normal scenarios. + * @tc.type: FUNC + */ +HWTEST_F(RegexRegcompTest, regcomp_001, TestSize.Level1) +{ + regex_t preg; + const char* pattern = "abc"; + int result = regcomp(&preg, pattern, 0); + EXPECT_EQ(result, 0); + regfree(&preg); +} + +/** + * @tc.name: regcomp_002 + * @tc.desc: Verify that when given a pattern with an invalid regular expression, the regcomp function is expected to + * return a non-zero result indicating compilation failure. + * @tc.type: FUNC + */ +HWTEST_F(RegexRegcompTest, regcomp_002, TestSize.Level1) +{ + regex_t preg; + const char* pattern = "[a-z]"; + int result = regcomp(&preg, pattern, 0); + EXPECT_EQ(result, 0); + pattern = "[a-z"; + result = regcomp(&preg, pattern, 0); + ASSERT_NE(result, 0); + regfree(&preg); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regerror_test.cpp b/libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regerror_test.cpp new file mode 100644 index 000000000..fc6158917 --- /dev/null +++ b/libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regerror_test.cpp @@ -0,0 +1,45 @@ +#include +#include + +using namespace testing::ext; +using namespace std; + +constexpr int BUF_SIZE = 256; + +class RegexRegerrorTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: regerror_001 + * @tc.desc: Verify whether the regcomp interface can successfully compile the valid regular expression "abc" and ensure + * that it returns 0, thus confirming its correct behavior in normal scenarios. + * @tc.type: FUNC + */ +HWTEST_F(RegexRegerrorTest, regerror_001, TestSize.Level1) +{ + regex_t regex; + int ret = regcomp(®ex, "[a-z", REG_EXTENDED); + EXPECT_EQ(ret, REG_EBRACK); + char errBuf[BUF_SIZE] = { 0 }; + regerror(ret, ®ex, errBuf, BUF_SIZE); + EXPECT_GT(strlen(errBuf), 0); + regfree(®ex); +} + +/** + * @tc.name: regerror_002 + * @tc.desc: Verify the correctness of the regerror interface in converting regular expression error codes to + * corresponding error messages by checking its return value. + * @tc.type: FUNC + */ +HWTEST_F(RegexRegerrorTest, regerror_002, TestSize.Level1) +{ + regex_t regex; + int compError = regcomp(®ex, "*", REG_EXTENDED); + ASSERT_NE(0, compError); + int errorCode = regerror(compError, ®ex, nullptr, 0); + EXPECT_TRUE(errorCode > 0); + regfree(®ex); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regfree_test.cpp b/libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regfree_test.cpp new file mode 100644 index 000000000..24ba99e5b --- /dev/null +++ b/libc-test/src/functionalext/supplement/regex/regex_gtest/regex_regfree_test.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace testing::ext; + +class RegexRegfreeTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: regfree_001 + * @tc.desc: Verify the regfree interface by ensuring that it correctly releases the memory associated with a compiled + * regular expression, as indicated by the re_nsub field being set to 0. + * @tc.type: FUNC + */ +HWTEST_F(RegexRegfreeTest, regfree_001, TestSize.Level1) +{ + regex_t regex; + int ret = regcomp(®ex, "[a-z]", REG_EXTENDED); + EXPECT_EQ(ret, 0); + regfree(®ex); + EXPECT_EQ(regex.re_nsub, 0); +} diff --git a/libc-test/src/functionalext/supplement/sched/sched_gtest/sched_sched_getscheduler_test.cpp b/libc-test/src/functionalext/supplement/sched/sched_gtest/sched_sched_getscheduler_test.cpp new file mode 100644 index 000000000..533dffd36 --- /dev/null +++ b/libc-test/src/functionalext/supplement/sched/sched_gtest/sched_sched_getscheduler_test.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +class SchedSchedGetschedulerTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: sched_getscheduler_001 + * @tc.desc: Verify the functionality of setting and retrieving the scheduling policy. It includes setting the policy to + * SCHED_BATCH and then restoring it to the original policy. + * @tc.type: FUNC + */ +HWTEST_F(SchedSchedGetschedulerTest, sched_getscheduler_001, TestSize.Level1) +{ + int originalPolicy = sched_getscheduler(getpid()); + sched_param param; + EXPECT_EQ(0, sched_getparam(getpid(), ¶m)); + param.sched_priority = sched_get_priority_min(originalPolicy); + EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_BATCH, ¶m)); + EXPECT_EQ(SCHED_BATCH, sched_getscheduler(getpid())); + EXPECT_EQ(0, sched_setscheduler(getpid(), originalPolicy, ¶m)); + EXPECT_EQ(originalPolicy, sched_getscheduler(getpid())); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/search/search_gtest/search_remque_test.cpp b/libc-test/src/functionalext/supplement/search/search_gtest/search_remque_test.cpp new file mode 100644 index 000000000..fbe000929 --- /dev/null +++ b/libc-test/src/functionalext/supplement/search/search_gtest/search_remque_test.cpp @@ -0,0 +1,34 @@ +#include +#include + +using namespace testing::ext; + +class DirentRemqueTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +struct Node { + struct Node* next; + struct Node* prev; +}; + +/** + * @tc.name: remque_001 + * @tc.desc: Verify whether the forward and backward pointers of the linked list are correctly updated after + * successfully removing the specified element from the doubly linked list using the remque interface. + * @tc.type: FUNC + */ +HWTEST_F(DirentRemqueTest, remque_001, TestSize.Level1) +{ + struct Node e1 = { nullptr, nullptr }; + struct Node e2 = { nullptr, nullptr }; + struct Node e3 = { nullptr, nullptr }; + e1.next = &e2; + e2.prev = &e1; + e2.next = &e3; + e3.prev = &e2; + remque(&e2); + EXPECT_EQ(e1.next, &e3); + EXPECT_EQ(e3.prev, &e1); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/signal/signal_gtest/signal_raise_test.cpp b/libc-test/src/functionalext/supplement/signal/signal_gtest/signal_raise_test.cpp new file mode 100644 index 000000000..590beb56a --- /dev/null +++ b/libc-test/src/functionalext/supplement/signal/signal_gtest/signal_raise_test.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +class SignalRaiseTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +constexpr int RAISE_NUM = 9999; +bool g_signalFlag = false; + +void HandleSignal(int signum) +{ + g_signalFlag = true; +} + +/** + * @tc.name: raise_001 + * @tc.desc: Verify whether the raise interface returns the expected error code EINVAL when attempting to raise an + * invalid signal value. + * @tc.type: FUNC + */ +HWTEST_F(SignalRaiseTest, raise_001, TestSize.Level1) +{ + int result = raise(RAISE_NUM); + ASSERT_NE(result, 0); + EXPECT_EQ(errno, EINVAL); +} + +/** + * @tc.name: raise_002 + * @tc.desc: Verify whether the interface call fails and returns -1 when an invalid signal number (-1) is passed, and to + * verify that the error code is correctly set to EINVAL. + * @tc.type: FUNC + */ +HWTEST_F(SignalRaiseTest, raise_002, TestSize.Level1) +{ + errno = 0; + int result = raise(-1); + EXPECT_EQ(-1, result); + EXPECT_EQ(errno, EINVAL); +} + +/** + * @tc.name: raise_003 + * @tc.desc: Verify whether the raise interface can successfully ignore a signal when called with a valid signal value + * and set to be ignored. + * @tc.type: FUNC + */ +HWTEST_F(SignalRaiseTest, raise_003, TestSize.Level1) +{ + signal(SIGUSR1, SIG_IGN); + int result = raise(SIGUSR1); + EXPECT_EQ(result, 0); +} + +/** + * @tc.name: raise_004 + * @tc.desc: Verify the behavior of the raise function. It checks whether the raise(SIGRTMIN) call successfully triggers + * the signal handler when the handling method of SIGRTMIN is set to SIG_HOLD. + * @tc.type: FUNC + */ +HWTEST_F(SignalRaiseTest, raise_004, TestSize.Level1) +{ + signal(SIGRTMIN, HandleSignal); + sigset(SIGRTMIN, SIG_HOLD); + int result = raise(SIGRTMIN); + EXPECT_EQ(result, 0); + EXPECT_FALSE(g_signalFlag); +} + +/** + * @tc.name: raise_005 + * @tc.desc: Verify whether the function raise(SIGALRM) can correctly trigger the signal and call the corresponding + * signal handling function when the SIGALRM signal handling function is set. + * @tc.type: FUNC + */ +HWTEST_F(SignalRaiseTest, raise_005, TestSize.Level1) +{ + signal(SIGALRM, HandleSignal); + int result = raise(SIGALRM); + EXPECT_EQ(result, 0); + sleep(1); + EXPECT_TRUE(g_signalFlag); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stat/stat_gtest/stat_fstatfs64_test.cpp b/libc-test/src/functionalext/supplement/stat/stat_gtest/stat_fstatfs64_test.cpp index fa1b7c49c..4d447edd6 100644 --- a/libc-test/src/functionalext/supplement/stat/stat_gtest/stat_fstatfs64_test.cpp +++ b/libc-test/src/functionalext/supplement/stat/stat_gtest/stat_fstatfs64_test.cpp @@ -17,10 +17,10 @@ class StatFstatfs64Test : public testing::Test { */ HWTEST_F(StatFstatfs64Test, fstatfs64_001, TestSize.Level1) { - int fileDescriptor = open("/proc", O_RDONLY); - EXPECT_TRUE(fileDescriptor >= 0); + int fd = open("/proc", O_RDONLY); + ASSERT_NE(fd, -1); struct statfs64 stat; - int result = fstatfs64(fileDescriptor, &stat); + int result = fstatfs64(fd, &stat); + close(fd); EXPECT_EQ(0, result); - close(fileDescriptor); } \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fdopen_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fdopen_test.cpp new file mode 100644 index 000000000..de59a8b30 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fdopen_test.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace testing::ext; + +class StdioFdopenTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +class StdioFdopenDeathTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fdopen_001 + * @tc.desc: Read the contents of the file through the file descriptor. + * @tc.type: FUNC + */ +HWTEST_F(StdioFdopenTest, fdopen_001, TestSize.Level1) +{ + int fd = open("/proc/version", O_RDONLY); + ASSERT_NE(-1, fd); + FILE* fp = fdopen(fd, "r"); + EXPECT_NE(nullptr, fp); + + fclose(fp); + close(fd); +} + +/** + * @tc.name: fdopen_002 + * @tc.desc: input a invalid file mode. + * @tc.type: FUNC + */ +HWTEST_F(StdioFdopenDeathTest, fdopen_002, TestSize.Level1) +{ + int fd = open("/proc/version", O_RDONLY); + ASSERT_NE(-1, fd); + EXPECT_DEATH(fdopen(fd, nullptr), ""); + close(fd); +} diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fflush_unlocked_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fflush_unlocked_test.cpp index 2dfd241e3..a9c60ce6c 100644 --- a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fflush_unlocked_test.cpp +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fflush_unlocked_test.cpp @@ -20,4 +20,5 @@ HWTEST_F(StdioFflushunlockedTest, fflush_unlocked_001, TestSize.Level1) int result = fflush_unlocked(file); EXPECT_EQ(0, result); fclose(file); + remove("test_fflush_unlocked"); } \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fgetc_unlocked_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fgetc_unlocked_test.cpp new file mode 100644 index 000000000..422605aa8 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fgetc_unlocked_test.cpp @@ -0,0 +1,55 @@ +#include +#include + +using namespace testing::ext; + +class StdioFgetcUnlockedTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fgetc_unlocked_001 + * @tc.desc: Test whether the fgetc_unlocked interface can correctly return the ASCII value of each character when + * reading the file content character by character, and whether it can correctly return EOF after reading all + * characters. + * @tc.type: FUNC + */ +HWTEST_F(StdioFgetcUnlockedTest, fgetc_unlocked_001, TestSize.Level1) +{ + const char* fileName = "test.txt"; + FILE* file = fopen(fileName, "w"); + ASSERT_NE(file, nullptr); + const char* data = "Hello, World!"; + fputs(data, file); + fclose(file); + file = fopen(fileName, "r"); + ASSERT_NE(file, nullptr); + int expected; + for (const char* p = data; *p != '\0'; ++p) { + expected = *p; + int actual = fgetc_unlocked(file); + EXPECT_EQ(expected, actual); + } + int eof = fgetc_unlocked(file); + EXPECT_EQ(EOF, eof); + fclose(file); + remove(fileName); +} + +/** + * @tc.name: fgetc_unlocked_002 + * @tc.desc: Verify that calling fgetc_unlocked on an empty file should return EOF. + * @tc.type: FUNC + */ +HWTEST_F(StdioFgetcUnlockedTest, fgetc_unlocked_002, TestSize.Level1) +{ + FILE* fp = fopen("empty.txt", "w"); + fclose(fp); + fp = fopen("empty.txt", "r"); + ASSERT_NE(fp, nullptr); + int ch = fgetc_unlocked(fp); + EXPECT_EQ(ch, EOF); + fclose(fp); + remove("empty.txt"); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fileno_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fileno_test.cpp new file mode 100644 index 000000000..ead0d0021 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fileno_test.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +constexpr int BUF_SIZE = 100; + +class StdioFilenoTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fileno_001 + * @tc.desc: Verify the functionality of the fileno interface. It opens a file, retrieves its file descriptor using the + * fileno function, and performs assertions to validate the result. The file is then closed, and the error + * code is checked. Finally, the file is removed to ensure the success of the operation. + * @tc.type: FUNC + */ +HWTEST_F(StdioFilenoTest, fileno_001, TestSize.Level1) +{ + FILE* file = fopen("example.txt", "w"); + ASSERT_NE(file, nullptr); + errno = 0; + int result = fileno(file); + ASSERT_NE(result, -1); + fclose(file); + EXPECT_EQ(0, errno); + remove("example.txt"); +} + +/** + * @tc.name: fileno_002 + * @tc.desc: Verify the correctness of the file descriptor associated with a file stream object using the standard + * library function fileno. + * @tc.type: FUNC + */ +HWTEST_F(StdioFilenoTest, fileno_002, TestSize.Level1) +{ + const char* file = "test.txt"; + FILE* fp = fopen(file, "w"); + ASSERT_NE(fp, nullptr); + const char* data = "Hello, fileno!"; + int result = fputs(data, fp); + EXPECT_GE(result, 0); + fclose(fp); + FILE* ptr = fopen(file, "r"); + ASSERT_NE(ptr, nullptr); + int fd = fileno(ptr); + ASSERT_NE(fd, 0); + char buffer[BUF_SIZE]; + memset(buffer, 0, sizeof(buffer)); + ssize_t bytesRead = read(fd, buffer, sizeof(buffer)); + EXPECT_GT(bytesRead, 0); + EXPECT_STREQ(buffer, data); + fclose(ptr); + remove("test.txt"); +} + +/** + * @tc.name: fileno_003 + * @tc.desc: Verify whether the file descriptor associated with a process pipe opened by popen is correctly obtained + * using the fileno function. + * @tc.type: FUNC + */ +HWTEST_F(StdioFilenoTest, fileno_003, TestSize.Level1) +{ + FILE* ptr = popen("ls", "r"); + ASSERT_NE(ptr, nullptr); + int result = fileno(ptr); + EXPECT_GE(result, 0); + pclose(ptr); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fopen64_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fopen64_test.cpp new file mode 100644 index 000000000..36a85f427 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fopen64_test.cpp @@ -0,0 +1,60 @@ +#include +#include + +using namespace testing::ext; + +constexpr int BUF_SIZE = 100; + +class StdioFopen64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fopen64_001 + * @tc.desc: Verify whether the fopen64 interface can successfully open the /proc/version file and return a non-null + * file pointer. + * @tc.type: FUNC + */ +HWTEST_F(StdioFopen64Test, fopen64_001, TestSize.Level1) +{ + FILE* file = fopen64("/proc/version", "r"); + ASSERT_NE(file, nullptr); + fclose(file); +} + +/** + * @tc.name: fopen64_002 + * @tc.desc: Verify fopen64 ability to correctly open and close large files as expected, and to handle file operations + * correctly. + * @tc.type: FUNC + */ +HWTEST_F(StdioFopen64Test, fopen64_002, TestSize.Level1) +{ + FILE* file = fopen64("test.txt", "w"); + ASSERT_NE(file, nullptr); + fclose(file); + EXPECT_EQ(0, remove("test.txt")); +} + +/** + * @tc.name: fopen64_003 + * @tc.desc: Verify fopen64 ability to open, read, write, and close files correctly, as well as handle the case when the + * file does not exist. Such tests help ensure that the fopen64 function works properly in various scenarios + * and can handle edge cases of file operations. + * @tc.type: FUNC + */ +HWTEST_F(StdioFopen64Test, fopen64_003, TestSize.Level1) +{ + FILE* file = fopen64("test.txt", "w"); + ASSERT_NE(file, nullptr); + std::fprintf(file, "This is a test message.\n"); + fclose(file); + file = fopen64("test.txt", "r"); + ASSERT_NE(file, nullptr); + char buffer[BUF_SIZE]; + fgets(buffer, BUF_SIZE, file); + EXPECT_STREQ(buffer, "This is a test message.\n"); + fclose(file); + EXPECT_EQ(0, remove("test.txt")); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fpurge_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fpurge_test.cpp index 9eb36098e..818ab0e1c 100644 --- a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fpurge_test.cpp +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fpurge_test.cpp @@ -21,4 +21,5 @@ HWTEST_F(StdioFpurgeTest, fpurge_001, TestSize.Level1) int result = fpurge(file); EXPECT_EQ(0, result); fclose(file); + remove("test_fpurge.txt"); } \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fputs_unlocked_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fputs_unlocked_test.cpp new file mode 100644 index 000000000..01e61223f --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fputs_unlocked_test.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace testing::ext; + +class StdioFputsUnlockedTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fputs_unlocked_001 + * @tc.desc: Test whether the fputs_unlocked interface can correctly return the number of bytes written when writing + * content to a file and whether the written content is consistent with expectations. + * @tc.type: FUNC + */ +HWTEST_F(StdioFputsUnlockedTest, fputs_unlocked_001, TestSize.Level1) +{ + FILE* fp = fopen("test.txt", "w"); + ASSERT_NE(fp, nullptr); + const char* content = "Hello, fputs_unlocked!"; + int result = fputs_unlocked(content, fp); + EXPECT_GE(result, 0); + fclose(fp); + remove("test.txt"); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fread_unlocked_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fread_unlocked_test.cpp new file mode 100644 index 000000000..60701eb69 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fread_unlocked_test.cpp @@ -0,0 +1,56 @@ +#include +#include + +using namespace testing::ext; + +constexpr int BUF_SIZE = 256; + +class StdioFreadUnlockedTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fread_unlocked_001 + * @tc.desc: Verify the functionality of the fread_unlocked interface. It writes data to a file, then uses + * fread_unlocked to read data from the file, and verifies that the number of bytes read and the read content + * match the expected result. + * @tc.type: FUNC + */ +HWTEST_F(StdioFreadUnlockedTest, fread_unlocked_001, TestSize.Level1) +{ + FILE* file = fopen("test.txt", "w"); + ASSERT_NE(file, nullptr); + const char* content = "Hello, fread_unlocked!"; + fwrite(content, sizeof(char), strlen(content), file); + fclose(file); + file = fopen("test.txt", "r"); + ASSERT_NE(file, nullptr); + char buffer[BUF_SIZE]; + size_t byteRead = fread_unlocked(buffer, sizeof(char), sizeof(buffer), file); + + EXPECT_EQ(byteRead, strlen(content)); + EXPECT_STREQ(buffer, content); + fclose(file); + remove("test.txt"); +} + +/** + * @tc.name: fread_unlocked_002 + * @tc.desc: Verify whether the fread_unlocked function returns 0 when reading an empty file, and also checks the + * success of file opening, reading, and closing operations. + * @tc.type: FUNC + */ +HWTEST_F(StdioFreadUnlockedTest, fread_unlocked_002, TestSize.Level1) +{ + FILE* file = fopen("empty.bin", "wb"); + ASSERT_NE(file, nullptr); + fclose(file); + file = fopen("empty.bin", "rb"); + ASSERT_NE(file, nullptr); + char buffer[BUF_SIZE]; + size_t byteRead = fread_unlocked(buffer, 1, sizeof(buffer), file); + EXPECT_EQ(byteRead, 0); + fclose(file); + remove("empty.bin"); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_freopen64_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_freopen64_test.cpp new file mode 100644 index 000000000..743560191 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_freopen64_test.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +using namespace testing::ext; + +class StdioFreopen64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: freopen64_001 + * @tc.desc: Reopen the file stream and verify the file descriptor flag, ensuring that freopen64 correctly sets the + * FD_CLOEXEC flag. + * @tc.type: FUNC + */ +HWTEST_F(StdioFreopen64Test, freopen64_001, TestSize.Level1) +{ + FILE* file = fopen("/proc/version", "r"); + ASSERT_NE(file, nullptr); + int flags = fcntl(fileno(file), F_GETFD); + ASSERT_NE(flags, -1); + EXPECT_TRUE((flags & FD_CLOEXEC) == 0); + file = freopen64("/proc/version", "re", file); + ASSERT_NE(file, nullptr); + flags = fcntl(fileno(file), F_GETFD); + ASSERT_NE(flags, -1); + EXPECT_TRUE((flags & FD_CLOEXEC) == FD_CLOEXEC); + fclose(file); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fseeko64_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fseeko64_test.cpp new file mode 100644 index 000000000..d334ce5be --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fseeko64_test.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +class StdioFseeko64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +constexpr int INVALID_WHENCE = 111; +constexpr off64_t OFFSET = -3; + +/** + * @tc.name: fseeko64_001 + * @tc.desc: This test verifies whether the fseeko64 functions can correctly locate and obtain large file offsets. + * @tc.type: FUNC + */ +HWTEST_F(StdioFseeko64Test, fseeko64_001, TestSize.Level1) +{ + const char* testFileName = "fseeko64.txt"; + FILE* tempFile = fopen64(testFileName, "w+"); + ASSERT_NE(tempFile, nullptr); + EXPECT_EQ(0, fseeko64(tempFile, 0x1'0000'0000, SEEK_SET)); + fclose(tempFile); + EXPECT_TRUE(remove(testFileName) == 0); +} + +/** + * @tc.name: fseeko64_002 + * @tc.desc: This test verifies whether passing an illegal 'when' parameter correctly returns an EINVAL error when using + * the fseeko64 function. + * @tc.type: FUNC + */ +HWTEST_F(StdioFseeko64Test, fseeko64_002, TestSize.Level1) +{ + const char* testFileName = "fseeko64.txt"; + FILE* tempFile = fopen64(testFileName, "w+"); + ASSERT_NE(tempFile, nullptr); + EXPECT_EQ(-1, fseeko64(tempFile, 0, INVALID_WHENCE)); + EXPECT_EQ(EINVAL, errno); + fclose(tempFile); + EXPECT_TRUE(remove(testFileName) == 0); +} + +/** + * @tc.name: fseeko64_003 + * @tc.desc: This test verifies whether passing a negative offset parameter correctly returns an EINVAL error when using + * the fseeko64 function. + * @tc.type: FUNC + */ +HWTEST_F(StdioFseeko64Test, fseeko64_003, TestSize.Level1) +{ + const char* testFileName = "fseeko64.txt"; + FILE* tempFile = fopen64(testFileName, "w+"); + ASSERT_NE(tempFile, nullptr); + errno = 0; + EXPECT_EQ(-1, fseeko64(tempFile, OFFSET, SEEK_SET)); + EXPECT_EQ(EINVAL, errno); + fclose(tempFile); + EXPECT_TRUE(remove(testFileName) == 0); +} + +/** + * @tc.name: fseeko64_004 + * @tc.desc: This test verifies that in file operations, using the fseeko64 function can correctly position the file + * pointer to the specified offset position, and using the ftello64 function to obtain the offset value of the + * current file pointer. + * @tc.type: FUNC + */ +HWTEST_F(StdioFseeko64Test, fseeko64_004, TestSize.Level1) +{ + const char* testFileName = "fseeko64.txt"; + FILE* tempFile = fopen64(testFileName, "w+"); + ASSERT_NE(tempFile, nullptr); + ASSERT_EQ(0, fseeko64(tempFile, 0x3'0000'0000, SEEK_SET)); + ASSERT_EQ(0x3'0000'0000, ftello64(tempFile)); + ASSERT_EQ(0, fseeko64(tempFile, 0x2'0000'0000, SEEK_CUR)); + ASSERT_EQ(0x5'0000'0000, ftello64(tempFile)); + fclose(tempFile); + EXPECT_TRUE(remove(testFileName) == 0); +} + +/** + * @tc.name: fseeko64_005 + * @tc.desc: This test verifies that in file operations, the fseeko64 function is used to position the file pointer to + * the end of the file, and the ftello64 function is used to obtain the file size to ensure that the file size + * matches the expected content length. + * @tc.type: FUNC + */ +HWTEST_F(StdioFseeko64Test, fseeko64_005, TestSize.Level1) +{ + const char* testFileName = "fseeko64.txt"; + FILE* tempFile = fopen64(testFileName, "w+"); + ASSERT_NE(tempFile, nullptr); + const char* content = "Hello, World!"; + fputs(content, tempFile); + fseeko64(tempFile, 0, SEEK_END); + off64_t fileSize = ftello64(tempFile); + fclose(tempFile); + EXPECT_EQ(fileSize, strlen(content)); + EXPECT_TRUE(remove(testFileName) == 0); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fsetpos64_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fsetpos64_test.cpp new file mode 100644 index 000000000..bb939137f --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fsetpos64_test.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +class StdioFgetpos64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +constexpr int OFFSET = 4096; + +/** + * @tc.name: fsetpos64_001 + * @tc.desc: This test verifies that in file operations, using the fsetpos64 function can restore the file pointer to + * the previously saved location, thereby achieving the positioning and recovery function of the file pointer. + * @tc.type: FUNC + */ +HWTEST_F(StdioFgetpos64Test, fsetpos64_001, TestSize.Level1) +{ + const char* testFileName = "fsetpos64.txt"; + FILE* tempFile = fopen64(testFileName, "w+"); + ASSERT_NE(tempFile, nullptr); + EXPECT_EQ(0, fseek(tempFile, OFFSET, SEEK_SET)); + EXPECT_EQ(ftello(tempFile), OFFSET); + fpos64_t initialPos; + EXPECT_EQ(0, fgetpos64(tempFile, &initialPos)); + EXPECT_EQ(0, fseek(tempFile, OFFSET, SEEK_CUR)); + EXPECT_EQ(ftello(tempFile), OFFSET + OFFSET); + EXPECT_EQ(0, fsetpos64(tempFile, &initialPos)); + EXPECT_EQ(ftello(tempFile), OFFSET); + fclose(tempFile); + EXPECT_TRUE(remove(testFileName) == 0); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_ftello64_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_ftello64_test.cpp new file mode 100644 index 000000000..95df0d043 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_ftello64_test.cpp @@ -0,0 +1,46 @@ +#include +#include + +using namespace testing::ext; + +class StdioFtello64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: ftello64_001 + * @tc.desc: This test verifies whether the ftelo64 function returns the correct file position offset value after + * creating a temporary file and using the fseeko64 function for localization. + * @tc.type: FUNC + */ +HWTEST_F(StdioFtello64Test, ftello64_001, TestSize.Level1) +{ + const char* testFileName = "ftello64.txt"; + FILE* tempFile = fopen64(testFileName, "w+"); + ASSERT_NE(tempFile, nullptr); + EXPECT_EQ(0, fseeko64(tempFile, 0x4'0000'0000, SEEK_CUR)); + EXPECT_EQ(0x4'0000'0000, ftello64(tempFile)); + fclose(tempFile); + EXPECT_TRUE(remove(testFileName) == 0); +} + +/** + * @tc.name: ftello64_002 + * @tc.desc: This test verifies whether the fseek and ftello64 functions can correctly return the current location and + * size of the file after creating a temporary file and truncating its size to the specified size using the + * ftruncate64 function. + + * @tc.type: FUNC + */ +HWTEST_F(StdioFtello64Test, ftello64_002, TestSize.Level1) +{ + const char* testFileName = "ftello64.txt"; + FILE* tempFile = fopen64(testFileName, "w+"); + ASSERT_NE(tempFile, nullptr); + EXPECT_EQ(0, ftruncate64(fileno(tempFile), 0x5'0000'0000)); + EXPECT_EQ(0, fseek(tempFile, 0, SEEK_END)); + EXPECT_EQ(0x5'0000'0000, ftello64(tempFile)); + fclose(tempFile); + EXPECT_TRUE(remove(testFileName) == 0); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_ftello_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_ftello_test.cpp new file mode 100644 index 000000000..56b321cb0 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_ftello_test.cpp @@ -0,0 +1,137 @@ +#include +#include +#include + +using namespace testing::ext; + +class StdioFtelloTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +constexpr int BUFSIZE = 32; +constexpr int OFFSET_10 = 10; +constexpr int OFFSET_20 = 20; +constexpr int OFFSET_30 = 30; +constexpr int OFFSET_100 = 100; +constexpr int OFFSET_MINUS_2 = -2; +constexpr int OFFSET_MINUS_200 = -200; + +/** + * @tc.name: ftello_001 + * @tc.desc: Test that when the file stream does not exist, ftello returns an exception value. + * @tc.type: FUNC + */ +HWTEST_F(StdioFtelloTest, ftello_001, TestSize.Level1) +{ + const char* testFileName = "ftello_001.txt"; + FILE* tempFile = fopen(testFileName, "w+"); + ASSERT_NE(tempFile, nullptr); + const char* content = "Hello, World!"; + fputs(content, tempFile); + fclose(tempFile); + remove(testFileName); + + EXPECT_EQ(-1, ftello(tempFile)); +} + +/** + * @tc.name: ftello_002 + * @tc.desc: Test the ability to read the current position of a system file stream normally. + * @tc.type: FUNC + */ +HWTEST_F(StdioFtelloTest, ftello_002, TestSize.Level1) +{ + FILE* file = fopen("/proc/version", "r"); + ASSERT_NE(file, nullptr); + long offset = ftello(file); + ASSERT_NE(offset, -1); + fclose(file); +} + +/** + * @tc.name: ftello_003 + * @tc.desc: Test that after offsetting from the starting position and current position of the file stream, the current + * position is correct. + * @tc.type: FUNC + */ +HWTEST_F(StdioFtelloTest, ftello_003, TestSize.Level1) +{ + const char* filePath = "/tmp/ftello.txt"; + FILE* file = fopen(filePath, "w+"); + ASSERT_NE(file, nullptr); + fseek(file, OFFSET_10, SEEK_SET); + fseek(file, OFFSET_20, SEEK_CUR); + long int position = ftello(file); + EXPECT_EQ(OFFSET_30, position); + fclose(file); + EXPECT_TRUE(remove(filePath) == 0); +} + +/** + * @tc.name: ftello_004 + * @tc.desc: Test that after offsetting an outlier from the end of the file stream, the current position remains at the + * position of the string length in the file stream. + * @tc.type: FUNC + */ +HWTEST_F(StdioFtelloTest, ftello_004, TestSize.Level1) +{ + FILE* tempFile; + EXPECT_NE(nullptr, tempFile = fmemopen(nullptr, BUFSIZE, "a")); + const char* content = "Hello, World!"; + fputs(content, tempFile); + + fseeko(tempFile, OFFSET_MINUS_200, SEEK_END); + off_t position = ftello(tempFile); + EXPECT_EQ(strlen(content), position); + fclose(tempFile); +} + +/** + * @tc.name: ftello_005 + * @tc.desc: Test when offsetting an outlier from the beginning position, the file stream position is not offset. + * @tc.type: FUNC + */ +HWTEST_F(StdioFtelloTest, ftello_005, TestSize.Level1) +{ + FILE* tempFile; + EXPECT_NE(nullptr, tempFile = fmemopen(nullptr, BUFSIZE, "a")); + fseeko(tempFile, -1, SEEK_SET); + off_t position = ftello(tempFile); + EXPECT_EQ(0, position); + fclose(tempFile); +} + +/** + * @tc.name: ftello_006 + * @tc.desc: Test that after offsetting from the starting position and end position of the file stream, the current + * position is correct. + * @tc.type: FUNC + */ +HWTEST_F(StdioFtelloTest, ftello_006, TestSize.Level1) +{ + FILE* tempFile = fmemopen(nullptr, BUFSIZE, "a"); + const char* content = "Hello, World!"; + fputs(content, tempFile); + + fseeko(tempFile, OFFSET_20, SEEK_SET); + fseeko(tempFile, OFFSET_MINUS_2, SEEK_END); + off_t position = ftello(tempFile); + EXPECT_EQ(strlen(content) + OFFSET_MINUS_2, position); + fclose(tempFile); +} + +/** + * @tc.name: ftello_007 + * @tc.desc: Test when offsetting an outlier from the current position, the file stream position is not offset. + * @tc.type: FUNC + */ +HWTEST_F(StdioFtelloTest, ftello_007, TestSize.Level1) +{ + FILE* tempFile; + EXPECT_NE(nullptr, tempFile = fmemopen(nullptr, BUFSIZE, "a")); + fseeko(tempFile, OFFSET_100, SEEK_SET); + fseeko(tempFile, OFFSET_MINUS_200, SEEK_CUR); + EXPECT_EQ(0, ftello(tempFile)); + fclose(tempFile); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_funlockfile_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_funlockfile_test.cpp new file mode 100644 index 000000000..695dc9c50 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_funlockfile_test.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +class StdioFunlockfileTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +constexpr int SIZE = 100; + +/** + * @tc.name: funlockfile_001 + * @tc.desc: This test verifies that after locking the file, Funlockfile was successfully used to unlock the operation + * and ensure that data can be written and read correctly. + * @tc.type: FUNC + */ +HWTEST_F(StdioFunlockfileTest, funlockfile_001, TestSize.Level1) +{ + const char* testFileName = "funlockfile.txt"; + FILE* file = fopen(testFileName, "w+"); + ASSERT_NE(file, nullptr); + flockfile(file); + const char* testData = "Test data"; + fputs(testData, file); + fflush(file); + funlockfile(file); + fclose(file); + file = fopen(testFileName, "r"); + char buffer[SIZE]; + fgets(buffer, sizeof(buffer), file); + fclose(file); + ASSERT_STREQ(buffer, testData); + EXPECT_TRUE(remove(testFileName) == 0); +} + +/** + * @tc.name: funlockfile_002 + * @tc.desc: This test verifies that after locking the file, the feof function is used to obtain the file end flag, and + * the file is finally unlocked. + * @tc.type: FUNC + */ +HWTEST_F(StdioFunlockfileTest, funlockfile_002, TestSize.Level1) +{ + const char* testFileName = "funlockfile.txt"; + FILE* file = fopen(testFileName, "w+"); + ASSERT_NE(file, nullptr); + flockfile(file); + feof(file); + funlockfile(file); + fclose(file); + EXPECT_TRUE(remove(testFileName) == 0); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fwrite_unlocked_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fwrite_unlocked_test.cpp new file mode 100644 index 000000000..0cbedcb20 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fwrite_unlocked_test.cpp @@ -0,0 +1,49 @@ +#include +#include + +using namespace testing::ext; + +class StdioFwriteunlockedTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: fwrite_unlocked_001 + * @tc.desc: test the functionality of the fwrite_unlocked function in relation to file operations. + * @tc.type: FUNC + **/ +HWTEST_F(StdioFwriteunlockedTest, fwrite_unlocked_001, TestSize.Level1) +{ + const char* testFileName = "fwrite_unlocked.txt"; + FILE* tempFile = fopen(testFileName, "w+"); + ASSERT_NE(tempFile, nullptr); + const char* str = "Hello, World!"; + size_t strSize = strlen(str); + size_t result = fwrite_unlocked(str, sizeof(char), strSize, tempFile); + EXPECT_EQ(result, strSize); + fclose(tempFile); + EXPECT_TRUE(remove(testFileName) == 0); +} + +/** + * @tc.name: fwrite_unlocked_002 + * @tc.desc: This test verifies that fwrite is called when a file is opened in read-only mode_ The unlocked function + * will not successfully write any bytes when writing data, and the file content will not be modified. + * @tc.type: FUNC + **/ +HWTEST_F(StdioFwriteunlockedTest, fwrite_unlocked_002, TestSize.Level1) +{ + const char* filePath = "/tmp/fwrite_unlocked_test.txt"; + FILE* file = fopen(filePath, "w"); + ASSERT_NE(file, nullptr); + fclose(file); + file = fopen(filePath, "r"); + ASSERT_NE(file, nullptr); + const char* str = "Hello, World!"; + size_t strSize = strlen(str); + size_t bytesWritten = fwrite_unlocked(str, sizeof(char), strSize, file); + fclose(file); + EXPECT_EQ(bytesWritten, 0); + EXPECT_TRUE(remove(filePath) == 0); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fwscanf_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fwscanf_test.cpp new file mode 100644 index 000000000..20d729282 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_fwscanf_test.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace testing::ext; + +class StdioFwscanfTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +constexpr int NAME_SIZE = 20; +constexpr int EXPECTED_ARGS = 2; +constexpr int EXPECTED_AGE = 30; + +/** + * @tc.name: fwscanf_001 + * @tc.desc: This test verifies that the fwscanf function can correctly parse the data in the file and compare it with + * the expected results to ensure that the read person names and ages match the expected values. + * @tc.type: FUNC + */ +HWTEST_F(StdioFwscanfTest, fwscanf_001, TestSize.Level1) +{ + const char* testFileName = "fwscanf.txt"; + FILE* fd = fopen(testFileName, "w"); + ASSERT_NE(fd, nullptr); + fprintf(fd, "John 30"); + fclose(fd); + fd = fopen(testFileName, "r"); + ASSERT_NE(fd, nullptr); + wchar_t personName[NAME_SIZE]; + int personAge; + int result = fwscanf(fd, L"%ls %d", personName, &personAge); + fclose(fd); + EXPECT_EQ(result, EXPECTED_ARGS); + EXPECT_STREQ(static_cast(personName), L"John"); + EXPECT_EQ(personAge, EXPECTED_AGE); + EXPECT_TRUE(remove(testFileName) == 0); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_printf_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_printf_test.cpp new file mode 100644 index 000000000..055ae8226 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_printf_test.cpp @@ -0,0 +1,120 @@ +#include +#include + +using namespace testing::ext; + +constexpr int INT_OUTPUT = 6; +constexpr double DOUBLE_OUTPUT = 6.0; +constexpr float FLOAT_OUTPUT = 6.0f; + +class StdioPrintfTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: printf_001 + * @tc.desc: Verify the output data in the specified format (the format character is d) + * @tc.type: FUNC + */ +HWTEST_F(StdioPrintfTest, printf_001, TestSize.Level1) +{ + int result = printf("%d\n", INT_OUTPUT); + EXPECT_GT(result, 0); +} + +/** + * @tc.name: printf_002 + * @tc.desc: Verify the output data in the specified format (the format character is o) + * @tc.type: FUNC + */ +HWTEST_F(StdioPrintfTest, printf_002, TestSize.Level1) +{ + int result = printf("%o\n", INT_OUTPUT); + EXPECT_GT(result, 0); +} + +/** + * @tc.name: printf_003 + * @tc.desc: Verify the output data in the specified format (the format character is x,X) + * @tc.type: FUNC + */ +HWTEST_F(StdioPrintfTest, printf_003, TestSize.Level1) +{ + int result = printf("%x\n", INT_OUTPUT); + EXPECT_GT(result, 0); + result = printf("%X\n", INT_OUTPUT); + EXPECT_GT(result, 0); +} + +/** + * @tc.name: printf_004 + * @tc.desc: Verify the output data in the specified format (the format character is u) + * @tc.type: FUNC + */ +HWTEST_F(StdioPrintfTest, printf_004, TestSize.Level1) +{ + int result = printf("%u\n", INT_OUTPUT); + EXPECT_GT(result, 0); +} + +/** + * @tc.name: printf_005 + * @tc.desc: Verify the output data in the specified format (the format character is f) + * @tc.type: FUNC + */ +HWTEST_F(StdioPrintfTest, printf_005, TestSize.Level1) +{ + int result = printf("%f\n", FLOAT_OUTPUT); + EXPECT_GT(result, 0); +} + +/** + * @tc.name: printf_006 + * @tc.desc: Verify the output data in the specified format (the format character is e,E) + * @tc.type: FUNC + */ +HWTEST_F(StdioPrintfTest, printf_006, TestSize.Level1) +{ + int result = printf("%e\n", DOUBLE_OUTPUT); + EXPECT_GT(result, 0); + result = printf("%E\n", DOUBLE_OUTPUT); + EXPECT_GT(result, 0); +} + +/** + * @tc.name: printf_007 + * @tc.desc: Verify the output data in the specified format (the format character is g,G) + * @tc.type: FUNC + */ +HWTEST_F(StdioPrintfTest, printf_007, TestSize.Level1) +{ + int result = printf("%g\n", DOUBLE_OUTPUT); + EXPECT_GT(result, 0); + result = printf("%G\n", DOUBLE_OUTPUT); + EXPECT_GT(result, 0); +} + +/** + * @tc.name: printf_008 + * @tc.desc: Verify the output data in the specified format (the format character is c) + * @tc.type: FUNC + */ +HWTEST_F(StdioPrintfTest, printf_008, TestSize.Level1) +{ + char ch = 'a'; + int result = printf("%c\n", ch); + EXPECT_GT(result, 0); +} + +/** + * @tc.name: printf_009 + * @tc.desc: Verify the output data in the specified format (the format character is s) + * @tc.type: FUNC + */ +HWTEST_F(StdioPrintfTest, printf_009, TestSize.Level1) +{ + char num[] = "test"; + int result = printf("%s\n", num); + EXPECT_GT(result, 0); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_scanf_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_scanf_test.cpp new file mode 100644 index 000000000..ac7d28a18 --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_scanf_test.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +constexpr int STR_SIZE = 1024; + +class StdioScanfTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: scanf_001 + * @tc.desc: Verify whether the read operation of this scanf is normal. + * @tc.type: FUNC + */ +HWTEST_F(StdioScanfTest, scanf_001, TestSize.Level1) +{ + pid_t pid; + pid = fork(); + ASSERT_NE(-1, pid); + if (pid == 0) { + char str[STR_SIZE]; + char srcStr[STR_SIZE] = "test"; + int fd = open("./scanf_test.txt", O_WRONLY | O_CREAT, 0777); + ASSERT_NE(-1, fd); + write(fd, srcStr, sizeof(srcStr)); + close(fd); + fd = open("./scanf_test.txt", O_RDONLY); + ASSERT_NE(-1, fd); + int orgFd = dup(STDIN_FILENO); + dup2(fd, STDIN_FILENO); + scanf("%s", str); + EXPECT_STREQ(srcStr, str); + dup2(orgFd, STDIN_FILENO); + close(fd); + remove("./scanf_test.txt"); + _exit(0); + } else { + waitpid(pid, nullptr, 0); + } +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_wscanf_test.cpp b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_wscanf_test.cpp new file mode 100644 index 000000000..f3e5780fd --- /dev/null +++ b/libc-test/src/functionalext/supplement/stdio/stdio_gtest/stdio_wscanf_test.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +constexpr unsigned int BUF_SIZE = 10; + +class StdioWscanfTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: wscanf_001 + * @tc.desc: Redirects the input file descriptor so that wscanf reads it from "StdioWscanfTest.wscanf_001" + * @tc.type: FUNC + */ +HWTEST_F(StdioWscanfTest, wscanf_001, TestSize.Level1) +{ + const char* input = "StdioWscanfTest.wscanf_001"; + const wchar_t* data = L"S"; + const size_t strSize = wcslen(data) * sizeof(wchar_t); + int fd, stdinBak; + wchar_t wstr[BUF_SIZE] = { 0 }; + + fd = open(input, O_WRONLY | O_CREAT | O_TRUNC, 0644); + ASSERT_NE(-1, fd); + write(fd, data, strSize); + close(fd); + + stdinBak = dup(STDIN_FILENO); + ASSERT_NE(-1, stdinBak); + ASSERT_EQ(0, close(STDIN_FILENO)); + fd = open(input, O_RDONLY); + ASSERT_NE(-1, fd); + ASSERT_EQ(STDIN_FILENO, fd); + EXPECT_LT(0, wscanf(L"%ls", wstr)); + ASSERT_EQ(0, close(fd)); + + ASSERT_EQ(STDIN_FILENO, dup(stdinBak)); + ASSERT_EQ(0, close(stdinBak)); + ASSERT_EQ(0, remove(input)); + + EXPECT_EQ(0, wcscmp(data, wstr)); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/string/string_gtest/string_strcasecmp_l_test.cpp b/libc-test/src/functionalext/supplement/string/string_gtest/string_strcasecmp_l_test.cpp new file mode 100644 index 000000000..f698ce07a --- /dev/null +++ b/libc-test/src/functionalext/supplement/string/string_gtest/string_strcasecmp_l_test.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace testing::ext; + +class StringStrcasecmplTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: strcasecmp_l_001 + * @tc.desc: This test verifies strcasecmp_l be able to correctly perform case insensitive string comparisons when + * specifying regional settings. + * @tc.type: FUNC + */ +HWTEST_F(StringStrcasecmplTest, strcasecmp_l_001, TestSize.Level1) +{ + locale_t locale = newlocale(LC_ALL, "C", nullptr); + EXPECT_EQ(strcasecmp_l("world", "WORLD", locale), 0); + EXPECT_EQ(strcasecmp_l("world1", "world2", locale), -1); + EXPECT_EQ(strcasecmp_l("world2", "world1", locale), 1); + freelocale(locale); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/string/string_gtest/string_strncasecmp_l_test.cpp b/libc-test/src/functionalext/supplement/string/string_gtest/string_strncasecmp_l_test.cpp new file mode 100644 index 000000000..7ba867cb3 --- /dev/null +++ b/libc-test/src/functionalext/supplement/string/string_gtest/string_strncasecmp_l_test.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace testing::ext; + +class StringStrncasecmplTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +constexpr int LEN_3 = 3; +constexpr int LEN_6 = 6; +constexpr int EXPECT_VALUE = -1; + +/** + * @tc.name: strncasecmp_l_001 + * @tc.desc: This test verifies strncasecmp_l function can correctly perform case insensitive string comparison + * operations with limited length when specifying a specific region setting. + * @tc.type: FUNC + */ +HWTEST_F(StringStrncasecmplTest, strncasecmp_l_001, TestSize.Level1) +{ + locale_t locale = newlocale(LC_ALL, "C", nullptr); + EXPECT_EQ(strncasecmp_l("world", "WORLD", LEN_3, locale), 0); + EXPECT_EQ(strncasecmp_l("xyzBB", "XYZCC", LEN_3, locale), 0); + EXPECT_EQ(strncasecmp_l("world1", "world2", LEN_6, locale), EXPECT_VALUE); + EXPECT_EQ(strncasecmp_l("world2", "world1", LEN_6, locale), 1); + freelocale(locale); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_preadv64_test.cpp b/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_preadv64_test.cpp new file mode 100644 index 000000000..ef60d7e4b --- /dev/null +++ b/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_preadv64_test.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +constexpr int BUF1_SIZE = 10; +constexpr int BUF2_SIZE = 10; +constexpr int IOV_SIZE = 2; + +class SysPreadv64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: preadv64_001 + * @tc.desc: Verify whether preadv64 function can successfully read data from a file when it is opened in read-only + * mode. + * @tc.type: FUNC + */ +HWTEST_F(SysPreadv64Test, preadv64_001, TestSize.Level1) +{ + const char* file = "test.txt"; + int fd = open(file, O_CREAT | O_RDONLY, 0644); + ASSERT_NE(fd, -1); + char buf1[BUF1_SIZE], buf2[BUF2_SIZE]; + struct iovec iov[IOV_SIZE]; + iov[0].iov_base = buf1; + iov[0].iov_len = sizeof(buf1); + iov[1].iov_base = buf2; + iov[1].iov_len = sizeof(buf2); + ssize_t ret = preadv64(fd, iov, IOV_SIZE, 0); + ASSERT_NE(ret, -1); + close(fd); + remove(file); +} + +/** + * @tc.name: preadv64_002 + * @tc.desc: Verify that the preadv64 interface can correctly read data from a file at the specified offset. + * @tc.type: FUNC + */ +HWTEST_F(SysPreadv64Test, preadv64_002, TestSize.Level1) +{ + const char* file = "test.txt"; + int fd = open(file, O_CREAT | O_RDWR, 0644); + ASSERT_NE(fd, -1); + const char* data = "Hello, world!"; + EXPECT_EQ(write(fd, data, strlen(data)), strlen(data)); + struct iovec iov[IOV_SIZE]; + char buf1[BUF1_SIZE]; + char buf2[BUF2_SIZE]; + iov[0].iov_base = buf1; + iov[0].iov_len = sizeof(buf1); + iov[1].iov_base = buf2; + iov[1].iov_len = sizeof(buf2); + ssize_t n = preadv64(fd, iov, IOV_SIZE, 0); + EXPECT_EQ(n, static_cast(strlen(data))); + close(fd); + remove("test.txt"); +} + +/** + * @tc.name: preadv64_003 + * @tc.desc: Test the functionality of reading and writing on a specified file descriptor using the pwritev64 and + * preadv64 functions. The test involves using different offsets and data lengths to verify the correctness + * and reliability of the functions. By comparing the results of reading and writing, as well as validating + * the data in the buffer, the correctness of the function is confirmed. + * @tc.type: FUNC + */ +HWTEST_F(SysPreadv64Test, preadv64_003, TestSize.Level1) +{ + const char* file = "test.txt"; + int fd = open(file, O_CREAT | O_RDWR, 0644); + ASSERT_NE(fd, -1); + char buf[] = "world"; + struct iovec iov[1]; + iov[0].iov_base = buf; + iov[0].iov_len = sizeof(buf); + EXPECT_EQ(sizeof(buf), pwritev64(fd, iov, 1, sizeof(buf))); + EXPECT_EQ(0, lseek(fd, 0, SEEK_CUR)); + strcpy(buf, "hello"); + EXPECT_EQ(sizeof(buf), pwritev64(fd, iov, 1, 0)); + EXPECT_EQ(0, lseek(fd, 0, SEEK_CUR)); + EXPECT_EQ(sizeof(buf), preadv64(fd, iov, 1, sizeof(buf))); + EXPECT_STREQ("world", buf); + EXPECT_EQ(sizeof(buf), preadv64(fd, iov, 1, 0)); + EXPECT_STREQ("hello", buf); + close(fd); + remove("test.txt"); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_prlimit64_test.cpp b/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_prlimit64_test.cpp new file mode 100644 index 000000000..4978cfd47 --- /dev/null +++ b/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_prlimit64_test.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace testing::ext; + +constexpr rlim64_t KSOFT_LIMIT = 100; +constexpr rlim64_t KHARD_LIMIT = 200; + +class SysPrlimit64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** +* @tc.name: prlimit64_001 +* @tc.desc: Verify the prlimit64 interface by setting and retrieving process resource limits, and ensures the + correctness of setting new limits and retrieving old limits. +* @tc.type: FUNC +*/ +HWTEST_F(SysPrlimit64Test, prlimit64_001, TestSize.Level1) +{ + pid_t pid = getpid(); + int resource = RLIMIT_NOFILE; + struct rlimit64 newLimit, oldLimit; + newLimit.rlim_cur = KSOFT_LIMIT; + newLimit.rlim_max = KHARD_LIMIT; + int result = prlimit64(pid, resource, &newLimit, &oldLimit); + EXPECT_EQ(result, 0); + result = prlimit64(pid, resource, nullptr, &oldLimit); + EXPECT_EQ(result, 0); + EXPECT_EQ(oldLimit.rlim_cur, newLimit.rlim_cur); + EXPECT_EQ(oldLimit.rlim_max, newLimit.rlim_max); +} + +/** +* @tc.name: prlimit64_002 +* @tc.desc: Test the prlimit64 interface. The purpose is to verify that when calling this interface, it can correctly + retrieve the core file size limit of the current process, and the obtained resource limit values match the + expected values. +* @tc.type: FUNC +*/ +HWTEST_F(SysPrlimit64Test, prlimit64_002, TestSize.Level1) +{ + struct rlimit64 newLimit; + int result = prlimit64(0, RLIMIT_CORE, &newLimit, nullptr); + EXPECT_EQ(result, 0); + result = prlimit64(0, RLIMIT_CORE, &newLimit, nullptr); + EXPECT_EQ(result, 0); + EXPECT_EQ(newLimit.rlim_cur, newLimit.rlim_cur); + EXPECT_EQ(newLimit.rlim_max, newLimit.rlim_max); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_process_vm_writev_test.cpp b/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_process_vm_writev_test.cpp new file mode 100644 index 000000000..3e86b7960 --- /dev/null +++ b/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_process_vm_writev_test.cpp @@ -0,0 +1,71 @@ +#include +#include +#include + +using namespace testing::ext; + +constexpr int DST_SIZE = 1024; +constexpr int SRC_SIZE = 1024; + +class SysProcessVmWritevTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: process_vm_writev_001 + * @tc.desc: Verify whether the process_vm_writev interface returns the expected error code ESRCH when attempting to + * write data to the memory space of a non-existent target process. + * @tc.type: FUNC + */ +HWTEST_F(SysProcessVmWritevTest, process_vm_writev_001, TestSize.Level1) +{ + const char* data = "Hello, world!"; + size_t dataLength = strlen(data); + struct iovec source[1]; + source[0].iov_base = const_cast(data); + source[0].iov_len = dataLength; + struct iovec destination[1]; + destination[0].iov_base = reinterpret_cast(0x1000); + destination[0].iov_len = dataLength; + ssize_t result = process_vm_writev(-1, source, 1, destination, 1, 0); + EXPECT_EQ(result, -1); + EXPECT_EQ(errno, ESRCH); +} + +/** + * @tc.name: process_vm_writev_002 + * @tc.desc: Verify whether the process_vm_writev interface returns the expected error code EFAULT when called with null + * pointer arguments. + * @tc.type: FUNC + */ +HWTEST_F(SysProcessVmWritevTest, process_vm_writev_002, TestSize.Level1) +{ + ssize_t result = process_vm_writev(getpid(), nullptr, 1, nullptr, 1, 0); + EXPECT_EQ(result, -1); + EXPECT_EQ(errno, EFAULT); +} + +/** + * @tc.name: process_vm_writev_003 + * @tc.desc: Validate the process_vm_writev interface. It tests the functionality of the interface by performing memory + * copy between a local process and a remote process. Firstly, it attempts to write data from the local + * process to the specified memory region in the remote process and verifies the result. Then, it tries to + * write to a null pointer memory region in the remote process to test the interface's handling of error + * conditions. + * @tc.type: FUNC + */ +HWTEST_F(SysProcessVmWritevTest, process_vm_writev_003, TestSize.Level1) +{ + EXPECT_EQ(0, process_vm_writev(0, nullptr, 0, nullptr, 0, 0)); + char src[SRC_SIZE] = "Hello,world!"; + char dst[DST_SIZE] = ""; + iovec remote = { dst, sizeof(dst) }; + iovec local = { src, sizeof(src) }; + EXPECT_EQ(ssize_t(sizeof(src)), process_vm_writev(getpid(), &local, 1, &remote, 1, 0)); + EXPECT_EQ('H', dst[0]); + EXPECT_EQ(0, memcmp(src, dst, sizeof(src))); + remote = { nullptr, sizeof(dst) }; + EXPECT_EQ(-1, process_vm_writev(getpid(), &local, 1, &remote, 1, 0)); + EXPECT_EQ(EFAULT, errno); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_pwritev64_test.cpp b/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_pwritev64_test.cpp new file mode 100644 index 000000000..4fbdef024 --- /dev/null +++ b/libc-test/src/functionalext/supplement/sys/sys_gtest/sys_pwritev64_test.cpp @@ -0,0 +1,38 @@ +#include +#include +#include + +using namespace testing::ext; + +constexpr int IOV_SIZE = 2; + +class SysPwritev64Test : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +extern "C" ssize_t pwritev64(int fd, const struct iovec* iov, int count, off_t ofs); + +/** + * @tc.name: pwritev64_001 + * @tc.desc: Validate the pwritev64 interface by performing a write operation to a file using multiple buffer segments, + * and then checking the success of the operation as well as the correctness of the data written. + * @tc.type: FUNC + */ +HWTEST_F(SysPwritev64Test, pwritev64_001, TestSize.Level1) +{ + const char* file = "test.txt"; + int fd = open(file, O_CREAT | O_WRONLY, 0644); + ASSERT_NE(fd, -1); + struct iovec iov[IOV_SIZE]; + char buffer1[] = "Hello "; + char buffer2[] = "World!"; + iov[0].iov_base = buffer1; + iov[0].iov_len = strlen(buffer1); + iov[1].iov_base = buffer2; + iov[1].iov_len = strlen(buffer2); + ssize_t result = pwritev64(fd, iov, IOV_SIZE, 0); + EXPECT_EQ(result, strlen(buffer1) + strlen(buffer2)); + close(fd); + EXPECT_EQ(0, remove(file)); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/termios/termios_gtest/termios_tcflush_test.cpp b/libc-test/src/functionalext/supplement/termios/termios_gtest/termios_tcflush_test.cpp new file mode 100644 index 000000000..e31da4c52 --- /dev/null +++ b/libc-test/src/functionalext/supplement/termios/termios_gtest/termios_tcflush_test.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +using namespace testing::ext; + +class TermiosTcflushTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +/** + * @tc.name: tcflush_001 + * @tc.desc: This test verifies that when using the tcflush function to clear the terminal input/output buffer, the + * function returns a value of 0, indicating successful clearing of the buffer. + * @tc.type: FUNC + */ +HWTEST_F(TermiosTcflushTest, tcflush_001, TestSize.Level1) +{ + const char* ttyDevice = "/dev/ptmx"; + int fd = open(ttyDevice, O_RDWR); + ASSERT_NE(fd, -1); + int result = tcflush(fd, TCIOFLUSH); + EXPECT_EQ(result, 0); + close(fd); +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/unistd/unistd_gtest/unistd_sync_test.cpp b/libc-test/src/functionalext/supplement/unistd/unistd_gtest/unistd_sync_test.cpp new file mode 100644 index 000000000..11cdef436 --- /dev/null +++ b/libc-test/src/functionalext/supplement/unistd/unistd_gtest/unistd_sync_test.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +using namespace testing::ext; + +class UnistdSyncTest : public testing::Test { + void SetUp() override {} + void TearDown() override {} +}; + +constexpr int SIZE = 100; + +/** + * @tc.name: sync_001 + * @tc.desc: This test verifies that after the file is created and the data is written, the sync function is called to + * synchronize the data to the disk, and ensure that after the file is reopened, the data in the file is + * consistent with the data written previously. + * @tc.type: FUNC + */ +HWTEST_F(UnistdSyncTest, sync_001, TestSize.Level1) +{ + const char* testFileName = "sync.txt"; + const char* testData = "test data"; + FILE* file = fopen(testFileName, "w+"); + ASSERT_NE(file, nullptr); + fputs(testData, file); + fflush(file); + fclose(file); + sync(); + file = fopen(testFileName, "r"); + ASSERT_NE(file, nullptr); + char buffer[SIZE] = { 0 }; + fgets(buffer, sizeof(buffer), file); + fclose(file); + EXPECT_STREQ(testData, buffer); + EXPECT_EQ(remove(testFileName), 0); +} \ No newline at end of file -- Gitee