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_ci
329762338dSopenharmony_ciusing namespace testing::ext;
339762338dSopenharmony_ci
349762338dSopenharmony_ciclass HatsUtimensatTest : public testing::Test {
359762338dSopenharmony_cipublic:
369762338dSopenharmony_cistatic void SetUpTestCase();
379762338dSopenharmony_cistatic void TearDownTestCase();
389762338dSopenharmony_civoid SetUp();
399762338dSopenharmony_civoid TearDown();
409762338dSopenharmony_ciprivate:
419762338dSopenharmony_ci};
429762338dSopenharmony_civoid HatsUtimensatTest::SetUp()
439762338dSopenharmony_ci{
449762338dSopenharmony_ci}
459762338dSopenharmony_civoid HatsUtimensatTest::TearDown()
469762338dSopenharmony_ci{
479762338dSopenharmony_ci}
489762338dSopenharmony_civoid HatsUtimensatTest::SetUpTestCase()
499762338dSopenharmony_ci{
509762338dSopenharmony_ci}
519762338dSopenharmony_civoid HatsUtimensatTest::TearDownTestCase()
529762338dSopenharmony_ci{
539762338dSopenharmony_ci}
549762338dSopenharmony_ci
559762338dSopenharmony_cistatic const char *UTIMENSAT_TEST_FILE = "/data/local/tmp/tryUtimensat.txt";
569762338dSopenharmony_cistatic const int ACCESS_TIME_SEC = 10;
579762338dSopenharmony_cistatic const int ACCESS_TIME_NSEC = 0;
589762338dSopenharmony_cistatic const int MODIFICATION_TIME_SEC = 11;
599762338dSopenharmony_cistatic const int MODIFICATION_TIME_NSEC = 0;
609762338dSopenharmony_ci
619762338dSopenharmony_ci/*
629762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_UTIMENSAT_0100
639762338dSopenharmony_ci * @tc.name   : UtimensatChangeFileTimeSuccess_0001
649762338dSopenharmony_ci * @tc.desc   : utimensat change file access time and modification time success.
659762338dSopenharmony_ci * @tc.size   : MediumTest
669762338dSopenharmony_ci * @tc.type   : Function
679762338dSopenharmony_ci * @tc.level  : Level 1
689762338dSopenharmony_ci */
699762338dSopenharmony_ciHWTEST_F(HatsUtimensatTest, UtimensatChangeFileTimeSuccess_0001, Function | MediumTest | Level1)
709762338dSopenharmony_ci{
719762338dSopenharmony_ci    int ret;
729762338dSopenharmony_ci    int fd;
739762338dSopenharmony_ci    struct timespec newTime[2];
749762338dSopenharmony_ci    struct stat fileStat1;
759762338dSopenharmony_ci    struct stat fileStat2;
769762338dSopenharmony_ci
779762338dSopenharmony_ci    fd = open(UTIMENSAT_TEST_FILE, O_RDONLY);
789762338dSopenharmony_ci    if (fd != -1) {
799762338dSopenharmony_ci        ret = remove(UTIMENSAT_TEST_FILE);
809762338dSopenharmony_ci        EXPECT_TRUE(ret == 0);
819762338dSopenharmony_ci    }
829762338dSopenharmony_ci    close(fd);
839762338dSopenharmony_ci
849762338dSopenharmony_ci    fd = open(UTIMENSAT_TEST_FILE, O_RDONLY | O_CREAT | O_TRUNC, 0644);
859762338dSopenharmony_ci    EXPECT_TRUE(fd >= 3);
869762338dSopenharmony_ci    close(fd);
879762338dSopenharmony_ci
889762338dSopenharmony_ci    ret = stat(UTIMENSAT_TEST_FILE, &fileStat1);
899762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
909762338dSopenharmony_ci
919762338dSopenharmony_ci    //access time
929762338dSopenharmony_ci    newTime[0].tv_sec = ACCESS_TIME_SEC;
939762338dSopenharmony_ci    newTime[0].tv_nsec = ACCESS_TIME_NSEC;
949762338dSopenharmony_ci    //modification time
959762338dSopenharmony_ci    newTime[1].tv_sec = MODIFICATION_TIME_SEC;
969762338dSopenharmony_ci    newTime[1].tv_nsec = MODIFICATION_TIME_NSEC;
979762338dSopenharmony_ci
989762338dSopenharmony_ci    ret = utimensat(AT_FDCWD, UTIMENSAT_TEST_FILE, newTime, 0);
999762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
1009762338dSopenharmony_ci
1019762338dSopenharmony_ci    ret = stat(UTIMENSAT_TEST_FILE, &fileStat2);
1029762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
1039762338dSopenharmony_ci
1049762338dSopenharmony_ci    ret = (fileStat1.st_atime != fileStat2.st_atime);
1059762338dSopenharmony_ci    EXPECT_TRUE(ret == 1);
1069762338dSopenharmony_ci    ret = (fileStat1.st_mtime != fileStat2.st_mtime);
1079762338dSopenharmony_ci    EXPECT_TRUE(ret == 1);
1089762338dSopenharmony_ci}
1099762338dSopenharmony_ci
1109762338dSopenharmony_ci/*
1119762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_UTIMENSAT_0200
1129762338dSopenharmony_ci * @tc.name   : UtimensatInvalidTimeFail_0002
1139762338dSopenharmony_ci * @tc.desc   : utimensat set time using invalid time value fail.
1149762338dSopenharmony_ci * @tc.size   : MediumTest
1159762338dSopenharmony_ci * @tc.type   : Function
1169762338dSopenharmony_ci * @tc.level  : Level 2
1179762338dSopenharmony_ci */
1189762338dSopenharmony_ciHWTEST_F(HatsUtimensatTest, UtimensatInvalidTimeFail_0002, Function | MediumTest | Level2)
1199762338dSopenharmony_ci{
1209762338dSopenharmony_ci    int ret;
1219762338dSopenharmony_ci    int fd;
1229762338dSopenharmony_ci    struct timespec newTime[2];
1239762338dSopenharmony_ci    struct stat fileStat1;
1249762338dSopenharmony_ci
1259762338dSopenharmony_ci    fd = open(UTIMENSAT_TEST_FILE, O_RDONLY);
1269762338dSopenharmony_ci    if (fd != -1) {
1279762338dSopenharmony_ci        ret = remove(UTIMENSAT_TEST_FILE);
1289762338dSopenharmony_ci        EXPECT_TRUE(ret == 0);
1299762338dSopenharmony_ci    }
1309762338dSopenharmony_ci    close(fd);
1319762338dSopenharmony_ci
1329762338dSopenharmony_ci    fd = open(UTIMENSAT_TEST_FILE, O_RDONLY | O_CREAT | O_TRUNC, 0644);
1339762338dSopenharmony_ci    EXPECT_TRUE(fd >= 3);
1349762338dSopenharmony_ci    close(fd);
1359762338dSopenharmony_ci
1369762338dSopenharmony_ci    ret = stat(UTIMENSAT_TEST_FILE, &fileStat1);
1379762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
1389762338dSopenharmony_ci
1399762338dSopenharmony_ci    //access time
1409762338dSopenharmony_ci    newTime[0].tv_sec = -1;
1419762338dSopenharmony_ci    newTime[0].tv_nsec = -1;
1429762338dSopenharmony_ci    //modification time
1439762338dSopenharmony_ci    newTime[1].tv_sec = -1;
1449762338dSopenharmony_ci    newTime[1].tv_nsec = -1;
1459762338dSopenharmony_ci
1469762338dSopenharmony_ci    errno = 0;
1479762338dSopenharmony_ci    ret = utimensat(AT_FDCWD, UTIMENSAT_TEST_FILE, newTime, 0);
1489762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
1499762338dSopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1509762338dSopenharmony_ci}
1519762338dSopenharmony_ci
1529762338dSopenharmony_ci/*
1539762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_UTIMENSAT_0300
1549762338dSopenharmony_ci * @tc.name   : UtimensatSetTimeOfNonexistFileFail_0003
1559762338dSopenharmony_ci * @tc.desc   : utimensat set time of a non-exist file fail.
1569762338dSopenharmony_ci * @tc.size   : MediumTest
1579762338dSopenharmony_ci * @tc.type   : Function
1589762338dSopenharmony_ci * @tc.level  : Level 2
1599762338dSopenharmony_ci */
1609762338dSopenharmony_ciHWTEST_F(HatsUtimensatTest, UtimensatSetTimeOfNonexistFileFail_0003, Function | MediumTest | Level2)
1619762338dSopenharmony_ci{
1629762338dSopenharmony_ci    int ret;
1639762338dSopenharmony_ci    int fd;
1649762338dSopenharmony_ci    struct timespec newTime[2];
1659762338dSopenharmony_ci
1669762338dSopenharmony_ci    fd = open(UTIMENSAT_TEST_FILE, O_RDONLY);
1679762338dSopenharmony_ci    if (fd != -1) {
1689762338dSopenharmony_ci        ret = remove(UTIMENSAT_TEST_FILE);
1699762338dSopenharmony_ci        EXPECT_TRUE(ret == 0);
1709762338dSopenharmony_ci    }
1719762338dSopenharmony_ci    close(fd);
1729762338dSopenharmony_ci
1739762338dSopenharmony_ci    //access time
1749762338dSopenharmony_ci    newTime[0].tv_sec = ACCESS_TIME_SEC;
1759762338dSopenharmony_ci    newTime[0].tv_nsec = ACCESS_TIME_NSEC;
1769762338dSopenharmony_ci    //modification time
1779762338dSopenharmony_ci    newTime[1].tv_sec = MODIFICATION_TIME_SEC;
1789762338dSopenharmony_ci    newTime[1].tv_nsec = MODIFICATION_TIME_NSEC;
1799762338dSopenharmony_ci
1809762338dSopenharmony_ci    errno = 0;
1819762338dSopenharmony_ci    ret = utimensat(AT_FDCWD, UTIMENSAT_TEST_FILE, newTime, 0);
1829762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
1839762338dSopenharmony_ci    EXPECT_EQ(errno, ENOENT);
1849762338dSopenharmony_ci}
1859762338dSopenharmony_ci
1869762338dSopenharmony_ci/*
1879762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_UTIMENSAT_0400
1889762338dSopenharmony_ci * @tc.name   : UtimensatInvalidFlagFail_0004
1899762338dSopenharmony_ci * @tc.desc   : utimensat change time with invalid flag fail.
1909762338dSopenharmony_ci * @tc.size   : MediumTest
1919762338dSopenharmony_ci * @tc.type   : Function
1929762338dSopenharmony_ci * @tc.level  : Level 2
1939762338dSopenharmony_ci */
1949762338dSopenharmony_ciHWTEST_F(HatsUtimensatTest, UtimensatInvalidFlagFail_0004, Function | MediumTest | Level2)
1959762338dSopenharmony_ci{
1969762338dSopenharmony_ci    int ret;
1979762338dSopenharmony_ci    int fd;
1989762338dSopenharmony_ci    struct timespec newTime[2];
1999762338dSopenharmony_ci
2009762338dSopenharmony_ci    fd = open(UTIMENSAT_TEST_FILE, O_RDONLY);
2019762338dSopenharmony_ci    if (fd != -1) {
2029762338dSopenharmony_ci        ret = remove(UTIMENSAT_TEST_FILE);
2039762338dSopenharmony_ci        EXPECT_TRUE(ret == 0);
2049762338dSopenharmony_ci    }
2059762338dSopenharmony_ci    close(fd);
2069762338dSopenharmony_ci
2079762338dSopenharmony_ci    fd = open(UTIMENSAT_TEST_FILE, O_RDONLY | O_CREAT | O_TRUNC, 0644);
2089762338dSopenharmony_ci    EXPECT_TRUE(fd >= 3);
2099762338dSopenharmony_ci    close(fd);
2109762338dSopenharmony_ci
2119762338dSopenharmony_ci    //access time
2129762338dSopenharmony_ci    newTime[0].tv_sec = ACCESS_TIME_SEC;
2139762338dSopenharmony_ci    newTime[0].tv_nsec = ACCESS_TIME_NSEC;
2149762338dSopenharmony_ci    //modification time
2159762338dSopenharmony_ci    newTime[1].tv_sec = MODIFICATION_TIME_SEC;
2169762338dSopenharmony_ci    newTime[1].tv_nsec = MODIFICATION_TIME_NSEC;
2179762338dSopenharmony_ci
2189762338dSopenharmony_ci    errno = 0;
2199762338dSopenharmony_ci    ret = utimensat(AT_FDCWD, UTIMENSAT_TEST_FILE, newTime, -1);
2209762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
2219762338dSopenharmony_ci    EXPECT_EQ(errno, EINVAL);
2229762338dSopenharmony_ci}
223