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 recvfrom () call for 1-1 style sockets
6 *
7 * TEST1: Bad socket descriptor
8 * TEST2: Invalid socket
9 * TEST3: Invalid message pointer
10 * TEST4: On a listening socket
11 * TEST5: Reading on a socket that received SHUTDOWN
12 * TEST6: Reading the pending message on socket that received SHUTDOWN
13 * TEST7: No more message and association is shutdown
14 *
15 * The SCTP implementation is free software;
16 * you can redistribute it and/or modify it under the terms of
17 * the GNU General Public License as published by
18 * the Free Software Foundation; either version 2, or (at your option)
19 * any later version.
20 *
21 * The SCTP implementation is distributed in the hope that it
22 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
23 * ************************
24 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25 * See the GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with GNU CC; see the file COPYING. If not, write to
29 * the Free Software Foundation, 59 Temple Place - Suite 330,
30 * Boston, MA 02111-1307, USA.
31 *
32 * Please send any bug reports or fixes you make to the
33 * email address(es):
34 * lksctp developers <lksctp-developers@lists.sourceforge.net>
35 *
36 * Or submit a bug report through the following website:
37 * http://www.sf.net/projects/lksctp
38 *
39 * Any bugs reported given to us we will try to fix... any fixes shared will
40 * be incorporated into the next SCTP release
41 *
42 */
43
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h> /* for sockaddr_in */
52 #include <arpa/inet.h>
53 #include <errno.h>
54 #include <netinet/sctp.h>
55 #include <sys/uio.h>
56 #include <linux/socket.h>
57 #include <sctputil.h>
58 #include "tst_kernel.h"
59
60 char *TCID = __FILE__;
61 int TST_TOTAL = 7;
62 int TST_CNT = 0;
63
64 int
main(void)65 main(void)
66 {
67 int msg_count;
68 socklen_t len;
69 int sk,pf_class,lstn_sk,acpt_sk, flag;
70 char *message = "hello, world!\n";
71 char *message_rcv;
72 int count;
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 message_rcv = malloc(512);
88
89 pf_class = PF_INET;
90
91 sk = 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 msg_count = (strlen(message) + 1);
116
117 flag = MSG_NOSIGNAL;
118 /*Sending the message*/
119 count = test_send(sk, message, msg_count, flag);
120
121 /*recvfrom () TEST1: Bad socket descriptor, EBADF Expected error*/
122 count = recvfrom(-1, message_rcv, msg_count, flag,
123 (struct sockaddr *)&svr_addr, &len);
124 if (count != -1 || errno != EBADF)
125 tst_brkm(TBROK, tst_exit, "recvfrom with a bad socket "
126 "descriptor count:%d, errno:%d", count, errno);
127
128 tst_resm(TPASS, "recvfrom() with a bad socket descriptor - EBADF");
129
130 /*recvfrom () TEST2: Invalid socket , ENOTSOCK Expected error*/
131 strcpy(filename, "/tmp/sctptest.XXXXXX");
132 fd = mkstemp(filename);
133 if (fd == -1)
134 tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
135 filename, strerror(errno));
136 count = recvfrom(fd, message_rcv, msg_count, flag,
137 (struct sockaddr *)&svr_addr, &len);
138 if (count == -1)
139 err_no = errno;
140 close(fd);
141 unlink(filename);
142 if (count != -1 || err_no != ENOTSOCK)
143 tst_brkm(TBROK, tst_exit, "recvfrom with invalid socket "
144 "count:%d, errno:%d", count, err_no);
145
146 tst_resm(TPASS, "recvfrom() with invalid socket - ENOTSOCK");
147
148 /*recvfrom () TEST3: Invalid message pointer EFAULT, Expected error*/
149 count = recvfrom(acpt_sk, (char *)-1, msg_count, flag,
150 (struct sockaddr *)&svr_addr, &len);
151 if (count != -1 || errno != EFAULT)
152 tst_brkm(TBROK, tst_exit, "recvfrom with invalid message "
153 "pointer count:%d, errno:%d", count, errno);
154
155 tst_resm(TPASS, "recvfrom() with invalid message ptr - EFAULT");
156
157 /*TEST4: recvfrom on listening socket,ENOTCONN Expected error*/
158 count = recvfrom(lstn_sk, message_rcv, msg_count, flag,
159 (struct sockaddr *)&svr_addr, &len);
160 if (count != -1 || errno != ENOTCONN)
161 tst_brkm(TBROK, tst_exit, "recvfrom on listening socket "
162 "count:%d, errno:%d", count, errno);
163
164 tst_resm(TPASS, "recvfrom() on listening socket - ENOTCONN");
165
166 count = test_send(acpt_sk, message, msg_count, flag);
167
168 test_shutdown(sk, SHUT_WR);
169
170 /*recvfrom () TEST5:reading on a socket that received SHUTDOWN*/
171 count = recvfrom(acpt_sk, message_rcv, msg_count, flag,
172 (struct sockaddr *)&svr_addr, &len);
173 if (count < 0)
174 tst_brkm(TBROK, tst_exit, "recvfrom on a socket that has "
175 "received shutdown count:%d, errno:%d", count, errno);
176
177 tst_resm(TPASS, "recvfrom() on a socket that has received shutdown - "
178 "EOF");
179
180 /*recvfrom () TEST6:reading the pending message on socket that sent
181 SHUTDOWN*/
182 count = recvfrom(sk, message_rcv, msg_count, flag,
183 (struct sockaddr *)&svr_addr, &len);
184 if (count < 0)
185 tst_brkm(TBROK, tst_exit, "recvfrom on a socket with pending "
186 "message that has sent shutdown count:%d, errno:%d",
187 count, errno);
188
189 tst_resm(TPASS, "recvfrom() on a socket with pending message that has "
190 "sent shutdown - SUCCESS");
191
192 /*recvfrom () TEST7: No more message and association is shutdown,
193 ENOTCONN Expected error*/
194 count = recvfrom(sk, message_rcv, msg_count, flag,
195 (struct sockaddr *)&svr_addr, &len);
196 if (count != -1 || errno != ENOTCONN)
197 tst_brkm(TBROK, tst_exit, "recvfrom on a socket with no "
198 "pending messages and has sent shutdown count:%d, "
199 "errno:%d", count, errno);
200
201 tst_resm(TPASS, "recvfrom() on a socket with no pending messages and "
202 " has sent shutdown - ENOTCONN");
203
204 close(sk);
205 close(lstn_sk);
206 close(acpt_sk);
207 return 0;
208
209 }
210