1#!/bin/sh 2# Validate realpath operation 3 4# Copyright (C) 2011-2023 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 <https://www.gnu.org/licenses/>. 18 19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src 20print_ver_ realpath 21 22stat_single=$(stat -c %d:%i /) || framework_failure_ 23stat_double=$(stat -c %d:%i //) || framework_failure_ 24double_slash=// 25if test x"$stat_single" = x"$stat_double"; then 26 double_slash=/ 27fi 28nl=' 29' 30 31test -d /dev || framework_failure_ 32 33# Setup dir, file, symlink structure 34 35mkdir -p dir1/dir2 || framework_failure_ 36ln -s dir1/dir2 ldir2 || framework_failure_ 37touch dir1/f dir1/dir2/f || framework_failure_ 38ln -s / one || framework_failure_ 39ln -s // two || framework_failure_ 40ln -s /// three || framework_failure_ 41 42# Basic operation 43realpath -Pqz . >/dev/null || fail=1 44# Operand is required 45returns_ 1 realpath >/dev/null || fail=1 46returns_ 1 realpath --relative-base . --relative-to . || fail=1 47returns_ 1 realpath --relative-base . || fail=1 48 49# -e --relative-* require directories 50returns_ 1 realpath -e --relative-to=dir1/f --relative-base=. . || fail=1 51realpath -e --relative-to=dir1/ --relative-base=. . || fail=1 52 53# Note NUL params are unconditionally rejected by canonicalize_filename_mode 54returns_ 1 realpath -m '' || fail=1 55returns_ 1 realpath --relative-base= --relative-to=. . || fail=1 56 57# symlink resolution 58this=$(realpath .) 59test "$(realpath ldir2/..)" = "$this/dir1" || fail=1 60test "$(realpath -L ldir2/..)" = "$this" || fail=1 61test "$(realpath -s ldir2)" = "$this/ldir2" || fail=1 62 63# relative string handling 64test $(realpath -m --relative-to=prefix prefixed/1) = '../prefixed/1' || fail=1 65test $(realpath -m --relative-to=prefixed prefix/1) = '../prefix/1' || fail=1 66test $(realpath -m --relative-to=prefixed prefixed/1) = '1' || fail=1 67 68# Ensure no redundant trailing '/' present, as was the case in v8.15 69test $(realpath -sm --relative-to=/usr /) = '..' || fail=1 70# Ensure no redundant leading '../' present, as was the case in v8.15 71test $(realpath -sm --relative-to=/ /usr) = 'usr' || fail=1 72 73# Ensure --relative-base works 74out=$(realpath -sm --relative-base=/usr --relative-to=/usr /tmp /usr) || fail=1 75test "$out" = "/tmp$nl." || fail=1 76out=$(realpath -sm --relative-base=/ --relative-to=/ / /usr) || fail=1 77test "$out" = ".${nl}usr" || fail=1 78# --relative-to defaults to the value of --relative-base 79out=$(realpath -sm --relative-base=/usr /tmp /usr) || fail=1 80test "$out" = "/tmp$nl." || fail=1 81out=$(realpath -sm --relative-base=/ / /usr) || fail=1 82test "$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'). 85out=$(realpath -sm --relative-base=/usr/local --relative-to=/usr \ 86 /usr /usr/local) || fail=1 87test "$out" = "/usr${nl}/usr/local" || fail=1 88 89# Ensure // is handled correctly. 90test "$(realpath / // ///)" = "/$nl$double_slash$nl/" || fail=1 91test "$(realpath one two three)" = "/$nl$double_slash$nl/" || fail=1 92out=$(realpath -sm --relative-to=/ / // /dev //dev) || fail=1 93if test $double_slash = //; then 94 test "$out" = ".$nl//${nl}dev$nl//dev" || fail=1 95else 96 test "$out" = ".$nl.${nl}dev${nl}dev" || fail=1 97fi 98out=$(realpath -sm --relative-to=// / // /dev //dev) || fail=1 99if test $double_slash = //; then 100 test "$out" = "/$nl.$nl/dev${nl}dev" || fail=1 101else 102 test "$out" = ".$nl.${nl}dev${nl}dev" || fail=1 103fi 104out=$(realpath --relative-base=/ --relative-to=// / //) || fail=1 105if test $double_slash = //; then 106 test "$out" = "/$nl//" || fail=1 107else 108 test "$out" = ".$nl." || fail=1 109fi 110 111Exit $fail 112