diff --git a/libc-test/src/common/BUILD.gn b/libc-test/src/common/BUILD.gn index a55a3cd014bc4425ebf823494bf7b70b6e39a995..d23a4cfc59993b433ae3807394baae9670cdc3bd 100644 --- a/libc-test/src/common/BUILD.gn +++ b/libc-test/src/common/BUILD.gn @@ -78,3 +78,36 @@ config("config_runtest") { libs = [ "//${target_out_dir}/libtest.a" ] } + +config("config_unittest") { + include_dirs = [ "." ] + + cflags_c = [ + "-pipe", + "-std=c99", + "-ffreestanding", + "-nostdinc", + "-D_POSIX_C_SOURCE=200809L", + "-Wall", + "-Wno-unused", + "-Wno-unused-function", + "-Wno-missing-braces", + "-Wno-overflow", + "-Wno-unknown-pragmas", + "-Wno-unsupported-floating-point-opt", + "-Wno-parentheses", + "-fno-builtin", + "-frounding-math", + "-Werror=implicit-function-declaration", + "-Werror=implicit-int", + "-Werror=pointer-sign", + "-Werror=pointer-arith", + "-Wno-error=unused-function", + "-g", + "-D_FILE_OFFSET_BITS=64", + ] + + ldflags = [ "-nostdlib" ] + + libs = [ "//${target_out_dir}/libtest.a" ] +} diff --git a/libc-test/src/functional/pthread_getname_np.c b/libc-test/src/functional/pthread_getname_np.c index 37cd3d3ce19475ae2f4cc0c23ecb7d8e16e3fde1..c0d8081f8cf8a2dc8672e98f2164be0d3c6e1306 100644 --- a/libc-test/src/functional/pthread_getname_np.c +++ b/libc-test/src/functional/pthread_getname_np.c @@ -26,6 +26,9 @@ int main(int argc, char **argv) char set_thread_name[] = "THREADFOO"; char default_name[] = "pthread_getname"; + rc = pthread_getname_np(pthread_self(), thread_name, 0); + if (rc != ERANGE) errExitEN(rc, "pthread_getname_np(invalid args[len]) failed"); + rc = pthread_getname_np(pthread_self(), thread_name, NAMELEN); if (rc != 0) errExitEN(rc, "pthread_getname_np failed"); if(strcmp(thread_name, default_name) != 0) perror("pthread name comparison failed"); diff --git a/libc-test/src/functionalext/fortify/fcntl_ext.c b/libc-test/src/functionalext/fortify/fcntl_ext.c index cd1edb97626dac33c131065d3745547027764464..3a7320aa876aea10e4632092043b50e6e145ba6a 100644 --- a/libc-test/src/functionalext/fortify/fcntl_ext.c +++ b/libc-test/src/functionalext/fortify/fcntl_ext.c @@ -22,6 +22,14 @@ #define FILE_MODE_ALL (0777) +#ifdef open64 +#undef open64 +#endif + +#ifdef openat64 +#undef openat64 +#endif + /** * @tc.name : open_0010 * @tc.desc : test open normal condition @@ -104,6 +112,19 @@ static void open_0030(void) return; } +/** + * @tc.name : open_0040 + * @tc.desc : test open only O_RDWR + * @tc.level : Level 1 + */ +static void open_0040(void) +{ + int fd = open("/proc/version", O_RDWR); + TEST(fd != -1); + close(fd); + + return; +} /** * @tc.name : openat_0010 @@ -187,6 +208,20 @@ static void openat_0030(void) return; } +/** + * @tc.name : openat_0040 + * @tc.desc : test openat only O_RDWR + * @tc.level : Level 1 + */ +static void openat_0040(void) +{ + int fd = openat(AT_FDCWD, "/proc/version", O_RDWR); + TEST(fd != -1); + close(fd); + + return; +} + #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) /** * @tc.name : open64_0010 @@ -270,6 +305,19 @@ static void open64_0030(void) return; } +/** + * @tc.name : open64_0040 + * @tc.desc : test open64 only O_RDWR + * @tc.level : Level 1 + */ +static void open64_0040(void) +{ + int fd = open64("/proc/version", O_RDWR); + TEST(fd != -1); + close(fd); + + return; +} /** * @tc.name : openat64_0010 @@ -352,22 +400,40 @@ static void openat64_0030(void) return; } + +/** + * @tc.name : openat64_0040 + * @tc.desc : test openat64 only O_RDWR + * @tc.level : Level 1 + */ +static void openat64_0040(void) +{ + int fd = openat64(AT_FDCWD, "/proc/version", O_RDWR); + TEST(fd != -1); + close(fd); + + return; +} #endif int main(int argc, char *argv[]) { open_0010(); open_0020(); open_0030(); + open_0040(); openat_0010(); openat_0020(); openat_0030(); + openat_0040(); #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) open64_0010(); open64_0020(); open64_0030(); + open64_0040(); openat64_0010(); openat64_0020(); openat64_0030(); + openat64_0040(); #endif return t_status; } \ No newline at end of file diff --git a/libc-test/src/functionalext/fortify/poll.c b/libc-test/src/functionalext/fortify/poll.c index 8ad626e985b399b39c25c91d4ce6838520ef9118..7bb289bcc7dfc1f50e864cc4867d6842dec26259 100644 --- a/libc-test/src/functionalext/fortify/poll.c +++ b/libc-test/src/functionalext/fortify/poll.c @@ -73,6 +73,20 @@ static void poll_0020(void) return; } +/** + * @tc.name : poll_0030 + * @tc.desc : test poll with two fds and param count is 1 + * @tc.level : Level 1 + */ +static void poll_0030(void) +{ + nfds_t fd_count = atoi("1"); + struct pollfd buf[2] = {{0, POLLIN, 0}, {1, POLLIN, 0}}; + TEST(poll(buf, fd_count, 1) == 1); + return; +} + + #ifdef _GNU_SOURCE /** * @tc.name : ppoll_0010 @@ -126,14 +140,31 @@ static void ppoll_0020(void) return; } + +/** + * @tc.name : ppoll_0030 + * @tc.desc : test ppoll with two fds and param count is 1 + * @tc.level : Level 1 + */ +static void ppoll_0030(void) +{ + nfds_t fd_count = atoi("1"); + struct pollfd buf[2] = {{0, POLLIN, 0}, {1, POLLIN, 0}}; + struct timespec ts = { .tv_nsec = PPOLL_TIMESPEC_NSEC }; + TEST(ppoll(buf, fd_count, &ts, NULL) == 1); + return; +} + #endif int main(int argc, char *argv[]) { poll_0010(); poll_0020(); + poll_0030(); #ifdef _GNU_SOURCE ppoll_0010(); ppoll_0020(); + ppoll_0030(); #endif return t_status; diff --git a/libc-test/src/functionalext/fortify/stdio.c b/libc-test/src/functionalext/fortify/stdio.c index 42bf153ab4480032a2e2d6b10037480d8eeb18fa..909a25fc894f881d6593aabcbe2eb387159b0cbb 100644 --- a/libc-test/src/functionalext/fortify/stdio.c +++ b/libc-test/src/functionalext/fortify/stdio.c @@ -56,6 +56,7 @@ static void stdio_dynamic_chk_001(void) EXPECT_STREQ(hello_world, buf); fclose(fp); + return; } /** @@ -89,7 +90,7 @@ static void stdio_dynamic_chk_002(void) for (size_t i = read_size; i < bufferSize; ++i) { EXPECT_EQ('\xff', buf[i]); } - + return; } /** @@ -117,7 +118,7 @@ static void stdio_dynamic_chk_003(void) break; case 0: fread(buf, 1, ct, fp); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -127,6 +128,7 @@ static void stdio_dynamic_chk_003(void) break; } fclose(fp); + return; } /** @@ -155,7 +157,7 @@ static void stdio_dynamic_chk_004(void) break; case 0: fwrite(buf, 1, ct, fp); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -165,6 +167,7 @@ static void stdio_dynamic_chk_004(void) break; } fclose(fp); + return; } /** @@ -184,6 +187,7 @@ static void stdio_dynamic_chk_005(void) EXPECT_TRUE(get != NULL); EXPECT_TRUE(strcmp(hello_world, get) == 0); fclose(fp); + return; } /** @@ -212,6 +216,7 @@ static void stdio_dynamic_chk_006(void) EXPECT_TRUE(get3 != NULL); EXPECT_TRUE(strcmp("hello boy!", get3) == 0); fclose(fp); + return; } /** @@ -240,7 +245,7 @@ static void stdio_dynamic_chk_007(void) break; case 0: fgets(buf, n, fp); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -250,6 +255,7 @@ static void stdio_dynamic_chk_007(void) break; } fclose(fp); + return; } /** @@ -262,6 +268,7 @@ static void stdio_dynamic_chk_008(void) char buf[] = "world"; sprintf(buf, "hello"); EXPECT_TRUE(strcmp(buf, "hello") == 0); + return; } /** @@ -277,6 +284,7 @@ static void stdio_dynamic_chk_009(void) char value[] = "hello : world!"; EXPECT_TRUE(strcmp(buf, value) == 0); + return; } @@ -303,7 +311,7 @@ static void stdio_dynamic_chk_010(void) break; case 0: sprintf(buf, "hello : %s", "world!"); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -312,6 +320,7 @@ static void stdio_dynamic_chk_010(void) kill(pid, SIGCONT); break; } + return; } /** @@ -339,7 +348,7 @@ static void stdio_dynamic_chk_011(void) break; case 0: // 10 > sizeof buf snprintf(buf, printSize, "hello : %s", "world!"); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -348,6 +357,7 @@ static void stdio_dynamic_chk_011(void) kill(pid, SIGCONT); break; } + return; } @@ -394,7 +404,7 @@ static void stdio_dynamic_chk_012(void) break; case 0: vsnprintf_test("hello : %s", "world!"); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -403,6 +413,7 @@ static void stdio_dynamic_chk_012(void) kill(pid, SIGCONT); break; } + return; } /** @@ -425,7 +436,7 @@ static void stdio_dynamic_chk_013(void) break; case 0: vsprintf_test("%s", "0123456789"); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -434,6 +445,7 @@ static void stdio_dynamic_chk_013(void) kill(pid, SIGCONT); break; } + return; } int main() diff --git a/libc-test/src/functionalext/fortify/string_ext.c b/libc-test/src/functionalext/fortify/string_ext.c index effe03d591c1ae418524f8fcff7f48fa9624ee78..cfade06fb55969685392ce4952fdd0404fa88c0f 100644 --- a/libc-test/src/functionalext/fortify/string_ext.c +++ b/libc-test/src/functionalext/fortify/string_ext.c @@ -852,6 +852,11 @@ static void test_memset_0020() return; } +/** + * @tc.name : test_strlen_0010 + * @tc.desc : Ability to test the strlen normal condition + * @tc.level : Level 0 + */ static void test_strlen_0010() { struct sigaction sigabrt = { @@ -881,6 +886,36 @@ static void test_strlen_0010() return; } +/** + * @tc.name : test_strlen_0020 + * @tc.desc : Ability to test the strlen with NULL + * @tc.level : Level 2 + */ +static void test_strlen_0020() +{ + struct sigaction sigabrt = { + .sa_handler = SignalHandler, + }; + sigaction(SIGABRT, &sigabrt, NULL); + + int status; + int pid = fork(); + switch (pid) { + case -1: + t_error("fork failed: %s\n", strerror(errno)); + break; + case 0: + strlen(NULL); + exit(0); + default: + waitpid(pid, &status, WUNTRACED); + TEST(WIFEXITED(status) == 0); + kill(pid, SIGCONT); + break; + } + return; +} + int main(int argc, char *argv[]) { test_strcat_0010(); test_strcat_0020(); @@ -907,7 +942,8 @@ int main(int argc, char *argv[]) { test_memcpy_0010(); test_memcpy_0020(); test_strlen_0010(); - + test_strlen_0020(); + #ifdef _GNU_SOURCE test_mempcpy_0010(); test_mempcpy_0020(); diff --git a/libc-test/src/functionalext/fortify/unistd.c b/libc-test/src/functionalext/fortify/unistd.c index 8476a341a0f22276b893eaa8344442cbbe6b2a5d..69544286ff15166482abeac7a7053bea13efb60b 100644 --- a/libc-test/src/functionalext/fortify/unistd.c +++ b/libc-test/src/functionalext/fortify/unistd.c @@ -46,7 +46,6 @@ static void unistd_dynamic_chk_001(void) case 0: getcwd(buf, n); exit(0); - break; default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -55,6 +54,7 @@ static void unistd_dynamic_chk_001(void) kill(pid, SIGCONT); break; } + return; } /** @@ -84,7 +84,7 @@ static void unistd_dynamic_chk_002(void) break; case 0: pread(fd, buf, n, 0); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -94,6 +94,7 @@ static void unistd_dynamic_chk_002(void) break; } close(fd); + return; } /** @@ -123,7 +124,7 @@ static void unistd_dynamic_chk_003(void) break; case 0: pread(fd, buf, n, 0); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -133,6 +134,7 @@ static void unistd_dynamic_chk_003(void) break; } close(fd); + return; } @@ -163,7 +165,7 @@ static void unistd_dynamic_chk_004(void) break; case 0: pread64(fd, buf, n, 0); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -173,6 +175,7 @@ static void unistd_dynamic_chk_004(void) break; } close(fd); + return; } /** @@ -202,7 +205,7 @@ static void unistd_dynamic_chk_005(void) break; case 0: pread64(fd, buf, n, 0); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -212,6 +215,7 @@ static void unistd_dynamic_chk_005(void) break; } close(fd); + return; } /** @@ -240,7 +244,7 @@ static void unistd_dynamic_chk_006(void) break; case 0: pwrite64(fd, buf, n, 0); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -250,6 +254,7 @@ static void unistd_dynamic_chk_006(void) break; } close(fd); + return; } /** @@ -278,7 +283,7 @@ static void unistd_dynamic_chk_007(void) break; case 0: pwrite64(fd, buf, n, 0); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -288,6 +293,7 @@ static void unistd_dynamic_chk_007(void) break; } close(fd); + return; } @@ -318,7 +324,7 @@ static void unistd_dynamic_chk_008(void) break; case 0: pwrite(fd, buf, n, 0); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -328,6 +334,7 @@ static void unistd_dynamic_chk_008(void) break; } close(fd); + return; } /** @@ -357,7 +364,7 @@ static void unistd_dynamic_chk_009(void) break; case 0: pwrite(fd, buf, n, 0); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -367,6 +374,7 @@ static void unistd_dynamic_chk_009(void) break; } close(fd); + return; } /** @@ -394,7 +402,7 @@ static void unistd_dynamic_chk_010(void) break; case 0: read(fd, buf, n); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -404,6 +412,7 @@ static void unistd_dynamic_chk_010(void) break; } close(fd); + return; } /** @@ -431,7 +440,7 @@ static void unistd_dynamic_chk_011(void) break; case 0: read(fd, buf, n); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -441,6 +450,7 @@ static void unistd_dynamic_chk_011(void) break; } close(fd); + return; } /** @@ -470,7 +480,7 @@ static void unistd_dynamic_chk_012(void) break; case 0: write(fd, buf, n); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -480,6 +490,7 @@ static void unistd_dynamic_chk_012(void) break; } close(fd); + return; } /** @@ -509,7 +520,7 @@ static void unistd_dynamic_chk_013(void) break; case 0: write(fd, buf, n); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -519,6 +530,7 @@ static void unistd_dynamic_chk_013(void) break; } close(fd); + return; } /** @@ -546,7 +558,7 @@ static void unistd_dynamic_chk_014(void) break; case 0: readlink(path, buf, n); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -555,6 +567,7 @@ static void unistd_dynamic_chk_014(void) kill(pid, SIGCONT); break; } + return; } /** @@ -582,7 +595,7 @@ static void unistd_dynamic_chk_015(void) break; case 0: readlink(path, buf, n); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -591,6 +604,7 @@ static void unistd_dynamic_chk_015(void) kill(pid, SIGCONT); break; } + return; } /** * @tc.name : readlinkat @@ -618,7 +632,7 @@ static void unistd_dynamic_chk_016(void) break; case 0: readlinkat(AT_FDCWD, path, buf, n); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -627,6 +641,7 @@ static void unistd_dynamic_chk_016(void) kill(pid, SIGCONT); break; } + return; } /** @@ -655,7 +670,7 @@ static void unistd_dynamic_chk_017(void) break; case 0: readlinkat(AT_FDCWD, path, buf, n); - break; + exit(0); default: waitpid(pid, &status, WUNTRACED); TEST(WIFEXITED(status) == 0); @@ -664,6 +679,7 @@ static void unistd_dynamic_chk_017(void) kill(pid, SIGCONT); break; } + return; } int main(int argc, char *argv[]) { diff --git a/libc-test/src/functionalext/info/fatal_message.c b/libc-test/src/functionalext/info/fatal_message.c index 38e8c00f74c3bfbc2e2692c88a5b8124cba03b1f..c4b88ec8290c4c32d91554c1dc4cd38b32902226 100644 --- a/libc-test/src/functionalext/info/fatal_message.c +++ b/libc-test/src/functionalext/info/fatal_message.c @@ -222,12 +222,39 @@ static void fatal_message_0050(void) pthread_join(fatalMessageThread2, NULL); } +/** + * @tc.name : set_fatal_message + * @tc.desc : Test the function of null message. + * @tc.level : Level 0 + */ +static void fatal_message_0060(void) +{ + const char* msg = NULL; + fatal_msg_t *fatal_message = NULL; + + int pidParent = 0; + int pidChild = 0; + + pid_t fpid; + fpid = fork(); + if (fpid < 0) { + t_printf("error in fork!"); + } else if (fpid == 0) { + pidChild = getpid(); + set_fatal_message(msg); + fatal_message = get_fatal_message(); + EXPECT_TRUE(fatal_message->msg == msg); + exit(pidChild); + } +} + TEST_FUN G_Fun_Array[] = { fatal_message_0010, fatal_message_0020, fatal_message_0030, fatal_message_0040, fatal_message_0050, + fatal_message_0060, }; int main(void) diff --git a/libc-test/src/functionalext/legacy/BUILD.gn b/libc-test/src/functionalext/legacy/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..42e747980c8e1f774889adfa7ceddf85ff6ead70 --- /dev/null +++ b/libc-test/src/functionalext/legacy/BUILD.gn @@ -0,0 +1,30 @@ +# 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. + +import("//build/test.gni") +import("../../../test_template.gni") +import("test_src_functionalext_legacy.gni") + +foreach(s, functionalext_legacy_test) { + test_unittest(s) { + target_dir = "functionalext/legacy" + } +} + +group("functionalext_legacy_test") { + testonly = true + deps = [] + foreach(s, functionalext_legacy_test) { + deps += [ ":${s}" ] + } +} diff --git a/libc-test/src/functionalext/legacy/test_src_functionalext_legacy.gni b/libc-test/src/functionalext/legacy/test_src_functionalext_legacy.gni new file mode 100644 index 0000000000000000000000000000000000000000..5d69f322812250f0846bae90d652d166bf4d4fa1 --- /dev/null +++ b/libc-test/src/functionalext/legacy/test_src_functionalext_legacy.gni @@ -0,0 +1,14 @@ +# 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. + +functionalext_legacy_test = [ "ulimit" ] diff --git a/libc-test/src/functionalext/legacy/ulimit.c b/libc-test/src/functionalext/legacy/ulimit.c new file mode 100644 index 0000000000000000000000000000000000000000..d113d5009462c052172d20c11f9cd9b6214d114f --- /dev/null +++ b/libc-test/src/functionalext/legacy/ulimit.c @@ -0,0 +1,61 @@ +/** + * 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 "functionalext.h" + +#define MAX_FILE_SIZE 4096 + +/** + * @tc.name : ulimit_0100 + * @tc.desc : Test ulimit() with UL_SETFSIZE + * @tc.level : Level 0 + */ +static void ulimit_0100(void) +{ + int cmd = UL_SETFSIZE; + long result = ulimit(cmd, MAX_FILE_SIZE); + if (result < 0) { + EXPECT_PTRNE("ulimit_0100", result, 0); + return; + } + + EXPECT_LONGEQ("ulimit_0100", MAX_FILE_SIZE, result); +} + +/** + * @tc.name : ulimit_0200 + * @tc.desc : Test ulimit() with UL_GETFSIZE + * @tc.level : Level 1 + */ +static void ulimit_0200(void) +{ + int cmd = UL_GETFSIZE; + long result = ulimit(cmd); + if (result < 0) { + EXPECT_PTRNE("ulimit_0200", result, 0); + return; + } + + EXPECT_LONGEQ("ulimit_0200", MAX_FILE_SIZE, result); +} + +int main(void) +{ + ulimit_0100(); + ulimit_0200(); + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/locale/dcngettext.c b/libc-test/src/functionalext/locale/dcngettext.c new file mode 100644 index 0000000000000000000000000000000000000000..cdea692e5d48d44d4a80681c5fc5090a4c99fde0 --- /dev/null +++ b/libc-test/src/functionalext/locale/dcngettext.c @@ -0,0 +1,50 @@ +/** + * 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 "functionalext.h" + +#define PACKAGE "test_dcngettext" + +/** + * @tc.name : dcngettext_0100 + * @tc.desc : Get string from dcngettext() with mo file + * This case need push the test_dcngettext.mo file of the current path to remote board + * The remote path should be "${test_src_dir}/zh_CN/LC_MESSAGES/test_dcngettext.mo" + * @tc.level : Level 0 + */ +static void dcngettext_0100(void) +{ + char *msgid = "hello"; + setlocale(LC_ALL, "zh_CN"); + bindtextdomain(PACKAGE, "."); + textdomain(PACKAGE); + char *result = dcngettext(PACKAGE, msgid, 0, 1, LC_MESSAGES); + if (!result) { + EXPECT_PTRNE("dcngettext_0100", result, NULL); + return; + } + + EXPECT_TRUE("dcngettext_0100", strlen(result) > 0); + EXPECT_STREQ("dcngettext_0100", "nihao", result); +} + +int main(void) +{ + dcngettext_0100(); + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/locale/langinfo.c b/libc-test/src/functionalext/locale/langinfo.c index a6d58ea72f9c3f8b734a6201f0784951d68fa3c0..23113b99959fe291c681b346ce65331b1c34ab07 100644 --- a/libc-test/src/functionalext/locale/langinfo.c +++ b/libc-test/src/functionalext/locale/langinfo.c @@ -127,6 +127,23 @@ void nl_langinfo_0500() EXPECT_STREQ("nl_langinfo_0500", ptr, "UTF-8"); } +/** + * @tc.name : nl_langinfo_0600 + * @tc.desc : Assert whether the return value result is not "C.UTF-8" + * when the function nl_langinfo passes in the "65535" parameter + * @tc.level : Level 2 + */ +void nl_langinfo_0600() +{ + char *lo = setlocale(LC_ALL, ""); + if (!lo) { + EXPECT_PTRNE("nl_langinfo_0600", lo, NULL); + return; + } + char *ptr = nl_langinfo(RADIXCHAR - 1); + EXPECT_STREQ("nl_langinfo_0600", ptr, "C.UTF-8"); +} + int main(void) { langinfo_0100(); @@ -134,5 +151,6 @@ int main(void) nl_langinfo_0300(); nl_langinfo_0400(); nl_langinfo_0500(); + nl_langinfo_0600(); return t_status; } \ No newline at end of file diff --git a/libc-test/src/functionalext/locale/test_dcngettext.mo b/libc-test/src/functionalext/locale/test_dcngettext.mo new file mode 100644 index 0000000000000000000000000000000000000000..d5dc0cf1caeac65361db47ac78d4f10afe427088 Binary files /dev/null and b/libc-test/src/functionalext/locale/test_dcngettext.mo differ diff --git a/libc-test/src/functionalext/locale/test_src_functionalext_locale.gni b/libc-test/src/functionalext/locale/test_src_functionalext_locale.gni index fea42b57e23628252082a90b49b3c1b446ecfd92..f4844e3911045c8ba2497d7c39936679f632344a 100644 --- a/libc-test/src/functionalext/locale/test_src_functionalext_locale.gni +++ b/libc-test/src/functionalext/locale/test_src_functionalext_locale.gni @@ -12,6 +12,7 @@ # limitations under the License. functionalext_locale_test = [ + "dcngettext", "duplocale", "langinfo", "localeconv", diff --git a/libc-test/src/functionalext/sched/BUILD.gn b/libc-test/src/functionalext/sched/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..5289948c769f08a35bbdb99ac59661ca55abb1c4 --- /dev/null +++ b/libc-test/src/functionalext/sched/BUILD.gn @@ -0,0 +1,29 @@ +# 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. + +import("../../../test_template.gni") +import("test_src_functionalext_sched.gni") + +foreach(s, functionalext_sched_list) { + test_unittest(s) { + target_dir = "functionalext/sched" + } +} + +group("functionalext_sched_test") { + testonly = true + deps = [] + foreach(s, functionalext_sched_list) { + deps += [ ":${s}" ] + } +} diff --git a/libc-test/src/functionalext/sched/sched_setparam.c b/libc-test/src/functionalext/sched/sched_setparam.c new file mode 100644 index 0000000000000000000000000000000000000000..7afe1dc5619babc9d102858de5304f9bc09dc004 --- /dev/null +++ b/libc-test/src/functionalext/sched/sched_setparam.c @@ -0,0 +1,49 @@ +/* + * 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 +#include "functionalext.h" +#include "test.h" + +typedef void (*TEST_FUN)(void); + +/** + * @tc.name : sched_setparam + * @tc.desc : Test the function of sched_setparam. + * @tc.level : Level 2 + */ +static void sched_setparam_0010(void) +{ + pid_t pid; + pid = getpid(); + EXPECT_EQ("sched_setparam_0010", sched_setparam(pid, NULL), -1); +} + +TEST_FUN G_Fun_Array[] = { + sched_setparam_0010, +}; + +int main(void) +{ + int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); + for (int pos = 0; pos < num; ++pos) { + G_Fun_Array[pos](); + } + + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/sched/test_src_functionalext_sched.gni b/libc-test/src/functionalext/sched/test_src_functionalext_sched.gni new file mode 100644 index 0000000000000000000000000000000000000000..e4d943c1d60cb561f4f161c32741c5a00bc1e9f4 --- /dev/null +++ b/libc-test/src/functionalext/sched/test_src_functionalext_sched.gni @@ -0,0 +1,16 @@ +# +# 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. +# + +functionalext_sched_list = [ "sched_setparam" ] diff --git a/libc-test/src/functionalext/supplement/linux/clone.c b/libc-test/src/functionalext/supplement/linux/clone.c index ef8323524aaa6ddcfcec7376b9e485257ec6bf08..0e5a762c98edc3510518f53ca02c21b4f842af5d 100644 --- a/libc-test/src/functionalext/supplement/linux/clone.c +++ b/libc-test/src/functionalext/supplement/linux/clone.c @@ -40,8 +40,23 @@ void clone_0100(void) EXPECT_NE("clone_0100", cpid, -1); } +/** + * @tc.name : clone_0200 + * @tc.desc : Parameter flags is 0. + * @tc.level : Level 2 + */ +void clone_0200(void) +{ + void *stack = malloc(STACK_SIZE); + int cpid = -1; + cpid = clone((int (*)(void *))test, (char *)stack + STACK_SIZE, 0, NULL); + sleep(1); + EXPECT_NE("clone_0100", cpid, -1); +} + int main(int argc, char *argv[]) { clone_0100(); + clone_0200(); return t_status; } \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/passwd/getspnam_r.c b/libc-test/src/functionalext/supplement/passwd/getspnam_r.c new file mode 100644 index 0000000000000000000000000000000000000000..a36974323112782fe1479bb9bd63b844c4e0d368 --- /dev/null +++ b/libc-test/src/functionalext/supplement/passwd/getspnam_r.c @@ -0,0 +1,46 @@ +/* + * 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 "test.h" + +/** + * @tc.name : getspnam_r_0100 + * @tc.desc : Get user database files. Test precondtions: /etc/shadow. + * @tc.level : Level 0 + */ +void getspnam_r_0100(void) +{ + errno = 0; + char buf[512]; + gid_t gid = 0; + + struct spwd *spwd; + struct spwd spwd_storage; + const char *spwd_name = "root"; + + int result = getspnam_r(spwd_name, &spwd_storage, buf, sizeof(buf), &spwd); + if (result != 0) { + t_error("%s getgrnam_r failed\n", __func__); + } +} + +int main(int argc, char *argv[]) +{ + getspnam_r_0100(); + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/passwd/test_src_functionalext_supplement_passwd.gni b/libc-test/src/functionalext/supplement/passwd/test_src_functionalext_supplement_passwd.gni index 44acbab94a562e4df3f3ddc522ceb0b26e317910..44f7adcffc8095df79024e012c1466ed5d99d282 100644 --- a/libc-test/src/functionalext/supplement/passwd/test_src_functionalext_supplement_passwd.gni +++ b/libc-test/src/functionalext/supplement/passwd/test_src_functionalext_supplement_passwd.gni @@ -22,4 +22,5 @@ functionalext_supplement_passwd_test = [ "getgrouplist", "getgrgid_r", "getgrnam_r", + "getspnam_r", ] diff --git a/libc-test/src/functionalext/test_src_functionalext.gni b/libc-test/src/functionalext/test_src_functionalext.gni index e6080d8514330cf46e508e103d853435904d45d1..421c847dc2485a9f39c72d11408b2e85c4604f07 100644 --- a/libc-test/src/functionalext/test_src_functionalext.gni +++ b/libc-test/src/functionalext/test_src_functionalext.gni @@ -25,4 +25,7 @@ functionalext_list = [ "supplement:functionalext_supplement_test", "fortify:functionalext_fortify_test", "symver:functionalext_symver_test", + "sched:functionalext_sched_test", + "unittest:functionalext_unittest_test", + "legacy:functionalext_legacy_test", ] diff --git a/libc-test/src/functionalext/thread/pthread_cond_timedwait.c b/libc-test/src/functionalext/thread/pthread_cond_timedwait.c index 855526a6f500379cead7fc40da3b218cd3e8868e..a6f68c26a413676fe4ac3a95a65bb3089c112822 100644 --- a/libc-test/src/functionalext/thread/pthread_cond_timedwait.c +++ b/libc-test/src/functionalext/thread/pthread_cond_timedwait.c @@ -147,6 +147,19 @@ void pthread_cond_timedwait_monotonic_np_0010(void) EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); } +/** + * @tc.number : pthread_cond_timedwait_monotonic_np_0020 + * @tc.desc : Test whether the pthread_cond_timedwait_monotonic_np is invalid + * @tc.level : Level 2 + */ +void pthread_cond_timedwait_monotonic_np_0020(void) +{ + pthread_cond_t *cond = (pthread_cond_t *)NULL; + pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + struct timespec ts = {0}; + EXPECT_EQ(pthread_cond_timedwait_monotonic_np(cond, &mutex, &ts), EINVAL); +} + /** * @tc.number : pthread_cond_timeout_np_0010 * @tc.desc : Test whether the pthread_cond_timeout_np is normal @@ -707,6 +720,7 @@ TEST_FUN G_Fun_Array[] = { pthread_cond_timedwait_0020, pthread_cond_timedwait_time64_0010, pthread_cond_timedwait_monotonic_np_0010, + pthread_cond_timedwait_monotonic_np_0020, pthread_cond_timeout_np_0010, pthread_cond_clockwait_0010, pthread_cond_timedwait_Time_0010, diff --git a/libc-test/src/functionalext/thread/pthread_mutex_ext.c b/libc-test/src/functionalext/thread/pthread_mutex_ext.c index ac11d990f34145ef1575899564fabdb0352fbe0f..44ce2ad77acd2769984e6b22b2ee9eb04a5018a9 100644 --- a/libc-test/src/functionalext/thread/pthread_mutex_ext.c +++ b/libc-test/src/functionalext/thread/pthread_mutex_ext.c @@ -301,6 +301,20 @@ static void pthread_mutex_timedlock_monotonic_np_0020(void) /********************************************* Test case dividing line ***********************************************/ +/** + * @tc.name : pthread_mutex_timedlock_monotonic_np_0030 + * @tc.desc : test pthread_mutex_timedlock_monotonic_np_0030 with invalid args + * @tc.level : Level 2 + */ +static void pthread_mutex_timedlock_monotonic_np_0030(void) +{ + pthread_mutex_t *mtx = (pthread_mutex_t *)NULL; + struct timespec ts = {0}; + TEST(pthread_mutex_timedlock_monotonic_np(mtx, &ts) == EINVAL); +} + +/********************************************* Test case dividing line ***********************************************/ + static void *PthreadLockTimeoutNPOut(void *arg) { pthread_mutex_t *mtx = (pthread_mutex_t*)arg; @@ -379,6 +393,7 @@ int main(void) pthread_mutex_clocklock_0060(); pthread_mutex_timedlock_monotonic_np_0010(); pthread_mutex_timedlock_monotonic_np_0020(); + pthread_mutex_timedlock_monotonic_np_0030(); pthread_mutex_lock_timeout_np_0010(); pthread_mutex_lock_timeout_np_0020(); diff --git a/libc-test/src/functionalext/thread/pthread_rwlock_rdlock.c b/libc-test/src/functionalext/thread/pthread_rwlock_rdlock.c index b7990e438f17e2b2e73bf9af5fde8fe53295f25a..01823c6bb7117be58cfd87bff77eb9dda9f97b63 100644 --- a/libc-test/src/functionalext/thread/pthread_rwlock_rdlock.c +++ b/libc-test/src/functionalext/thread/pthread_rwlock_rdlock.c @@ -269,6 +269,17 @@ static void pthread_rwlock_timedrdlock_monotonic_np_0020(void) TEST(pthread_rwlock_destroy(&g_rwlock6) == 0); } +/** + * @tc.name : pthread_rwlock_timedrdlock_monotonic_np_0030 + * @tc.desc : test pthread_rwlock_timedrdlock_monotonic_np with invalid rwlock + * @tc.level : Level 2 + */ +static void pthread_rwlock_timedrdlock_monotonic_np_0030(void) +{ + struct timespec ts = {0}; + TEST(pthread_rwlock_timedrdlock_monotonic_np((pthread_rwlock_t *)NULL, &ts) == EINVAL); +} + int main(void) { pthread_rwlock_clockrdlock_0010(); @@ -277,6 +288,7 @@ int main(void) pthread_rwlock_clockrdlock_0040(); pthread_rwlock_timedrdlock_monotonic_np_0010(); pthread_rwlock_timedrdlock_monotonic_np_0020(); + pthread_rwlock_timedrdlock_monotonic_np_0030(); return t_status; } \ No newline at end of file diff --git a/libc-test/src/functionalext/thread/pthread_rwlock_wrlock.c b/libc-test/src/functionalext/thread/pthread_rwlock_wrlock.c index 9572de8d1de2d1a9c00540405621d13a5a0d657d..f58fe147b44295213e1b4352ec894a62ce85ddc0 100644 --- a/libc-test/src/functionalext/thread/pthread_rwlock_wrlock.c +++ b/libc-test/src/functionalext/thread/pthread_rwlock_wrlock.c @@ -328,6 +328,17 @@ void pthread_rwlock_timedwrlock_0100(void) EXPECT_EQ(pthread_rwlock_destroy(&w_rwlock4), 0); } +/** + * @tc.number : pthread_rwlock_timedwrlock_0110 + * @tc.desc : Test the case of pthread_rwlock_timedwrlock_monotonic_np with invalid rwlock + * @tc.level : Level 2 + */ +void pthread_rwlock_timedwrlock_0110(void) +{ + struct timespec ts = {0}; + EXPECT_EQ(pthread_rwlock_timedwrlock_monotonic_np((pthread_rwlock_t *)NULL, &ts), EINVAL); +} + TEST_FUN G_Fun_Array[] = { pthread_rwlock_timedwrlock_0010, pthread_rwlock_timedwrlock_0020, @@ -339,6 +350,7 @@ TEST_FUN G_Fun_Array[] = { pthread_rwlock_timedwrlock_0080, pthread_rwlock_timedwrlock_0090, pthread_rwlock_timedwrlock_0100, + pthread_rwlock_timedwrlock_0110, }; int main(void) diff --git a/libc-test/src/functionalext/time/gmtime_r.c b/libc-test/src/functionalext/time/gmtime_r.c index 243a56fc53c759bd81c662412dc4a9108eb62841..d640c58cb8b59757942c4c52da1d0a5e36d6ffe3 100644 --- a/libc-test/src/functionalext/time/gmtime_r.c +++ b/libc-test/src/functionalext/time/gmtime_r.c @@ -15,6 +15,7 @@ #include #include +#include #include "gmtime_data.h" #include "functionalext.h" @@ -50,8 +51,22 @@ void gmtime_r_0100(void) } } +/** + * @tc.name : gmtime_r_0200 + * @tc.desc : test gmtime_r() with invalid input paramter + * @tc.level : Level 2 + */ +void gmtime_r_0200(void) +{ + time_t invalid_time = INT_MAX * 31622400LL + 1; + struct tm res = {0}; + struct tm *gmtm = gmtime_r(&invalid_time, &res); + EXPECT_TRUE("gmtime_r_0200", NULL == (void *)gmtm); +} + int main(void) { gmtime_r_0100(); + gmtime_r_0200(); return t_status; } \ No newline at end of file diff --git a/libc-test/src/functionalext/time/strptime_ext.c b/libc-test/src/functionalext/time/strptime_ext.c index 0eef8481fae294a285b9ca7466989c60d82d6ff9..2e634d32b0e36fbb796d48917ce33934b3920cd5 100644 --- a/libc-test/src/functionalext/time/strptime_ext.c +++ b/libc-test/src/functionalext/time/strptime_ext.c @@ -229,6 +229,40 @@ void strptime_0900(void) EXPECT_STREQ("strptime_0900", "2021-9-30", buffResult); } +/** + * @tc.name : strptime_1000 + * @tc.desc : according to different time zones, convert a string to a time + * type according to a specific time format + * @tc.level : Level 1 + */ +void strptime_1000(void) +{ + const char *buffer = "2021-01-23"; + struct tm tmTime = {0}; + char *result = strptime(buffer, "%G", &tmTime); + char buffResult[gBufferSize]; + int cnt = sprintf(buffResult, "%s", result); + EXPECT_TRUE("strptime_1000", cnt > 0); + EXPECT_STREQ("strptime_1000", "-01-23", buffResult); +} + +/** + * @tc.name : strptime_1100 + * @tc.desc : according to different time zones, convert a string to a time + * type according to a specific time format + * @tc.level : Level 1 + */ +void strptime_1100(void) +{ + const char *buffer = "23"; + struct tm tmTime = {0}; + strptime(buffer, "%j", &tmTime); + char buffResult[gBufferSize]; + int cnt = sprintf(buffResult, "%d", tmTime.tm_yday); + EXPECT_TRUE("strptime_1100", cnt > 0); + EXPECT_STREQ("strptime_1100", "22", buffResult); +} + int main(void) { strptime_0100(); @@ -240,5 +274,7 @@ int main(void) strptime_0700(); strptime_0800(); strptime_0900(); + strptime_1000(); + strptime_1100(); return t_status; } \ No newline at end of file diff --git a/libc-test/src/functionalext/unittest/BUILD.gn b/libc-test/src/functionalext/unittest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d5ecd1d2cf09686bec1091ec6426431826ffe7eb --- /dev/null +++ b/libc-test/src/functionalext/unittest/BUILD.gn @@ -0,0 +1,186 @@ +# 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. + +import("../../../test_template.gni") + +ohos_executable("unittest_ldso_ns_config") { + subsystem_name = "musl" + part_name = "libc-test" + + sources = [ "unit_test_ldso_ns_config.c" ] + + include_dirs = [ + "${musl_src_base}/ldso", + "//${test_dir}/src/common", + "//${test_dir}/src/functionalext/common", + "${musl_src_base}/src/include", + "${musl_src_base}/include", + "${musl_src_base}/src/internal", + ] + + configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] + + deps = [ "//third_party/musl:create_porting_src" ] + + libs = [ "${musl_lib_dir}/libc.a" ] +} + +ohos_executable("unittest_ldso_namesapce") { + subsystem_name = "musl" + part_name = "libc-test" + + sources = [ + "${musl_src_base}/ldso/namespace.c", + "unit_test_ldso_namespace.c", + "unit_test_mock_molloc.c", + "unit_test_mock_strops.c", + ] + + include_dirs = [ + "${musl_src_base}/ldso", + "//${test_dir}/src/common", + "//${test_dir}/src/functionalext/common", + "${musl_src_base}/src/include", + "${musl_src_base}/include", + "${musl_src_base}/src/internal", + ] + + configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] + + deps = [ "//third_party/musl:create_porting_src" ] +} + +ohos_executable("unittest_ldso_ld_log") { + subsystem_name = "musl" + part_name = "libc-test" + + sources = [ + "${musl_src_base}/ldso/ld_log.c", + "unit_test_ldso_ld_log.c", + "unit_test_mock_hilog_adapter.c", + ] + + include_dirs = [ + "${musl_src_base}/ldso", + "//${test_dir}/src/common", + "//${test_dir}/src/functionalext/common", + "${musl_src_base}/src/include", + "${musl_src_base}/include", + "${musl_src_base}/src/internal", + "//${test_dir}/src/functionalext/unittest", + ] + + configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] + + deps = [ "//third_party/musl:create_porting_src" ] + defines = [ "OHOS_ENABLE_PARAMETER" ] +} + +ohos_executable("unittest_ldso_dynlink") { + subsystem_name = "musl" + part_name = "libc-test" + + sources = [ "unit_test_ldso_dynlink.c" ] + + include_dirs = [ + "${musl_src_base}/ldso", + "//${test_dir}/src/common", + "//${test_dir}/src/functionalext/common", + "${musl_src_base}/src/include", + "${musl_src_base}/include", + "${musl_src_base}/src/internal", + ] + + configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] + + deps = [ "//third_party/musl:create_porting_src" ] + + libs = [ "${musl_lib_dir}/libc.a" ] +} + +ohos_executable("unittest_hilog_adapter") { + subsystem_name = "musl" + part_name = "libc-test" + + sources = [ "unit_test_hilog_adapter.c" ] + + include_dirs = [ + "//${test_dir}/src/functionalext/common", + "//${test_dir}/src/common", + "${musl_src_base}/src/internal", + ] + + configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] + + deps = [ "//third_party/musl:create_porting_src" ] + + libs = [ "${musl_lib_dir}/libc.a" ] +} + +ohos_executable("unittest_hilog_vsnprint") { + subsystem_name = "musl" + part_name = "libc-test" + + sources = [ "unit_test_hilog_vsnprint_f_p.c" ] + + include_dirs = [ + "//${test_dir}/src/functionalext/common", + "//${test_dir}/src/common", + "${musl_src_base}/src/hilog", + ] + + configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] + + deps = [ "//third_party/musl:create_porting_src" ] + + libs = [ "${musl_lib_dir}/libc.a" ] +} + +ohos_executable("unittest_hook_preinit") { + subsystem_name = "musl" + part_name = "libc-test" + + sources = [ "unit_test_hook_preinit.c" ] + + include_dirs = [ + "${musl_src_base}/src/hook", + "//${test_dir}/src/common", + "//${test_dir}/src/functionalext/common", + "${musl_src_base}/src/include", + "${musl_src_base}/include", + "${musl_src_base}/src/internal", + ] + + configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] + + deps = [ "//third_party/musl:create_porting_src" ] + + if(musl_unit_test_flag){ + defines = [ "UNIT_TEST_STATIC" ] + } + + libs = [ "${musl_lib_dir}/libc.a" ] +} + +group("functionalext_unittest_test") { + testonly = true + deps = [ + ":unittest_hilog_adapter", + ":unittest_hilog_vsnprint", + ":unittest_hook_preinit", + ":unittest_ldso_dynlink", + ":unittest_ldso_ld_log", + ":unittest_ldso_namesapce", + ":unittest_ldso_ns_config", + ] +} diff --git a/libc-test/src/functionalext/unittest/sys_param.h b/libc-test/src/functionalext/unittest/sys_param.h new file mode 100644 index 0000000000000000000000000000000000000000..ed72e98ace8072c6b560ed057fe917e8a94691be --- /dev/null +++ b/libc-test/src/functionalext/unittest/sys_param.h @@ -0,0 +1,17 @@ +/** + * 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 + +int SystemReadParam(char *name, char *buffer, uint32_t *length); \ No newline at end of file diff --git a/libc-test/src/functionalext/unittest/unit_test_hilog_adapter.c b/libc-test/src/functionalext/unittest/unit_test_hilog_adapter.c new file mode 100644 index 0000000000000000000000000000000000000000..d850469a262acb275e3af65efec10a28810513eb --- /dev/null +++ b/libc-test/src/functionalext/unittest/unit_test_hilog_adapter.c @@ -0,0 +1,44 @@ +/* + * 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 "functionalext.h" +#include "test.h" + +#define MUSL_LOG_TYPE LOG_CORE +#define MUSL_LOG_DOMAIN 0xD003F00 +#define MUSL_LOG_TAG "MUSL" +#define LOG_ERROR 6 + +#define MUSL_LOGE(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_ERROR, MUSL_LOG_DOMAIN, MUSL_LOG_TAG, __VA_ARGS__)) + +/** + * @tc.name : reboot_0010 + * @tc.desc : test HiLogAdapterPrint after musl_log_reset + * @tc.level : Level 2 + */ +static void HiLogAdapterPrint_0010(void) +{ + musl_log_reset(); + int ret = HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_ERROR, MUSL_LOG_DOMAIN, MUSL_LOG_TAG, "a"); + EXPECT_EQ("HiLogAdapterPrint_0010", ret, -1); +} + +int main(void) +{ + HiLogAdapterPrint_0010(); + + return t_status; +} diff --git a/libc-test/src/functionalext/unittest/unit_test_hilog_vsnprint_f_p.c b/libc-test/src/functionalext/unittest/unit_test_hilog_vsnprint_f_p.c new file mode 100644 index 0000000000000000000000000000000000000000..a7cfe54608bd2897a27beb4312ed8e3f1ce8dd23 --- /dev/null +++ b/libc-test/src/functionalext/unittest/unit_test_hilog_vsnprint_f_p.c @@ -0,0 +1,536 @@ +/* + * 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 "test.h" +#include +#include "functionalext.h" + +#define MAX_LOG_LEN 1024 /* maximum length of a log, include '\0' */ +#define MAX_TAG_LEN 32 /* log tag size, include '\0' */ +#define SECUREC_STRING_MAX_LEN 0x7fffffffUL + +typedef void (*TEST_FUN)(void); + +static int vsprintf_test(char *strDest, size_t destMax, size_t count, int priv, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vsnprintfp_s(strDest, destMax, count, priv, fmt, ap); + va_end(ap); + return ret; +} + +/** + * @tc.name : vsnprintfp_s_0010 + * @tc.desc : test vsnprintf normal condition + * @tc.level : Level 0 + */ +static void vsnprintfp_s_0010(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "MUSL"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt); + EXPECT_TRUE("vsnprintfp_s_0010", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0010", buf, "MUSL"); +} + +/** + * @tc.name : vsnprintfp_s_0020 + * @tc.desc : test vsnprintf both param buf and fmt are NULL + * @tc.level : Level 2 + */ +static void vsnprintfp_s_0020(void) +{ + char *buf = NULL; + char *fmt = NULL; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt); + EXPECT_TRUE("vsnprintfp_s_0020", ret < 0); +} + +/** + * @tc.name : vsnprintfp_s_0030 + * @tc.desc : test vsnprintf param destMax is smaller than count + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0030(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "MUSL"; + int ret = vsprintf_test(buf, MAX_LOG_LEN - 1, MAX_LOG_LEN, true, fmt); + EXPECT_TRUE("vsnprintfp_s_0030", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0030", buf, "MUSL"); +} + +/** + * @tc.name : vsnprintfp_s_0040 + * @tc.desc : test vsnprintf param fmt with %s + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0040(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%s"; + int ret = vsprintf_test(buf, MAX_LOG_LEN - 1, MAX_LOG_LEN, false, fmt, "MUSL"); + EXPECT_TRUE("vsnprintfp_s_0040", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0040", buf, "MUSL"); +} + +/** + * @tc.name : vsnprintfp_s_0050 + * @tc.desc : test vsnprintf param destMax is 0 + * @tc.level : Level 2 + */ +static void vsnprintfp_s_0050(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "MUSL"; + int ret = vsprintf_test(buf, 0, MAX_LOG_LEN - 1, true, fmt); + EXPECT_TRUE("vsnprintfp_s_0050", ret < 0); + EXPECT_STREQ("vsnprintfp_s_0050", buf, ""); +} + +/** + * @tc.name : vsnprintfp_s_0060 + * @tc.desc : test vsnprintf param destMax is greater than SECUREC_STRING_MAX_LEN + * @tc.level : Level 2 + */ +static void vsnprintfp_s_0060(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "MUSL"; + int ret = vsprintf_test(buf, SECUREC_STRING_MAX_LEN + 1, MAX_LOG_LEN - 1, true, fmt); + EXPECT_TRUE("vsnprintfp_s_0060", ret < 0); + EXPECT_STREQ("vsnprintfp_s_0060", buf, ""); +} + +/** + * @tc.name : vsnprintfp_s_0070 + * @tc.desc : test vsnprintf param count is greater than SECUREC_STRING_MAX_LEN + * @tc.level : Level 2 + */ +static void vsnprintfp_s_0070(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "MUSL"; + int ret = vsprintf_test(buf, MAX_LOG_LEN - 1, SECUREC_STRING_MAX_LEN + 1, true, fmt); + EXPECT_TRUE("vsnprintfp_s_0070", ret < 0); + EXPECT_STREQ("vsnprintfp_s_0070", buf, ""); +} + +/** + * @tc.name : vsnprintfp_s_0080 + * @tc.desc : test vsnprintf param fmt is NULL + * @tc.level : Level 2 + */ +static void vsnprintfp_s_0080(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = NULL; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt); + EXPECT_TRUE("vsnprintfp_s_0080", ret < 0); +} + +/** + * @tc.name : vsnprintfp_s_0090 + * @tc.desc : test vsnprintf param count is less than buf's size + * @tc.level : Level 2 + */ +static void vsnprintfp_s_0090(void) +{ + char buf[2] = {0}; + char *fmt = "%s"; + int ret = vsprintf_test(buf, 1, 1, false, fmt, "test_test"); + EXPECT_TRUE("vsnprintfp_s_0090", ret < 0); +} + +/** + * @tc.name : vsnprintfp_s_0100 + * @tc.desc : test vsnprintf param fmt is "" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0100(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = ""; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt); + EXPECT_TRUE("vsnprintfp_s_0100", ret == 0); + EXPECT_STREQ("vsnprintfp_s_0100", buf, ""); +} + +/** + * @tc.name : vsnprintfp_s_0110 + * @tc.desc : test vsnprintf param fmt is "%02d" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0110(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%02d"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 2); + EXPECT_TRUE("vsnprintfp_s_0110", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0110", buf, "02"); +} + +/** + * @tc.name : vsnprintfp_s_0120 + * @tc.desc : test vsnprintf param fmt is "%p" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0120(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%p"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, &buf[0]); + EXPECT_TRUE("vsnprintfp_s_0120", ret > 0); + EXPECT_TRUE("vsnprintfp_s_0120", strlen(buf) > 0); +} + +/** + * @tc.name : vsnprintfp_s_0130 + * @tc.desc : test vsnprintf param fmt is "%S" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0130(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%S"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, "Test"); + EXPECT_TRUE("vsnprintfp_s_0130", ret < 0); +} + +/** + * @tc.name : vsnprintfp_s_0140 + * @tc.desc : test vsnprintf param fmt is "%c" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0140(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%c"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 'a'); + EXPECT_TRUE("vsnprintfp_s_0140", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0140", buf, "a"); +} + +/** + * @tc.name : vsnprintfp_s_0150 + * @tc.desc : test vsnprintf param fmt is "%lu" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0150(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%lu"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 21); + EXPECT_TRUE("vsnprintfp_s_0150", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0150", buf, "21"); +} + +/** + * @tc.name : vsnprintfp_s_0160 + * @tc.desc : test vsnprintf param fmt is "%.3f, %2.2f" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0160(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%.3f, %2.2f"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 3.14, 2.2); + EXPECT_TRUE("vsnprintfp_s_0160", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0160", buf, "3.140, 2.20"); +} + +/** + * @tc.name : vsnprintfp_s_0170 + * @tc.desc : test vsnprintf param fmt is "%% %n\n" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0170(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%% %n\n"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt); + EXPECT_TRUE("vsnprintfp_s_0170", ret < 0); +} + +/** + * @tc.name : vsnprintfp_s_0180 + * @tc.desc : test vsnprintf param fmt is "%F" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0180(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%F"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 3.14); + EXPECT_TRUE("vsnprintfp_s_0180", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0180", buf, "3.140000"); +} + +/** + * @tc.name : vsnprintfp_s_0190 + * @tc.desc : test vsnprintf param fmt is "%x" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0190(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%x"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 61); + EXPECT_TRUE("vsnprintfp_s_0190", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0190", buf, "3d"); +} + +/** + * @tc.name : vsnprintfp_s_0200 + * @tc.desc : test vsnprintf param fmt is "%X" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0200(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%X"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 61); + EXPECT_TRUE("vsnprintfp_s_0200", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0200", buf, "3D"); +} + +/** + * @tc.name : vsnprintfp_s_0210 + * @tc.desc : test vsnprintf param fmt is "%5d*" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0210(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "*%5d*"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 1024); + EXPECT_TRUE("vsnprintfp_s_0210", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0210", buf, "* 1024*"); +} + +/** + * @tc.name : vsnprintfp_s_0220 + * @tc.desc : test vsnprintf param fmt is "%% %n\n" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0220(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%#0*X"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 8, 128); + EXPECT_TRUE("vsnprintfp_s_0220", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0220", buf, "0X000080"); +} + +/** + * @tc.name : vsnprintfp_s_0230 + * @tc.desc : test vsnprintf param fmt is "%+d" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0230(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%+d"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 22); + EXPECT_TRUE("vsnprintfp_s_0230", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0230", buf, "+22"); +} + +/** + * @tc.name : vsnprintfp_s_0240 + * @tc.desc : test vsnprintf param fmt is "%-d" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0240(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%-d"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 22); + EXPECT_TRUE("vsnprintfp_s_0240", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0240", buf, "22"); +} + +/** + * @tc.name : vsnprintfp_s_0250 + * @tc.desc : test vsnprintf param fmt is "%ho" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0250(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%ho"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 9); + EXPECT_TRUE("vsnprintfp_s_0250", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0250", buf, "11"); +} + +/** + * @tc.name : vsnprintfp_s_0260 + * @tc.desc : test vsnprintf param fmt is "%Le" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0260(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%Le"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 10000); + EXPECT_TRUE("vsnprintfp_s_0260", ret == 0); +} + +/** + * @tc.name : vsnprintfp_s_0270 + * @tc.desc : test vsnprintf param fmt is "%{public}d" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0270(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%{public}d"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt, 22); + EXPECT_TRUE("vsnprintfp_s_0270", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0270", buf, "22"); +} + +/** + * @tc.name : vsnprintfp_s_0280 + * @tc.desc : test vsnprintf param fmt is "%{private}d" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0280(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%{private}d"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt, 22); + EXPECT_TRUE("vsnprintfp_s_0280", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0280", buf, ""); +} + +/** + * @tc.name : vsnprintfp_s_0290 + * @tc.desc : test vsnprintf param fmt is "%C" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0290(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%C"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 'a'); + EXPECT_TRUE("vsnprintfp_s_0290", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0290", buf, "a"); +} + +/** + * @tc.name : vsnprintfp_s_0300 + * @tc.desc : test vsnprintf param fmt is "%c" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0300(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%c"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 'a'); + EXPECT_TRUE("vsnprintfp_s_0300", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0300", buf, "a"); +} + +/** + * @tc.name : vsnprintfp_s_0310 + * @tc.desc : test vsnprintf param fmt is "%I64 %hho" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0310(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%I64o %hho"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 2, 3); + EXPECT_TRUE("vsnprintfp_s_0310", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0310", buf, "6040000000003 66"); +} + +/** + * @tc.name : vsnprintfp_s_0320 + * @tc.desc : test vsnprintf param fmt is "%.3f" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0320(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "%.3f"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 3.14); + EXPECT_TRUE("vsnprintfp_s_0320", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0320", buf, "3.140"); +} + +/** + * @tc.name : vsnprintfp_s_0330 + * @tc.desc : test vsnprintf param fmt is "%{private}f" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0330(void) +{ + char buf[2] = {0}; + char *fmt = "%{private}f"; + int ret = vsprintf_test(buf, 2, 1, false, fmt, 3.14); + EXPECT_TRUE("vsnprintfp_s_0330", ret < 0); + EXPECT_STREQ("vsnprintfp_s_0330", buf, "3"); +} + +/** + * @tc.name : vsnprintfp_s_0340 + * @tc.desc : test vsnprintf param fmt is "% d" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0340(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "% d"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt, 2); + EXPECT_TRUE("vsnprintfp_s_0340", ret > 0); + EXPECT_STREQ("vsnprintfp_s_0340", buf, " 2"); +} + +/** + * @tc.name : vsnprintfp_s_0350 + * @tc.desc : test vsnprintf param fmt is "*%t*" + * @tc.level : Level 1 + */ +static void vsnprintfp_s_0350(void) +{ + char buf[MAX_LOG_LEN] = {0}; + char *fmt = "*%t*"; + int ret = vsprintf_test(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, false, fmt); + EXPECT_TRUE("vsnprintfp_s_0350", ret < 0); +} + +TEST_FUN G_Fun_Array[] = { + vsnprintfp_s_0010, vsnprintfp_s_0020, vsnprintfp_s_0030, vsnprintfp_s_0040, vsnprintfp_s_0050, + vsnprintfp_s_0060, vsnprintfp_s_0070, vsnprintfp_s_0080, vsnprintfp_s_0090, vsnprintfp_s_0100, + vsnprintfp_s_0110, vsnprintfp_s_0120, vsnprintfp_s_0130, vsnprintfp_s_0140, vsnprintfp_s_0150, + vsnprintfp_s_0160, vsnprintfp_s_0170, vsnprintfp_s_0180, vsnprintfp_s_0190, vsnprintfp_s_0200, + vsnprintfp_s_0210, vsnprintfp_s_0220, vsnprintfp_s_0230, vsnprintfp_s_0240, vsnprintfp_s_0250, + vsnprintfp_s_0260, vsnprintfp_s_0270, vsnprintfp_s_0280, vsnprintfp_s_0290, vsnprintfp_s_0300, + vsnprintfp_s_0310, vsnprintfp_s_0320, vsnprintfp_s_0330, vsnprintfp_s_0340, vsnprintfp_s_0350, +}; + +int main(void) +{ + int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); + for (int pos = 0; pos < num; ++pos) { + G_Fun_Array[pos](); + } + return t_status; +} diff --git a/libc-test/src/functionalext/unittest/unit_test_hook_preinit.c b/libc-test/src/functionalext/unittest/unit_test_hook_preinit.c new file mode 100644 index 0000000000000000000000000000000000000000..dc0da9e443dcd1e1a9e128579360a664dc735aca --- /dev/null +++ b/libc-test/src/functionalext/unittest/unit_test_hook_preinit.c @@ -0,0 +1,117 @@ +/** + * 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 "test.h" +#include "functionalext.h" + +typedef void (*TEST_FUN)(void); + +#ifdef UNIT_TEST_STATIC +enum EnumHookMode; + +int parse_hook_variable(enum EnumHookMode* mode, char* path, int size); +/** + * @tc.name : preinit_test_0010 + * @tc.desc : test + * @tc.level : Level 2 + */ +static void preinit_test_0010(void) +{ + int ret = parse_hook_variable(NULL, NULL, 0); + EXPECT_EQ(preinit_test_0010, ret, -1); +} + +bool get_proc_name(pid_t pid, char *buf, unsigned int buf_len); +/** + * @tc.name : preinit_test_0020 + * @tc.desc : test + * @tc.level : Level 2 + */ +static void preinit_test_0020(void) +{ + int ret = get_proc_name(0, NULL, 0); + EXPECT_EQ(preinit_test_0020, ret, false); +} + +void clear_function_table(); +/** + * @tc.name : preinit_test_0030 + * @tc.desc : test + * @tc.level : Level 2 + */ +static void preinit_test_0030(void) +{ + clear_function_table(); + EXPECT_TRUE(preinit_test_0030, true); +} + +void get_memleak_hook_param(); +/** + * @tc.name : preinit_test_0040 + * @tc.desc : test + * @tc.level : Level 2 + */ +static void preinit_test_0040(void) +{ + get_memleak_hook_param(); + EXPECT_TRUE(preinit_test_0040, true); +} + +void __restore_hook_function_table(); +/** + * @tc.name : preinit_test_0050 + * @tc.desc : test + * @tc.level : Level 2 + */ +static void preinit_test_0050(void) +{ + __restore_hook_function_table(); + EXPECT_TRUE(preinit_test_0050, true); +} + +void __install_malloc_hook(); +/** + * @tc.name : preinit_test_0060 + * @tc.desc : test + * @tc.level : Level 2 + */ +static void preinit_test_0060(void) +{ + __install_malloc_hook(); + EXPECT_TRUE(preinit_test_0060, true); +} + +#endif + +TEST_FUN G_Fun_Array[] = { +#ifdef UNIT_TEST_STATIC + preinit_test_0010, + preinit_test_0020, + preinit_test_0030, + preinit_test_0040, + preinit_test_0050, + preinit_test_0060, +#endif +}; + +int main(void) +{ + int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); + for (int pos = 0; pos < num; ++pos) { + G_Fun_Array[pos](); + } + + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/unittest/unit_test_ldso_dynlink.c b/libc-test/src/functionalext/unittest/unit_test_ldso_dynlink.c new file mode 100644 index 0000000000000000000000000000000000000000..75718c173f6466ebee43a53607b47fa62a9ee228 --- /dev/null +++ b/libc-test/src/functionalext/unittest/unit_test_ldso_dynlink.c @@ -0,0 +1,178 @@ +/** + * 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 "test.h" +#include "dlfcn.h" +#include "namespace.h" +#include "functionalext.h" + +typedef void (*TEST_FUN)(void); + +/** + * @tc.name : dynlink_test_0010 + * @tc.desc : dlns_set_namespace_lib_path test + * @tc.level : Level 2 + */ +static void dynlink_test_0010(void) +{ + int ret = dlns_set_namespace_lib_path(NULL, NULL); + EXPECT_EQ(dynlink_test_0010, ret, EINVAL); +} + +/** + * @tc.name : dynlink_test_0020 + * @tc.desc : dlns_set_namespace_lib_path test + * @tc.level : Level 2 + */ +static void dynlink_test_0020(void) +{ + int ret = dlns_set_namespace_lib_path("abc", "abc"); + EXPECT_EQ(dynlink_test_0020, ret, ENOKEY); +} + +/** + * @tc.name : dynlink_test_0030 + * @tc.desc : dlns_set_namespace_lib_path test + * @tc.level : Level 1 + */ +static void dynlink_test_0030(void) +{ + int ret = dlns_set_namespace_lib_path("default", "/data"); + EXPECT_EQ(dynlink_test_0030, ret, 0); +} + +/** + * @tc.name : dynlink_test_0040 + * @tc.desc : dlns_set_namespace_separated test + * @tc.level : Level 2 + */ +static void dynlink_test_0040(void) +{ + int ret = dlns_set_namespace_separated(NULL, true); + EXPECT_EQ(dynlink_test_0040, ret, EINVAL); +} + +/** + * @tc.name : dynlink_test_0050 + * @tc.desc : dlns_set_namespace_separated test + * @tc.level : Level 2 + */ +static void dynlink_test_0050(void) +{ + int ret = dlns_set_namespace_separated("abc", true); + EXPECT_EQ(dynlink_test_0050, ret, ENOKEY); +} + +/** + * @tc.name : dynlink_test_0060 + * @tc.desc : dlns_set_namespace_separated test + * @tc.level : Level 0 + */ +static void dynlink_test_0060(void) +{ + int ret = dlns_set_namespace_separated("default", true); + EXPECT_EQ(dynlink_test_0060, ret, 0); +} + +/** + * @tc.name : dynlink_test_0070 + * @tc.desc : dlns_set_namespace_permitted_paths test + * @tc.level : Level 2 + */ +static void dynlink_test_0070(void) +{ + int ret = dlns_set_namespace_permitted_paths(NULL, NULL); + EXPECT_EQ(dynlink_test_0070, ret, EINVAL); +} + +/** + * @tc.name : dynlink_test_0080 + * @tc.desc : dlns_set_namespace_permitted_paths test + * @tc.level : Level 2 + */ +static void dynlink_test_0080(void) +{ + int ret = dlns_set_namespace_permitted_paths("abc", "abc"); + EXPECT_EQ(dynlink_test_0080, ret, ENOKEY); +} + +/** + * @tc.name : dynlink_test_0090 + * @tc.desc : dlns_set_namespace_permitted_paths test + * @tc.level : Level 0 + */ +static void dynlink_test_0090(void) +{ + int ret = dlns_set_namespace_permitted_paths("default", "/data"); + EXPECT_EQ(dynlink_test_0090, ret, 0); +} + +/** + * @tc.name : dynlink_test_0100 + * @tc.desc : dlns_set_namespace_allowed_libs test + * @tc.level : Level 2 + */ +static void dynlink_test_0100(void) +{ + int ret = dlns_set_namespace_allowed_libs(NULL, NULL); + EXPECT_EQ(dynlink_test_0100, ret, EINVAL); +} + +/** + * @tc.name : dynlink_test_0110 + * @tc.desc : dlns_set_namespace_allowed_libs test + * @tc.level : Level 2 + */ +static void dynlink_test_0110(void) +{ + int ret = dlns_set_namespace_allowed_libs("abc", "abc"); + EXPECT_EQ(dynlink_test_0110, ret, ENOKEY); +} + +/** + * @tc.name : dynlink_test_0120 + * @tc.desc : dlns_set_namespace_allowed_libs test + * @tc.level : Level 0 + */ +static void dynlink_test_0120(void) +{ + int ret = dlns_set_namespace_allowed_libs("default", "/data"); + EXPECT_EQ(dynlink_test_0120, ret, 0); +} + +TEST_FUN G_Fun_Array[] = { + dynlink_test_0010, + dynlink_test_0020, + dynlink_test_0030, + dynlink_test_0040, + dynlink_test_0050, + dynlink_test_0060, + dynlink_test_0070, + dynlink_test_0080, + dynlink_test_0090, + dynlink_test_0100, + dynlink_test_0110, + dynlink_test_0120, +}; + +int main(void) +{ + int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); + for (int pos = 0; pos < num; ++pos) { + G_Fun_Array[pos](); + } + + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/unittest/unit_test_ldso_ld_log.c b/libc-test/src/functionalext/unittest/unit_test_ldso_ld_log.c new file mode 100644 index 0000000000000000000000000000000000000000..9c66943f90dd22502ca1f8bb2ad2d69f33b55f63 --- /dev/null +++ b/libc-test/src/functionalext/unittest/unit_test_ldso_ld_log.c @@ -0,0 +1,44 @@ +/** + * 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 "test.h" +#include "ld_log.h" +#include "functionalext.h" + +typedef void (*TEST_FUN)(void); + +/** + * @tc.name : strops_test_0010 + * @tc.desc : strlwc test args null + * @tc.level : Level 0 + */ +static void ld_log_test_0010(void) +{ + ld_log_reset(); + EXPECT_TRUE(ld_log_test_0010, true); +} + +TEST_FUN G_Fun_Array[] = { + ld_log_test_0010, +}; + +int main(void) +{ + int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); + for (int pos = 0; pos < num; ++pos) { + G_Fun_Array[pos](); + } + + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/unittest/unit_test_ldso_namespace.c b/libc-test/src/functionalext/unittest/unit_test_ldso_namespace.c new file mode 100644 index 0000000000000000000000000000000000000000..8cf3d5daa28ea69f477c959b52195dd26e51c507 --- /dev/null +++ b/libc-test/src/functionalext/unittest/unit_test_ldso_namespace.c @@ -0,0 +1,638 @@ +/** + * 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 "test.h" +#include "namespace.h" +#include "functionalext.h" + +typedef void (*TEST_FUN)(void); + +struct dso { + char* name; +}; + +/** + * @tc.name : namespace_test_0010 + * @tc.desc : ns_free test arg is null + * @tc.level : Level 2 + */ +static void namespace_test_0010(void) +{ + ns_free(NULL); + EXPECT_TRUE(namespace_test_0010, true); +} + +/** + * @tc.name : namespace_test_0020 + * @tc.desc : ns_free test + * @tc.level : Level 0 + */ +static void namespace_test_0020(void) +{ + ns_t ns; + int tmp = 1; + ns.ns_dsos = (dsolist*)&tmp; + ns.ns_name = (char*)&tmp; + ns.env_paths = (char*)&tmp; + ns.lib_paths = (char*)&tmp; + ns.asan_lib_paths = (char*)&tmp; + ns.permitted_paths = NULL; + ns.asan_permitted_paths = NULL; + ns.allowed_libs = NULL; + ns_inherit_list list; + list.num = 1; + ns_inherit inherit; + inherit.shared_libs = NULL; + ns_inherit* pInherit = &inherit; + list.inherits = &pInherit; + ns.ns_inherits = &list; + + ns_free(&ns); + EXPECT_TRUE(namespace_test_0020, true); +} + +/** + * @tc.name : namespace_test_0030 + * @tc.desc : ns_free test + * @tc.level : Level 2 + */ +static void namespace_test_0030(void) +{ + ns_t ns; + ns.ns_dsos = NULL; + ns.ns_name = NULL; + ns.env_paths = NULL; + ns.lib_paths = NULL; + ns.asan_lib_paths = NULL; + ns.permitted_paths = NULL; + ns.asan_permitted_paths = NULL; + ns.allowed_libs = NULL; + ns.ns_inherits = NULL; + + ns_free(&ns); + EXPECT_TRUE(namespace_test_0030, true); +} + +/** + * @tc.name : namespace_test_0040 + * @tc.desc : ns_add_dso test + * @tc.level : Level 2 + */ +static void namespace_test_0040(void) +{ + ns_add_dso(NULL, NULL); + EXPECT_TRUE(namespace_test_0040, true); +} + +/** + * @tc.name : namespace_test_0050 + * @tc.desc : ns_add_dso test + * @tc.level : Level 2 + */ +static void namespace_test_0050(void) +{ + ns_t ns; + ns_add_dso(&ns, NULL); + EXPECT_TRUE(namespace_test_0050, true); +} + +/** + * @tc.name : namespace_test_0060 + * @tc.desc : ns_add_dso test + * @tc.level : Level 2 + */ +static void namespace_test_0060(void) +{ + struct dso d; + ns_add_dso(NULL, &d); + EXPECT_TRUE(namespace_test_0060, true); +} + +/** + * @tc.name : namespace_test_0070 + * @tc.desc : ns_add_dso test + * @tc.level : Level 1 + */ +static void namespace_test_0070(void) +{ + ns_t ns; + struct dso d; + dsolist list; + list.num = 1; + list.size = 1; + ns.ns_dsos = &list; + ns_add_dso(&ns, &d); + EXPECT_EQ(namespace_test_0070, list.size, 2); +} + +/** + * @tc.name : namespace_test_0080 + * @tc.desc : nslist_add_ns test + * @tc.level : Level 1 + */ +static void namespace_test_0080(void) +{ + ns_t ns; + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + nslist_add_ns(&ns); + EXPECT_TRUE(namespace_test_0080, true); +} + +/** + * @tc.name : namespace_test_0090 + * @tc.desc : nslist_add_ns test + * @tc.level : Level 2 + */ +static void namespace_test_0090(void) +{ + nslist_add_ns(NULL); + EXPECT_TRUE(namespace_test_0090, true); +} + +/** + * @tc.name : namespace_test_0100 + * @tc.desc : ns_set_name test + * @tc.level : Level 2 + */ +static void namespace_test_0100(void) +{ + ns_set_name(NULL, NULL); + EXPECT_TRUE(namespace_test_0100, true); +} + +/** + * @tc.name : namespace_test_0110 + * @tc.desc : ns_set_name test + * @tc.level : Level 2 + */ +static void namespace_test_0110(void) +{ + ns_t ns; + ns_set_name(&ns, NULL); + EXPECT_TRUE(namespace_test_0110, true); +} + +/** + * @tc.name : namespace_test_0120 + * @tc.desc : ns_set_name test + * @tc.level : Level 2 + */ +static void namespace_test_0120(void) +{ + ns_set_name(NULL, "abc"); + EXPECT_TRUE(namespace_test_0120, true); +} + +/** + * @tc.name : namespace_test_0130 + * @tc.desc : ns_set_env_paths test + * @tc.level : Level 2 + */ +static void namespace_test_0130(void) +{ + ns_set_env_paths(NULL, "abc"); + EXPECT_TRUE(namespace_test_0130, true); +} + +/** + * @tc.name : namespace_test_0140 + * @tc.desc : ns_set_env_paths test + * @tc.level : Level 1 + */ +static void namespace_test_0140(void) +{ + ns_t ns; + ns.env_paths = NULL; + ns_set_env_paths(&ns, "abc"); + EXPECT_EQ(namespace_test_0140, strcmp(ns.env_paths, "abc"), 0); +} + +/** + * @tc.name : namespace_test_0150 + * @tc.desc : ns_set_env_paths test + * @tc.level : Level 1 + */ +static void namespace_test_0150(void) +{ + ns_t ns; + ns.env_paths = "abc"; + ns_set_env_paths(&ns, "abc"); + EXPECT_EQ(namespace_test_0150, strcmp(ns.env_paths, "abc"), 0); +} + +/** + * @tc.name : namespace_test_0160 + * @tc.desc : ns_set_env_paths test + * @tc.level : Level 2 + */ +static void namespace_test_0160(void) +{ + ns_t ns; + ns.env_paths = NULL; + ns_set_env_paths(&ns, NULL); + EXPECT_EQ(namespace_test_0160, ns.env_paths, NULL); +} + +/** + * @tc.name : namespace_test_0170 + * @tc.desc : ns_set_env_paths test + * @tc.level : Level 2 + */ +static void namespace_test_0170(void) +{ + ns_t ns; + ns.env_paths = "abc"; + ns_set_env_paths(&ns, NULL); + EXPECT_EQ(namespace_test_0170, ns.env_paths, NULL); +} + +/** + * @tc.name : namespace_test_0180 + * @tc.desc : ns_set_asan_lib_paths test + * @tc.level : Level 2 + */ +static void namespace_test_0180(void) +{ + ns_set_asan_lib_paths(NULL, "abc"); + EXPECT_TRUE(namespace_test_0180, true); +} + +/** + * @tc.name : namespace_test_0190 + * @tc.desc : ns_set_asan_lib_paths test + * @tc.level : Level 1 + */ +static void namespace_test_0190(void) +{ + ns_t ns; + ns.asan_lib_paths = NULL; + ns_set_asan_lib_paths(&ns, "abc"); + EXPECT_EQ(namespace_test_0190, strcmp(ns.asan_lib_paths, "abc"), 0); +} + +/** + * @tc.name : namespace_test_0200 + * @tc.desc : ns_set_asan_lib_paths test + * @tc.level : Level 1 + */ +static void namespace_test_0200(void) +{ + ns_t ns; + ns.asan_lib_paths = "abc"; + ns_set_asan_lib_paths(&ns, "abc"); + EXPECT_EQ(namespace_test_0200, strcmp(ns.asan_lib_paths, "abc"), 0); +} + +/** + * @tc.name : namespace_test_0210 + * @tc.desc : ns_set_asan_lib_paths test + * @tc.level : Level 2 + */ +static void namespace_test_0210(void) +{ + ns_t ns; + ns.asan_lib_paths = NULL; + ns_set_asan_lib_paths(&ns, NULL); + EXPECT_EQ(namespace_test_0210, ns.asan_lib_paths, NULL); +} + +/** + * @tc.name : namespace_test_0220 + * @tc.desc : ns_set_asan_lib_paths test + * @tc.level : Level 1 + */ +static void namespace_test_0220(void) +{ + ns_t ns; + ns.asan_lib_paths = "abc"; + ns_set_asan_lib_paths(&ns, NULL); + EXPECT_EQ(namespace_test_0220, ns.asan_lib_paths, NULL); +} + +/** + * @tc.name : namespace_test_0230 + * @tc.desc : ns_set_permitted_paths test + * @tc.level : Level 1 + */ +static void namespace_test_0230(void) +{ + ns_t ns; + strlist list; + ns.permitted_paths = &list; + ns_set_permitted_paths(&ns, NULL); + EXPECT_EQ(namespace_test_0230, ns.permitted_paths, NULL); +} + +/** + * @tc.name : namespace_test_0240 + * @tc.desc : ns_set_permitted_paths test + * @tc.level : Level 2 + */ +static void namespace_test_0240(void) +{ + ns_set_permitted_paths(NULL, NULL); + EXPECT_TRUE(namespace_test_0240, true); +} + +/** + * @tc.name : namespace_test_0250 + * @tc.desc : ns_set_permitted_paths test + * @tc.level : Level 2 + */ +static void namespace_test_0250(void) +{ + ns_t ns; + ns.permitted_paths = NULL; + ns_set_permitted_paths(&ns, NULL); + EXPECT_EQ(namespace_test_0250, ns.permitted_paths, NULL); +} + +/** + * @tc.name : namespace_test_0260 + * @tc.desc : ns_set_asan_permitted_paths test + * @tc.level : Level 1 + */ +static void namespace_test_0260(void) +{ + ns_t ns; + strlist list; + ns.asan_permitted_paths = &list; + ns_set_asan_permitted_paths(&ns, NULL); + EXPECT_EQ(namespace_test_0260, ns.asan_permitted_paths, NULL); +} + +/** + * @tc.name : namespace_test_0270 + * @tc.desc : ns_set_asan_permitted_paths test + * @tc.level : Level 2 + */ +static void namespace_test_0270(void) +{ + ns_set_asan_permitted_paths(NULL, NULL); + EXPECT_TRUE(namespace_test_0270, true); +} + +/** + * @tc.name : namespace_test_0280 + * @tc.desc : ns_set_asan_permitted_paths test + * @tc.level : Level 1 + */ +static void namespace_test_0280(void) +{ + ns_t ns; + ns.asan_permitted_paths = NULL; + ns_set_asan_permitted_paths(&ns, NULL); + EXPECT_EQ(namespace_test_0280, ns.asan_permitted_paths, NULL); +} + +/** + * @tc.name : namespace_test_0290 + * @tc.desc : ns_set_separated test + * @tc.level : Level 2 + */ +static void namespace_test_0290(void) +{ + ns_set_separated(NULL, false); + EXPECT_TRUE(namespace_test_0290, true); +} + +/** + * @tc.name : namespace_test_0300 + * @tc.desc : ns_set_allowed_libs test + * @tc.level : Level 2 + */ +static void namespace_test_0300(void) +{ + ns_set_allowed_libs(NULL, "abc"); + EXPECT_TRUE(namespace_test_0300, true); +} + +/** + * @tc.name : namespace_test_0310 + * @tc.desc : ns_set_allowed_libs test + * @tc.level : Level 1 + */ +static void namespace_test_0310(void) +{ + ns_t ns; + ns.allowed_libs = NULL; + ns_set_allowed_libs(&ns, "abc"); + EXPECT_EQ(namespace_test_0310, (ns.allowed_libs != NULL), true); +} + +/** + * @tc.name : namespace_test_0320 + * @tc.desc : ns_set_allowed_libs test + * @tc.level : Level 0 + */ +static void namespace_test_0320(void) +{ + ns_t ns; + strlist list; + ns.allowed_libs = &list; + ns_set_allowed_libs(&ns, "abc"); + EXPECT_EQ(namespace_test_0320, (ns.allowed_libs != NULL), true); +} + +/** + * @tc.name : namespace_test_0330 + * @tc.desc : ns_set_allowed_libs test + * @tc.level : Level 1 + */ +static void namespace_test_0330(void) +{ + ns_t ns; + ns.allowed_libs = NULL; + ns_set_allowed_libs(&ns, NULL); + EXPECT_EQ(namespace_test_0330, ns.allowed_libs, NULL); +} + +/** + * @tc.name : namespace_test_0400 + * @tc.desc : ns_set_allowed_libs test + * @tc.level : Level 1 + */ +static void namespace_test_0400(void) +{ + ns_t ns; + strlist list; + ns.allowed_libs = &list; + ns_set_allowed_libs(&ns, NULL); + EXPECT_EQ(namespace_test_0400, ns.allowed_libs, NULL); +} + +/** + * @tc.name : namespace_test_0340 + * @tc.desc : ns_add_inherit test + * @tc.level : Level 2 + */ +static void namespace_test_0340(void) +{ + ns_t ns; + ns_add_inherit(&ns, NULL, NULL); + EXPECT_TRUE(namespace_test_0340, true); +} + +/** + * @tc.name : namespace_test_0350 + * @tc.desc : ns_add_inherit test + * @tc.level : Level 2 + */ +static void namespace_test_0350(void) +{ + + ns_add_inherit(NULL, NULL, NULL); + EXPECT_TRUE(namespace_test_0350, true); +} + +/** + * @tc.name : namespace_test_0360 + * @tc.desc : ns_add_inherit test + * @tc.level : Level 2 + */ +static void namespace_test_0360(void) +{ + ns_t ns; + ns_add_inherit(NULL, &ns, NULL); + EXPECT_TRUE(namespace_test_0360, true); +} + +/** + * @tc.name : namespace_test_0370 + * @tc.desc : is_accessible test + * @tc.level : Level 1 + */ +static void namespace_test_0370(void) +{ + ns_t ns; + ns.separated = true; + bool ret = is_accessible(&ns, NULL , true, true); + EXPECT_EQ(namespace_test_0370, ret, false); +} + +/** + * @tc.name : namespace_test_0380 + * @tc.desc : is_accessible test + * @tc.level : Level 1 + */ +static void namespace_test_0380(void) +{ + ns_t ns; + ns.separated = false; + bool ret = is_accessible(&ns, NULL, true, false); + EXPECT_EQ(namespace_test_0380, ret, false); +} + +/** + * @tc.name : namespace_test_0390 + * @tc.desc : is_accessible test + * @tc.level : Level 1 + */ +static void namespace_test_0390(void) +{ + ns_t ns; + ns.separated = false; + strlist list; + list.num = 1; + char *name = "test.so"; + list.strs = &name; + + ns.allowed_libs = &list; + ns.env_paths = NULL; + ns.lib_paths = NULL; + ns.asan_lib_paths = NULL; + ns.asan_permitted_paths = NULL; + ns.permitted_paths = NULL; + bool ret = is_accessible(&ns, "/data/test.so", true, false); + EXPECT_EQ(namespace_test_0390, ret, false); +} + +/** + * @tc.name : namespace_test_0410 + * @tc.desc : is_sharable test + * @tc.level : Level 0 + */ +static void namespace_test_0410(void) +{ + bool ret = is_sharable(NULL, NULL); + EXPECT_EQ(namespace_test_0410, ret, true); +} + +TEST_FUN G_Fun_Array[] = { + namespace_test_0010, + namespace_test_0020, + namespace_test_0030, + namespace_test_0040, + namespace_test_0050, + namespace_test_0060, + namespace_test_0070, + namespace_test_0080, + namespace_test_0090, + namespace_test_0100, + namespace_test_0110, + namespace_test_0120, + namespace_test_0130, + namespace_test_0140, + namespace_test_0150, + namespace_test_0160, + namespace_test_0170, + namespace_test_0180, + namespace_test_0190, + namespace_test_0200, + namespace_test_0210, + namespace_test_0220, + namespace_test_0230, + namespace_test_0250, + namespace_test_0260, + namespace_test_0270, + namespace_test_0280, + namespace_test_0290, + namespace_test_0300, + namespace_test_0310, + namespace_test_0320, + namespace_test_0330, + namespace_test_0340, + namespace_test_0350, + namespace_test_0360, + namespace_test_0370, + namespace_test_0380, + namespace_test_0390, + namespace_test_0400, + namespace_test_0410, +}; + +int main(void) +{ + int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); + for (int pos = 0; pos < num; ++pos) { + G_Fun_Array[pos](); + } + + return t_status; +} diff --git a/libc-test/src/functionalext/unittest/unit_test_ldso_ns_config.c b/libc-test/src/functionalext/unittest/unit_test_ldso_ns_config.c new file mode 100644 index 0000000000000000000000000000000000000000..b3ad320ad64d95bafad984e63eadcba25c062afc --- /dev/null +++ b/libc-test/src/functionalext/unittest/unit_test_ldso_ns_config.c @@ -0,0 +1,204 @@ +/** + * 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 "test.h" +#include "ns_config.h" +#include "functionalext.h" + +typedef void (*TEST_FUN)(void); + +/** + * @tc.name : ns_config_0010 + * @tc.desc : test error config + * @tc.level : Level 1 + */ +static void ns_config_test_0010(void) +{ + ns_configor* configor = NULL; + configor = configor_init(); + configor->set_error_callback(NULL); + configor_free(); + + EXPECT_TRUE(ns_config_test_0010, true); +} + +int errorback(const char* format, ...) +{ + return 0; +} + +/** + * @tc.name : ns_config_0030 + * @tc.desc : test error config + * @tc.level : Level 0 + */ +static void ns_config_test_0020(void) +{ + ns_configor* configor = NULL; + configor = configor_init(); + configor->set_error_callback(&errorback); + configor_free(); + + EXPECT_TRUE(ns_config_test_0020, true); +} + +/** + * @tc.name : ns_config_0030 + * @tc.desc : test parse config + * @tc.level : Level 2 + */ +static void ns_config_test_0030(void) +{ + ns_configor* configor = NULL; + configor = configor_init(); + configor->set_error_callback(NULL); + int ret = configor->parse("ttttttttt.txt", "ttttttttt"); + configor_free(); + EXPECT_EQ(ns_config_test_0030, ret, -2); +} + +/** + * @tc.name : ns_config_0040 + * @tc.desc : test get_namespaces + * @tc.level : Level 2 + */ +static void ns_config_test_0040(void) +{ + ns_configor* configor = NULL; + configor = configor_init(); + strlist *ret = configor->get_namespaces(); + configor_free(); + EXPECT_EQ(ns_config_test_0040, ret, NULL); +} + +/** + * @tc.name : ns_config_0050 + * @tc.desc : test get_namespaces + * @tc.level : Level 2 + */ +static void ns_config_test_0050(void) +{ + ns_configor* configor = NULL; + configor = configor_init(); + kvlist kv; + kv.num = 1; + char *key = "a"; + char *val = "b"; + kv.key = &key; + kv.val = &val; + configor->kvs = &kv; + strlist *ret = configor->get_namespaces(); + configor_free(); + EXPECT_EQ(ns_config_test_0050, ret, NULL); +} + +/** + * @tc.name : ns_config_0060 + * @tc.desc : test get_namespaces + * @tc.level : Level 2 + */ +static void ns_config_test_0060(void) +{ + ns_configor* configor = NULL; + configor = configor_init(); + kvlist kv; + kv.num = 1; + char *key = "added.nslist"; + char *val = "b"; + kv.key = &key; + kv.val = &val; + configor->kvs = &kv; + strlist *ret = configor->get_namespaces(); + configor_free(); + EXPECT_EQ(ns_config_test_0060, ret, NULL); +} + +/** + * @tc.name : ns_config_0070 + * @tc.desc : test get_lib_paths + * @tc.level : Level 2 + */ +static void ns_config_test_0070(void) +{ + ns_configor* configor = NULL; + configor = configor_init(); + char* ret = configor->get_lib_paths(NULL); + configor_free(); + EXPECT_EQ(ns_config_test_0070, ret, NULL); +} + +/** + * @tc.name : ns_config_0080 + * @tc.desc : test get_lib_paths + * @tc.level : Level 2 + */ +static void ns_config_test_0080(void) +{ + ns_configor* configor = NULL; + configor = configor_init(); + char* ret = configor->get_lib_paths("test"); + configor_free(); + EXPECT_EQ(ns_config_test_0080, ret, NULL); +} + +/** + * @tc.name : ns_config_0090 + * @tc.desc : test get_asan_lib_paths + * @tc.level : Level 2 + */ +static void ns_config_test_0090(void) +{ + ns_configor* configor = NULL; + configor = configor_init(); + char* ret = configor->get_asan_lib_paths(NULL); + configor_free(); + EXPECT_EQ(ns_config_test_0090, ret, NULL); +} + +/** + * @tc.name : ns_config_0100 + * @tc.desc : test get_asan_lib_paths + * @tc.level : Level 2 + */ +static void ns_config_test_0100(void) +{ + ns_configor* configor = NULL; + configor = configor_init(); + char* ret = configor->get_asan_lib_paths("test"); + configor_free(); + EXPECT_EQ(ns_config_test_0100, ret, NULL); +} + +TEST_FUN G_Fun_Array[] = { + ns_config_test_0010, + ns_config_test_0020, + ns_config_test_0030, + ns_config_test_0040, + ns_config_test_0050, + ns_config_test_0060, + ns_config_test_0070, + ns_config_test_0080, + ns_config_test_0090, + ns_config_test_0100, +}; + +int main(void) +{ + int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); + for (int pos = 0; pos < num; ++pos) { + G_Fun_Array[pos](); + } + + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/unittest/unit_test_mock_hilog_adapter.c b/libc-test/src/functionalext/unittest/unit_test_mock_hilog_adapter.c new file mode 100644 index 0000000000000000000000000000000000000000..4c7471b7a9735c353a4df3171bf3d2029587c3a3 --- /dev/null +++ b/libc-test/src/functionalext/unittest/unit_test_mock_hilog_adapter.c @@ -0,0 +1,39 @@ +/** + * 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 +#include + +#define SECOND_CALLED 2 +static int flag = 0; + +bool is_musl_log_enable() +{ + return true; +} + +int SystemReadParam(char *name, char *buffer, uint32_t *length) +{ + flag++; + if (flag == SECOND_CALLED) + { + return 1; + } else { + return 0; + } +} \ No newline at end of file diff --git a/libc-test/src/functionalext/unittest/unit_test_mock_molloc.c b/libc-test/src/functionalext/unittest/unit_test_mock_molloc.c new file mode 100644 index 0000000000000000000000000000000000000000..ba3bb3761ae700e2ccfcf621006860d1af928996 --- /dev/null +++ b/libc-test/src/functionalext/unittest/unit_test_mock_molloc.c @@ -0,0 +1,37 @@ +/** + * 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 "malloc_impl.h" + +static char buf[80] = "abcde"; + +void *internal_malloc(size_t n) +{ + return (void*)buf; +} + +void internal_free(void *p) +{} + +void *internal_calloc(size_t m, size_t n) +{ + return (void*)buf; +} + +void *internal_realloc(void* p, size_t n) +{ + buf[n+1] = '\0'; + return (void*)buf; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/unittest/unit_test_mock_strops.c b/libc-test/src/functionalext/unittest/unit_test_mock_strops.c new file mode 100644 index 0000000000000000000000000000000000000000..dd6124e6484160cce4a76c861b2ddc6b3d1bde59 --- /dev/null +++ b/libc-test/src/functionalext/unittest/unit_test_mock_strops.c @@ -0,0 +1,98 @@ +/* + * 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 "strops.h" + +/* string to lower */ +void strlwc(char *str) +{ + if (str == NULL) return; + while (*str != '\0') { + *str = (char)tolower(*str); + str++ ; + } + return; +} +/* trim head and tail spaces of string */ +size_t strtrim(char *str) +{ + char *last = NULL; + char *dest = str; + + if (str == NULL) return 0; + + last = str + strlen(str); + while (isspace((int)*str) && *str) str++; + while (last > str) { + if (!isspace((int)*(last-1))) break; + last--; + } + *last = (char)0; + + memmove(dest, str, last-str+1); + return last-str; +} + +static strlist slist; +static char* list[2]; + +strlist *strsplit(const char *str, const char *split_s) +{ + if(!str) return NULL; + + slist.num = 2; + slist.size = 16; + + slist.strs = list; + + slist.strs[0] = "/data/test"; + slist.strs[1] = "/etc"; + return &slist; +} + +#define STR_DEFAULT_SIZE 16 + +strlist *strlist_alloc(size_t size) +{ + return NULL; +} + +static void strlist_realloc(strlist *strs) +{ +} + +void strlist_free(strlist *strs) +{ +} + +void strlist_set(strlist *strs,const char *str) +{ + if (!strs || !str) return; + if (strs->num == strs->size) { + strlist_realloc(strs); + } + if (strs->num < strs->size) { + strs->strs[strs->num] = ld_strdup(str); + if (strs->strs[strs->num]) strs->num++; + } +} + +static char starupbuf[80]; +char *ld_strdup(const char *s) +{ + size_t l = strlen(s); + return memcpy(starupbuf, s, l+1); +} \ No newline at end of file diff --git a/libc-test/test_template.gni b/libc-test/test_template.gni index d809d74ea589ec590f7ee6da4493b779931a868f..c09f53f300ebdf9d832e394759cf9c10aaffdd35 100644 --- a/libc-test/test_template.gni +++ b/libc-test/test_template.gni @@ -20,6 +20,8 @@ musl_lib_dir = test_lib_dir = "musl/libc-test-lib" +musl_src_base = "${root_out_dir}/obj/${musl_base_dir}/${musl_ported_dir}" + template("test_unittest") { assert(defined(invoker.target_name), "file name is required in target ${target_name}") diff --git a/musl_config.gni b/musl_config.gni index 355a4d5ab38e29b7d7dcd84abc89394c96139514..d347fb726b792c98d000df193c909b2a0b902e23 100644 --- a/musl_config.gni +++ b/musl_config.gni @@ -69,3 +69,8 @@ declare_args() { product_path = "" } } + +declare_args() { + # for unit test + musl_unit_test_flag = false +} diff --git a/musl_template.gni b/musl_template.gni index 417c97479a37a33a7ca0544c41d797a6d234a681..ea7e5d9dd7a8818c623c050ef34d1c6db7d49bc2 100644 --- a/musl_template.gni +++ b/musl_template.gni @@ -714,6 +714,10 @@ template("musl_libs") { defines = [ "OHOS_ENABLE_PARAMETER" ] deps += [ "//base/startup/init/services/param/base:parameterbase" ] } + + if(musl_unit_test_flag){ + defines += [ "UNIT_TEST_STATIC" ] + } configs -= musl_inherited_configs diff --git a/porting/linux/user/src/hook/musl_preinit.c b/porting/linux/user/src/hook/musl_preinit.c index 66a668c6858cef71ed9fc410f560194aa38e2adb..19d884a89be907d32645d7c345bcafb93875d003 100644 --- a/porting/linux/user/src/hook/musl_preinit.c +++ b/porting/linux/user/src/hook/musl_preinit.c @@ -36,6 +36,12 @@ which need be escaped. #include #include "musl_log.h" +#ifdef UNIT_TEST_STATIC + #define UT_STATIC +#else + #define UT_STATIC static +#endif + void* ohos_malloc_hook_init_function(size_t bytes); static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { @@ -79,7 +85,7 @@ static void get_native_hook_param(char *buf, unsigned int buf_len) #endif } -static void get_memleak_hook_param() +UT_STATIC void get_memleak_hook_param() { #ifdef OHOS_ENABLE_PARAMETER const char *key = kMemTrackPropertyEnable; @@ -94,7 +100,7 @@ static void get_memleak_hook_param() #endif } -static int parse_hook_variable(enum EnumHookMode* mode, char* path, int size) +UT_STATIC int parse_hook_variable(enum EnumHookMode* mode, char* path, int size) { if (!mode || !path || size <= 0) { return -1; @@ -145,7 +151,7 @@ static int parse_hook_variable(enum EnumHookMode* mode, char* path, int size) return 0; } -static bool get_proc_name(pid_t pid, char *buf, unsigned int buf_len) +UT_STATIC bool get_proc_name(pid_t pid, char *buf, unsigned int buf_len) { if (pid <= 0) { return false; @@ -282,7 +288,7 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa return true; } -static void clear_function_table() +UT_STATIC void clear_function_table() { for (size_t i = 0; i < LAST_FUNCTION; i++) { function_of_shared_lib[i] = NULL; @@ -469,7 +475,7 @@ static void __set_default_malloc() atomic_store_explicit(&__musl_libc_globals.current_dispatch_table, (volatile const long long)NULL, memory_order_seq_cst); } -static void __restore_hook_function_table() +UT_STATIC void __restore_hook_function_table() { for (size_t i = 0; i < LAST_FUNCTION; i++) { if (__get_memleak_hook_flag()) { @@ -480,7 +486,7 @@ static void __restore_hook_function_table() } } -static void __install_malloc_hook() +UT_STATIC void __install_malloc_hook() { if (__get_memleak_hook_flag()) { return; diff --git a/porting/linux/user/src/internal/hilog_adapter.h b/porting/linux/user/src/internal/hilog_adapter.h index 707b269323c0c208ce5500e3a96bedfd273fc850..bfca3cfbc10401801d6cb79ea2dfec5c804cde73 100644 --- a/porting/linux/user/src/internal/hilog_adapter.h +++ b/porting/linux/user/src/internal/hilog_adapter.h @@ -20,7 +20,9 @@ #include #include +#ifndef hidden #define hidden __attribute__((visibility("hidden"))) +#endif // Log type typedef enum {