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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: getsockopt01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that getsockopt() returns the proper errno for various failure cases |
| 25 | * |
| 26 | * Usage: <for command-line> |
| 27 | * getsockopt01 [-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 | dcf2c3d | 2002-03-26 19:42:00 +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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 56 | char *TCID = "getsockopt01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | int testno; |
| 58 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | int s; /* socket descriptor */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | struct sockaddr_in sin0, fsin1; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | int sinlen; |
| 62 | int optval; |
| 63 | socklen_t optlen; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
| 65 | void setup(void), setup0(void), setup1(void), |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | cleanup(void), cleanup0(void), cleanup1(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
| 68 | struct test_case_t { /* test case structure */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | int domain; /* PF_INET, PF_UNIX, ... */ |
| 70 | int type; /* SOCK_STREAM, SOCK_DGRAM ... */ |
| 71 | int proto; /* protocol number (usually 0 = default) */ |
| 72 | int level; /* IPPROTO_* */ |
| 73 | int optname; |
| 74 | void *optval; |
| 75 | socklen_t *optlen; |
| 76 | struct sockaddr *sin; |
| 77 | int salen; |
| 78 | int retval; /* syscall return value */ |
| 79 | int experrno; /* expected errno */ |
| 80 | void (*setup) (void); |
| 81 | void (*cleanup) (void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | char *desc; |
| 83 | } tdat[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | { |
| 85 | PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, |
| 86 | &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1), |
| 87 | -1, EBADF, setup0, cleanup0, "bad file descriptor"} |
| 88 | , { |
| 89 | PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, |
| 90 | &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1), |
| 91 | -1, ENOTSOCK, setup0, cleanup0, "bad file descriptor"} |
| 92 | , |
mridge | 161b01e | 2006-02-01 22:48:00 +0000 | [diff] [blame] | 93 | #ifndef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | { |
| 95 | PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, 0, &optlen, |
| 96 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 97 | EFAULT, setup1, cleanup1, "invalid option buffer"} |
| 98 | , { |
| 99 | PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, 0, |
| 100 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 101 | EFAULT, setup1, cleanup1, "invalid optlen"} |
| 102 | , |
mridge | 161b01e | 2006-02-01 22:48:00 +0000 | [diff] [blame] | 103 | #endif /* UCLINUX */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | { |
| 105 | PF_INET, SOCK_STREAM, 0, 500, SO_OOBINLINE, &optval, &optlen, |
| 106 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 107 | EOPNOTSUPP, setup1, cleanup1, "invalid level"} |
| 108 | , { |
| 109 | PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval, |
| 110 | &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1), |
| 111 | -1, EOPNOTSUPP, setup1, cleanup1, "invalid option name"} |
| 112 | , { |
| 113 | PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval, |
| 114 | &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1), |
| 115 | -1, EOPNOTSUPP, setup1, cleanup1, |
| 116 | "invalid option name (UDP)"} |
| 117 | , { |
| 118 | PF_INET, SOCK_STREAM, 0, IPPROTO_IP, -1, &optval, &optlen, |
| 119 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 120 | ENOPROTOOPT, setup1, cleanup1, "invalid option name (IP)"} |
| 121 | , { |
| 122 | PF_INET, SOCK_STREAM, 0, IPPROTO_TCP, -1, &optval, &optlen, |
| 123 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 124 | ENOPROTOOPT, setup1, cleanup1, "invalid option name (TCP)"} |
| 125 | ,}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 127 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 131 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 132 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 134 | msg = parse_opts(argc, argv, NULL, NULL); |
| 135 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | setup(); |
| 140 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 142 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | tdat[testno].setup(); |
| 145 | |
| 146 | TEST(getsockopt(s, tdat[testno].level, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | tdat[testno].optname, |
| 148 | tdat[testno].optval, |
| 149 | tdat[testno].optlen)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | if (TEST_RETURN != tdat[testno].retval || |
| 151 | (TEST_RETURN < 0 && |
| 152 | TEST_ERRNO != tdat[testno].experrno)) { |
| 153 | tst_resm(TFAIL, "%s ; returned" |
subrata_modak | 358c3ee | 2009-10-26 14:55:46 +0000 | [diff] [blame] | 154 | " %ld (expected %d), errno %d (expected" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | " %d)", tdat[testno].desc, |
| 156 | TEST_RETURN, tdat[testno].retval, |
| 157 | TEST_ERRNO, tdat[testno].experrno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | } else { |
| 159 | tst_resm(TPASS, "%s successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | tdat[testno].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | } |
| 162 | tdat[testno].cleanup(); |
| 163 | } |
| 164 | } |
| 165 | cleanup(); |
| 166 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 167 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 168 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | { |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 172 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | /* initialize local sockaddr */ |
| 175 | sin0.sin_family = AF_INET; |
| 176 | sin0.sin_port = 0; |
| 177 | sin0.sin_addr.s_addr = INADDR_ANY; |
| 178 | } |
| 179 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | } |
| 183 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 184 | void setup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | { |
| 186 | if (tdat[testno].experrno == EBADF) |
| 187 | s = 400; /* anything not an open file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 188 | else if ((s = open("/dev/null", O_WRONLY)) == -1) |
| 189 | tst_brkm(TBROK, cleanup, "error opening /dev/null - " |
| 190 | "errno: %s", strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | } |
| 192 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 193 | void cleanup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | { |
| 195 | s = -1; |
| 196 | } |
| 197 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 198 | void setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | { |
| 200 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
| 201 | if (s < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 202 | tst_brkm(TBROK, cleanup, "socket setup failed for getsockopt: " |
| 203 | "%s", strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | } |
robbiew | 96d2337 | 2003-03-26 20:40:04 +0000 | [diff] [blame] | 205 | if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | tst_brkm(TBROK, cleanup, "socket bind failed for getsockopt: " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | "%s", strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | } |
| 209 | sinlen = sizeof(fsin1); |
| 210 | optlen = sizeof(optval); |
| 211 | } |
| 212 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | void cleanup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | (void)close(s); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | s = -1; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 217 | } |