• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4  * Copyright (c) 2020 SUSE LLC
5  *
6  * 03/30/1992 AUTHOR: Richard Logan CO-PILOT: William Roske
7  *
8  */
9 /*\
10  * [Description]
11  * Negative test for dup(2) with bad fds.
12  *
13  * [Algorithm]
14  * Call dup(2) with invalid argument and make sure it returns -1 with errno set
15  * to EBADF.
16  */
17 
18 #include "tst_test.h"
19 
20 static struct tcase {
21 	int fd;
22 	int expected_errno;
23 } tcases[] = {
24 	{-1, EBADF},
25 	{1500, EBADF},
26 };
27 
run(unsigned int n)28 static void run(unsigned int n)
29 {
30 	struct tcase *tc = &tcases[n];
31 
32 	TEST(dup(tc->fd));
33 
34 	if (TST_RET < -1) {
35 		tst_res(TFAIL | TTERRNO, "Invalid dup() return value %ld",
36 			TST_RET);
37 		return;
38 	}
39 
40 	if (TST_RET == -1) {
41 		if (tc->expected_errno == TST_ERR) {
42 			tst_res(TPASS | TTERRNO, "dup(%d) failed as expected",
43 				tc->fd);
44 		} else {
45 			tst_res(TFAIL | TTERRNO, "dup(%d) failed unexpectedly",
46 				tc->fd);
47 		}
48 		return;
49 	}
50 
51 	tst_res(TFAIL, "dup(%d) succeeded unexpectedly", tc->fd);
52 	SAFE_CLOSE(TST_RET);
53 }
54 
55 static struct tst_test test = {
56 	.test = run,
57 	.tcnt = ARRAY_SIZE(tcases),
58 };
59