| 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 shutdown() call for 1-1 style sockets |
| 6 | * |
| 7 | * TEST1: Bad socket descriptor |
| 8 | * TEST2: Invalid socket |
| 9 | * TEST3: shutdown with SHUT_WR flag to disable new send |
| 10 | * TEST4: shutdown with SHUT_RD flag to disable new receive |
| 11 | * TEST5: shutdown with SHUT_RDWR flag to disable new receive/send |
| 12 | * TEST6: Unconnected socket |
| 13 | * |
| 14 | * The SCTP implementation is free software; |
| 15 | * you can redistribute it and/or modify it under the terms of |
| 16 | * the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2, or (at your option) |
| 18 | * any later version. |
| 19 | * |
| 20 | * The SCTP implementation is distributed in the hope that it |
| 21 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 22 | * ************************ |
| 23 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 24 | * See the GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with GNU CC; see the file COPYING. If not, write to |
| 28 | * the Free Software Foundation, 59 Temple Place - Suite 330, |
| 29 | * Boston, MA 02111-1307, USA. |
| 30 | * |
| 31 | * Please send any bug reports or fixes you make to the |
| 32 | * email address(es): |
| 33 | * lksctp developers <[email protected]> |
| 34 | * |
| 35 | * Or submit a bug report through the following website: |
| 36 | * https://github.com/sctp/lksctp-tools/ |
| 37 | * |
| 38 | * Any bugs reported given to us we will try to fix... any fixes shared will |
| 39 | * be incorporated into the next SCTP release |
| 40 | * |
| 41 | */ |
| 42 | |
| 43 | #include <stdio.h> |
| 44 | #include <errno.h> |
| 45 | #include <sys/socket.h> |
| 46 | #include <netinet/in.h> |
| 47 | #include <arpa/inet.h> |
| 48 | #include <linux/socket.h> |
| 49 | #include <netinet/sctp.h>cxx . . $ `<file>/src/include/netinet/sctp.h`/ |
| 50 | #include <sys/types.h> |
| 51 | #include <unistd.h> |
| 52 | #include <string.h> |
| 53 | #include <stdlib.h> |
| 54 | #include <sctputil.h>cxx . . $ `<file>/src/testlib/sctputil.h`/ |
| 55 | |
| 56 | char *TCIDcxx . . $ TCID. = __FILE__; |
| 57 | int TST_TOTALcxx . . $ TST_TOTAL. = 6; |
| 58 | int TST_CNTcxx . . $ TST_CNT. = 0; |
| 59 | |
| 60 | #define MAX_CLIENTScxx . . $ `src/func_tests/test_1_to_1_shutdown.c:60:9`! 10 |
| 61 | |
| 62 | int |
| 63 | maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 64 | { |
| 65 | int clnt_sk[MAX_CLIENTScxx . . $ `src/func_tests/test_1_to_1_shutdown.c:60:9`!], acpt_sk[MAX_CLIENTScxx . . $ `src/func_tests/test_1_to_1_shutdown.c:60:9`!],sk; |
| 66 | int lstn_sk; |
| 67 | struct sockaddr_incxx . . $ sockaddr_in# lstn_addr, acpt_addr; |
| 68 | socklen_tcxx . . $ socklen_t# addrlen; |
| 69 | int error, i; |
| 70 | char *message = "hello, world!\n"; |
| 71 | char msgbuf[100]; |
| 72 | int pf_class; |
| 73 | int fd, err_no = 0; |
| 74 | char filename[21]; |
| 75 | |
| 76 | /* Rather than fflush() throughout the code, set stdout to |
| 77 | * be unbuffered. |
| 78 | */ |
| 79 | 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); |
| 80 | 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); |
| 81 | |
| 82 | /* Initialize the server and client addresses. */ |
| 83 | pf_class = PF_INETcxx . . $ `<external>/8962547315054503424/socket.h:45:9`!; |
| 84 | |
| 85 | lstn_addr.sin_familycxx . . $ sockaddr_in#sin_family. = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!; |
| 86 | lstn_addr.sin_addrcxx . . $ sockaddr_in#sin_addr..s_addrcxx . . $ in_addr#s_addr. = SCTP_IP_LOOPBACKcxx . . $ `src/testlib/sctputil.h:76:9`!; |
| 87 | lstn_addr.sin_portcxx . . $ sockaddr_in#sin_port. = htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(SCTP_TESTPORT_1cxx . . $ `src/testlib/sctputil.h:71:9`!); |
| 88 | |
| 89 | 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 | 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`!); |
| 91 | |
| 92 | test_bindcxx . . $ test_bind(18e507052d945257).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *) &lstn_addr, sizeof(lstn_addr)); |
| 93 | |
| 94 | test_listencxx . . $ test_listen(9b79fb6aee4c0440).(lstn_sk, MAX_CLIENTScxx . . $ `src/func_tests/test_1_to_1_shutdown.c:60:9`!); |
| 95 | |
| 96 | for (i = 0; i < MAX_CLIENTScxx . . $ `src/func_tests/test_1_to_1_shutdown.c:60:9`!; i++) { |
| 97 | clnt_sk[i] = test_socketcxx . . $ test_socket(2326284602b0bb2d).(pf_class, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, IPPROTO_SCTPcxx . . $ `<external>/9581057695539925927/in.h:87:9`!); |
| 98 | test_connectcxx . . $ test_connect(18e507052d945257).(clnt_sk[i], (struct sockaddrcxx . . $ sockaddr# *)&lstn_addr, |
| 99 | sizeof(lstn_addr)); |
| 100 | } |
| 101 | |
| 102 | for (i = 0; i < MAX_CLIENTScxx . . $ `src/func_tests/test_1_to_1_shutdown.c:60:9`!; i++) { |
| 103 | addrlen = sizeof(acpt_addr); |
| 104 | acpt_sk[i] = test_acceptcxx . . $ test_accept(46fcd054f4a4d98b).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *)&acpt_addr, |
| 105 | &addrlen); |
| 106 | } |
| 107 | |
| 108 | /*shutdown() TEST1: Bad socket descriptor, EBADF Expected error*/ |
| 109 | error = shutdowncxx . . $ shutdown(9b79fb6aee4c0440).(-1, SHUT_WRcxx . . $ `<external>/14344695994193663403/socket.h:46:9`!); |
| 110 | if (error != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EBADFcxx . . $ `<external>/8987895939074593727/errno-base.h:13:9`!) |
| 111 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "shutdown with a bad socket " |
| 112 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 113 | |
| 114 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "shutdown() with a bad socket descriptor - EBADF"); |
| 115 | |
| 116 | /*shutdown() TEST2: Invalid socket, ENOTSOCK Expected error*/ |
| 117 | strcpycxx . . $ strcpy(b7b3a83273496486).(filename, "/tmp/sctptest.XXXXXX"); |
| 118 | fd = mkstempcxx . . $ mkstemp(1ef8e6ec083aabb9).(filename); |
| 119 | if (fd == -1) |
| 120 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "Failed to mkstemp %s: %s", |
| 121 | filename, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 122 | error = shutdowncxx . . $ shutdown(9b79fb6aee4c0440).(fd, SHUT_WRcxx . . $ `<external>/14344695994193663403/socket.h:46:9`!); |
| 123 | if (error == -1) |
| 124 | err_no = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 125 | closecxx . . $ close(7864480464b09eea).(fd); |
| 126 | unlinkcxx . . $ unlink(5c4306d8553b3696).(filename); |
| 127 | if (error != -1 || err_no != ENOTSOCKcxx . . $ `<external>/17922322188510340118/errno.h:72:9`!) |
| 128 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "shutdown with an invalid socket " |
| 129 | "error:%d, errno:%d", error, err_no); |
| 130 | |
| 131 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "shutdown() with an invalid socket - ENOTSOCK"); |
| 132 | |
| 133 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 134 | /*Do a send first before doing shutdown*/ |
| 135 | test_sendcxx . . $ test_send(9d1ebb2cd9604347).(acpt_sk[0], message, strlencxx . . $ strlen(751346f8406fd082).(message), 0); |
| 136 | |
| 137 | /*shutdown() TEST3: shutdown with SHUT_WR flag to disable new send*/ |
| 138 | error = shutdowncxx . . $ shutdown(9b79fb6aee4c0440).(clnt_sk[0], SHUT_WRcxx . . $ `<external>/14344695994193663403/socket.h:46:9`!); |
| 139 | if (error < 0) |
| 140 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "shutdown with SHUT_WR flag " |
| 141 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 142 | |
| 143 | /* Reading on a socket that has received SHUTDOWN should return 0 |
| 144 | * indicating EOF. |
| 145 | */ |
| 146 | error = recvcxx . . $ recv(4136b1fcb15a75da).(acpt_sk[0], msgbuf, 100, 0); |
| 147 | if ((error != 0) || (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != 0)) |
| 148 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recv on a SHUTDOWN received socket " |
| 149 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 150 | |
| 151 | /* Read the pending message on clnt_sk[0] that was received before |
| 152 | * SHUTDOWN call. |
| 153 | */ |
| 154 | test_recvcxx . . $ test_recv(6076e73854a1e617).(clnt_sk[0], msgbuf, 100, 0); |
| 155 | |
| 156 | /* No more messages and the association is SHUTDOWN, should fail. */ |
| 157 | error = recvcxx . . $ recv(4136b1fcb15a75da).(clnt_sk[0], msgbuf, 100, 0); |
| 158 | if ((error != -1) || (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOTCONNcxx . . $ `<external>/17922322188510340118/errno.h:91:9`!)) |
| 159 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recv on a SHUT_WR socket with no " |
| 160 | "messages error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 161 | |
| 162 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "shutdown() with SHUT_WR flag - SUCCESS"); |
| 163 | |
| 164 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 165 | |
| 166 | /*shutdown() TEST4: shutdown with SHUT_RD flag to disable new receive*/ |
| 167 | test_shutdowncxx . . $ test_shutdown(9b79fb6aee4c0440).(clnt_sk[1], SHUT_RDcxx . . $ `<external>/14344695994193663403/socket.h:44:9`!); |
| 168 | |
| 169 | error = recvcxx . . $ recv(4136b1fcb15a75da).(clnt_sk[1], msgbuf, 100, 0); |
| 170 | if ((error != 0) || (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != 0)) |
| 171 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recv on a SHUT_RD socket " |
| 172 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 173 | |
| 174 | /* Sending a message on SHUT_RD socket. */ |
| 175 | error = test_sendcxx . . $ test_send(9d1ebb2cd9604347).(clnt_sk[1], message, strlencxx . . $ strlen(751346f8406fd082).(message), 0); |
| 176 | if (error < 0) |
| 177 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "send on a SHUT_RD socket " |
| 178 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 179 | |
| 180 | /* Receive the message sent on SHUT_RD socket. */ |
| 181 | test_recvcxx . . $ test_recv(6076e73854a1e617).(acpt_sk[1], msgbuf, 100, 0); |
| 182 | |
| 183 | /* Send a message to the SHUT_RD socket. */ |
| 184 | test_sendcxx . . $ test_send(9d1ebb2cd9604347).(acpt_sk[1], message, strlencxx . . $ strlen(751346f8406fd082).(message), 0); |
| 185 | |
| 186 | /* We should not receive the message as the socket is SHUT_RD */ |
| 187 | error = recvcxx . . $ recv(4136b1fcb15a75da).(clnt_sk[1], msgbuf, 100, 0); |
| 188 | if ((error != 0) || (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != 0)) |
| 189 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recv on a SHUT_RD socket " |
| 190 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 191 | |
| 192 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "shutdown() with SHUT_RD flag - SUCCESS"); |
| 193 | |
| 194 | /*shutdown() TEST5: shutdown with SHUT_RDWR flag to disable new |
| 195 | receive/send*/ |
| 196 | test_shutdowncxx . . $ test_shutdown(9b79fb6aee4c0440).(clnt_sk[2], SHUT_RDWRcxx . . $ `<external>/14344695994193663403/socket.h:48:9`!); |
| 197 | |
| 198 | error = recvcxx . . $ recv(4136b1fcb15a75da).(acpt_sk[2], msgbuf, 100, 0); |
| 199 | if ((error != 0) || (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != 0)) |
| 200 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recv on a SHUTDOWN received socket " |
| 201 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 202 | |
| 203 | error = recvcxx . . $ recv(4136b1fcb15a75da).(clnt_sk[2], msgbuf, 100, 0); |
| 204 | if ((error != 0) || (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != 0)) |
| 205 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "recv on a SHUT_RDWR socket " |
| 206 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 207 | |
| 208 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "shutdown() with SHUT_RDWR flag - SUCCESS"); |
| 209 | |
| 210 | /*shutdown() TEST6: Unconnected socket, ENOTCONN Expected error*/ |
| 211 | error = shutdowncxx . . $ shutdown(9b79fb6aee4c0440).(sk, SHUT_RDcxx . . $ `<external>/14344695994193663403/socket.h:44:9`!); |
| 212 | if ((error != -1) || (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOTCONNcxx . . $ `<external>/17922322188510340118/errno.h:91:9`!)) |
| 213 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "shutdown on an unconnected socket " |
| 214 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 215 | |
| 216 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "shutdown() on an unconnected socket - SUCCESS"); |
| 217 | |
| 218 | for (i = 0; i < MAX_CLIENTScxx . . $ `src/func_tests/test_1_to_1_shutdown.c:60:9`!; i++) |
| 219 | closecxx . . $ close(7864480464b09eea).(clnt_sk[i]); |
| 220 | for (i = 0; i < MAX_CLIENTScxx . . $ `src/func_tests/test_1_to_1_shutdown.c:60:9`!; i++) |
| 221 | closecxx . . $ close(7864480464b09eea).(acpt_sk[i]); |
| 222 | |
| 223 | |
| 224 | closecxx . . $ close(7864480464b09eea).(lstn_sk); |
| 225 | closecxx . . $ close(7864480464b09eea).(sk); |
| 226 | |
| 227 | return 0; |
| 228 | } |