Home | History | Annotate | Download | only in getsockopt
      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
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     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>
     45 #include <fcntl.h>
     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"
     55 
     56 char *TCID = "getsockopt01";
     57 int testno;
     58 
     59 int s;				/* socket descriptor */
     60 struct sockaddr_in sin0, fsin1;
     61 int sinlen;
     62 int optval;
     63 socklen_t optlen;
     64 
     65 void setup(void), setup0(void), setup1(void),
     66 cleanup(void), cleanup0(void), cleanup1(void);
     67 
     68 struct test_case_t {		/* test case structure */
     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);
     82 	char *desc;
     83 } tdat[] = {
     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 	,
     93 #ifndef UCLINUX
     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 	,
    103 #endif /* UCLINUX */
    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 ,};
    126 
    127 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
    128 
    129 int main(int argc, char *argv[])
    130 {
    131 	int lc;
    132 
    133 	tst_parse_opts(argc, argv, NULL, NULL);
    134 
    135 	setup();
    136 
    137 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
    138 		tst_count = 0;
    139 		for (testno = 0; testno < TST_TOTAL; ++testno) {
    140 			tdat[testno].setup();
    141 
    142 			TEST(getsockopt(s, tdat[testno].level,
    143 					tdat[testno].optname,
    144 					tdat[testno].optval,
    145 					tdat[testno].optlen));
    146 			if (TEST_RETURN != tdat[testno].retval ||
    147 			    (TEST_RETURN < 0 &&
    148 			     TEST_ERRNO != tdat[testno].experrno)) {
    149 				tst_resm(TFAIL, "%s ; returned"
    150 					 " %ld (expected %d), errno %d (expected"
    151 					 " %d)", tdat[testno].desc,
    152 					 TEST_RETURN, tdat[testno].retval,
    153 					 TEST_ERRNO, tdat[testno].experrno);
    154 			} else {
    155 				tst_resm(TPASS, "%s successful",
    156 					 tdat[testno].desc);
    157 			}
    158 			tdat[testno].cleanup();
    159 		}
    160 	}
    161 	cleanup();
    162 
    163 	tst_exit();
    164 }
    165 
    166 void setup(void)
    167 {
    168 	TEST_PAUSE;
    169 
    170 	/* initialize local sockaddr */
    171 	sin0.sin_family = AF_INET;
    172 	sin0.sin_port = 0;
    173 	sin0.sin_addr.s_addr = INADDR_ANY;
    174 }
    175 
    176 void cleanup(void)
    177 {
    178 }
    179 
    180 void setup0(void)
    181 {
    182 	if (tdat[testno].experrno == EBADF)
    183 		s = 400;	/* anything not an open file */
    184 	else if ((s = open("/dev/null", O_WRONLY)) == -1)
    185 		tst_brkm(TBROK, cleanup, "error opening /dev/null - "
    186 			 "errno: %s", strerror(errno));
    187 }
    188 
    189 void cleanup0(void)
    190 {
    191 	s = -1;
    192 }
    193 
    194 void setup1(void)
    195 {
    196 	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
    197 	if (s < 0) {
    198 		tst_brkm(TBROK, cleanup, "socket setup failed for getsockopt: "
    199 			 "%s", strerror(errno));
    200 	}
    201 	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
    202 		tst_brkm(TBROK, cleanup, "socket bind failed for getsockopt: "
    203 			 "%s", strerror(errno));
    204 	}
    205 	sinlen = sizeof(fsin1);
    206 	optlen = sizeof(optval);
    207 }
    208 
    209 void cleanup1(void)
    210 {
    211 	(void)close(s);
    212 	s = -1;
    213 }
    214