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