blob: c96663e7729a44e330f84f01d21faa3dcc88a2b2 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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>
plarsb9c945b2002-03-26 19:34:27 +000046#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000047
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"
plars865695b2001-08-27 22:15:12 +000055
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020056char *TCID = "bind01";
plars865695b2001-08-27 22:15:12 +000057int testno;
58
subrata_modak56207ce2009-03-23 13:35:39 +000059int s; /* socket descriptor */
plars865695b2001-08-27 22:15:12 +000060struct sockaddr_in sin1, sin2, sin3;
61struct sockaddr_un sun1;
62
63void setup(void), setup0(void), setup1(void), setup2(void),
subrata_modak56207ce2009-03-23 13:35:39 +000064cleanup(void), cleanup0(void), cleanup1(void);
plars865695b2001-08-27 22:15:12 +000065
66struct test_case_t { /* test case structure */
subrata_modak56207ce2009-03-23 13:35:39 +000067 int domain; /* PF_INET, PF_UNIX, ... */
68 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
69 int proto; /* protocol number (usually 0 = default) */
plars865695b2001-08-27 22:15:12 +000070 struct sockaddr *sockaddr; /* socket address buffer */
subrata_modak56207ce2009-03-23 13:35:39 +000071 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);
plars865695b2001-08-27 22:15:12 +000076 char *desc;
77} tdat[] = {
vapier7ec19d92006-02-27 04:38:56 +000078#ifndef UCLINUX
79/* Skip since uClinux does not implement memory protection */
subrata_modak56207ce2009-03-23 13:35:39 +000080 {
81 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)-1,
82 sizeof(struct sockaddr_in), -1, EFAULT, setup0,
83 cleanup0, "invalid sockaddr"},
vapier7ec19d92006-02-27 04:38:56 +000084#endif
subrata_modak56207ce2009-03-23 13:35:39 +000085 {
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,};
plars865695b2001-08-27 22:15:12 +0000103
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200104int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
plars865695b2001-08-27 22:15:12 +0000105
subrata_modak56207ce2009-03-23 13:35:39 +0000106int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +0000107{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200108 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200109 const char *msg;
plars865695b2001-08-27 22:15:12 +0000110
Garrett Cooper45e285d2010-11-22 12:19:25 -0800111 msg = parse_opts(argc, argv, NULL, NULL);
112 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000113 tst_brkm(TBROK, 0, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000114 }
115
116 setup();
117
plars865695b2001-08-27 22:15:12 +0000118 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800119 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000120
subrata_modak56207ce2009-03-23 13:35:39 +0000121 for (testno = 0; testno < TST_TOTAL; ++testno) {
plars865695b2001-08-27 22:15:12 +0000122 tdat[testno].setup();
123
subrata_modak56207ce2009-03-23 13:35:39 +0000124 TEST(bind
125 (s, tdat[testno].sockaddr, tdat[testno].salen));
plars865695b2001-08-27 22:15:12 +0000126 if (TEST_RETURN > 0) {
127 TEST_RETURN = 0;
128 } else {
plars865695b2001-08-27 22:15:12 +0000129 }
130 if (TEST_RETURN != tdat[testno].retval ||
131 (TEST_RETURN < 0 &&
132 TEST_ERRNO != tdat[testno].experrno)) {
133 tst_resm(TFAIL, "%s ; returned"
vapier8de367c2009-08-28 11:21:48 +0000134 " %ld (expected %d), errno %d (expected"
subrata_modak56207ce2009-03-23 13:35:39 +0000135 " %d)", tdat[testno].desc,
136 TEST_RETURN, tdat[testno].retval,
137 TEST_ERRNO, tdat[testno].experrno);
plars865695b2001-08-27 22:15:12 +0000138 } else {
139 tst_resm(TPASS, "%s successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000140 tdat[testno].desc);
plars865695b2001-08-27 22:15:12 +0000141 }
142 tdat[testno].cleanup();
143 }
144 }
145 cleanup();
robbiew2c945242002-11-11 19:01:25 +0000146
Garrett Cooper2c282152010-12-16 00:55:50 -0800147 tst_exit();
148}
plars865695b2001-08-27 22:15:12 +0000149
subrata_modak56207ce2009-03-23 13:35:39 +0000150void setup(void)
plars865695b2001-08-27 22:15:12 +0000151{
plars865695b2001-08-27 22:15:12 +0000152
subrata_modak56207ce2009-03-23 13:35:39 +0000153 TEST_PAUSE; /* if -p option specified */
plars865695b2001-08-27 22:15:12 +0000154
155 /* initialize sockaddr's */
156 sin1.sin_family = AF_INET;
subrata_modak56207ce2009-03-23 13:35:39 +0000157 /* this port must be unused! */
Jan Stancek6e4e77a2014-03-07 11:39:56 +0100158 sin1.sin_port = tst_get_unused_port(NULL, AF_INET, SOCK_STREAM);
plars865695b2001-08-27 22:15:12 +0000159 sin1.sin_addr.s_addr = INADDR_ANY;
160
161 sin2.sin_family = AF_INET;
162 sin2.sin_port = 0;
163 sin2.sin_addr.s_addr = INADDR_ANY;
164
165 sin3.sin_family = AF_INET;
166 sin3.sin_port = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000167 /* assumes 10.255.254.253 is not a local interface address! */
plars865695b2001-08-27 22:15:12 +0000168 sin3.sin_addr.s_addr = htonl(0x0AFFFEFD);
169
170 sun1.sun_family = AF_UNIX;
171 strncpy(sun1.sun_path, ".", sizeof(sun1.sun_path));
172
173}
174
subrata_modak56207ce2009-03-23 13:35:39 +0000175void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000176{
plars865695b2001-08-27 22:15:12 +0000177}
178
subrata_modak56207ce2009-03-23 13:35:39 +0000179void setup0(void)
plars865695b2001-08-27 22:15:12 +0000180{
181 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
vapier8de367c2009-08-28 11:21:48 +0000182 if (s < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183 tst_brkm(TBROK | TERRNO, cleanup,
184 "socket() failed for bind test %d", testno);
plars865695b2001-08-27 22:15:12 +0000185}
186
subrata_modak56207ce2009-03-23 13:35:39 +0000187void cleanup0(void)
plars865695b2001-08-27 22:15:12 +0000188{
subrata_modak56207ce2009-03-23 13:35:39 +0000189 (void)close(s);
plars865695b2001-08-27 22:15:12 +0000190}
191
subrata_modak56207ce2009-03-23 13:35:39 +0000192void setup1(void)
plars865695b2001-08-27 22:15:12 +0000193{
plarsb9c945b2002-03-26 19:34:27 +0000194 /* setup for the "not a socket" case */
subrata_modak56207ce2009-03-23 13:35:39 +0000195 if ((s = open("/dev/null", O_WRONLY)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800196 tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed");
plarsb9c945b2002-03-26 19:34:27 +0000197
plars865695b2001-08-27 22:15:12 +0000198}
199
subrata_modak56207ce2009-03-23 13:35:39 +0000200void cleanup1(void)
plars865695b2001-08-27 22:15:12 +0000201{
202 s = -1;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700203}