build: ensure make-prime-list doesn't access out of bounds memory
[platform/upstream/coreutils.git] / tests / misc / realpath.sh
1 #!/bin/sh
2 # Validate realpath operation
3
4 # Copyright (C) 2011-2013 Free Software Foundation, Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program 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
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ realpath
21
22 stat_single=$(stat -c %d:%i /) || framework_failure_
23 stat_double=$(stat -c %d:%i //) || framework_failure_
24 double_slash=//
25 if test x"$stat_single" = x"$stat_double"; then
26   double_slash=/
27 fi
28 nl='
29 '
30
31 test -d /dev || framework_failure_
32
33 # Setup dir, file, symlink structure
34
35 mkdir -p dir1/dir2 || framework_failure_
36 ln -s dir1/dir2 ldir2 || framework_failure_
37 touch dir1/f dir1/dir2/f || framework_failure_
38 ln -s / one || framework_failure_
39 ln -s // two || framework_failure_
40 ln -s /// three || framework_failure_
41
42 # Basic operation
43 realpath -Pqz . >/dev/null || fail=1
44 # Operand is required
45 realpath >/dev/null && fail=1
46 realpath --relative-base . --relative-to . && fail=1
47 realpath --relative-base . && fail=1
48
49 # -e --relative-* require directories
50 realpath -e --relative-to=dir1/f --relative-base=. . && fail=1
51 realpath -e --relative-to=dir1/  --relative-base=. . || fail=1
52
53 # Note NUL params are unconditionally rejected by canonicalize_filename_mode
54 realpath -m '' && fail=1
55 realpath --relative-base= --relative-to=. . && fail=1
56
57 # symlink resolution
58 this=$(realpath .)
59 test "$(realpath ldir2/..)" = "$this/dir1" || fail=1
60 test "$(realpath -L ldir2/..)" = "$this" || fail=1
61 test "$(realpath -s ldir2)" = "$this/ldir2" || fail=1
62
63 # relative string handling
64 test $(realpath -m --relative-to=prefix prefixed/1) = '../prefixed/1' || fail=1
65 test $(realpath -m --relative-to=prefixed prefix/1) = '../prefix/1' || fail=1
66 test $(realpath -m --relative-to=prefixed prefixed/1) = '1' || fail=1
67
68 # Ensure no redundant trailing '/' present, as was the case in v8.15
69 test $(realpath -sm --relative-to=/usr /) = '..' || fail=1
70 # Ensure no redundant leading '../' present, as was the case in v8.15
71 test $(realpath -sm --relative-to=/ /usr) = 'usr' || fail=1
72
73 # Ensure --relative-base works
74 out=$(realpath -sm --relative-base=/usr --relative-to=/usr /tmp /usr) || fail=1
75 test "$out" = "/tmp$nl." || fail=1
76 out=$(realpath -sm --relative-base=/ --relative-to=/ / /usr) || fail=1
77 test "$out" = ".${nl}usr" || fail=1
78 # --relative-to defaults to the value of --relative-base
79 out=$(realpath -sm --relative-base=/usr /tmp /usr) || fail=1
80 test "$out" = "/tmp$nl." || fail=1
81 out=$(realpath -sm --relative-base=/ / /usr) || fail=1
82 test "$out" = ".${nl}usr" || fail=1
83 # For now, --relative-base must be a prefix of --relative-to, or all output
84 # will be absolute (compare to MacOS 'relpath -d dir start end').
85 out=$(realpath -sm --relative-base=/usr/local --relative-to=/usr \
86     /usr /usr/local) || fail=1
87 test "$out" = "/usr${nl}/usr/local" || fail=1
88
89 # Ensure // is handled correctly.
90 test "$(realpath / // ///)" = "/$nl$double_slash$nl/" || fail=1
91 test "$(realpath one two three)" = "/$nl$double_slash$nl/" || fail=1
92 out=$(realpath -sm --relative-to=/ / // /dev //dev) || fail=1
93 if test $double_slash = //; then
94   test "$out" = ".$nl//${nl}dev$nl//dev" || fail=1
95 else
96   test "$out" = ".$nl.${nl}dev${nl}dev" || fail=1
97 fi
98 out=$(realpath -sm --relative-to=// / // /dev //dev) || fail=1
99 if test $double_slash = //; then
100   test "$out" = "/$nl.$nl/dev${nl}dev" || fail=1
101 else
102   test "$out" = ".$nl.${nl}dev${nl}dev" || fail=1
103 fi
104 out=$(realpath --relative-base=/ --relative-to=// / //) || fail=1
105 if test $double_slash = //; then
106   test "$out" = "/$nl//" || fail=1
107 else
108   test "$out" = ".$nl." || fail=1
109 fi
110
111 Exit $fail