1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * Test Name: getsockopt01
22f08c3bdfSopenharmony_ci *
23f08c3bdfSopenharmony_ci * Test Description:
24f08c3bdfSopenharmony_ci *  Verify that getsockopt() returns the proper errno for various failure cases
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci * Usage:  <for command-line>
27f08c3bdfSopenharmony_ci *  getsockopt01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
28f08c3bdfSopenharmony_ci *     where,  -c n : Run n copies concurrently.
29f08c3bdfSopenharmony_ci *             -e   : Turn on errno logging.
30f08c3bdfSopenharmony_ci *	       -i n : Execute test n times.
31f08c3bdfSopenharmony_ci *	       -I x : Execute test for x seconds.
32f08c3bdfSopenharmony_ci *	       -P x : Pause for x seconds between iterations.
33f08c3bdfSopenharmony_ci *	       -t   : Turn on syscall timing.
34f08c3bdfSopenharmony_ci *
35f08c3bdfSopenharmony_ci * HISTORY
36f08c3bdfSopenharmony_ci *	07/2001 Ported by Wayne Boyer
37f08c3bdfSopenharmony_ci *
38f08c3bdfSopenharmony_ci * RESTRICTIONS:
39f08c3bdfSopenharmony_ci *  None.
40f08c3bdfSopenharmony_ci */
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci#include <stdio.h>
43f08c3bdfSopenharmony_ci#include <unistd.h>
44f08c3bdfSopenharmony_ci#include <errno.h>
45f08c3bdfSopenharmony_ci#include <fcntl.h>
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci#include <sys/types.h>
48f08c3bdfSopenharmony_ci#include <sys/socket.h>
49f08c3bdfSopenharmony_ci#include <sys/signal.h>
50f08c3bdfSopenharmony_ci#include <sys/ioctl.h>
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci#include <netinet/in.h>
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci#include "test.h"
55f08c3bdfSopenharmony_ci#include "safe_macros.h"
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cichar *TCID = "getsockopt01";
58f08c3bdfSopenharmony_ciint testno;
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ciint s;				/* socket descriptor */
61f08c3bdfSopenharmony_cistruct sockaddr_in sin0, fsin1;
62f08c3bdfSopenharmony_ciint sinlen;
63f08c3bdfSopenharmony_ciint optval;
64f08c3bdfSopenharmony_cisocklen_t optlen;
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_civoid setup(void), setup0(void), setup1(void),
67f08c3bdfSopenharmony_cicleanup(void), cleanup0(void), cleanup1(void);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_cistruct test_case_t {		/* test case structure */
70f08c3bdfSopenharmony_ci	int domain;		/* PF_INET, PF_UNIX, ... */
71f08c3bdfSopenharmony_ci	int type;		/* SOCK_STREAM, SOCK_DGRAM ... */
72f08c3bdfSopenharmony_ci	int proto;		/* protocol number (usually 0 = default) */
73f08c3bdfSopenharmony_ci	int level;		/* IPPROTO_* */
74f08c3bdfSopenharmony_ci	int optname;
75f08c3bdfSopenharmony_ci	void *optval;
76f08c3bdfSopenharmony_ci	socklen_t *optlen;
77f08c3bdfSopenharmony_ci	struct sockaddr *sin;
78f08c3bdfSopenharmony_ci	int salen;
79f08c3bdfSopenharmony_ci	int retval;		/* syscall return value */
80f08c3bdfSopenharmony_ci	int experrno;		/* expected errno */
81f08c3bdfSopenharmony_ci	void (*setup) (void);
82f08c3bdfSopenharmony_ci	void (*cleanup) (void);
83f08c3bdfSopenharmony_ci	char *desc;
84f08c3bdfSopenharmony_ci} tdat[] = {
85f08c3bdfSopenharmony_ci	{
86f08c3bdfSopenharmony_ci	PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval,
87f08c3bdfSopenharmony_ci		    &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1),
88f08c3bdfSopenharmony_ci		    -1, EBADF, setup0, cleanup0, "bad file descriptor"}
89f08c3bdfSopenharmony_ci	, {
90f08c3bdfSopenharmony_ci	PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval,
91f08c3bdfSopenharmony_ci		    &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1),
92f08c3bdfSopenharmony_ci		    -1, ENOTSOCK, setup0, cleanup0, "bad file descriptor"}
93f08c3bdfSopenharmony_ci	,
94f08c3bdfSopenharmony_ci#ifndef UCLINUX
95f08c3bdfSopenharmony_ci	{
96f08c3bdfSopenharmony_ci	PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, 0, &optlen,
97f08c3bdfSopenharmony_ci		    (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
98f08c3bdfSopenharmony_ci		    EFAULT, setup1, cleanup1, "invalid option buffer"}
99f08c3bdfSopenharmony_ci	, {
100f08c3bdfSopenharmony_ci	PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, 0,
101f08c3bdfSopenharmony_ci		    (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
102f08c3bdfSopenharmony_ci		    EFAULT, setup1, cleanup1, "invalid optlen"}
103f08c3bdfSopenharmony_ci	,
104f08c3bdfSopenharmony_ci#endif /* UCLINUX */
105f08c3bdfSopenharmony_ci	{
106f08c3bdfSopenharmony_ci	PF_INET, SOCK_STREAM, 0, 500, SO_OOBINLINE, &optval, &optlen,
107f08c3bdfSopenharmony_ci		    (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
108f08c3bdfSopenharmony_ci		    EOPNOTSUPP, setup1, cleanup1, "invalid level"}
109f08c3bdfSopenharmony_ci	, {
110f08c3bdfSopenharmony_ci	PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval,
111f08c3bdfSopenharmony_ci		    &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1),
112f08c3bdfSopenharmony_ci		    -1, EOPNOTSUPP, setup1, cleanup1, "invalid option name"}
113f08c3bdfSopenharmony_ci	, {
114f08c3bdfSopenharmony_ci	PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval,
115f08c3bdfSopenharmony_ci		    &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1),
116f08c3bdfSopenharmony_ci		    -1, EOPNOTSUPP, setup1, cleanup1,
117f08c3bdfSopenharmony_ci		    "invalid option name (UDP)"}
118f08c3bdfSopenharmony_ci	, {
119f08c3bdfSopenharmony_ci	PF_INET, SOCK_STREAM, 0, IPPROTO_IP, -1, &optval, &optlen,
120f08c3bdfSopenharmony_ci		    (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
121f08c3bdfSopenharmony_ci		    ENOPROTOOPT, setup1, cleanup1, "invalid option name (IP)"}
122f08c3bdfSopenharmony_ci	, {
123f08c3bdfSopenharmony_ci	PF_INET, SOCK_STREAM, 0, IPPROTO_TCP, -1, &optval, &optlen,
124f08c3bdfSopenharmony_ci		    (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
125f08c3bdfSopenharmony_ci		    ENOPROTOOPT, setup1, cleanup1, "invalid option name (TCP)"}
126f08c3bdfSopenharmony_ci,};
127f08c3bdfSopenharmony_ci
128f08c3bdfSopenharmony_ciint TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_ciint main(int argc, char *argv[])
131f08c3bdfSopenharmony_ci{
132f08c3bdfSopenharmony_ci	int lc;
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci	tst_parse_opts(argc, argv, NULL, NULL);
135f08c3bdfSopenharmony_ci
136f08c3bdfSopenharmony_ci	setup();
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); ++lc) {
139f08c3bdfSopenharmony_ci		tst_count = 0;
140f08c3bdfSopenharmony_ci		for (testno = 0; testno < TST_TOTAL; ++testno) {
141f08c3bdfSopenharmony_ci			tdat[testno].setup();
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci			TEST(getsockopt(s, tdat[testno].level,
144f08c3bdfSopenharmony_ci					tdat[testno].optname,
145f08c3bdfSopenharmony_ci					tdat[testno].optval,
146f08c3bdfSopenharmony_ci					tdat[testno].optlen));
147f08c3bdfSopenharmony_ci			if (TEST_RETURN != tdat[testno].retval ||
148f08c3bdfSopenharmony_ci			    (TEST_RETURN < 0 &&
149f08c3bdfSopenharmony_ci			     TEST_ERRNO != tdat[testno].experrno)) {
150f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "%s ; returned"
151f08c3bdfSopenharmony_ci					 " %ld (expected %d), errno %d (expected"
152f08c3bdfSopenharmony_ci					 " %d)", tdat[testno].desc,
153f08c3bdfSopenharmony_ci					 TEST_RETURN, tdat[testno].retval,
154f08c3bdfSopenharmony_ci					 TEST_ERRNO, tdat[testno].experrno);
155f08c3bdfSopenharmony_ci			} else {
156f08c3bdfSopenharmony_ci				tst_resm(TPASS, "%s successful",
157f08c3bdfSopenharmony_ci					 tdat[testno].desc);
158f08c3bdfSopenharmony_ci			}
159f08c3bdfSopenharmony_ci			tdat[testno].cleanup();
160f08c3bdfSopenharmony_ci		}
161f08c3bdfSopenharmony_ci	}
162f08c3bdfSopenharmony_ci	cleanup();
163f08c3bdfSopenharmony_ci
164f08c3bdfSopenharmony_ci	tst_exit();
165f08c3bdfSopenharmony_ci}
166f08c3bdfSopenharmony_ci
167f08c3bdfSopenharmony_civoid setup(void)
168f08c3bdfSopenharmony_ci{
169f08c3bdfSopenharmony_ci	TEST_PAUSE;
170f08c3bdfSopenharmony_ci
171f08c3bdfSopenharmony_ci	/* initialize local sockaddr */
172f08c3bdfSopenharmony_ci	sin0.sin_family = AF_INET;
173f08c3bdfSopenharmony_ci	sin0.sin_port = 0;
174f08c3bdfSopenharmony_ci	sin0.sin_addr.s_addr = INADDR_ANY;
175f08c3bdfSopenharmony_ci}
176f08c3bdfSopenharmony_ci
177f08c3bdfSopenharmony_civoid cleanup(void)
178f08c3bdfSopenharmony_ci{
179f08c3bdfSopenharmony_ci}
180f08c3bdfSopenharmony_ci
181f08c3bdfSopenharmony_civoid setup0(void)
182f08c3bdfSopenharmony_ci{
183f08c3bdfSopenharmony_ci	if (tdat[testno].experrno == EBADF)
184f08c3bdfSopenharmony_ci		s = 400;	/* anything not an open file */
185f08c3bdfSopenharmony_ci	else if ((s = open("/dev/null", O_WRONLY)) == -1)
186f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup, "error opening /dev/null - "
187f08c3bdfSopenharmony_ci			 "errno: %s", strerror(errno));
188f08c3bdfSopenharmony_ci}
189f08c3bdfSopenharmony_ci
190f08c3bdfSopenharmony_civoid cleanup0(void)
191f08c3bdfSopenharmony_ci{
192f08c3bdfSopenharmony_ci	s = -1;
193f08c3bdfSopenharmony_ci}
194f08c3bdfSopenharmony_ci
195f08c3bdfSopenharmony_civoid setup1(void)
196f08c3bdfSopenharmony_ci{
197f08c3bdfSopenharmony_ci	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
198f08c3bdfSopenharmony_ci			tdat[testno].proto);
199f08c3bdfSopenharmony_ci	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
200f08c3bdfSopenharmony_ci	sinlen = sizeof(fsin1);
201f08c3bdfSopenharmony_ci	optlen = sizeof(optval);
202f08c3bdfSopenharmony_ci}
203f08c3bdfSopenharmony_ci
204f08c3bdfSopenharmony_civoid cleanup1(void)
205f08c3bdfSopenharmony_ci{
206f08c3bdfSopenharmony_ci	(void)close(s);
207f08c3bdfSopenharmony_ci	s = -1;
208f08c3bdfSopenharmony_ci}
209