blob: 28f5e75162b023f7abf3f8fef582bca8015c63bc [file] [log] [blame]
Chris Sosa0356d3b2010-09-16 22:46:221#!/usr/bin/python
2
3# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Unit tests for autoupdate.py."""
8
Dale Curtisc9aaf3a2011-08-09 22:47:409import json
Chris Sosa0356d3b2010-09-16 22:46:2210import os
Chris Sosa6a3697f2013-01-30 00:44:4311import shutil
Chris Sosa7c931362010-10-12 02:49:0112import socket
Chris Sosa0356d3b2010-09-16 22:46:2213import unittest
Chris Sosa0356d3b2010-09-16 22:46:2214
Gilad Arnoldabb352e2012-09-23 08:24:2715import cherrypy
16import mox
17
Chris Sosa0356d3b2010-09-16 22:46:2218import autoupdate
Chris Sosa52148582012-11-15 23:35:5819import autoupdate_lib
Gilad Arnold55a2a372012-10-02 16:46:3220import common_util
Chris Sosa0356d3b2010-09-16 22:46:2221
Gilad Arnoldabb352e2012-09-23 08:24:2722
Chris Sosa0356d3b2010-09-16 22:46:2223_TEST_REQUEST = """
Chris Sosa52148582012-11-15 23:35:5824<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" protocol="3.0">
25 <app version="%(version)s" track="%(track)s" board="%(board)s" />
26 <updatecheck />
27 <event eventresult="%(event_result)d" eventtype="%(event_type)d" />
Chris Sosa0356d3b2010-09-16 22:46:2228</client_test>"""
29
Chris Sosa6a3697f2013-01-30 00:44:4330#pylint: disable=W0212
Chris Sosa0356d3b2010-09-16 22:46:2231class AutoupdateTest(mox.MoxTestBase):
32 def setUp(self):
33 mox.MoxTestBase.setUp(self)
Gilad Arnold55a2a372012-10-02 16:46:3234 self.mox.StubOutWithMock(common_util, 'GetFileSize')
35 self.mox.StubOutWithMock(common_util, 'GetFileSha1')
36 self.mox.StubOutWithMock(common_util, 'GetFileSha256')
Chris Sosa52148582012-11-15 23:35:5837 self.mox.StubOutWithMock(autoupdate_lib, 'GetUpdateResponse')
Chris Sosa0356d3b2010-09-16 22:46:2238 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
Gilad Arnold0c9c8602012-10-03 06:58:5839 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs')
Chris Sosa7c931362010-10-12 02:49:0140 self.port = 8080
Chris Sosa0356d3b2010-09-16 22:46:2241 self.test_board = 'test-board'
42 self.build_root = '/src_path/build/images'
43 self.latest_dir = '12345_af_12-a1'
44 self.latest_verision = '12345_af_12'
45 self.static_image_dir = '/tmp/static-dir/'
Chris Sosa7c931362010-10-12 02:49:0146 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 22:47:4047 self.test_dict = {
48 'client': 'ChromeOSUpdateEngine-1.0',
49 'version': 'ForcedUpdate',
50 'track': 'unused_var',
51 'board': self.test_board,
52 'event_result': 2,
53 'event_type': 3
54 }
Chris Sosa0356d3b2010-09-16 22:46:2255 self.test_data = _TEST_REQUEST % self.test_dict
56 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
Gilad Arnold0c9c8602012-10-03 06:58:5857 self.sha1 = 12345
Chris Sosa0356d3b2010-09-16 22:46:2258 self.size = 54321
59 self.url = 'http://%s/static/update.gz' % self.hostname
60 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 18:51:3661 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-26 00:26:1762 cherrypy.request.base = 'http://%s' % self.hostname
Chris Sosa6a3697f2013-01-30 00:44:4363 os.makedirs(self.static_image_dir)
64
65 def tearDown(self):
66 shutil.rmtree(self.static_image_dir)
Chris Sosa54555862010-10-26 00:26:1767
Gilad Arnold0c9c8602012-10-03 06:58:5868 def _DummyAutoupdateConstructor(self, **kwargs):
Chris Sosa0356d3b2010-09-16 22:46:2269 """Creates a dummy autoupdater. Used to avoid using constructor."""
70 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-12 02:49:0171 static_dir=self.static_image_dir,
Gilad Arnold0c9c8602012-10-03 06:58:5872 **kwargs)
Chris Sosa0356d3b2010-09-16 22:46:2273 return dummy
74
Chris Sosa744e1472011-09-08 02:32:5075 def testGetRightSignedDeltaPayloadDir(self):
76 """Test that our directory is what we expect it to be for signed updates."""
Gilad Arnold55a2a372012-10-02 16:46:3277 self.mox.StubOutWithMock(common_util, 'GetFileMd5')
Chris Sosa744e1472011-09-08 02:32:5078 key_path = 'test_key_path'
79 src_image = 'test_src_image'
80 target_image = 'test_target_image'
Gilad Arnold55a2a372012-10-02 16:46:3281 src_hash = '12345'
82 target_hash = '67890'
83 key_hash = 'abcde'
Chris Sosa744e1472011-09-08 02:32:5084
Gilad Arnold55a2a372012-10-02 16:46:3285 common_util.GetFileMd5(src_image).AndReturn(src_hash)
86 common_util.GetFileMd5(target_image).AndReturn(target_hash)
87 common_util.GetFileMd5(key_path).AndReturn(key_hash)
Chris Sosa744e1472011-09-08 02:32:5088
89 self.mox.ReplayAll()
90 au_mock = self._DummyAutoupdateConstructor()
91 au_mock.private_key = key_path
92 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 19:31:3693 self.assertEqual(os.path.basename(update_dir),
Gilad Arnold55a2a372012-10-02 16:46:3294 '%s_%s+%s+patched_kernel' %
95 (src_hash, target_hash, key_hash))
Chris Sosa744e1472011-09-08 02:32:5096 self.mox.VerifyAll()
97
Chris Sosa0356d3b2010-09-16 22:46:2298 def testGenerateLatestUpdateImageWithForced(self):
Don Garrettf90edf02010-11-17 01:36:1499 self.mox.StubOutWithMock(autoupdate.Autoupdate,
100 'GenerateUpdateImageWithCache')
Chris Sosa6a3697f2013-01-30 00:44:43101 au_mock = self._DummyAutoupdateConstructor()
102 au_mock._GetLatestImageDir(self.test_board).AndReturn(
103 os.path.join(self.build_root, self.test_board, self.latest_dir))
104 au_mock.GenerateUpdateImageWithCache(
105 os.path.join(self.build_root, self.test_board, self.latest_dir,
106 'chromiumos_image.bin'),
Don Garrettf90edf02010-11-17 01:36:14107 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 22:46:22108
109 self.mox.ReplayAll()
Chris Sosa0356d3b2010-09-16 22:46:22110 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
111 'ForcedUpdate',
112 self.static_image_dir))
113 self.mox.VerifyAll()
114
115 def testHandleUpdatePingForForcedImage(self):
Don Garrettf90edf02010-11-17 01:36:14116 self.mox.StubOutWithMock(autoupdate.Autoupdate,
117 'GenerateUpdateImageWithCache')
Chris Sosa6a3697f2013-01-30 00:44:43118 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
119 au_mock = self._DummyAutoupdateConstructor()
Chris Sosa0356d3b2010-09-16 22:46:22120 test_data = _TEST_REQUEST % self.test_dict
121
Chris Sosa6a3697f2013-01-30 00:44:43122 # Generate a fake payload.
123 update_gz = os.path.join(self.static_image_dir, autoupdate.UPDATE_FILE)
124 with open(update_gz, 'w') as fh:
125 fh.write('')
126
127 au_mock.GenerateUpdateImageWithCache(
Chris Sosaa387a872010-09-29 18:51:36128 self.forced_image_path,
Chris Sosa6a3697f2013-01-30 00:44:43129 static_image_dir=self.static_image_dir).AndReturn(None)
Gilad Arnold55a2a372012-10-02 16:46:32130 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-03 06:58:58131 self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 16:46:32132 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 18:51:36133 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 16:46:32134 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 22:46:22135 self.static_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa6a3697f2013-01-30 00:44:43136 au_mock._StoreMetadataToFile(self.static_image_dir,
137 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 23:35:58138 autoupdate_lib.GetUpdateResponse(
139 self.sha1, self.sha256, self.size, self.url, False, '3.0',
140 False).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 22:46:22141
142 self.mox.ReplayAll()
Chris Sosa0356d3b2010-09-16 22:46:22143 au_mock.forced_image = self.forced_image_path
144 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
145 self.mox.VerifyAll()
146
147 def testHandleUpdatePingForLatestImage(self):
148 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
Chris Sosa6a3697f2013-01-30 00:44:43149 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
150 au_mock = self._DummyAutoupdateConstructor()
Chris Sosa0356d3b2010-09-16 22:46:22151
152 test_data = _TEST_REQUEST % self.test_dict
153
Chris Sosa6a3697f2013-01-30 00:44:43154 # Generate a fake payload.
155 update_gz = os.path.join(self.static_image_dir, autoupdate.UPDATE_FILE)
156 with open(update_gz, 'w') as fh:
157 fh.write('')
158
159 au_mock.GenerateLatestUpdateImage(
160 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(None)
Gilad Arnold55a2a372012-10-02 16:46:32161 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-03 06:58:58162 self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 16:46:32163 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 18:51:36164 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 16:46:32165 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 22:46:22166 self.static_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa6a3697f2013-01-30 00:44:43167 au_mock._StoreMetadataToFile(self.static_image_dir,
168 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 23:35:58169 autoupdate_lib.GetUpdateResponse(
Chris Sosa6a3697f2013-01-30 00:44:43170 self.sha1, self.sha256, self.size, self.url, False, '3.0',
171 False).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 22:46:22172
173 self.mox.ReplayAll()
Chris Sosa0356d3b2010-09-16 22:46:22174 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Chris Sosa6a3697f2013-01-30 00:44:43175 curr_host_info = au_mock.host_infos.GetHostInfo('127.0.0.1')
Chris Sosa1885d032012-11-30 01:07:27176 self.assertEqual(curr_host_info.attrs['last_known_version'],
Gilad Arnold286a0062012-01-12 21:47:02177 'ForcedUpdate')
Chris Sosa1885d032012-11-30 01:07:27178 self.assertEqual(curr_host_info.attrs['last_event_type'],
Gilad Arnold286a0062012-01-12 21:47:02179 self.test_dict['event_type'])
Chris Sosa1885d032012-11-30 01:07:27180 self.assertEqual(curr_host_info.attrs['last_event_status'],
Gilad Arnold286a0062012-01-12 21:47:02181 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 22:46:22182 self.mox.VerifyAll()
183
Don Garrett0ad09372010-12-07 00:20:30184 def testChangeUrlPort(self):
185 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
186 self.assertEqual(r, 'http://fuzzy:8085/static')
187
188 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
189 self.assertEqual(r, 'http://fuzzy:8085/static')
190
191 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
192 self.assertEqual(r, 'ftp://fuzzy:8085/static')
193
194 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
195 self.assertEqual(r, 'ftp://fuzzy:8085')
196
Dale Curtisc9aaf3a2011-08-09 22:47:40197 def testHandleHostInfoPing(self):
198 au_mock = self._DummyAutoupdateConstructor()
199 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
200
Gilad Arnold286a0062012-01-12 21:47:02201 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 22:47:40202 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 21:47:02203 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 22:47:40204 self.assertEqual(
205 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
206
207 def testHandleSetUpdatePing(self):
208 au_mock = self._DummyAutoupdateConstructor()
209 test_ip = '1.2.3.4'
210 test_label = 'test/old-update'
211 self.assertRaises(
212 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
213 self.assertRaises(
214 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
215 self.assertRaises(
216 AssertionError, au_mock.HandleSetUpdatePing, None, None)
217
218 au_mock.HandleSetUpdatePing(test_ip, test_label)
219 self.assertEqual(
Chris Sosa1885d032012-11-30 01:07:27220 au_mock.host_infos.GetHostInfo(test_ip).attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 21:47:02221 test_label)
Dale Curtisc9aaf3a2011-08-09 22:47:40222
223 def testHandleUpdatePingWithSetUpdate(self):
224 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
Chris Sosa6a3697f2013-01-30 00:44:43225 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
226 au_mock = self._DummyAutoupdateConstructor()
Dale Curtisc9aaf3a2011-08-09 22:47:40227
228 test_data = _TEST_REQUEST % self.test_dict
229 test_label = 'new_update-test/the-new-update'
230 new_image_dir = os.path.join(self.static_image_dir, test_label)
231 new_url = self.url.replace('update.gz', test_label + '/update.gz')
232
Chris Sosa6a3697f2013-01-30 00:44:43233 au_mock.GenerateLatestUpdateImage(
234 self.test_board, 'ForcedUpdate', new_image_dir).AndReturn(None)
235
236 # Generate a fake payload.
237 os.makedirs(new_image_dir)
238 update_gz = os.path.join(new_image_dir, autoupdate.UPDATE_FILE)
239 with open(update_gz, 'w') as fh:
240 fh.write('')
241
Gilad Arnold55a2a372012-10-02 16:46:32242 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-03 06:58:58243 new_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 16:46:32244 common_util.GetFileSha256(os.path.join(
Dale Curtisc9aaf3a2011-08-09 22:47:40245 new_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 16:46:32246 common_util.GetFileSize(os.path.join(
Dale Curtisc9aaf3a2011-08-09 22:47:40247 new_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa6a3697f2013-01-30 00:44:43248 au_mock._StoreMetadataToFile(new_image_dir,
249 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 23:35:58250 autoupdate_lib.GetUpdateResponse(
251 self.sha1, self.sha256, self.size, new_url, False, '3.0',
252 False).AndReturn(self.payload)
Dale Curtisc9aaf3a2011-08-09 22:47:40253
254 self.mox.ReplayAll()
Dale Curtisc9aaf3a2011-08-09 22:47:40255 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
256 self.assertEqual(
Gilad Arnold286a0062012-01-12 21:47:02257 au_mock.host_infos.GetHostInfo('127.0.0.1').
Chris Sosa1885d032012-11-30 01:07:27258 attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 21:47:02259 test_label)
Dale Curtisc9aaf3a2011-08-09 22:47:40260 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 21:47:02261 self.assertFalse('forced_update_label' in
262 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 22:47:40263
Daniel Erat8a0bc4a2011-09-30 15:52:52264 def testGetVersionFromDir(self):
265 au = self._DummyAutoupdateConstructor()
266
267 # New-style version number.
268 self.assertEqual(
269 au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
270 '1102.0.2011_09_30_0806')
271
272 # Old-style version number.
273 self.assertEqual(
274 au._GetVersionFromDir('/foo/x86-alex/0.15.938.2011_08_23_0941-a1'),
275 '0.15.938.2011_08_23_0941')
276
277 def testCanUpdate(self):
278 au = self._DummyAutoupdateConstructor()
279
280 # When both the client and the server have new-style versions, we should
281 # just compare the tokens directly.
282 self.assertTrue(
283 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
284 self.assertTrue(
285 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
286 self.assertFalse(
287 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
288 self.assertFalse(
289 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
290
291 # When the device has an old four-token version number, we should skip the
292 # first two tokens and compare the rest. If there's a tie, go with the
293 # server's version.
294 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.1'))
295 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.0'))
296 self.assertFalse(au._CanUpdate('0.16.892.0', '890.0.0'))
297
298 # Test the case where both the client and the server have old-style
299 # versions.
300 self.assertTrue(au._CanUpdate('0.16.892.0', '0.16.892.1'))
301 self.assertFalse(au._CanUpdate('0.16.892.0', '0.16.892.0'))
302
Gilad Arnold0c9c8602012-10-03 06:58:58303 def testHandleUpdatePingRemotePayload(self):
304 remote_urlbase = 'http://remotehost:6666'
305 remote_payload_path = 'static/path/to/update.gz'
306 remote_url = '/'.join([remote_urlbase, remote_payload_path, 'update.gz'])
Chris Sosa6a3697f2013-01-30 00:44:43307 au_mock = self._DummyAutoupdateConstructor(urlbase=remote_urlbase,
308 payload_path=remote_payload_path,
309 remote_payload=True)
Gilad Arnold0c9c8602012-10-03 06:58:58310
311 test_data = _TEST_REQUEST % self.test_dict
312
Chris Sosa6a3697f2013-01-30 00:44:43313 au_mock._GetRemotePayloadAttrs(remote_url).AndReturn(
314 autoupdate.UpdateMetadata(self.sha1, self.sha256, self.size, False))
Chris Sosa52148582012-11-15 23:35:58315 autoupdate_lib.GetUpdateResponse(
316 self.sha1, self.sha256, self.size, remote_url, False,
317 '3.0', False).AndReturn(self.payload)
Gilad Arnold0c9c8602012-10-03 06:58:58318
319 self.mox.ReplayAll()
Gilad Arnold0c9c8602012-10-03 06:58:58320 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
321 self.mox.VerifyAll()
322
Chris Sosa0356d3b2010-09-16 22:46:22323
Gilad Arnoldc65330c2012-09-20 22:17:48324if __name__ == '__main__':
325 unittest.main()