toybox/tests/cp.test
<<
>>
Prefs
   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 one/{six,seven,eight}
  87testing "-r /abspath dest" \
  88        "cp -r \"$(readlink -f one)\" dir && diff -r one dir && echo yes" \
  89        "yes\n" "" ""
  90testing "-r dir again" "cp -r one/. dir && diff -r one dir && echo yes" \
  91        "yes\n" "" ""
  92mkdir dir2
  93testing "-r dir1/* dir2" \
  94        "cp -r one/* dir2 && diff -r one dir2 && echo yes" "yes\n" "" ""
  95rm -rf one dir dir2
  96
  97mkdir one; touch one/two; cp one/two one/three
  98cp -pr one/ one_ # Succeeds twice in a row
  99testing "-pr dir/." "cp -pr one/. one_ && echo yes" "yes\n" "" ""
 100rm -rf one one_
 101mkdir one; touch one/two; ln -s two one/three
 102cp -pr one/ one_ # First time ok, second mustn't fail with "File exists"
 103testing "-pr dir/. symlink child" "cp -pr one/. one_ && echo yes" "yes\n" "" ""
 104rm -rf one one_
 105
 106touch walrus
 107chmod 644 walrus
 108ln -s walrus woot
 109testing "symlink dest permissions" "cp woot carpenter && stat -c %A carpenter" \
 110  "-rw-r--r--\n" "" ""
 111testing "duplicated --preserve options" \
 112  "cp --preserve=mode,mode walrus walrus2 2>&1 || echo bad" "" "" ""
 113rm -rf walrus woot carpenter
 114
 115mkdir dir
 116echo a > file
 117echo b > b
 118testing "-T file" "cp -T b file && cat file" "b\n" "" ""
 119testing "-T dir" "cp -T b dir 2>/dev/null || echo expected" "expected\n" "" ""
 120rm b file
 121
 122mkdir -p b/c/d/ a/
 123echo a > b/c/d/file
 124testing "--parents b/c/d/file a/" "cp --parents b/c/d/file a/ && cat a/b/c/d/file" "a\n" "" ""
 125rm -rf a/ b/
 126
 127echo a > file
 128testing "-P file" "cp -P file fdst && stat -c %F fdst" "regular file\n" "" ""
 129ln -s file lnk
 130testing "-P symlink" "cp -P lnk ldst && stat -c %F ldst" "symbolic link\n" "" ""
 131testing "follow symlink" "cp lnk ldst2 && stat -c %F ldst2" "regular file\n" "" ""
 132rm file fdst lnk ldst ldst2
 133
 134mkdir sub
 135testing "-t one arg" 'cp -t sub/ input && cat sub/input' 'yes\n' 'yes\n' ''
 136toyonly testing "-Dt" 'cp -Dt sub2 input && cat sub2/input' 'and\n' 'and\n' ''
 137rm -rf sub sub2
 138
 139testing '-u1' 'echo one>one; sleep .1; echo two>two; cp -u one two; cat two' \
 140  'two\n' '' ''
 141testing '-u2' 'echo two>two; sleep .1; echo one>one; cp -u one two; cat two' \
 142  'one\n' '' ''
 143mkdir a b
 144
 145echo potato > a/one
 146echo potato > a/two
 147touch b/one b/two
 148testing '-i' 'cp -ri a/. b/. 2>/dev/null; cmp -s a/one b/one || cmp -s a/one b/two && echo yes' \
 149  'yes\n' '' 'n\ny\n'
 150rm -rf one two a b
 151
 152# cp -r ../source destdir
 153# cp -r one/two/three missing
 154# cp -r one/two/three two
 155# cp file1 file2 dir
 156# cp file1 missing file2 -> dir
 157
 158# Make sure it's truncating existing file
 159# copy with -d at top level, with -d in directory, without -d at top level,
 160#      without -d in directory
 161
 162umask $OLDUMASK
 163