1 /* 2 * Copyright (c) International Business Machines Corp., 2001 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12 * the GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. 16 */ 17 18 /* 19 * Test Name: socket01 20 * 21 * Test Description: 22 * Verify that socket() returns the proper errno for various failure cases 23 * 24 */ 25 26 #include <stdio.h> 27 #include <unistd.h> 28 #include <errno.h> 29 #include <sys/types.h> 30 #include <sys/socket.h> 31 #include <sys/un.h> 32 #include <netinet/in.h> 33 #include "tst_test.h" 34 35 struct test_case_t { 36 int domain; 37 int type; 38 int proto; 39 int retval; 40 int experrno; 41 char *desc; 42 } tdat[] = { 43 {0, SOCK_STREAM, 0, -1, EAFNOSUPPORT, "invalid domain"}, 44 {PF_INET, 75, 0, -1, EINVAL, "invalid type"}, 45 {PF_UNIX, SOCK_DGRAM, 0, 0, 0, "UNIX domain dgram"}, 46 {PF_INET, SOCK_RAW, 0, -1, EPROTONOSUPPORT, "raw open as non-root"}, 47 {PF_INET, SOCK_DGRAM, 17, 0, 0, "UDP socket"}, 48 {PF_INET, SOCK_STREAM, 17, -1, EPROTONOSUPPORT, "UDP stream"}, 49 {PF_INET, SOCK_DGRAM, 6, -1, EPROTONOSUPPORT, "TCP dgram"}, 50 {PF_INET, SOCK_STREAM, 6, 0, 0, "TCP socket"}, 51 {PF_INET, SOCK_STREAM, 1, -1, EPROTONOSUPPORT, "ICMP stream"} 52 }; 53 54 static void verify_socket(unsigned int n) 55 { 56 int fd; 57 struct test_case_t *tc = &tdat[n]; 58 59 TEST(fd = socket(tc->domain, tc->type, tc->proto)); 60 if (TEST_RETURN >= 0) 61 TEST_RETURN = 0; 62 63 if (fd > 0) 64 SAFE_CLOSE(fd); 65 66 if (TEST_RETURN != tc->retval) { 67 tst_res(TFAIL, "%s returned %d (expected %d)", 68 tc->desc, fd, tc->retval); 69 return; 70 } 71 72 if (TEST_ERRNO != tc->experrno) { 73 tst_res(TFAIL | TTERRNO, "expected %s(%d)", 74 tst_strerrno(tc->experrno), tc->experrno); 75 return; 76 } 77 78 tst_res(TPASS, "%s successful", tc->desc); 79 } 80 81 /* 82 * See: 83 * commit 86c8f9d158f68538a971a47206a46a22c7479bac 84 * ... 85 * [IPV4] Fix EPROTONOSUPPORT error in inet_create 86 */ 87 static void setup(void) 88 { 89 unsigned int i; 90 91 if (tst_kvercmp(2, 6, 16) >= 0) 92 return; 93 94 for (i = 0; i < ARRAY_SIZE(tdat); i++) { 95 if (tdat[i].experrno == EPROTONOSUPPORT) 96 tdat[i].experrno = ESOCKTNOSUPPORT; 97 } 98 } 99 100 static struct tst_test test = { 101 .tcnt = ARRAY_SIZE(tdat), 102 .setup = setup, 103 .test = verify_socket 104 }; 105