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
30 using namespace testing::ext;
31
32 static const char *TEST_FILE = "/data/local/tmp/read_head_test.txt";
33 static const char *TEST_DATA = "Hello world!";
34 static const int TEST_DATA_LEN = strlen(TEST_DATA);
35
36 class HatsReadaheadTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 private:
43 };
SetUp()44 void HatsReadaheadTest::SetUp()
45 {
46 }
TearDown()47 void HatsReadaheadTest::TearDown()
48 {
49 }
SetUpTestCase()50 void HatsReadaheadTest::SetUpTestCase()
51 {
52 int fd = open(TEST_FILE, O_WRONLY | O_CREAT, 0644);
53 write(fd, TEST_DATA, TEST_DATA_LEN);
54 close(fd);
55 }
TearDownTestCase()56 void HatsReadaheadTest::TearDownTestCase()
57 {
58 (void)remove(TEST_FILE);
59 }
60
61 /*
62 * @tc.number : SUB_KERNEL_SYSCALL_READAHEAD_0100
63 * @tc.name : ReadaheadFileSuccess_0001
64 * @tc.desc : Readahead on a valid fd success.
65 * @tc.size : MediumTest
66 * @tc.type : Function
67 * @tc.level : Level 1
68 */
HWTEST_F(HatsReadaheadTest, ReadaheadFileSuccess_0001, Function | MediumTest | Level1)69 HWTEST_F(HatsReadaheadTest, ReadaheadFileSuccess_0001, Function | MediumTest | Level1)
70 {
71 int fd = open(TEST_FILE, O_RDONLY);
72 EXPECT_TRUE(fd > 0);
73 ssize_t size = readahead(fd, 0, TEST_DATA_LEN);
74 EXPECT_EQ(size, 0);
75
76 size = readahead(fd, TEST_DATA_LEN, TEST_DATA_LEN);
77 EXPECT_EQ(size, 0);
78 close(fd);
79 }
80
81 /*
82 * @tc.number : SUB_KERNEL_SYSCALL_READAHEAD_0200
83 * @tc.name : ReadaheadInvalidFdFailed_0002
84 * @tc.desc : Readahead read with invalid fd failed.
85 * @tc.size : MediumTest
86 * @tc.type : Function
87 * @tc.level : Level 2
88 */
HWTEST_F(HatsReadaheadTest, ReadaheadInvalidFdFailed_0002, Function | MediumTest | Level2)89 HWTEST_F(HatsReadaheadTest, ReadaheadInvalidFdFailed_0002, Function | MediumTest | Level2)
90 {
91 errno = 0;
92 ssize_t size = readahead(-1, 0, TEST_DATA_LEN);
93 EXPECT_EQ(size, -1);
94 EXPECT_EQ(errno, EBADF);
95
96 int fd = open(TEST_FILE, O_WRONLY);
97 EXPECT_TRUE(fd > 0);
98
99 errno = 0;
100 size = readahead(fd, 0, TEST_DATA_LEN);
101 EXPECT_EQ(size, -1);
102 EXPECT_EQ(errno, EBADF);
103 }
104