plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: socketpair01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that socketpair() returns the proper errno for various failure cases |
| 25 | * |
| 26 | * Usage: <for command-line> |
| 27 | * socketpair01 [-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 John George |
| 37 | * -Ported |
| 38 | * |
| 39 | * Restrictions: |
| 40 | * None. |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | #include <unistd.h> |
| 46 | #include <errno.h> |
| 47 | |
| 48 | #include <sys/types.h> |
| 49 | #include <sys/socket.h> |
| 50 | #include <sys/un.h> |
| 51 | |
| 52 | #include <netinet/in.h> |
| 53 | |
| 54 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 56 | char *TCID = "socketpair01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | int testno; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | int sv[2]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
| 61 | void setup(void), cleanup(void); |
| 62 | |
| 63 | struct test_case_t { /* test case structure */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | int domain; /* PF_INET, PF_UNIX, ... */ |
| 65 | int type; /* SOCK_STREAM, SOCK_DGRAM ... */ |
| 66 | int proto; /* protocol number (usually 0 = default) */ |
| 67 | int *sv; /* socket descriptor vector */ |
| 68 | int retval; /* syscall return value */ |
| 69 | int experrno; /* expected errno */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | char *desc; |
| 71 | } tdat[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | { |
| 73 | 0, SOCK_STREAM, 0, sv, -1, EAFNOSUPPORT, "invalid domain"}, { |
| 74 | PF_INET, 75, 0, sv, -1, EINVAL, "invalid type"}, { |
| 75 | PF_UNIX, SOCK_DGRAM, 0, sv, 0, 0, "UNIX domain dgram"}, { |
| 76 | PF_INET, SOCK_RAW, 0, sv, -1, ESOCKTNOSUPPORT, |
| 77 | "raw open as non-root"}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 78 | #ifndef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | /* Skip since uClinux does not implement memory protection */ |
| 80 | { |
subrata_modak | 6b9fab2 | 2009-08-04 11:33:11 +0000 | [diff] [blame] | 81 | PF_UNIX, SOCK_STREAM, 0, 0, -1, EFAULT, "bad aligned pointer"}, { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | PF_UNIX, SOCK_STREAM, 0, (int *)7, -1, EFAULT, |
| 83 | "bad unaligned pointer"}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 84 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | { |
| 86 | PF_INET, SOCK_DGRAM, 17, sv, -1, EOPNOTSUPP, "UDP socket"}, { |
| 87 | PF_INET, SOCK_DGRAM, 6, sv, -1, ESOCKTNOSUPPORT, "TCP dgram"}, { |
| 88 | PF_INET, SOCK_STREAM, 6, sv, -1, EOPNOTSUPP, "TCP socket"}, { |
| 89 | PF_INET, SOCK_STREAM, 1, sv, -1, ESOCKTNOSUPPORT, "ICMP stream"},}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 91 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 95 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 96 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | int s; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 99 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | setup(); |
| 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 105 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
| 107 | TEST((s = socketpair(tdat[testno].domain, |
| 108 | tdat[testno].type, |
| 109 | tdat[testno].proto, |
| 110 | tdat[testno].sv))); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | if (TEST_RETURN >= 0) { |
| 112 | TEST_RETURN = 0; /* > 0 equivalent */ |
| 113 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | } |
| 115 | if (TEST_RETURN != tdat[testno].retval || |
| 116 | (TEST_RETURN && |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | (TEST_ERRNO != tdat[testno].experrno |
| 118 | && TEST_ERRNO != EPROTONOSUPPORT))) { |
| 119 | tst_resm(TFAIL, |
| 120 | "%s ; returned" |
| 121 | " %d (expected %d), errno %d (expected" |
| 122 | " %d)", tdat[testno].desc, s, |
| 123 | tdat[testno].retval, TEST_ERRNO, |
| 124 | tdat[testno].experrno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | } else { |
| 126 | tst_resm(TPASS, "%s successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | tdat[testno].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | (void)close(s); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | cleanup(); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 134 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 135 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 136 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 141 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 146 | } |