1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 5f08c3bdfSopenharmony_ci * 6f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 7f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 8f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 9f08c3bdfSopenharmony_ci * (at your option) any later version. 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 12f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 13f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 14f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 15f08c3bdfSopenharmony_ci * 16f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 17f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software Foundation, 18f08c3bdfSopenharmony_ci * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19f08c3bdfSopenharmony_ci */ 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ci/* 22f08c3bdfSopenharmony_ci * Verify that getpeername() returns the proper errno for various failure cases 23f08c3bdfSopenharmony_ci */ 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci#include <stdio.h> 26f08c3bdfSopenharmony_ci#include <unistd.h> 27f08c3bdfSopenharmony_ci#include <errno.h> 28f08c3bdfSopenharmony_ci#include <fcntl.h> 29f08c3bdfSopenharmony_ci#include <sys/types.h> 30f08c3bdfSopenharmony_ci#include <sys/socket.h> 31f08c3bdfSopenharmony_ci#include <sys/signal.h> 32f08c3bdfSopenharmony_ci#include <sys/ioctl.h> 33f08c3bdfSopenharmony_ci#include <netinet/in.h> 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci#include "test.h" 36f08c3bdfSopenharmony_ci#include "safe_macros.h" 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_cistatic struct sockaddr_in server_addr; 39f08c3bdfSopenharmony_cistatic struct sockaddr_in fsin1; 40f08c3bdfSopenharmony_cistatic socklen_t sinlen; 41f08c3bdfSopenharmony_cistatic socklen_t invalid_sinlen = -1; 42f08c3bdfSopenharmony_cistatic int sv[2]; 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_cistatic void setup(void); 45f08c3bdfSopenharmony_cistatic void setup2(int); 46f08c3bdfSopenharmony_cistatic void setup3(int); 47f08c3bdfSopenharmony_cistatic void setup4(int); 48f08c3bdfSopenharmony_cistatic void cleanup(void); 49f08c3bdfSopenharmony_cistatic void cleanup2(int); 50f08c3bdfSopenharmony_cistatic void cleanup4(int); 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_cistruct test_case_t { 53f08c3bdfSopenharmony_ci int sockfd; 54f08c3bdfSopenharmony_ci struct sockaddr *sockaddr; 55f08c3bdfSopenharmony_ci socklen_t *addrlen; 56f08c3bdfSopenharmony_ci int expretval; 57f08c3bdfSopenharmony_ci int experrno; 58f08c3bdfSopenharmony_ci void (*setup) (int); 59f08c3bdfSopenharmony_ci void (*cleanup) (int); 60f08c3bdfSopenharmony_ci char *name; 61f08c3bdfSopenharmony_ci} test_cases[] = { 62f08c3bdfSopenharmony_ci {-1, (struct sockaddr *)&fsin1, &sinlen, -1, EBADF, NULL, NULL, 63f08c3bdfSopenharmony_ci "EBADF"}, 64f08c3bdfSopenharmony_ci {-1, (struct sockaddr *)&fsin1, &sinlen, -1, ENOTSOCK, setup2, cleanup2, 65f08c3bdfSopenharmony_ci "ENOTSOCK"}, 66f08c3bdfSopenharmony_ci {-1, (struct sockaddr *)&fsin1, &sinlen, -1, ENOTCONN, setup3, cleanup2, 67f08c3bdfSopenharmony_ci "ENOTCONN"}, 68f08c3bdfSopenharmony_ci {-1, (struct sockaddr *)&fsin1, &invalid_sinlen, -1, EINVAL, setup4, 69f08c3bdfSopenharmony_ci cleanup4, "EINVAL"}, 70f08c3bdfSopenharmony_ci#ifndef UCLINUX 71f08c3bdfSopenharmony_ci {-1, (struct sockaddr *)-1, &sinlen, -1, EFAULT, setup4, cleanup4, 72f08c3bdfSopenharmony_ci "EFAULT"}, 73f08c3bdfSopenharmony_ci {-1, (struct sockaddr *)&fsin1, NULL, -1, EFAULT, setup4, 74f08c3bdfSopenharmony_ci cleanup4, "EFAULT"}, 75f08c3bdfSopenharmony_ci {-1, (struct sockaddr *)&fsin1, (socklen_t *)1, -1, EFAULT, setup4, 76f08c3bdfSopenharmony_ci cleanup4, "EFAULT"}, 77f08c3bdfSopenharmony_ci#endif 78f08c3bdfSopenharmony_ci}; 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_cichar *TCID = "getpeername01"; 81f08c3bdfSopenharmony_ciint TST_TOTAL = ARRAY_SIZE(test_cases); 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ciint main(int argc, char *argv[]) 84f08c3bdfSopenharmony_ci{ 85f08c3bdfSopenharmony_ci int lc; 86f08c3bdfSopenharmony_ci int i; 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_ci tst_parse_opts(argc, argv, NULL, NULL); 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci setup(); 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); ++lc) { 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci tst_count = 0; 95f08c3bdfSopenharmony_ci 96f08c3bdfSopenharmony_ci for (i = 0; i < TST_TOTAL; ++i) { 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_ci if (test_cases[i].setup != NULL) 99f08c3bdfSopenharmony_ci test_cases[i].setup(i); 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci TEST(getpeername(test_cases[i].sockfd, 102f08c3bdfSopenharmony_ci test_cases[i].sockaddr, 103f08c3bdfSopenharmony_ci test_cases[i].addrlen)); 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci if (TEST_RETURN == test_cases[i].expretval && 106f08c3bdfSopenharmony_ci TEST_ERRNO == test_cases[i].experrno) { 107f08c3bdfSopenharmony_ci tst_resm(TPASS, 108f08c3bdfSopenharmony_ci "test getpeername() %s successful", 109f08c3bdfSopenharmony_ci test_cases[i].name); 110f08c3bdfSopenharmony_ci } else { 111f08c3bdfSopenharmony_ci tst_resm(TFAIL, 112f08c3bdfSopenharmony_ci "test getpeername() %s failed; " 113f08c3bdfSopenharmony_ci "returned %ld (expected %d), errno %d " 114f08c3bdfSopenharmony_ci "(expected %d)", test_cases[i].name, 115f08c3bdfSopenharmony_ci TEST_RETURN, test_cases[i].expretval, 116f08c3bdfSopenharmony_ci TEST_ERRNO, test_cases[i].experrno); 117f08c3bdfSopenharmony_ci } 118f08c3bdfSopenharmony_ci 119f08c3bdfSopenharmony_ci if (test_cases[i].cleanup != NULL) 120f08c3bdfSopenharmony_ci test_cases[i].cleanup(i); 121f08c3bdfSopenharmony_ci } 122f08c3bdfSopenharmony_ci } 123f08c3bdfSopenharmony_ci 124f08c3bdfSopenharmony_ci cleanup(); 125f08c3bdfSopenharmony_ci 126f08c3bdfSopenharmony_ci tst_exit(); 127f08c3bdfSopenharmony_ci} 128f08c3bdfSopenharmony_ci 129f08c3bdfSopenharmony_cistatic void setup(void) 130f08c3bdfSopenharmony_ci{ 131f08c3bdfSopenharmony_ci TEST_PAUSE; 132f08c3bdfSopenharmony_ci 133f08c3bdfSopenharmony_ci server_addr.sin_family = AF_INET; 134f08c3bdfSopenharmony_ci server_addr.sin_port = 0; 135f08c3bdfSopenharmony_ci server_addr.sin_addr.s_addr = INADDR_ANY; 136f08c3bdfSopenharmony_ci 137f08c3bdfSopenharmony_ci sinlen = sizeof(fsin1); 138f08c3bdfSopenharmony_ci} 139f08c3bdfSopenharmony_ci 140f08c3bdfSopenharmony_cistatic void cleanup(void) 141f08c3bdfSopenharmony_ci{ 142f08c3bdfSopenharmony_ci} 143f08c3bdfSopenharmony_ci 144f08c3bdfSopenharmony_cistatic void setup2(int i) 145f08c3bdfSopenharmony_ci{ 146f08c3bdfSopenharmony_ci test_cases[i].sockfd = SAFE_OPEN(cleanup, "/dev/null", O_WRONLY, 0666); 147f08c3bdfSopenharmony_ci} 148f08c3bdfSopenharmony_ci 149f08c3bdfSopenharmony_cistatic void setup3(int i) 150f08c3bdfSopenharmony_ci{ 151f08c3bdfSopenharmony_ci test_cases[i].sockfd = socket(PF_INET, SOCK_STREAM, 0); 152f08c3bdfSopenharmony_ci if (test_cases[i].sockfd < 0) { 153f08c3bdfSopenharmony_ci tst_brkm(TBROK | TERRNO, cleanup, 154f08c3bdfSopenharmony_ci "socket setup failed for getpeername test %d", i); 155f08c3bdfSopenharmony_ci } 156f08c3bdfSopenharmony_ci SAFE_BIND(cleanup, test_cases[i].sockfd, 157f08c3bdfSopenharmony_ci (struct sockaddr *)&server_addr, sizeof(server_addr)); 158f08c3bdfSopenharmony_ci} 159f08c3bdfSopenharmony_ci 160f08c3bdfSopenharmony_cistatic void setup4(int i) 161f08c3bdfSopenharmony_ci{ 162f08c3bdfSopenharmony_ci if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) { 163f08c3bdfSopenharmony_ci tst_brkm(TBROK | TERRNO, cleanup, 164f08c3bdfSopenharmony_ci "socketpair failed for getpeername test %d", i); 165f08c3bdfSopenharmony_ci } 166f08c3bdfSopenharmony_ci test_cases[i].sockfd = sv[0]; 167f08c3bdfSopenharmony_ci} 168f08c3bdfSopenharmony_ci 169f08c3bdfSopenharmony_cistatic void cleanup2(int i) 170f08c3bdfSopenharmony_ci{ 171f08c3bdfSopenharmony_ci SAFE_CLOSE(cleanup, test_cases[i].sockfd); 172f08c3bdfSopenharmony_ci} 173f08c3bdfSopenharmony_ci 174f08c3bdfSopenharmony_cistatic void cleanup4(int i) 175f08c3bdfSopenharmony_ci{ 176f08c3bdfSopenharmony_ci SAFE_CLOSE(cleanup, sv[0]); 177f08c3bdfSopenharmony_ci SAFE_CLOSE(cleanup, sv[1]); 178f08c3bdfSopenharmony_ci} 179