Skip to content

Commit

Permalink
getsockname01: refactor with new LTP API
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Manzini <andrea.manzini@suse.com>
  • Loading branch information
ilmanzo committed Apr 8, 2024
1 parent 6f97789 commit d867576
Showing 1 changed file with 53 additions and 158 deletions.
211 changes: 53 additions & 158 deletions testcases/kernel/syscalls/getsockname/getsockname01.c
Original file line number Diff line number Diff line change
@@ -1,181 +1,76 @@
/*
*
* 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
*/
// SPDX-License-Identifier: GPL-2.0-or-later

/*
* Test Name: getsockname01
*
* Test Description:
* Verify that getsockname() returns the proper errno for various failure cases
*
* Usage: <for command-line>
* getsockname01 [-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.
* Copyright (c) International Business Machines Corp., 2001
* 07/2001 Ported by Wayne Boyer
* Copyright (C) 2024 SUSE LLC Andrea Manzini <andrea.manzini@suse.com>
*/

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <sys/ioctl.h>

#include <netinet/in.h>

#include "test.h"
#include "safe_macros.h"

char *TCID = "getsockname01";
int testno;
/*\
* [Description]
* Verify that getsockname() 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 socket buffer o invalid socklen
*/

int s; /* socket descriptor */
struct sockaddr_in sin0, fsin1;
socklen_t sinlen;
#include "tst_test.h"

void setup(void), setup0(void), setup1(void),
cleanup(void), cleanup0(void), cleanup1(void);
static int sockfd;
static struct sockaddr_in sin0, fsin1;
static socklen_t sinlen;

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) */
struct sockaddr *sockaddr; /* socket address buffer */
socklen_t *salen; /* getsockname's 3rd argument */
int retval; /* syscall return value */
int experrno; /* expected errno */
void (*setup) (void);
void (*cleanup) (void);
char *desc;
} tdat[] = {
{
PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
&sinlen, -1, EBADF, setup0, cleanup0,
"bad file descriptor"}, {
PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
&sinlen, -1, ENOTSOCK, setup0, cleanup0,
"bad file descriptor"},
#ifndef UCLINUX
/* Skip since uClinux does not implement memory protection */
{
PF_INET, SOCK_STREAM, 0, NULL,
&sinlen, -1, EFAULT, setup1, cleanup1,
"invalid socket buffer"}, {
/* invalid salen test for aligned input */
PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
NULL, -1, EFAULT, setup1, cleanup1,
"invalid aligned salen"}, {
/* invalid salen test for unaligned input */
PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
(socklen_t *) 1, -1, EFAULT, setup1, cleanup1,
"invalid unaligned salen"},
#endif
};

int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);

int main(int argc, char *argv[])
static void setup_notopen(void)
{
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(getsockname(s, tdat[testno].sockaddr,
tdat[testno].salen));
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 = 400;
}

void setup(void)
static void setup_file(void)
{
TEST_PAUSE;

/* initialize local sockaddr */
sin0.sin_family = AF_INET;
sin0.sin_port = 0;
sin0.sin_addr.s_addr = INADDR_ANY;
sockfd = SAFE_OPEN("/dev/null", O_WRONLY);
}

void cleanup(void)
static void setup_bind(void)
{
sockfd = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
SAFE_BIND(sockfd, (struct sockaddr *)&sin0, sizeof(sin0));
sinlen = sizeof(fsin1);
}

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));

}
static struct test_case {
struct sockaddr_in *sockaddr;
socklen_t *addrlen;
int experrno;
void (*setup)(void);
char *desc;
} tcases[] = {
{ .sockaddr = &fsin1, .addrlen = &sinlen, .experrno = EBADF, .setup = setup_notopen, "bad file descriptor"},
{ .sockaddr = &fsin1, .addrlen = &sinlen, .experrno = ENOTSOCK, .setup = setup_file, "bad file descriptor"},
{ .addrlen = &sinlen, .experrno = EFAULT, .setup = setup_bind, "invalid socket buffer"},
{ .sockaddr = &fsin1, .experrno = EFAULT, .setup = setup_bind, "invalid aligned salen"},
{ .sockaddr = &fsin1, .addrlen = (socklen_t *) 1, .experrno = EFAULT, setup_bind, "invalid unaligned salen"},
};

void cleanup0(void)
static void check_getsockname(unsigned int nr)
{
s = -1;
}
struct test_case *tc = &tcases[nr];

void setup1(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);
tc->setup();
TST_EXP_FAIL(getsockname(sockfd, (struct sockaddr *) tc->sockaddr, tc->addrlen), tc->experrno, "%s", tc->desc);
if (sockfd)
close(sockfd);
sockfd = -1;
}

void cleanup1(void)
static void setup(void)
{
(void)close(s);
s = -1;
sin0.sin_family = AF_INET;
sin0.sin_port = 0;
sin0.sin_addr.s_addr = INADDR_ANY;
}

static struct tst_test test = {
.setup = setup,
.test = check_getsockname,
.tcnt = ARRAY_SIZE(tcases),
};

0 comments on commit d867576

Please sign in to comment.