1 /* 2 * 3 * Copyright (c) International Business Machines Corp., 2001 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13 * the GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /* 21 * Test Name: setsockopt01 22 * 23 * Test Description: 24 * Verify that setsockopt() returns the proper errno for various failure cases 25 * 26 * Usage: <for command-line> 27 * setsockopt01 [-c n] [-e] [-i n] [-I x] [-p x] [-t] 28 * where, -c n : Run n copies concurrently. 29 * -e : Turn on errno logging. 30 * -i n : Execute test n times. 31 * -I x : Execute test for x seconds. 32 * -P x : Pause for x seconds between iterations. 33 * -t : Turn on syscall timing. 34 * 35 * History 36 * 07/2001 John George 37 * -Ported 38 * 39 * Restrictions: 40 * None. 41 * 42 */ 43 44 #include <stdio.h> 45 #include <unistd.h> 46 #include <errno.h> 47 #include <fcntl.h> 48 49 #include <sys/types.h> 50 #include <sys/socket.h> 51 #include <sys/signal.h> 52 #include <sys/ioctl.h> 53 54 #include <netinet/in.h> 55 56 #include "test.h" 57 #include "safe_macros.h" 58 59 char *TCID = "setsockopt01"; 60 int testno; 61 62 int s; /* socket descriptor */ 63 struct sockaddr_in sin0, fsin1; 64 int optval; 65 66 void setup(void), setup0(void), setup1(void), 67 cleanup(void), cleanup0(void), cleanup1(void); 68 69 struct test_case_t { /* test case structure */ 70 int domain; /* PF_INET, PF_UNIX, ... */ 71 int type; /* SOCK_STREAM, SOCK_DGRAM ... */ 72 int proto; /* protocol number (usually 0 = default) */ 73 int level; /* IPPROTO_* */ 74 int optname; 75 void *optval; 76 int optlen; 77 struct sockaddr *sin; 78 int salen; 79 int retval; /* syscall return value */ 80 int experrno; /* expected errno */ 81 void (*setup) (void); 82 void (*cleanup) (void); 83 char *desc; 84 } tdat[] = { 85 { 86 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, 87 sizeof(optval), (struct sockaddr *)&fsin1, 88 sizeof(fsin1), -1, EBADF, setup0, cleanup0, 89 "bad file descriptor"} 90 , { 91 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, 92 sizeof(optval), (struct sockaddr *)&fsin1, 93 sizeof(fsin1), -1, ENOTSOCK, setup0, cleanup0, 94 "bad file descriptor"} 95 , 96 #if !defined(UCLINUX) 97 { 98 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, 0, 99 sizeof(optval), (struct sockaddr *)&fsin1, 100 sizeof(fsin1), -1, EFAULT, setup1, cleanup1, 101 "invalid option buffer"} 102 , 103 #endif 104 { 105 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, 0, 106 (struct sockaddr *)&fsin1, sizeof(fsin1), -1, 107 EINVAL, setup1, cleanup1, "invalid optlen"} 108 , { 109 PF_INET, SOCK_STREAM, 0, 500, SO_OOBINLINE, &optval, 110 sizeof(optval), (struct sockaddr *)&fsin1, 111 sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1, 112 "invalid level"} 113 , { 114 PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval, 115 sizeof(optval), (struct sockaddr *)&fsin1, 116 sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1, 117 "invalid option name (UDP)"} 118 , { 119 PF_INET, SOCK_STREAM, 0, IPPROTO_IP, -1, &optval, 120 sizeof(optval), (struct sockaddr *)&fsin1, 121 sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1, 122 "invalid option name (IP)"} 123 , { 124 PF_INET, SOCK_STREAM, 0, IPPROTO_TCP, -1, &optval, 125 sizeof(optval), (struct sockaddr *)&fsin1, 126 sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1, 127 "invalid option name (TCP)"} 128 ,}; 129 130 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); 131 132 int main(int argc, char *argv[]) 133 { 134 int lc; 135 136 tst_parse_opts(argc, argv, NULL, NULL); 137 setup(); 138 139 for (lc = 0; TEST_LOOPING(lc); ++lc) { 140 tst_count = 0; 141 for (testno = 0; testno < TST_TOTAL; ++testno) { 142 tdat[testno].setup(); 143 144 TEST(setsockopt(s, tdat[testno].level, 145 tdat[testno].optname, 146 tdat[testno].optval, 147 tdat[testno].optlen)); 148 149 if (TEST_RETURN == -1) { 150 } 151 152 if (TEST_RETURN != tdat[testno].retval || 153 (TEST_RETURN < 0 && 154 TEST_ERRNO != tdat[testno].experrno)) { 155 tst_resm(TFAIL, "%s ; returned" 156 " %ld (expected %d), errno %d (expected" 157 " %d)", tdat[testno].desc, 158 TEST_RETURN, tdat[testno].retval, 159 TEST_ERRNO, tdat[testno].experrno); 160 } else { 161 tst_resm(TPASS, "%s successful", 162 tdat[testno].desc); 163 } 164 tdat[testno].cleanup(); 165 } 166 } 167 cleanup(); 168 tst_exit(); 169 } 170 171 void setup(void) 172 { 173 174 tst_sig(NOFORK, DEF_HANDLER, cleanup); 175 176 TEST_PAUSE; 177 178 /* initialize local sockaddr */ 179 sin0.sin_family = AF_INET; 180 sin0.sin_port = 0; 181 sin0.sin_addr.s_addr = INADDR_ANY; 182 } 183 184 void cleanup(void) 185 { 186 } 187 188 void setup0(void) 189 { 190 if (tdat[testno].experrno == EBADF) 191 s = 400; /* anything not an open file */ 192 else if ((s = open("/dev/null", O_WRONLY)) == -1) 193 tst_brkm(TBROK, cleanup, "error opening /dev/null - " 194 "errno: %s", strerror(errno)); 195 } 196 197 void cleanup0(void) 198 { 199 s = -1; 200 } 201 202 void setup1(void) 203 { 204 s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type, 205 tdat[testno].proto); 206 SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0)); 207 } 208 209 void cleanup1(void) 210 { 211 (void)close(s); 212 s = -1; 213 } 214