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