1 /* BZ #22679 getcwd(3) should not succeed without returning an absolute path.
2
3 Copyright (C) 2018-2022 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library 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 the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <support/check.h>
24 #include <support/namespace.h>
25 #include <support/support.h>
26 #include <support/temp_file.h>
27 #include <support/test-driver.h>
28 #include <support/xunistd.h>
29 #include <unistd.h>
30
31 static char *chroot_dir;
32
33 /* The actual test. Run it in a subprocess, so that the test harness
34 can remove the temporary directory in --direct mode. */
35 static void
getcwd_callback(void * closure)36 getcwd_callback (void *closure)
37 {
38 xchroot (chroot_dir);
39
40 errno = 0;
41 char *cwd = getcwd (NULL, 0);
42 TEST_COMPARE (errno, ENOENT);
43 TEST_VERIFY (cwd == NULL);
44
45 errno = 0;
46 cwd = realpath (".", NULL);
47 TEST_COMPARE (errno, ENOENT);
48 TEST_VERIFY (cwd == NULL);
49
50 _exit (0);
51 }
52
53 static int
do_test(void)54 do_test (void)
55 {
56 support_become_root ();
57 if (!support_can_chroot ())
58 return EXIT_UNSUPPORTED;
59
60 chroot_dir = support_create_temp_directory ("tst-getcwd-abspath-");
61 support_isolate_in_subprocess (getcwd_callback, NULL);
62
63 return 0;
64 }
65
66 #include <support/test-driver.c>
67