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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * Test Name: socket01
22 *
23 * Test Description:
24 * Verify that socket() returns the proper errno for various failure cases
25 *
26 * Usage: <for command-line>
27 * socket01 [-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"
55
56 char *TCID = "socket01";
57 int testno;
58
59 void setup(void), cleanup(void);
60
61 struct test_case_t { /* test case structure */
62 int domain; /* PF_INET, PF_UNIX, ... */
63 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
64 int proto; /* protocol number (usually 0 = default) */
65 int retval; /* syscall return value */
66 int experrno; /* expected errno */
67 char *desc;
68 } tdat[] = {
69 {
70 0, SOCK_STREAM, 0, -1, EAFNOSUPPORT, "invalid domain"}, {
71 PF_INET, 75, 0, -1, EINVAL, "invalid type"}, {
72 PF_UNIX, SOCK_DGRAM, 0, 0, 0, "UNIX domain dgram"}, {
73 PF_INET, SOCK_RAW, 0, -1, ESOCKTNOSUPPORT, "raw open as non-root"},
74 {
75 PF_INET, SOCK_DGRAM, 17, 0, 0, "UDP socket"}, {
76 PF_INET, SOCK_STREAM, 17, -1, ESOCKTNOSUPPORT, "UDP stream"}, {
77 PF_INET, SOCK_DGRAM, 6, -1, ESOCKTNOSUPPORT, "TCP dgram"}, {
78 PF_INET, SOCK_STREAM, 6, 0, 0, "TCP socket"}, {
79 PF_INET, SOCK_STREAM, 1, -1, ESOCKTNOSUPPORT, "ICMP stream"},};
80
81 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
82
main(int argc,char * argv[])83 int main(int argc, char *argv[])
84 {
85 int lc;
86 int s;
87
88 tst_parse_opts(argc, argv, NULL, NULL);
89
90 setup();
91
92 for (lc = 0; TEST_LOOPING(lc); ++lc) {
93 tst_count = 0;
94 for (testno = 0; testno < TST_TOTAL; ++testno) {
95 TEST((s = socket(tdat[testno].domain, tdat[testno].type,
96 tdat[testno].proto)));
97 if (TEST_RETURN >= 0) {
98 TEST_RETURN = 0; /* > 0 equivalent */
99 } else {
100 }
101 if (TEST_RETURN != tdat[testno].retval || (TEST_RETURN < 0 && (TEST_ERRNO != tdat[testno].experrno && TEST_ERRNO != EPROTONOSUPPORT))) { /* Change for defect 21065 for kernel change */
102 tst_resm(TFAIL, "%s ; returned" /* of return code for this test but don't want */
103 " %d (expected %d), errno %d (expected" /* to break on older kernels */
104 " %d)", tdat[testno].desc,
105 s, tdat[testno].retval,
106 TEST_ERRNO, tdat[testno].experrno);
107 } else {
108 tst_resm(TPASS, "%s successful",
109 tdat[testno].desc);
110 }
111 (void)close(s);
112 }
113 }
114 cleanup();
115 tst_exit();
116
117 }
118
setup(void)119 void setup(void)
120 {
121
122 TEST_PAUSE;
123 }
124
cleanup(void)125 void cleanup(void)
126 {
127 }
128