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: getsockname01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that getsockname() returns the proper errno for various failure cases |
| 25 | * |
| 26 | * Usage: <for command-line> |
| 27 | * getsockname01 [-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 | #include <stdio.h> |
| 43 | #include <unistd.h> |
| 44 | #include <errno.h> |
plars | 94d7956 | 2002-03-26 19:39:42 +0000 | [diff] [blame] | 45 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | |
| 47 | #include <sys/types.h> |
| 48 | #include <sys/socket.h> |
| 49 | #include <sys/signal.h> |
| 50 | #include <sys/ioctl.h> |
| 51 | |
| 52 | #include <netinet/in.h> |
| 53 | |
| 54 | #include "test.h" |
| 55 | #include "usctest.h" |
| 56 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 57 | char *TCID = "getsockname01"; /* Test program identifier. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | int testno; |
| 59 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 60 | int s; /* socket descriptor */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | struct sockaddr_in sin0, fsin1; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 62 | socklen_t sinlen; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 64 | void setup(void), setup0(void), setup1(void), |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 65 | cleanup(void), cleanup0(void), cleanup1(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
| 67 | struct test_case_t { /* test case structure */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 68 | int domain; /* PF_INET, PF_UNIX, ... */ |
| 69 | int type; /* SOCK_STREAM, SOCK_DGRAM ... */ |
| 70 | int proto; /* protocol number (usually 0 = default) */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | struct sockaddr *sockaddr; /* socket address buffer */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 72 | socklen_t *salen; /* getsockname's 3rd argument */ |
| 73 | int retval; /* syscall return value */ |
| 74 | int experrno; /* expected errno */ |
| 75 | void (*setup) (void); |
| 76 | void (*cleanup) (void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | char *desc; |
| 78 | } tdat[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 79 | { |
| 80 | PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1, |
| 81 | &sinlen, -1, EBADF, setup0, cleanup0, |
| 82 | "bad file descriptor"}, { |
| 83 | PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1, |
| 84 | &sinlen, -1, ENOTSOCK, setup0, cleanup0, |
| 85 | "bad file descriptor"}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 86 | #ifndef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 87 | /* Skip since uClinux does not implement memory protection */ |
| 88 | { |
| 89 | PF_INET, SOCK_STREAM, 0, (struct sockaddr *)0, |
| 90 | &sinlen, -1, EFAULT, setup1, cleanup1, |
| 91 | "invalid socket buffer"}, { |
| 92 | PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1, |
| 93 | (socklen_t *) 1, -1, EFAULT, setup1, cleanup1, |
| 94 | "invalid salen"}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 95 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 98 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); /* Total number of test cases. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 100 | int exp_enos[] = { EBADF, ENOTSOCK, EFAULT, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | extern int Tst_count; |
| 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 104 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | { |
| 106 | int lc; /* loop counter */ |
| 107 | char *msg; /* message returned from parse_opts */ |
| 108 | |
| 109 | /* Parse standard options given to run the test. */ |
| 110 | msg = parse_opts(argc, argv, (option_t *) NULL, NULL); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 111 | if (msg != (char *)NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 113 | tst_exit(); |
| 114 | } |
| 115 | |
| 116 | setup(); |
| 117 | |
| 118 | /* Check looping state if -i option given */ |
| 119 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
| 120 | Tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 121 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | tdat[testno].setup(); |
| 123 | |
| 124 | TEST(getsockname(s, tdat[testno].sockaddr, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 125 | tdat[testno].salen)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | TEST_ERROR_LOG(TEST_ERRNO); |
| 127 | if (TEST_RETURN != tdat[testno].retval || |
| 128 | (TEST_RETURN < 0 && |
| 129 | TEST_ERRNO != tdat[testno].experrno)) { |
| 130 | tst_resm(TFAIL, "%s ; returned" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 131 | " %d (expected %d), errno %d (expected" |
| 132 | " %d)", tdat[testno].desc, |
| 133 | TEST_RETURN, tdat[testno].retval, |
| 134 | TEST_ERRNO, tdat[testno].experrno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } else { |
| 136 | tst_resm(TPASS, "%s successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 137 | tdat[testno].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | } |
| 139 | tdat[testno].cleanup(); |
| 140 | } |
| 141 | } |
| 142 | cleanup(); |
| 143 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 144 | /*NOTREACHED*/ return 0; |
| 145 | } /* End main */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 147 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 149 | TEST_PAUSE; /* if -P option specified */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
| 151 | /* set up expected error numbers */ |
| 152 | TEST_EXP_ENOS(exp_enos); |
| 153 | |
| 154 | /* initialize local sockaddr */ |
| 155 | sin0.sin_family = AF_INET; |
| 156 | sin0.sin_port = 0; |
| 157 | sin0.sin_addr.s_addr = INADDR_ANY; |
| 158 | } |
| 159 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 160 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | { |
| 162 | TEST_CLEANUP; |
| 163 | tst_exit(); |
| 164 | } |
| 165 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 166 | void setup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | { |
| 168 | if (tdat[testno].experrno == EBADF) |
| 169 | s = 400; /* anything not an open file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 170 | else if ((s = open("/dev/null", O_WRONLY)) == -1) |
| 171 | tst_brkm(TBROK, cleanup, "error opening /dev/null - " |
| 172 | "errno: %s", strerror(errno)); |
plars | 94d7956 | 2002-03-26 19:39:42 +0000 | [diff] [blame] | 173 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | } |
| 175 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 176 | void cleanup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | { |
| 178 | s = -1; |
| 179 | } |
| 180 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 181 | void setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | { |
| 183 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
| 184 | if (s < 0) { |
| 185 | tst_brkm(TBROK, cleanup, "socket setup failed for getsockname " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 186 | "test %d: %s", testno, strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | } |
robbiew | 96d2337 | 2003-03-26 20:40:04 +0000 | [diff] [blame] | 188 | if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | tst_brkm(TBROK, cleanup, "socket bind failed for getsockname " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 190 | "test %d: %s", testno, strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | } |
| 192 | sinlen = sizeof(fsin1); |
| 193 | } |
| 194 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 195 | void cleanup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 197 | (void)close(s); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | s = -1; |
| 199 | } |