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 #include <errno.h>
16 #include <netinet/in.h>
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #include "fortify_test.h"
23 #include "test.h"
24
25 #define SCTP_IP_LOOPBACK htonl(0x7f000001)
26 #define BUF_SIZE_10 (10)
27 #define BUF_SIZE_100 (100)
28
29 /**
30 * @tc.name : send_and_recv_0010
31 * @tc.desc : test send and recv normal condition
32 * @tc.level : Level 0
33 */
send_and_recv_0010(void)34 static void send_and_recv_0010(void)
35 {
36 char buf[BUF_SIZE_100];
37 int sockets[2];
38
39 TEST(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);
40 TEST(send(sockets[1], "x", 1, 0) == 1);
41 TEST(recv(sockets[0], buf, sizeof buf, 0) == 1);
42 TEST(buf[0]=='x', "'%c'\n", buf[0]);
43
44 return;
45 }
46
47 /**
48 * @tc.name : send_0010
49 * @tc.desc : test send suppress compiler optimizations
50 * @tc.level : Level 2
51 */
send_0010(void)52 static void send_0010(void)
53 {
54 struct sigaction sigabrt = {
55 .sa_handler = SignalHandler,
56 };
57 sigaction(SIGABRT, &sigabrt, NULL);
58
59 size_t data_len = atoi("11");
60 char buf[BUF_SIZE_10];
61 int status;
62 int pid = fork();
63 switch (pid) {
64 case -1:
65 t_error("fork failed: %s\n", strerror(errno));
66 break;
67 case 0:
68 send(0, buf, data_len, 0);
69 exit(0);
70 default:
71 waitpid(pid, &status, WUNTRACED);
72 TEST(WIFEXITED(status) == 0);
73 TEST(WIFSTOPPED(status) == 1);
74 TEST(WSTOPSIG(status) == SIGSTOP);
75 kill(pid, SIGCONT);
76 break;
77 }
78
79 return;
80 }
81
82 /**
83 * @tc.name : recv_0010
84 * @tc.desc : test recv suppress compiler optimizations
85 * @tc.level : Level 2
86 */
recv_0010(void)87 static void recv_0010(void)
88 {
89 struct sigaction sigabrt = {
90 .sa_handler = SignalHandler,
91 };
92 sigaction(SIGABRT, &sigabrt, NULL);
93
94 size_t data_len = atoi("11");
95 char buf[BUF_SIZE_10];
96 int status;
97 int pid = fork();
98 switch (pid) {
99 case -1:
100 t_error("fork failed: %s\n", strerror(errno));
101 break;
102 case 0:
103 recv(0, buf, data_len, 0);
104 exit(0);
105 default:
106 waitpid(pid, &status, WUNTRACED);
107 TEST(WIFEXITED(status) == 0);
108 TEST(WIFSTOPPED(status) == 1);
109 TEST(WSTOPSIG(status) == SIGSTOP);
110 kill(pid, SIGCONT);
111 break;
112 }
113
114 return;
115 }
116
117 /**
118 * @tc.name : sendto_and_recvfrom_0010
119 * @tc.desc : test sendto and recvfrom normal condition
120 * @tc.level : Level 0
121 */
sendto_and_recvfrom_0010(void)122 static void sendto_and_recvfrom_0010(void)
123 {
124 struct sockaddr_in sa = { .sin_family = AF_INET };
125 int sendFd, recvFd, t;
126 char buf[BUF_SIZE_100];
127
128 TEST((recvFd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) >= 0);
129 TEST(bind(recvFd, (void *)&sa, sizeof sa)==0);
130 TEST(getsockname(recvFd, (void *)&sa, (socklen_t[]){sizeof sa}) == 0);
131
132 TEST(setsockopt(recvFd, SOL_SOCKET, SO_RCVTIMEO,
133 &(struct timeval){.tv_usec=1}, sizeof(struct timeval)) == 0);
134
135 TEST((sendFd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) >= 0);
136 sa.sin_addr.s_addr = SCTP_IP_LOOPBACK;
137 TEST(sendto(sendFd, "x", 1, 0, (void *)&sa, sizeof sa) == 1);
138 TEST(recvfrom(recvFd, buf, sizeof buf, 0, (void *)&sa, (socklen_t[]){sizeof sa}) == 1);
139 TEST(buf[0]=='x', "'%c'\n", buf[0]);
140
141 return;
142 }
143
144 /**
145 * @tc.name : sendto_0010
146 * @tc.desc : test sendto suppress compiler optimizations
147 * @tc.level : Level 2
148 */
sendto_0010(void)149 static void sendto_0010(void)
150 {
151 struct sigaction sigabrt = {
152 .sa_handler = SignalHandler,
153 };
154 sigaction(SIGABRT, &sigabrt, NULL);
155
156 size_t data_len = atoi("11");
157 char buf[BUF_SIZE_10];
158 int status;
159 int pid = fork();
160 switch (pid) {
161 case -1:
162 t_error("fork failed: %s\n", strerror(errno));
163 break;
164 case 0:
165 sendto(0, buf, data_len, 0, NULL, 0);
166 exit(0);
167 default:
168 waitpid(pid, &status, WUNTRACED);
169 TEST(WIFEXITED(status) == 0);
170 TEST(WIFSTOPPED(status) == 1);
171 TEST(WSTOPSIG(status) == SIGSTOP);
172 kill(pid, SIGCONT);
173 break;
174 }
175
176 return;
177 }
178
179 /**
180 * @tc.name : recvfrom_0010
181 * @tc.desc : test recv suppress compiler optimizations
182 * @tc.level : Level 2
183 */
recvfrom_0010(void)184 static void recvfrom_0010(void)
185 {
186 struct sigaction sigabrt = {
187 .sa_handler = SignalHandler,
188 };
189 sigaction(SIGABRT, &sigabrt, NULL);
190
191 size_t data_len = atoi("11");
192 char buf[BUF_SIZE_10];
193 int status;
194 int pid = fork();
195 switch (pid) {
196 case -1:
197 t_error("fork failed: %s\n", strerror(errno));
198 break;
199 case 0:
200 recvfrom(0, buf, data_len, 0, NULL, 0);
201 exit(0);
202 default:
203 waitpid(pid, &status, WUNTRACED);
204 TEST(WIFEXITED(status) == 0);
205 TEST(WIFSTOPPED(status) == 1);
206 TEST(WSTOPSIG(status) == SIGSTOP);
207 kill(pid, SIGCONT);
208 break;
209 }
210
211 return;
212 }
213
main(int argc,char * argv[])214 int main(int argc, char *argv[]) {
215 send_and_recv_0010();
216 send_0010();
217 recv_0010();
218 sendto_and_recvfrom_0010();
219 sendto_0010();
220 recvfrom_0010();
221
222 return t_status;
223 }