blob: c8378bf47947d875ae207b6a92e39f4a32081c51 [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: getsockname01
22 *
23 * Test Description:
24 * Verify that getsockname() returns the proper errno for various failure cases
25 *
26 * Usage: <for command-line>
27 * getsockname01 [-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 Ported by Wayne Boyer
37 *
38 * RESTRICTIONS:
39 * None.
40 */
41
42#include <stdio.h>
43#include <unistd.h>
44#include <errno.h>
plars94d79562002-03-26 19:39:42 +000045#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000046
47#include <sys/types.h>
48#include <sys/socket.h>
49#include <sys/signal.h>
50#include <sys/ioctl.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 = "getsockname01";
plars865695b2001-08-27 22:15:12 +000057int testno;
58
subrata_modak56207ce2009-03-23 13:35:39 +000059int s; /* socket descriptor */
plars865695b2001-08-27 22:15:12 +000060struct sockaddr_in sin0, fsin1;
subrata_modak56207ce2009-03-23 13:35:39 +000061socklen_t sinlen;
plars865695b2001-08-27 22:15:12 +000062
subrata_modak4bb656a2009-02-26 12:02:09 +000063void setup(void), setup0(void), setup1(void),
subrata_modak56207ce2009-03-23 13:35:39 +000064cleanup(void), cleanup0(void), cleanup1(void);
plars865695b2001-08-27 22:15:12 +000065
66struct test_case_t { /* test case structure */
subrata_modak56207ce2009-03-23 13:35:39 +000067 int domain; /* PF_INET, PF_UNIX, ... */
68 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
69 int proto; /* protocol number (usually 0 = default) */
plars865695b2001-08-27 22:15:12 +000070 struct sockaddr *sockaddr; /* socket address buffer */
subrata_modak56207ce2009-03-23 13:35:39 +000071 socklen_t *salen; /* getsockname's 3rd argument */
72 int retval; /* syscall return value */
73 int experrno; /* expected errno */
74 void (*setup) (void);
75 void (*cleanup) (void);
plars865695b2001-08-27 22:15:12 +000076 char *desc;
77} tdat[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000078 {
79 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
80 &sinlen, -1, EBADF, setup0, cleanup0,
81 "bad file descriptor"}, {
82 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
83 &sinlen, -1, ENOTSOCK, setup0, cleanup0,
84 "bad file descriptor"},
vapier7ec19d92006-02-27 04:38:56 +000085#ifndef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +000086 /* Skip since uClinux does not implement memory protection */
87 {
Cyril Hrubis4e2bab82014-09-24 16:34:35 +020088 PF_INET, SOCK_STREAM, 0, NULL,
subrata_modak56207ce2009-03-23 13:35:39 +000089 &sinlen, -1, EFAULT, setup1, cleanup1,
90 "invalid socket buffer"}, {
Wanlong Gao354ebb42012-12-07 10:10:04 +080091 /* invalid salen test for aligned input */
subrata_modak6b9fab22009-08-04 11:33:11 +000092 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
Cyril Hrubis4e2bab82014-09-24 16:34:35 +020093 NULL, -1, EFAULT, setup1, cleanup1,
subrata_modak6b9fab22009-08-04 11:33:11 +000094 "invalid aligned salen"}, {
Wanlong Gao354ebb42012-12-07 10:10:04 +080095 /* invalid salen test for unaligned input */
subrata_modak56207ce2009-03-23 13:35:39 +000096 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
97 (socklen_t *) 1, -1, EFAULT, setup1, cleanup1,
subrata_modak6b9fab22009-08-04 11:33:11 +000098 "invalid unaligned salen"},
vapier7ec19d92006-02-27 04:38:56 +000099#endif
plars865695b2001-08-27 22:15:12 +0000100};
101
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200102int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
plars865695b2001-08-27 22:15:12 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +0000105{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200106 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200107 const char *msg;
plars865695b2001-08-27 22:15:12 +0000108
Garrett Cooper45e285d2010-11-22 12:19:25 -0800109 msg = parse_opts(argc, argv, NULL, NULL);
110 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000111 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000112 }
113
114 setup();
115
plars865695b2001-08-27 22:15:12 +0000116 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800117 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000118 for (testno = 0; testno < TST_TOTAL; ++testno) {
plars865695b2001-08-27 22:15:12 +0000119 tdat[testno].setup();
120
121 TEST(getsockname(s, tdat[testno].sockaddr,
subrata_modak56207ce2009-03-23 13:35:39 +0000122 tdat[testno].salen));
plars865695b2001-08-27 22:15:12 +0000123 if (TEST_RETURN != tdat[testno].retval ||
124 (TEST_RETURN < 0 &&
125 TEST_ERRNO != tdat[testno].experrno)) {
126 tst_resm(TFAIL, "%s ; returned"
subrata_modak358c3ee2009-10-26 14:55:46 +0000127 " %ld (expected %d), errno %d (expected"
subrata_modak56207ce2009-03-23 13:35:39 +0000128 " %d)", tdat[testno].desc,
129 TEST_RETURN, tdat[testno].retval,
130 TEST_ERRNO, tdat[testno].experrno);
plars865695b2001-08-27 22:15:12 +0000131 } else {
132 tst_resm(TPASS, "%s successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000133 tdat[testno].desc);
plars865695b2001-08-27 22:15:12 +0000134 }
135 tdat[testno].cleanup();
136 }
137 }
138 cleanup();
139
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800140 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800141}
plars865695b2001-08-27 22:15:12 +0000142
subrata_modak56207ce2009-03-23 13:35:39 +0000143void setup(void)
plars865695b2001-08-27 22:15:12 +0000144{
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200145 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000146
plars865695b2001-08-27 22:15:12 +0000147 /* initialize local sockaddr */
148 sin0.sin_family = AF_INET;
149 sin0.sin_port = 0;
150 sin0.sin_addr.s_addr = INADDR_ANY;
151}
152
subrata_modak56207ce2009-03-23 13:35:39 +0000153void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000154{
plars865695b2001-08-27 22:15:12 +0000155}
156
subrata_modak56207ce2009-03-23 13:35:39 +0000157void setup0(void)
plars865695b2001-08-27 22:15:12 +0000158{
159 if (tdat[testno].experrno == EBADF)
160 s = 400; /* anything not an open file */
subrata_modak56207ce2009-03-23 13:35:39 +0000161 else if ((s = open("/dev/null", O_WRONLY)) == -1)
162 tst_brkm(TBROK, cleanup, "error opening /dev/null - "
163 "errno: %s", strerror(errno));
plars94d79562002-03-26 19:39:42 +0000164
plars865695b2001-08-27 22:15:12 +0000165}
166
subrata_modak56207ce2009-03-23 13:35:39 +0000167void cleanup0(void)
plars865695b2001-08-27 22:15:12 +0000168{
169 s = -1;
170}
171
subrata_modak56207ce2009-03-23 13:35:39 +0000172void setup1(void)
plars865695b2001-08-27 22:15:12 +0000173{
174 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
175 if (s < 0) {
176 tst_brkm(TBROK, cleanup, "socket setup failed for getsockname "
subrata_modak56207ce2009-03-23 13:35:39 +0000177 "test %d: %s", testno, strerror(errno));
plars865695b2001-08-27 22:15:12 +0000178 }
robbiew96d23372003-03-26 20:40:04 +0000179 if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
plars865695b2001-08-27 22:15:12 +0000180 tst_brkm(TBROK, cleanup, "socket bind failed for getsockname "
subrata_modak56207ce2009-03-23 13:35:39 +0000181 "test %d: %s", testno, strerror(errno));
plars865695b2001-08-27 22:15:12 +0000182 }
183 sinlen = sizeof(fsin1);
184}
185
subrata_modak56207ce2009-03-23 13:35:39 +0000186void cleanup1(void)
plars865695b2001-08-27 22:15:12 +0000187{
subrata_modak56207ce2009-03-23 13:35:39 +0000188 (void)close(s);
plars865695b2001-08-27 22:15:12 +0000189 s = -1;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700190}