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: bind01 22 * 23 * Test Description: 24 * Verify that bind() returns the proper errno for various failure cases 25 * 26 * Usage: <for command-line> 27 * bind01 [-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 Ported by Wayne Boyer 37 * 38 * RESTRICTIONS: 39 * None. 40 * 41 */ 42 43 #include <stdio.h> 44 #include <unistd.h> 45 #include <errno.h> 46 #include <fcntl.h> 47 48 #include <sys/types.h> 49 #include <sys/socket.h> 50 #include <sys/un.h> 51 52 #include <netinet/in.h> 53 54 #include "test.h" 55 #include "safe_macros.h" 56 57 char *TCID = "bind01"; 58 int testno; 59 60 int s; /* socket descriptor */ 61 struct sockaddr_in sin1, sin2, sin3; 62 struct sockaddr_un sun1; 63 64 void setup(void), setup0(void), setup1(void), setup2(void), 65 cleanup(void), cleanup0(void), cleanup1(void); 66 67 struct test_case_t { /* test case structure */ 68 int domain; /* PF_INET, PF_UNIX, ... */ 69 int type; /* SOCK_STREAM, SOCK_DGRAM ... */ 70 int proto; /* protocol number (usually 0 = default) */ 71 struct sockaddr *sockaddr; /* socket address buffer */ 72 int salen; /* bind's 3rd argument */ 73 int retval; /* syscall return value */ 74 int experrno; /* expected errno */ 75 void (*setup) (void); 76 void (*cleanup) (void); 77 char *desc; 78 } tdat[] = { 79 #ifndef UCLINUX 80 /* Skip since uClinux does not implement memory protection */ 81 { 82 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)-1, 83 sizeof(struct sockaddr_in), -1, EFAULT, setup0, 84 cleanup0, "invalid sockaddr"}, 85 #endif 86 { 87 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin1, 88 3, -1, EINVAL, setup0, cleanup0, "invalid salen"}, { 89 0, 0, 0, (struct sockaddr *)&sin1, 90 sizeof(sin1), -1, ENOTSOCK, setup1, cleanup1, 91 "invalid socket"} 92 , { 93 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin2, 94 sizeof(sin2), 0, 0, setup0, cleanup0, "INADDR_ANYPORT"} 95 , { 96 PF_UNIX, SOCK_STREAM, 0, (struct sockaddr *)&sun1, 97 sizeof(sun1), -1, EADDRINUSE, setup0, cleanup0, 98 "UNIX-domain of current directory"} 99 , { 100 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin3, 101 sizeof(sin3), -1, EADDRNOTAVAIL, setup0, cleanup0, 102 "non-local address"} 103 ,}; 104 105 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); 106 107 int main(int argc, char *argv[]) 108 { 109 int lc; 110 111 tst_parse_opts(argc, argv, NULL, NULL); 112 113 setup(); 114 115 for (lc = 0; TEST_LOOPING(lc); ++lc) { 116 tst_count = 0; 117 118 for (testno = 0; testno < TST_TOTAL; ++testno) { 119 tdat[testno].setup(); 120 121 TEST(bind 122 (s, tdat[testno].sockaddr, tdat[testno].salen)); 123 if (TEST_RETURN > 0) { 124 TEST_RETURN = 0; 125 } else { 126 } 127 if (TEST_RETURN != tdat[testno].retval || 128 (TEST_RETURN < 0 && 129 TEST_ERRNO != tdat[testno].experrno)) { 130 tst_resm(TFAIL, "%s ; returned" 131 " %ld (expected %d), errno %d (expected" 132 " %d)", tdat[testno].desc, 133 TEST_RETURN, tdat[testno].retval, 134 TEST_ERRNO, tdat[testno].experrno); 135 } else { 136 tst_resm(TPASS, "%s successful", 137 tdat[testno].desc); 138 } 139 tdat[testno].cleanup(); 140 } 141 } 142 cleanup(); 143 144 tst_exit(); 145 } 146 147 void setup(void) 148 { 149 150 TEST_PAUSE; /* if -p option specified */ 151 152 /* initialize sockaddr's */ 153 sin1.sin_family = AF_INET; 154 /* this port must be unused! */ 155 sin1.sin_port = tst_get_unused_port(NULL, AF_INET, SOCK_STREAM); 156 sin1.sin_addr.s_addr = INADDR_ANY; 157 158 sin2.sin_family = AF_INET; 159 sin2.sin_port = 0; 160 sin2.sin_addr.s_addr = INADDR_ANY; 161 162 sin3.sin_family = AF_INET; 163 sin3.sin_port = 0; 164 /* assumes 10.255.254.253 is not a local interface address! */ 165 sin3.sin_addr.s_addr = htonl(0x0AFFFEFD); 166 167 sun1.sun_family = AF_UNIX; 168 strncpy(sun1.sun_path, ".", sizeof(sun1.sun_path)); 169 170 } 171 172 void cleanup(void) 173 { 174 } 175 176 void setup0(void) 177 { 178 s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type, 179 tdat[testno].proto); 180 } 181 182 void cleanup0(void) 183 { 184 (void)close(s); 185 } 186 187 void setup1(void) 188 { 189 /* setup for the "not a socket" case */ 190 if ((s = open("/dev/null", O_WRONLY)) == -1) 191 tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed"); 192 193 } 194 195 void cleanup1(void) 196 { 197 s = -1; 198 } 199