Imported Upstream version 1.0.10
[platform/upstream/lksctp-tools.git] / src / func_tests / test_1_to_1_recvfrom.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 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
59 char *TCID = __FILE__;
60 int TST_TOTAL = 7;
61 int TST_CNT = 0;
62
63 int
64 main(int argc, char *argv[])
65 {
66         int ret, msg_count;
67         socklen_t len;
68         int sk,pf_class,lstn_sk,acpt_sk, flag;
69         char *message = "hello, world!\n";
70         char *message_rcv;
71         int count;
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         message_rcv = malloc(512);
82
83         pf_class = PF_INET;
84
85         sk = 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         msg_count = (strlen(message) + 1);
110
111         flag = MSG_NOSIGNAL;
112         /*Sending the message*/
113         count = test_send(sk, message, msg_count, flag);
114
115         /*recvfrom () TEST1: Bad socket descriptor, EBADF Expected error*/
116         count = recvfrom(-1, message_rcv, msg_count, flag,
117                          (struct sockaddr *)&svr_addr, &len);
118         if (count != -1 || errno != EBADF)
119                 tst_brkm(TBROK, tst_exit, "recvfrom with a bad socket "
120                          "descriptor count:%d, errno:%d", count, errno);
121
122         tst_resm(TPASS, "recvfrom() with a bad socket descriptor - EBADF");
123
124         /*recvfrom () TEST2: Invalid socket , ENOTSOCK Expected error*/
125         count = recvfrom(0, message_rcv, msg_count, flag,
126                          (struct sockaddr *)&svr_addr, &len);
127         if (count != -1 || errno != ENOTSOCK)
128                 tst_brkm(TBROK, tst_exit, "recvfrom with invalid socket "
129                          "count:%d, errno:%d", count, errno);
130
131         tst_resm(TPASS, "recvfrom() with invalid socket - ENOTSOCK");
132
133         /*recvfrom () TEST3: Invalid message pointer EFAULT, Expected error*/
134         count = recvfrom(acpt_sk, (char *)-1, msg_count, flag,
135                          (struct sockaddr *)&svr_addr, &len);
136         if (count != -1 || errno != EFAULT)
137                 tst_brkm(TBROK, tst_exit, "recvfrom with invalid message "
138                          "pointer count:%d, errno:%d", count, errno);
139
140         tst_resm(TPASS, "recvfrom() with invalid message ptr - EFAULT");
141
142         /*TEST4: recvfrom on listening socket,ENOTCONN Expected error*/
143         count = recvfrom(lstn_sk, message_rcv, msg_count, flag,
144                          (struct sockaddr *)&svr_addr, &len);
145         if (count != -1 || errno != ENOTCONN)
146                 tst_brkm(TBROK, tst_exit, "recvfrom on listening socket "
147                          "count:%d, errno:%d", count, errno);
148
149         tst_resm(TPASS, "recvfrom() on listening socket - ENOTCONN");
150
151         count = test_send(acpt_sk, message, msg_count, flag);
152
153         ret = test_shutdown(sk, SHUT_WR);
154
155         /*recvfrom () TEST5:reading on a socket that received SHUTDOWN*/
156         count = recvfrom(acpt_sk, message_rcv, msg_count, flag,
157                          (struct sockaddr *)&svr_addr, &len);
158         if (count < 0)
159                 tst_brkm(TBROK, tst_exit, "recvfrom on a socket that has "
160                          "received shutdown count:%d, errno:%d", count, errno);
161
162         tst_resm(TPASS, "recvfrom() on a socket that has received shutdown - "
163                  "EOF");
164
165         /*recvfrom () TEST6:reading the pending message on socket that sent 
166         SHUTDOWN*/
167         count = recvfrom(sk, message_rcv, msg_count, flag,
168                          (struct sockaddr *)&svr_addr, &len);
169         if (count < 0)
170                 tst_brkm(TBROK, tst_exit, "recvfrom on a socket with pending "
171                          "message that has sent shutdown count:%d, errno:%d",
172                          count, errno);
173
174         tst_resm(TPASS, "recvfrom() on a socket with pending message that has "
175                  "sent shutdown - SUCCESS");
176
177         /*recvfrom () TEST7: No more message and association is shutdown,
178         ENOTCONN Expected error*/
179         count = recvfrom(sk, message_rcv, msg_count, flag,
180                          (struct sockaddr *)&svr_addr, &len);
181         if (count != -1 || errno != ENOTCONN)
182                 tst_brkm(TBROK, tst_exit, "recvfrom on a socket with no "
183                          "pending messages and has sent shutdown count:%d, "
184                          "errno:%d", count, errno);
185
186         tst_resm(TPASS, "recvfrom() on a socket with no pending messages and "
187                  " has sent shutdown - ENOTCONN");
188
189         close(sk);
190         close(lstn_sk);
191         close(acpt_sk);
192         return 0;
193         
194 }