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 <fcntl.h>
229762338dSopenharmony_ci#include <unistd.h>
239762338dSopenharmony_ci#include <gtest/gtest.h>
249762338dSopenharmony_ci#include <sys/file.h>
259762338dSopenharmony_ci#include <sys/stat.h>
269762338dSopenharmony_ci#include <sys/types.h>
279762338dSopenharmony_ci#include <sys/xattr.h>
289762338dSopenharmony_ci#include "securec.h"
299762338dSopenharmony_ci
309762338dSopenharmony_ciusing namespace testing::ext;
319762338dSopenharmony_ciusing namespace std;
329762338dSopenharmony_ci
339762338dSopenharmony_ciclass CopyFileRangeApiTest : public testing::Test {
349762338dSopenharmony_cipublic:
359762338dSopenharmony_ci    static void SetUpTestCase();
369762338dSopenharmony_ci    static void TearDownTestCase();
379762338dSopenharmony_ci    void SetUp();
389762338dSopenharmony_ci    void TearDown();
399762338dSopenharmony_ciprivate:
409762338dSopenharmony_ci};
419762338dSopenharmony_civoid CopyFileRangeApiTest::SetUp()
429762338dSopenharmony_ci{
439762338dSopenharmony_ci}
449762338dSopenharmony_civoid CopyFileRangeApiTest::TearDown()
459762338dSopenharmony_ci{
469762338dSopenharmony_ci}
479762338dSopenharmony_civoid CopyFileRangeApiTest::SetUpTestCase()
489762338dSopenharmony_ci{
499762338dSopenharmony_ci}
509762338dSopenharmony_civoid CopyFileRangeApiTest::TearDownTestCase()
519762338dSopenharmony_ci{
529762338dSopenharmony_ci}
539762338dSopenharmony_ci
549762338dSopenharmony_cistatic const int MAX_LEN = 128;
559762338dSopenharmony_cistatic const char *TEST_FILE = "/data/local/tmp/CopyFileRange.txt";
569762338dSopenharmony_cistatic const char *TEST_SRC_FILE = "/data/local/tmp/CopyFileRangeSource.txt";
579762338dSopenharmony_cistatic const char *TEST_DES_FILE = "/data/local/tmp/CopyFileRangeDestination.txt";
589762338dSopenharmony_cistatic const char *TEST_SRC_DIR = "/data/local/tmp/CopyFileRangeSource";
599762338dSopenharmony_cistatic const char *TEST_DES_DIR = "/data/local/tmp/CopyFileRangeDestination";
609762338dSopenharmony_cistatic const char *TEST_DATA = "Hello, world!";
619762338dSopenharmony_cimode_t MODE_0644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
629762338dSopenharmony_cimode_t MODE_0755 = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
639762338dSopenharmony_ci
649762338dSopenharmony_ci/*
659762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_COPYFILERANGE_0100
669762338dSopenharmony_ci * @tc.name   : CopyFileRangeCopyValidFdContentSuccess_0001
679762338dSopenharmony_ci * @tc.desc   : copy_file_range copy TEST_DATA  in fdOut with specify length from fdIn success.
689762338dSopenharmony_ci * @tc.size   : MediumTest
699762338dSopenharmony_ci * @tc.type   : Function
709762338dSopenharmony_ci * @tc.level  : Level 1
719762338dSopenharmony_ci */
729762338dSopenharmony_ciHWTEST_F(CopyFileRangeApiTest, CopyFileRangeCopyValidFdContentSuccess_0001, Function | MediumTest | Level1)
739762338dSopenharmony_ci{
749762338dSopenharmony_ci    ssize_t size;
759762338dSopenharmony_ci    loff_t offIn = 0;
769762338dSopenharmony_ci    loff_t offOut = 0;
779762338dSopenharmony_ci    int ret = 0;
789762338dSopenharmony_ci    char buffer[MAX_LEN];
799762338dSopenharmony_ci    int fdIn = open(TEST_SRC_FILE, O_RDWR | O_CREAT | O_TRUNC, MODE_0644);
809762338dSopenharmony_ci    EXPECT_TRUE(fdIn > 0);
819762338dSopenharmony_ci    ret = write(fdIn, TEST_DATA, strlen(TEST_DATA));
829762338dSopenharmony_ci    EXPECT_EQ(ret, strlen(TEST_DATA));
839762338dSopenharmony_ci    int fdOut = open(TEST_DES_FILE, O_RDWR | O_CREAT | O_TRUNC, MODE_0644);
849762338dSopenharmony_ci    EXPECT_TRUE(fdOut > 0);
859762338dSopenharmony_ci
869762338dSopenharmony_ci    size = copy_file_range(fdIn, &offIn, fdOut, &offOut, strlen(TEST_DATA), 0);
879762338dSopenharmony_ci    EXPECT_EQ(size, strlen(TEST_DATA));
889762338dSopenharmony_ci    ret = read(fdOut, buffer, strlen(TEST_DATA));
899762338dSopenharmony_ci    EXPECT_EQ(size, strlen(TEST_DATA));
909762338dSopenharmony_ci    EXPECT_STREQ(buffer, TEST_DATA);
919762338dSopenharmony_ci
929762338dSopenharmony_ci    close(fdIn);
939762338dSopenharmony_ci    close(fdOut);
949762338dSopenharmony_ci    remove(TEST_SRC_FILE);
959762338dSopenharmony_ci    remove(TEST_DES_FILE);
969762338dSopenharmony_ci}
979762338dSopenharmony_ci
989762338dSopenharmony_ci/*
999762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_COPYFILERANGE_0200
1009762338dSopenharmony_ci * @tc.name   : CopyFileRangeCopyInvalidFdContentFail_0002
1019762338dSopenharmony_ci * @tc.desc   : copy_file_range copy from invalid fd or copy into invalid fd fail, errno EBADF.
1029762338dSopenharmony_ci * @tc.size   : MediumTest
1039762338dSopenharmony_ci * @tc.type   : Function
1049762338dSopenharmony_ci * @tc.level  : Level 2
1059762338dSopenharmony_ci */
1069762338dSopenharmony_ciHWTEST_F(CopyFileRangeApiTest, CopyFileRangeCopyInvalidFdContentFail_0002, Function | MediumTest | Level2)
1079762338dSopenharmony_ci{
1089762338dSopenharmony_ci    ssize_t size;
1099762338dSopenharmony_ci    loff_t offIn = 0;
1109762338dSopenharmony_ci    loff_t offOut = 0;
1119762338dSopenharmony_ci    int invalidFd = -1;
1129762338dSopenharmony_ci    int fd = open(TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, MODE_0644);
1139762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
1149762338dSopenharmony_ci
1159762338dSopenharmony_ci    errno = 0;
1169762338dSopenharmony_ci    size = copy_file_range(invalidFd, &offIn, fd, &offOut, strlen(TEST_DATA), 0);
1179762338dSopenharmony_ci    EXPECT_EQ(size, -1);
1189762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
1199762338dSopenharmony_ci
1209762338dSopenharmony_ci    errno = 0;
1219762338dSopenharmony_ci    size = copy_file_range(fd, &offIn, invalidFd, &offOut, strlen(TEST_DATA), 0);
1229762338dSopenharmony_ci    EXPECT_EQ(size, -1);
1239762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
1249762338dSopenharmony_ci
1259762338dSopenharmony_ci    close(fd);
1269762338dSopenharmony_ci    remove(TEST_FILE);
1279762338dSopenharmony_ci}
1289762338dSopenharmony_ci
1299762338dSopenharmony_ci/*
1309762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_COPYFILERANGE_0300
1319762338dSopenharmony_ci * @tc.name   : CopyFileRangeCopyInvalidFlagFail_0003
1329762338dSopenharmony_ci * @tc.desc   : copy_file_range copy with invalid flag fail, errno EINVAL.
1339762338dSopenharmony_ci * @tc.size   : MediumTest
1349762338dSopenharmony_ci * @tc.type   : Function
1359762338dSopenharmony_ci * @tc.level  : Level 2
1369762338dSopenharmony_ci */
1379762338dSopenharmony_ciHWTEST_F(CopyFileRangeApiTest, CopyFileRangeCopyInvalidFlagFail_0003, Function | MediumTest | Level2)
1389762338dSopenharmony_ci{
1399762338dSopenharmony_ci    ssize_t size;
1409762338dSopenharmony_ci    loff_t offIn = 0;
1419762338dSopenharmony_ci    loff_t offOut = 0;
1429762338dSopenharmony_ci    int fdIn = open(TEST_SRC_FILE, O_RDWR | O_CREAT | O_TRUNC, MODE_0644);
1439762338dSopenharmony_ci    EXPECT_TRUE(fdIn > 0);
1449762338dSopenharmony_ci    int fdOut = open(TEST_DES_FILE, O_RDWR | O_CREAT | O_TRUNC, MODE_0644);
1459762338dSopenharmony_ci    EXPECT_TRUE(fdOut > 0);
1469762338dSopenharmony_ci
1479762338dSopenharmony_ci    errno = 0;
1489762338dSopenharmony_ci    size = copy_file_range(fdIn, &offIn, fdOut, &offOut, strlen(TEST_DATA), 1);
1499762338dSopenharmony_ci    EXPECT_EQ(size, -1);
1509762338dSopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1519762338dSopenharmony_ci
1529762338dSopenharmony_ci    close(fdIn);
1539762338dSopenharmony_ci    close(fdOut);
1549762338dSopenharmony_ci    remove(TEST_SRC_FILE);
1559762338dSopenharmony_ci    remove(TEST_DES_FILE);
1569762338dSopenharmony_ci}
1579762338dSopenharmony_ci
1589762338dSopenharmony_ci/*
1599762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_COPYFILERANGE_0400
1609762338dSopenharmony_ci * @tc.name   : CopyFileRangeCopyFdNoPermissionFail_0004
1619762338dSopenharmony_ci * @tc.desc   : copy_file_range copy TEST_DATA without correct permission in fdOut or fdIn fail, errno EBADF.
1629762338dSopenharmony_ci * @tc.size   : MediumTest
1639762338dSopenharmony_ci * @tc.type   : Function
1649762338dSopenharmony_ci * @tc.level  : Level 2
1659762338dSopenharmony_ci */
1669762338dSopenharmony_ciHWTEST_F(CopyFileRangeApiTest, CopyFileRangeCopyFdNoPermissionFail_0004, Function | MediumTest | Level2)
1679762338dSopenharmony_ci{
1689762338dSopenharmony_ci    ssize_t size;
1699762338dSopenharmony_ci    loff_t offIn = 0;
1709762338dSopenharmony_ci    loff_t offOut = 0;
1719762338dSopenharmony_ci    int result = 0;
1729762338dSopenharmony_ci    int fdIn = open(TEST_SRC_FILE, O_WRONLY | O_CREAT, MODE_0644);
1739762338dSopenharmony_ci    EXPECT_TRUE(fdIn > 0);
1749762338dSopenharmony_ci    result = write(fdIn, TEST_DATA, strlen(TEST_DATA));
1759762338dSopenharmony_ci    EXPECT_EQ(result, strlen(TEST_DATA));
1769762338dSopenharmony_ci    int fdOut = open(TEST_DES_FILE, O_RDONLY | O_CREAT, MODE_0644);
1779762338dSopenharmony_ci    EXPECT_TRUE(fdOut > 0);
1789762338dSopenharmony_ci    int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
1799762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
1809762338dSopenharmony_ci
1819762338dSopenharmony_ci    errno = 0;
1829762338dSopenharmony_ci    size = copy_file_range(fdIn, &offIn, fd, &offOut, strlen(TEST_DATA), 0);
1839762338dSopenharmony_ci    EXPECT_EQ(size, -1);
1849762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
1859762338dSopenharmony_ci
1869762338dSopenharmony_ci    errno = 0;
1879762338dSopenharmony_ci    size = copy_file_range(fd, &offIn, fdOut, &offOut, strlen(TEST_DATA), 0);
1889762338dSopenharmony_ci    EXPECT_EQ(size, -1);
1899762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
1909762338dSopenharmony_ci
1919762338dSopenharmony_ci    close(fd);
1929762338dSopenharmony_ci    close(fdIn);
1939762338dSopenharmony_ci    close(fdOut);
1949762338dSopenharmony_ci    remove(TEST_FILE);
1959762338dSopenharmony_ci    remove(TEST_SRC_FILE);
1969762338dSopenharmony_ci    remove(TEST_DES_FILE);
1979762338dSopenharmony_ci}
1989762338dSopenharmony_ci
1999762338dSopenharmony_ci/*
2009762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_COPYFILERANGE_0500
2019762338dSopenharmony_ci * @tc.name   : CopyFileRangeCopyOverOffsetContentFail_0005
2029762338dSopenharmony_ci * @tc.desc   : copy_file_range copy TEST_DATA  in fdOut with over offset length from fdIn .
2039762338dSopenharmony_ci * @tc.size   : MediumTest
2049762338dSopenharmony_ci * @tc.type   : Function
2059762338dSopenharmony_ci * @tc.level  : Level 2
2069762338dSopenharmony_ci */
2079762338dSopenharmony_ciHWTEST_F(CopyFileRangeApiTest, CopyFileRangeCopyOverOffsetContentFail_0005, Function | MediumTest | Level2)
2089762338dSopenharmony_ci{
2099762338dSopenharmony_ci    int ret;
2109762338dSopenharmony_ci    ssize_t size;
2119762338dSopenharmony_ci    loff_t offIn = 0;
2129762338dSopenharmony_ci    loff_t offOut = 0;
2139762338dSopenharmony_ci    if (access(TEST_SRC_DIR, F_OK) != 0) {
2149762338dSopenharmony_ci        ret = mkdir(TEST_SRC_DIR, MODE_0755);
2159762338dSopenharmony_ci        EXPECT_EQ(ret, 0);
2169762338dSopenharmony_ci    }
2179762338dSopenharmony_ci    int fdIn = open(TEST_SRC_DIR, O_RDONLY | O_DIRECTORY);
2189762338dSopenharmony_ci    EXPECT_NE(fdIn, -1);
2199762338dSopenharmony_ci    if (access(TEST_DES_DIR, F_OK) != 0) {
2209762338dSopenharmony_ci        ret = mkdir(TEST_DES_DIR, MODE_0755);
2219762338dSopenharmony_ci        EXPECT_EQ(ret, 0);
2229762338dSopenharmony_ci    }
2239762338dSopenharmony_ci    int fdOut = open(TEST_DES_DIR, O_RDONLY | O_DIRECTORY);
2249762338dSopenharmony_ci    EXPECT_NE(fdOut, -1);
2259762338dSopenharmony_ci
2269762338dSopenharmony_ci    errno = 0;
2279762338dSopenharmony_ci    size = copy_file_range(fdIn, &offIn, fdOut, &offOut, strlen(TEST_DATA), 0);
2289762338dSopenharmony_ci    EXPECT_EQ(size, -1);
2299762338dSopenharmony_ci    EXPECT_EQ(errno, EISDIR);
2309762338dSopenharmony_ci
2319762338dSopenharmony_ci    close(fdIn);
2329762338dSopenharmony_ci    close(fdOut);
2339762338dSopenharmony_ci    remove(TEST_SRC_DIR);
2349762338dSopenharmony_ci    remove(TEST_DES_DIR);
2359762338dSopenharmony_ci}