blob: 72d583e4deda3c7738d58db9508129709432a5f2 [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 Sosa52148582012-11-15 15:35:58 -080040 self.mox.StubOutWithMock(autoupdate_lib, 'GetUpdateResponse')
Gilad Arnold0c9c8602012-10-02 23:58:58 -070041 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs')
Chris Sosa7c931362010-10-11 19:49:01 -070042 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070043 self.test_board = 'test-board'
joychen121fc9b2013-08-02 14:30:30 -070044 self.build_root = tempfile.mkdtemp('autoupdate_build_root')
Chris Sosa0356d3b2010-09-16 15:46:22 -070045 self.latest_dir = '12345_af_12-a1'
46 self.latest_verision = '12345_af_12'
joychen121fc9b2013-08-02 14:30:30 -070047 self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir')
Chris Sosa7c931362010-10-11 19:49:01 -070048 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070049 self.test_dict = {
50 'client': 'ChromeOSUpdateEngine-1.0',
51 'version': 'ForcedUpdate',
52 'track': 'unused_var',
53 'board': self.test_board,
54 'event_result': 2,
55 'event_type': 3
56 }
Chris Sosa0356d3b2010-09-16 15:46:22 -070057 self.test_data = _TEST_REQUEST % self.test_dict
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
joychen121fc9b2013-08-02 14:30:30 -070064 common_util.MkDirP(self.static_image_dir)
65 self._xbuddy = xbuddy.XBuddy(False,
66 root_dir=None,
67 static_dir=self.static_image_dir)
68 self.mox.StubOutWithMock(xbuddy.XBuddy, '_GetArtifact')
Chris Sosa6a3697f2013-01-29 16:44:43 -080069
70 def tearDown(self):
joychen121fc9b2013-08-02 14:30:30 -070071 shutil.rmtree(self.build_root)
Chris Sosa6a3697f2013-01-29 16:44:43 -080072 shutil.rmtree(self.static_image_dir)
Chris Sosa54555862010-10-25 17:26:17 -070073
Gilad Arnold0c9c8602012-10-02 23:58:58 -070074 def _DummyAutoupdateConstructor(self, **kwargs):
Chris Sosa0356d3b2010-09-16 15:46:22 -070075 """Creates a dummy autoupdater. Used to avoid using constructor."""
joychen121fc9b2013-08-02 14:30:30 -070076 dummy = autoupdate.Autoupdate(self._xbuddy,
77 root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070078 static_dir=self.static_image_dir,
Gilad Arnold0c9c8602012-10-02 23:58:58 -070079 **kwargs)
Chris Sosa0356d3b2010-09-16 15:46:22 -070080 return dummy
81
Chris Sosa744e1472011-09-07 19:32:50 -070082 def testGetRightSignedDeltaPayloadDir(self):
83 """Test that our directory is what we expect it to be for signed updates."""
Gilad Arnold55a2a372012-10-02 09:46:32 -070084 self.mox.StubOutWithMock(common_util, 'GetFileMd5')
Chris Sosa744e1472011-09-07 19:32:50 -070085 key_path = 'test_key_path'
86 src_image = 'test_src_image'
87 target_image = 'test_target_image'
Gilad Arnold55a2a372012-10-02 09:46:32 -070088 src_hash = '12345'
89 target_hash = '67890'
90 key_hash = 'abcde'
Chris Sosa744e1472011-09-07 19:32:50 -070091
Gilad Arnold55a2a372012-10-02 09:46:32 -070092 common_util.GetFileMd5(src_image).AndReturn(src_hash)
93 common_util.GetFileMd5(target_image).AndReturn(target_hash)
94 common_util.GetFileMd5(key_path).AndReturn(key_hash)
Chris Sosa744e1472011-09-07 19:32:50 -070095
96 self.mox.ReplayAll()
97 au_mock = self._DummyAutoupdateConstructor()
98 au_mock.private_key = key_path
99 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 15:31:36 -0400100 self.assertEqual(os.path.basename(update_dir),
Gilad Arnold55a2a372012-10-02 09:46:32 -0700101 '%s_%s+%s+patched_kernel' %
102 (src_hash, target_hash, key_hash))
Chris Sosa744e1472011-09-07 19:32:50 -0700103 self.mox.VerifyAll()
104
joychen121fc9b2013-08-02 14:30:30 -0700105 def testGenerateLatestUpdateImage(self):
106 """Test default behavior in response to plain update call."""
107 latest_label = os.path.join(self.test_board, self.latest_dir)
108 # Generate a fake latest image
109 latest_image_dir = os.path.join(self.static_image_dir, latest_label)
110 common_util.MkDirP(latest_image_dir)
111 image = os.path.join(latest_image_dir, constants.TEST_IMAGE_FILE)
112 with open(image, 'w') as fh:
113 fh.write('')
114
Don Garrettf90edf02010-11-16 17:36:14 -0800115 self.mox.StubOutWithMock(autoupdate.Autoupdate,
116 'GenerateUpdateImageWithCache')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800117 au_mock = self._DummyAutoupdateConstructor()
joychen121fc9b2013-08-02 14:30:30 -0700118
119 self._xbuddy._GetArtifact(
120 [''], self.test_board, lookup_only=True).AndReturn(
121 (latest_label, constants.TEST_IMAGE_FILE))
122
Chris Sosa6a3697f2013-01-29 16:44:43 -0800123 au_mock.GenerateUpdateImageWithCache(
joychen121fc9b2013-08-02 14:30:30 -0700124 os.path.join(self.static_image_dir, self.test_board, self.latest_dir,
125 constants.TEST_IMAGE_FILE),
126 static_image_dir=latest_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700127
128 self.mox.ReplayAll()
joychen121fc9b2013-08-02 14:30:30 -0700129 test_data = _TEST_REQUEST % self.test_dict
130 self.assertTrue(au_mock.HandleUpdatePing(test_data))
Chris Sosa0356d3b2010-09-16 15:46:22 -0700131 self.mox.VerifyAll()
132
133 def testHandleUpdatePingForForcedImage(self):
joychen121fc9b2013-08-02 14:30:30 -0700134 """Test update response to having a forced image."""
Don Garrettf90edf02010-11-16 17:36:14 -0800135 self.mox.StubOutWithMock(autoupdate.Autoupdate,
136 'GenerateUpdateImageWithCache')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800137 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
138 au_mock = self._DummyAutoupdateConstructor()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700139 test_data = _TEST_REQUEST % self.test_dict
140
joychen121fc9b2013-08-02 14:30:30 -0700141 # Generate a fake image
142 forced_image_dir = '/tmp/path_to_force/'
143 forced_image = forced_image_dir + constants.IMAGE_FILE
144 common_util.MkDirP(forced_image_dir)
145 with open(forced_image, 'w') as fh:
Chris Sosa6a3697f2013-01-29 16:44:43 -0800146 fh.write('')
147
joychen121fc9b2013-08-02 14:30:30 -0700148 static_forced_image_dir = os.path.join(self.static_image_dir,
149 'forced_image')
150 static_forced_image = os.path.join(static_forced_image_dir,
151 constants.IMAGE_FILE)
152 cache_image_dir = os.path.join(static_forced_image_dir, 'cache')
153
154 # Mock out GenerateUpdateImageWithCache to make an update file in cache
155 def mock_fn(_image, static_image_dir):
156 print 'mock_fn'
157 # No good way to introduce an update file during execution.
158 cache_dir = os.path.join(static_image_dir, 'cache')
159 common_util.MkDirP(cache_dir)
160 update_image = os.path.join(cache_dir, constants.UPDATE_FILE)
161 with open(update_image, 'w') as fh:
162 fh.write('')
163
164 au_mock.GenerateUpdateImageWithCache(static_forced_image,
165 static_image_dir=static_forced_image_dir).WithSideEffects(
166 mock_fn).AndReturn('cache')
167
Gilad Arnold55a2a372012-10-02 09:46:32 -0700168 common_util.GetFileSha1(os.path.join(
joychen121fc9b2013-08-02 14:30:30 -0700169 cache_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700170 common_util.GetFileSha256(os.path.join(
joychen121fc9b2013-08-02 14:30:30 -0700171 cache_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700172 common_util.GetFileSize(os.path.join(
joychen121fc9b2013-08-02 14:30:30 -0700173 cache_image_dir, 'update.gz')).AndReturn(self.size)
174 au_mock._StoreMetadataToFile(cache_image_dir,
Chris Sosa6a3697f2013-01-29 16:44:43 -0800175 mox.IsA(autoupdate.UpdateMetadata))
joychen121fc9b2013-08-02 14:30:30 -0700176 forced_url = 'http://%s/static/%s/update.gz' % (self.hostname,
177 'forced_image/cache')
Chris Sosa52148582012-11-15 15:35:58 -0800178 autoupdate_lib.GetUpdateResponse(
joychen121fc9b2013-08-02 14:30:30 -0700179 self.sha1, self.sha256, self.size, forced_url, False, '3.0',
Chris Sosa52148582012-11-15 15:35:58 -0800180 False).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700181
182 self.mox.ReplayAll()
joychen121fc9b2013-08-02 14:30:30 -0700183 au_mock.forced_image = forced_image
Chris Sosa0356d3b2010-09-16 15:46:22 -0700184 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
185 self.mox.VerifyAll()
186
Don Garrett0ad09372010-12-06 16:20:30 -0800187 def testChangeUrlPort(self):
188 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
189 self.assertEqual(r, 'http://fuzzy:8085/static')
190
191 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
192 self.assertEqual(r, 'http://fuzzy:8085/static')
193
194 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
195 self.assertEqual(r, 'ftp://fuzzy:8085/static')
196
197 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
198 self.assertEqual(r, 'ftp://fuzzy:8085')
199
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700200 def testHandleHostInfoPing(self):
201 au_mock = self._DummyAutoupdateConstructor()
202 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
203
Gilad Arnold286a0062012-01-12 13:47:02 -0800204 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700205 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -0800206 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700207 self.assertEqual(
208 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
209
210 def testHandleSetUpdatePing(self):
211 au_mock = self._DummyAutoupdateConstructor()
212 test_ip = '1.2.3.4'
213 test_label = 'test/old-update'
214 self.assertRaises(
215 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
216 self.assertRaises(
217 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
218 self.assertRaises(
219 AssertionError, au_mock.HandleSetUpdatePing, None, None)
220
221 au_mock.HandleSetUpdatePing(test_ip, test_label)
222 self.assertEqual(
Chris Sosa1885d032012-11-29 17:07:27 -0800223 au_mock.host_infos.GetHostInfo(test_ip).attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800224 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700225
226 def testHandleUpdatePingWithSetUpdate(self):
joychen7c2054a2013-07-25 11:14:07 -0700227 """If update is set, it should use the update found in that directory."""
Chris Sosa6a3697f2013-01-29 16:44:43 -0800228 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
229 au_mock = self._DummyAutoupdateConstructor()
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700230
231 test_data = _TEST_REQUEST % self.test_dict
232 test_label = 'new_update-test/the-new-update'
233 new_image_dir = os.path.join(self.static_image_dir, test_label)
234 new_url = self.url.replace('update.gz', test_label + '/update.gz')
235
Chris Sosa6a3697f2013-01-29 16:44:43 -0800236 # Generate a fake payload.
joychen121fc9b2013-08-02 14:30:30 -0700237 common_util.MkDirP(new_image_dir)
joychen7c2054a2013-07-25 11:14:07 -0700238 update_gz = os.path.join(new_image_dir, constants.UPDATE_FILE)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800239 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 Sosa6a3697f2013-01-29 16:44:43 -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
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700272 def testCanUpdate(self):
273 au = self._DummyAutoupdateConstructor()
274
275 # When both the client and the server have new-style versions, we should
276 # just compare the tokens directly.
277 self.assertTrue(
278 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
279 self.assertTrue(
280 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
281 self.assertFalse(
282 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
283 self.assertFalse(
284 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
285
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700286 def testHandleUpdatePingRemotePayload(self):
287 remote_urlbase = 'http://remotehost:6666'
288 remote_payload_path = 'static/path/to/update.gz'
289 remote_url = '/'.join([remote_urlbase, remote_payload_path, 'update.gz'])
Chris Sosa6a3697f2013-01-29 16:44:43 -0800290 au_mock = self._DummyAutoupdateConstructor(urlbase=remote_urlbase,
291 payload_path=remote_payload_path,
292 remote_payload=True)
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700293
294 test_data = _TEST_REQUEST % self.test_dict
295
Chris Sosa6a3697f2013-01-29 16:44:43 -0800296 au_mock._GetRemotePayloadAttrs(remote_url).AndReturn(
297 autoupdate.UpdateMetadata(self.sha1, self.sha256, self.size, False))
Chris Sosa52148582012-11-15 15:35:58 -0800298 autoupdate_lib.GetUpdateResponse(
299 self.sha1, self.sha256, self.size, remote_url, False,
300 '3.0', False).AndReturn(self.payload)
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700301
302 self.mox.ReplayAll()
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700303 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
304 self.mox.VerifyAll()
305
Chris Sosa0356d3b2010-09-16 15:46:22 -0700306
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700307if __name__ == '__main__':
308 unittest.main()