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