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 socket (), bind () and listen () for
10 * TEST1: Invalid domain
12 * TEST3: Opening a TCP style socket
16 * TEST4: Invalid address
17 * TEST5: Invalid address length
18 * TEST6: Invalid socket descriptor
19 * TEST7: Invalid host name
20 * TEST8: On a socket that is already bound
21 * TEST9: On reserved ports
22 * TEST10: INADDR_ANY address and non-zero port
23 * TEST11: INADDR_ANY address and zero port
24 * TEST12: Local address and zero port
28 * TEST13: Bad socket descriptor
29 * TEST14: Invalid socket
30 * TEST15: Listening a bound socket
32 * The SCTP implementation is free software;
33 * you can redistribute it and/or modify it under the terms of
34 * the GNU General Public License as published by
35 * the Free Software Foundation; either version 2, or (at your option)
38 * The SCTP implementation is distributed in the hope that it
39 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
40 * ************************
41 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
42 * See the GNU General Public License for more details.
44 * You should have received a copy of the GNU General Public License
45 * along with GNU CC; see the file COPYING. If not, write to
46 * the Free Software Foundation, 59 Temple Place - Suite 330,
47 * Boston, MA 02111-1307, USA.
49 * Please send any bug reports or fixes you make to the
51 * lksctp developers <lksctp-developers@lists.sourceforge.net>
53 * Or submit a bug report through the following website:
54 * http://www.sf.net/projects/lksctp
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
66 #include <sys/types.h>
67 #include <sys/socket.h>
68 #include <netinet/in.h> /* for sockaddr_in */
69 #include <arpa/inet.h>
71 #include <netinet/sctp.h>
75 #define SCTP_RESERVED_PORT 7
76 #define SCTP_INV_LOOPBACK "172.31.43.112"
78 char *TCID = __FILE__;
83 main(int argc, char *argv[])
89 struct sockaddr_in bind_addr;
91 /* Rather than fflush() throughout the code, set stdout to
94 setvbuf(stdout, NULL, _IONBF, 0);
95 setvbuf(stderr, NULL, _IONBF, 0);
99 /* socket() TEST1: Invalid domain, EAFNOSUPPORT Expected error */
100 sk = socket(-1, SOCK_STREAM, IPPROTO_SCTP);
101 if (sk != -1 || errno != EAFNOSUPPORT)
102 tst_brkm(TBROK, tst_exit, "socket() with invalid domain "
103 "error:%d, errno:%d", error, errno);
105 tst_resm(TPASS, "socket() with invalid domain - EAFNOSUPPORT");
107 /*socket() TEST2 : Invalid type, EINVAL Expected error*/
108 sk = socket(pf_class, -1, IPPROTO_SCTP);
109 if (sk != -1 || errno != EINVAL)
110 tst_brkm(TBROK, tst_exit, "socket() with invalid type "
111 "error:%d, errno:%d", error, errno);
113 tst_resm(TPASS, "socket() with invalid type - EINVAL");
115 /*socket() TEST3: opening a socket*/
116 sk = socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
118 tst_brkm(TBROK, tst_exit, "valid socket() call "
119 "error:%d, errno:%d", error, errno);
121 tst_resm(TPASS, "socket() - SUCCESS");
123 /*bind() TEST4: Invalid structure, EFAULT Expected error */
124 error = bind(sk, (struct sockaddr *)-1, sizeof(struct sockaddr_in));
125 if (error != -1 || errno != EFAULT)
126 tst_brkm(TBROK, tst_exit, "bind() with invalid address ptr "
127 "error:%d, errno:%d", error, errno);
129 tst_resm(TPASS, "bind() with invalid address ptr - EFAULT");
131 /*bind() TEST5: Invalid address length, EINVAL Expect error*/
132 bind_addr.sin_family = AF_INET;
133 bind_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
134 bind_addr.sin_port = htons(SCTP_TESTPORT_1);
136 error = bind(sk, (struct sockaddr *) &bind_addr, sizeof(bind_addr)-2);
137 if (error != -1 || errno != EINVAL)
138 tst_brkm(TBROK, tst_exit, "bind() with invalid address length "
139 "error:%d, errno:%d", error, errno);
141 tst_resm(TPASS, "bind() with invalid address length - EINVAL");
143 /*bind() TEST6: Invalid socket descriptor, ENOTSOCK Expect Error*/
144 error = bind(0, (struct sockaddr *) &bind_addr, sizeof(bind_addr));
145 if (error != -1 || errno != ENOTSOCK)
146 tst_brkm(TBROK, tst_exit, "bind() with invalid socket "
147 "descriptor error:%d, errno:%d", error, errno);
149 tst_resm(TPASS, "bind() with invalid socket descriptor - ENOTSOCK");
151 /*bind() TEST7: Invalid host name, EADDRNOTAVAIL Expect Error*/
152 /*Assigning invalid host name*/
153 bind_addr.sin_addr.s_addr = inet_addr(SCTP_INV_LOOPBACK);
154 error = bind(sk, (struct sockaddr *) &bind_addr, sizeof(bind_addr));
155 if (error != -1 || errno != EADDRNOTAVAIL)
156 tst_brkm(TBROK, tst_exit, "bind() with invalid local "
157 "address error:%d, errno:%d", error, errno);
159 tst_resm(TPASS, "bind() with invalid local address - EADDRNOTAVAIL");
161 /*bind() TEST8: Bind on a socket that has already called bind
162 EINAVL, Expected error*/
163 bind_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
164 /*Calling bind first time, it should pass*/
165 test_bind(sk, (struct sockaddr *) &bind_addr, sizeof(bind_addr));
167 error = bind(sk, (struct sockaddr *) &bind_addr, sizeof(bind_addr));
168 if (error != -1 || errno != EINVAL)
169 tst_brkm(TBROK, tst_exit, "bind() on an already bound socket "
170 "error:%d, errno:%d", error, errno);
172 tst_resm(TPASS, "bind() on an already bound socket - EINVAL");
174 /*Closing the socket which succeed in bind() */
177 /*Opening the socket again for further test*/
178 sk = socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
180 /*bind() TEST9: Bind on reserved ports EACCES, Expected error*/
181 /*Assigning a reserved port*/
184 bind_addr.sin_port = htons(SCTP_RESERVED_PORT);
185 error = bind(sk, (struct sockaddr *) &bind_addr,
187 if (error != -1 || errno != EACCES)
188 tst_brkm(TBROK, tst_exit, "bind() on reserverd port "
189 "error:%d, errno:%d", error, errno);
191 tst_resm(TPASS, "bind() on reserved port - EACCESS");
194 /*bind() TEST10: INADDR_ANY address and non-zero port, bind() should
196 bind_addr.sin_addr.s_addr = INADDR_ANY;
197 bind_addr.sin_port = htons(SCTP_TESTPORT_1);
198 error = bind(sk, (struct sockaddr *) &bind_addr,sizeof(bind_addr));
200 tst_brkm(TBROK, tst_exit, "bind() with INADDR_ANY address and "
201 "non-zero port error:%d, errno:%d", error, errno);
203 tst_resm(TPASS, "bind() with INADDR_ANY address and non-zero port - "
206 /*Closing the socket which succeed in bind() */
209 /*Opening the socket again for further test*/
210 sk = socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
212 /*bind() TEST11: INADDR_ANY address and zero port, bind() should
214 bind_addr.sin_port = 0;
215 error = bind(sk, (struct sockaddr *) &bind_addr,sizeof(bind_addr));
217 tst_brkm(TBROK, tst_exit, "bind() with INADDR_ANY address and "
218 "zero port error:%d, errno:%d", error, errno);
220 tst_resm(TPASS, "bind() with INADDR_ANY address and zero port - "
223 /*Closing the socket which succeed in bind() */
226 /*Opening the socket again for further test*/
227 sk = socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
229 /*bind() TEST12: local address and zero port, bind() should
231 bind_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
232 bind_addr.sin_port = 0;
233 error = bind(sk, (struct sockaddr *) &bind_addr,sizeof(bind_addr));
235 tst_brkm(TBROK, tst_exit, "bind() with local address and "
236 "zero port error:%d, errno:%d", error, errno);
238 tst_resm(TPASS, "bind() with local address and zero port - "
241 /*listen() TEST13: Bad socket descriptor EBADF, Expected error*/
242 error = listen(-1, 3);
243 if (error != -1 || errno != EBADF)
244 tst_brkm(TBROK, tst_exit, "listen() with bad socket descriptor "
245 "error:%d, errno:%d", error, errno);
247 tst_resm(TPASS, "listen() with bad socket descriptor - EBADF");
249 /*listen() TEST14: Invalid socket ENOTSOCK, Expected error*/
250 error = listen(0, 3);
251 if (error != -1 || errno != ENOTSOCK)
252 tst_brkm(TBROK, tst_exit, "listen() with invalid socket "
253 "error:%d, errno:%d", error, errno);
255 tst_resm(TPASS, "listen() with invalid socket - ENOTSOCK");
257 /*listen() TEST15:listen on a bound socket, should succeed*/
258 error = listen(sk, 3);
260 tst_brkm(TBROK, tst_exit, "listen() on a bound socket "
261 "error:%d, errno:%d", error, errno);
263 tst_resm(TPASS, "listen() on a bound socket - SUCCESS");