| 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 <[email protected]> |
| 35 | * |
| 36 | * Or submit a bug report through the following website: |
| 37 | * https://github.com/sctp/lksctp-tools/ |
| 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>cxx . . $ `<file>/src/include/netinet/sctp.h`/ |
| 55 | #include <sys/uio.h> |
| 56 | #include <linux/socket.h> |
| 57 | #include <sctputil.h>cxx . . $ `<file>/src/testlib/sctputil.h`/ |
| 58 | |
| 59 | char *TCIDcxx . . $ TCID. = __FILE__; |
| 60 | int TST_TOTALcxx . . $ TST_TOTAL. = 7; |
| 61 | int TST_CNTcxx . . $ TST_CNT. = 0; |
| 62 | |
| 63 | int |
| 64 | maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 65 | { |
| 66 | int msg_count; |
| 67 | socklen_tcxx . . $ 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 | int fd, err_no = 0; |
| 73 | char filename[21]; |
| 74 | |
| 75 | struct sockaddr_incxx . . $ sockaddr_in# conn_addr,lstn_addr,svr_addr; |
| 76 | |
| 77 | /* Rather than fflush() throughout the code, set stdout to |
| 78 | * be unbuffered. |
| 79 | */ |
| 80 | setvbufcxx . . $ setvbuf(4da3ba8c983f31bc).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, _IONBFcxx . . $ `<external>/232824719582829767/stdio.h:100:9`!, 0); |
| 81 | setvbufcxx . . $ setvbuf(4da3ba8c983f31bc).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, _IONBFcxx . . $ `<external>/232824719582829767/stdio.h:100:9`!, 0); |
| 82 | |
| 83 | message_rcv = test_malloccxx . . $ test_malloc(bfcfa4d6b7f7ef64).(512); |
| 84 | |
| 85 | pf_class = PF_INETcxx . . $ `<external>/8962547315054503424/socket.h:45:9`!; |
| 86 | |
| 87 | sk = test_socketcxx . . $ test_socket(2326284602b0bb2d).(pf_class, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, IPPROTO_SCTPcxx . . $ `<external>/9581057695539925927/in.h:87:9`!); |
| 88 | |
| 89 | lstn_sk = test_socketcxx . . $ test_socket(2326284602b0bb2d).(pf_class, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, IPPROTO_SCTPcxx . . $ `<external>/9581057695539925927/in.h:87:9`!); |
| 90 | |
| 91 | conn_addr.sin_familycxx . . $ sockaddr_in#sin_family. = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!; |
| 92 | conn_addr.sin_addrcxx . . $ sockaddr_in#sin_addr..s_addrcxx . . $ in_addr#s_addr. = SCTP_IP_LOOPBACKcxx . . $ `src/testlib/sctputil.h:76:9`!; |
| 93 | conn_addr.sin_portcxx . . $ sockaddr_in#sin_port. = htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(SCTP_TESTPORT_1cxx . . $ `src/testlib/sctputil.h:71:9`!); |
| 94 | |
| 95 | lstn_addr.sin_familycxx . . $ sockaddr_in#sin_family. = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!; |
| 96 | lstn_addr.sin_addrcxx . . $ sockaddr_in#sin_addr..s_addrcxx . . $ in_addr#s_addr. = SCTP_IP_LOOPBACKcxx . . $ `src/testlib/sctputil.h:76:9`!; |
| 97 | lstn_addr.sin_portcxx . . $ sockaddr_in#sin_port. = htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(SCTP_TESTPORT_1cxx . . $ `src/testlib/sctputil.h:71:9`!); |
| 98 | |
| 99 | /*Binding the listen socket*/ |
| 100 | test_bindcxx . . $ test_bind(18e507052d945257).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *) &lstn_addr, sizeof(lstn_addr)); |
| 101 | |
| 102 | /*Listening the socket*/ |
| 103 | test_listencxx . . $ test_listen(9b79fb6aee4c0440).(lstn_sk, 10); |
| 104 | |
| 105 | len = sizeof(struct sockaddr_incxx . . $ sockaddr_in#); |
| 106 | |
| 107 | test_connectcxx . . $ test_connect(18e507052d945257).(sk, (struct sockaddrcxx . . $ sockaddr# *) &conn_addr, len); |
| 108 | |
| 109 | acpt_sk = test_acceptcxx . . $ test_accept(46fcd054f4a4d98b).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *)&svr_addr, &len); |
| 110 | |
| 111 | msg_count = (strlencxx . . $ strlen(751346f8406fd082).(message) + 1); |
| 112 | |
| 113 | flag = MSG_NOSIGNALcxx . . $ `<external>/8962547315054503424/socket.h:242:9`!; |
| 114 | /*Sending the message*/ |
| 115 | count = test_sendcxx . . $ test_send(9d1ebb2cd9604347).(sk, message, msg_count, flag); |
| 116 | |
| 117 | /*recvfrom () TEST1: Bad socket descriptor, EBADF Expected error*/ |
| 118 | count = recvfromcxx . . $ recvfrom(aeb87f7c8304e243).(-1, message_rcv, msg_count, flag, |
| 119 | (struct sockaddrcxx . . $ sockaddr# *)&svr_addr, &len); |
| 120 | if (count != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EBADFcxx . . $ `<external>/8987895939074593727/errno-base.h:13:9`!) |
| 121 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recvfrom with a bad socket " |
| 122 | "descriptor count:%d, errno:%d", count, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 123 | |
| 124 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "recvfrom() with a bad socket descriptor - EBADF"); |
| 125 | |
| 126 | /*recvfrom () TEST2: Invalid socket , ENOTSOCK Expected error*/ |
| 127 | strcpycxx . . $ strcpy(b7b3a83273496486).(filename, "/tmp/sctptest.XXXXXX"); |
| 128 | fd = mkstempcxx . . $ mkstemp(1ef8e6ec083aabb9).(filename); |
| 129 | if (fd == -1) |
| 130 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "Failed to mkstemp %s: %s", |
| 131 | filename, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 132 | count = recvfromcxx . . $ recvfrom(aeb87f7c8304e243).(fd, message_rcv, msg_count, flag, |
| 133 | (struct sockaddrcxx . . $ sockaddr# *)&svr_addr, &len); |
| 134 | if (count == -1) |
| 135 | err_no = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 136 | closecxx . . $ close(7864480464b09eea).(fd); |
| 137 | unlinkcxx . . $ unlink(5c4306d8553b3696).(filename); |
| 138 | if (count != -1 || err_no != ENOTSOCKcxx . . $ `<external>/17922322188510340118/errno.h:72:9`!) |
| 139 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recvfrom with invalid socket " |
| 140 | "count:%d, errno:%d", count, err_no); |
| 141 | |
| 142 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "recvfrom() with invalid socket - ENOTSOCK"); |
| 143 | |
| 144 | /*recvfrom () TEST3: Invalid message pointer EFAULT, Expected error*/ |
| 145 | count = recvfromcxx . . $ recvfrom(aeb87f7c8304e243).(acpt_sk, (char *)-1, msg_count, flag, |
| 146 | (struct sockaddrcxx . . $ sockaddr# *)&svr_addr, &len); |
| 147 | if (count != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!) |
| 148 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recvfrom with invalid message " |
| 149 | "pointer count:%d, errno:%d", count, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 150 | |
| 151 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "recvfrom() with invalid message ptr - EFAULT"); |
| 152 | |
| 153 | /*TEST4: recvfrom on listening socket,ENOTCONN Expected error*/ |
| 154 | count = recvfromcxx . . $ recvfrom(aeb87f7c8304e243).(lstn_sk, message_rcv, msg_count, flag, |
| 155 | (struct sockaddrcxx . . $ sockaddr# *)&svr_addr, &len); |
| 156 | if (count != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOTCONNcxx . . $ `<external>/17922322188510340118/errno.h:91:9`!) |
| 157 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recvfrom on listening socket " |
| 158 | "count:%d, errno:%d", count, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 159 | |
| 160 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "recvfrom() on listening socket - ENOTCONN"); |
| 161 | |
| 162 | count = test_sendcxx . . $ test_send(9d1ebb2cd9604347).(acpt_sk, message, msg_count, flag); |
| 163 | |
| 164 | test_shutdowncxx . . $ test_shutdown(9b79fb6aee4c0440).(sk, SHUT_WRcxx . . $ `<external>/14344695994193663403/socket.h:46:9`!); |
| 165 | |
| 166 | /*recvfrom () TEST5:reading on a socket that received SHUTDOWN*/ |
| 167 | count = recvfromcxx . . $ recvfrom(aeb87f7c8304e243).(acpt_sk, message_rcv, msg_count, flag, |
| 168 | (struct sockaddrcxx . . $ sockaddr# *)&svr_addr, &len); |
| 169 | if (count < 0) |
| 170 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recvfrom on a socket that has " |
| 171 | "received shutdown count:%d, errno:%d", count, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 172 | |
| 173 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "recvfrom() on a socket that has received shutdown - " |
| 174 | "EOF"); |
| 175 | |
| 176 | /*recvfrom () TEST6:reading the pending message on socket that sent |
| 177 | SHUTDOWN*/ |
| 178 | count = recvfromcxx . . $ recvfrom(aeb87f7c8304e243).(sk, message_rcv, msg_count, flag, |
| 179 | (struct sockaddrcxx . . $ sockaddr# *)&svr_addr, &len); |
| 180 | if (count < 0) |
| 181 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recvfrom on a socket with pending " |
| 182 | "message that has sent shutdown count:%d, errno:%d", |
| 183 | count, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 184 | |
| 185 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "recvfrom() on a socket with pending message that has " |
| 186 | "sent shutdown - SUCCESS"); |
| 187 | |
| 188 | /*recvfrom () TEST7: No more message and association is shutdown, |
| 189 | ENOTCONN Expected error*/ |
| 190 | count = recvfromcxx . . $ recvfrom(aeb87f7c8304e243).(sk, message_rcv, msg_count, flag, |
| 191 | (struct sockaddrcxx . . $ sockaddr# *)&svr_addr, &len); |
| 192 | if (count != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOTCONNcxx . . $ `<external>/17922322188510340118/errno.h:91:9`!) |
| 193 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recvfrom on a socket with no " |
| 194 | "pending messages and has sent shutdown count:%d, " |
| 195 | "errno:%d", count, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 196 | |
| 197 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "recvfrom() on a socket with no pending messages and " |
| 198 | " has sent shutdown - ENOTCONN"); |
| 199 | |
| 200 | closecxx . . $ close(7864480464b09eea).(sk); |
| 201 | closecxx . . $ close(7864480464b09eea).(lstn_sk); |
| 202 | closecxx . . $ close(7864480464b09eea).(acpt_sk); |
| 203 | |
| 204 | freecxx . . $ free(5d22bdacc48458e8).(message_rcv); |
| 205 | return 0; |
| 206 | } |