blob: 706586dce0bddca26a9f91314e06e0664a5af900 [file] [log] [blame]
Laszlo Csomord456fca2017-08-10 10:21:53 +02001# pylint: disable=g-direct-third-party-import
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00002# Copyright 2015 The Bazel Authors. All rights reserved.
Alex Humeskya4ecde62015-05-21 17:08:42 +00003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Unit tests for stubify_incremental_install."""
17
18import os
19import unittest
Tony Aiutoeecb3442022-08-30 06:33:24 -070020from unittest import mock
Alex Humeskya4ecde62015-05-21 17:08:42 +000021import zipfile
22
laszlocsomord93a1462019-11-04 09:14:39 -080023# Do not edit this line. Copybara replaces it with PY2 migration helper.
laszlocsomord93a1462019-11-04 09:14:39 -080024
laszlocsomorad628ec2019-11-04 00:48:05 -080025from tools.android import incremental_install
Alex Humeskya4ecde62015-05-21 17:08:42 +000026
27
28class MockAdb(object):
29 """Mocks the Android ADB binary."""
30
31 def __init__(self):
32 # Map of file name -> contents.
33 self.files = {}
Lukacs Berkifaff7182015-08-20 14:27:42 +000034 self.split_apks = set()
Alex Humeskya4ecde62015-05-21 17:08:42 +000035 self._error = None
36 self.package_timestamp = None
Lukacs Berkifaff7182015-08-20 14:27:42 +000037 self._last_package_timestamp = 1
Alex Humeskya4ecde62015-05-21 17:08:42 +000038 self.shell_cmdlns = []
Lukacs Berkia3a33d72015-08-19 08:34:55 +000039 self.abi = "armeabi-v7a"
Alex Humeskya4ecde62015-05-21 17:08:42 +000040
41 def Exec(self, args):
42 if self._error:
43 error_info, arg = self._error # pylint: disable=unpacking-non-sequence
44 if not arg or arg in args:
45 return self._CreatePopenMock(*error_info)
46
47 returncode = 0
48 stdout = ""
49 stderr = ""
50 cmd = args[1]
51 if cmd == "push":
52 # "/test/adb push local remote"
Laszlo Csomord456fca2017-08-10 10:21:53 +020053 with open(args[2], "rb") as f:
Googler246ee562017-12-19 11:04:21 -080054 content = f.read().decode("utf-8")
Alex Humeskya4ecde62015-05-21 17:08:42 +000055 self.files[args[3]] = content
56 elif cmd == "pull":
57 # "/test/adb pull remote local"
58 remote = args[2]
59 local = args[3]
60 content = self.files.get(remote)
61 if content is not None:
Laszlo Csomord456fca2017-08-10 10:21:53 +020062 with open(local, "wb") as f:
Tony Aiutoeecb3442022-08-30 06:33:24 -070063 f.write(content.encode("utf-8"))
Alex Humeskya4ecde62015-05-21 17:08:42 +000064 else:
65 returncode = 1
66 stderr = "remote object '%s' does not exist\n" % remote
67 elif cmd == "install":
68 self.package_timestamp = self._last_package_timestamp
69 self._last_package_timestamp += 1
70 return self._CreatePopenMock(0, "Success", "")
Lukacs Berkifaff7182015-08-20 14:27:42 +000071 elif cmd == "install-multiple":
72 if args[3] == "-p":
Laszlo Csomord456fca2017-08-10 10:21:53 +020073 with open(args[5], "rb") as f:
Googler246ee562017-12-19 11:04:21 -080074 content = f.read().decode("utf-8")
Lukacs Berkifaff7182015-08-20 14:27:42 +000075 self.split_apks.add(content)
76 else:
77 self.package_timestamp = self._last_package_timestamp
78 self._last_package_timestamp += 1
79 return self._CreatePopenMock(0, "Success", "")
80 elif cmd == "uninstall":
81 self._CreatePopenMock(0, "Success", "")
82 self.split_apks = set()
83 self.package_timestamp = None
Alex Humeskya4ecde62015-05-21 17:08:42 +000084 elif cmd == "shell":
85 # "/test/adb shell ..."
86 # mkdir, rm, am (application manager), or monkey
87 shell_cmdln = args[2]
88 self.shell_cmdlns.append(shell_cmdln)
Googler73d4fc92015-07-30 20:39:46 +000089 if shell_cmdln.startswith(("mkdir", "am", "monkey", "input")):
Alex Humeskya4ecde62015-05-21 17:08:42 +000090 pass
Tony Aiutoeecb3442022-08-30 06:33:24 -070091 elif shell_cmdln.startswith("dumpsys package "):
Lukacs Berki0dffeac2015-09-15 07:18:47 +000092 if self.package_timestamp is not None:
93 timestamp = "firstInstallTime=%s" % self.package_timestamp
94 else:
95 timestamp = ""
96 return self._CreatePopenMock(0, timestamp, "")
Tony Aiutoeecb3442022-08-30 06:33:24 -070097 elif shell_cmdln.startswith("rm"):
Alex Humeskya4ecde62015-05-21 17:08:42 +000098 file_path = shell_cmdln.split()[2]
99 self.files.pop(file_path, None)
Tony Aiutoeecb3442022-08-30 06:33:24 -0700100 elif shell_cmdln.startswith("getprop ro.product.cpu.abi"):
Lukacs Berkia3a33d72015-08-19 08:34:55 +0000101 return self._CreatePopenMock(0, self.abi, "")
Alex Humeskya4ecde62015-05-21 17:08:42 +0000102 else:
103 raise Exception("Unknown shell command line: %s" % shell_cmdln)
104 # Return a mock subprocess.Popen object
105 return self._CreatePopenMock(returncode, stdout, stderr)
106
107 def _CreatePopenMock(self, returncode, stdout, stderr):
108 return mock.Mock(
109 returncode=returncode, communicate=lambda: (stdout, stderr))
110
111 def SetError(self, returncode, stdout, stderr, for_arg=None):
112 self._error = ((returncode, stdout, stderr), for_arg)
113
Lukacs Berkia3a33d72015-08-19 08:34:55 +0000114 def SetAbi(self, abi):
115 self.abi = abi
116
Alex Humeskya4ecde62015-05-21 17:08:42 +0000117
118class IncrementalInstallTest(unittest.TestCase):
119 """Unit tests for incremental install."""
120
121 _DEXMANIFEST = "dexmanifest.txt"
122 _ADB_PATH = "/test/adb"
123 _OUTPUT_MARKER = "full_deploy_marker"
124 _APK = "myapp_incremental.apk"
125 _RESOURCE_APK = "incremental.ap_"
126 _STUB_DATAFILE = "stub_application_data.txt"
127 _OLD_APP_PACKGE = "old.app.package"
128 _APP_PACKAGE = "new.app.package"
129 _EXEC_ROOT = "."
130
131 def setUp(self):
132 os.chdir(os.environ["TEST_TMPDIR"])
133
134 self._mock_adb = MockAdb()
135
136 # Write the stub datafile which contains the package name of the app.
Laszlo Csomord456fca2017-08-10 10:21:53 +0200137 with open(self._STUB_DATAFILE, "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800138 f.write(("\n".join([self._OLD_APP_PACKGE, self._APP_PACKAGE]))
139 .encode("utf-8"))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000140
141 # Write the local resource apk file.
Laszlo Csomord456fca2017-08-10 10:21:53 +0200142 with open(self._RESOURCE_APK, "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800143 f.write(b"resource apk")
Alex Humeskya4ecde62015-05-21 17:08:42 +0000144
145 # Mock out subprocess.Popen to use our mock adb.
146 self._popen_patch = mock.patch.object(incremental_install, "subprocess")
147 self._popen = self._popen_patch.start().Popen
148 self._popen.side_effect = lambda args, **kwargs: self._mock_adb.Exec(args)
149
150 def tearDown(self):
151 self._popen_patch.stop()
152
153 def _CreateZip(self, name="zip1", *files):
154 if not files:
155 files = [("zp1", "content1"), ("zp2", "content2")]
156 with zipfile.ZipFile(name, "w") as z:
157 for f, content in files:
158 z.writestr(f, content)
159
160 def _CreateLocalManifest(self, *lines):
161 content = "\n".join(lines)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200162 with open(self._DEXMANIFEST, "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800163 f.write(content.encode("utf-8"))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000164 return content
165
166 def _CreateRemoteManifest(self, *lines):
167 self._PutDeviceFile("dex/manifest", "\n".join(lines))
168
169 def _GetDeviceAppPath(self, f):
170 return os.path.join(
171 incremental_install.DEVICE_DIRECTORY, self._APP_PACKAGE, f)
172
173 def _GetDeviceFile(self, f):
174 return self._mock_adb.files[self._GetDeviceAppPath(f)]
175
176 def _PutDeviceFile(self, f, content):
177 self._mock_adb.files[self._GetDeviceAppPath(f)] = content
178
Lukacs Berkib4b19bc2015-07-30 11:45:35 +0000179 def _DeleteDeviceFile(self, f):
180 self._mock_adb.files.pop(self._GetDeviceAppPath(f), None)
181
Lukacs Berkiecbf2422015-07-28 07:57:45 +0000182 def _CallIncrementalInstall(self, incremental, native_libs=None,
Lukacs Berkifaff7182015-08-20 14:27:42 +0000183 split_main_apk=None, split_apks=None,
Googler73d4fc92015-07-30 20:39:46 +0000184 start_type="no"):
Lukacs Berkifaff7182015-08-20 14:27:42 +0000185 if split_main_apk:
186 apk = split_main_apk
187 elif incremental:
Alex Humeskya4ecde62015-05-21 17:08:42 +0000188 apk = None
189 else:
190 apk = self._APK
Lukacs Berkiecbf2422015-07-28 07:57:45 +0000191
Alex Humeskya4ecde62015-05-21 17:08:42 +0000192 incremental_install.IncrementalInstall(
193 adb_path=self._ADB_PATH,
194 execroot=self._EXEC_ROOT,
195 stub_datafile=self._STUB_DATAFILE,
196 dexmanifest=self._DEXMANIFEST,
197 apk=apk,
198 resource_apk=self._RESOURCE_APK,
Lukacs Berkifaff7182015-08-20 14:27:42 +0000199 split_main_apk=split_main_apk,
200 split_apks=split_apks,
Lukacs Berkiecbf2422015-07-28 07:57:45 +0000201 native_libs=native_libs,
Alex Humeskya4ecde62015-05-21 17:08:42 +0000202 output_marker=self._OUTPUT_MARKER,
203 adb_jobs=1,
Googler73d4fc92015-07-30 20:39:46 +0000204 start_type=start_type,
Alex Humeskya4ecde62015-05-21 17:08:42 +0000205 user_home_dir="/home/root")
206
207 def testUploadToPristineDevice(self):
Alex Humeskya4ecde62015-05-21 17:08:42 +0000208 self._CreateZip()
209
Laszlo Csomord456fca2017-08-10 10:21:53 +0200210 with open("dex1", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800211 f.write(b"content3")
Alex Humeskya4ecde62015-05-21 17:08:42 +0000212
213 manifest = self._CreateLocalManifest(
214 "zip1 zp1 ip1 0",
215 "zip1 zp2 ip2 0",
216 "dex1 - ip3 0")
217
218 self._CallIncrementalInstall(incremental=False)
219
220 resources_checksum_path = self._GetDeviceAppPath("resources_checksum")
221 self.assertTrue(resources_checksum_path in self._mock_adb.files)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200222 self.assertEqual(manifest, self._GetDeviceFile("dex/manifest"))
223 self.assertEqual("content1", self._GetDeviceFile("dex/ip1"))
224 self.assertEqual("content2", self._GetDeviceFile("dex/ip2"))
225 self.assertEqual("content3", self._GetDeviceFile("dex/ip3"))
226 self.assertEqual("resource apk", self._GetDeviceFile("resources.ap_"))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000227
Lukacs Berkifaff7182015-08-20 14:27:42 +0000228 def testSplitInstallToPristineDevice(self):
Laszlo Csomord456fca2017-08-10 10:21:53 +0200229 with open("split1", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800230 f.write(b"split_content1")
Lukacs Berkifaff7182015-08-20 14:27:42 +0000231
Laszlo Csomord456fca2017-08-10 10:21:53 +0200232 with open("main", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800233 f.write(b"main_Content")
Lukacs Berkifaff7182015-08-20 14:27:42 +0000234
235 self._CallIncrementalInstall(
236 incremental=False, split_main_apk="main", split_apks=["split1"])
Laszlo Csomord456fca2017-08-10 10:21:53 +0200237 self.assertEqual(set(["split_content1"]), self._mock_adb.split_apks)
Lukacs Berkifaff7182015-08-20 14:27:42 +0000238
239 def testSplitInstallUnchanged(self):
Laszlo Csomord456fca2017-08-10 10:21:53 +0200240 with open("split1", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800241 f.write(b"split_content1")
Lukacs Berkifaff7182015-08-20 14:27:42 +0000242
Laszlo Csomord456fca2017-08-10 10:21:53 +0200243 with open("main", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800244 f.write(b"main_Content")
Lukacs Berkifaff7182015-08-20 14:27:42 +0000245
246 self._CallIncrementalInstall(
247 incremental=False, split_main_apk="main", split_apks=["split1"])
Laszlo Csomord456fca2017-08-10 10:21:53 +0200248 self.assertEqual(set(["split_content1"]), self._mock_adb.split_apks)
Lukacs Berkifaff7182015-08-20 14:27:42 +0000249 self._mock_adb.split_apks = set()
250 self._CallIncrementalInstall(
251 incremental=False, split_main_apk="main", split_apks=["split1"])
Laszlo Csomord456fca2017-08-10 10:21:53 +0200252 self.assertEqual(set([]), self._mock_adb.split_apks)
Lukacs Berkifaff7182015-08-20 14:27:42 +0000253
254 def testSplitInstallChanges(self):
Laszlo Csomord456fca2017-08-10 10:21:53 +0200255 with open("split1", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800256 f.write(b"split_content1")
Lukacs Berkifaff7182015-08-20 14:27:42 +0000257
Laszlo Csomord456fca2017-08-10 10:21:53 +0200258 with open("main", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800259 f.write(b"main_Content")
Lukacs Berkifaff7182015-08-20 14:27:42 +0000260
261 self._CallIncrementalInstall(
262 incremental=False, split_main_apk="main", split_apks=["split1"])
Laszlo Csomord456fca2017-08-10 10:21:53 +0200263 self.assertEqual(set(["split_content1"]), self._mock_adb.split_apks)
Lukacs Berkifaff7182015-08-20 14:27:42 +0000264
Laszlo Csomord456fca2017-08-10 10:21:53 +0200265 with open("split1", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800266 f.write(b"split_content2")
Lukacs Berkifaff7182015-08-20 14:27:42 +0000267 self._mock_adb.split_apks = set()
268 self._CallIncrementalInstall(
269 incremental=False, split_main_apk="main", split_apks=["split1"])
Laszlo Csomord456fca2017-08-10 10:21:53 +0200270 self.assertEqual(set(["split_content2"]), self._mock_adb.split_apks)
Lukacs Berkifaff7182015-08-20 14:27:42 +0000271
Lukacs Berkib4b19bc2015-07-30 11:45:35 +0000272 def testMissingNativeManifestWithIncrementalInstall(self):
273 self._CreateZip()
Laszlo Csomord456fca2017-08-10 10:21:53 +0200274 with open("liba.so", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800275 f.write(b"liba_1")
Lukacs Berkib4b19bc2015-07-30 11:45:35 +0000276
277 # Upload a library to the device.
278 native_libs = ["armeabi-v7a:liba.so"]
279 self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200280 self.assertEqual("liba_1", self._GetDeviceFile("native/liba.so"))
Lukacs Berkib4b19bc2015-07-30 11:45:35 +0000281
282 # Delete the manifest, overwrite the library and check that even an
283 # incremental install straightens things out.
284 self._PutDeviceFile("native/liba.so", "GARBAGE")
285 self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200286 self.assertEqual("liba_1", self._GetDeviceFile("native/liba.so"))
Lukacs Berkib4b19bc2015-07-30 11:45:35 +0000287
288 def testNonIncrementalInstallOverwritesNativeLibs(self):
289 self._CreateZip()
Laszlo Csomord456fca2017-08-10 10:21:53 +0200290 with open("liba.so", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800291 f.write(b"liba_1")
Lukacs Berkib4b19bc2015-07-30 11:45:35 +0000292
293 # Upload a library to the device.
294 native_libs = ["armeabi-v7a:liba.so"]
295 self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200296 self.assertEqual("liba_1", self._GetDeviceFile("native/liba.so"))
Lukacs Berkib4b19bc2015-07-30 11:45:35 +0000297
298 # Change a library on the device. Incremental install should not replace the
299 # changed file, because it only checks the manifest.
300 self._PutDeviceFile("native/liba.so", "GARBAGE")
301 self._CallIncrementalInstall(incremental=True, native_libs=native_libs)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200302 self.assertEqual("GARBAGE", self._GetDeviceFile("native/liba.so"))
Lukacs Berkib4b19bc2015-07-30 11:45:35 +0000303
304 # However, a full install should overwrite it.
305 self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200306 self.assertEqual("liba_1", self._GetDeviceFile("native/liba.so"))
Lukacs Berkib4b19bc2015-07-30 11:45:35 +0000307
Lukacs Berkia3a33d72015-08-19 08:34:55 +0000308 def testNativeAbiCompatibility(self):
309 self._CreateZip()
Laszlo Csomord456fca2017-08-10 10:21:53 +0200310 with open("liba.so", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800311 f.write(b"liba")
Lukacs Berkia3a33d72015-08-19 08:34:55 +0000312
313 native_libs = ["armeabi:liba.so"]
314 self._mock_adb.SetAbi("arm64-v8a")
315 self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200316 self.assertEqual("liba", self._GetDeviceFile("native/liba.so"))
Lukacs Berkia3a33d72015-08-19 08:34:55 +0000317
Lukacs Berkiecbf2422015-07-28 07:57:45 +0000318 def testUploadNativeLibs(self):
319 self._CreateZip()
Laszlo Csomord456fca2017-08-10 10:21:53 +0200320 with open("liba.so", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800321 f.write(b"liba_1")
Laszlo Csomord456fca2017-08-10 10:21:53 +0200322 with open("libb.so", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800323 f.write(b"libb_1")
Alex Humeskya4ecde62015-05-21 17:08:42 +0000324
Lukacs Berkiecbf2422015-07-28 07:57:45 +0000325 native_libs = ["armeabi-v7a:liba.so", "armeabi-v7a:libb.so"]
326 self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200327 self.assertEqual("liba_1", self._GetDeviceFile("native/liba.so"))
328 self.assertEqual("libb_1", self._GetDeviceFile("native/libb.so"))
Lukacs Berkiecbf2422015-07-28 07:57:45 +0000329
330 # Change a library
Laszlo Csomord456fca2017-08-10 10:21:53 +0200331 with open("libb.so", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800332 f.write(b"libb_2")
Lukacs Berkiecbf2422015-07-28 07:57:45 +0000333 self._CallIncrementalInstall(incremental=True, native_libs=native_libs)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200334 self.assertEqual("libb_2", self._GetDeviceFile("native/libb.so"))
Lukacs Berkiecbf2422015-07-28 07:57:45 +0000335
336 # Delete a library
337 self._CallIncrementalInstall(
338 incremental=True, native_libs=["armeabi-v7a:liba.so"])
339 self.assertFalse(
340 self._GetDeviceAppPath("native/libb.so") in self._mock_adb.files)
341
342 # Add the deleted library back
343 self._CallIncrementalInstall(incremental=True, native_libs=native_libs)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200344 self.assertEqual("libb_2", self._GetDeviceFile("native/libb.so"))
Lukacs Berkiecbf2422015-07-28 07:57:45 +0000345
346 def testUploadWithOneChangedFile(self):
Alex Humeskya4ecde62015-05-21 17:08:42 +0000347 # Existing manifest from a previous install.
348 self._CreateRemoteManifest(
349 "zip1 zp1 ip1 0",
350 "zip1 zp2 ip2 1")
351
352 # Existing files from a previous install.
353 self._PutDeviceFile("dex/ip1", "old content1")
354 self._PutDeviceFile("dex/ip2", "old content2")
355 self._PutDeviceFile("install_timestamp", "0")
356 self._mock_adb.package_timestamp = "0"
357
358 self._CreateZip()
359
360 # Updated dex manifest.
361 self._CreateLocalManifest(
362 "zip1 zp1 ip1 0",
363 "zip1 zp2 ip2 2")
364
365 self._CallIncrementalInstall(incremental=True)
366
367 # This is a bit of a dishonest test: the local content for "ip1" is
368 # "content1" and the remote content for it is "old content1", but
369 # the checksums for that file are the same in the local and remote manifest.
370 # We just want to make sure that only one file was updated, so to
371 # distinguish that we force the local and remote content to be different but
372 # keep the checksum the same.
Laszlo Csomord456fca2017-08-10 10:21:53 +0200373 self.assertEqual("old content1", self._GetDeviceFile("dex/ip1"))
374 self.assertEqual("content2", self._GetDeviceFile("dex/ip2"))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000375
376 def testFullUploadWithOneChangedFile(self):
377
378 # Existing manifest from a previous install.
379 self._CreateRemoteManifest(
380 "zip1 zp1 ip1 0",
381 "zip1 zp2 ip2 1")
382
383 self._PutDeviceFile("dex/ip1", "old content1")
384 self._PutDeviceFile("dex/ip2", "old content2")
385 self._PutDeviceFile("install_timestamp", "0")
386 self._mock_adb.package_timestamp = "0"
387
388 self._CreateZip()
389
390 self._CreateLocalManifest(
391 "zip1 zp1 ip1 0",
392 "zip1 zp2 ip2 2")
393
394 self._CallIncrementalInstall(incremental=False)
395
396 # Even though the checksums for ip1 were the same, the file still got
397 # updated. This is a bit of a dishonest test because the local and remote
398 # content for ip1 were different, but their checksums were the same.
Laszlo Csomord456fca2017-08-10 10:21:53 +0200399 self.assertEqual("content1", self._GetDeviceFile("dex/ip1"))
400 self.assertEqual("content2", self._GetDeviceFile("dex/ip2"))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000401
402 def testUploadWithNewFile(self):
403
404 self._CreateRemoteManifest("zip1 zp1 ip1 0")
405 self._PutDeviceFile("dex/ip1", "content1")
406 self._PutDeviceFile("install_timestamp", "0")
407 self._mock_adb.package_timestamp = "0"
408
409 self._CreateLocalManifest(
410 "zip1 zp1 ip1 0",
411 "zip1 zp2 ip2 1")
412
413 self._CreateZip()
414
415 self._CallIncrementalInstall(incremental=True)
416
Laszlo Csomord456fca2017-08-10 10:21:53 +0200417 self.assertEqual("content1", self._GetDeviceFile("dex/ip1"))
418 self.assertEqual("content2", self._GetDeviceFile("dex/ip2"))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000419
420 def testDeletesFile(self):
421
422 self._CreateRemoteManifest(
423 "zip1 zp1 ip1 0",
424 "zip1 zip2 ip2 1")
425 self._PutDeviceFile("dex/ip1", "content1")
426 self._PutDeviceFile("dex/ip2", "content2")
Lukacs Berkifaff7182015-08-20 14:27:42 +0000427 self._PutDeviceFile("install_timestamp", "1")
428 self._mock_adb.package_timestamp = "1"
Alex Humeskya4ecde62015-05-21 17:08:42 +0000429
430 self._CreateZip("zip1", ("zp1", "content1"))
431 self._CreateLocalManifest("zip1 zp1 ip1 0")
432
433 self.assertTrue(self._GetDeviceAppPath("dex/ip2") in self._mock_adb.files)
434 self._CallIncrementalInstall(incremental=True)
435 self.assertFalse(self._GetDeviceAppPath("dex/ip2") in self._mock_adb.files)
436
437 def testNothingToUpdate(self):
438 self._CreateRemoteManifest(
439 "zip1 zp1 ip1 0",
440 "zip1 zip2 ip2 1",
441 "dex1 - ip3 0")
442 self._PutDeviceFile("dex/ip1", "content1")
443 self._PutDeviceFile("dex/ip2", "content2")
444 self._PutDeviceFile("dex/ip3", "content3")
445 self._PutDeviceFile("install_timestamp", "0")
446 self._mock_adb.package_timestamp = "0"
447
448 self._CreateZip()
449 self._CreateLocalManifest(
450 "zip1 zp1 ip1 0",
451 "zip1 zip2 ip2 1",
452 "dex1 - ip3 0")
453
454 self._CallIncrementalInstall(incremental=True)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200455 self.assertEqual("content1", self._GetDeviceFile("dex/ip1"))
456 self.assertEqual("content2", self._GetDeviceFile("dex/ip2"))
457 self.assertEqual("content3", self._GetDeviceFile("dex/ip3"))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000458
459 def testNoResourcesToUpdate(self):
460 self._CreateRemoteManifest("zip1 zp1 ip1 0")
461 self._PutDeviceFile("dex/ip1", "content1")
462 # The local file is actually "resource apk", but the checksum on the device
463 # for the resources file is set to be the same as the checksum for the
464 # local file so that we can confirm that it was not updated.
465 self._PutDeviceFile("resources.ap_", "resources")
466 self._PutDeviceFile("resources_checksum",
467 incremental_install.Checksum(self._RESOURCE_APK))
468 self._PutDeviceFile("install_timestamp", "0")
469 self._mock_adb.package_timestamp = "0"
470
471 self._CreateZip()
472 self._CreateLocalManifest("zip1 zp1 ip1 0")
473
474 self._CallIncrementalInstall(incremental=True)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200475 self.assertEqual("resources", self._GetDeviceFile("resources.ap_"))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000476
477 def testUpdateResources(self):
478 self._CreateRemoteManifest("zip1 zp1 ip1 0")
479 self._PutDeviceFile("dex/ip1", "content1")
480 self._PutDeviceFile("resources.ap_", "resources")
481 self._PutDeviceFile("resources_checksum", "checksum")
482 self._PutDeviceFile("install_timestamp", "0")
483 self._mock_adb.package_timestamp = "0"
484
485 self._CreateZip()
486 self._CreateLocalManifest("zip1 zp1 ip1 0")
487
488 self._CallIncrementalInstall(incremental=True)
Laszlo Csomord456fca2017-08-10 10:21:53 +0200489 self.assertEqual("resource apk", self._GetDeviceFile("resources.ap_"))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000490
491 def testNoDevice(self):
492 self._mock_adb.SetError(1, "", "device not found")
493 try:
494 self._CallIncrementalInstall(incremental=True)
495 self.fail("Should have quit if there is no device")
Alex Humeskyf0a5ac62015-09-22 00:41:11 +0000496 except SystemExit as e:
497 # make sure it's the right SystemExit reason
498 self.assertTrue("Device not found" in str(e))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000499
500 def testUnauthorizedDevice(self):
501 self._mock_adb.SetError(1, "", "device unauthorized. Please check the "
502 "confirmation dialog on your device")
503 try:
504 self._CallIncrementalInstall(incremental=True)
505 self.fail("Should have quit if the device is unauthorized.")
Alex Humeskyf0a5ac62015-09-22 00:41:11 +0000506 except SystemExit as e:
507 # make sure it's the right SystemExit reason
508 self.assertTrue("Device unauthorized." in str(e))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000509
510 def testInstallFailure(self):
Lukacs Berki60f6cf62015-12-16 08:57:12 +0000511 self._mock_adb.SetError(0, "Failure", "INSTALL_FAILED", for_arg="install")
Alex Humeskya4ecde62015-05-21 17:08:42 +0000512 self._CreateZip()
513 self._CreateLocalManifest("zip1 zp1 ip1 0")
514 try:
515 self._CallIncrementalInstall(incremental=False)
516 self.fail("Should have quit if the install failed.")
Alex Humeskyf0a5ac62015-09-22 00:41:11 +0000517 except SystemExit as e:
518 # make sure it's the right SystemExit reason
519 self.assertTrue("Failure" in str(e))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000520
Googler73d4fc92015-07-30 20:39:46 +0000521 def testStartCold(self):
Alex Humeskya4ecde62015-05-21 17:08:42 +0000522 # Based on testUploadToPristineDevice
523 self._CreateZip()
524
Laszlo Csomord456fca2017-08-10 10:21:53 +0200525 with open("dex1", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800526 f.write(b"content3")
Alex Humeskya4ecde62015-05-21 17:08:42 +0000527
528 self._CreateLocalManifest(
529 "zip1 zp1 ip1 0",
530 "zip1 zp2 ip2 0",
531 "dex1 - ip3 0")
532
Googler73d4fc92015-07-30 20:39:46 +0000533 self._CallIncrementalInstall(incremental=False, start_type="cold")
Alex Humeskya4ecde62015-05-21 17:08:42 +0000534
535 self.assertTrue(("monkey -p %s -c android.intent.category.LAUNCHER 1" %
536 self._APP_PACKAGE) in self._mock_adb.shell_cmdlns)
537
Googler2b88f622017-03-16 21:52:14 +0000538 def testDebugStart(self):
539 self._CreateZip()
540
Laszlo Csomord456fca2017-08-10 10:21:53 +0200541 with open("dex1", "wb") as f:
Googler246ee562017-12-19 11:04:21 -0800542 f.write(b"content3")
Googler2b88f622017-03-16 21:52:14 +0000543
544 self._CreateLocalManifest(
545 "zip1 zp1 ip1 0",
546 "zip1 zp2 ip2 0",
547 "dex1 - ip3 0")
548
549 self._CallIncrementalInstall(incremental=False, start_type="debug")
550 enable_debug_cmd = ("am set-debug-app -w --persistent %s" %
551 self._APP_PACKAGE)
552 start_app_cmd = ("monkey -p %s -c android.intent.category.LAUNCHER 1" %
553 self._APP_PACKAGE)
554 self.assertTrue(enable_debug_cmd in self._mock_adb.shell_cmdlns)
555 self.assertTrue(start_app_cmd in self._mock_adb.shell_cmdlns)
556
Googler73d4fc92015-07-30 20:39:46 +0000557 def testColdStop(self):
558 self._CreateRemoteManifest(
559 "zip1 zp1 ip1 0",
560 "zip1 zip2 ip2 1",
561 "dex1 - ip3 0")
562 self._PutDeviceFile("dex/ip1", "content1")
563 self._PutDeviceFile("dex/ip2", "content2")
564 self._PutDeviceFile("dex/ip3", "content3")
565 self._PutDeviceFile("install_timestamp", "0")
566 self._mock_adb.package_timestamp = "0"
567
568 self._CreateZip()
569 self._CreateLocalManifest(
570 "zip1 zp1 ip1 0",
571 "zip1 zip2 ip2 1",
572 "dex1 - ip3 0")
573 self._CallIncrementalInstall(incremental=True, start_type="cold")
574
575 stop_cmd = "am force-stop %s" % self._APP_PACKAGE
576 self.assertTrue(stop_cmd in self._mock_adb.shell_cmdlns)
577
578 def testWarmStop(self):
579 self._CreateRemoteManifest(
580 "zip1 zp1 ip1 0",
581 "zip1 zip2 ip2 1",
582 "dex1 - ip3 0")
583 self._PutDeviceFile("dex/ip1", "content1")
584 self._PutDeviceFile("dex/ip2", "content2")
585 self._PutDeviceFile("dex/ip3", "content3")
586 self._PutDeviceFile("install_timestamp", "0")
587 self._mock_adb.package_timestamp = "0"
588
589 self._CreateZip()
590 self._CreateLocalManifest(
591 "zip1 zp1 ip1 0",
592 "zip1 zip2 ip2 1",
593 "dex1 - ip3 0")
594 self._CallIncrementalInstall(incremental=True, start_type="warm")
595
596 background_cmd = "input keyevent KEYCODE_APP_SWITCH"
597 stop_cmd = "am kill %s" % self._APP_PACKAGE
598 self.assertTrue(background_cmd in self._mock_adb.shell_cmdlns)
599 self.assertTrue(stop_cmd in self._mock_adb.shell_cmdlns)
600
Alex Humeskya4ecde62015-05-21 17:08:42 +0000601 def testMultipleDevicesError(self):
602 errors = [
603 "more than one device and emulator",
604 "more than one device",
605 "more than one emulator",
606 ]
607 for error in errors:
Alex Humeskyf0a5ac62015-09-22 00:41:11 +0000608 self._mock_adb.SetError(1, "", error)
Alex Humeskya4ecde62015-05-21 17:08:42 +0000609 try:
610 self._CallIncrementalInstall(incremental=True)
611 self.fail("Should have quit if there were multiple devices.")
Alex Humeskyf0a5ac62015-09-22 00:41:11 +0000612 except SystemExit as e:
613 # make sure it's the right SystemExit reason
614 self.assertTrue("Try specifying a device serial" in str(e))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000615
616 def testIncrementalInstallOnPristineDevice(self):
617 self._CreateZip()
618 self._CreateLocalManifest(
619 "zip1 zp1 ip1 0",
620 "zip1 zip2 ip2 1",
621 "dex1 - ip3 0")
622
623 try:
624 self._CallIncrementalInstall(incremental=True)
625 self.fail("Should have quit for incremental install on pristine device")
626 except SystemExit:
627 pass
628
629 def testIncrementalInstallWithWrongInstallTimestamp(self):
630 self._CreateRemoteManifest(
631 "zip1 zp1 ip1 0",
632 "zip1 zip2 ip2 1",
633 "dex1 - ip3 0")
634 self._PutDeviceFile("dex/ip1", "content1")
635 self._PutDeviceFile("dex/ip2", "content2")
636 self._PutDeviceFile("dex/ip3", "content3")
637 self._mock_adb.package_timestamp = "WRONG"
638
639 self._CreateZip()
640 self._CreateLocalManifest(
641 "zip1 zp1 ip1 0",
642 "zip1 zip2 ip2 1",
643 "dex1 - ip3 0")
644
645 try:
646 self._CallIncrementalInstall(incremental=True)
647 self.fail("Should have quit if install timestamp is wrong")
648 except SystemExit:
649 pass
650
Alex Humeskyf0a5ac62015-09-22 00:41:11 +0000651 def testSdkTooOld(self):
652 self._mock_adb.SetError(
653 0, "INSTALL_FAILED_OLDER_SDK", "", for_arg="install")
654 self._CreateZip()
655 self._CreateLocalManifest("zip1 zp1 ip1 0")
656 try:
657 self._CallIncrementalInstall(incremental=False)
658 self.fail("Should have quit if the SDK is too old.")
659 except SystemExit as e:
660 # make sure it's the right SystemExit reason
661 self.assertTrue("minSdkVersion" in str(e))
662
663
Alex Humeskya4ecde62015-05-21 17:08:42 +0000664if __name__ == "__main__":
665 unittest.main()