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 <cstring>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22
23 using namespace testing::ext;
24 using namespace std;
25
26 class Pread64Test : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp();
31 void TearDown();
32
33 private:
34 };
35
36 static const char* TEST_DATA = "Hello, world!";
37 static const size_t TEST_LEN = strlen(TEST_DATA);
38 static const char* TEST_FILE = "/data/local/tmp/test_file.txt";
39
SetUpTestCase()40 void Pread64Test::SetUpTestCase()
41 {
42 }
43
TearDownTestCase()44 void Pread64Test::TearDownTestCase()
45 {
46 }
47
SetUp()48 void Pread64Test::SetUp()
49 {
50 int fd = -1;
51 if (access(TEST_FILE, F_OK) == 0) {
52 remove(TEST_FILE);
53 }
54
55 fd = open(TEST_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
56 write(fd, TEST_DATA, strlen(TEST_DATA));
57 close(fd);
58 }
59
TearDown()60 void Pread64Test::TearDown()
61 {
62 (void)remove(TEST_FILE);
63 }
64
65 /*
66 * @tc.number : SUB_KERNEL_SYSCALL_PREAD64_0100
67 * @tc.name : Pread64ReadSuccess_0001
68 * @tc.desc : Test basic read functionality of pread64.
69 * @tc.size : MediumTest
70 * @tc.type : Function
71 * @tc.level : Level 1
72 */
HWTEST_F(Pread64Test, Pread64ReadSuccess_0001, Function | MediumTest | Level1)73 HWTEST_F(Pread64Test, Pread64ReadSuccess_0001, Function | MediumTest | Level1)
74 {
75 ssize_t size;
76 int midLen = TEST_LEN / 2;
77 char buf[20] = {0};
78 int fd = open(TEST_FILE, O_RDONLY);
79
80 // pread64 from file start
81 size = pread64(fd, buf, TEST_LEN, 0);
82 EXPECT_EQ(size, TEST_LEN);
83 EXPECT_STREQ(buf, TEST_DATA);
84
85 // pread64 from file middle
86 std::fill_n(buf, sizeof(buf), 0);
87 size = pread64(fd, buf, TEST_LEN, midLen);
88 EXPECT_EQ(size, TEST_LEN - midLen);
89 EXPECT_STREQ(buf, &TEST_DATA[midLen]);
90
91 // pread64 from file end
92 std::fill_n(buf, sizeof(buf), 0);
93 size = pread64(fd, buf, TEST_LEN, TEST_LEN);
94 EXPECT_EQ(size, 0);
95
96 close(fd);
97 }
98
99 /*
100 * @tc.number : SUB_KERNEL_SYSCALL_PREAD64_0200
101 * @tc.name : Pread64InvalidFdFailed_0002
102 * @tc.desc : Test using an invalid file descriptor, errno EBADF.
103 * @tc.size : MediumTest
104 * @tc.type : Function
105 * @tc.level : Level 2
106 */
HWTEST_F(Pread64Test, Pread64InvalidFdFailed_0002, Function | MediumTest | Level2)107 HWTEST_F(Pread64Test, Pread64InvalidFdFailed_0002, Function | MediumTest | Level2)
108 {
109 char buf[20] = {0};
110 ssize_t size;
111 errno = 0;
112 size = pread64(-1, buf, TEST_LEN, 0);
113 EXPECT_EQ(size, -1);
114 EXPECT_EQ(errno, EBADF);
115 }
116