Home | History | Annotate | Download | only in socketpair
      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: socketpair01
     20 *
     21 * Test Description:
     22 * Verify that socketpair() returns the proper errno for various failure cases
     23 */
     24 
     25 #include <stdio.h>
     26 #include <unistd.h>
     27 #include <errno.h>
     28 #include <sys/types.h>
     29 #include <sys/socket.h>
     30 #include <sys/un.h>
     31 #include <netinet/in.h>
     32 #include "tst_test.h"
     33 
     34 static int fds[2];
     35 
     36 struct test_case_t {
     37 	int domain;
     38 	int type;
     39 	int proto;
     40 	int *sv;
     41 	int retval;
     42 	int experrno;
     43 	char *desc;
     44 } tdat[] = {
     45 	{0, SOCK_STREAM, 0, fds, -1, EAFNOSUPPORT, "invalid domain"},
     46 	{PF_INET, 75, 0, fds, -1, EINVAL, "invalid type"},
     47 	{PF_UNIX, SOCK_DGRAM, 0, fds, 0, 0, "UNIX domain dgram"},
     48 	{PF_INET, SOCK_RAW, 0, fds, -1, EPROTONOSUPPORT, "raw open as non-root"},
     49 #ifndef UCLINUX
     50 	{PF_UNIX, SOCK_STREAM, 0, 0, -1, EFAULT, "bad aligned pointer"},
     51 	{PF_UNIX, SOCK_STREAM, 0, (int *)7, -1, EFAULT, "bad unaligned pointer"},
     52 #endif
     53 	{PF_INET, SOCK_DGRAM, 17, fds, -1, EOPNOTSUPP, "UDP socket"},
     54 	{PF_INET, SOCK_DGRAM, 6, fds, -1, EPROTONOSUPPORT, "TCP dgram"},
     55 	{PF_INET, SOCK_STREAM, 6, fds, -1, EOPNOTSUPP, "TCP socket"},
     56 	{PF_INET, SOCK_STREAM, 1, fds, -1, EPROTONOSUPPORT, "ICMP stream"}
     57 };
     58 
     59 static void verify_socketpair(unsigned int n)
     60 {
     61 	struct test_case_t *tc = &tdat[n];
     62 
     63 	TEST(socketpair(tc->domain, tc->type, tc->proto, tc->sv));
     64 
     65 	if (TEST_RETURN == 0) {
     66 		SAFE_CLOSE(fds[0]);
     67 		SAFE_CLOSE(fds[1]);
     68 	}
     69 
     70 	if (TEST_RETURN != tc->retval) {
     71 		tst_res(TFAIL, "%s returned %ld (expected %d)",
     72 		        tc->desc, TEST_RETURN, tc->retval);
     73 		return;
     74 	}
     75 
     76 	if (TEST_ERRNO != tc->experrno) {
     77 		tst_res(TFAIL | TTERRNO, "expected %s(%d)",
     78 		        tst_strerrno(tc->experrno), tc->experrno);
     79 		return;
     80 	}
     81 
     82 	tst_res(TPASS, "%s successful", tc->desc);
     83 }
     84 
     85 /*
     86  * See:
     87  * commit 86c8f9d158f68538a971a47206a46a22c7479bac
     88  * ...
     89  * [IPV4] Fix EPROTONOSUPPORT error in inet_create
     90  */
     91 static void setup(void)
     92 {
     93 	unsigned int i;
     94 
     95 	if (tst_kvercmp(2, 6, 16) >= 0)
     96 		return;
     97 
     98 	for (i = 0; i < ARRAY_SIZE(tdat); i++) {
     99 		if (tdat[i].experrno == EPROTONOSUPPORT)
    100 				tdat[i].experrno = ESOCKTNOSUPPORT;
    101 	}
    102 }
    103 
    104 static struct tst_test test = {
    105 	.tcnt = ARRAY_SIZE(tdat),
    106 	.setup = setup,
    107 	.test = verify_socketpair
    108 };
    109