blob: 2917fd2b65414fe7c3da63b073454eac24c8e082 [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
joychen121fc9b2013-08-02 14:30:30 -070013import tempfile
Chris Sosa0356d3b2010-09-16 15:46:22 -070014import unittest
Chris Sosa0356d3b2010-09-16 15:46:22 -070015
Gilad Arnoldabb352e2012-09-23 01:24:27 -070016import cherrypy
17import mox
18
Chris Sosa0356d3b2010-09-16 15:46:22 -070019import autoupdate
Chris Sosa52148582012-11-15 15:35:58 -080020import autoupdate_lib
Gilad Arnold55a2a372012-10-02 09:46:32 -070021import common_util
joychen7c2054a2013-07-25 11:14:07 -070022import devserver_constants as constants
joychen121fc9b2013-08-02 14:30:30 -070023import xbuddy
Chris Sosa0356d3b2010-09-16 15:46:22 -070024
Gilad Arnoldabb352e2012-09-23 01:24:27 -070025
Chris Sosa0356d3b2010-09-16 15:46:22 -070026_TEST_REQUEST = """
Chris Sosa52148582012-11-15 15:35:58 -080027<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" protocol="3.0">
28 <app version="%(version)s" track="%(track)s" board="%(board)s" />
29 <updatecheck />
30 <event eventresult="%(event_result)d" eventtype="%(event_type)d" />
Chris Sosa0356d3b2010-09-16 15:46:22 -070031</client_test>"""
32
Chris Sosa6a3697f2013-01-29 16:44:43 -080033#pylint: disable=W0212
Chris Sosa0356d3b2010-09-16 15:46:22 -070034class AutoupdateTest(mox.MoxTestBase):
35 def setUp(self):
36 mox.MoxTestBase.setUp(self)
Gilad Arnold55a2a372012-10-02 09:46:32 -070037 self.mox.StubOutWithMock(common_util, 'GetFileSize')
38 self.mox.StubOutWithMock(common_util, 'GetFileSha1')
39 self.mox.StubOutWithMock(common_util, 'GetFileSha256')
Chris Sosac4e87842013-08-16 18:04:14 -070040 self.mox.StubOutWithMock(common_util, 'IsInsideChroot')
Chris Sosa52148582012-11-15 15:35:58 -080041 self.mox.StubOutWithMock(autoupdate_lib, 'GetUpdateResponse')
Gilad Arnold0c9c8602012-10-02 23:58:58 -070042 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs')
Chris Sosa7c931362010-10-11 19:49:01 -070043 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070044 self.test_board = 'test-board'
joychen121fc9b2013-08-02 14:30:30 -070045 self.build_root = tempfile.mkdtemp('autoupdate_build_root')
Chris Sosa0356d3b2010-09-16 15:46:22 -070046 self.latest_dir = '12345_af_12-a1'
47 self.latest_verision = '12345_af_12'
joychen121fc9b2013-08-02 14:30:30 -070048 self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir')
Chris Sosa7c931362010-10-11 19:49:01 -070049 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070050 self.test_dict = {
51 'client': 'ChromeOSUpdateEngine-1.0',
52 'version': 'ForcedUpdate',
53 'track': 'unused_var',
54 'board': self.test_board,
55 'event_result': 2,
56 'event_type': 3
57 }
Chris Sosa0356d3b2010-09-16 15:46:22 -070058 self.test_data = _TEST_REQUEST % self.test_dict
Gilad Arnold0c9c8602012-10-02 23:58:58 -070059 self.sha1 = 12345
Chris Sosa0356d3b2010-09-16 15:46:22 -070060 self.size = 54321
61 self.url = 'http://%s/static/update.gz' % self.hostname
62 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070063 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070064 cherrypy.request.base = 'http://%s' % self.hostname
joychen121fc9b2013-08-02 14:30:30 -070065 common_util.MkDirP(self.static_image_dir)
66 self._xbuddy = xbuddy.XBuddy(False,
67 root_dir=None,
68 static_dir=self.static_image_dir)
69 self.mox.StubOutWithMock(xbuddy.XBuddy, '_GetArtifact')
Chris Sosa6a3697f2013-01-29 16:44:43 -080070
71 def tearDown(self):
joychen121fc9b2013-08-02 14:30:30 -070072 shutil.rmtree(self.build_root)
Chris Sosa6a3697f2013-01-29 16:44:43 -080073 shutil.rmtree(self.static_image_dir)
Chris Sosa54555862010-10-25 17:26:17 -070074
Gilad Arnold0c9c8602012-10-02 23:58:58 -070075 def _DummyAutoupdateConstructor(self, **kwargs):
Chris Sosa0356d3b2010-09-16 15:46:22 -070076 """Creates a dummy autoupdater. Used to avoid using constructor."""
joychen121fc9b2013-08-02 14:30:30 -070077 dummy = autoupdate.Autoupdate(self._xbuddy,
78 root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070079 static_dir=self.static_image_dir,
Gilad Arnold0c9c8602012-10-02 23:58:58 -070080 **kwargs)
Chris Sosa0356d3b2010-09-16 15:46:22 -070081 return dummy
82
Chris Sosa744e1472011-09-07 19:32:50 -070083 def testGetRightSignedDeltaPayloadDir(self):
84 """Test that our directory is what we expect it to be for signed updates."""
Gilad Arnold55a2a372012-10-02 09:46:32 -070085 self.mox.StubOutWithMock(common_util, 'GetFileMd5')
Chris Sosa744e1472011-09-07 19:32:50 -070086 key_path = 'test_key_path'
87 src_image = 'test_src_image'
88 target_image = 'test_target_image'
Gilad Arnold55a2a372012-10-02 09:46:32 -070089 src_hash = '12345'
90 target_hash = '67890'
91 key_hash = 'abcde'
Chris Sosa744e1472011-09-07 19:32:50 -070092
Gilad Arnold55a2a372012-10-02 09:46:32 -070093 common_util.GetFileMd5(src_image).AndReturn(src_hash)
94 common_util.GetFileMd5(target_image).AndReturn(target_hash)
95 common_util.GetFileMd5(key_path).AndReturn(key_hash)
Chris Sosa744e1472011-09-07 19:32:50 -070096
97 self.mox.ReplayAll()
98 au_mock = self._DummyAutoupdateConstructor()
99 au_mock.private_key = key_path
100 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 15:31:36 -0400101 self.assertEqual(os.path.basename(update_dir),
Gilad Arnold55a2a372012-10-02 09:46:32 -0700102 '%s_%s+%s+patched_kernel' %
103 (src_hash, target_hash, key_hash))
Chris Sosa744e1472011-09-07 19:32:50 -0700104 self.mox.VerifyAll()
105
joychen121fc9b2013-08-02 14:30:30 -0700106 def testGenerateLatestUpdateImage(self):
107 """Test default behavior in response to plain update call."""
108 latest_label = os.path.join(self.test_board, self.latest_dir)
109 # Generate a fake latest image
110 latest_image_dir = os.path.join(self.static_image_dir, latest_label)
111 common_util.MkDirP(latest_image_dir)
112 image = os.path.join(latest_image_dir, constants.TEST_IMAGE_FILE)
113 with open(image, 'w') as fh:
114 fh.write('')
115
Don Garrettf90edf02010-11-16 17:36:14 -0800116 self.mox.StubOutWithMock(autoupdate.Autoupdate,
117 'GenerateUpdateImageWithCache')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800118 au_mock = self._DummyAutoupdateConstructor()
joychen121fc9b2013-08-02 14:30:30 -0700119
Chris Sosac4e87842013-08-16 18:04:14 -0700120 common_util.IsInsideChroot().AndReturn(True)
joychen121fc9b2013-08-02 14:30:30 -0700121 self._xbuddy._GetArtifact(
122 [''], self.test_board, lookup_only=True).AndReturn(
123 (latest_label, constants.TEST_IMAGE_FILE))
124
Chris Sosa6a3697f2013-01-29 16:44:43 -0800125 au_mock.GenerateUpdateImageWithCache(
joychen121fc9b2013-08-02 14:30:30 -0700126 os.path.join(self.static_image_dir, self.test_board, self.latest_dir,
127 constants.TEST_IMAGE_FILE),
128 static_image_dir=latest_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700129
130 self.mox.ReplayAll()
joychen121fc9b2013-08-02 14:30:30 -0700131 test_data = _TEST_REQUEST % self.test_dict
132 self.assertTrue(au_mock.HandleUpdatePing(test_data))
Chris Sosa0356d3b2010-09-16 15:46:22 -0700133 self.mox.VerifyAll()
134
135 def testHandleUpdatePingForForcedImage(self):
joychen121fc9b2013-08-02 14:30:30 -0700136 """Test update response to having a forced image."""
Don Garrettf90edf02010-11-16 17:36:14 -0800137 self.mox.StubOutWithMock(autoupdate.Autoupdate,
138 'GenerateUpdateImageWithCache')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800139 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
140 au_mock = self._DummyAutoupdateConstructor()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700141 test_data = _TEST_REQUEST % self.test_dict
142
joychen121fc9b2013-08-02 14:30:30 -0700143 # Generate a fake image
144 forced_image_dir = '/tmp/path_to_force/'
145 forced_image = forced_image_dir + constants.IMAGE_FILE
146 common_util.MkDirP(forced_image_dir)
147 with open(forced_image, 'w') as fh:
Chris Sosa6a3697f2013-01-29 16:44:43 -0800148 fh.write('')
149
joychen18737f32013-08-16 17:18:12 -0700150 cache_image_dir = os.path.join(self.static_image_dir, 'cache')
joychen121fc9b2013-08-02 14:30:30 -0700151
152 # Mock out GenerateUpdateImageWithCache to make an update file in cache
153 def mock_fn(_image, static_image_dir):
154 print 'mock_fn'
155 # No good way to introduce an update file during execution.
156 cache_dir = os.path.join(static_image_dir, 'cache')
157 common_util.MkDirP(cache_dir)
158 update_image = os.path.join(cache_dir, constants.UPDATE_FILE)
159 with open(update_image, 'w') as fh:
160 fh.write('')
161
Chris Sosac4e87842013-08-16 18:04:14 -0700162 common_util.IsInsideChroot().AndReturn(True)
joychen18737f32013-08-16 17:18:12 -0700163 au_mock.GenerateUpdateImageWithCache(forced_image,
164 static_image_dir=self.static_image_dir).WithSideEffects(
joychen121fc9b2013-08-02 14:30:30 -0700165 mock_fn).AndReturn('cache')
166
Gilad Arnold55a2a372012-10-02 09:46:32 -0700167 common_util.GetFileSha1(os.path.join(
joychen121fc9b2013-08-02 14:30:30 -0700168 cache_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700169 common_util.GetFileSha256(os.path.join(
joychen121fc9b2013-08-02 14:30:30 -0700170 cache_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700171 common_util.GetFileSize(os.path.join(
joychen121fc9b2013-08-02 14:30:30 -0700172 cache_image_dir, 'update.gz')).AndReturn(self.size)
173 au_mock._StoreMetadataToFile(cache_image_dir,
Chris Sosa6a3697f2013-01-29 16:44:43 -0800174 mox.IsA(autoupdate.UpdateMetadata))
joychen121fc9b2013-08-02 14:30:30 -0700175 forced_url = 'http://%s/static/%s/update.gz' % (self.hostname,
joychen18737f32013-08-16 17:18:12 -0700176 'cache')
Chris Sosa52148582012-11-15 15:35:58 -0800177 autoupdate_lib.GetUpdateResponse(
joychen121fc9b2013-08-02 14:30:30 -0700178 self.sha1, self.sha256, self.size, forced_url, False, '3.0',
Chris Sosa52148582012-11-15 15:35:58 -0800179 False).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700180
181 self.mox.ReplayAll()
joychen121fc9b2013-08-02 14:30:30 -0700182 au_mock.forced_image = forced_image
Chris Sosa0356d3b2010-09-16 15:46:22 -0700183 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
184 self.mox.VerifyAll()
185
joychendbfe6c92013-08-16 20:03:49 -0700186 def testHandleForcePregenerateXBuddy(self):
187 """Check pregenerating an xbuddy path.
188
189 A forced image that starts with 'xbuddy:' uses the following path to
190 obtain an update.
191 """
192 self.mox.StubOutWithMock(autoupdate.Autoupdate,
193 'GetUpdateForLabel')
194 au_mock = self._DummyAutoupdateConstructor()
195 au_mock.forced_image = "xbuddy:b/v/a"
196
197 au_mock.GetUpdateForLabel(
198 autoupdate.FORCED_UPDATE, 'b/v/a').AndReturn('p')
199 self.mox.ReplayAll()
200
201 au_mock.PreGenerateUpdate()
202 self.mox.VerifyAll()
203
Don Garrett0ad09372010-12-06 16:20:30 -0800204 def testChangeUrlPort(self):
205 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
206 self.assertEqual(r, 'http://fuzzy:8085/static')
207
208 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
209 self.assertEqual(r, 'http://fuzzy:8085/static')
210
211 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
212 self.assertEqual(r, 'ftp://fuzzy:8085/static')
213
214 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
215 self.assertEqual(r, 'ftp://fuzzy:8085')
216
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700217 def testHandleHostInfoPing(self):
218 au_mock = self._DummyAutoupdateConstructor()
219 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
220
Gilad Arnold286a0062012-01-12 13:47:02 -0800221 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700222 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -0800223 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700224 self.assertEqual(
225 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
226
227 def testHandleSetUpdatePing(self):
228 au_mock = self._DummyAutoupdateConstructor()
229 test_ip = '1.2.3.4'
230 test_label = 'test/old-update'
231 self.assertRaises(
232 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
233 self.assertRaises(
234 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
235 self.assertRaises(
236 AssertionError, au_mock.HandleSetUpdatePing, None, None)
237
238 au_mock.HandleSetUpdatePing(test_ip, test_label)
239 self.assertEqual(
Chris Sosa1885d032012-11-29 17:07:27 -0800240 au_mock.host_infos.GetHostInfo(test_ip).attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800241 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700242
243 def testHandleUpdatePingWithSetUpdate(self):
joychen7c2054a2013-07-25 11:14:07 -0700244 """If update is set, it should use the update found in that directory."""
Chris Sosa6a3697f2013-01-29 16:44:43 -0800245 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
246 au_mock = self._DummyAutoupdateConstructor()
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700247
248 test_data = _TEST_REQUEST % self.test_dict
249 test_label = 'new_update-test/the-new-update'
250 new_image_dir = os.path.join(self.static_image_dir, test_label)
251 new_url = self.url.replace('update.gz', test_label + '/update.gz')
252
Chris Sosa6a3697f2013-01-29 16:44:43 -0800253 # Generate a fake payload.
joychen121fc9b2013-08-02 14:30:30 -0700254 common_util.MkDirP(new_image_dir)
joychen7c2054a2013-07-25 11:14:07 -0700255 update_gz = os.path.join(new_image_dir, constants.UPDATE_FILE)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800256 with open(update_gz, 'w') as fh:
257 fh.write('')
258
Gilad Arnold55a2a372012-10-02 09:46:32 -0700259 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700260 new_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700261 common_util.GetFileSha256(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700262 new_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700263 common_util.GetFileSize(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700264 new_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800265 au_mock._StoreMetadataToFile(new_image_dir,
266 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 15:35:58 -0800267 autoupdate_lib.GetUpdateResponse(
268 self.sha1, self.sha256, self.size, new_url, False, '3.0',
269 False).AndReturn(self.payload)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700270
271 self.mox.ReplayAll()
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700272 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
273 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800274 au_mock.host_infos.GetHostInfo('127.0.0.1').
Chris Sosa1885d032012-11-29 17:07:27 -0800275 attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800276 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700277 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800278 self.assertFalse('forced_update_label' in
279 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700280
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700281 def testGetVersionFromDir(self):
282 au = self._DummyAutoupdateConstructor()
283
284 # New-style version number.
285 self.assertEqual(
286 au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
287 '1102.0.2011_09_30_0806')
288
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700289 def testCanUpdate(self):
290 au = self._DummyAutoupdateConstructor()
291
292 # When both the client and the server have new-style versions, we should
293 # just compare the tokens directly.
294 self.assertTrue(
295 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
296 self.assertTrue(
297 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
298 self.assertFalse(
299 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
300 self.assertFalse(
301 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
302
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700303 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-29 16:44:43 -0800307 au_mock = self._DummyAutoupdateConstructor(urlbase=remote_urlbase,
308 payload_path=remote_payload_path,
309 remote_payload=True)
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700310
311 test_data = _TEST_REQUEST % self.test_dict
312
Chris Sosa6a3697f2013-01-29 16:44:43 -0800313 au_mock._GetRemotePayloadAttrs(remote_url).AndReturn(
314 autoupdate.UpdateMetadata(self.sha1, self.sha256, self.size, False))
Chris Sosa52148582012-11-15 15:35:58 -0800315 autoupdate_lib.GetUpdateResponse(
316 self.sha1, self.sha256, self.size, remote_url, False,
317 '3.0', False).AndReturn(self.payload)
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700318
319 self.mox.ReplayAll()
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700320 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
321 self.mox.VerifyAll()
322
Chris Sosa0356d3b2010-09-16 15:46:22 -0700323
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700324if __name__ == '__main__':
325 unittest.main()