Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # A test suite for applypatch. Run in a client where you have done |
| 4 | # envsetup, choosecombo, etc. |
| 5 | # |
| 6 | # DO NOT RUN THIS ON A DEVICE YOU CARE ABOUT. It will mess up your |
| 7 | # system partition. |
| 8 | # |
| 9 | # |
| 10 | # TODO: find some way to get this run regularly along with the rest of |
| 11 | # the tests. |
| 12 | |
| 13 | EMULATOR_PORT=5580 |
| 14 | DATA_DIR=$ANDROID_BUILD_TOP/build/tools/applypatch/testdata |
| 15 | |
| 16 | # This must be the filename that applypatch uses for its copies. |
| 17 | CACHE_TEMP_SOURCE=/cache/saved.file |
| 18 | |
| 19 | # Put all binaries and files here. We use /cache because it's a |
| 20 | # temporary filesystem in the emulator; it's created fresh each time |
| 21 | # the emulator starts. |
| 22 | WORK_DIR=/system |
| 23 | |
| 24 | # partition that WORK_DIR is located on, without the leading slash |
| 25 | WORK_FS=system |
| 26 | |
| 27 | # set to 0 to use a device instead |
| 28 | USE_EMULATOR=1 |
| 29 | |
| 30 | # ------------------------ |
| 31 | |
| 32 | tmpdir=$(mktemp -d) |
| 33 | |
| 34 | if [ "$USE_EMULATOR" == 1 ]; then |
| 35 | emulator -wipe-data -noaudio -no-window -port $EMULATOR_PORT & |
| 36 | pid_emulator=$! |
| 37 | ADB="adb -s emulator-$EMULATOR_PORT " |
| 38 | else |
| 39 | ADB="adb -d " |
| 40 | fi |
| 41 | |
| 42 | echo "waiting to connect to device" |
| 43 | $ADB wait-for-device |
| 44 | echo "device is available" |
| 45 | $ADB remount |
| 46 | # free up enough space on the system partition for the test to run. |
| 47 | $ADB shell rm -r /system/media |
| 48 | |
| 49 | # run a command on the device; exit with the exit status of the device |
| 50 | # command. |
| 51 | run_command() { |
| 52 | $ADB shell "$@" \; echo \$? | awk '{if (b) {print a}; a=$0; b=1} END {exit a}' |
| 53 | } |
| 54 | |
| 55 | testname() { |
| 56 | echo |
| 57 | echo "$1"... |
| 58 | testname="$1" |
| 59 | } |
| 60 | |
| 61 | fail() { |
| 62 | echo |
| 63 | echo FAIL: $testname |
| 64 | echo |
| 65 | [ "$open_pid" == "" ] || kill $open_pid |
| 66 | [ "$pid_emulator" == "" ] || kill $pid_emulator |
| 67 | exit 1 |
| 68 | } |
| 69 | |
| 70 | sha1() { |
| 71 | sha1sum $1 | awk '{print $1}' |
| 72 | } |
| 73 | |
| 74 | free_space() { |
| 75 | run_command df | awk "/$1/ {print gensub(/K/, \"\", \"g\", \$6)}" |
| 76 | } |
| 77 | |
| 78 | cleanup() { |
| 79 | # not necessary if we're about to kill the emulator, but nice for |
| 80 | # running on real devices or already-running emulators. |
| 81 | testname "removing test files" |
| 82 | run_command rm $WORK_DIR/bloat.dat |
| 83 | run_command rm $WORK_DIR/old.file |
| 84 | run_command rm $WORK_DIR/patch.bsdiff |
| 85 | run_command rm $WORK_DIR/applypatch |
| 86 | run_command rm $CACHE_TEMP_SOURCE |
| 87 | run_command rm /cache/bloat*.dat |
| 88 | |
| 89 | [ "$pid_emulator" == "" ] || kill $pid_emulator |
| 90 | |
| 91 | rm -rf $tmpdir |
| 92 | } |
| 93 | |
| 94 | cleanup |
| 95 | |
| 96 | $ADB push $ANDROID_PRODUCT_OUT/system/bin/applypatch $WORK_DIR/applypatch |
| 97 | |
| 98 | BAD1_SHA1=$(printf "%040x" $RANDOM) |
| 99 | BAD2_SHA1=$(printf "%040x" $RANDOM) |
| 100 | OLD_SHA1=$(sha1 $DATA_DIR/old.file) |
| 101 | NEW_SHA1=$(sha1 $DATA_DIR/new.file) |
| 102 | NEW_SIZE=$(stat -c %s $DATA_DIR/new.file) |
| 103 | |
| 104 | # --------------- basic execution ---------------------- |
| 105 | |
| 106 | testname "usage message" |
| 107 | run_command $WORK_DIR/applypatch && fail |
| 108 | |
| 109 | testname "display license" |
| 110 | run_command $WORK_DIR/applypatch -l | grep -q -i copyright || fail |
| 111 | |
| 112 | |
| 113 | # --------------- check mode ---------------------- |
| 114 | |
| 115 | $ADB push $DATA_DIR/old.file $WORK_DIR |
| 116 | |
| 117 | testname "check mode single" |
| 118 | run_command $WORK_DIR/applypatch -c $WORK_DIR/old.file $OLD_SHA1 || fail |
| 119 | |
| 120 | testname "check mode multiple" |
| 121 | run_command $WORK_DIR/applypatch -c $WORK_DIR/old.file $BAD1_SHA1 $OLD_SHA1 $BAD2_SHA1|| fail |
| 122 | |
| 123 | testname "check mode failure" |
| 124 | run_command $WORK_DIR/applypatch -c $WORK_DIR/old.file $BAD2_SHA1 $BAD1_SHA1 && fail |
| 125 | |
| 126 | $ADB push $DATA_DIR/old.file $CACHE_TEMP_SOURCE |
| 127 | # put some junk in the old file |
| 128 | run_command dd if=/dev/urandom of=$WORK_DIR/old.file count=100 bs=1024 || fail |
| 129 | |
| 130 | testname "check mode cache (corrupted) single" |
| 131 | run_command $WORK_DIR/applypatch -c $WORK_DIR/old.file $OLD_SHA1 || fail |
| 132 | |
| 133 | testname "check mode cache (corrupted) multiple" |
| 134 | run_command $WORK_DIR/applypatch -c $WORK_DIR/old.file $BAD1_SHA1 $OLD_SHA1 $BAD2_SHA1|| fail |
| 135 | |
| 136 | testname "check mode cache (corrupted) failure" |
| 137 | run_command $WORK_DIR/applypatch -c $WORK_DIR/old.file $BAD2_SHA1 $BAD1_SHA1 && fail |
| 138 | |
| 139 | # remove the old file entirely |
| 140 | run_command rm $WORK_DIR/old.file |
| 141 | |
| 142 | testname "check mode cache (missing) single" |
| 143 | run_command $WORK_DIR/applypatch -c $WORK_DIR/old.file $OLD_SHA1 || fail |
| 144 | |
| 145 | testname "check mode cache (missing) multiple" |
| 146 | run_command $WORK_DIR/applypatch -c $WORK_DIR/old.file $BAD1_SHA1 $OLD_SHA1 $BAD2_SHA1|| fail |
| 147 | |
| 148 | testname "check mode cache (missing) failure" |
| 149 | run_command $WORK_DIR/applypatch -c $WORK_DIR/old.file $BAD2_SHA1 $BAD1_SHA1 && fail |
| 150 | |
| 151 | |
| 152 | # --------------- apply patch ---------------------- |
| 153 | |
| 154 | $ADB push $DATA_DIR/old.file $WORK_DIR |
| 155 | $ADB push $DATA_DIR/patch.bsdiff $WORK_DIR |
| 156 | |
| 157 | # Check that the partition has enough space to apply the patch without |
| 158 | # copying. If it doesn't, we'll be testing the low-space condition |
| 159 | # when we intend to test the not-low-space condition. |
| 160 | testname "apply patches (with enough space)" |
| 161 | free_kb=$(free_space $WORK_FS) |
| 162 | echo "${free_kb}kb free on /$WORK_FS." |
| 163 | if (( free_kb * 1024 < NEW_SIZE * 3 / 2 )); then |
| 164 | echo "Not enough space on /$WORK_FS to patch test file." |
| 165 | echo |
| 166 | echo "This doesn't mean that applypatch is necessarily broken;" |
| 167 | echo "just that /$WORK_FS doesn't have enough free space to" |
| 168 | echo "properly run this test." |
| 169 | exit 1 |
| 170 | fi |
| 171 | |
| 172 | testname "apply bsdiff patch" |
| 173 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file - $NEW_SHA1 $NEW_SIZE $BAD1_SHA1:$WORK_DIR/foo $OLD_SHA1:$WORK_DIR/patch.bsdiff || fail |
| 174 | $ADB pull $WORK_DIR/old.file $tmpdir/patched |
| 175 | diff -q $DATA_DIR/new.file $tmpdir/patched || fail |
| 176 | |
| 177 | testname "reapply bsdiff patch" |
| 178 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file - $NEW_SHA1 $NEW_SIZE $BAD1_SHA1:$WORK_DIR/foo $OLD_SHA1:$WORK_DIR/patch.bsdiff || fail |
| 179 | $ADB pull $WORK_DIR/old.file $tmpdir/patched |
| 180 | diff -q $DATA_DIR/new.file $tmpdir/patched || fail |
| 181 | |
| 182 | |
| 183 | # --------------- apply patch in new location ---------------------- |
| 184 | |
| 185 | $ADB push $DATA_DIR/old.file $WORK_DIR |
| 186 | $ADB push $DATA_DIR/patch.bsdiff $WORK_DIR |
| 187 | |
| 188 | # Check that the partition has enough space to apply the patch without |
| 189 | # copying. If it doesn't, we'll be testing the low-space condition |
| 190 | # when we intend to test the not-low-space condition. |
| 191 | testname "apply patch to new location (with enough space)" |
| 192 | free_kb=$(free_space $WORK_FS) |
| 193 | echo "${free_kb}kb free on /$WORK_FS." |
| 194 | if (( free_kb * 1024 < NEW_SIZE * 3 / 2 )); then |
| 195 | echo "Not enough space on /$WORK_FS to patch test file." |
| 196 | echo |
| 197 | echo "This doesn't mean that applypatch is necessarily broken;" |
| 198 | echo "just that /$WORK_FS doesn't have enough free space to" |
| 199 | echo "properly run this test." |
| 200 | exit 1 |
| 201 | fi |
| 202 | |
| 203 | run_command rm $WORK_DIR/new.file |
| 204 | run_command rm $CACHE_TEMP_SOURCE |
| 205 | |
| 206 | testname "apply bsdiff patch to new location" |
| 207 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file $WORK_DIR/new.file $NEW_SHA1 $NEW_SIZE $BAD1_SHA1:$WORK_DIR/foo $OLD_SHA1:$WORK_DIR/patch.bsdiff || fail |
| 208 | $ADB pull $WORK_DIR/new.file $tmpdir/patched |
| 209 | diff -q $DATA_DIR/new.file $tmpdir/patched || fail |
| 210 | |
| 211 | testname "reapply bsdiff patch to new location" |
| 212 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file $WORK_DIR/new.file $NEW_SHA1 $NEW_SIZE $BAD1_SHA1:$WORK_DIR/foo $OLD_SHA1:$WORK_DIR/patch.bsdiff || fail |
| 213 | $ADB pull $WORK_DIR/new.file $tmpdir/patched |
| 214 | diff -q $DATA_DIR/new.file $tmpdir/patched || fail |
| 215 | |
| 216 | $ADB push $DATA_DIR/old.file $CACHE_TEMP_SOURCE |
| 217 | # put some junk in the old file |
| 218 | run_command dd if=/dev/urandom of=$WORK_DIR/old.file count=100 bs=1024 || fail |
| 219 | |
| 220 | testname "apply bsdiff patch to new location with corrupted source" |
| 221 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file $WORK_DIR/new.file $NEW_SHA1 $NEW_SIZE $OLD_SHA1:$WORK_DIR/patch.bsdiff $BAD1_SHA1:$WORK_DIR/foo || fail |
| 222 | $ADB pull $WORK_DIR/new.file $tmpdir/patched |
| 223 | diff -q $DATA_DIR/new.file $tmpdir/patched || fail |
| 224 | |
| 225 | # put some junk in the cache copy, too |
| 226 | run_command dd if=/dev/urandom of=$CACHE_TEMP_SOURCE count=100 bs=1024 || fail |
| 227 | |
| 228 | run_command rm $WORK_DIR/new.file |
| 229 | testname "apply bsdiff patch to new location with corrupted source and copy (no new file)" |
| 230 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file $WORK_DIR/new.file $NEW_SHA1 $NEW_SIZE $OLD_SHA1:$WORK_DIR/patch.bsdiff $BAD1_SHA1:$WORK_DIR/foo && fail |
| 231 | |
| 232 | # put some junk in the new file |
| 233 | run_command dd if=/dev/urandom of=$WORK_DIR/new.file count=100 bs=1024 || fail |
| 234 | |
| 235 | testname "apply bsdiff patch to new location with corrupted source and copy (bad new file)" |
| 236 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file $WORK_DIR/new.file $NEW_SHA1 $NEW_SIZE $OLD_SHA1:$WORK_DIR/patch.bsdiff $BAD1_SHA1:$WORK_DIR/foo && fail |
| 237 | |
| 238 | # --------------- apply patch with low space on /system ---------------------- |
| 239 | |
| 240 | $ADB push $DATA_DIR/old.file $WORK_DIR |
| 241 | $ADB push $DATA_DIR/patch.bsdiff $WORK_DIR |
| 242 | |
| 243 | free_kb=$(free_space $WORK_FS) |
| 244 | echo "${free_kb}kb free on /$WORK_FS; we'll soon fix that." |
| 245 | echo run_command dd if=/dev/zero of=$WORK_DIR/bloat.dat count=$((free_kb-512)) bs=1024 || fail |
| 246 | run_command dd if=/dev/zero of=$WORK_DIR/bloat.dat count=$((free_kb-512)) bs=1024 || fail |
| 247 | free_kb=$(free_space $WORK_FS) |
| 248 | echo "${free_kb}kb free on /$WORK_FS now." |
| 249 | |
| 250 | testname "apply bsdiff patch with low space" |
| 251 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file - $NEW_SHA1 $NEW_SIZE $BAD1_SHA1:$WORK_DIR/foo $OLD_SHA1:$WORK_DIR/patch.bsdiff || fail |
| 252 | $ADB pull $WORK_DIR/old.file $tmpdir/patched |
| 253 | diff -q $DATA_DIR/new.file $tmpdir/patched || fail |
| 254 | |
| 255 | testname "reapply bsdiff patch with low space" |
| 256 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file - $NEW_SHA1 $NEW_SIZE $BAD1_SHA1:$WORK_DIR/foo $OLD_SHA1:$WORK_DIR/patch.bsdiff || fail |
| 257 | $ADB pull $WORK_DIR/old.file $tmpdir/patched |
| 258 | diff -q $DATA_DIR/new.file $tmpdir/patched || fail |
| 259 | |
| 260 | # --------------- apply patch with low space on /system and /cache ---------------------- |
| 261 | |
| 262 | $ADB push $DATA_DIR/old.file $WORK_DIR |
| 263 | $ADB push $DATA_DIR/patch.bsdiff $WORK_DIR |
| 264 | |
| 265 | free_kb=$(free_space $WORK_FS) |
| 266 | echo "${free_kb}kb free on /$WORK_FS" |
| 267 | |
| 268 | run_command mkdir /cache/subdir |
| 269 | run_command 'echo > /cache/subdir/a.file' |
| 270 | run_command 'echo > /cache/a.file' |
| 271 | run_command mkdir /cache/recovery /cache/recovery/otatest |
| 272 | run_command 'echo > /cache/recovery/otatest/b.file' |
| 273 | run_command "echo > $CACHE_TEMP_SOURCE" |
| 274 | free_kb=$(free_space cache) |
| 275 | echo "${free_kb}kb free on /cache; we'll soon fix that." |
| 276 | run_command dd if=/dev/zero of=/cache/bloat_small.dat count=128 bs=1024 || fail |
| 277 | run_command dd if=/dev/zero of=/cache/bloat_large.dat count=$((free_kb-640)) bs=1024 || fail |
| 278 | free_kb=$(free_space cache) |
| 279 | echo "${free_kb}kb free on /cache now." |
| 280 | |
| 281 | testname "apply bsdiff patch with low space, full cache, can't delete enough" |
| 282 | $ADB shell 'cat >> /cache/bloat_large.dat' & open_pid=$! |
| 283 | echo "open_pid is $open_pid" |
| 284 | |
| 285 | # size check should fail even though it deletes some stuff |
| 286 | run_command $WORK_DIR/applypatch -s $NEW_SIZE && fail |
| 287 | run_command ls /cache/bloat_small.dat && fail # was deleted |
| 288 | run_command ls /cache/a.file && fail # was deleted |
| 289 | run_command ls /cache/recovery/otatest/b.file && fail # was deleted |
| 290 | run_command ls /cache/bloat_large.dat || fail # wasn't deleted because it was open |
| 291 | run_command ls /cache/subdir/a.file || fail # wasn't deleted because it's in a subdir |
| 292 | run_command ls $CACHE_TEMP_SOURCE || fail # wasn't deleted because it's the source file copy |
| 293 | |
| 294 | # should fail; not enough files can be deleted |
| 295 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file - $NEW_SHA1 $NEW_SIZE $BAD1_SHA1:$WORK_DIR/foo $OLD_SHA1:$WORK_DIR/patch.bsdiff && fail |
| 296 | run_command ls /cache/bloat_large.dat || fail # wasn't deleted because it was open |
| 297 | run_command ls /cache/subdir/a.file || fail # wasn't deleted because it's in a subdir |
| 298 | run_command ls $CACHE_TEMP_SOURCE || fail # wasn't deleted because it's the source file copy |
| 299 | |
| 300 | kill $open_pid # /cache/bloat_large.dat is no longer open |
| 301 | |
| 302 | testname "apply bsdiff patch with low space, full cache, can delete enough" |
| 303 | |
| 304 | # should succeed after deleting /cache/bloat_large.dat |
| 305 | run_command $WORK_DIR/applypatch -s $NEW_SIZE || fail |
| 306 | run_command ls /cache/bloat_large.dat && fail # was deleted |
| 307 | run_command ls /cache/subdir/a.file || fail # still wasn't deleted because it's in a subdir |
| 308 | run_command ls $CACHE_TEMP_SOURCE || fail # wasn't deleted because it's the source file copy |
| 309 | |
| 310 | # should succeed |
| 311 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file - $NEW_SHA1 $NEW_SIZE $BAD1_SHA1:$WORK_DIR/foo $OLD_SHA1:$WORK_DIR/patch.bsdiff || fail |
| 312 | $ADB pull $WORK_DIR/old.file $tmpdir/patched |
| 313 | diff -q $DATA_DIR/new.file $tmpdir/patched || fail |
| 314 | run_command ls /cache/subdir/a.file || fail # still wasn't deleted because it's in a subdir |
| 315 | run_command ls $CACHE_TEMP_SOURCE && fail # was deleted because patching overwrote it, then deleted it |
| 316 | |
| 317 | # --------------- apply patch from cache ---------------------- |
| 318 | |
| 319 | $ADB push $DATA_DIR/old.file $CACHE_TEMP_SOURCE |
| 320 | # put some junk in the old file |
| 321 | run_command dd if=/dev/urandom of=$WORK_DIR/old.file count=100 bs=1024 || fail |
| 322 | |
| 323 | testname "apply bsdiff patch from cache (corrupted source) with low space" |
| 324 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file - $NEW_SHA1 $NEW_SIZE $BAD1_SHA1:$WORK_DIR/foo $OLD_SHA1:$WORK_DIR/patch.bsdiff || fail |
| 325 | $ADB pull $WORK_DIR/old.file $tmpdir/patched |
| 326 | diff -q $DATA_DIR/new.file $tmpdir/patched || fail |
| 327 | |
| 328 | $ADB push $DATA_DIR/old.file $CACHE_TEMP_SOURCE |
| 329 | # remove the old file entirely |
| 330 | run_command rm $WORK_DIR/old.file |
| 331 | |
| 332 | testname "apply bsdiff patch from cache (missing source) with low space" |
| 333 | run_command $WORK_DIR/applypatch $WORK_DIR/old.file - $NEW_SHA1 $NEW_SIZE $BAD1_SHA1:$WORK_DIR/foo $OLD_SHA1:$WORK_DIR/patch.bsdiff || fail |
| 334 | $ADB pull $WORK_DIR/old.file $tmpdir/patched |
| 335 | diff -q $DATA_DIR/new.file $tmpdir/patched || fail |
| 336 | |
| 337 | |
| 338 | # --------------- cleanup ---------------------- |
| 339 | |
| 340 | cleanup |
| 341 | |
| 342 | echo |
| 343 | echo PASS |
| 344 | echo |
| 345 | |