blob: db02919149de361b6c0baa73cf2a7a46fc718916 [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * Test Name: listen01
22 *
23 * Test Description:
24 * Verify that listen() returns the proper errno for various failure cases
25 *
26 * Usage: <for command-line>
27 * listen01 [-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>
plars0c49ebf2002-03-26 20:36:34 +000046#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000047
48#include <sys/types.h>
49#include <sys/socket.h>
50#include <sys/signal.h>
51#include <sys/un.h>
52
53#include <netinet/in.h>
54
55#include "test.h"
56#include "usctest.h"
57
58char *TCID="listen01"; /* Test program identifier. */
59int testno;
60
61int s; /* socket descriptor */
62
63void setup(void), setup0(void), setup1(void),
64 cleanup(void), cleanup0(void), cleanup1(void);
65
66struct 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 int backlog; /* connect's 3rd argument */
71 int retval; /* syscall return value */
72 int experrno; /* expected errno */
73 void (*setup)(void);
74 void (*cleanup)(void);
75 char *desc;
76} tdat[] = {
77 { 0, 0, 0, 0, -1, EBADF, setup0, cleanup0,
78 "bad file descriptor" },
79 { 0, 0, 0, 0, -1, ENOTSOCK, setup0, cleanup0,
80 "not a socket" },
81 { PF_INET, SOCK_DGRAM, 0, 0, -1, EOPNOTSUPP, setup1, cleanup1,
82 "UDP listen" },
83};
84
85int TST_TOTAL=sizeof(tdat)/sizeof(tdat[0]); /* Total number of test cases. */
86
87int exp_enos[] = {EBADF, ENOTSOCK, EOPNOTSUPP, 0};
88
89extern int Tst_count;
90
91int
92main(int argc, char *argv[])
93{
94 int lc; /* loop counter */
95 char *msg; /* message returned from parse_opts */
plars865695b2001-08-27 22:15:12 +000096
97 /* Parse standard options given to run the test. */
98 msg = parse_opts(argc, argv, (option_t *) NULL, NULL);
99 if (msg != (char *) NULL) {
100 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
101 tst_exit();
102 }
103
104 setup();
105
106 /* set up expected error numbers */
107 TEST_EXP_ENOS(exp_enos);
108
109 /* Check looping state if -i option given */
110 for (lc = 0; TEST_LOOPING(lc); ++lc) {
111 Tst_count = 0;
112 for (testno=0; testno < TST_TOTAL; ++testno) {
113 tdat[testno].setup();
114
115 TEST(listen(s, tdat[testno].backlog));
116 if (TEST_RETURN != tdat[testno].retval ||
117 (TEST_RETURN < 0 &&
118 TEST_ERRNO != tdat[testno].experrno)) {
119 tst_resm(TFAIL, "%s ; returned"
120 " %d (expected %d), errno %d (expected"
121 " %d)", tdat[testno].desc,
122 TEST_RETURN, tdat[testno].retval,
123 TEST_ERRNO, tdat[testno].experrno);
124 } else {
125 TEST_ERROR_LOG(TEST_ERRNO);
126 tst_resm(TPASS, "%s successful",
127 tdat[testno].desc);
128 }
129 tdat[testno].cleanup();
130 }
131 }
132 cleanup();
133
134 /*NOTREACHED*/
subrata_modak43337a32009-02-26 11:43:51 +0000135 return 0;
plars865695b2001-08-27 22:15:12 +0000136} /* End main */
137
138void
139setup(void)
140{
141 TEST_PAUSE; /* if -P option specified */
142}
143
144void
145cleanup(void)
146{
147 TEST_CLEANUP;
148 tst_exit();
149}
150
151void
152setup0(void)
153{
154 if (tdat[testno].experrno == EBADF)
155 s = 400; /* anything not an open file */
156 else
plars0c49ebf2002-03-26 20:36:34 +0000157 if((s = open("/dev/null", O_WRONLY)) == -1)
158 tst_brkm(TBROK, cleanup, "error opening /dev/null - "
159 "errno: %s", strerror(errno));
plars865695b2001-08-27 22:15:12 +0000160}
161
162void
163cleanup0(void)
164{
165 s = -1;
166}
167
168void
169setup1(void)
170{
171 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
172 if (s < 0) {
173 tst_brkm(TBROK, cleanup, "socket setup failed for listen: "
174 "%s", strerror(errno));
175 }
176}
177
178void
179cleanup1(void)
180{
181 (void) close(s);
182 s = -1;
183}