blob: 72c56a24a68570763b7b1e669a7a600cb26250e4 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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"
plars865695b2001-08-27 22:15:12 +000055
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020056char *TCID = "socketpair01";
plars865695b2001-08-27 22:15:12 +000057int testno;
plars865695b2001-08-27 22:15:12 +000058
subrata_modak56207ce2009-03-23 13:35:39 +000059int sv[2];
plars865695b2001-08-27 22:15:12 +000060
61void setup(void), cleanup(void);
62
63struct test_case_t { /* test case structure */
subrata_modak56207ce2009-03-23 13:35:39 +000064 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 */
plars865695b2001-08-27 22:15:12 +000070 char *desc;
71} tdat[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000072 {
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"},
vapier7ec19d92006-02-27 04:38:56 +000078#ifndef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +000079 /* Skip since uClinux does not implement memory protection */
80 {
subrata_modak6b9fab22009-08-04 11:33:11 +000081 PF_UNIX, SOCK_STREAM, 0, 0, -1, EFAULT, "bad aligned pointer"}, {
Wanlong Gao354ebb42012-12-07 10:10:04 +080082 PF_UNIX, SOCK_STREAM, 0, (int *)7, -1, EFAULT,
83 "bad unaligned pointer"},
vapier7ec19d92006-02-27 04:38:56 +000084#endif
subrata_modak56207ce2009-03-23 13:35:39 +000085 {
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"}, {
89PF_INET, SOCK_STREAM, 1, sv, -1, ESOCKTNOSUPPORT, "ICMP stream"},};
plars865695b2001-08-27 22:15:12 +000090
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020091int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
plars865695b2001-08-27 22:15:12 +000092
subrata_modak56207ce2009-03-23 13:35:39 +000093int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +000094{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020095 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020096 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000097 int s;
plars865695b2001-08-27 22:15:12 +000098
Garrett Cooper53740502010-12-16 00:04:01 -080099 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000100 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000101
102 setup();
103
plars865695b2001-08-27 22:15:12 +0000104 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800105 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000106 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)));
plars865695b2001-08-27 22:15:12 +0000111 if (TEST_RETURN >= 0) {
112 TEST_RETURN = 0; /* > 0 equivalent */
113 } else {
plars865695b2001-08-27 22:15:12 +0000114 }
115 if (TEST_RETURN != tdat[testno].retval ||
116 (TEST_RETURN &&
subrata_modak56207ce2009-03-23 13:35:39 +0000117 (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);
plars865695b2001-08-27 22:15:12 +0000125 } else {
126 tst_resm(TPASS, "%s successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000127 tdat[testno].desc);
plars865695b2001-08-27 22:15:12 +0000128 }
subrata_modak56207ce2009-03-23 13:35:39 +0000129 (void)close(s);
plars865695b2001-08-27 22:15:12 +0000130 }
131 }
132
133 cleanup();
robbiewfa451a12003-03-27 20:52:36 +0000134
Garrett Cooper53740502010-12-16 00:04:01 -0800135 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800136}
plars865695b2001-08-27 22:15:12 +0000137
subrata_modak56207ce2009-03-23 13:35:39 +0000138void setup(void)
plars865695b2001-08-27 22:15:12 +0000139{
plars865695b2001-08-27 22:15:12 +0000140
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200141 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000142}
143
subrata_modak56207ce2009-03-23 13:35:39 +0000144void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000145{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700146}