1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5# Create test file
6dd if=/dev/urandom of=random bs=64 count=1 2> /dev/null
7
8#testing "name" "command" "result" "infile" "stdin"
9
10testing "not enough arguments [fail]" "cp one 2>/dev/null || echo yes" \
11	"yes\n" "" ""
12testing "-missing source [fail]" "cp missing two 2>/dev/null || echo yes" \
13	"yes\n" "" ""
14testing "file->file" "cp random two && cmp random two && echo yes" \
15	"yes\n" "" ""
16rm two
17
18mkdir two
19testing "file->dir" "cp random two && cmp random two/random && echo yes" \
20	"yes\n" "" ""
21rm two/random
22testing "file->dir/file" \
23	"cp random two/random && cmp random two/random && echo yes" \
24	"yes\n" "" ""
25testing "-r dir->missing" \
26	"cp -r two three && cmp random three/random && echo yes" \
27	"yes\n" "" ""
28touch walrus
29testing "-r dir->file [fail]" \
30	"cp -r two walrus 2>/dev/null || echo yes" "yes\n" "" ""
31touch two/three
32testing "-r dir hits file." \
33	"cp -r three two 2>/dev/null || echo yes" "yes\n" "" ""
34rm -rf two three walrus
35
36touch two
37chmod 000 two
38testing "file->inaccessable [fail]" \
39	"cp random two 2>/dev/null || echo yes" "yes\n" "" ""
40rm -f two
41
42touch two
43chmod 000 two
44testing "-f file->inaccessable" \
45	"cp -f random two && cmp random two && echo yes" "yes\n" "" ""
46mkdir sub
47chmod 000 sub
48testing "file->inaccessable_dir [fail]" \
49	"cp random sub 2>/dev/null || echo yes" "yes\n" "" ""
50rm two
51rmdir sub
52
53# This test fails because our -rf deletes existing target files without
54# regard to what we'd be copying over it. Posix says to only do that if
55# we'd be copying a file over the file, but does not say _why_.
56
57#mkdir dir
58#touch file
59#testing "-rf dir file [fail]" "cp -rf dir file 2>/dev/null || echo yes" \
60#	"yes\n" "" ""
61#rm -rf dir file
62
63touch one two
64testing "file1 file2 missing [fail]" \
65	"cp one two missing 2>/dev/null || echo yes" "yes\n" "" ""
66mkdir dir
67testing "dir file missing [fail]" \
68	"cp dir two missing 2>/dev/null || echo yes" "yes\n" "" ""
69testing "-rf dir file missing [fail]" \
70	"cp dir two missing 2>/dev/null || echo yes" "yes\n" "" ""
71testing "file1 file2 file [fail]" \
72	"cp random one two 2>/dev/null || echo yes" "yes\n" "" ""
73testing "file1 file2 dir" \
74	"cp random one dir && cmp random dir/random && cmp one dir/one && echo yes" \
75	"yes\n" "" ""
76rm one two random
77rm -rf dir
78
79mkdir -p one/two/three/four
80touch one/two/three/five
81touch one/{six,seven,eight}
82testing "-r /abspath dest" \
83	"cp -r \"$(readlink -f one)\" dir && diff -r one dir && echo yes" \
84	"yes\n" "" ""
85testing "-r dir again" "cp -r one/. dir && diff -r one dir && echo yes" \
86	"yes\n" "" ""
87mkdir dir2
88testing "-r dir1/* dir2" \
89	"cp -r one/* dir2 && diff -r one dir2 && echo yes" "yes\n" "" ""
90rm -rf one dir dir2
91
92touch walrus
93chmod 644 walrus
94ln -s walrus woot
95
96testing "symlink dest permissions" "cp woot carpenter && stat -c %A carpenter" \
97  "-rw-r--r--\n" "" ""
98
99# cp -r ../source destdir
100# cp -r one/two/three missing
101# cp -r one/two/three two
102# mkdir one; touch one/two; ln -s two one/three
103# cp file1 file2 dir
104# cp file1 missing file2 -> dir
105
106# Make sure it's truncating existing file
107# copy with -d at top level, with -d in directory, without -d at top level,
108#      without -d in directory
109