| 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 accept () and close () call for |
| 6 | * 1-1 style sockets |
| 7 | * |
| 8 | * accept () Tests: |
| 9 | * --------------- |
| 10 | * TEST1: Bad socket descriptor |
| 11 | * TEST2: Invalid socket |
| 12 | * TEST3: Invalid address |
| 13 | * TEST4: On a non-listening socket |
| 14 | * TEST5: On a established socket |
| 15 | * TEST6: On a CLOSED association |
| 16 | * TEST7: Extracting the association on the listening socket |
| 17 | * |
| 18 | * close () Tests: |
| 19 | * -------------- |
| 20 | * TEST8: Bad socket descriptor |
| 21 | * TEST9: valid socket descriptor |
| 22 | * TEST10: Closed socket descriptor |
| 23 | * |
| 24 | * The SCTP implementation is free software; |
| 25 | * you can redistribute it and/or modify it under the terms of |
| 26 | * the GNU General Public License as published by |
| 27 | * the Free Software Foundation; either version 2, or (at your option) |
| 28 | * any later version. |
| 29 | * |
| 30 | * The SCTP implementation is distributed in the hope that it |
| 31 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 32 | * ************************ |
| 33 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 34 | * See the GNU General Public License for more details. |
| 35 | * |
| 36 | * You should have received a copy of the GNU General Public License |
| 37 | * along with GNU CC; see the file COPYING. If not, write to |
| 38 | * the Free Software Foundation, 59 Temple Place - Suite 330, |
| 39 | * Boston, MA 02111-1307, USA. |
| 40 | * |
| 41 | * Please send any bug reports or fixes you make to the |
| 42 | * email address(es): |
| 43 | * lksctp developers <[email protected]> |
| 44 | * |
| 45 | * Or submit a bug report through the following website: |
| 46 | * https://github.com/sctp/lksctp-tools/ |
| 47 | * |
| 48 | * Any bugs reported given to us we will try to fix... any fixes shared will |
| 49 | * be incorporated into the next SCTP release. |
| 50 | * |
| 51 | */ |
| 52 | |
| 53 | #include <stdio.h> |
| 54 | #include <unistd.h> |
| 55 | #include <fcntl.h> |
| 56 | #include <stdlib.h> |
| 57 | #include <string.h> |
| 58 | #include <sys/types.h> |
| 59 | #include <sys/socket.h> |
| 60 | #include <netinet/in.h> /* for sockaddr_in */ |
| 61 | #include <arpa/inet.h> |
| 62 | #include <errno.h> |
| 63 | #include <netinet/sctp.h>cxx . . $ `<file>/src/include/netinet/sctp.h`/ |
| 64 | #include <sys/uio.h> |
| 65 | #include <sctputil.h>cxx . . $ `<file>/src/testlib/sctputil.h`/ |
| 66 | |
| 67 | char *TCIDcxx . . $ TCID. = __FILE__; |
| 68 | int TST_TOTALcxx . . $ TST_TOTAL. = 10; |
| 69 | int TST_CNTcxx . . $ TST_CNT. = 0; |
| 70 | |
| 71 | #define SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`! 10 |
| 72 | |
| 73 | int |
| 74 | maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 75 | { |
| 76 | socklen_tcxx . . $ socklen_t# len; |
| 77 | int i; |
| 78 | int sk,lstn_sk,clnt_sk[SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`!],acpt_sk,pf_class; |
| 79 | int new_sk[SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`!],clnt2_sk[SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`!]; |
| 80 | int error; |
| 81 | int fd, err_no = 0; |
| 82 | char filename[21]; |
| 83 | |
| 84 | struct sockaddr_incxx . . $ sockaddr_in# conn_addr,lstn_addr,acpt_addr; |
| 85 | |
| 86 | /* Rather than fflush() throughout the code, set stdout to |
| 87 | * be unbuffered. |
| 88 | */ |
| 89 | 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); |
| 90 | 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); |
| 91 | |
| 92 | pf_class = PF_INETcxx . . $ `<external>/8962547315054503424/socket.h:45:9`!; |
| 93 | |
| 94 | 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`!); |
| 95 | |
| 96 | for (i=0 ; i < SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`! ; i++) |
| 97 | new_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 | |
| 99 | /* Creating a regular socket */ |
| 100 | for (i = 0 ; i < SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`! ; i++) |
| 101 | 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`!); |
| 102 | |
| 103 | for (i = 0 ; i < SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`! ; i++) |
| 104 | clnt2_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`!); |
| 105 | |
| 106 | /* Creating a listen socket */ |
| 107 | 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`!); |
| 108 | |
| 109 | conn_addr.sin_familycxx . . $ sockaddr_in#sin_family. = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!; |
| 110 | conn_addr.sin_addrcxx . . $ sockaddr_in#sin_addr..s_addrcxx . . $ in_addr#s_addr. = SCTP_IP_LOOPBACKcxx . . $ `src/testlib/sctputil.h:76:9`!; |
| 111 | conn_addr.sin_portcxx . . $ sockaddr_in#sin_port. = htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(SCTP_TESTPORT_1cxx . . $ `src/testlib/sctputil.h:71:9`!); |
| 112 | |
| 113 | lstn_addr.sin_familycxx . . $ sockaddr_in#sin_family. = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!; |
| 114 | lstn_addr.sin_addrcxx . . $ sockaddr_in#sin_addr..s_addrcxx . . $ in_addr#s_addr. = SCTP_IP_LOOPBACKcxx . . $ `src/testlib/sctputil.h:76:9`!; |
| 115 | lstn_addr.sin_portcxx . . $ sockaddr_in#sin_port. = htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(SCTP_TESTPORT_1cxx . . $ `src/testlib/sctputil.h:71:9`!); |
| 116 | |
| 117 | /* Binding the listen socket */ |
| 118 | test_bindcxx . . $ test_bind(18e507052d945257).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *) &lstn_addr, sizeof(lstn_addr)); |
| 119 | |
| 120 | /* Listening many sockets as we are calling too many connect here */ |
| 121 | test_listencxx . . $ test_listen(9b79fb6aee4c0440).(lstn_sk, SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`! ); |
| 122 | |
| 123 | /* connect() is called just to make sure accept() doesn't block the |
| 124 | * program |
| 125 | */ |
| 126 | i = 0; |
| 127 | len = sizeof(struct sockaddr_incxx . . $ sockaddr_in#); |
| 128 | test_connectcxx . . $ test_connect(18e507052d945257).(clnt_sk[i++], (struct sockaddrcxx . . $ sockaddr# *) &conn_addr, len); |
| 129 | |
| 130 | /* accept() TEST1: Bad socket descriptor EBADF, Expected error */ |
| 131 | error = acceptcxx . . $ accept(46fcd054f4a4d98b).(-1, (struct sockaddrcxx . . $ sockaddr# *) &acpt_addr, &len); |
| 132 | if (error != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EBADFcxx . . $ `<external>/8987895939074593727/errno-base.h:13:9`!) |
| 133 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "accept with a bad socket descriptor" |
| 134 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 135 | |
| 136 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "accept() with a bad socket descriptor - EBADF"); |
| 137 | |
| 138 | /*accept() TEST2: Invalid socket ENOTSOCK, Expected error*/ |
| 139 | strcpycxx . . $ strcpy(b7b3a83273496486).(filename, "/tmp/sctptest.XXXXXX"); |
| 140 | fd = mkstempcxx . . $ mkstemp(1ef8e6ec083aabb9).(filename); |
| 141 | if (fd == -1) |
| 142 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "Failed to mkstemp %s: %s", |
| 143 | filename, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 144 | error = acceptcxx . . $ accept(46fcd054f4a4d98b).(fd, (struct sockaddrcxx . . $ sockaddr# *) &acpt_addr, &len); |
| 145 | if (error == -1) |
| 146 | err_no = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 147 | closecxx . . $ close(7864480464b09eea).(fd); |
| 148 | unlinkcxx . . $ unlink(5c4306d8553b3696).(filename); |
| 149 | if (error != -1 || err_no != ENOTSOCKcxx . . $ `<external>/17922322188510340118/errno.h:72:9`!) |
| 150 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "accept with invalid socket" |
| 151 | "error:%d, errno:%d", error, err_no); |
| 152 | |
| 153 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "accept() with invalid socket - ENOTSOCK"); |
| 154 | |
| 155 | /*accept() TEST3: Invalid address EFAULT, Expected error*/ |
| 156 | error = acceptcxx . . $ accept(46fcd054f4a4d98b).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *) -1, &len); |
| 157 | if (error != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!) |
| 158 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "accept with invalid address" |
| 159 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 160 | |
| 161 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "accept() with invalid address - EFAULT"); |
| 162 | |
| 163 | test_connectcxx . . $ test_connect(18e507052d945257).(clnt_sk[i++], (struct sockaddrcxx . . $ sockaddr# *) &conn_addr, len); |
| 164 | |
| 165 | /*accept() TEST4: on a non-listening socket EINVAL, Expected error*/ |
| 166 | error = acceptcxx . . $ accept(46fcd054f4a4d98b).(sk, (struct sockaddrcxx . . $ sockaddr# *) &acpt_addr, &len); |
| 167 | if (error != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!) |
| 168 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "accept on a non-listening socket" |
| 169 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 170 | |
| 171 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "accept() on a non-listening socket - EINVAL"); |
| 172 | |
| 173 | test_connectcxx . . $ test_connect(18e507052d945257).(clnt_sk[i++], (struct sockaddrcxx . . $ sockaddr# *) &conn_addr, len); |
| 174 | |
| 175 | /*Calling accept to establish the connection*/ |
| 176 | acpt_sk = test_acceptcxx . . $ test_accept(46fcd054f4a4d98b).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *) &acpt_addr, &len); |
| 177 | |
| 178 | /* accept() TEST5: On a established socket EINVAL or EACCES when |
| 179 | * SELinux set to enforcing, Expected error. */ |
| 180 | error = acceptcxx . . $ accept(46fcd054f4a4d98b).(acpt_sk, (struct sockaddrcxx . . $ sockaddr# *) &acpt_addr, &len); |
| 181 | if (error != -1 || (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`! && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`!)) { |
| 182 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "accept on an established socket" |
| 183 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 184 | } |
| 185 | |
| 186 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "accept() on an established socket - %d", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 187 | |
| 188 | /*Closing the previously established association*/ |
| 189 | closecxx . . $ close(7864480464b09eea).(acpt_sk); |
| 190 | |
| 191 | test_connectcxx . . $ test_connect(18e507052d945257).(clnt_sk[i], (struct sockaddrcxx . . $ sockaddr# *) &conn_addr, len); |
| 192 | |
| 193 | /*accept() TEST6: On the CLOSED association should succeed*/ |
| 194 | acpt_sk = acceptcxx . . $ accept(46fcd054f4a4d98b).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *) &acpt_addr, &len); |
| 195 | if (acpt_sk < 0) |
| 196 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "accept a closed association" |
| 197 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 198 | |
| 199 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "accept() a closed association - SUCCESS"); |
| 200 | |
| 201 | closecxx . . $ close(7864480464b09eea).(acpt_sk); |
| 202 | |
| 203 | /*accept() TEST7: Extracting the association on the listening socket |
| 204 | as new socket, new socket socket descriptor should return*/ |
| 205 | for (i = 0 ; i < (SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`! - 1); i++) |
| 206 | test_connectcxx . . $ test_connect(18e507052d945257).(clnt2_sk[i], (struct sockaddrcxx . . $ sockaddr# *) &conn_addr, len); |
| 207 | |
| 208 | for (i = 0 ; i < (SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`! - 1); i++) |
| 209 | new_sk[i] = test_acceptcxx . . $ test_accept(46fcd054f4a4d98b).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *)&acpt_addr, |
| 210 | &len); |
| 211 | |
| 212 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "accept() on a listening socket - SUCCESS"); |
| 213 | |
| 214 | |
| 215 | /*close() TEST8: Bad socket descriptor, EBADF Expected error*/ |
| 216 | error = closecxx . . $ close(7864480464b09eea).(-1); |
| 217 | if (error != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EBADFcxx . . $ `<external>/8987895939074593727/errno-base.h:13:9`!) |
| 218 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "close with a bad socket descriptor " |
| 219 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 220 | |
| 221 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "close() with a bad socket descriptor - EBADF"); |
| 222 | |
| 223 | /*close() TEST9: valid socket descriptor should succeed*/ |
| 224 | error = closecxx . . $ close(7864480464b09eea).(sk); |
| 225 | if (error < 0) |
| 226 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "close with a valid socket descriptor" |
| 227 | " error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 228 | |
| 229 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "close() with a valid socket descriptor - SUCCESS"); |
| 230 | |
| 231 | /*close() TEST10: closed socket descriptor, EBADF Expected error*/ |
| 232 | error = closecxx . . $ close(7864480464b09eea).(sk); |
| 233 | if (error != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EBADFcxx . . $ `<external>/8987895939074593727/errno-base.h:13:9`!) |
| 234 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "close with a closed socket " |
| 235 | "descriptor error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 236 | |
| 237 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "close() with a closed socket descriptor - EBADF"); |
| 238 | |
| 239 | for (i = 0 ; i < SK_MAXcxx . . $ `src/func_tests/test_1_to_1_accept_close.c:71:9`! ; i++) { |
| 240 | closecxx . . $ close(7864480464b09eea).(clnt_sk[i]); |
| 241 | closecxx . . $ close(7864480464b09eea).(new_sk[i]); |
| 242 | closecxx . . $ close(7864480464b09eea).(clnt2_sk[i]); |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | } |