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: setsockopt01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that setsockopt() returns the proper errno for various failure cases |
| 25 | * |
| 26 | * Usage: <for command-line> |
| 27 | * setsockopt01 [-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 John George |
| 37 | * -Ported |
| 38 | * |
| 39 | * Restrictions: |
| 40 | * None. |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | #include <unistd.h> |
| 46 | #include <errno.h> |
plars | bf553cc | 2002-03-26 20:46:30 +0000 | [diff] [blame] | 47 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
| 49 | #include <sys/types.h> |
| 50 | #include <sys/socket.h> |
| 51 | #include <sys/signal.h> |
| 52 | #include <sys/ioctl.h> |
| 53 | |
| 54 | #include <netinet/in.h> |
| 55 | |
| 56 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 58 | char *TCID = "setsockopt01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | int testno; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | int s; /* socket descriptor */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | struct sockaddr_in sin0, fsin1; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | int optval; |
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 | int 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 | sizeof(optval), (struct sockaddr *)&fsin1, |
| 87 | sizeof(fsin1), -1, EBADF, setup0, cleanup0, |
| 88 | "bad file descriptor"} |
| 89 | , { |
| 90 | PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, |
| 91 | sizeof(optval), (struct sockaddr *)&fsin1, |
| 92 | sizeof(fsin1), -1, ENOTSOCK, setup0, cleanup0, |
| 93 | "bad file descriptor"} |
| 94 | , |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 95 | #if !defined(UCLINUX) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | { |
| 97 | PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, 0, |
| 98 | sizeof(optval), (struct sockaddr *)&fsin1, |
| 99 | sizeof(fsin1), -1, EFAULT, setup1, cleanup1, |
| 100 | "invalid option buffer"} |
| 101 | , |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 102 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | { |
| 104 | PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, 0, |
| 105 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 106 | EINVAL, setup1, cleanup1, "invalid optlen"} |
| 107 | , { |
| 108 | PF_INET, SOCK_STREAM, 0, 500, SO_OOBINLINE, &optval, |
| 109 | sizeof(optval), (struct sockaddr *)&fsin1, |
| 110 | sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1, |
| 111 | "invalid level"} |
| 112 | , { |
| 113 | PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval, |
| 114 | sizeof(optval), (struct sockaddr *)&fsin1, |
| 115 | sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1, |
| 116 | "invalid option name (UDP)"} |
| 117 | , { |
| 118 | PF_INET, SOCK_STREAM, 0, IPPROTO_IP, -1, &optval, |
| 119 | sizeof(optval), (struct sockaddr *)&fsin1, |
| 120 | sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1, |
| 121 | "invalid option name (IP)"} |
| 122 | , { |
| 123 | PF_INET, SOCK_STREAM, 0, IPPROTO_TCP, -1, &optval, |
| 124 | sizeof(optval), (struct sockaddr *)&fsin1, |
| 125 | sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1, |
| 126 | "invalid option name (TCP)"} |
| 127 | ,}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 129 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 133 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 134 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 136 | msg = parse_opts(argc, argv, NULL, NULL); |
| 137 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | setup(); |
| 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 143 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | tdat[testno].setup(); |
| 146 | |
| 147 | TEST(setsockopt(s, tdat[testno].level, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | tdat[testno].optname, |
| 149 | tdat[testno].optval, |
| 150 | tdat[testno].optlen)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | |
| 152 | if (TEST_RETURN == -1) { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 153 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
| 155 | if (TEST_RETURN != tdat[testno].retval || |
| 156 | (TEST_RETURN < 0 && |
| 157 | TEST_ERRNO != tdat[testno].experrno)) { |
| 158 | tst_resm(TFAIL, "%s ; returned" |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 159 | " %ld (expected %d), errno %d (expected" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | " %d)", tdat[testno].desc, |
| 161 | TEST_RETURN, tdat[testno].retval, |
| 162 | TEST_ERRNO, tdat[testno].experrno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | } else { |
| 164 | tst_resm(TPASS, "%s successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | tdat[testno].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } |
| 167 | tdat[testno].cleanup(); |
| 168 | } |
| 169 | } |
| 170 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 171 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 172 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 176 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 178 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 179 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | |
| 181 | /* initialize local sockaddr */ |
| 182 | sin0.sin_family = AF_INET; |
| 183 | sin0.sin_port = 0; |
| 184 | sin0.sin_addr.s_addr = INADDR_ANY; |
| 185 | } |
| 186 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 187 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | } |
| 190 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 191 | void setup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | { |
| 193 | if (tdat[testno].experrno == EBADF) |
| 194 | s = 400; /* anything not an open file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 195 | else if ((s = open("/dev/null", O_WRONLY)) == -1) |
plars | bf553cc | 2002-03-26 20:46:30 +0000 | [diff] [blame] | 196 | tst_brkm(TBROK, cleanup, "error opening /dev/null - " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | "errno: %s", strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | } |
| 199 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 200 | void cleanup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | { |
| 202 | s = -1; |
| 203 | } |
| 204 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 205 | void setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | { |
| 207 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
| 208 | if (s < 0) { |
| 209 | tst_brkm(TBROK, cleanup, "socket setup failed for setsockopt:" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | " %s", strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 211 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | tst_brkm(TBROK, cleanup, "socket bind failed for setsockopt:" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 214 | " %s", strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 218 | void cleanup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | (void)close(s); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | s = -1; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 222 | } |