1 /**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
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 <unistd.h>
17 #include <sys/uio.h>
18 #include <fcntl.h>
19 #include "functionalext.h"
20
21 /**
22 * @tc.name : pwritev_0100
23 * @tc.desc : Create a temporary file and centrally write the contents of multiple buffers to the file(success)
24 * @tc.level : Level 0
25 */
pwritev_0100(void)26 void pwritev_0100(void)
27 {
28 char buf1[] = "preadv";
29 char buf2[] = "and pwritev";
30 struct iovec iov[2];
31
32 iov[0].iov_base = buf1;
33 iov[0].iov_len = sizeof(buf1) / sizeof(char);
34 iov[1].iov_base = buf2;
35 iov[1].iov_len = sizeof(buf2) / sizeof(char);
36
37 int fd = open("pwritev_function_value.c", O_RDWR | O_CREAT, TEST_MODE);
38 int count = sizeof(iov) / sizeof(struct iovec);
39 int ret = pwritev(fd, iov, count, 0);
40 EXPECT_NE("pwritev_0100", ret, EOF);
41
42 close(fd);
43 ret = access("pwritev_function_value.c", F_OK);
44 if (ret == 0) {
45 ret = remove("pwritev_function_value.c");
46 EXPECT_EQ("pwritev_0100", ret, CMPFLAG);
47 }
48 }
49
50 /**
51 * @tc.name : pwritev_0200
52 * @tc.desc : Create a temporary file and centrally write the contents of multiple buffers to the file(failed)
53 * @tc.level : Level 2
54 */
pwritev_0200(void)55 void pwritev_0200(void)
56 {
57 char buf1[] = "pwritev1";
58 char buf2[] = "pwritev2";
59 struct iovec iov[2];
60
61 iov[0].iov_base = buf1;
62 iov[0].iov_len = sizeof(buf1) / sizeof(char);
63 iov[1].iov_base = buf2;
64 iov[1].iov_len = sizeof(buf2) / sizeof(char);
65
66 int fd = open("pwritev_function_value.c", O_RDWR | O_CREAT, TEST_MODE);
67 if (fd == -1) {
68 EXPECT_NE("pwritev_0200", fd, ERREXPECT);
69 return;
70 }
71 int count = sizeof(iov) / sizeof(struct iovec);
72 int ret = pwritev(fd, iov, count + 1, 0);
73 EXPECT_EQ("pwritev_0200", ret, EOF);
74
75 close(fd);
76 ret = access("pwritev_function_value.c", F_OK);
77 if (ret == 0) {
78 ret = remove("pwritev_function_value.c");
79 EXPECT_EQ("pwritev_0200", ret, CMPFLAG);
80 }
81 }
82
83 /**
84 * @tc.name : pwritev_0300
85 * @tc.desc : Write multiple buffer contents to invalid file descriptor
86 * @tc.level : Level 2
87 */
pwritev_0300(void)88 void pwritev_0300(void)
89 {
90 char buf1[] = "pwritev1";
91 char buf2[] = "pwritev2";
92 struct iovec iov[2];
93
94 iov[0].iov_base = buf1;
95 iov[0].iov_len = sizeof(buf1) / sizeof(char);
96 iov[1].iov_base = buf2;
97 iov[1].iov_len = sizeof(buf2) / sizeof(char);
98 int count = sizeof(iov) / sizeof(struct iovec);
99 int ret = pwritev(-1, iov, count, 0);
100 EXPECT_EQ("pwritev_0300", ret, EOF);
101 }
102
main(void)103 int main(void)
104 {
105 pwritev_0100();
106 pwritev_0200();
107 pwritev_0300();
108 return t_status;
109 }
110