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