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