blob: ee9835679e80e575e7ba96101cda935c29f34fb9 [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: 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>
plarsbf553cc2002-03-26 20:46:30 +000047#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000048
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"
plars865695b2001-08-27 22:15:12 +000057
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020058char *TCID = "setsockopt01";
plars865695b2001-08-27 22:15:12 +000059int testno;
plars865695b2001-08-27 22:15:12 +000060
subrata_modak56207ce2009-03-23 13:35:39 +000061int s; /* socket descriptor */
plars865695b2001-08-27 22:15:12 +000062struct sockaddr_in sin0, fsin1;
subrata_modak56207ce2009-03-23 13:35:39 +000063int optval;
plars865695b2001-08-27 22:15:12 +000064
65void setup(void), setup0(void), setup1(void),
subrata_modak56207ce2009-03-23 13:35:39 +000066cleanup(void), cleanup0(void), cleanup1(void);
plars865695b2001-08-27 22:15:12 +000067
68struct test_case_t { /* test case structure */
subrata_modak56207ce2009-03-23 13:35:39 +000069 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);
plars865695b2001-08-27 22:15:12 +000082 char *desc;
83} tdat[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000084 {
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 ,
mreed1081534c32006-08-03 05:21:24 +000095#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +000096 {
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 ,
mreed1081534c32006-08-03 05:21:24 +0000102#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000103 {
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,};
plars865695b2001-08-27 22:15:12 +0000128
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200129int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
plars865695b2001-08-27 22:15:12 +0000130
subrata_modak56207ce2009-03-23 13:35:39 +0000131int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +0000132{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200133 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200134 const char *msg;
plars865695b2001-08-27 22:15:12 +0000135
Garrett Cooper45e285d2010-11-22 12:19:25 -0800136 msg = parse_opts(argc, argv, NULL, NULL);
137 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000138 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 }
plars865695b2001-08-27 22:15:12 +0000140 setup();
141
plars865695b2001-08-27 22:15:12 +0000142 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800143 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000144 for (testno = 0; testno < TST_TOTAL; ++testno) {
plars865695b2001-08-27 22:15:12 +0000145 tdat[testno].setup();
146
147 TEST(setsockopt(s, tdat[testno].level,
subrata_modak56207ce2009-03-23 13:35:39 +0000148 tdat[testno].optname,
149 tdat[testno].optval,
150 tdat[testno].optlen));
plars865695b2001-08-27 22:15:12 +0000151
152 if (TEST_RETURN == -1) {
subrata_modakbdbaec52009-02-26 12:14:51 +0000153 }
plars865695b2001-08-27 22:15:12 +0000154
155 if (TEST_RETURN != tdat[testno].retval ||
156 (TEST_RETURN < 0 &&
157 TEST_ERRNO != tdat[testno].experrno)) {
158 tst_resm(TFAIL, "%s ; returned"
subrata_modak923b23f2009-11-02 13:57:16 +0000159 " %ld (expected %d), errno %d (expected"
subrata_modak56207ce2009-03-23 13:35:39 +0000160 " %d)", tdat[testno].desc,
161 TEST_RETURN, tdat[testno].retval,
162 TEST_ERRNO, tdat[testno].experrno);
plars865695b2001-08-27 22:15:12 +0000163 } else {
164 tst_resm(TPASS, "%s successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000165 tdat[testno].desc);
plars865695b2001-08-27 22:15:12 +0000166 }
167 tdat[testno].cleanup();
168 }
169 }
170 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800171 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800172}
plars865695b2001-08-27 22:15:12 +0000173
subrata_modak56207ce2009-03-23 13:35:39 +0000174void setup(void)
plars865695b2001-08-27 22:15:12 +0000175{
Garrett Cooper2c282152010-12-16 00:55:50 -0800176
plars865695b2001-08-27 22:15:12 +0000177 tst_sig(NOFORK, DEF_HANDLER, cleanup);
178
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200179 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000180
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_modak56207ce2009-03-23 13:35:39 +0000187void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000188{
plars865695b2001-08-27 22:15:12 +0000189}
190
subrata_modak56207ce2009-03-23 13:35:39 +0000191void setup0(void)
plars865695b2001-08-27 22:15:12 +0000192{
193 if (tdat[testno].experrno == EBADF)
194 s = 400; /* anything not an open file */
subrata_modak56207ce2009-03-23 13:35:39 +0000195 else if ((s = open("/dev/null", O_WRONLY)) == -1)
plarsbf553cc2002-03-26 20:46:30 +0000196 tst_brkm(TBROK, cleanup, "error opening /dev/null - "
subrata_modak56207ce2009-03-23 13:35:39 +0000197 "errno: %s", strerror(errno));
plars865695b2001-08-27 22:15:12 +0000198}
199
subrata_modak56207ce2009-03-23 13:35:39 +0000200void cleanup0(void)
plars865695b2001-08-27 22:15:12 +0000201{
202 s = -1;
203}
204
subrata_modak56207ce2009-03-23 13:35:39 +0000205void setup1(void)
plars865695b2001-08-27 22:15:12 +0000206{
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_modak56207ce2009-03-23 13:35:39 +0000210 " %s", strerror(errno));
plars865695b2001-08-27 22:15:12 +0000211 }
subrata_modak56207ce2009-03-23 13:35:39 +0000212 if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
plars865695b2001-08-27 22:15:12 +0000213 tst_brkm(TBROK, cleanup, "socket bind failed for setsockopt:"
subrata_modak56207ce2009-03-23 13:35:39 +0000214 " %s", strerror(errno));
plars865695b2001-08-27 22:15:12 +0000215 }
216}
217
subrata_modak56207ce2009-03-23 13:35:39 +0000218void cleanup1(void)
plars865695b2001-08-27 22:15:12 +0000219{
subrata_modak56207ce2009-03-23 13:35:39 +0000220 (void)close(s);
plars865695b2001-08-27 22:15:12 +0000221 s = -1;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700222}