1#!/usr/bin/env bash
2
3# Before running the script:
4#
5# * lunch with eng variant. For example:
6#   lunch flame-eng
7# * Build the full system image and flash your device
8#   m; vendor/google/tools/flashall
9# * Remount the device to allow system rw.
10#   adb remount; adb reboot
11# * Connect the device to host machine to allow atest to build properly
12
13# Note: This script assumes the device runs with NEW_INSTALL_ONLY strategy.
14# The script will have to be updated when switching to BEST_EFFORT strategy.
15
16set -e
17
18TEST_APP_PATH="$ANDROID_TARGET_OUT_TESTCASES/CtsSharedUserMigrationInstallTestApp"
19PKGNAME='android.uidmigration.cts.InstallTestApp'
20
21cleanup() {
22  adb shell pm uninstall $PKGNAME >/dev/null 2>&1 || true
23  adb shell pm uninstall-system-updates $PKGNAME >/dev/null 2>&1 || true
24  adb shell stop || true
25  adb shell rm -rf /system/app/InstallTestApp || true
26  adb shell start || true
27}
28
29trap cleanup EXIT
30
31wait_boot_complete() {
32  while [ "$(adb shell getprop sys.boot_completed | tr -d '\r')" != "1" ]; do
33    sleep 3
34  done
35}
36
37export -f wait_boot_complete
38
39adb_stop() {
40  adb shell stop
41  adb shell setprop sys.boot_completed 0
42}
43
44# Build our test APKs
45atest -b CtsSharedUserMigrationTestCases
46
47# APK references
48#
49# InstallTestApp : APK with sharedUserId
50# InstallTestApp3: APK without sharedUserId
51# InstallTestApp4: APK with sharedUserId + sharedUserMaxSdkVersion
52
53# Make sure system is writable
54adb root
55adb remount
56
57# Push the APK as a system app
58adb shell mkdir /system/app/InstallTestApp
59adb push $TEST_APP_PATH/*/*.apk /system/app/InstallTestApp/InstallTestApp.apk
60
61# Restart PMS to load the package
62adb_stop
63adb shell start
64timeout 60 bash -c wait_boot_complete
65
66DUMPSYS_CMD="adb shell dumpsys package $PKGNAME | grep -o 'sharedUser=.*' | head -1"
67
68# Make sure package is installed and is part of shared UID
69SHARED_USER="$($DUMPSYS_CMD)"
70if [ -z "$SHARED_USER" ]; then
71  echo '! InstallTestApp is not installed properly'
72  exit 1
73fi
74
75# Installing an upgrade with sharedUserMaxSdkVersion shall not change its UID
76adb install -r ${TEST_APP_PATH}4/*/*.apk
77SHARED_USER="$($DUMPSYS_CMD)"
78if [ -z "$SHARED_USER" ]; then
79  echo '! InstallTestApp should remain in shared UID after normal upgrade'
80  exit 1
81fi
82
83echo '*****************'
84echo '* Test 1 PASSED *'
85echo '*****************'
86
87# Uninstall the upgrade
88# BUG? For some reason this command always return 1
89adb shell pm uninstall-system-updates $PKGNAME || true
90
91# Removing sharedUserId after an OTA should work
92adb_stop
93adb push ${TEST_APP_PATH}3/*/*.apk /system/app/InstallTestApp/InstallTestApp.apk
94adb shell start
95timeout 60 bash -c wait_boot_complete
96
97if ! adb shell pm list packages | grep -q $PKGNAME; then
98  echo '! InstallTestApp should still be installed after OTA'
99  exit 1
100fi
101
102SHARED_USER="$($DUMPSYS_CMD)"
103if [ -n "$SHARED_USER" ]; then
104  echo '! InstallTestApp should not be in shared UID after removing sharedUserId'
105  exit 1
106fi
107
108echo '*****************'
109echo '* Test 2 PASSED *'
110echo '*****************'
111
112# Adding sharedUserId back after an OTA should work
113adb_stop
114adb push $TEST_APP_PATH/*/*.apk /system/app/InstallTestApp/InstallTestApp.apk
115adb shell start
116timeout 60 bash -c wait_boot_complete
117
118SHARED_USER="$($DUMPSYS_CMD)"
119if [ -z "$SHARED_USER" ]; then
120  echo 'InstallTestApp should be in shared UID after adding sharedUserId!'
121  exit 1
122fi
123
124echo '*****************'
125echo '* Test 3 PASSED *'
126echo '*****************'
127
128# Adding sharedUserMaxSdkVersion in an OTA should not affect appId
129adb_stop
130adb push ${TEST_APP_PATH}4/*/*.apk /system/app/InstallTestApp/InstallTestApp.apk
131adb shell start
132timeout 60 bash -c wait_boot_complete
133
134SHARED_USER="$($DUMPSYS_CMD)"
135if [ -z "$SHARED_USER" ]; then
136  echo '! InstallTestApp should be in shared UID even after adding sharedUserMaxSdkVersion after OTA'
137  exit 1
138fi
139
140echo '*****************'
141echo '* Test 4 PASSED *'
142echo '*****************'
143
144# Remove the app, restart, and reinstall APK with sharedUserMaxSdkVersion
145# The new app should not be in shared UID (due to NEW_INSTALL_ONLY)
146adb_stop
147adb shell rm -rf /system/app/InstallTestApp
148adb shell start
149timeout 60 bash -c wait_boot_complete
150
151adb_stop
152adb shell mkdir /system/app/InstallTestApp
153adb push ${TEST_APP_PATH}4/*/*.apk /system/app/InstallTestApp/InstallTestApp.apk
154adb shell start
155timeout 60 bash -c wait_boot_complete
156
157if ! adb shell pm list packages | grep -q $PKGNAME; then
158  echo '! InstallTestApp should be installed'
159  exit 1
160fi
161
162SHARED_USER="$($DUMPSYS_CMD)"
163if [ -n "$SHARED_USER" ]; then
164  echo '! InstallTestApp should not be in shared UID when newly installed with sharedUserMaxSdkVersion'
165  exit 1
166fi
167
168echo '*****************'
169echo '* Test 5 PASSED *'
170echo '*****************'
171