Imported Upstream version 1.0.10
[platform/upstream/lksctp-tools.git] / src / func_tests / test_1_to_1_sendmsg.c
1 /* SCTP kernel Implementation
2  * Copyright (c) 2003 Hewlett-Packard Development Company, L.P
3  * (C) Copyright IBM Corp. 2004
4  *
5  * This file has test cases to test the sendmsg() call for 1-1 style sockets
6  *
7  * TEST1: Bad socket descriptor
8  * TEST2: Invalid socket
9  * TEST3: On a listening socket
10  * TEST4: Invalid iovec pointer
11  * TEST5: Invalid iovec length
12  * TEST6: Invalid msghdr pointer
13  * TEST7: Invalid sinfo flags
14  * TEST8: SCTP_EOF flag set
15  * TEST9: SCTP_ABORT flag set
16  * TEST10: On a closed association
17  *
18  * TEST11: Sending data from server socket to client socket
19  * TEST12: Sending data from client socket to server socket
20  * TEST13: Sending data from unconnected client to server 
21  * TEST14: Sending a message on SHUT_RD socket
22  *
23  * The SCTP implementation is free software;
24  * you can redistribute it and/or modify it under the terms of
25  * the GNU General Public License as published by
26  * the Free Software Foundation; either version 2, or (at your option)
27  * any later version.
28  *
29  * The SCTP implementation is distributed in the hope that it
30  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
31  *                 ************************
32  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
33  * See the GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with GNU CC; see the file COPYING.  If not, write to
37  * the Free Software Foundation, 59 Temple Place - Suite 330,
38  * Boston, MA 02111-1307, USA.
39  *
40  * Please send any bug reports or fixes you make to the
41  * email address(es):
42  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
43  *
44  * Or submit a bug report through the following website:
45  *    http://www.sf.net/projects/lksctp
46  *
47  * Any bugs reported given to us we will try to fix... any fixes shared will
48  * be incorporated into the next SCTP release
49  *
50  */
51
52 #include <stdio.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>         /* for sockaddr_in */
60 #include <arpa/inet.h>
61 #include <errno.h>
62 #include <netinet/sctp.h>
63 #include <sys/uio.h>
64 #include <linux/socket.h>
65 #include <sctputil.h>
66
67 char *TCID = __FILE__;
68 int TST_TOTAL = 14;
69 int TST_CNT = 0;
70
71 int
72 main(int argc, char *argv[])
73 {
74         socklen_t len;
75         int msg_count;
76         int sk,sk1,pf_class,lstn_sk,acpt_sk,acpt1_sk, flag;
77         struct msghdr outmessage;
78         char *message = "hello, world!\n";
79         struct iovec iov;
80         struct sctp_sndrcvinfo *sinfo;
81         int count;
82         char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
83         struct cmsghdr *cmsg;
84         struct iovec out_iov;
85         char * buffer;
86         struct msghdr inmessage;
87         char * buffer_snd;
88         char * buffer_rcv;
89         struct sockaddr_in conn_addr,lstn_addr,svr_addr;
90         struct iovec iov_rcv;
91         char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
92
93         /* Rather than fflush() throughout the code, set stdout to
94          * be unbuffered.
95          */
96         setvbuf(stdout, NULL, _IONBF, 0);
97         setvbuf(stderr, NULL, _IONBF, 0);
98
99         pf_class = PF_INET;
100
101         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
102
103         sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
104
105         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
106
107         conn_addr.sin_family = AF_INET;
108         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
109         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
110
111         lstn_addr.sin_family = AF_INET;
112         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
113         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
114
115         /*Binding the listen socket*/
116         test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
117
118         /*Listening the socket*/
119         test_listen(lstn_sk, 10);
120
121         len = sizeof(struct sockaddr_in);
122         
123         test_connect(sk, (struct sockaddr *) &conn_addr, len);
124
125         acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
126
127         memset(&outmessage, 0, sizeof(outmessage));
128         buffer = malloc(REALLY_BIG);
129
130         outmessage.msg_name = &conn_addr;
131         outmessage.msg_namelen = sizeof(conn_addr);
132         outmessage.msg_iov = &out_iov;
133         outmessage.msg_iovlen = 1;
134         outmessage.msg_control = outcmsg;
135         outmessage.msg_controllen = sizeof(outcmsg);
136         outmessage.msg_flags = 0;
137
138         cmsg = CMSG_FIRSTHDR(&outmessage);
139         cmsg->cmsg_level = IPPROTO_SCTP;
140         cmsg->cmsg_type = SCTP_SNDRCV;
141         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
142         outmessage.msg_controllen = cmsg->cmsg_len;
143         sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
144         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
145
146         iov.iov_base = buffer;
147         iov.iov_len = REALLY_BIG;
148         outmessage.msg_iov->iov_base = message;
149
150         outmessage.msg_iov->iov_len = strlen(message) + 1;
151
152         flag = MSG_NOSIGNAL;
153         /*sendmsg () TEST1: Bad socket descriptor, EBADF Expected error*/
154         count = sendmsg(-1, &outmessage, flag);
155         if (count != -1 || errno != EBADF)
156                 tst_brkm(TBROK, tst_exit, "sendmsg with a bad socket "
157                          "descriptor count:%d, errno:%d", count, errno);
158
159         tst_resm(TPASS, "sendmsg() with a bad socket descriptor - EBADF");
160         
161         /*sendmsg () TEST2: Invalid socket, ENOTSOCK Expected error*/
162         count = sendmsg(0, &outmessage, flag);
163         if (count != -1 || errno != ENOTSOCK)
164                 tst_brkm(TBROK, tst_exit, "sendmsg with invalid socket "
165                          "count:%d, errno:%d", count, errno);
166
167         tst_resm(TPASS, "sendmsg() with invalid socket - ENOTSOCK");
168
169         /*sendmsg () TEST3: sendmsg on listening socket, EPIPE Expected error*/
170         count = sendmsg(lstn_sk, &outmessage, flag);
171         if (count != -1 || errno != EPIPE)
172                 tst_brkm(TBROK, tst_exit, "sendmsg on a listening socket "
173                          "count:%d, errno:%d", count, errno);
174
175         tst_resm(TPASS, "sendmsg() on a listening socket - EPIPE");
176
177         /*sendmsg () TEST4: Invalid iovec pointer EFAULT, Expected error*/
178         outmessage.msg_iov = (struct iovec *)-1;
179         count = sendmsg(sk, &outmessage, flag);
180         if (count != -1 || errno != EFAULT)
181                 tst_brkm(TBROK, tst_exit, "sendmsg with invalid iovec "
182                          "pointer count:%d, errno:%d", count, errno);
183
184         tst_resm(TPASS, "sendmsg() with invalid iovec ptr - EFAULT");
185         
186         outmessage.msg_iov = &out_iov;
187
188         /*sendmsg () TEST5: Invalid iovec count EINVAL, Expected error*/
189         outmessage.msg_iovlen = 0;
190         count = sendmsg(sk, &outmessage, flag);
191         if (count != -1 || errno != EINVAL)
192                 tst_brkm(TBROK, tst_exit, "sendmsg with invalid iovec "
193                          "length count:%d, errno:%d", count, errno);
194
195         tst_resm(TPASS, "sendmsg() with invalid iovec length - EINVAL");
196
197         outmessage.msg_iovlen = 1;
198         
199         /*sendmsg () TEST6: Invalid msghdr pointer EFAULT, Expected error*/
200         count = sendmsg(sk, (struct msghdr *)-1, flag);
201         if (count != -1 || errno != EFAULT)
202                 tst_brkm(TBROK, tst_exit, "sendmsg with invalid msghdr "
203                          "pointer count:%d, errno:%d", count, errno);
204
205         tst_resm(TPASS, "sendmsg() with invalid msghdr ptr - EFAULT");
206
207         /*sendmsg () TEST7: Invalid sinfo flag EINVAL, Expected error*/
208         sinfo->sinfo_flags = 999;
209         count = sendmsg(sk, &outmessage, -1);
210         if (count != -1 || errno != EINVAL)
211                 tst_brkm(TBROK, tst_exit, "sendmsg with invalid sinfo "
212                          "flags count:%d, errno:%d", count, errno);
213
214         tst_resm(TPASS, "sendmsg() with invalid sinfo flags - EINVAL");
215
216         /*sendmsg () TEST8: SCTP_EOF flag EINVAL, Expected error*/
217         sinfo->sinfo_flags = SCTP_EOF;
218         count = sendmsg(sk, &outmessage, flag);
219         if (count != -1 || errno != EINVAL)
220                 tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_EOF flag "
221                          "count:%d, errno:%d", count, errno);
222
223         tst_resm(TPASS, "sendmsg() with SCTP_EOF flag - EINVAL");
224
225         /*sendmsg () TEST9: SCTP_ABORT flag EINVAL, Expected error*/
226         sinfo->sinfo_flags = SCTP_ABORT;
227         count = sendmsg(sk, &outmessage, flag);
228         if (count != -1 || errno != EINVAL)
229                 tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_ABORT flag "
230                          "count:%d, errno:%d", count, errno);
231
232         tst_resm(TPASS, "sendmsg() with SCTP_ABORT flag - EINVAL");
233
234         sinfo->sinfo_flags = 0; 
235         
236         test_connect(sk1, (struct sockaddr *) &lstn_addr, len);
237                  
238         test_sendmsg(sk1, &outmessage, flag, strlen(message)+1);
239
240         close(sk1);
241         acpt1_sk = test_accept(lstn_sk, (struct sockaddr *)&conn_addr, &len);
242
243         /*sendmsg () TEST10:sendmsg on closed association, EPIPE Expected error*/
244         count = sendmsg(acpt1_sk, &outmessage, flag);
245         if (count != -1 || errno != EPIPE)
246                 tst_brkm(TBROK, tst_exit, "sendmsg on a closed association "
247                          "count:%d, errno:%d", count, errno);
248
249         tst_resm(TPASS, "sendmsg() on a closed association - EPIPE");
250
251         close(acpt1_sk);
252         close(sk);
253         close(lstn_sk);
254         close(acpt_sk);
255
256         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
257
258         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
259
260         conn_addr.sin_family = AF_INET;
261         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
262         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
263
264         lstn_addr.sin_family = AF_INET;
265         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
266         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
267
268         /*Binding the listen socket*/
269         test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
270
271         /*Listening the socket*/
272         test_listen(lstn_sk, 10);
273
274         len = sizeof(struct sockaddr_in);
275         flag = MSG_NOSIGNAL;
276         
277         test_connect(sk, (struct sockaddr *) &conn_addr, len);
278
279         acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
280
281         memset(&outmessage, 0, sizeof(outmessage));
282         buffer_snd = malloc(REALLY_BIG);
283
284         outmessage.msg_name = &svr_addr;
285         outmessage.msg_namelen = sizeof(svr_addr);
286         outmessage.msg_iov = &out_iov;
287         outmessage.msg_iovlen = 1;
288         outmessage.msg_control = outcmsg;
289         outmessage.msg_controllen = sizeof(outcmsg);
290         outmessage.msg_flags = 0;
291
292         cmsg = CMSG_FIRSTHDR(&outmessage);
293         cmsg->cmsg_level = IPPROTO_SCTP;
294         cmsg->cmsg_type = SCTP_SNDRCV;
295         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
296         outmessage.msg_controllen = cmsg->cmsg_len;
297         sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
298         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
299
300         iov.iov_base = buffer_snd;
301         iov.iov_len = REALLY_BIG;
302         outmessage.msg_iov->iov_base = message;
303
304         outmessage.msg_iov->iov_len = strlen(message) + 1;
305
306         memset(&inmessage, 0, sizeof(inmessage));
307         buffer_rcv = malloc(REALLY_BIG);
308
309         iov_rcv.iov_base = buffer_rcv;
310         iov_rcv.iov_len = REALLY_BIG;
311         inmessage.msg_iov = &iov_rcv;
312         inmessage.msg_iovlen = 1;
313         inmessage.msg_control = incmsg;
314         inmessage.msg_controllen = sizeof(incmsg);
315
316         msg_count = strlen(message) + 1;
317
318         /*sendmsg() TEST11: Sending data from server socket to client socket*/
319         count = sendmsg(acpt_sk, &outmessage, flag);
320         if (count != msg_count)
321                 tst_brkm(TBROK, tst_exit, "sendmsg from accept socket to "
322                          "client count:%d, errno:%d", count, errno);
323
324         tst_resm(TPASS, "sendmsg() from accept socket to client - SUCCESS");
325
326         count = test_recvmsg(sk, &inmessage, flag);
327         test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
328
329         outmessage.msg_name = &conn_addr;
330         outmessage.msg_namelen = sizeof(conn_addr);
331         /*sendmsg() TEST12: Sending data from client socket to server socket*/
332         count = sendmsg(sk, &outmessage, flag);
333         if (count != msg_count)
334                 tst_brkm(TBROK, tst_exit, "sendmsg from client to server "
335                          "count:%d, errno:%d", count, errno);
336
337         tst_resm(TPASS, "sendmsg() from client to server - SUCCESS");
338
339         count = test_recvmsg(acpt_sk, &inmessage, flag);
340         test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
341
342         outmessage.msg_name = &conn_addr;
343         outmessage.msg_namelen = sizeof(conn_addr);
344         close(sk);
345         close(acpt_sk);
346         sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
347
348         /*sendmsg() TEST13: Sending data from unconnected client socket to 
349         server socket*/
350         count = sendmsg(sk1, &outmessage, flag);
351         if (count != msg_count)
352                 tst_brkm(TBROK, tst_exit, "sendmsg from unconnected client to "
353                          "server count:%d, errno:%d", count, errno);
354
355         tst_resm(TPASS, "sendmsg() from unconnected clt to server - SUCCESS");
356
357         acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
358
359         count = test_recvmsg(acpt_sk, &inmessage, flag);
360         test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
361
362         test_shutdown(sk1, SHUT_RD);
363
364         /*sendmsg() TEST14: Sending a message on SHUT_RD socket*/
365         count = sendmsg(sk1, &outmessage, flag);
366         if (count != msg_count)
367                 tst_brkm(TBROK, tst_exit, "sendmsg on a SHUT_RD socket "
368                          "count:%d, errno:%d", count, errno);
369
370         tst_resm(TPASS, "sendmsg() on a SHUT_RD socket - SUCCESS");
371
372         count = test_recvmsg(acpt_sk, &inmessage, flag);
373         test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
374
375         close(sk1);
376         close(lstn_sk);
377         close(acpt_sk);
378         return 0;
379 }