blob: f2356a5129157c2abf858f1410ad2aff8e148b78 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
zenglg.jyf8cb57f2013-12-13 19:55:35 +08004 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
zenglg.jyf8cb57f2013-12-13 19:55:35 +080017 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000019 */
20
21/*
plars865695b2001-08-27 22:15:12 +000022 * Verify that getpeername() returns the proper errno for various failure cases
plars865695b2001-08-27 22:15:12 +000023 */
24
25#include <stdio.h>
26#include <unistd.h>
27#include <errno.h>
plarsb05da4c2002-03-26 19:38:25 +000028#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000029#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/signal.h>
32#include <sys/ioctl.h>
plars865695b2001-08-27 22:15:12 +000033#include <netinet/in.h>
34
35#include "test.h"
zenglg.jyf8cb57f2013-12-13 19:55:35 +080036#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000037
zenglg.jyf8cb57f2013-12-13 19:55:35 +080038static struct sockaddr_in server_addr;
39static struct sockaddr_in fsin1;
40static socklen_t sinlen;
zenglg.jyb24f1672013-12-13 19:56:27 +080041static socklen_t invalid_sinlen = -1;
zenglg.jyf8cb57f2013-12-13 19:55:35 +080042static int sv[2];
plars865695b2001-08-27 22:15:12 +000043
zenglg.jyf8cb57f2013-12-13 19:55:35 +080044static void setup(void);
45static void setup2(int);
46static void setup3(int);
47static void setup4(int);
48static void cleanup(void);
49static void cleanup2(int);
50static void cleanup4(int);
plars865695b2001-08-27 22:15:12 +000051
zenglg.jyf8cb57f2013-12-13 19:55:35 +080052struct test_case_t {
53 int sockfd;
54 struct sockaddr *sockaddr;
55 socklen_t *addrlen;
56 int expretval;
57 int experrno;
58 void (*setup) (int);
59 void (*cleanup) (int);
60 char *name;
61} test_cases[] = {
62 {-1, (struct sockaddr *)&fsin1, &sinlen, -1, EBADF, NULL, NULL,
63 "EBADF"},
64 {-1, (struct sockaddr *)&fsin1, &sinlen, -1, ENOTSOCK, setup2, cleanup2,
65 "ENOTSOCK"},
66 {-1, (struct sockaddr *)&fsin1, &sinlen, -1, ENOTCONN, setup3, cleanup2,
67 "ENOTCONN"},
zenglg.jyb24f1672013-12-13 19:56:27 +080068 {-1, (struct sockaddr *)&fsin1, &invalid_sinlen, -1, EINVAL, setup4,
69 cleanup4, "EINVAL"},
vapier7ec19d92006-02-27 04:38:56 +000070#ifndef UCLINUX
zenglg.jyf8cb57f2013-12-13 19:55:35 +080071 {-1, (struct sockaddr *)-1, &sinlen, -1, EFAULT, setup4, cleanup4,
72 "EFAULT"},
Cyril Hrubis4e2bab82014-09-24 16:34:35 +020073 {-1, (struct sockaddr *)&fsin1, NULL, -1, EFAULT, setup4,
zenglg.jyf8cb57f2013-12-13 19:55:35 +080074 cleanup4, "EFAULT"},
75 {-1, (struct sockaddr *)&fsin1, (socklen_t *)1, -1, EFAULT, setup4,
76 cleanup4, "EFAULT"},
vapier7ec19d92006-02-27 04:38:56 +000077#endif
plars865695b2001-08-27 22:15:12 +000078};
79
zenglg.jyf8cb57f2013-12-13 19:55:35 +080080char *TCID = "getpeername01";
81int TST_TOTAL = ARRAY_SIZE(test_cases);
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;
zenglg.jyf8cb57f2013-12-13 19:55:35 +080087 int i;
plars865695b2001-08-27 22:15:12 +000088
Garrett Cooper45e285d2010-11-22 12:19:25 -080089 msg = parse_opts(argc, argv, NULL, NULL);
zenglg.jyf8cb57f2013-12-13 19:55:35 +080090 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 setup();
94
plars865695b2001-08-27 22:15:12 +000095 for (lc = 0; TEST_LOOPING(lc); ++lc) {
plars865695b2001-08-27 22:15:12 +000096
zenglg.jyf8cb57f2013-12-13 19:55:35 +080097 tst_count = 0;
98
99 for (i = 0; i < TST_TOTAL; ++i) {
100
101 if (test_cases[i].setup != NULL)
102 test_cases[i].setup(i);
103
104 TEST(getpeername(test_cases[i].sockfd,
105 test_cases[i].sockaddr,
106 test_cases[i].addrlen));
107
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800108 if (TEST_RETURN == test_cases[i].expretval &&
109 TEST_ERRNO == test_cases[i].experrno) {
110 tst_resm(TPASS,
111 "test getpeername() %s successful",
112 test_cases[i].name);
plars865695b2001-08-27 22:15:12 +0000113 } else {
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800114 tst_resm(TFAIL,
115 "test getpeername() %s failed; "
116 "returned %ld (expected %d), errno %d "
117 "(expected %d)", test_cases[i].name,
118 TEST_RETURN, test_cases[i].expretval,
119 TEST_ERRNO, test_cases[i].experrno);
plars865695b2001-08-27 22:15:12 +0000120 }
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800121
122 if (test_cases[i].cleanup != NULL)
123 test_cases[i].cleanup(i);
plars865695b2001-08-27 22:15:12 +0000124 }
125 }
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800126
plars865695b2001-08-27 22:15:12 +0000127 cleanup();
128
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800129 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800130}
plars865695b2001-08-27 22:15:12 +0000131
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800132static void setup(void)
plars865695b2001-08-27 22:15:12 +0000133{
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200134 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000135
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800136 server_addr.sin_family = AF_INET;
137 server_addr.sin_port = 0;
138 server_addr.sin_addr.s_addr = INADDR_ANY;
plars865695b2001-08-27 22:15:12 +0000139
plars865695b2001-08-27 22:15:12 +0000140 sinlen = sizeof(fsin1);
141}
142
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800143static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000144{
plars865695b2001-08-27 22:15:12 +0000145}
146
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800147static void setup2(int i)
plars865695b2001-08-27 22:15:12 +0000148{
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800149 test_cases[i].sockfd = SAFE_OPEN(cleanup, "/dev/null", O_WRONLY, 0666);
150}
plars865695b2001-08-27 22:15:12 +0000151
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800152static void setup3(int i)
153{
154 test_cases[i].sockfd = socket(PF_INET, SOCK_STREAM, 0);
155 if (test_cases[i].sockfd < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 tst_brkm(TBROK | TERRNO, cleanup,
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800157 "socket setup failed for getpeername test %d", i);
plars865695b2001-08-27 22:15:12 +0000158 }
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800159 if (bind(test_cases[i].sockfd, (struct sockaddr *)&server_addr,
160 sizeof(server_addr)) < 0) {
161 tst_brkm(TBROK | TERRNO, cleanup,
162 "socket bind failed for getpeername test %d", i);
163 }
plars865695b2001-08-27 22:15:12 +0000164}
165
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800166static void setup4(int i)
plars865695b2001-08-27 22:15:12 +0000167{
zenglg.jyf8cb57f2013-12-13 19:55:35 +0800168 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
169 tst_brkm(TBROK | TERRNO, cleanup,
170 "socketpair failed for getpeername test %d", i);
171 }
172 test_cases[i].sockfd = sv[0];
173}
174
175static void cleanup2(int i)
176{
177 SAFE_CLOSE(cleanup, test_cases[i].sockfd);
178}
179
180static void cleanup4(int i)
181{
182 SAFE_CLOSE(cleanup, sv[0]);
183 SAFE_CLOSE(cleanup, sv[1]);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700184}