forked from linux-test-project/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
getsockopt01: refactor with new LTP API
Signed-off-by: Andrea Manzini <andrea.manzini@suse.com>
- Loading branch information
Showing
1 changed file
with
64 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,208 +1,88 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
/* | ||
* | ||
* Copyright (c) International Business Machines Corp., 2001 | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | ||
* the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* Copyright (c) International Business Machines Corp., 2001 | ||
* 07/2001 Ported by Wayne Boyer | ||
* Copyright (C) 2024 SUSE LLC Andrea Manzini <andrea.manzini@suse.com> | ||
*/ | ||
|
||
/* | ||
* Test Name: getsockopt01 | ||
* | ||
* Test Description: | ||
* Verify that getsockopt() returns the proper errno for various failure cases | ||
* | ||
* Usage: <for command-line> | ||
* getsockopt01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] | ||
* where, -c n : Run n copies concurrently. | ||
* -e : Turn on errno logging. | ||
* -i n : Execute test n times. | ||
* -I x : Execute test for x seconds. | ||
* -P x : Pause for x seconds between iterations. | ||
* -t : Turn on syscall timing. | ||
* | ||
* HISTORY | ||
* 07/2001 Ported by Wayne Boyer | ||
* | ||
* RESTRICTIONS: | ||
* None. | ||
/*\ | ||
* [Description] | ||
* Verify that getsockopt() returns the proper errno for various failure cases: | ||
* - EBADF on a not open file | ||
* - ENOTSOCK on a file descriptor not linked to a socket | ||
* - EFAULT on invalid address of value or length | ||
* - EOPNOTSUPP on invalid option name or protocol | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include "tst_test.h" | ||
|
||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <sys/signal.h> | ||
#include <sys/ioctl.h> | ||
static int sock_fake, sock_null, sock_bind; | ||
static struct sockaddr_in sin0; | ||
static int sinlen; | ||
static int optval; | ||
static socklen_t optlen; | ||
|
||
#include <netinet/in.h> | ||
static struct test_case { | ||
int *sockfd; | ||
int level; | ||
int optname; | ||
void *optval; | ||
socklen_t *optlen; | ||
int experrno; | ||
char *desc; | ||
} tcases[] = { | ||
{.sockfd = &sock_fake, .level = SOL_SOCKET, .optname = SO_OOBINLINE, .optval = &optval, | ||
.optlen = &optlen, .experrno = EBADF, .desc = "bad file descriptor"}, | ||
|
||
#include "test.h" | ||
#include "safe_macros.h" | ||
{.sockfd = &sock_null, .level = SOL_SOCKET, .optname = SO_OOBINLINE, .optval = &optval, | ||
.optlen = &optlen, .experrno = ENOTSOCK, .desc = "bad file descriptor"}, | ||
|
||
char *TCID = "getsockopt01"; | ||
int testno; | ||
{.sockfd = &sock_bind, .level = SOL_SOCKET, .optname = SO_OOBINLINE, .optval = 0, | ||
.optlen = &optlen, .experrno = EFAULT, .desc = "invalid option buffer"}, | ||
|
||
int s; /* socket descriptor */ | ||
struct sockaddr_in sin0, fsin1; | ||
int sinlen; | ||
int optval; | ||
socklen_t optlen; | ||
{.sockfd = &sock_bind, .level = SOL_SOCKET, .optname = SO_OOBINLINE, .optval = &optval, | ||
.optlen = 0, .experrno = EFAULT, .desc = "invalid optlen"}, | ||
|
||
void setup(void), setup0(void), setup1(void), | ||
cleanup(void), cleanup0(void), cleanup1(void); | ||
{.sockfd = &sock_bind, .level = 500, .optname = SO_OOBINLINE, .optval = &optval, | ||
.optlen = &optlen, .experrno = EOPNOTSUPP, .desc = "invalid level"}, | ||
|
||
struct test_case_t { /* test case structure */ | ||
int domain; /* PF_INET, PF_UNIX, ... */ | ||
int type; /* SOCK_STREAM, SOCK_DGRAM ... */ | ||
int proto; /* protocol number (usually 0 = default) */ | ||
int level; /* IPPROTO_* */ | ||
int optname; | ||
void *optval; | ||
socklen_t *optlen; | ||
struct sockaddr *sin; | ||
int salen; | ||
int retval; /* syscall return value */ | ||
int experrno; /* expected errno */ | ||
void (*setup) (void); | ||
void (*cleanup) (void); | ||
char *desc; | ||
} tdat[] = { | ||
{ | ||
PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, | ||
&optlen, (struct sockaddr *)&fsin1, sizeof(fsin1), | ||
-1, EBADF, setup0, cleanup0, "bad file descriptor"} | ||
, { | ||
PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, | ||
&optlen, (struct sockaddr *)&fsin1, sizeof(fsin1), | ||
-1, ENOTSOCK, setup0, cleanup0, "bad file descriptor"} | ||
, | ||
#ifndef UCLINUX | ||
{ | ||
PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, 0, &optlen, | ||
(struct sockaddr *)&fsin1, sizeof(fsin1), -1, | ||
EFAULT, setup1, cleanup1, "invalid option buffer"} | ||
, { | ||
PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, 0, | ||
(struct sockaddr *)&fsin1, sizeof(fsin1), -1, | ||
EFAULT, setup1, cleanup1, "invalid optlen"} | ||
, | ||
#endif /* UCLINUX */ | ||
{ | ||
PF_INET, SOCK_STREAM, 0, 500, SO_OOBINLINE, &optval, &optlen, | ||
(struct sockaddr *)&fsin1, sizeof(fsin1), -1, | ||
EOPNOTSUPP, setup1, cleanup1, "invalid level"} | ||
, { | ||
PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval, | ||
&optlen, (struct sockaddr *)&fsin1, sizeof(fsin1), | ||
-1, EOPNOTSUPP, setup1, cleanup1, "invalid option name"} | ||
, { | ||
PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval, | ||
&optlen, (struct sockaddr *)&fsin1, sizeof(fsin1), | ||
-1, EOPNOTSUPP, setup1, cleanup1, | ||
"invalid option name (UDP)"} | ||
, { | ||
PF_INET, SOCK_STREAM, 0, IPPROTO_IP, -1, &optval, &optlen, | ||
(struct sockaddr *)&fsin1, sizeof(fsin1), -1, | ||
ENOPROTOOPT, setup1, cleanup1, "invalid option name (IP)"} | ||
, { | ||
PF_INET, SOCK_STREAM, 0, IPPROTO_TCP, -1, &optval, &optlen, | ||
(struct sockaddr *)&fsin1, sizeof(fsin1), -1, | ||
ENOPROTOOPT, setup1, cleanup1, "invalid option name (TCP)"} | ||
,}; | ||
|
||
int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int lc; | ||
|
||
tst_parse_opts(argc, argv, NULL, NULL); | ||
|
||
setup(); | ||
|
||
for (lc = 0; TEST_LOOPING(lc); ++lc) { | ||
tst_count = 0; | ||
for (testno = 0; testno < TST_TOTAL; ++testno) { | ||
tdat[testno].setup(); | ||
|
||
TEST(getsockopt(s, tdat[testno].level, | ||
tdat[testno].optname, | ||
tdat[testno].optval, | ||
tdat[testno].optlen)); | ||
if (TEST_RETURN != tdat[testno].retval || | ||
(TEST_RETURN < 0 && | ||
TEST_ERRNO != tdat[testno].experrno)) { | ||
tst_resm(TFAIL, "%s ; returned" | ||
" %ld (expected %d), errno %d (expected" | ||
" %d)", tdat[testno].desc, | ||
TEST_RETURN, tdat[testno].retval, | ||
TEST_ERRNO, tdat[testno].experrno); | ||
} else { | ||
tst_resm(TPASS, "%s successful", | ||
tdat[testno].desc); | ||
} | ||
tdat[testno].cleanup(); | ||
} | ||
} | ||
cleanup(); | ||
|
||
tst_exit(); | ||
} | ||
{.sockfd = &sock_bind, .level = IPPROTO_UDP, .optname = SO_OOBINLINE, .optval = &optval, | ||
.optlen = &optlen, .experrno = EOPNOTSUPP, .desc = "invalid option name"}, | ||
|
||
void setup(void) | ||
{ | ||
TEST_PAUSE; | ||
{.sockfd = &sock_bind, .level = IPPROTO_UDP, .optname = SO_OOBINLINE, .optval = &optval, | ||
.optlen = &optlen, .experrno = EOPNOTSUPP, .desc = "invalid option name (UDP)"}, | ||
|
||
/* initialize local sockaddr */ | ||
sin0.sin_family = AF_INET; | ||
sin0.sin_port = 0; | ||
sin0.sin_addr.s_addr = INADDR_ANY; | ||
} | ||
{.sockfd = &sock_bind, .level = IPPROTO_IP, .optname = -1, .optval = &optval, | ||
.optlen = &optlen, .experrno = ENOPROTOOPT, .desc = "invalid option name (IP)"}, | ||
|
||
void cleanup(void) | ||
{ | ||
} | ||
{.sockfd = &sock_bind, .level = IPPROTO_TCP, .optname = -1, .optval = &optval, | ||
.optlen = &optlen, .experrno = ENOPROTOOPT, .desc = "invalid option name (TCP)"} | ||
}; | ||
|
||
void setup0(void) | ||
{ | ||
if (tdat[testno].experrno == EBADF) | ||
s = 400; /* anything not an open file */ | ||
else if ((s = open("/dev/null", O_WRONLY)) == -1) | ||
tst_brkm(TBROK, cleanup, "error opening /dev/null - " | ||
"errno: %s", strerror(errno)); | ||
} | ||
|
||
void cleanup0(void) | ||
static void check_getsockopt(unsigned int nr) | ||
{ | ||
s = -1; | ||
struct test_case *tc = &tcases[nr]; | ||
|
||
TST_EXP_FAIL(getsockopt(*(tc->sockfd), tc->level, tc->optname, tc->optval, tc->optlen), tc->experrno, "%s", tc->desc); | ||
} | ||
|
||
void setup1(void) | ||
static void setup(void) | ||
{ | ||
s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type, | ||
tdat[testno].proto); | ||
SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0)); | ||
sinlen = sizeof(fsin1); | ||
sin0.sin_family = AF_INET; | ||
sin0.sin_port = 0; | ||
sin0.sin_addr.s_addr = INADDR_ANY; | ||
sock_fake = 400; | ||
sock_null = SAFE_OPEN("/dev/null", O_WRONLY); | ||
sock_bind = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0); | ||
SAFE_BIND(sock_bind, (struct sockaddr *)&sin0, sizeof(sin0)); | ||
sinlen = sizeof(sin0); | ||
optlen = sizeof(optval); | ||
} | ||
|
||
void cleanup1(void) | ||
{ | ||
(void)close(s); | ||
s = -1; | ||
} | ||
static struct tst_test test = { | ||
.setup = setup, | ||
.test = check_getsockopt, | ||
.tcnt = ARRAY_SIZE(tcases), | ||
}; |