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