blob: 1fdb241474ed9fe38c1a5fe23b47778541f26806 [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: 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"
plars865695b2001-08-27 22:15:12 +000056
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020057char *TCID = "listen01";
plars865695b2001-08-27 22:15:12 +000058int testno;
59
subrata_modak56207ce2009-03-23 13:35:39 +000060int s; /* socket descriptor */
plars865695b2001-08-27 22:15:12 +000061
62void setup(void), setup0(void), setup1(void),
subrata_modak56207ce2009-03-23 13:35:39 +000063cleanup(void), cleanup0(void), cleanup1(void);
plars865695b2001-08-27 22:15:12 +000064
65struct test_case_t { /* test case structure */
subrata_modak56207ce2009-03-23 13:35:39 +000066 int domain; /* PF_INET, PF_UNIX, ... */
67 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
68 int proto; /* protocol number (usually 0 = default) */
69 int backlog; /* connect's 3rd argument */
70 int retval; /* syscall return value */
71 int experrno; /* expected errno */
72 void (*setup) (void);
73 void (*cleanup) (void);
plars865695b2001-08-27 22:15:12 +000074 char *desc;
75} tdat[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000076 {
77 0, 0, 0, 0, -1, EBADF, setup0, cleanup0, "bad file descriptor"}, {
78 0, 0, 0, 0, -1, ENOTSOCK, setup0, cleanup0, "not a socket"}, {
79PF_INET, SOCK_DGRAM, 0, 0, -1, EOPNOTSUPP, setup1, cleanup1,
80 "UDP listen"},};
plars865695b2001-08-27 22:15:12 +000081
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020082int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
plars865695b2001-08-27 22:15:12 +000083
subrata_modak56207ce2009-03-23 13:35:39 +000084int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +000085{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020086 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020087 const char *msg;
plars865695b2001-08-27 22:15:12 +000088
Garrett Cooper45e285d2010-11-22 12:19:25 -080089 msg = parse_opts(argc, argv, NULL, NULL);
90 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +000091 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000092 }
93
94 setup();
95
plars865695b2001-08-27 22:15:12 +000096 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080097 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +000098 for (testno = 0; testno < TST_TOTAL; ++testno) {
plars865695b2001-08-27 22:15:12 +000099 tdat[testno].setup();
100
101 TEST(listen(s, tdat[testno].backlog));
102 if (TEST_RETURN != tdat[testno].retval ||
103 (TEST_RETURN < 0 &&
104 TEST_ERRNO != tdat[testno].experrno)) {
105 tst_resm(TFAIL, "%s ; returned"
subrata_modak358c3ee2009-10-26 14:55:46 +0000106 " %ld (expected %d), errno %d (expected"
subrata_modak56207ce2009-03-23 13:35:39 +0000107 " %d)", tdat[testno].desc,
108 TEST_RETURN, tdat[testno].retval,
109 TEST_ERRNO, tdat[testno].experrno);
plars865695b2001-08-27 22:15:12 +0000110 } else {
plars865695b2001-08-27 22:15:12 +0000111 tst_resm(TPASS, "%s successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000112 tdat[testno].desc);
plars865695b2001-08-27 22:15:12 +0000113 }
114 tdat[testno].cleanup();
115 }
116 }
117 cleanup();
118
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800119 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800120}
plars865695b2001-08-27 22:15:12 +0000121
subrata_modak56207ce2009-03-23 13:35:39 +0000122void setup(void)
plars865695b2001-08-27 22:15:12 +0000123{
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200124 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000125}
126
subrata_modak56207ce2009-03-23 13:35:39 +0000127void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000128{
plars865695b2001-08-27 22:15:12 +0000129}
130
subrata_modak56207ce2009-03-23 13:35:39 +0000131void setup0(void)
plars865695b2001-08-27 22:15:12 +0000132{
133 if (tdat[testno].experrno == EBADF)
134 s = 400; /* anything not an open file */
subrata_modak56207ce2009-03-23 13:35:39 +0000135 else if ((s = open("/dev/null", O_WRONLY)) == -1)
plars0c49ebf2002-03-26 20:36:34 +0000136 tst_brkm(TBROK, cleanup, "error opening /dev/null - "
subrata_modak56207ce2009-03-23 13:35:39 +0000137 "errno: %s", strerror(errno));
plars865695b2001-08-27 22:15:12 +0000138}
139
subrata_modak56207ce2009-03-23 13:35:39 +0000140void cleanup0(void)
plars865695b2001-08-27 22:15:12 +0000141{
142 s = -1;
143}
144
subrata_modak56207ce2009-03-23 13:35:39 +0000145void setup1(void)
plars865695b2001-08-27 22:15:12 +0000146{
147 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
148 if (s < 0) {
149 tst_brkm(TBROK, cleanup, "socket setup failed for listen: "
subrata_modak56207ce2009-03-23 13:35:39 +0000150 "%s", strerror(errno));
plars865695b2001-08-27 22:15:12 +0000151 }
152}
153
subrata_modak56207ce2009-03-23 13:35:39 +0000154void cleanup1(void)
plars865695b2001-08-27 22:15:12 +0000155{
subrata_modak56207ce2009-03-23 13:35:39 +0000156 (void)close(s);
plars865695b2001-08-27 22:15:12 +0000157 s = -1;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700158}