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