aboutsummaryrefslogtreecommitdiff
path: root/tests/cpio.test
blob: 3955800f3122da1bba47a8fb9f42c6074883da0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash

[ -f testing.sh ] && . testing.sh

# We need to test name and file padding.
# This means all possible values of strlen(name)+1 % 4,
# plus file sizes of at least 0-4.

touch a bb ccc dddd
testing "name padding" "cpio -o -H newc|cpio -it" "a\nbb\nccc\ndddd\n" "" "a\nbb\nccc\ndddd\n"
rm a bb ccc dddd

touch a
printf '1' >b
printf '22' >c
printf '333' >d
testing "file padding" "cpio -o -H newc|cpio -it" "a\nb\nc\nd\n" "" "a\nb\nc\nd\n"
rm a b c d

touch a
printf '1' >bb
printf '22' >ccc
printf '333' >dddd
# With the proper padding, header length, and file length, 
# the relevant bit should be here:
# 110*5 + 4*3 + 2 + 6*3 = 550 + 12 + 20 = 582
# files are padded to n*4, names are padded to 2 + n*4 due to the header length
testing "archive length" "cpio -o -H newc|dd ibs=2 skip=291 count=5 2>/dev/null" "TRAILER!!!" "" "a\nbb\nccc\ndddd\n"
testing "archive magic" "cpio -o -H newc|dd ibs=2 count=3 2>/dev/null" "070701" "" "a\n"
# check name length (8 bytes before the empty "crc")
testing "name length" "cpio -o -H newc|dd ibs=2 skip=47 count=4 2>/dev/null" "00000002" "" "a\n"
testing "-t" "cpio -o -H newc|cpio -it" "a\nbb\n" "" "a\nbb"
testing "-t --quiet" "cpio -o -H newc|cpio -it --quiet" "a\nbb\n" "" "a\nbb"
mkdir out
testing "-p" "cpio -p out && find out | sort" "out\nout/a\nout/bb\n" "" "a\nbb"
rm -rf out
testing "-pd" "cpio -pd out && find out | sort" "out\nout/a\nout/bb\n" "" "a\nbb"
rm a bb ccc dddd

# archive dangling symlinks and empty files even if we cannot open them
touch a; chmod a-rwx a; ln -s a/cant b
toyonly testing "archives unreadable empty files" "cpio -o -H newc|cpio -it" "b\na\n" "" "b\na\n"
chmod u+rw a; rm -f a b

mkdir a
echo "old" >a/b
echo "a/b" | cpio -o -H newc >a.cpio
rm -rf a
testing "-i doesn't create leading directories" "cpio -i <a.cpio 2>/dev/null; [ -e a ] || echo yes" "yes\n" "" ""
rm -rf a
testing "-id creates leading directories" "cpio -id <a.cpio && cat a/b" "old\n" "" ""
rm -rf a a.cpio

mkdir a
echo "old" >a/b
find a | cpio -o -H newc >a.cpio
testing "-i keeps existing files" "echo new >a/b && cpio -i <a.cpio 2>/dev/null; cat a/b" "new\n" "" ""
testing "-id keeps existing files" "echo new >a/b && cpio -id <a.cpio 2>/dev/null; cat a/b" "new\n" "" ""
testing "-iu replaces existing files; no error" "echo new >a/b && cpio -iu <a.cpio && cat a/b" "old\n" "" ""
testing "-idu replaces existing files; no error" "echo new >a/b && cpio -idu <a.cpio && cat a/b" "old\n" "" ""
rm -rf a a.cpio

testing "error on empty file" \
  "setsid cpio -i < /dev/null 2>/dev/null || echo err" "err\n" "" ""