1/*
2 * Copyright (C) 2024 HiHope Open Source Organization.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <cerrno>
17#include <cstdio>
18#include <cstdlib>
19#include <string>
20#include <vector>
21#include <fcntl.h>
22#include <unistd.h>
23#include <arpa/inet.h>
24#include <gtest/gtest.h>
25#include <netinet/in.h>
26#include <sys/stat.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29
30using namespace testing::ext;
31
32class HatsSyncFileRangeTest : public testing::Test {
33public:
34static void SetUpTestCase();
35static void TearDownTestCase();
36void SetUp();
37void TearDown();
38private:
39};
40void HatsSyncFileRangeTest::SetUp()
41{
42}
43void HatsSyncFileRangeTest::TearDown()
44{
45}
46void HatsSyncFileRangeTest::SetUpTestCase()
47{
48}
49void HatsSyncFileRangeTest::TearDownTestCase()
50{
51}
52
53static const char *SYNC_RANGE_TEST_FILE = "/data/local/tmp/trySync.txt";
54static const char *TEST_DATA = "Hello sync!";
55static const int TEST_DATA_LEN = strlen(TEST_DATA);
56static const int FILE_OFFSET = 0;
57
58/*
59 * @tc.number : SUB_KERNEL_SYSCALL_SYNCFILERANGE_0100
60 * @tc.name   : SyncFileRangeFileToDiskSuccess_0001
61 * @tc.desc   : sync_file_range write file from buffer to disk success.
62 * @tc.size   : MediumTest
63 * @tc.type   : Function
64 * @tc.level  : Level 1
65 */
66HWTEST_F(HatsSyncFileRangeTest, SyncFileRangeFileToDiskSuccess_0001, Function | MediumTest | Level1)
67{
68    int fd;
69    int ret;
70
71    fd = open(SYNC_RANGE_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
72    EXPECT_TRUE(fd > 0);
73
74    ret = sync_file_range(fd, FILE_OFFSET, TEST_DATA_LEN, 0);
75    EXPECT_EQ(ret, 0);
76
77    close(fd);
78    remove(SYNC_RANGE_TEST_FILE);
79}
80
81/*
82 * @tc.number : SUB_KERNEL_SYSCALL_SYNCFILERANGE_0200
83 * @tc.name   : SyncFileRangeInvalidFdFail_0002
84 * @tc.desc   : sync_file_range sync invalid fd to disk fail, errno EBADF.
85 * @tc.size   : MediumTest
86 * @tc.type   : Function
87 * @tc.level  : Level 2
88 */
89HWTEST_F(HatsSyncFileRangeTest, SyncFileRangeInvalidFdFail_0002, Function | MediumTest | Level2)
90{
91    errno = 0;
92    int ret = sync_file_range(-1, FILE_OFFSET, TEST_DATA_LEN, 0);
93    EXPECT_EQ(ret, -1);
94    EXPECT_EQ(errno, EBADF);
95}
96
97/*
98 * @tc.number : SUB_KERNEL_SYSCALL_SYNCFILERANGE_0300
99 * @tc.name   : SyncFileRangeUsingInvalidFlagFail_0003
100 * @tc.desc   : sync_file_range sync file to disk using invalid flag fail.
101 * @tc.size   : MediumTest
102 * @tc.type   : Function
103 * @tc.level  : Level 2
104 */
105HWTEST_F(HatsSyncFileRangeTest, SyncFileRangeInvalidOffsetFail_0003, Function | MediumTest | Level2)
106{
107    int ret;
108    int fd = open(SYNC_RANGE_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
109    EXPECT_TRUE(fd > 0);
110
111    errno = 0;
112    ret = sync_file_range(fd, FILE_OFFSET, TEST_DATA_LEN, -1);
113    EXPECT_EQ(ret, -1);
114    EXPECT_EQ(errno, EINVAL);
115
116    close(fd);
117    remove(SYNC_RANGE_TEST_FILE);
118}
119
120/*
121 * @tc.number : SUB_KERNEL_SYSCALL_SYNCFILERANGE_0400
122 * @tc.name   : SyncFileRangeInvalidNbytesFail_0004
123 * @tc.desc   : sync_file_range sync file to disk when nbytes is invalid fail.
124 * @tc.size   : MediumTest
125 * @tc.type   : Function
126 * @tc.level  : Level 2
127 */
128HWTEST_F(HatsSyncFileRangeTest, SyncFileRangeInvalidNbytesFail_0004, Function | MediumTest | Level2)
129{
130    int fd = open(SYNC_RANGE_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
131    EXPECT_TRUE(fd > 0);
132    ssize_t size = write(fd, TEST_DATA, TEST_DATA_LEN);
133    EXPECT_EQ(size, TEST_DATA_LEN);
134
135    errno = 0;
136    int ret = sync_file_range(fd, FILE_OFFSET, -1, 0);
137    EXPECT_EQ(ret, -1);
138    EXPECT_EQ(errno, EINVAL);
139
140    close(fd);
141    remove(SYNC_RANGE_TEST_FILE);
142}
143