1 /* SCTP kernel Implementation
2 * Copyright (c) 2003 Hewlett-Packard Development Company, L.P
3 * (C) Copyright IBM Corp. 2004
5 * This file has test cases to test the sendmsg() call for 1-1 style sockets
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
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
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)
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.
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.
40 * Please send any bug reports or fixes you make to the
42 * lksctp developers <lksctp-developers@lists.sourceforge.net>
44 * Or submit a bug report through the following website:
45 * http://www.sf.net/projects/lksctp
47 * Any bugs reported given to us we will try to fix... any fixes shared will
48 * be incorporated into the next SCTP release
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h> /* for sockaddr_in */
60 #include <arpa/inet.h>
62 #include <netinet/sctp.h>
64 #include <linux/socket.h>
67 char *TCID = __FILE__;
72 main(int argc, char *argv[])
76 int sk,sk1,pf_class,lstn_sk,acpt_sk,acpt1_sk, flag;
77 struct msghdr outmessage;
78 char *message = "hello, world!\n";
80 struct sctp_sndrcvinfo *sinfo;
82 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
86 struct msghdr inmessage;
89 struct sockaddr_in conn_addr,lstn_addr,svr_addr;
91 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
93 /* Rather than fflush() throughout the code, set stdout to
96 setvbuf(stdout, NULL, _IONBF, 0);
97 setvbuf(stderr, NULL, _IONBF, 0);
101 sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
103 sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
105 lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
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);
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);
115 /*Binding the listen socket*/
116 test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
118 /*Listening the socket*/
119 test_listen(lstn_sk, 10);
121 len = sizeof(struct sockaddr_in);
123 test_connect(sk, (struct sockaddr *) &conn_addr, len);
125 acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
127 memset(&outmessage, 0, sizeof(outmessage));
128 buffer = malloc(REALLY_BIG);
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;
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));
146 iov.iov_base = buffer;
147 iov.iov_len = REALLY_BIG;
148 outmessage.msg_iov->iov_base = message;
150 outmessage.msg_iov->iov_len = strlen(message) + 1;
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);
159 tst_resm(TPASS, "sendmsg() with a bad socket descriptor - EBADF");
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);
167 tst_resm(TPASS, "sendmsg() with invalid socket - ENOTSOCK");
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);
175 tst_resm(TPASS, "sendmsg() on a listening socket - EPIPE");
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);
184 tst_resm(TPASS, "sendmsg() with invalid iovec ptr - EFAULT");
186 outmessage.msg_iov = &out_iov;
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);
195 tst_resm(TPASS, "sendmsg() with invalid iovec length - EINVAL");
197 outmessage.msg_iovlen = 1;
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);
205 tst_resm(TPASS, "sendmsg() with invalid msghdr ptr - EFAULT");
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);
214 tst_resm(TPASS, "sendmsg() with invalid sinfo flags - EINVAL");
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);
223 tst_resm(TPASS, "sendmsg() with SCTP_EOF flag - EINVAL");
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);
232 tst_resm(TPASS, "sendmsg() with SCTP_ABORT flag - EINVAL");
234 sinfo->sinfo_flags = 0;
236 test_connect(sk1, (struct sockaddr *) &lstn_addr, len);
238 test_sendmsg(sk1, &outmessage, flag, strlen(message)+1);
241 acpt1_sk = test_accept(lstn_sk, (struct sockaddr *)&conn_addr, &len);
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);
249 tst_resm(TPASS, "sendmsg() on a closed association - EPIPE");
256 sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
258 lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
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);
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);
268 /*Binding the listen socket*/
269 test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
271 /*Listening the socket*/
272 test_listen(lstn_sk, 10);
274 len = sizeof(struct sockaddr_in);
277 test_connect(sk, (struct sockaddr *) &conn_addr, len);
279 acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
281 memset(&outmessage, 0, sizeof(outmessage));
282 buffer_snd = malloc(REALLY_BIG);
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;
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));
300 iov.iov_base = buffer_snd;
301 iov.iov_len = REALLY_BIG;
302 outmessage.msg_iov->iov_base = message;
304 outmessage.msg_iov->iov_len = strlen(message) + 1;
306 memset(&inmessage, 0, sizeof(inmessage));
307 buffer_rcv = malloc(REALLY_BIG);
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);
316 msg_count = strlen(message) + 1;
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);
324 tst_resm(TPASS, "sendmsg() from accept socket to client - SUCCESS");
326 count = test_recvmsg(sk, &inmessage, flag);
327 test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
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);
337 tst_resm(TPASS, "sendmsg() from client to server - SUCCESS");
339 count = test_recvmsg(acpt_sk, &inmessage, flag);
340 test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
342 outmessage.msg_name = &conn_addr;
343 outmessage.msg_namelen = sizeof(conn_addr);
346 sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
348 /*sendmsg() TEST13: Sending data from unconnected client socket to
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);
355 tst_resm(TPASS, "sendmsg() from unconnected clt to server - SUCCESS");
357 acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
359 count = test_recvmsg(acpt_sk, &inmessage, flag);
360 test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
362 test_shutdown(sk1, SHUT_RD);
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);
370 tst_resm(TPASS, "sendmsg() on a SHUT_RD socket - SUCCESS");
372 count = test_recvmsg(acpt_sk, &inmessage, flag);
373 test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);