blob: ba206c3fd6b020ffc52294faf71044093486ec69 [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: 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"
plars865695b2001-08-27 22:15:12 +000055
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020056char *TCID = "socket01";
plars865695b2001-08-27 22:15:12 +000057int testno;
plars865695b2001-08-27 22:15:12 +000058
59void setup(void), cleanup(void);
60
61struct test_case_t { /* test case structure */
subrata_modak56207ce2009-03-23 13:35:39 +000062 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 */
plars865695b2001-08-27 22:15:12 +000067 char *desc;
68} tdat[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000069 {
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"}, {
79PF_INET, SOCK_STREAM, 1, -1, ESOCKTNOSUPPORT, "ICMP stream"},};
plars865695b2001-08-27 22:15:12 +000080
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020081int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
plars865695b2001-08-27 22:15:12 +000082
subrata_modak56207ce2009-03-23 13:35:39 +000083int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +000084{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020085 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020086 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000087 int s;
plars865695b2001-08-27 22:15:12 +000088
Garrett Cooper45e285d2010-11-22 12:19:25 -080089 msg = parse_opts(argc, argv, NULL, NULL);
90 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +000091 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000092 }
93
94 setup();
95
plars865695b2001-08-27 22:15:12 +000096 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080097 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +000098 for (testno = 0; testno < TST_TOTAL; ++testno) {
99 TEST((s = socket(tdat[testno].domain, tdat[testno].type,
100 tdat[testno].proto)));
plars865695b2001-08-27 22:15:12 +0000101 if (TEST_RETURN >= 0) {
102 TEST_RETURN = 0; /* > 0 equivalent */
103 } else {
plars865695b2001-08-27 22:15:12 +0000104 }
subrata_modak56207ce2009-03-23 13:35:39 +0000105 if (TEST_RETURN != tdat[testno].retval || (TEST_RETURN < 0 && (TEST_ERRNO != tdat[testno].experrno && TEST_ERRNO != EPROTONOSUPPORT))) { /* Change for defect 21065 for kernel change */
106 tst_resm(TFAIL, "%s ; returned" /* of return code for this test but don't want */
107 " %d (expected %d), errno %d (expected" /* to break on older kernels */
108 " %d)", tdat[testno].desc,
109 s, tdat[testno].retval,
110 TEST_ERRNO, tdat[testno].experrno);
plars865695b2001-08-27 22:15:12 +0000111 } else {
112 tst_resm(TPASS, "%s successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000113 tdat[testno].desc);
plars865695b2001-08-27 22:15:12 +0000114 }
subrata_modak56207ce2009-03-23 13:35:39 +0000115 (void)close(s);
plars865695b2001-08-27 22:15:12 +0000116 }
117 }
118 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800119 tst_exit();
robbiewfa451a12003-03-27 20:52:36 +0000120
Garrett Cooper2c282152010-12-16 00:55:50 -0800121}
robbiewfa451a12003-03-27 20:52:36 +0000122
subrata_modak56207ce2009-03-23 13:35:39 +0000123void setup(void)
plars865695b2001-08-27 22:15:12 +0000124{
plars865695b2001-08-27 22:15:12 +0000125
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200126 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000127}
128
subrata_modak56207ce2009-03-23 13:35:39 +0000129void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000130{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700131}