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 #define BUFFER_SIZE 8
22
23 /**
24 * @tc.name : preadv_0100
25 * @tc.desc : Create and open the preadv_function_value.c file, write data to the file, call the preadv function
26 * to read the data in the file from the beginning of the file, close and delete the created file
27 * @tc.level : Level 1
28 */
preadv_0100(void)29 void preadv_0100(void)
30 {
31 char buf1[BUFFER_SIZE];
32 char buf2[BUFFER_SIZE];
33 struct iovec iov[2];
34
35 memset(buf1, '\0', BUFFER_SIZE);
36 memset(buf2, '\0', BUFFER_SIZE);
37
38 iov[0].iov_base = buf1;
39 iov[0].iov_len = sizeof(buf1) / sizeof(char);
40 iov[1].iov_base = buf2;
41 iov[1].iov_len = sizeof(buf2) / sizeof(char);
42
43 int fd = open("./preadv_function_value.c", O_RDWR | O_CREAT, TEST_MODE);
44 if (fd == -1) {
45 EXPECT_NE("preadv_0100", fd, ERREXPECT);
46 return;
47 }
48 char data[] = "preadv test";
49 int arraysize = sizeof(data) / sizeof(char);
50 int ret = write(fd, data, sizeof(data));
51 if (ret < 0) {
52 EXPECT_NE("preadv_0100", ret, EOF);
53 return;
54 }
55 int count = sizeof(iov) / sizeof(struct iovec);
56 ret = preadv(fd, iov, count, 0);
57 EXPECT_EQ("preadv_0100", ret, arraysize);
58
59 close(fd);
60 ret = access("preadv_function_value.c", F_OK);
61 if (ret != -1) {
62 ret = remove("./preadv_function_value.c");
63 EXPECT_EQ("preadv_0100", ret, CMPFLAG);
64 }
65 }
66
67 /**
68 * @tc.name : preadv_0200
69 * @tc.desc : Pass in the abnormal file descriptor and call the preadv function to read the data(failed)
70 * @tc.level : Level 2
71 */
preadv_0200(void)72 void preadv_0200(void)
73 {
74 char buf1[8];
75 char buf2[8];
76 struct iovec iov[2];
77
78 iov[0].iov_base = buf1;
79 iov[0].iov_len = sizeof(buf1) / sizeof(char);
80 iov[1].iov_base = buf2;
81 iov[1].iov_len = sizeof(buf2) / sizeof(char);
82 int count = sizeof(iov) / sizeof(struct iovec);
83 int ret = preadv(-1, iov, count, 0);
84 EXPECT_EQ("preadv_0200", ret, EOF);
85 }
86
main(void)87 int main(void)
88 {
89 preadv_0100();
90 preadv_0200();
91 return t_status;
92 }
93