1# 2# Copyright (c) 2016 Jilles Tjoelker 3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions 7# are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24# SUCH DAMAGE. 25# 26# $FreeBSD$ 27# 28 29copy_to_nonexistent_with_opts() { 30 printf 'test\n123\r456\r\n789\0z' >testf 31 atf_check install "$@" testf copyf 32 cmp testf copyf || atf_fail "bad copy" 33 [ ! testf -nt copyf ] || atf_fail "bad timestamp" 34 [ ! -e copyf.bak ] || atf_fail "no backup expected" 35} 36 37atf_test_case copy_to_nonexistent 38copy_to_nonexistent_body() { 39 copy_to_nonexistent_with_opts 40} 41 42atf_test_case copy_to_nonexistent_safe 43copy_to_nonexistent_safe_body() { 44 copy_to_nonexistent_with_opts -S 45} 46 47atf_test_case copy_to_nonexistent_comparing 48copy_to_nonexistent_comparing_body() { 49 copy_to_nonexistent_with_opts -C 50} 51 52atf_test_case copy_to_nonexistent_safe_comparing 53copy_to_nonexistent_safe_comparing_body() { 54 copy_to_nonexistent_with_opts -S -C 55} 56 57atf_test_case copy_to_nonexistent_backup 58copy_to_nonexistent_backup_body() { 59 copy_to_nonexistent_with_opts -b -B.bak 60} 61 62atf_test_case copy_to_nonexistent_backup_safe 63copy_to_nonexistent_backup_safe_body() { 64 copy_to_nonexistent_with_opts -b -B.bak -S 65} 66 67copy_self_with_opts() { 68 printf 'test\n123\r456\r\n789\0z' >testf 69 printf 'test\n123\r456\r\n789\0z' >testf2 70 atf_check -s not-exit:0 -o empty -e match:. install "$@" testf testf 71 cmp testf testf2 || atf_fail "file changed after self-copy attempt" 72} 73 74atf_test_case copy_self 75copy_self_body() { 76 copy_self_with_opts 77} 78 79atf_test_case copy_self_safe 80copy_self_safe_body() { 81 copy_self_with_opts -S 82} 83 84atf_test_case copy_self_comparing 85copy_self_comparing_body() { 86 copy_self_with_opts -C 87} 88 89atf_test_case copy_self_safe_comparing 90copy_self_safe_comparing_body() { 91 copy_self_with_opts -S -C 92} 93 94overwrite_with_opts() { 95 printf 'test\n123\r456\r\n789\0z' >testf 96 printf 'test\n123\r456\r\n789\0w' >otherf 97 atf_check install "$@" testf otherf 98 cmp testf otherf || atf_fail "bad overwrite" 99 [ ! testf -nt otherf ] || atf_fail "bad timestamp" 100} 101 102atf_test_case overwrite 103overwrite_body() { 104 overwrite_with_opts 105} 106 107atf_test_case overwrite_safe 108overwrite_safe_body() { 109 overwrite_with_opts -S 110} 111 112atf_test_case overwrite_comparing 113overwrite_comparing_body() { 114 overwrite_with_opts -C 115} 116 117atf_test_case overwrite_safe_comparing 118overwrite_safe_comparing_body() { 119 overwrite_with_opts -S -C 120} 121 122overwrite_eq_with_opts() { 123 printf 'test\n123\r456\r\n789\0z' >testf 124 printf 'test\n123\r456\r\n789\0z' >otherf 125 atf_check install "$@" testf otherf 126 cmp testf otherf || atf_fail "bad overwrite" 127 [ ! testf -nt otherf ] || atf_fail "bad timestamp" 128} 129 130atf_test_case overwrite_eq 131overwrite_eq_body() { 132 overwrite_eq_with_opts 133} 134 135atf_test_case overwrite_eq_safe 136overwrite_eq_safe_body() { 137 overwrite_eq_with_opts -S 138} 139 140atf_test_case overwrite_eq_comparing 141overwrite_eq_comparing_body() { 142 overwrite_eq_with_opts -C 143} 144 145atf_test_case overwrite_eq_safe_comparing 146overwrite_eq_safe_comparing_body() { 147 overwrite_eq_with_opts -S -C 148} 149 150overwrite_backup_with_opts() { 151 printf 'test\n123\r456\r\n789\0z' >testf 152 printf 'test\n123\r456\r\n789\0w' >otherf 153 printf 'test\n123\r456\r\n789\0w' >otherf2 154 atf_check install -b -B.bak "$@" testf otherf 155 cmp testf otherf || atf_fail "bad overwrite" 156 [ ! testf -nt otherf ] || atf_fail "bad timestamp" 157 cmp otherf.bak otherf2 || atf_fail "bad backup" 158} 159 160atf_test_case overwrite_backup 161overwrite_backup_body() { 162 overwrite_backup_with_opts 163} 164 165atf_test_case overwrite_backup_safe 166overwrite_backup_safe_body() { 167 overwrite_backup_with_opts -S 168} 169 170atf_test_case overwrite_backup_comparing 171overwrite_backup_comparing_body() { 172 overwrite_backup_with_opts -C 173} 174 175atf_test_case overwrite_backup_safe_comparing 176overwrite_backup_safe_comparing_body() { 177 overwrite_backup_with_opts -S -C 178} 179 180setup_stripbin() { 181 cat <<\STRIPBIN >stripbin 182#!/bin/sh 183tr z @ <"$1" >"$1.new" && mv -- "$1.new" "$1" 184STRIPBIN 185 chmod 755 stripbin 186 export STRIPBIN="$PWD/stripbin" 187} 188 189strip_changing_with_opts() { 190 setup_stripbin 191 printf 'test\n123\r456\r\n789\0z' >testf 192 atf_check install -s "$@" testf copyf 193 [ ! testf -nt copyf ] || atf_fail "bad timestamp" 194 printf 'test\n123\r456\r\n789\0@' >otherf 195 cmp otherf copyf || atf_fail "bad stripped copy" 196} 197 198atf_test_case strip_changing 199strip_changing_body() { 200 strip_changing_with_opts 201} 202 203atf_test_case strip_changing_comparing 204strip_changing_comparing_body() { 205 strip_changing_with_opts -C 206} 207 208strip_changing_overwrite_with_opts() { 209 setup_stripbin 210 printf 'test\n123\r456\r\n789\0z' >testf 211 printf 'test\n123\r456\r\n789\0w' >copyf 212 atf_check install -s "$@" testf copyf 213 [ ! testf -nt copyf ] || atf_fail "bad timestamp" 214 printf 'test\n123\r456\r\n789\0@' >otherf 215 cmp otherf copyf || atf_fail "bad stripped copy" 216} 217 218atf_test_case strip_changing_overwrite 219strip_changing_overwrite_body() { 220 strip_changing_overwrite_with_opts 221} 222 223atf_test_case strip_changing_overwrite_comparing 224strip_changing_overwrite_comparing_body() { 225 strip_changing_overwrite_with_opts -C 226} 227 228strip_changing_overwrite_eq_with_opts() { 229 setup_stripbin 230 printf 'test\n123\r456\r\n789\0z' >testf 231 printf 'test\n123\r456\r\n789\0@' >copyf 232 atf_check install -s "$@" testf copyf 233 [ ! testf -nt copyf ] || atf_fail "bad timestamp" 234 printf 'test\n123\r456\r\n789\0@' >otherf 235 cmp otherf copyf || atf_fail "bad stripped copy" 236} 237 238atf_test_case strip_changing_overwrite_eq 239strip_changing_overwrite_eq_body() { 240 strip_changing_overwrite_eq_with_opts 241} 242 243atf_test_case strip_changing_overwrite_eq_comparing 244strip_changing_overwrite_eq_comparing_body() { 245 strip_changing_overwrite_eq_with_opts -C 246} 247 248atf_test_case strip_noop 249strip_noop_body() { 250 export STRIPBIN=true 251 printf 'test\n123\r456\r\n789\0z' >testf 252 atf_check install -s testf copyf 253 [ ! testf -nt copyf ] || atf_fail "bad timestamp" 254 printf 'test\n123\r456\r\n789\0z' >otherf 255 cmp otherf copyf || atf_fail "bad stripped copy" 256} 257 258atf_test_case hard_link 259hard_link_body() { 260 printf 'test\n123\r456\r\n789\0z' >testf 261 atf_check install -l h testf copyf 262 [ testf -ef copyf ] || atf_fail "not same file" 263 [ ! -L copyf ] || atf_fail "copy is symlink" 264} 265 266atf_test_case symbolic_link 267symbolic_link_body() { 268 printf 'test\n123\r456\r\n789\0z' >testf 269 atf_check install -l s testf copyf 270 [ testf -ef copyf ] || atf_fail "not same file" 271 [ -L copyf ] || atf_fail "copy is not symlink" 272} 273 274atf_test_case symbolic_link_absolute 275symbolic_link_absolute_body() { 276 printf 'test\n123\r456\r\n789\0z' >testf 277 atf_check install -l sa testf copyf 278 [ testf -ef copyf ] || atf_fail "not same file" 279 [ -L copyf ] || atf_fail "copy is not symlink" 280 [ "$(readlink copyf)" = "$(pwd -P)/testf" ] || atf_fail "unexpected symlink contents" 281} 282 283atf_test_case symbolic_link_relative 284symbolic_link_relative_body() { 285 printf 'test\n123\r456\r\n789\0z' >testf 286 atf_check install -l sr testf copyf 287 [ testf -ef copyf ] || atf_fail "not same file" 288 [ -L copyf ] || atf_fail "copy is not symlink" 289 [ "$(readlink copyf)" = "testf" ] || atf_fail "unexpected symlink contents" 290} 291 292atf_test_case mkdir_simple 293mkdir_simple_body() { 294 atf_check install -d dir1/dir2 295 [ -d dir1 ] || atf_fail "dir1 missing" 296 [ -d dir1/dir2 ] || atf_fail "dir2 missing" 297 atf_check install -d dir1/dir2/dir3 298 [ -d dir1/dir2/dir3 ] || atf_fail "dir3 missing" 299 atf_check install -d dir1 300 atf_check install -d dir1/dir2/dir3 301} 302 303atf_init_test_cases() { 304 atf_add_test_case copy_to_nonexistent 305 atf_add_test_case copy_to_nonexistent_safe 306 atf_add_test_case copy_to_nonexistent_comparing 307 atf_add_test_case copy_to_nonexistent_safe_comparing 308 atf_add_test_case copy_to_nonexistent_backup 309 atf_add_test_case copy_to_nonexistent_backup_safe 310 atf_add_test_case copy_self 311 atf_add_test_case copy_self_safe 312 atf_add_test_case copy_self_comparing 313 atf_add_test_case copy_self_safe_comparing 314 atf_add_test_case overwrite 315 atf_add_test_case overwrite_safe 316 atf_add_test_case overwrite_comparing 317 atf_add_test_case overwrite_safe_comparing 318 atf_add_test_case overwrite_eq 319 atf_add_test_case overwrite_eq_safe 320 atf_add_test_case overwrite_eq_comparing 321 atf_add_test_case overwrite_eq_safe_comparing 322 atf_add_test_case overwrite_backup 323 atf_add_test_case overwrite_backup_safe 324 atf_add_test_case overwrite_backup_comparing 325 atf_add_test_case overwrite_backup_safe_comparing 326 atf_add_test_case strip_changing 327 atf_add_test_case strip_changing_comparing 328 atf_add_test_case strip_changing_overwrite 329 atf_add_test_case strip_changing_overwrite_comparing 330 atf_add_test_case strip_changing_overwrite_eq 331 atf_add_test_case strip_changing_overwrite_eq_comparing 332 atf_add_test_case strip_noop 333 atf_add_test_case hard_link 334 atf_add_test_case symbolic_link 335 atf_add_test_case symbolic_link_absolute 336 atf_add_test_case symbolic_link_relative 337 atf_add_test_case mkdir_simple 338} 339