1#!/bin/bash 2 3[ -f testing.sh ] && . testing.sh 4 5OLDUMASK=$(umask) 6umask 0002 7 8# Create test file 9dd if=/dev/urandom of=random bs=64 count=1 2> /dev/null 10 11#testing "name" "command" "result" "infile" "stdin" 12 13testing "not enough arguments [fail]" "cp one 2>/dev/null || echo yes" \ 14 "yes\n" "" "" 15testing "-missing source [fail]" "cp missing two 2>/dev/null || echo yes" \ 16 "yes\n" "" "" 17testing "file->file" "cp random two && cmp random two && echo yes" \ 18 "yes\n" "" "" 19rm two 20 21mkdir two 22testing "file->dir" "cp random two && cmp random two/random && echo yes" \ 23 "yes\n" "" "" 24rm two/random 25testing "file->dir/file" \ 26 "cp random two/random && cmp random two/random && echo yes" \ 27 "yes\n" "" "" 28testing "-r dir->missing" \ 29 "cp -r two three && cmp random three/random && echo yes" \ 30 "yes\n" "" "" 31touch walrus 32testing "-r dir->file [fail]" \ 33 "cp -r two walrus 2>/dev/null || echo yes" "yes\n" "" "" 34touch two/three 35testing "-r dir hits file." \ 36 "cp -r three two 2>/dev/null || echo yes" "yes\n" "" "" 37rm -rf two three walrus 38 39touch two 40chmod 000 two 41skipnot [ $(id -u) -ne 0 ] # Root doesn't count. 42testing "file->inaccessible [fail]" \ 43 "cp random two 2>/dev/null || echo yes" "yes\n" "" "" 44rm -f two 45 46touch two 47chmod 000 two 48skipnot [ $(id -u) -ne 0 ] # Root doesn't count. 49testing "-f file->inaccessible" \ 50 "cp -f random two && cmp random two && echo yes" "yes\n" "" "" 51mkdir sub 52chmod 000 sub 53skipnot [ $(id -u) -ne 0 ] # Root doesn't count. 54testing "file->inaccessible_dir [fail]" \ 55 "cp random sub 2>/dev/null || echo yes" "yes\n" "" "" 56rm two 57rmdir sub 58 59# This test fails because our -rf deletes existing target files without 60# regard to what we'd be copying over it. Posix says to only do that if 61# we'd be copying a file over the file, but does not say _why_. 62 63#mkdir dir 64#touch file 65#testing "-rf dir file [fail]" "cp -rf dir file 2>/dev/null || echo yes" \ 66# "yes\n" "" "" 67#rm -rf dir file 68 69touch one two 70testing "file1 file2 missing [fail]" \ 71 "cp one two missing 2>/dev/null || echo yes" "yes\n" "" "" 72mkdir dir 73testing "dir file missing [fail]" \ 74 "cp dir two missing 2>/dev/null || echo yes" "yes\n" "" "" 75testing "-rf dir file missing [fail]" \ 76 "cp dir two missing 2>/dev/null || echo yes" "yes\n" "" "" 77testing "file1 file2 file [fail]" \ 78 "cp random one two 2>/dev/null || echo yes" "yes\n" "" "" 79testing "file1 file2 dir" \ 80 "cp random one dir && cmp random dir/random && cmp one dir/one && echo yes" \ 81 "yes\n" "" "" 82rm one two random 83rm -rf dir 84 85mkdir -p one/two/three/four 86touch one/two/three/five 87touch one/{six,seven,eight} 88testing "-r /abspath dest" \ 89 "cp -r \"$(readlink -f one)\" dir && diff -r one dir && echo yes" \ 90 "yes\n" "" "" 91testing "-r dir again" "cp -r one/. dir && diff -r one dir && echo yes" \ 92 "yes\n" "" "" 93mkdir dir2 94testing "-r dir1/* dir2" \ 95 "cp -r one/* dir2 && diff -r one dir2 && echo yes" "yes\n" "" "" 96rm -rf one dir dir2 97 98mkdir one; touch one/two; cp one/two one/three 99cp -pr one/ one_ # Succeeds twice in a row 100testing "-pr dir/." "cp -pr one/. one_ && echo yes" "yes\n" "" "" 101rm -rf one one_ 102mkdir one; touch one/two; ln -s two one/three 103cp -pr one/ one_ # First time ok, second mustn't fail with "File exists" 104testing "-pr dir/. symlink child" "cp -pr one/. one_ && echo yes" "yes\n" "" "" 105rm -rf one one_ 106 107touch walrus 108chmod 644 walrus 109ln -s walrus woot 110testing "symlink dest permissions" "cp woot carpenter && stat -c %A carpenter" \ 111 "-rw-r--r--\n" "" "" 112testing "duplicated --preserve options" \ 113 "cp --preserve=mode,mode walrus walrus2 2>&1 || echo bad" "" "" "" 114rm -rf walrus woot carpenter 115 116mkdir dir 117echo a > file 118echo b > b 119testing "-T file" "cp -T b file && cat file" "b\n" "" "" 120testing "-T dir" "cp -T b dir 2>/dev/null || echo expected" "expected\n" "" "" 121rm b file 122 123# cp -r ../source destdir 124# cp -r one/two/three missing 125# cp -r one/two/three two 126# cp file1 file2 dir 127# cp file1 missing file2 -> dir 128 129# Make sure it's truncating existing file 130# copy with -d at top level, with -d in directory, without -d at top level, 131# without -d in directory 132 133umask $OLDUMASK 134