From 3423f83e355172eb1d5a664655ae6ede9bb3e5cd Mon Sep 17 00:00:00 2001 From: xwx1135370 Date: Fri, 6 Jun 2025 13:50:48 +0800 Subject: [PATCH] Add mkostemp mkostemps mkstemp mkstemps and a64l test issue:https://gitee.com/openharmony/third_party_musl/issues/ICB5CT?from=project-issue Signed-off-by: xwx1135370 --- .../src/functionalext/supplement/misc/a64l.c | 102 ++++++++++++++++++ ...test_src_functionalext_supplement_misc.gni | 1 + .../functionalext/supplement/temp/mkdtemp.c | 9 +- .../functionalext/supplement/temp/mkostemp.c | 42 ++++++++ .../functionalext/supplement/temp/mkostemps.c | 59 ++++++++++ .../functionalext/supplement/temp/mkstemp.c | 67 ++++++++++++ .../functionalext/supplement/temp/mkstemps.c | 35 ++++++ .../functionalext/supplement/temp/mktemp.c | 23 ++-- 8 files changed, 325 insertions(+), 13 deletions(-) create mode 100644 libc-test/src/functionalext/supplement/misc/a64l.c diff --git a/libc-test/src/functionalext/supplement/misc/a64l.c b/libc-test/src/functionalext/supplement/misc/a64l.c new file mode 100644 index 000000000..50de07394 --- /dev/null +++ b/libc-test/src/functionalext/supplement/misc/a64l.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2025 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" + +#define VALUE_0 2 +#define VALUE_A 12 +#define VALUE_Z 63 +#define VALUE 64 +#define VALUE_0123 1327298 +#define VALUE_ABCD 3990348 +#define VALUE_ZZZZZ 1073741823 + +/** + * @tc.name : a64l_0100 + * @tc.desc : Verify normal character + * @tc.level : Level 0 + */ +void a64l_0100(void) +{ + long result1 = a64l("0"); + long result2 = a64l("A"); + long result3 = a64l("z"); + long result4 = a64l("./"); + EXPECT_EQ("a64l_0100", result1, VALUE_0); // expect result is 2 + EXPECT_EQ("a64l_0100", result2, VALUE_A); // expect result is 12 + EXPECT_EQ("a64l_0100", result3, VALUE_Z); // expect result is 63 + EXPECT_EQ("a64l_0100", result4, VALUE); // expect result is 64 +} + +/** + * @tc.name : a64l_0200 + * @tc.desc : Verify multi character + * @tc.level : Level 0 + */ +void a64l_0200(void) +{ + long result1 = a64l("0123"); + long result2 = a64l("ABCD"); + EXPECT_EQ("a64l_0200", result1, VALUE_0123); // (2 | (3 << 6) | (4 << 12) | (5 << 18)) + EXPECT_EQ("a64l_0200", result2, VALUE_ABCD); // (12 | (13 << 6) | (14 << 12) | (15 << 18) +} + +/** + * @tc.name : a64l_0300 + * @tc.desc : Verify illegal character + * @tc.level : Level 0 + */ +void a64l_0300(void) +{ + long result1 = a64l("A@B"); + long result2 = a64l("0#"); + EXPECT_EQ("a64l_0300", result1, VALUE_A); // expect result is 12 + EXPECT_EQ("a64l_0300", result2, VALUE_0); // expect result is 2 +} + +/** + * @tc.name : a64l_0400 + * @tc.desc : Verify empty characters + * @tc.level : Level 0 + */ +void a64l_0400(void) +{ + long result = a64l(""); + EXPECT_EQ("a64l_0400", result, 0); +} + +/** + * @tc.name : a64l_0500 + * @tc.desc : Verify the maximum length + * @tc.level : Level 0 + */ +void a64l_0500(void) +{ + long result1 = a64l("zzzzzz"); + long result2 = a64l("zzzzz"); + EXPECT_EQ("a64l_0500", result1, -1); // (63 | (63 << 6) | (63 << 12) | (63 << 18) | (63 << 24) | (63 << 30)) + EXPECT_EQ("a64l_0500", result2, VALUE_ZZZZZ); // (63 | (63 << 6) | (63 << 12) | (63 << 18) | (63 << 24)) +} + +int main(int argc, char *argv[]) +{ + a64l_0100(); + a64l_0200(); + a64l_0300(); + a64l_0400(); + a64l_0500(); + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/misc/test_src_functionalext_supplement_misc.gni b/libc-test/src/functionalext/supplement/misc/test_src_functionalext_supplement_misc.gni index e65a24266..c096a0cc5 100644 --- a/libc-test/src/functionalext/supplement/misc/test_src_functionalext_supplement_misc.gni +++ b/libc-test/src/functionalext/supplement/misc/test_src_functionalext_supplement_misc.gni @@ -50,4 +50,5 @@ functionalext_supplement_misc_test = [ "getrusage", "ioctl", "functionalext_wordfree", + "a64l", ] diff --git a/libc-test/src/functionalext/supplement/temp/mkdtemp.c b/libc-test/src/functionalext/supplement/temp/mkdtemp.c index 9e38dd2ea..0c4771c50 100755 --- a/libc-test/src/functionalext/supplement/temp/mkdtemp.c +++ b/libc-test/src/functionalext/supplement/temp/mkdtemp.c @@ -17,6 +17,7 @@ #include #include "functionalext.h" +#define DIR_MODE 0700 /** * @tc.name : mkdtemp_0100 * @tc.desc : Create a temporary directory with the correct parameters @@ -29,6 +30,7 @@ void mkdtemp_0100(void) EXPECT_PTRNE("mkdtemp_0100", ret, NULL); if (ret) { EXPECT_NE("mkdtemp_0100", ret[0], CMPFLAG); + rmdir(ret); } } @@ -88,6 +90,8 @@ void mkdtemp_0500(void) EXPECT_PTRNE("mkdtemp_0500", ret2, NULL); if (ret1 && ret2) { EXPECT_STRNE("mkdtemp_0500", ret1, ret2); // The two results should be different + rmdir(ret1); + rmdir(ret2); } } @@ -96,14 +100,15 @@ void mkdtemp_0500(void) * @tc.desc : Check directory permissions (should be 0700) * @tc.level : Level 2 */ -void mkdtemp_0600(void) { +void mkdtemp_0600(void) +{ char temp_dir[] = "test-XXXXXX"; char *ret = mkdtemp(temp_dir); EXPECT_PTRNE("mkdtemp_0600", ret, NULL); if (ret) { struct stat st; stat(ret, &st); - EXPECT_EQ("mkdtemp_0600", st.st_mode & 0777, 0700); + EXPECT_EQ("mkdtemp_0600", st.st_mode & DIR_MODE, DIR_MODE); rmdir(ret); } } diff --git a/libc-test/src/functionalext/supplement/temp/mkostemp.c b/libc-test/src/functionalext/supplement/temp/mkostemp.c index 38d521d8f..083e0ba84 100644 --- a/libc-test/src/functionalext/supplement/temp/mkostemp.c +++ b/libc-test/src/functionalext/supplement/temp/mkostemp.c @@ -155,6 +155,46 @@ void mkostemp_0600(void) } } +/** + * @tc.name : mkostemp_0700 + * @tc.desc : Template with 'XXXXXX' not at the end + * @tc.level : Level 2 + */ +void mkostemp_0700(void) +{ + char temp_dir[] = "/data/mkostemp_0700_XXXXXX_dir"; + int fd = mkostemp(temp_dir, O_SYNC); + //XXXXXX is not at the end, mkdtemp() will fail and return -1. Now template is unchanged. + EXPECT_TRUE("mkostemp_0700", fd == -1); + if (fd == -1) { + EXPECT_EQ("mkostemp_0700", errno, EINVAL); + EXPECT_STREQ("mkostemp_0700", temp_dir, "/data/mkostemp_0700_XXXXXX_dir"); + } +} + +/** + * @tc.name : mkostemp_0800 + * @tc.desc : Verify mkostemp generates unique filenames on multiple calls. + * @tc.level : Level 0 + */ +void mkostemp_0800(void) +{ + char tmpfile1[] = "/data/mkostemp_0800_XXXXXX"; + char tmpfile2[] = "/data/mkostemp_0800_XXXXXX"; + int fd1 = mkostemp(tmpfile1, O_CLOEXEC); + int fd2 = mkostemp(tmpfile2, O_CLOEXEC); + EXPECT_TRUE("mkostemp_0800", fd1 != -1 && fd2 != -1); + EXPECT_STRNE("mkostemp_0800", tmpfile1, tmpfile2); // The two results should be different + if (fd1 != -1) { + close(fd1); + remove(tmpfile1); // clean + } + if (fd2 != -1) { + close(fd2); + remove(tmpfile2); // clean + } +} + int main(void) { mkostemp_0100(); @@ -163,5 +203,7 @@ int main(void) mkostemp_0400(); mkostemp_0500(); mkostemp_0600(); + mkostemp_0700(); + mkostemp_0800(); return t_status; } \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/temp/mkostemps.c b/libc-test/src/functionalext/supplement/temp/mkostemps.c index 401ac0692..a0bb02095 100644 --- a/libc-test/src/functionalext/supplement/temp/mkostemps.c +++ b/libc-test/src/functionalext/supplement/temp/mkostemps.c @@ -85,10 +85,69 @@ void mkostemps_0300(void) } } +/** + * @tc.name : mkostemps_0400 + * @tc.desc : Template with 'XXXXXX' not at the end + * @tc.level : Level 2 + */ +void mkostemps_0400(void) +{ + char tmpfile[] = "/data/local/tmp/mkostemps_0400_XXXXXX_dir"; + int fd = mkostemps(tmpfile, 0, O_CREAT); + EXPECT_TRUE("mkostemps_0400", fd == -1); + if (fd == -1) { + EXPECT_EQ("mkostemp_0700", errno, EINVAL); + EXPECT_STREQ("mkostemp_0700", tmpfile, "/data/local/tmp/mkostemps_0400_XXXXXX_dir"); + } +} + +/** + * @tc.name : mkostemps_0500 + * @tc.desc : Verify mkostemps with large suffix length + * @tc.level : Level 2 + */ +void mkostemps_0500(void) +{ + char tmpfile[] = "/data/local/tmp/mkostemps_0500_XXXXXX.dat"; + int fd = mkostemps(tmpfile, 100, O_CREAT); // Suffix length larger than actual suffix + EXPECT_TRUE("mkostemps_0500", fd == -1); + if (fd == -1) { + EXPECT_EQ("mkostemps_0500", errno, EINVAL); + } +} + +/** + * @tc.name : mkostemps_0600 + * @tc.desc : Verify mkostemp generates unique filenames on multiple calls + * @tc.level : Level 2 + */ +void mkostemps_0600(void) +{ + char tmpfile1[] = "/data/local/tmp/mkostemps_0600_XXXXXX"; + char tmpfile2[] = "/data/local/tmp/mkostemps_0600_XXXXXX"; + int fd1 = mkostemps(tmpfile1, 0, O_CREAT); + int fd2 = mkostemps(tmpfile2, 0, O_CREAT); + EXPECT_TRUE("mkostemps_0600", fd1 != -1 && fd2 != -1); + if (fd1 != -1 && fd2 != -1) { + EXPECT_STRNE("mkstemp_0600", tmpfile1, tmpfile2); + } + if (fd1 != -1) { + close(fd1); + unlink(tmpfile1); + } + if (fd2 != -1) { + close(fd2); + unlink(tmpfile2); + } +} + int main(void) { mkostemps_0100(); mkostemps_0200(); mkostemps_0300(); + mkostemps_0400(); + mkostemps_0500(); + mkostemps_0600(); return t_status; } \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/temp/mkstemp.c b/libc-test/src/functionalext/supplement/temp/mkstemp.c index 31926e01a..98cf444a6 100644 --- a/libc-test/src/functionalext/supplement/temp/mkstemp.c +++ b/libc-test/src/functionalext/supplement/temp/mkstemp.c @@ -14,7 +14,10 @@ */ #include +#include #include "functionalext.h" +#define ST_MODE_1 0600 +#define ST_MODE_2 0777 /** * @tc.name : mkstemp_0100 @@ -61,9 +64,73 @@ void mkstemp_0200(void) } } +/** + * @tc.name : mkstemp_0300 + * @tc.desc : Template with 'XXXXXX' not at the end + * @tc.level : Level 2 + */ +void mkstemp_0300(void) +{ + char tmpfile[] = "/data/mkstemp_0300_XXXXXX_dir"; + int fd = mkstemp(tmpfile); + EXPECT_TRUE("mkstemp_0300", fd == -1); + if (fd == -1) { + EXPECT_EQ("mkstemp_0300", errno, EINVAL); + EXPECT_STREQ("mkstemp_0300", tmpfile, "/data/mkstemp_0300_XXXXXX_dir"); + } +} + +/** + * @tc.name : mkstemp_0400 + * @tc.desc : Verify file permissions (0600) + * @tc.level : Level 1 + */ +void mkstemp_0400(void) +{ + char tmpfile[] = "/data/mkstemp_0400_XXXXXX"; + int fd = mkstemp(tmpfile); + EXPECT_TRUE("mkstemp_0400", fd != -1); + if (fd != -1) { + struct stat st; + fstat(fd, &st); + EXPECT_EQ("mkstemp_0400", st.st_mode & ST_MODE_2, ST_MODE_1); + close(fd); + unlink(tmpfile); + } +} + +/** + * @tc.name : mkstemp_0500 + * @tc.desc : Verify concurrent mkstemp calls + * @tc.level : Level 1 + */ +void mkstemp_0500(void) +{ + char tmpfile1[] = "/data/mkstemp_0500_XXXXXX"; + char tmpfile2[] = "/data/mkstemp_0500_XXXXXX"; + int fd1 = mkstemp(tmpfile1); + int fd2 = mkstemp(tmpfile2); + EXPECT_TRUE("mkstemp_0500", fd1 != -1 && fd2 != -1); + if (fd1 != -1 && fd2 != -1) { + EXPECT_STRNE("mkstemp_0500", tmpfile1, tmpfile2); + } + + if (fd1 != -1) { + close(fd1); + unlink(tmpfile1); + } + if (fd2 != -1) { + close(fd2); + unlink(tmpfile2); + } +} + int main(void) { mkstemp_0100(); mkstemp_0200(); + mkstemp_0300(); + mkstemp_0400(); + mkstemp_0500(); return t_status; } \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/temp/mkstemps.c b/libc-test/src/functionalext/supplement/temp/mkstemps.c index 68b4870f6..b4a37fa73 100644 --- a/libc-test/src/functionalext/supplement/temp/mkstemps.c +++ b/libc-test/src/functionalext/supplement/temp/mkstemps.c @@ -16,6 +16,8 @@ #include #include "functionalext.h" +#define SUFFIX_LEN 5 + /** * @tc.name : mkstemps_0100 * @tc.desc : Provide the correct template, create a temporary file @@ -82,10 +84,43 @@ void mkstemps_0300(void) } } +/** + * @tc.name : mkstemps_0400 + * @tc.desc : Template with 'XXXXXX' not at the end + * @tc.level : Level 2 + */ +void mkstemps_0400(void) +{ + char tmpfile[] = "/data/mkstemps_0400_XXXXXX_dir"; + int fd = mkstemps(tmpfile, 0); + EXPECT_TRUE("mkstemps_0400", fd == -1); + if (fd == -1) { + EXPECT_EQ("mkstemps_0400", errno, EINVAL); + EXPECT_STREQ("mkstemps_0400", tmpfile, "/data/mkstemps_0400_XXXXXX_dir"); + } +} + +/** + * @tc.name : mkstemps_0500 + * @tc.desc : Verify with suffix length larger than actual suffix + * @tc.level : Level 2 + */ +void mkstemps_0500(void) +{ + char tmpfile[] = "/data/local/tmp/mkstemps_0500_XXXXXX.dat"; + int fd = mkstemps(tmpfile, strlen(".dat") + SUFFIX_LEN); // Exceeding the actual suffix length + EXPECT_TRUE("mkstemps_0500", fd == -1); + if (fd == -1) { + EXPECT_EQ("mkstemps_0500", errno, EINVAL); + } +} + int main(void) { mkstemps_0100(); mkstemps_0200(); mkstemps_0300(); + mkstemps_0400(); + mkstemps_0500(); return t_status; } \ No newline at end of file diff --git a/libc-test/src/functionalext/supplement/temp/mktemp.c b/libc-test/src/functionalext/supplement/temp/mktemp.c index afa850125..15e30cdd2 100755 --- a/libc-test/src/functionalext/supplement/temp/mktemp.c +++ b/libc-test/src/functionalext/supplement/temp/mktemp.c @@ -68,8 +68,8 @@ void mktemp_0300(void) */ void mktemp_0400(void) { - char temp_dir[] = "test-XXXXXX-dir"; - char *ret = mktemp(temp_dir); + char temp_file[] = "test-XXXXXX-dir"; + char *ret = mktemp(temp_file); EXPECT_PTRNE("mktemp_0400", ret, NULL); //XXXXXX is not at the end, mktemp() will fail if (ret) { @@ -79,15 +79,15 @@ void mktemp_0400(void) /** * @tc.name : mktemp_0500 - * @tc.desc : Repeated calls generate unique directories + * @tc.desc : Repeated calls generate unique file * @tc.level : Level 1 */ void mktemp_0500(void) { - char temp_dir1[] = "test-XXXXXX"; - char temp_dir2[] = "test-XXXXXX"; - char *ret1 = mkdtemp(temp_dir1); - char *ret2 = mkdtemp(temp_dir2); + char temp_file1[] = "test-XXXXXX"; + char temp_file2[] = "test-XXXXXX"; + char *ret1 = mktemp(temp_file1); + char *ret2 = mktemp(temp_file2); EXPECT_PTRNE("mktemp_0500", ret1, NULL); EXPECT_PTRNE("mktemp_0500", ret2, NULL); if (ret1 && ret2) { @@ -103,16 +103,17 @@ void mktemp_0500(void) void mktemp_0600(void) { char temp_file[] = "test-XXXXXX"; - + char new_file[256] = {0}; int fd = mkstemp(temp_file); // create file - if (fd != -1) { + strcpy(new_file, temp_file); // get the file name char *ret = mktemp(temp_file); EXPECT_PTRNE("mktemp_0600", ret, NULL); - if(ret) { + if (ret) { EXPECT_EQ("mktemp_0600", ret[0], 0); } - unlink(temp_file); // clean file + close(fd); + unlink(new_file); // clean file } } -- Gitee