1 # $NetBSD: t_magic_symlinks.sh,v 1.4 2023/04/03 21:35:59 gutteridge Exp $ 2 # 3 # Copyright (c) 2020 The NetBSD Foundation, Inc. 4 # All rights reserved. 5 # 6 # This code is derived from software contributed to The NetBSD Foundation 7 # by Jukka Ruohonen. 8 # 9 # Redistribution and use in source and binary forms, with or without 10 # modification, are permitted provided that the following conditions 11 # are met: 12 # 1. Redistributions of source code must retain the above copyright 13 # notice, this list of conditions and the following disclaimer. 14 # 2. Redistributions in binary form must reproduce the above copyright 15 # notice, this list of conditions and the following disclaimer in the 16 # documentation and/or other materials provided with the distribution. 17 # 18 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 # POSSIBILITY OF SUCH DAMAGE. 29 # 30 tmpdir="/tmp/test-magic-symlink" 31 32 init() { 33 34 enabled=$(sysctl vfs.generic.magiclinks | awk '{print $3}') 35 36 if [ $enabled -eq 0 ]; then 37 sysctl -w vfs.generic.magiclinks=1 >/dev/null 2>&1 38 echo "Initialized vfs.generic.magiclinks=1" 39 fi 40 41 mkdir "$tmpdir" 42 echo "$enabled" > "$tmpdir/enabled" 43 } 44 45 clean() { 46 47 enabled=$(cat "$tmpdir/enabled") 48 49 if [ $enabled -eq 0 ]; then 50 sysctl -w vfs.generic.magiclinks=$enabled >/dev/null 2>&1 51 echo "Restored vfs.generic.magiclinks=$enabled" 52 fi 53 54 rm -rf $tmpdir 55 } 56 57 check() { 58 59 init 60 cd "$tmpdir" 61 mkdir "$1" 62 echo 1 > "$1/magic" 63 ln -s "$2" "link" 64 cd "link" 65 66 if [ -z $(pwd | grep "$1") ]; then 67 atf_fail "kernel does not handle magic symlinks properly" 68 fi 69 70 if [ ! $(cat "magic") -eq 1 ]; then 71 atf_fail "kernel does not handle magic symlinks properly" 72 fi 73 } 74 75 # @domainname 76 # 77 atf_test_case domainname cleanup 78 domainname_head() { 79 atf_set "require.user" "root" 80 atf_set "descr" "Check that @domainname magic symlinks work" 81 } 82 83 domainname_body() { 84 check "$(domainname)" "@domainname" 85 } 86 87 domainname_cleanup() { 88 clean 89 } 90 91 # @hostname 92 # 93 atf_test_case hostname cleanup 94 hostname_head() { 95 atf_set "require.user" "root" 96 atf_set "descr" "Check that @hostname magic symlinks work" 97 } 98 99 hostname_body() { 100 check "$(hostname)" "@hostname" 101 } 102 103 hostname_cleanup() { 104 clean 105 } 106 107 # @machine 108 # 109 atf_test_case machine cleanup 110 machine_head() { 111 atf_set "require.user" "root" 112 atf_set "descr" "Check that @machine magic symlinks work" 113 } 114 115 machine_body() { 116 check "$(uname -m)" "@machine" 117 } 118 119 machine_cleanup() { 120 clean 121 } 122 123 # @machine_arch 124 # 125 atf_test_case machine_arch cleanup 126 machine_arch_head() { 127 atf_set "require.user" "root" 128 atf_set "descr" "Check that @machine_arch magic symlinks work" 129 } 130 131 machine_arch_body() { 132 check "$(uname -p)" "@machine_arch" 133 } 134 135 machine_arch_cleanup() { 136 clean 137 } 138 139 # @ostype 140 # 141 atf_test_case ostype cleanup 142 ostype_head() { 143 atf_set "require.user" "root" 144 atf_set "descr" "Check that @ostype magic symlinks work" 145 } 146 147 ostype_body() { 148 check "$(uname -s)" "@ostype" 149 } 150 151 ostype_cleanup() { 152 clean 153 } 154 155 # @ruid 156 # 157 atf_test_case ruid cleanup 158 ruid_head() { 159 atf_set "require.user" "root" 160 atf_set "descr" "Check that @ruid magic symlinks work" 161 } 162 163 ruid_body() { 164 check "$(id -ru)" "@ruid" 165 } 166 167 ruid_cleanup() { 168 clean 169 } 170 171 # @uid 172 # 173 atf_test_case uid cleanup 174 uid_head() { 175 atf_set "require.user" "root" 176 atf_set "descr" "Check that @uid magic symlinks work" 177 } 178 179 uid_body() { 180 check "$(id -u)" "@uid" 181 } 182 183 uid_cleanup() { 184 clean 185 } 186 187 # @rgid 188 # 189 atf_test_case rgid cleanup 190 rgid_head() { 191 atf_set "require.user" "root" 192 atf_set "descr" "Check that @rgid magic symlinks work" 193 } 194 195 rgid_body() { 196 check "$(id -rg)" "@rgid" 197 } 198 199 rgid_cleanup() { 200 clean 201 } 202 203 # @gid 204 # 205 atf_test_case gid cleanup 206 gid_head() { 207 atf_set "require.user" "root" 208 atf_set "descr" "Check that @gid magic symlinks work" 209 } 210 211 gid_body() { 212 check "$(id -g)" "@gid" 213 } 214 215 gid_cleanup() { 216 clean 217 } 218 219 # realpath(1) 220 # 221 atf_test_case realpath cleanup 222 realpath_head() { 223 atf_set "require.user" "root" 224 atf_set "descr" "Check that realpath(1) agrees with the " \ 225 "kernel on magic symlink(7)'s (PR lib/55361)" 226 } 227 228 realpath_body() { 229 230 check "$(uname -r)" "@osrelease" 231 realpath "$tmpdir/link" 232 233 if [ ! $? -eq 0 ]; then 234 atf_expect_fail "PR lib/55361" 235 atf_fail "realpath does not handle magic symlinks properly" 236 fi 237 } 238 239 realpath_cleanup() { 240 clean 241 } 242 243 atf_init_test_cases() { 244 atf_add_test_case domainname 245 atf_add_test_case hostname 246 atf_add_test_case machine 247 atf_add_test_case machine_arch 248 atf_add_test_case ostype 249 atf_add_test_case ruid 250 atf_add_test_case uid 251 atf_add_test_case rgid 252 atf_add_test_case gid 253 atf_add_test_case realpath 254 } 255