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 <fcntl.h>
17 #include <poll.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include "test.h"
21
22 const char *path1 = "/data/polltest1.txt";
23 const char *str1 = "HelloWorld";
24 const char *path2 = "/data/polltest2.txt";
25 const char *str2 = "TestforPoll";
26
27 /*
28 * @tc.name : poll_0100
29 * @tc.desc : Waits for one of a set of file descriptors to become ready to perform I/O.
30 * @tc.level : Level 0
31 */
poll_0100(void)32 void poll_0100(void)
33 {
34 char buf[BUFSIZ];
35 struct pollfd fds[2];
36 int time_delay = 2000;
37
38 int fd1 = open(path1, O_WRONLY | O_CREAT, TEST_MODE);
39 if (fd1 < 0) {
40 t_error("%s open path1 failed\n", __func__);
41 }
42 write(fd1, str1, strlen(str1) + 1);
43 close(fd1);
44
45 int fd2 = open(path2, O_WRONLY | O_CREAT, TEST_MODE);
46 if (fd2 < 0) {
47 t_error("%s open path2 failed\n", __func__);
48 }
49 write(fd2, str2, strlen(str2) + 1);
50 close(fd2);
51
52 fds[0].fd = open(path1, O_RDONLY | O_NONBLOCK);
53 if (fds[0].fd < 0) {
54 t_error("%s open failed\n", __func__);
55 }
56 fds[0].events = POLLIN;
57
58 fds[1].fd = open(path2, O_RDONLY | O_NONBLOCK);
59 if (fds[1].fd < 0) {
60 t_error("%s open failed\n", __func__);
61 }
62 fds[1].events = POLLIN;
63
64 while (fds[0].events || fds[1].events) {
65 int ret = poll(fds, 2, time_delay);
66 if (ret <= 0) {
67 t_error("%s poll failed\n", __func__);
68 }
69 for (int i = 0; i < 2; i++) {
70 if (fds[i].revents) {
71 memset(buf, 0, BUFSIZ);
72 int result = read(fds[i].fd, buf, BUFSIZ);
73 if (result < 0) {
74 t_error("%s read failed\n", __func__);
75 } else if (result == 0) {
76 close(fds[i].fd);
77 fds[i].events = 0;
78 } else {
79 buf[result] = '\0';
80 if (i == 0) {
81 if (strcmp(buf, str1)) {
82 t_error("%s read buf is %s, not HelloWorld\n", __func__, buf);
83 }
84 } else {
85 if (strcmp(buf, str2)) {
86 t_error("%s read buf is %s, not TestforPoll\n", __func__, buf);
87 }
88 }
89 }
90 }
91 }
92 }
93
94 remove(path1);
95 remove(path2);
96 }
97
98 /*
99 * @tc.name : poll_0200
100 * @tc.desc : When the parameter __fds is invalid, test the return value of the function
101 * @tc.level : Level 2
102 */
poll_0200(void)103 void poll_0200(void)
104 {
105 int result = poll(NULL, 1, 0);
106 if (result != -1) {
107 t_error("%s poll should be failed\n", __func__);
108 }
109 }
110
main(int argc,char * argv[])111 int main(int argc, char *argv[])
112 {
113 poll_0100();
114 poll_0200();
115 return t_status;
116 }