blob: 867a5407dca7d871f5ed9308918621813cf193e2 [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: 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>
plarsdcf2c3d2002-03-26 19:42:00 +000045#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000046
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"
plars865695b2001-08-27 22:15:12 +000055
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020056char *TCID = "getsockopt01";
plars865695b2001-08-27 22:15:12 +000057int testno;
58
subrata_modak56207ce2009-03-23 13:35:39 +000059int s; /* socket descriptor */
plars865695b2001-08-27 22:15:12 +000060struct sockaddr_in sin0, fsin1;
subrata_modak56207ce2009-03-23 13:35:39 +000061int sinlen;
62int optval;
63socklen_t optlen;
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 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);
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 &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 ,
mridge161b01e2006-02-01 22:48:00 +000093#ifndef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +000094 {
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 ,
mridge161b01e2006-02-01 22:48:00 +0000103#endif /* UCLINUX */
subrata_modak56207ce2009-03-23 13:35:39 +0000104 {
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,};
plars865695b2001-08-27 22:15:12 +0000126
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200127int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
plars865695b2001-08-27 22:15:12 +0000128
subrata_modak56207ce2009-03-23 13:35:39 +0000129int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +0000130{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200131 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200132 const char *msg;
plars865695b2001-08-27 22:15:12 +0000133
Garrett Cooper45e285d2010-11-22 12:19:25 -0800134 msg = parse_opts(argc, argv, NULL, NULL);
135 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000136 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000137 }
138
139 setup();
140
plars865695b2001-08-27 22:15:12 +0000141 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800142 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000143 for (testno = 0; testno < TST_TOTAL; ++testno) {
plars865695b2001-08-27 22:15:12 +0000144 tdat[testno].setup();
145
146 TEST(getsockopt(s, tdat[testno].level,
subrata_modak56207ce2009-03-23 13:35:39 +0000147 tdat[testno].optname,
148 tdat[testno].optval,
149 tdat[testno].optlen));
plars865695b2001-08-27 22:15:12 +0000150 if (TEST_RETURN != tdat[testno].retval ||
151 (TEST_RETURN < 0 &&
152 TEST_ERRNO != tdat[testno].experrno)) {
153 tst_resm(TFAIL, "%s ; returned"
subrata_modak358c3ee2009-10-26 14:55:46 +0000154 " %ld (expected %d), errno %d (expected"
subrata_modak56207ce2009-03-23 13:35:39 +0000155 " %d)", tdat[testno].desc,
156 TEST_RETURN, tdat[testno].retval,
157 TEST_ERRNO, tdat[testno].experrno);
plars865695b2001-08-27 22:15:12 +0000158 } else {
159 tst_resm(TPASS, "%s successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000160 tdat[testno].desc);
plars865695b2001-08-27 22:15:12 +0000161 }
162 tdat[testno].cleanup();
163 }
164 }
165 cleanup();
166
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800167 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800168}
plars865695b2001-08-27 22:15:12 +0000169
subrata_modak56207ce2009-03-23 13:35:39 +0000170void setup(void)
plars865695b2001-08-27 22:15:12 +0000171{
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200172 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000173
plars865695b2001-08-27 22:15:12 +0000174 /* 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_modak56207ce2009-03-23 13:35:39 +0000180void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000181{
plars865695b2001-08-27 22:15:12 +0000182}
183
subrata_modak56207ce2009-03-23 13:35:39 +0000184void setup0(void)
plars865695b2001-08-27 22:15:12 +0000185{
186 if (tdat[testno].experrno == EBADF)
187 s = 400; /* anything not an open file */
subrata_modak56207ce2009-03-23 13:35:39 +0000188 else if ((s = open("/dev/null", O_WRONLY)) == -1)
189 tst_brkm(TBROK, cleanup, "error opening /dev/null - "
190 "errno: %s", strerror(errno));
plars865695b2001-08-27 22:15:12 +0000191}
192
subrata_modak56207ce2009-03-23 13:35:39 +0000193void cleanup0(void)
plars865695b2001-08-27 22:15:12 +0000194{
195 s = -1;
196}
197
subrata_modak56207ce2009-03-23 13:35:39 +0000198void setup1(void)
plars865695b2001-08-27 22:15:12 +0000199{
200 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
201 if (s < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000202 tst_brkm(TBROK, cleanup, "socket setup failed for getsockopt: "
203 "%s", strerror(errno));
plars865695b2001-08-27 22:15:12 +0000204 }
robbiew96d23372003-03-26 20:40:04 +0000205 if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
plars865695b2001-08-27 22:15:12 +0000206 tst_brkm(TBROK, cleanup, "socket bind failed for getsockopt: "
subrata_modak56207ce2009-03-23 13:35:39 +0000207 "%s", strerror(errno));
plars865695b2001-08-27 22:15:12 +0000208 }
209 sinlen = sizeof(fsin1);
210 optlen = sizeof(optval);
211}
212
subrata_modak56207ce2009-03-23 13:35:39 +0000213void cleanup1(void)
plars865695b2001-08-27 22:15:12 +0000214{
subrata_modak56207ce2009-03-23 13:35:39 +0000215 (void)close(s);
plars865695b2001-08-27 22:15:12 +0000216 s = -1;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700217}