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 <cstdio>
17 #include <cstdlib>
18 #include <string>
19 #include <vector>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <gtest/gtest.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/uio.h>
26 #include "securec.h"
27
28 using namespace testing::ext;
29 using namespace std;
30
31 static const int MAX_LEN = 128;
32 static const char *TEST_FILE = "/data/local/tmp/test_file.txt";
33
34 class Pwritev64ApiTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp();
39 void TearDown();
40 private:
41 };
SetUp()42 void Pwritev64ApiTest::SetUp()
43 {
44 }
TearDown()45 void Pwritev64ApiTest::TearDown()
46 {
47 }
SetUpTestCase()48 void Pwritev64ApiTest::SetUpTestCase()
49 {
50 }
TearDownTestCase()51 void Pwritev64ApiTest::TearDownTestCase()
52 {
53 }
54
55 /*
56 * @tc.number : SUB_KERNEL_SYSCALL_PWRITEV64_0100
57 * @tc.name : Pwritev64NormalWriteSuccess_0001
58 * @tc.desc : pwritev64 should write data to the file at the specified offset.
59 * @tc.size : MediumTest
60 * @tc.type : Function
61 * @tc.level : Level 1
62 */
HWTEST_F(Pwritev64ApiTest, Pwritev64NormalWriteSuccess_0001, Function | MediumTest | Level1)63 HWTEST_F(Pwritev64ApiTest, Pwritev64NormalWriteSuccess_0001, Function | MediumTest | Level1)
64 {
65 char data1[] = "Hello";
66 char data2[] = "World!";
67 struct iovec iov[2] = {
68 {
69 .iov_base = data1,
70 .iov_len = sizeof(data1),
71 }, {
72 .iov_base = data2,
73 .iov_len = sizeof(data2),
74 }
75 };
76
77 int fd = open(TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
78 ssize_t size = pwritev64(fd, iov, 2, 0);
79 EXPECT_EQ(size, sizeof(data1) + sizeof(data2));
80
81 close(fd);
82 unlink(TEST_FILE);
83 }
84
85 /*
86 * @tc.number : SUB_KERNEL_SYSCALL_PWRITEV64_0200
87 * @tc.name : Pwritev64InvalidFdFailed_0002
88 * @tc.desc : pwritev64 should return -1 and set errno to EBADF for invalid fd.
89 * @tc.size : MediumTest
90 * @tc.type : Function
91 * @tc.level : Level 2
92 */
HWTEST_F(Pwritev64ApiTest, Pwritev64InvalidFdFailed_0002, Function | MediumTest | Level2)93 HWTEST_F(Pwritev64ApiTest, Pwritev64InvalidFdFailed_0002, Function | MediumTest | Level2)
94 {
95 char buffer[MAX_LEN] = { 0 };
96 struct iovec iov = {
97 .iov_base = buffer,
98 .iov_len = sizeof(buffer),
99 };
100 errno = 0;
101 ssize_t size = pwritev64(-1, &iov, 1, 0);
102 EXPECT_EQ(size, -1);
103 EXPECT_EQ(errno, EBADF);
104 }
105