Home | History | Annotate | Download | only in accept
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *   07/2001 Ported by Wayne Boyer
      5  *
      6  *   This program is free software;  you can redistribute it and/or modify
      7  *   it under the terms of the GNU General Public License as published by
      8  *   the Free Software Foundation; either version 2 of the License, or
      9  *   (at your option) any later version.
     10  *
     11  *   This program is distributed in the hope that it will be useful,
     12  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     14  *   the GNU General Public License for more details.
     15  *
     16  *   You should have received a copy of the GNU General Public License
     17  *   along with this program;  if not, write to the Free Software
     18  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     19  */
     20 
     21 /*
     22  * Description:
     23  *  Verify that accept() returns the proper errno for various failure cases
     24  */
     25 
     26 #include <stdio.h>
     27 #include <unistd.h>
     28 #include <errno.h>
     29 #include <fcntl.h>
     30 
     31 #include <sys/types.h>
     32 #include <sys/socket.h>
     33 #include <sys/signal.h>
     34 
     35 #include <netinet/in.h>
     36 
     37 #include "test.h"
     38 #include "safe_macros.h"
     39 
     40 char *TCID = "accept01";
     41 int testno;
     42 
     43 int s;				/* socket descriptor */
     44 struct sockaddr_in sin0, fsin1;
     45 socklen_t sinlen;
     46 
     47 static void setup(void);
     48 static void cleanup(void);
     49 static void setup0(void);
     50 static void cleanup0(void);
     51 static void setup1(void);
     52 static void cleanup1(void);
     53 static void setup2(void);
     54 static void setup3(void);
     55 
     56 struct test_case_t {		/* test case structure */
     57 	int domain;		/* PF_INET, PF_UNIX, ... */
     58 	int type;		/* SOCK_STREAM, SOCK_DGRAM ... */
     59 	int proto;		/* protocol number (usually 0 = default) */
     60 	struct sockaddr *sockaddr;	/* socket address buffer */
     61 	socklen_t *salen;	/* accept's 3rd argument */
     62 	int retval;		/* syscall return value */
     63 	int experrno;		/* expected errno */
     64 	void (*setup) (void);
     65 	void (*cleanup) (void);
     66 	char *desc;
     67 } tdat[] = {
     68 	{
     69 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
     70 		    &sinlen, -1, EBADF, setup0, cleanup0,
     71 		    "bad file descriptor"}, {
     72 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
     73 		    &sinlen, -1, ENOTSOCK, setup0, cleanup0,
     74 		    "bad file descriptor"}, {
     75 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)3,
     76 		    &sinlen, -1, EINVAL, setup1, cleanup1,
     77 		    "invalid socket buffer"}, {
     78 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
     79 		    (socklen_t *) 1, -1, EINVAL, setup1, cleanup1,
     80 		    "invalid salen"}, {
     81 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
     82 		    &sinlen, -1, EINVAL, setup2, cleanup1,
     83 		    "invalid salen"}, {
     84 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
     85 		    &sinlen, -1, EINVAL, setup3, cleanup1,
     86 		    "no queued connections"}, {
     87 	PF_INET, SOCK_DGRAM, 0, (struct sockaddr *)&fsin1,
     88 		    &sinlen, -1, EOPNOTSUPP, setup1, cleanup1,
     89 		    "UDP accept"},};
     90 
     91 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
     92 
     93 int main(int ac, char *av[])
     94 {
     95 	int lc;
     96 
     97 	tst_parse_opts(ac, av, NULL, NULL);
     98 
     99 	setup();
    100 
    101 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
    102 		tst_count = 0;
    103 		for (testno = 0; testno < TST_TOTAL; ++testno) {
    104 			tdat[testno].setup();
    105 
    106 			TEST(accept(s, tdat[testno].sockaddr,
    107 				    tdat[testno].salen));
    108 			if (TEST_RETURN > 0)
    109 				TEST_RETURN = 0;
    110 			if (TEST_RETURN != tdat[testno].retval ||
    111 			    (TEST_RETURN < 0 &&
    112 			     TEST_ERRNO != tdat[testno].experrno)) {
    113 				tst_resm(TFAIL, "%s ; returned"
    114 					 " %ld (expected %d), errno %d (expected"
    115 					 " %d)", tdat[testno].desc,
    116 					 TEST_RETURN, tdat[testno].retval,
    117 					 TEST_ERRNO, tdat[testno].experrno);
    118 			} else {
    119 				tst_resm(TPASS, "%s successful",
    120 					 tdat[testno].desc);
    121 			}
    122 			tdat[testno].cleanup();
    123 		}
    124 	}
    125 
    126 	cleanup();
    127 	tst_exit();
    128 }
    129 
    130 static void setup(void)
    131 {
    132 	TEST_PAUSE;
    133 
    134 	/* initialize local sockaddr */
    135 	sin0.sin_family = AF_INET;
    136 	sin0.sin_port = 0;
    137 	sin0.sin_addr.s_addr = INADDR_ANY;
    138 }
    139 
    140 static void cleanup(void)
    141 {
    142 }
    143 
    144 static void setup0(void)
    145 {
    146 	if (tdat[testno].experrno == EBADF)
    147 		s = 400;	/* anything not an open file */
    148 	else if ((s = open("/dev/null", O_WRONLY)) == -1)
    149 		tst_brkm(TBROK | TERRNO, cleanup, "error opening /dev/null");
    150 }
    151 
    152 static void cleanup0(void)
    153 {
    154 	s = -1;
    155 }
    156 
    157 static void setup1(void)
    158 {
    159 	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
    160 			tdat[testno].proto);
    161 	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
    162 	sinlen = sizeof(fsin1);
    163 }
    164 
    165 static void cleanup1(void)
    166 {
    167 	(void)close(s);
    168 	s = -1;
    169 }
    170 
    171 static void setup2(void)
    172 {
    173 	setup1();		/* get a socket in s */
    174 	sinlen = 1;		/* invalid s */
    175 }
    176 
    177 static void setup3(void)
    178 {
    179 	int one = 1;
    180 
    181 	setup1();
    182 	SAFE_IOCTL(cleanup, s, FIONBIO, &one);
    183 }
    184