1#!/bin/bash
2#
3# A script for testing imgdiff/applypatch.  It takes two full OTA
4# packages as arguments.  It generates (on the host) patches for all
5# the zip/jar/apk files they have in common, as well as boot and
6# recovery images.  It then applies the patches on the device (or
7# emulator) and checks that the resulting file is correct.
8
9EMULATOR_PORT=5580
10
11# set to 0 to use a device instead
12USE_EMULATOR=0
13
14# where on the device to do all the patching.
15WORK_DIR=/data/local/tmp
16
17START_OTA_PACKAGE=$1
18END_OTA_PACKAGE=$2
19
20# ------------------------
21
22tmpdir=$(mktemp -d)
23
24if [ "$USE_EMULATOR" == 1 ]; then
25  emulator -wipe-data -noaudio -no-window -port $EMULATOR_PORT &
26  pid_emulator=$!
27  ADB="adb -s emulator-$EMULATOR_PORT "
28else
29  ADB="adb -d "
30fi
31
32echo "waiting to connect to device"
33$ADB wait-for-device
34
35# run a command on the device; exit with the exit status of the device
36# command.
37run_command() {
38  $ADB shell "$@" \; echo \$? | awk '{if (b) {print a}; a=$0; b=1} END {exit a}'
39}
40
41testname() {
42  echo
43  echo "$1"...
44  testname="$1"
45}
46
47fail() {
48  echo
49  echo FAIL: $testname
50  echo
51  [ "$open_pid" == "" ] || kill $open_pid
52  [ "$pid_emulator" == "" ] || kill $pid_emulator
53  exit 1
54}
55
56sha1() {
57  sha1sum $1 | awk '{print $1}'
58}
59
60size() {
61  stat -c %s $1 | tr -d '\n'
62}
63
64cleanup() {
65  # not necessary if we're about to kill the emulator, but nice for
66  # running on real devices or already-running emulators.
67  testname "removing test files"
68  run_command rm $WORK_DIR/applypatch
69  run_command rm $WORK_DIR/source
70  run_command rm $WORK_DIR/target
71  run_command rm $WORK_DIR/patch
72
73  [ "$pid_emulator" == "" ] || kill $pid_emulator
74
75  rm -rf $tmpdir
76}
77
78$ADB push $ANDROID_PRODUCT_OUT/system/bin/applypatch $WORK_DIR/applypatch
79
80patch_and_apply() {
81  local fn=$1
82  shift
83
84  unzip -p $START_OTA_PACKAGE $fn > $tmpdir/source
85  unzip -p $END_OTA_PACKAGE $fn > $tmpdir/target
86  imgdiff "$@" $tmpdir/source $tmpdir/target $tmpdir/patch
87  bsdiff $tmpdir/source $tmpdir/target $tmpdir/patch.bs
88  echo "patch for $fn is $(size $tmpdir/patch) [of $(size $tmpdir/target)] ($(size $tmpdir/patch.bs) with bsdiff)"
89  echo "$fn $(size $tmpdir/patch) of $(size $tmpdir/target) bsdiff $(size $tmpdir/patch.bs)" >> /tmp/stats.txt
90  $ADB push $tmpdir/source $WORK_DIR/source || fail "source push failed"
91  run_command rm /data/local/tmp/target
92  $ADB push $tmpdir/patch $WORK_DIR/patch || fail "patch push failed"
93  run_command /data/local/tmp/applypatch /data/local/tmp/source \
94    /data/local/tmp/target $(sha1 $tmpdir/target) $(size $tmpdir/target) \
95    $(sha1 $tmpdir/source):/data/local/tmp/patch \
96    || fail "applypatch of $fn failed"
97  $ADB pull /data/local/tmp/target $tmpdir/result
98  diff -q $tmpdir/target $tmpdir/result || fail "patch output not correct!"
99}
100
101# --------------- basic execution ----------------------
102
103for i in $((zipinfo -1 $START_OTA_PACKAGE; zipinfo -1 $END_OTA_PACKAGE) | \
104           sort | uniq -d | egrep -e '[.](apk|jar|zip)$'); do
105  patch_and_apply $i -z
106done
107patch_and_apply boot.img
108patch_and_apply system/recovery.img
109
110
111# --------------- cleanup ----------------------
112
113cleanup
114
115echo
116echo PASS
117echo
118
119