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 accept () and close () call for
6  * 1-1 style sockets
7  *
8  * accept () Tests:
9  * ---------------
10  * TEST1: Bad socket descriptor
11  * TEST2: Invalid socket
12  * TEST3: Invalid address
13  * TEST4: On a non-listening socket
14  * TEST5: On a established socket
15  * TEST6: On a CLOSED association
16  * TEST7: Extracting the association on the listening socket
17  *
18  * close () Tests:
19  * --------------
20  * TEST8: Bad socket descriptor
21  * TEST9: valid socket descriptor
22  * TEST10: Closed socket descriptor
23  *
24  * The SCTP implementation is free software;
25  * you can redistribute it and/or modify it under the terms of
26  * the GNU General Public License as published by
27  * the Free Software Foundation; either version 2, or (at your option)
28  * any later version.
29  *
30  * The SCTP implementation is distributed in the hope that it
31  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
32  *                 ************************
33  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
34  * See the GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with GNU CC; see the file COPYING.  If not, write to
38  * the Free Software Foundation, 59 Temple Place - Suite 330,
39  * Boston, MA 02111-1307, USA.
40  *
41  * Please send any bug reports or fixes you make to the
42  * email address(es):
43  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
44  *
45  * Or submit a bug report through the following website:
46  *    http://www.sf.net/projects/lksctp
47  *
48  * Any bugs reported given to us we will try to fix... any fixes shared will
49  * be incorporated into the next SCTP release.
50  *
51  */
52 
53 #include <stdio.h>
54 #include <unistd.h>
55 #include <fcntl.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>         /* for sockaddr_in */
61 #include <arpa/inet.h>
62 #include <errno.h>
63 #include <netinet/sctp.h>
64 #include <sys/uio.h>
65 #include <sctputil.h>
66 
67 char *TCID = __FILE__;
68 int TST_TOTAL = 10;
69 int TST_CNT = 0;
70 
71 #define SK_MAX  10
72 
73 int
main(int argc,char * argv[])74 main(int argc, char *argv[])
75 {
76         socklen_t len;
77 	int i;
78 	int sk,lstn_sk,clnt_sk[SK_MAX],acpt_sk,pf_class;
79 	int new_sk[SK_MAX],clnt2_sk[SK_MAX];
80 	int error;
81 	int fd, err_no = 0;
82 	char filename[21];
83 
84         struct sockaddr_in conn_addr,lstn_addr,acpt_addr;
85 
86 	/* Rather than fflush() throughout the code, set stdout to
87          * be unbuffered.
88          */
89         setvbuf(stdout, NULL, _IONBF, 0);
90         setvbuf(stderr, NULL, _IONBF, 0);
91 
92         pf_class = PF_INET;
93 
94         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
95 
96 	for (i=0 ; i < SK_MAX ; i++)
97 		new_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
98 
99 	/* Creating a regular socket */
100 	for (i = 0 ; i < SK_MAX ; i++)
101 		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
102 
103 	for (i = 0 ; i < SK_MAX ; i++)
104 		clnt2_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
105 
106 	/* Creating a listen socket */
107         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
108 
109 	conn_addr.sin_family = AF_INET;
110         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
111         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
112 
113 	lstn_addr.sin_family = AF_INET;
114         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
115         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
116 
117 	/* Binding the listen socket */
118 	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
119 
120 	/* Listening many sockets as we are calling too many connect here */
121 	test_listen(lstn_sk, SK_MAX );
122 
123 	/* connect() is called just to make sure accept() doesn't block the
124 	 * program
125 	 */
126 	i = 0;
127 	len = sizeof(struct sockaddr_in);
128 	test_connect(clnt_sk[i++], (struct sockaddr *) &conn_addr, len);
129 
130 	/* accept() TEST1: Bad socket descriptor EBADF, Expected error */
131         error = accept(-1, (struct sockaddr *) &acpt_addr, &len);
132         if (error != -1 || errno != EBADF)
133 		tst_brkm(TBROK, tst_exit, "accept with a bad socket descriptor"
134                          "error:%d, errno:%d", error, errno);
135 
136 	tst_resm(TPASS, "accept() with a bad socket descriptor - EBADF");
137 
138         /*accept() TEST2: Invalid socket ENOTSOCK, Expected error*/
139 	strcpy(filename, "/tmp/sctptest.XXXXXX");
140 	fd = mkstemp(filename);
141 	if (fd == -1)
142 		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
143 				filename, strerror(errno));
144 	error = accept(fd, (struct sockaddr *) &acpt_addr, &len);
145 	if (error == -1)
146 		err_no = errno;
147 	close(fd);
148 	unlink(filename);
149 	if (error != -1 || err_no != ENOTSOCK)
150 		tst_brkm(TBROK, tst_exit, "accept with invalid socket"
151                          "error:%d, errno:%d", error, err_no);
152 
153 	tst_resm(TPASS, "accept() with invalid socket - ENOTSOCK");
154 
155         /*accept() TEST3: Invalid address EFAULT, Expected error*/
156         error = accept(lstn_sk, (struct sockaddr *) -1, &len);
157         if (error != -1 || errno != EFAULT)
158 		tst_brkm(TBROK, tst_exit, "accept with invalid address"
159                          "error:%d, errno:%d", error, errno);
160 
161 	tst_resm(TPASS, "accept() with invalid address - EFAULT");
162 
163 	test_connect(clnt_sk[i++], (struct sockaddr *) &conn_addr, len);
164 
165         /*accept() TEST4: on a non-listening socket EINVAL, Expected error*/
166         error = accept(sk, (struct sockaddr *) &acpt_addr, &len);
167         if (error != -1 || errno != EINVAL)
168 		tst_brkm(TBROK, tst_exit, "accept on a non-listening socket"
169                          "error:%d, errno:%d", error, errno);
170 
171 	tst_resm(TPASS, "accept() on a non-listening socket - EINVAL");
172 
173 	test_connect(clnt_sk[i++], (struct sockaddr *) &conn_addr, len);
174 
175 	/*Calling accept to establish the connection*/
176 	acpt_sk = test_accept(lstn_sk, (struct sockaddr *) &acpt_addr, &len);
177 
178 	/*accept() TEST5: On a established socket EINVAL, Expected error*/
179 	error = accept(acpt_sk, (struct sockaddr *) &acpt_addr, &len);
180 	if (error != -1 || (errno != EINVAL && errno != EACCES)) {
181 		tst_brkm(TBROK, tst_exit, "accept on an established socket"
182 		         "error:%d, errno:%d", error, errno);
183 	}
184 
185 	tst_resm(TPASS, "accept() on an established socket - %s",
186 		tst_strerrno(errno));
187 
188 	/*Closing the previously established association*/
189 	close(acpt_sk);
190 
191 	test_connect(clnt_sk[i], (struct sockaddr *) &conn_addr, len);
192 
193 	/*accept() TEST6: On the CLOSED association should succeed*/
194 	acpt_sk = accept(lstn_sk, (struct sockaddr *) &acpt_addr, &len);
195         if (acpt_sk < 0)
196 		tst_brkm(TBROK, tst_exit, "accept a closed association"
197                          "error:%d, errno:%d", error, errno);
198 
199 	tst_resm(TPASS, "accept() a closed association - SUCCESS");
200 
201 	close(acpt_sk);
202 
203 	/*accept() TEST7: Extracting the association on the listening socket
204 	as new socket, new socket socket descriptor should return*/
205 	for (i = 0 ; i < (SK_MAX - 1); i++)
206 		test_connect(clnt2_sk[i], (struct sockaddr *) &conn_addr, len);
207 
208 	for (i = 0 ; i < (SK_MAX - 1); i++)
209 		new_sk[i] = test_accept(lstn_sk, (struct sockaddr *)&acpt_addr,
210 					&len);
211 
212 	tst_resm(TPASS, "accept() on a listening socket - SUCCESS");
213 
214 
215         /*close() TEST8: Bad socket descriptor, EBADF Expected error*/
216 	error = close(-1);
217 	if (error != -1 || errno != EBADF)
218 		tst_brkm(TBROK, tst_exit, "close with a bad socket descriptor "
219                          "error:%d, errno:%d", error, errno);
220 
221 	tst_resm(TPASS, "close() with a bad socket descriptor - EBADF");
222 
223 	/*close() TEST9: valid socket descriptor should succeed*/
224 	error = close(sk);
225 	if (error < 0)
226 		tst_brkm(TBROK, tst_exit, "close with a valid socket descriptor"
227                          " error:%d, errno:%d", error, errno);
228 
229 	tst_resm(TPASS, "close() with a valid socket descriptor - SUCCESS");
230 
231 	/*close() TEST10: closed socket descriptor, EBADF Expected error*/
232         error = close(sk);
233         if (error != -1 || errno != EBADF)
234 		tst_brkm(TBROK, tst_exit, "close with a closed socket "
235 			 "descriptor error:%d, errno:%d", error, errno);
236 
237 	tst_resm(TPASS, "close() with a closed socket descriptor - EBADF");
238 
239 	for (i = 0 ; i < SK_MAX ; i++) {
240 		close(clnt_sk[i]);
241 		close(new_sk[i]);
242 		close(clnt2_sk[i]);
243 	}
244 
245 	return 0;
246 }
247