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