xref: /aosp_15_r20/external/toybox/tests/cpio.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1*cf5a6c84SAndroid Build Coastguard Worker#!/bin/bash
2*cf5a6c84SAndroid Build Coastguard Worker
3*cf5a6c84SAndroid Build Coastguard Worker[ -f testing.sh ] && . testing.sh
4*cf5a6c84SAndroid Build Coastguard Worker
5*cf5a6c84SAndroid Build Coastguard Worker# We need to test name and file padding.
6*cf5a6c84SAndroid Build Coastguard Worker# This means all possible values of strlen(name)+1 % 4,
7*cf5a6c84SAndroid Build Coastguard Worker# plus file sizes of at least 0-4.
8*cf5a6c84SAndroid Build Coastguard Worker
9*cf5a6c84SAndroid Build Coastguard WorkerCPIO="cpio${TEST_HOST:+ --quiet}"
10*cf5a6c84SAndroid Build Coastguard WorkerCPI_O="$CPIO -o -H newc"
11*cf5a6c84SAndroid Build Coastguard Worker
12*cf5a6c84SAndroid Build Coastguard Workertouch a bb ccc dddd
13*cf5a6c84SAndroid Build Coastguard Workertesting "name padding" "$CPI_O|$CPIO -it" "a\nbb\nccc\ndddd\n" \
14*cf5a6c84SAndroid Build Coastguard Worker  "" "a\nbb\nccc\ndddd\n"
15*cf5a6c84SAndroid Build Coastguard Workerrm a bb ccc dddd
16*cf5a6c84SAndroid Build Coastguard Worker
17*cf5a6c84SAndroid Build Coastguard Workertouch a
18*cf5a6c84SAndroid Build Coastguard Workerprintf '1' >b
19*cf5a6c84SAndroid Build Coastguard Workerprintf '22' >c
20*cf5a6c84SAndroid Build Coastguard Workerprintf '333' >d
21*cf5a6c84SAndroid Build Coastguard Workertesting "file padding" "$CPI_O|$CPIO -it" "a\nb\nc\nd\n" "" \
22*cf5a6c84SAndroid Build Coastguard Worker  "a\nb\nc\nd\n"
23*cf5a6c84SAndroid Build Coastguard Workerrm a b c d
24*cf5a6c84SAndroid Build Coastguard Worker
25*cf5a6c84SAndroid Build Coastguard Workertouch a
26*cf5a6c84SAndroid Build Coastguard Workerprintf '1' >bb
27*cf5a6c84SAndroid Build Coastguard Workerprintf '22' >ccc
28*cf5a6c84SAndroid Build Coastguard Workerprintf '333' >dddd
29*cf5a6c84SAndroid Build Coastguard Worker# With the proper padding, header length, and file length,
30*cf5a6c84SAndroid Build Coastguard Worker# the relevant bit should be here:
31*cf5a6c84SAndroid Build Coastguard Worker# 110*5 + 4*3 + 2 + 6*3 = 550 + 12 + 20 = 582
32*cf5a6c84SAndroid Build Coastguard Worker# files are padded to n*4, names are padded to 2 + n*4 due to the header length
33*cf5a6c84SAndroid Build Coastguard Workertesting "archive length" "$CPI_O|dd ibs=2 skip=291 count=5 2>/dev/null" "TRAILER!!!" "" "a\nbb\nccc\ndddd\n"
34*cf5a6c84SAndroid Build Coastguard Workertesting "archive magic" "$CPI_O|dd ibs=2 count=3 2>/dev/null" "070701" "" "a\n"
35*cf5a6c84SAndroid Build Coastguard Worker# check name length (8 bytes before the empty "crc")
36*cf5a6c84SAndroid Build Coastguard Workertesting "name length" "$CPI_O|dd ibs=2 skip=47 count=4 2>/dev/null" "00000002" "" "a\n"
37*cf5a6c84SAndroid Build Coastguard Workertesting "-t" "$CPI_O|$CPIO -it" "a\nbb\n" "" "a\nbb"
38*cf5a6c84SAndroid Build Coastguard Worker# Only actually tests anything on toybox. :)
39*cf5a6c84SAndroid Build Coastguard Workertesting "-t --quiet" "$CPI_O|$CPIO -it --quiet" "a\nbb\n" "" "a\nbb"
40*cf5a6c84SAndroid Build Coastguard Workermkdir out
41*cf5a6c84SAndroid Build Coastguard Workertesting "-p" "$CPIO -p out && find out | sort" "out\nout/a\nout/bb\n" "" "a\nbb"
42*cf5a6c84SAndroid Build Coastguard Workerrm -rf out
43*cf5a6c84SAndroid Build Coastguard Workertesting "-pd" "$CPIO -pd out && find out | sort" "out\nout/a\nout/bb\n" "" "a\nbb"
44*cf5a6c84SAndroid Build Coastguard Workerrm a bb ccc dddd
45*cf5a6c84SAndroid Build Coastguard Worker
46*cf5a6c84SAndroid Build Coastguard Worker# archive dangling symlinks and empty files even if we cannot open them
47*cf5a6c84SAndroid Build Coastguard Workertouch a; chmod a-rwx a; ln -s a/cant b
48*cf5a6c84SAndroid Build Coastguard Workertoyonly testing "archives unreadable empty files" "$CPI_O|$CPIO -it" "b\na\n" "" "b\na\n"
49*cf5a6c84SAndroid Build Coastguard Workerchmod u+rw a; rm -f a b
50*cf5a6c84SAndroid Build Coastguard Worker
51*cf5a6c84SAndroid Build Coastguard Workermkdir a
52*cf5a6c84SAndroid Build Coastguard Workerecho "old" >a/b
53*cf5a6c84SAndroid Build Coastguard Workerecho "a/b" | $CPI_O >a.cpio
54*cf5a6c84SAndroid Build Coastguard Workertesting "directory exists is not an error" \
55*cf5a6c84SAndroid Build Coastguard Worker  "$CPI_O | { $CPIO -i 2>&1 || echo bad; }" "" "" "a\n"
56*cf5a6c84SAndroid Build Coastguard Workerrm -rf a
57*cf5a6c84SAndroid Build Coastguard Workertesting "-i doesn't create leading directories" \
58*cf5a6c84SAndroid Build Coastguard Worker  "$CPIO -i <a.cpio 2>/dev/null; [ -e a ] || echo yes" "yes\n" "" ""
59*cf5a6c84SAndroid Build Coastguard Workerrm -rf a
60*cf5a6c84SAndroid Build Coastguard Workertesting "-id creates leading directories" "$CPIO -id <a.cpio && cat a/b" \
61*cf5a6c84SAndroid Build Coastguard Worker  "old\n" "" ""
62*cf5a6c84SAndroid Build Coastguard Workerrm -rf a a.cpio
63*cf5a6c84SAndroid Build Coastguard Worker
64*cf5a6c84SAndroid Build Coastguard Workermkdir a
65*cf5a6c84SAndroid Build Coastguard Workerecho "old" >a/b
66*cf5a6c84SAndroid Build Coastguard Workerfind a | $CPI_O >a.cpio
67*cf5a6c84SAndroid Build Coastguard Workertesting "-i keeps existing files" "echo new >a/b && $CPIO -i <a.cpio 2>/dev/null; cat a/b" "new\n" "" ""
68*cf5a6c84SAndroid Build Coastguard Workertesting "-id keeps existing files" "echo new >a/b && $CPIO -id <a.cpio 2>/dev/null; cat a/b" "new\n" "" ""
69*cf5a6c84SAndroid Build Coastguard Workertesting "-iu replaces existing files; no error" "echo new >a/b && $CPIO -iu <a.cpio && cat a/b" "old\n" "" ""
70*cf5a6c84SAndroid Build Coastguard Workertesting "-idu replaces existing files; no error" "echo new >a/b && $CPIO -idu <a.cpio && cat a/b" "old\n" "" ""
71*cf5a6c84SAndroid Build Coastguard Worker# The kernel's initramfs extractor does this
72*cf5a6c84SAndroid Build Coastguard Workertoyonly testing "skip NUL" "for i in a b; do dd if=/dev/zero bs=512 count=1 2>/dev/null; cat a.cpio; done | $CPIO -t -H newc" \
73*cf5a6c84SAndroid Build Coastguard Worker  "a\na/b\na\na/b\n" "" ""
74*cf5a6c84SAndroid Build Coastguard Workerrm -rf a a.cpio
75*cf5a6c84SAndroid Build Coastguard Worker
76*cf5a6c84SAndroid Build Coastguard Workertesting "error on empty file" "$CPIO -i 2>/dev/null || echo err" "err\n" "" ""
77*cf5a6c84SAndroid Build Coastguard Worker
78*cf5a6c84SAndroid Build Coastguard Workermkdir a
79*cf5a6c84SAndroid Build Coastguard Workertouch a/file
80*cf5a6c84SAndroid Build Coastguard Workerln -s a/symlink a/symlink
81*cf5a6c84SAndroid Build Coastguard Workermkdir a/dir
82*cf5a6c84SAndroid Build Coastguard Workerfind a | $CPI_O >a.cpio
83*cf5a6c84SAndroid Build Coastguard Workerif [ "$(id -u)" -eq 0 ]; then
84*cf5a6c84SAndroid Build Coastguard Worker  # We chown between user "root" and the last user in /etc/passwd,
85*cf5a6c84SAndroid Build Coastguard Worker  # and group "root" and the last group in /etc/group.
86*cf5a6c84SAndroid Build Coastguard Worker  USR="$(sed -n '$s/:.*//p' /etc/passwd)"
87*cf5a6c84SAndroid Build Coastguard Worker  GRP="$(sed -n '$s/:.*//p' /etc/group)"
88*cf5a6c84SAndroid Build Coastguard Worker  # Or if that fails, we assume we're on Android...
89*cf5a6c84SAndroid Build Coastguard Worker  : "${USR:=shell}"
90*cf5a6c84SAndroid Build Coastguard Worker  : "${GRP:=shell}"
91*cf5a6c84SAndroid Build Coastguard Worker  chown -h "${USR}:${GRP}" a/file a/symlink a/dir
92*cf5a6c84SAndroid Build Coastguard Workerfi
93*cf5a6c84SAndroid Build Coastguard Workerskipnot [ $(id -u) -eq 0 ]
94*cf5a6c84SAndroid Build Coastguard Workertesting "-t preserve ownership" "$CPIO -t <a.cpio >/dev/null && stat -c '%U:%G' a/file a/symlink a/dir" "${USR}:${GRP}\n${USR}:${GRP}\n${USR}:${GRP}\n" "" ""
95*cf5a6c84SAndroid Build Coastguard Workerrm -rf a a.cpio
96*cf5a6c84SAndroid Build Coastguard Worker
97*cf5a6c84SAndroid Build Coastguard Workerecho payload > one
98*cf5a6c84SAndroid Build Coastguard Workerln -s one two
99*cf5a6c84SAndroid Build Coastguard Workertesting '-L' "$CPI_O -L | grep -ao payload" 'payload\n' '' 'two\n'
100