blob: 453e0c94feec52a7860ba01d3b09aada70aaa700 [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 Sosa8dd80092012-12-10 13:39:11 -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
Chris Sosa0356d3b2010-09-16 15:46:22 -070021
Gilad Arnoldabb352e2012-09-23 01:24:27 -070022
Chris Sosa0356d3b2010-09-16 15:46:22 -070023_TEST_REQUEST = """
Chris Sosa52148582012-11-15 15:35:58 -080024<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 15:46:22 -070028</client_test>"""
29
Chris Sosa8dd80092012-12-10 13:39:11 -080030#pylint: disable=W0212
Chris Sosa0356d3b2010-09-16 15:46:22 -070031class AutoupdateTest(mox.MoxTestBase):
32 def setUp(self):
33 mox.MoxTestBase.setUp(self)
Gilad Arnold55a2a372012-10-02 09:46:32 -070034 self.mox.StubOutWithMock(common_util, 'GetFileSize')
35 self.mox.StubOutWithMock(common_util, 'GetFileSha1')
36 self.mox.StubOutWithMock(common_util, 'GetFileSha256')
Chris Sosa52148582012-11-15 15:35:58 -080037 self.mox.StubOutWithMock(autoupdate_lib, 'GetUpdateResponse')
Chris Sosa0356d3b2010-09-16 15:46:22 -070038 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
Gilad Arnold0c9c8602012-10-02 23:58:58 -070039 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs')
Chris Sosa7c931362010-10-11 19:49:01 -070040 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070041 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-11 19:49:01 -070046 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070047 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 15:46:22 -070055 self.test_data = _TEST_REQUEST % self.test_dict
56 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
Gilad Arnold0c9c8602012-10-02 23:58:58 -070057 self.sha1 = 12345
Chris Sosa0356d3b2010-09-16 15:46:22 -070058 self.size = 54321
59 self.url = 'http://%s/static/update.gz' % self.hostname
60 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070061 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070062 cherrypy.request.base = 'http://%s' % self.hostname
Chris Sosa8dd80092012-12-10 13:39:11 -080063 os.makedirs(self.static_image_dir)
64
65 def tearDown(self):
66 shutil.rmtree(self.static_image_dir)
Chris Sosa54555862010-10-25 17:26:17 -070067
Gilad Arnold0c9c8602012-10-02 23:58:58 -070068 def _DummyAutoupdateConstructor(self, **kwargs):
Chris Sosa0356d3b2010-09-16 15:46:22 -070069 """Creates a dummy autoupdater. Used to avoid using constructor."""
70 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070071 static_dir=self.static_image_dir,
Gilad Arnold0c9c8602012-10-02 23:58:58 -070072 **kwargs)
Chris Sosa0356d3b2010-09-16 15:46:22 -070073 return dummy
74
Chris Sosa744e1472011-09-07 19:32:50 -070075 def testGetRightSignedDeltaPayloadDir(self):
76 """Test that our directory is what we expect it to be for signed updates."""
Gilad Arnold55a2a372012-10-02 09:46:32 -070077 self.mox.StubOutWithMock(common_util, 'GetFileMd5')
Chris Sosa744e1472011-09-07 19:32:50 -070078 key_path = 'test_key_path'
79 src_image = 'test_src_image'
80 target_image = 'test_target_image'
Gilad Arnold55a2a372012-10-02 09:46:32 -070081 src_hash = '12345'
82 target_hash = '67890'
83 key_hash = 'abcde'
Chris Sosa744e1472011-09-07 19:32:50 -070084
Gilad Arnold55a2a372012-10-02 09:46:32 -070085 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-07 19:32:50 -070088
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 15:31:36 -040093 self.assertEqual(os.path.basename(update_dir),
Gilad Arnold55a2a372012-10-02 09:46:32 -070094 '%s_%s+%s+patched_kernel' %
95 (src_hash, target_hash, key_hash))
Chris Sosa744e1472011-09-07 19:32:50 -070096 self.mox.VerifyAll()
97
Chris Sosa0356d3b2010-09-16 15:46:22 -070098 def testGenerateLatestUpdateImageWithForced(self):
Don Garrettf90edf02010-11-16 17:36:14 -080099 self.mox.StubOutWithMock(autoupdate.Autoupdate,
100 'GenerateUpdateImageWithCache')
Chris Sosa8dd80092012-12-10 13:39:11 -0800101 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-16 17:36:14 -0800107 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700108
109 self.mox.ReplayAll()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700110 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-16 17:36:14 -0800116 self.mox.StubOutWithMock(autoupdate.Autoupdate,
117 'GenerateUpdateImageWithCache')
Chris Sosa8dd80092012-12-10 13:39:11 -0800118 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
119 au_mock = self._DummyAutoupdateConstructor()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700120 test_data = _TEST_REQUEST % self.test_dict
121
Chris Sosa8dd80092012-12-10 13:39:11 -0800122 # 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 11:51:36 -0700128 self.forced_image_path,
Chris Sosa8dd80092012-12-10 13:39:11 -0800129 static_image_dir=self.static_image_dir).AndReturn(None)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700130 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700131 self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700132 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 11:51:36 -0700133 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700134 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700135 self.static_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa8dd80092012-12-10 13:39:11 -0800136 au_mock._StoreMetadataToFile(self.static_image_dir,
137 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 15:35:58 -0800138 autoupdate_lib.GetUpdateResponse(
139 self.sha1, self.sha256, self.size, self.url, False, '3.0',
140 False).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700141
142 self.mox.ReplayAll()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700143 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 Sosa8dd80092012-12-10 13:39:11 -0800149 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
150 au_mock = self._DummyAutoupdateConstructor()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700151
152 test_data = _TEST_REQUEST % self.test_dict
153
Chris Sosa8dd80092012-12-10 13:39:11 -0800154 # 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 09:46:32 -0700161 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700162 self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700163 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 11:51:36 -0700164 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700165 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700166 self.static_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa8dd80092012-12-10 13:39:11 -0800167 au_mock._StoreMetadataToFile(self.static_image_dir,
168 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 15:35:58 -0800169 autoupdate_lib.GetUpdateResponse(
Chris Sosa8dd80092012-12-10 13:39:11 -0800170 self.sha1, self.sha256, self.size, self.url, False, '3.0',
171 False).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700172
173 self.mox.ReplayAll()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700174 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Chris Sosa8dd80092012-12-10 13:39:11 -0800175 curr_host_info = au_mock.host_infos.GetHostInfo('127.0.0.1')
Chris Sosa1885d032012-11-29 17:07:27 -0800176 self.assertEqual(curr_host_info.attrs['last_known_version'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800177 'ForcedUpdate')
Chris Sosa1885d032012-11-29 17:07:27 -0800178 self.assertEqual(curr_host_info.attrs['last_event_type'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800179 self.test_dict['event_type'])
Chris Sosa1885d032012-11-29 17:07:27 -0800180 self.assertEqual(curr_host_info.attrs['last_event_status'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800181 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 15:46:22 -0700182 self.mox.VerifyAll()
183
Don Garrett0ad09372010-12-06 16:20:30 -0800184 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 15:47:40 -0700197 def testHandleHostInfoPing(self):
198 au_mock = self._DummyAutoupdateConstructor()
199 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
200
Gilad Arnold286a0062012-01-12 13:47:02 -0800201 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700202 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -0800203 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700204 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-29 17:07:27 -0800220 au_mock.host_infos.GetHostInfo(test_ip).attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800221 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700222
223 def testHandleUpdatePingWithSetUpdate(self):
224 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
Chris Sosa8dd80092012-12-10 13:39:11 -0800225 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
226 au_mock = self._DummyAutoupdateConstructor()
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700227
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 Sosa8dd80092012-12-10 13:39:11 -0800233 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 09:46:32 -0700242 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700243 new_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700244 common_util.GetFileSha256(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700245 new_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700246 common_util.GetFileSize(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700247 new_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa8dd80092012-12-10 13:39:11 -0800248 au_mock._StoreMetadataToFile(new_image_dir,
249 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 15:35:58 -0800250 autoupdate_lib.GetUpdateResponse(
251 self.sha1, self.sha256, self.size, new_url, False, '3.0',
252 False).AndReturn(self.payload)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700253
254 self.mox.ReplayAll()
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700255 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
256 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800257 au_mock.host_infos.GetHostInfo('127.0.0.1').
Chris Sosa1885d032012-11-29 17:07:27 -0800258 attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800259 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700260 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800261 self.assertFalse('forced_update_label' in
262 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700263
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700264 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-02 23:58:58 -0700303 def testHandleUpdatePingRemotePayload(self):
Chris Sosa8dd80092012-12-10 13:39:11 -0800304 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs')
305
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700306 remote_urlbase = 'http://remotehost:6666'
307 remote_payload_path = 'static/path/to/update.gz'
308 remote_url = '/'.join([remote_urlbase, remote_payload_path, 'update.gz'])
Chris Sosa8dd80092012-12-10 13:39:11 -0800309 au_mock = self._DummyAutoupdateConstructor(urlbase=remote_urlbase,
310 payload_path=remote_payload_path,
311 remote_payload=True)
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700312
313 test_data = _TEST_REQUEST % self.test_dict
314
Chris Sosa8dd80092012-12-10 13:39:11 -0800315 au_mock._GetRemotePayloadAttrs(remote_url).AndReturn(
316 autoupdate.UpdateMetadata(self.sha1, self.sha256, self.size, False))
Chris Sosa52148582012-11-15 15:35:58 -0800317 autoupdate_lib.GetUpdateResponse(
318 self.sha1, self.sha256, self.size, remote_url, False,
319 '3.0', False).AndReturn(self.payload)
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700320
321 self.mox.ReplayAll()
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700322 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
323 self.mox.VerifyAll()
324
Chris Sosa0356d3b2010-09-16 15:46:22 -0700325
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700326if __name__ == '__main__':
327 unittest.main()