plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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., 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> |
plars | 0c49ebf | 2002-03-26 20:36:34 +0000 | [diff] [blame] | 46 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | |
| 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 | |
| 58 | char *TCID="listen01"; /* Test program identifier. */ |
| 59 | int testno; |
| 60 | |
| 61 | int s; /* socket descriptor */ |
| 62 | |
| 63 | void setup(void), setup0(void), setup1(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 | 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 | |
| 85 | int TST_TOTAL=sizeof(tdat)/sizeof(tdat[0]); /* Total number of test cases. */ |
| 86 | |
| 87 | int exp_enos[] = {EBADF, ENOTSOCK, EOPNOTSUPP, 0}; |
| 88 | |
| 89 | extern int Tst_count; |
| 90 | |
| 91 | int |
| 92 | main(int argc, char *argv[]) |
| 93 | { |
| 94 | int lc; /* loop counter */ |
| 95 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
| 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_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame^] | 135 | return 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | } /* End main */ |
| 137 | |
| 138 | void |
| 139 | setup(void) |
| 140 | { |
| 141 | TEST_PAUSE; /* if -P option specified */ |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | cleanup(void) |
| 146 | { |
| 147 | TEST_CLEANUP; |
| 148 | tst_exit(); |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | setup0(void) |
| 153 | { |
| 154 | if (tdat[testno].experrno == EBADF) |
| 155 | s = 400; /* anything not an open file */ |
| 156 | else |
plars | 0c49ebf | 2002-03-26 20:36:34 +0000 | [diff] [blame] | 157 | if((s = open("/dev/null", O_WRONLY)) == -1) |
| 158 | tst_brkm(TBROK, cleanup, "error opening /dev/null - " |
| 159 | "errno: %s", strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void |
| 163 | cleanup0(void) |
| 164 | { |
| 165 | s = -1; |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | setup1(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 | |
| 178 | void |
| 179 | cleanup1(void) |
| 180 | { |
| 181 | (void) close(s); |
| 182 | s = -1; |
| 183 | } |