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: getsockname01 22 * 23 * Test Description: 24 * Verify that getsockname() returns the proper errno for various failure cases 25 * 26 * Usage: <for command-line> 27 * getsockname01 [-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 56char *TCID = "getsockname01"; 57int testno; 58 59int s; /* socket descriptor */ 60struct sockaddr_in sin0, fsin1; 61socklen_t sinlen; 62 63void setup(void), setup0(void), setup1(void), 64cleanup(void), cleanup0(void), cleanup1(void); 65 66struct test_case_t { /* test case structure */ 67 int domain; /* PF_INET, PF_UNIX, ... */ 68 int type; /* SOCK_STREAM, SOCK_DGRAM ... */ 69 int proto; /* protocol number (usually 0 = default) */ 70 struct sockaddr *sockaddr; /* socket address buffer */ 71 socklen_t *salen; /* getsockname's 3rd argument */ 72 int retval; /* syscall return value */ 73 int experrno; /* expected errno */ 74 void (*setup) (void); 75 void (*cleanup) (void); 76 char *desc; 77} tdat[] = { 78 { 79 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1, 80 &sinlen, -1, EBADF, setup0, cleanup0, 81 "bad file descriptor"}, { 82 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1, 83 &sinlen, -1, ENOTSOCK, setup0, cleanup0, 84 "bad file descriptor"}, 85#ifndef UCLINUX 86 /* Skip since uClinux does not implement memory protection */ 87 { 88 PF_INET, SOCK_STREAM, 0, NULL, 89 &sinlen, -1, EFAULT, setup1, cleanup1, 90 "invalid socket buffer"}, { 91 /* invalid salen test for aligned input */ 92 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1, 93 NULL, -1, EFAULT, setup1, cleanup1, 94 "invalid aligned salen"}, { 95 /* invalid salen test for unaligned input */ 96 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1, 97 (socklen_t *) 1, -1, EFAULT, setup1, cleanup1, 98 "invalid unaligned salen"}, 99#endif 100}; 101 102int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); 103 104int main(int argc, char *argv[]) 105{ 106 int lc; 107 108 tst_parse_opts(argc, argv, NULL, NULL); 109 110 setup(); 111 112 for (lc = 0; TEST_LOOPING(lc); ++lc) { 113 tst_count = 0; 114 for (testno = 0; testno < TST_TOTAL; ++testno) { 115 tdat[testno].setup(); 116 117 TEST(getsockname(s, tdat[testno].sockaddr, 118 tdat[testno].salen)); 119 if (TEST_RETURN != tdat[testno].retval || 120 (TEST_RETURN < 0 && 121 TEST_ERRNO != tdat[testno].experrno)) { 122 tst_resm(TFAIL, "%s ; returned" 123 " %ld (expected %d), errno %d (expected" 124 " %d)", tdat[testno].desc, 125 TEST_RETURN, tdat[testno].retval, 126 TEST_ERRNO, tdat[testno].experrno); 127 } else { 128 tst_resm(TPASS, "%s successful", 129 tdat[testno].desc); 130 } 131 tdat[testno].cleanup(); 132 } 133 } 134 cleanup(); 135 136 tst_exit(); 137} 138 139void setup(void) 140{ 141 TEST_PAUSE; 142 143 /* initialize local sockaddr */ 144 sin0.sin_family = AF_INET; 145 sin0.sin_port = 0; 146 sin0.sin_addr.s_addr = INADDR_ANY; 147} 148 149void cleanup(void) 150{ 151} 152 153void setup0(void) 154{ 155 if (tdat[testno].experrno == EBADF) 156 s = 400; /* anything not an open file */ 157 else if ((s = open("/dev/null", O_WRONLY)) == -1) 158 tst_brkm(TBROK, cleanup, "error opening /dev/null - " 159 "errno: %s", strerror(errno)); 160 161} 162 163void cleanup0(void) 164{ 165 s = -1; 166} 167 168void setup1(void) 169{ 170 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); 171 if (s < 0) { 172 tst_brkm(TBROK, cleanup, "socket setup failed for getsockname " 173 "test %d: %s", testno, strerror(errno)); 174 } 175 if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) { 176 tst_brkm(TBROK, cleanup, "socket bind failed for getsockname " 177 "test %d: %s", testno, strerror(errno)); 178 } 179 sinlen = sizeof(fsin1); 180} 181 182void cleanup1(void) 183{ 184 (void)close(s); 185 s = -1; 186} 187