• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
main(int argc,char * argv[])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 	int fd, err_no = 0;
73 	char filename[21];
74 
75         struct sockaddr_in conn_addr,lstn_addr,svr_addr;
76 
77 	/* Rather than fflush() throughout the code, set stdout to
78          * be unbuffered.
79          */
80         setvbuf(stdout, NULL, _IONBF, 0);
81         setvbuf(stderr, NULL, _IONBF, 0);
82 
83         pf_class = PF_INET;
84 
85         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
86 
87         sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
88 
89         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
90 
91 	conn_addr.sin_family = AF_INET;
92         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
93         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
94 
95 	lstn_addr.sin_family = AF_INET;
96         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
97         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
98 
99 	/*Binding the listen socket*/
100         test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
101 
102         /*Listening the socket*/
103         test_listen(lstn_sk, 10);
104 
105 	len = sizeof(struct sockaddr_in);
106 
107 	test_connect(sk, (struct sockaddr *) &conn_addr, len);
108 
109 	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
110 
111 	len_snd = (strlen(message) + 1);
112 
113 	flag = MSG_NOSIGNAL;
114 	/*send () TEST1: Bad socket descriptor, EBADF Expected error*/
115 	count = send(-1, message, len_snd, flag);
116 	if (count != -1 || errno != EBADF)
117 		tst_brkm(TBROK, tst_exit, "send with a bad socket "
118 			 "descriptor count:%d, errno:%d", count, errno);
119 
120 	tst_resm(TPASS, "send() with a bad socket descriptor - EBADF");
121 
122 	/*send () TEST2: Invalid socket, ENOTSOCK Expected error*/
123 	strcpy(filename, "/tmp/sctptest.XXXXXX");
124 	fd = mkstemp(filename);
125 	if (fd == -1)
126 		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
127 				filename, strerror(errno));
128 	count = send(fd, message, len_snd, flag);
129 	if (count == -1)
130 		err_no = errno;
131 	close(fd);
132 	unlink(filename);
133 	if (count != -1 || err_no != ENOTSOCK)
134 		tst_brkm(TBROK, tst_exit, "send with invalid socket "
135 			 "count:%d, errno:%d", count, err_no);
136 
137 	tst_resm(TPASS, "send() with invalid socket - ENOTSOCK");
138 
139 	/*send () TEST3: send on listening socket, EPIPE Expected error*/
140 	count = send(lstn_sk, message, len_snd, flag);
141 	if (count != -1 || errno != EPIPE)
142 		tst_brkm(TBROK, tst_exit, "send on a listening socket "
143 			 "count:%d, errno:%d", count, errno);
144 
145 	tst_resm(TPASS, "send() on a listening socket - EPIPE");
146 #if 0
147 	/*send () TEST4: Invalid message address, EFAULT Expected error*/
148        /* FIXME this test should pass. Don't catch why...  */
149 	count = send(sk, (char *)0x1, len_snd, flag);
150 	if (count != -1 || errno != EFAULT)
151 		tst_brkm(TBROK, tst_exit, "send with invalid message "
152 			 "pointer count:%d, errno:%d", count, errno);
153 
154 	tst_resm(TPASS, "send() with invalid message ptr - EFAULT");
155 #endif
156 
157 	test_connect(sk1, (struct sockaddr *) &lstn_addr, len);
158 
159 	count = test_send(sk1, message, len_snd, flag);
160 
161 	close(sk1);
162 
163 	acpt1_sk = test_accept(lstn_sk, (struct sockaddr *)&conn_addr, &len);
164 
165 	/*send () TEST5: send on closed association, EPIPE Expected error*/
166 	count = send(acpt1_sk, message, len_snd, flag);
167 	if (count != -1 || errno != EPIPE)
168 		tst_brkm(TBROK, tst_exit, "send on a closed association "
169 			 "count:%d, errno:%d", count, errno);
170 
171 	tst_resm(TPASS, "send() on a closed association - EPIPE");
172 
173 	close(acpt1_sk);
174 	close(sk);
175 	close(lstn_sk);
176 	close(acpt_sk);
177 
178         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
179 
180         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
181 
182 	message_rcv = malloc(512);
183 
184 	/*Binding the listen socket*/
185         test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
186 
187         /*Listening the socket*/
188         test_listen(lstn_sk, 10);
189 
190 	conn_addr.sin_family = AF_INET;
191         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
192         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
193 
194 	len = sizeof(struct sockaddr_in);
195 
196 	test_connect(sk, (struct sockaddr *) &conn_addr, len);
197 
198 	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
199 
200 	msg_count = strlen(message) + 1;
201 
202 	/*send() TEST6: Sending data from client socket to server socket*/
203 	count = send(sk, message, msg_count, flag);
204 	if (count != msg_count)
205 		tst_brkm(TBROK, tst_exit, "send from client to server "
206                          "count:%d, errno:%d", count, errno);
207 
208 	tst_resm(TPASS, "send() from client to server - SUCCESS");
209 
210 	test_recv(acpt_sk, message_rcv, msg_count, flag);
211 
212 	strncpy(message_rcv,"\0",512);
213 
214 	/*send() TEST7: Sending data from accept socket to client socket*/
215 	count = send(acpt_sk, message, msg_count, flag);
216 	if (count != msg_count)
217 		tst_brkm(TBROK, tst_exit, "send from accept socket to client "
218                          "count:%d, errno:%d", count, errno);
219 
220 	tst_resm(TPASS, "send() from accept socket to client - SUCCESS");
221 
222 	test_recv(sk, message_rcv, msg_count, flag);
223 
224 	/*send() TEST8: Sending less number of data from the buffer*/
225 	/*Sending only 5 bytes so that only hello is received*/
226 	test_send(sk, message, 5 , flag);
227 	test_recv(acpt_sk, message_rcv, 5, flag);
228 
229 	tst_resm(TPASS, "send() partial data from a buffer - SUCCESS");
230 
231 	/* TEST9: sctp_send with no sinfo */
232 	test_sctp_send(sk, message, strlen(message) + 1 , NULL, flag);
233 	test_recv(acpt_sk, message_rcv, strlen(message) + 1, flag);
234 	tst_resm(TPASS, "sctp_send() with no sinfo - SUCCESS");
235 
236 	close(sk1);
237 	close(lstn_sk);
238 	close(acpt_sk);
239 
240 	return 0;
241 }
242