19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (C) 2024 HiHope Open Source Organization.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include <cerrno>
179762338dSopenharmony_ci#include <cstdio>
189762338dSopenharmony_ci#include <cstdlib>
199762338dSopenharmony_ci#include <string>
209762338dSopenharmony_ci#include <vector>
219762338dSopenharmony_ci#include <dirent.h>
229762338dSopenharmony_ci#include <fcntl.h>
239762338dSopenharmony_ci#include <unistd.h>
249762338dSopenharmony_ci#include <arpa/inet.h>
259762338dSopenharmony_ci#include <gtest/gtest.h>
269762338dSopenharmony_ci#include <netinet/in.h>
279762338dSopenharmony_ci#include <sys/stat.h>
289762338dSopenharmony_ci#include <sys/socket.h>
299762338dSopenharmony_ci#include <sys/types.h>
309762338dSopenharmony_ci
319762338dSopenharmony_ciusing namespace testing::ext;
329762338dSopenharmony_ci
339762338dSopenharmony_ciclass HatsUnlinkatTest : public testing::Test {
349762338dSopenharmony_cipublic:
359762338dSopenharmony_cistatic void SetUpTestCase();
369762338dSopenharmony_cistatic void TearDownTestCase();
379762338dSopenharmony_civoid SetUp();
389762338dSopenharmony_civoid TearDown();
399762338dSopenharmony_ciprivate:
409762338dSopenharmony_ci};
419762338dSopenharmony_civoid HatsUnlinkatTest::SetUp()
429762338dSopenharmony_ci{
439762338dSopenharmony_ci}
449762338dSopenharmony_civoid HatsUnlinkatTest::TearDown()
459762338dSopenharmony_ci{
469762338dSopenharmony_ci}
479762338dSopenharmony_civoid HatsUnlinkatTest::SetUpTestCase()
489762338dSopenharmony_ci{
499762338dSopenharmony_ci}
509762338dSopenharmony_civoid HatsUnlinkatTest::TearDownTestCase()
519762338dSopenharmony_ci{
529762338dSopenharmony_ci}
539762338dSopenharmony_ci
549762338dSopenharmony_cistatic const char *UNLINKAT_TEST_FILE = "/data/local/tmp/tryUnlinkat.txt";
559762338dSopenharmony_cistatic const char *UNLINKAT_TEST_DIR = "/data/local/tmp";
569762338dSopenharmony_cistatic const char *UNLINKAT_TEST_FILENAME = "tryUnlinkat.txt";
579762338dSopenharmony_ci
589762338dSopenharmony_ci/*
599762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0100
609762338dSopenharmony_ci * @tc.name   : UnlinkatUnlinkFileSuccess_0001
619762338dSopenharmony_ci * @tc.desc   : Unlinkat unlink file success.
629762338dSopenharmony_ci * @tc.size   : MediumTest
639762338dSopenharmony_ci * @tc.type   : Function
649762338dSopenharmony_ci * @tc.level  : Level 1
659762338dSopenharmony_ci */
669762338dSopenharmony_ciHWTEST_F(HatsUnlinkatTest, UnlinkatUnlinkFileSuccess_0001, Function | MediumTest | Level1)
679762338dSopenharmony_ci{
689762338dSopenharmony_ci    int fileFd;
699762338dSopenharmony_ci    int dirFd;
709762338dSopenharmony_ci    int ret;
719762338dSopenharmony_ci
729762338dSopenharmony_ci    fileFd = open(UNLINKAT_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
739762338dSopenharmony_ci    EXPECT_TRUE(fileFd >= 3);
749762338dSopenharmony_ci    close(fileFd);
759762338dSopenharmony_ci
769762338dSopenharmony_ci    dirFd = open(UNLINKAT_TEST_DIR, O_RDONLY);
779762338dSopenharmony_ci    EXPECT_TRUE(dirFd >= 3);
789762338dSopenharmony_ci
799762338dSopenharmony_ci    ret = unlinkat(dirFd, UNLINKAT_TEST_FILENAME, 0);
809762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
819762338dSopenharmony_ci    close(dirFd);
829762338dSopenharmony_ci
839762338dSopenharmony_ci    fileFd = open(UNLINKAT_TEST_FILE, O_RDWR, 0644);
849762338dSopenharmony_ci    EXPECT_TRUE(fileFd == -1);
859762338dSopenharmony_ci    close(fileFd);
869762338dSopenharmony_ci}
879762338dSopenharmony_ci
889762338dSopenharmony_ci/*
899762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0200
909762338dSopenharmony_ci * @tc.name   : UnlinkatUnlinkEmptyDirectorySuccess_0002
919762338dSopenharmony_ci * @tc.desc   : Unlinkat unlink an empty directory success.
929762338dSopenharmony_ci * @tc.size   : MediumTest
939762338dSopenharmony_ci * @tc.type   : Function
949762338dSopenharmony_ci * @tc.level  : Level 1
959762338dSopenharmony_ci */
969762338dSopenharmony_ciHWTEST_F(HatsUnlinkatTest, UnlinkatUnlinkEmptyDirectorySuccess_0002, Function | MediumTest | Level1)
979762338dSopenharmony_ci{
989762338dSopenharmony_ci    int ret;
999762338dSopenharmony_ci    DIR *testDir;
1009762338dSopenharmony_ci    struct dirent *dirEntry;
1019762338dSopenharmony_ci    const char *unlinkatNewDir1 = "/data/local/tmp/unlinkatDir1";
1029762338dSopenharmony_ci
1039762338dSopenharmony_ci
1049762338dSopenharmony_ci    ret = mkdir(unlinkatNewDir1, 0777);
1059762338dSopenharmony_ci    if (ret == -1) {
1069762338dSopenharmony_ci        testDir = opendir(unlinkatNewDir1);
1079762338dSopenharmony_ci        EXPECT_TRUE(testDir != nullptr);
1089762338dSopenharmony_ci
1099762338dSopenharmony_ci        dirEntry = readdir(testDir);
1109762338dSopenharmony_ci        EXPECT_TRUE(dirEntry != nullptr);
1119762338dSopenharmony_ci
1129762338dSopenharmony_ci        ret = strcmp(dirEntry->d_name, ".");
1139762338dSopenharmony_ci        EXPECT_TRUE(ret == 0);
1149762338dSopenharmony_ci    }
1159762338dSopenharmony_ci
1169762338dSopenharmony_ci    ret = unlinkat(AT_FDCWD, unlinkatNewDir1, AT_REMOVEDIR);
1179762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
1189762338dSopenharmony_ci}
1199762338dSopenharmony_ci
1209762338dSopenharmony_ci/*
1219762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0300
1229762338dSopenharmony_ci * @tc.name   : UnlinkatUnlinkNonemptyDirectoryFail_0003
1239762338dSopenharmony_ci * @tc.desc   : Unlinkat unlink a non-empty directory fail.
1249762338dSopenharmony_ci * @tc.size   : MediumTest
1259762338dSopenharmony_ci * @tc.type   : Function
1269762338dSopenharmony_ci * @tc.level  : Level 2
1279762338dSopenharmony_ci */
1289762338dSopenharmony_ciHWTEST_F(HatsUnlinkatTest, UnlinkatUnlinkNonemptyDirectoryFail_0003, Function | MediumTest | Level2)
1299762338dSopenharmony_ci{
1309762338dSopenharmony_ci    int fileFd;
1319762338dSopenharmony_ci    int ret;
1329762338dSopenharmony_ci    DIR *testDir;
1339762338dSopenharmony_ci    struct dirent *dirEntry;
1349762338dSopenharmony_ci    const char *unlinkatNewDir2 = "/data/local/tmp/unlinkatDir2";
1359762338dSopenharmony_ci    const char *unlinkatNewFile2 = "/data/local/tmp/unlinkatDir2/testFile.txt";
1369762338dSopenharmony_ci
1379762338dSopenharmony_ci    ret = mkdir(unlinkatNewDir2, 0777);
1389762338dSopenharmony_ci    if (ret == -1) {
1399762338dSopenharmony_ci        testDir = opendir(unlinkatNewDir2);
1409762338dSopenharmony_ci        EXPECT_TRUE(testDir != nullptr);
1419762338dSopenharmony_ci
1429762338dSopenharmony_ci        dirEntry = readdir(testDir);
1439762338dSopenharmony_ci        EXPECT_TRUE(dirEntry != nullptr);
1449762338dSopenharmony_ci
1459762338dSopenharmony_ci        ret = strcmp(dirEntry->d_name, ".");
1469762338dSopenharmony_ci        if (ret == 0) {
1479762338dSopenharmony_ci            fileFd = open(unlinkatNewFile2, O_RDWR | O_CREAT, 0777);
1489762338dSopenharmony_ci            EXPECT_TRUE(fileFd >= 3);
1499762338dSopenharmony_ci            close(fileFd);
1509762338dSopenharmony_ci        }
1519762338dSopenharmony_ci    } else {
1529762338dSopenharmony_ci        fileFd = open(unlinkatNewFile2, O_RDWR | O_CREAT, 0777);
1539762338dSopenharmony_ci        EXPECT_TRUE(fileFd >= 3);
1549762338dSopenharmony_ci        close(fileFd);
1559762338dSopenharmony_ci    }
1569762338dSopenharmony_ci    errno = 0;
1579762338dSopenharmony_ci    ret = unlinkat(AT_FDCWD, unlinkatNewDir2, AT_REMOVEDIR);
1589762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
1599762338dSopenharmony_ci    EXPECT_EQ(errno, ENOTEMPTY);
1609762338dSopenharmony_ci
1619762338dSopenharmony_ci    remove(unlinkatNewFile2);
1629762338dSopenharmony_ci}
1639762338dSopenharmony_ci
1649762338dSopenharmony_ci/*
1659762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0400
1669762338dSopenharmony_ci * @tc.name   : UnlinkatNonexistFileFail_0004
1679762338dSopenharmony_ci * @tc.desc   : Unlinkat a non-exist file fail.
1689762338dSopenharmony_ci * @tc.size   : MediumTest
1699762338dSopenharmony_ci * @tc.type   : Function
1709762338dSopenharmony_ci * @tc.level  : Level 2
1719762338dSopenharmony_ci */
1729762338dSopenharmony_ciHWTEST_F(HatsUnlinkatTest, UnlinkatNonexistFileFail_0004, Function | MediumTest | Level2)
1739762338dSopenharmony_ci{
1749762338dSopenharmony_ci    int fileFd;
1759762338dSopenharmony_ci    int dirFd;
1769762338dSopenharmony_ci    int ret;
1779762338dSopenharmony_ci
1789762338dSopenharmony_ci    remove(UNLINKAT_TEST_FILE);
1799762338dSopenharmony_ci    fileFd = open(UNLINKAT_TEST_FILE, O_RDWR | O_TRUNC, 0644);
1809762338dSopenharmony_ci    EXPECT_TRUE(fileFd == -1);
1819762338dSopenharmony_ci    close(fileFd);
1829762338dSopenharmony_ci
1839762338dSopenharmony_ci    dirFd = open(UNLINKAT_TEST_DIR, O_RDONLY);
1849762338dSopenharmony_ci    EXPECT_TRUE(dirFd >= 3);
1859762338dSopenharmony_ci
1869762338dSopenharmony_ci    errno = 0;
1879762338dSopenharmony_ci    ret = unlinkat(dirFd, UNLINKAT_TEST_FILENAME, 0);
1889762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
1899762338dSopenharmony_ci    EXPECT_EQ(errno, ENOENT);
1909762338dSopenharmony_ci    close(dirFd);
1919762338dSopenharmony_ci}
1929762338dSopenharmony_ci
1939762338dSopenharmony_ci/*
1949762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0500
1959762338dSopenharmony_ci * @tc.name   : UnlinkatDirectoryWhenFlagIsNotRemovedirFail_0005
1969762338dSopenharmony_ci * @tc.desc   : Unlinkat unlink a directory when flag is not set to AT_REMOVEDIR fail.
1979762338dSopenharmony_ci * @tc.size   : MediumTest
1989762338dSopenharmony_ci * @tc.type   : Function
1999762338dSopenharmony_ci * @tc.level  : Level 2
2009762338dSopenharmony_ci */
2019762338dSopenharmony_ciHWTEST_F(HatsUnlinkatTest, UnlinkatDirectoryWhenFlagIsNotRemovedirFail_0005, Function | MediumTest | Level2)
2029762338dSopenharmony_ci{
2039762338dSopenharmony_ci    int ret;
2049762338dSopenharmony_ci    DIR *testDir;
2059762338dSopenharmony_ci    struct dirent *dirEntry;
2069762338dSopenharmony_ci    const char *unlinkatNewDir3 = "/data/local/tmp/unlinkatDir3";
2079762338dSopenharmony_ci
2089762338dSopenharmony_ci    ret = mkdir(unlinkatNewDir3, 0777);
2099762338dSopenharmony_ci    if (ret == -1) {
2109762338dSopenharmony_ci        testDir = opendir(unlinkatNewDir3);
2119762338dSopenharmony_ci        EXPECT_TRUE(testDir != nullptr);
2129762338dSopenharmony_ci
2139762338dSopenharmony_ci        dirEntry = readdir(testDir);
2149762338dSopenharmony_ci        EXPECT_TRUE(dirEntry != nullptr);
2159762338dSopenharmony_ci
2169762338dSopenharmony_ci        ret = strcmp(dirEntry->d_name, ".");
2179762338dSopenharmony_ci        EXPECT_TRUE(ret == 0);
2189762338dSopenharmony_ci    }
2199762338dSopenharmony_ci    errno = 0;
2209762338dSopenharmony_ci    ret = unlinkat(AT_FDCWD, unlinkatNewDir3, 0);
2219762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
2229762338dSopenharmony_ci    EXPECT_EQ(errno, EISDIR);
2239762338dSopenharmony_ci}
2249762338dSopenharmony_ci
2259762338dSopenharmony_ci/*
2269762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0600
2279762338dSopenharmony_ci * @tc.name   : UnlinkatFileWhenFlagIsNot0Fail_0006
2289762338dSopenharmony_ci * @tc.desc   : Unlinkat a file when flag is not set 0 fail.
2299762338dSopenharmony_ci * @tc.size   : MediumTest
2309762338dSopenharmony_ci * @tc.type   : Function
2319762338dSopenharmony_ci * @tc.level  : Level 2
2329762338dSopenharmony_ci */
2339762338dSopenharmony_ciHWTEST_F(HatsUnlinkatTest, UnlinkatFileWhenFlagIsNot0Fail_0006, Function | MediumTest | Level2)
2349762338dSopenharmony_ci{
2359762338dSopenharmony_ci    int fileFd;
2369762338dSopenharmony_ci    int dirFd;
2379762338dSopenharmony_ci    int ret;
2389762338dSopenharmony_ci
2399762338dSopenharmony_ci    fileFd = open(UNLINKAT_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
2409762338dSopenharmony_ci    EXPECT_TRUE(fileFd >= 3);
2419762338dSopenharmony_ci    close(fileFd);
2429762338dSopenharmony_ci
2439762338dSopenharmony_ci    dirFd = open(UNLINKAT_TEST_DIR, O_RDONLY);
2449762338dSopenharmony_ci    EXPECT_TRUE(dirFd >= 3);
2459762338dSopenharmony_ci
2469762338dSopenharmony_ci    errno = 0;
2479762338dSopenharmony_ci    ret = unlinkat(dirFd, UNLINKAT_TEST_FILENAME, AT_REMOVEDIR);
2489762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
2499762338dSopenharmony_ci    EXPECT_EQ(errno, ENOTDIR);
2509762338dSopenharmony_ci    close(dirFd);
2519762338dSopenharmony_ci}