blob: decd3f528797a3d9d1185aa7a7eca5ed5ba53545 [file] [log] [blame]
Chris Sosa0356d3b2010-09-16 15:46:22 -07001#!/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 15:47:40 -07009import json
Chris Sosa0356d3b2010-09-16 15:46:22 -070010import os
Chris Sosa6a3697f2013-01-29 16:44:43 -080011import shutil
Chris Sosa7c931362010-10-11 19:49:01 -070012import socket
Chris Sosa0356d3b2010-09-16 15:46:22 -070013import unittest
Chris Sosa0356d3b2010-09-16 15:46:22 -070014
Gilad Arnoldabb352e2012-09-23 01:24:27 -070015import cherrypy
16import mox
17
Chris Sosa0356d3b2010-09-16 15:46:22 -070018import autoupdate
Chris Sosa52148582012-11-15 15:35:58 -080019import autoupdate_lib
Gilad Arnold55a2a372012-10-02 09:46:32 -070020import common_util
joychen7c2054a2013-07-25 11:14:07 -070021import devserver_constants as constants
Chris Sosa0356d3b2010-09-16 15:46:22 -070022
Gilad Arnoldabb352e2012-09-23 01:24:27 -070023
Chris Sosa0356d3b2010-09-16 15:46:22 -070024_TEST_REQUEST = """
Chris Sosa52148582012-11-15 15:35:58 -080025<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" protocol="3.0">
26 <app version="%(version)s" track="%(track)s" board="%(board)s" />
27 <updatecheck />
28 <event eventresult="%(event_result)d" eventtype="%(event_type)d" />
Chris Sosa0356d3b2010-09-16 15:46:22 -070029</client_test>"""
30
Chris Sosa6a3697f2013-01-29 16:44:43 -080031#pylint: disable=W0212
Chris Sosa0356d3b2010-09-16 15:46:22 -070032class AutoupdateTest(mox.MoxTestBase):
33 def setUp(self):
34 mox.MoxTestBase.setUp(self)
Gilad Arnold55a2a372012-10-02 09:46:32 -070035 self.mox.StubOutWithMock(common_util, 'GetFileSize')
36 self.mox.StubOutWithMock(common_util, 'GetFileSha1')
37 self.mox.StubOutWithMock(common_util, 'GetFileSha256')
Chris Sosa52148582012-11-15 15:35:58 -080038 self.mox.StubOutWithMock(autoupdate_lib, 'GetUpdateResponse')
joychen921e1fb2013-06-28 11:12:20 -070039 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetLatestImageDir')
Gilad Arnold0c9c8602012-10-02 23:58:58 -070040 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs')
Chris Sosa7c931362010-10-11 19:49:01 -070041 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070042 self.test_board = 'test-board'
43 self.build_root = '/src_path/build/images'
44 self.latest_dir = '12345_af_12-a1'
45 self.latest_verision = '12345_af_12'
46 self.static_image_dir = '/tmp/static-dir/'
Chris Sosa7c931362010-10-11 19:49:01 -070047 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070048 self.test_dict = {
49 'client': 'ChromeOSUpdateEngine-1.0',
50 'version': 'ForcedUpdate',
51 'track': 'unused_var',
52 'board': self.test_board,
53 'event_result': 2,
54 'event_type': 3
55 }
Chris Sosa0356d3b2010-09-16 15:46:22 -070056 self.test_data = _TEST_REQUEST % self.test_dict
57 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
Gilad Arnold0c9c8602012-10-02 23:58:58 -070058 self.sha1 = 12345
Chris Sosa0356d3b2010-09-16 15:46:22 -070059 self.size = 54321
60 self.url = 'http://%s/static/update.gz' % self.hostname
61 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070062 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070063 cherrypy.request.base = 'http://%s' % self.hostname
Chris Sosa6a3697f2013-01-29 16:44:43 -080064 os.makedirs(self.static_image_dir)
65
66 def tearDown(self):
67 shutil.rmtree(self.static_image_dir)
Chris Sosa54555862010-10-25 17:26:17 -070068
Gilad Arnold0c9c8602012-10-02 23:58:58 -070069 def _DummyAutoupdateConstructor(self, **kwargs):
Chris Sosa0356d3b2010-09-16 15:46:22 -070070 """Creates a dummy autoupdater. Used to avoid using constructor."""
71 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070072 static_dir=self.static_image_dir,
Gilad Arnold0c9c8602012-10-02 23:58:58 -070073 **kwargs)
Chris Sosa0356d3b2010-09-16 15:46:22 -070074 return dummy
75
Chris Sosa744e1472011-09-07 19:32:50 -070076 def testGetRightSignedDeltaPayloadDir(self):
77 """Test that our directory is what we expect it to be for signed updates."""
Gilad Arnold55a2a372012-10-02 09:46:32 -070078 self.mox.StubOutWithMock(common_util, 'GetFileMd5')
Chris Sosa744e1472011-09-07 19:32:50 -070079 key_path = 'test_key_path'
80 src_image = 'test_src_image'
81 target_image = 'test_target_image'
Gilad Arnold55a2a372012-10-02 09:46:32 -070082 src_hash = '12345'
83 target_hash = '67890'
84 key_hash = 'abcde'
Chris Sosa744e1472011-09-07 19:32:50 -070085
Gilad Arnold55a2a372012-10-02 09:46:32 -070086 common_util.GetFileMd5(src_image).AndReturn(src_hash)
87 common_util.GetFileMd5(target_image).AndReturn(target_hash)
88 common_util.GetFileMd5(key_path).AndReturn(key_hash)
Chris Sosa744e1472011-09-07 19:32:50 -070089
90 self.mox.ReplayAll()
91 au_mock = self._DummyAutoupdateConstructor()
92 au_mock.private_key = key_path
93 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 15:31:36 -040094 self.assertEqual(os.path.basename(update_dir),
Gilad Arnold55a2a372012-10-02 09:46:32 -070095 '%s_%s+%s+patched_kernel' %
96 (src_hash, target_hash, key_hash))
Chris Sosa744e1472011-09-07 19:32:50 -070097 self.mox.VerifyAll()
98
Chris Sosa0356d3b2010-09-16 15:46:22 -070099 def testGenerateLatestUpdateImageWithForced(self):
Don Garrettf90edf02010-11-16 17:36:14 -0800100 self.mox.StubOutWithMock(autoupdate.Autoupdate,
101 'GenerateUpdateImageWithCache')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800102 au_mock = self._DummyAutoupdateConstructor()
joychen921e1fb2013-06-28 11:12:20 -0700103 au_mock.GetLatestImageDir(self.test_board).AndReturn(
Chris Sosa6a3697f2013-01-29 16:44:43 -0800104 os.path.join(self.build_root, self.test_board, self.latest_dir))
105 au_mock.GenerateUpdateImageWithCache(
106 os.path.join(self.build_root, self.test_board, self.latest_dir,
107 'chromiumos_image.bin'),
Don Garrettf90edf02010-11-16 17:36:14 -0800108 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700109
110 self.mox.ReplayAll()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700111 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
112 'ForcedUpdate',
113 self.static_image_dir))
114 self.mox.VerifyAll()
115
116 def testHandleUpdatePingForForcedImage(self):
Don Garrettf90edf02010-11-16 17:36:14 -0800117 self.mox.StubOutWithMock(autoupdate.Autoupdate,
118 'GenerateUpdateImageWithCache')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800119 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
120 au_mock = self._DummyAutoupdateConstructor()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700121 test_data = _TEST_REQUEST % self.test_dict
122
Chris Sosa6a3697f2013-01-29 16:44:43 -0800123 # Generate a fake payload.
joychen7c2054a2013-07-25 11:14:07 -0700124 update_gz = os.path.join(self.static_image_dir, constants.UPDATE_FILE)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800125 with open(update_gz, 'w') as fh:
126 fh.write('')
127
128 au_mock.GenerateUpdateImageWithCache(
Chris Sosaa387a872010-09-29 11:51:36 -0700129 self.forced_image_path,
Chris Sosa6a3697f2013-01-29 16:44:43 -0800130 static_image_dir=self.static_image_dir).AndReturn(None)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700131 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700132 self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700133 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 11:51:36 -0700134 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700135 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700136 self.static_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800137 au_mock._StoreMetadataToFile(self.static_image_dir,
138 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 15:35:58 -0800139 autoupdate_lib.GetUpdateResponse(
140 self.sha1, self.sha256, self.size, self.url, False, '3.0',
141 False).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700142
143 self.mox.ReplayAll()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700144 au_mock.forced_image = self.forced_image_path
145 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
146 self.mox.VerifyAll()
147
148 def testHandleUpdatePingForLatestImage(self):
149 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800150 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
151 au_mock = self._DummyAutoupdateConstructor()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700152
153 test_data = _TEST_REQUEST % self.test_dict
154
Chris Sosa6a3697f2013-01-29 16:44:43 -0800155 # Generate a fake payload.
joychen7c2054a2013-07-25 11:14:07 -0700156 update_gz = os.path.join(self.static_image_dir, constants.UPDATE_FILE)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800157 with open(update_gz, 'w') as fh:
158 fh.write('')
159
160 au_mock.GenerateLatestUpdateImage(
161 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(None)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700162 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700163 self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700164 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 11:51:36 -0700165 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700166 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700167 self.static_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800168 au_mock._StoreMetadataToFile(self.static_image_dir,
169 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 15:35:58 -0800170 autoupdate_lib.GetUpdateResponse(
Chris Sosa6a3697f2013-01-29 16:44:43 -0800171 self.sha1, self.sha256, self.size, self.url, False, '3.0',
172 False).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700173
174 self.mox.ReplayAll()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700175 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800176 curr_host_info = au_mock.host_infos.GetHostInfo('127.0.0.1')
Chris Sosa1885d032012-11-29 17:07:27 -0800177 self.assertEqual(curr_host_info.attrs['last_known_version'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800178 'ForcedUpdate')
Chris Sosa1885d032012-11-29 17:07:27 -0800179 self.assertEqual(curr_host_info.attrs['last_event_type'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800180 self.test_dict['event_type'])
Chris Sosa1885d032012-11-29 17:07:27 -0800181 self.assertEqual(curr_host_info.attrs['last_event_status'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800182 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 15:46:22 -0700183 self.mox.VerifyAll()
184
Don Garrett0ad09372010-12-06 16:20:30 -0800185 def testChangeUrlPort(self):
186 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
187 self.assertEqual(r, 'http://fuzzy:8085/static')
188
189 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
190 self.assertEqual(r, 'http://fuzzy:8085/static')
191
192 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
193 self.assertEqual(r, 'ftp://fuzzy:8085/static')
194
195 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
196 self.assertEqual(r, 'ftp://fuzzy:8085')
197
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700198 def testHandleHostInfoPing(self):
199 au_mock = self._DummyAutoupdateConstructor()
200 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
201
Gilad Arnold286a0062012-01-12 13:47:02 -0800202 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700203 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -0800204 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700205 self.assertEqual(
206 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
207
208 def testHandleSetUpdatePing(self):
209 au_mock = self._DummyAutoupdateConstructor()
210 test_ip = '1.2.3.4'
211 test_label = 'test/old-update'
212 self.assertRaises(
213 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
214 self.assertRaises(
215 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
216 self.assertRaises(
217 AssertionError, au_mock.HandleSetUpdatePing, None, None)
218
219 au_mock.HandleSetUpdatePing(test_ip, test_label)
220 self.assertEqual(
Chris Sosa1885d032012-11-29 17:07:27 -0800221 au_mock.host_infos.GetHostInfo(test_ip).attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800222 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700223
224 def testHandleUpdatePingWithSetUpdate(self):
joychen7c2054a2013-07-25 11:14:07 -0700225 """If update is set, it should use the update found in that directory."""
Chris Sosa6a3697f2013-01-29 16:44:43 -0800226 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
227 au_mock = self._DummyAutoupdateConstructor()
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700228
229 test_data = _TEST_REQUEST % self.test_dict
230 test_label = 'new_update-test/the-new-update'
231 new_image_dir = os.path.join(self.static_image_dir, test_label)
232 new_url = self.url.replace('update.gz', test_label + '/update.gz')
233
Chris Sosa6a3697f2013-01-29 16:44:43 -0800234 # Generate a fake payload.
235 os.makedirs(new_image_dir)
joychen7c2054a2013-07-25 11:14:07 -0700236 update_gz = os.path.join(new_image_dir, constants.UPDATE_FILE)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800237 with open(update_gz, 'w') as fh:
238 fh.write('')
239
Gilad Arnold55a2a372012-10-02 09:46:32 -0700240 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700241 new_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700242 common_util.GetFileSha256(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700243 new_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700244 common_util.GetFileSize(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700245 new_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800246 au_mock._StoreMetadataToFile(new_image_dir,
247 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 15:35:58 -0800248 autoupdate_lib.GetUpdateResponse(
249 self.sha1, self.sha256, self.size, new_url, False, '3.0',
250 False).AndReturn(self.payload)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700251
252 self.mox.ReplayAll()
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700253 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
254 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800255 au_mock.host_infos.GetHostInfo('127.0.0.1').
Chris Sosa1885d032012-11-29 17:07:27 -0800256 attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800257 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700258 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800259 self.assertFalse('forced_update_label' in
260 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700261
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700262 def testGetVersionFromDir(self):
263 au = self._DummyAutoupdateConstructor()
264
265 # New-style version number.
266 self.assertEqual(
267 au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
268 '1102.0.2011_09_30_0806')
269
270 # Old-style version number.
271 self.assertEqual(
272 au._GetVersionFromDir('/foo/x86-alex/0.15.938.2011_08_23_0941-a1'),
273 '0.15.938.2011_08_23_0941')
274
275 def testCanUpdate(self):
276 au = self._DummyAutoupdateConstructor()
277
278 # When both the client and the server have new-style versions, we should
279 # just compare the tokens directly.
280 self.assertTrue(
281 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
282 self.assertTrue(
283 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
284 self.assertFalse(
285 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
286 self.assertFalse(
287 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
288
289 # When the device has an old four-token version number, we should skip the
290 # first two tokens and compare the rest. If there's a tie, go with the
291 # server's version.
292 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.1'))
293 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.0'))
294 self.assertFalse(au._CanUpdate('0.16.892.0', '890.0.0'))
295
296 # Test the case where both the client and the server have old-style
297 # versions.
298 self.assertTrue(au._CanUpdate('0.16.892.0', '0.16.892.1'))
299 self.assertFalse(au._CanUpdate('0.16.892.0', '0.16.892.0'))
300
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700301 def testHandleUpdatePingRemotePayload(self):
302 remote_urlbase = 'http://remotehost:6666'
303 remote_payload_path = 'static/path/to/update.gz'
304 remote_url = '/'.join([remote_urlbase, remote_payload_path, 'update.gz'])
Chris Sosa6a3697f2013-01-29 16:44:43 -0800305 au_mock = self._DummyAutoupdateConstructor(urlbase=remote_urlbase,
306 payload_path=remote_payload_path,
307 remote_payload=True)
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700308
309 test_data = _TEST_REQUEST % self.test_dict
310
Chris Sosa6a3697f2013-01-29 16:44:43 -0800311 au_mock._GetRemotePayloadAttrs(remote_url).AndReturn(
312 autoupdate.UpdateMetadata(self.sha1, self.sha256, self.size, False))
Chris Sosa52148582012-11-15 15:35:58 -0800313 autoupdate_lib.GetUpdateResponse(
314 self.sha1, self.sha256, self.size, remote_url, False,
315 '3.0', False).AndReturn(self.payload)
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700316
317 self.mox.ReplayAll()
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700318 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
319 self.mox.VerifyAll()
320
Chris Sosa0356d3b2010-09-16 15:46:22 -0700321
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700322if __name__ == '__main__':
323 unittest.main()