1#!/bin/bash
2
3# Copyright (C) 2009 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# uncomment for debugging
18#export DRY_RUN="echo"
19source test_backup_common.sh
20
21[ -z "$BUGREPORT_DIR" ] && BUGREPORT_DIR="$HOME/backup/bugreports"
22
23function check_file
24{
25    data=$(a shell cat /data/data/com.android.backuptest/$1)
26    if [ "$data" = "$2" ] ; then
27        echo "$1 has correct value [$2]"
28        return 0
29    else
30        echo $1 is INCORRECT
31        echo "   value:    [$data]"
32        echo "   expected: [$2]"
33        return 1
34    fi
35}
36
37function check_exists
38{
39    # return 0 if file exists, 1 otherwise
40    data=$(a shell "ls $@ 2> /dev/null >/dev/null && echo -n exists")
41    if [ "$data" = "exists" ]; then
42        return 0
43    else
44        return 1
45    fi
46}
47
48# Make sure adb is root so we can poke at com.android.backuptest's data
49adb_root
50
51# delete the old data
52echo --- Previous files
53a shell "ls -l /data/data/com.android.backuptest/files"
54a shell "rm /data/data/com.android.backuptest/files/*"
55echo --- Previous shared_prefs
56a shell "ls -l /data/data/com.android.backuptest/shared_prefs"
57a shell "rm /data/data/com.android.backuptest/shared_prefs/*"
58echo --- Erased files and shared_prefs
59a shell "ls -l /data/data/com.android.backuptest/files"
60a shell "ls -l /data/data/com.android.backuptest/shared_prefs"
61echo ---
62
63echo
64echo
65
66# FIXME: there's probably a smarter way to do this
67# FIXME: if we can get the android ID, that's probably the safest thing to do
68# pick the most recent set and restore from it
69restore_set=$(a shell bmgr list sets | head -n1 | awk '{print $1}')
70
71# run the restore
72echo "Restoring from set [$restore_set]"
73a shell bmgr restore "$restore_set"
74
75echo
76echo
77
78# check the results
79export need_bug=0
80
81# make sure files have the expected contents
82check_file files/file.txt "first file" || need_bug=1
83check_file files/another_file.txt "asdf" || need_bug=1
84#check_file files/3.txt "3" || need_bug=1
85check_file files/empty.txt "" || need_bug=1
86check_file shared_prefs/raw.xml '<map><int name="pref" value="1" /></map>' || need_bug=1
87
88# make sure that missing files weren't somehow created
89check_exists files/file_doesnt_exist.txt && need_bug=1
90check_exists files/no_files_here.txt && need_bug=1
91
92if [ \( "$need_bug" -ne 0 \) -a -d "$BUGREPORT_DIR" ]; then
93    dev_id=$(a get-serialno)
94    filename="${dev_id}_`date +%s`"
95    echo "Grabbing bugreport; filename is $filename"
96    a bugreport > "$BUGREPORT_DIR/$filename.txt"
97fi
98
99echo
100echo --- Restored files
101a shell "ls -l /data/data/com.android.backuptest/files"
102echo --- Restored shared_prefs
103a shell "ls -l /data/data/com.android.backuptest/shared_prefs"
104echo ---
105echo
106
107echo "Last 3 timestamps in 3.txt:"
108a shell cat /data/data/com.android.backuptest/files/3.txt | tail -n 3
109
110exit $need_bug
111
112