1 /* Test for /proc/self/fd (or /dev/fd) pathname construction.
2 Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
20 #include <fd_to_filename.h>
22 #include <support/check.h>
23 #include <support/xunistd.h>
25 /* Run a check on one value. */
30 /* Negative descriptor values violate the precondition. */
33 struct fd_to_filename storage
;
34 char *actual
= __fd_to_filename (value
, &storage
);
36 snprintf (expected
, sizeof (expected
), FD_TO_FILENAME_PREFIX
"%d", value
);
37 TEST_COMPARE_STRING (actual
, expected
);
40 /* Check various ranges constructed around powers. */
42 check_ranges (int base
)
44 unsigned int power
= 1;
47 for (int factor
= 1; factor
< base
; ++factor
)
48 for (int shift
= -1000; shift
<= 1000; ++shift
)
49 check (factor
* power
+ shift
);
51 while (!__builtin_mul_overflow (power
, base
, &power
));
54 /* Check that it is actually possible to use a the constructed
62 struct fd_to_filename storage
;
63 int read_alias
= xopen (__fd_to_filename (pipes
[0], &storage
), O_RDONLY
, 0);
64 int write_alias
= xopen (__fd_to_filename (pipes
[1], &storage
), O_WRONLY
, 0);
66 /* Ensure that all the descriptor numbers are different. */
67 TEST_VERIFY (pipes
[0] < pipes
[1]);
68 TEST_VERIFY (pipes
[1] < read_alias
);
69 TEST_VERIFY (read_alias
< write_alias
);
71 xwrite (write_alias
, "1", 1);
73 TEST_COMPARE_BLOB ("1", 1, buf
, read (pipes
[0], buf
, sizeof (buf
)));
75 xwrite (pipes
[1], "2", 1);
76 TEST_COMPARE_BLOB ("2", 1, buf
, read (read_alias
, buf
, sizeof (buf
)));
78 xwrite (write_alias
, "3", 1);
79 TEST_COMPARE_BLOB ("3", 1, buf
, read (read_alias
, buf
, sizeof (buf
)));
81 xwrite (pipes
[1], "4", 1);
82 TEST_COMPARE_BLOB ("4", 1, buf
, read (pipes
[0], buf
, sizeof (buf
)));
101 #include <support/test-driver.c>