blob: f06b98140719b04ecd513c2ed1b8f9dd38f63f3b [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 Sosa7c931362010-10-11 19:49:01 -070011import socket
Chris Sosa0356d3b2010-09-16 15:46:22 -070012import unittest
Chris Sosa0356d3b2010-09-16 15:46:22 -070013
Gilad Arnoldabb352e2012-09-23 01:24:27 -070014import cherrypy
15import mox
16
Chris Sosa0356d3b2010-09-16 15:46:22 -070017import autoupdate
Gilad Arnold55a2a372012-10-02 09:46:32 -070018import common_util
Chris Sosa0356d3b2010-09-16 15:46:22 -070019
Gilad Arnoldabb352e2012-09-23 01:24:27 -070020
Chris Sosa0356d3b2010-09-16 15:46:22 -070021_TEST_REQUEST = """
22<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" >
23 <o:app version="%(version)s" track="%(track)s" board="%(board)s" />
Chris Sosaa387a872010-09-29 11:51:36 -070024 <o:updatecheck />
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070025 <o:event eventresult="%(event_result)d" eventtype="%(event_type)d" />
Chris Sosa0356d3b2010-09-16 15:46:22 -070026</client_test>"""
27
28
29class AutoupdateTest(mox.MoxTestBase):
30 def setUp(self):
31 mox.MoxTestBase.setUp(self)
Gilad Arnold55a2a372012-10-02 09:46:32 -070032 self.mox.StubOutWithMock(common_util, 'GetFileSize')
33 self.mox.StubOutWithMock(common_util, 'GetFileSha1')
34 self.mox.StubOutWithMock(common_util, 'GetFileSha256')
Chris Sosa0356d3b2010-09-16 15:46:22 -070035 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload')
36 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
Gilad Arnold0c9c8602012-10-02 23:58:58 -070037 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs')
Chris Sosa7c931362010-10-11 19:49:01 -070038 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070039 self.test_board = 'test-board'
40 self.build_root = '/src_path/build/images'
41 self.latest_dir = '12345_af_12-a1'
42 self.latest_verision = '12345_af_12'
43 self.static_image_dir = '/tmp/static-dir/'
Chris Sosa7c931362010-10-11 19:49:01 -070044 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070045 self.test_dict = {
46 'client': 'ChromeOSUpdateEngine-1.0',
47 'version': 'ForcedUpdate',
48 'track': 'unused_var',
49 'board': self.test_board,
50 'event_result': 2,
51 'event_type': 3
52 }
Chris Sosa0356d3b2010-09-16 15:46:22 -070053 self.test_data = _TEST_REQUEST % self.test_dict
54 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
Gilad Arnold0c9c8602012-10-02 23:58:58 -070055 self.sha1 = 12345
Chris Sosa0356d3b2010-09-16 15:46:22 -070056 self.size = 54321
57 self.url = 'http://%s/static/update.gz' % self.hostname
58 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070059 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070060 cherrypy.request.base = 'http://%s' % self.hostname
61
Gilad Arnold0c9c8602012-10-02 23:58:58 -070062 def _DummyAutoupdateConstructor(self, **kwargs):
Chris Sosa0356d3b2010-09-16 15:46:22 -070063 """Creates a dummy autoupdater. Used to avoid using constructor."""
64 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070065 static_dir=self.static_image_dir,
Gilad Arnold0c9c8602012-10-02 23:58:58 -070066 **kwargs)
Chris Sosa0356d3b2010-09-16 15:46:22 -070067 return dummy
68
Chris Sosa744e1472011-09-07 19:32:50 -070069 def testGetRightSignedDeltaPayloadDir(self):
70 """Test that our directory is what we expect it to be for signed updates."""
Gilad Arnold55a2a372012-10-02 09:46:32 -070071 self.mox.StubOutWithMock(common_util, 'GetFileMd5')
Chris Sosa744e1472011-09-07 19:32:50 -070072 key_path = 'test_key_path'
73 src_image = 'test_src_image'
74 target_image = 'test_target_image'
Gilad Arnold55a2a372012-10-02 09:46:32 -070075 src_hash = '12345'
76 target_hash = '67890'
77 key_hash = 'abcde'
Chris Sosa744e1472011-09-07 19:32:50 -070078
Gilad Arnold55a2a372012-10-02 09:46:32 -070079 common_util.GetFileMd5(src_image).AndReturn(src_hash)
80 common_util.GetFileMd5(target_image).AndReturn(target_hash)
81 common_util.GetFileMd5(key_path).AndReturn(key_hash)
Chris Sosa744e1472011-09-07 19:32:50 -070082
83 self.mox.ReplayAll()
84 au_mock = self._DummyAutoupdateConstructor()
85 au_mock.private_key = key_path
86 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 15:31:36 -040087 self.assertEqual(os.path.basename(update_dir),
Gilad Arnold55a2a372012-10-02 09:46:32 -070088 '%s_%s+%s+patched_kernel' %
89 (src_hash, target_hash, key_hash))
Chris Sosa744e1472011-09-07 19:32:50 -070090 self.mox.VerifyAll()
91
Chris Sosa0356d3b2010-09-16 15:46:22 -070092 def testGenerateLatestUpdateImageWithForced(self):
Don Garrettf90edf02010-11-16 17:36:14 -080093 self.mox.StubOutWithMock(autoupdate.Autoupdate,
94 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -070095 autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn(
96 '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir))
Don Garrettf90edf02010-11-16 17:36:14 -080097 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosa0356d3b2010-09-16 15:46:22 -070098 '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board,
99 self.latest_dir),
Don Garrettf90edf02010-11-16 17:36:14 -0800100 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700101
102 self.mox.ReplayAll()
103 au_mock = self._DummyAutoupdateConstructor()
104 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
105 'ForcedUpdate',
106 self.static_image_dir))
107 self.mox.VerifyAll()
108
109 def testHandleUpdatePingForForcedImage(self):
Don Garrettf90edf02010-11-16 17:36:14 -0800110 self.mox.StubOutWithMock(autoupdate.Autoupdate,
111 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700112
113 test_data = _TEST_REQUEST % self.test_dict
114
Don Garrettf90edf02010-11-16 17:36:14 -0800115 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosaa387a872010-09-29 11:51:36 -0700116 self.forced_image_path,
Don Garrettf90edf02010-11-16 17:36:14 -0800117 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Gilad Arnold55a2a372012-10-02 09:46:32 -0700118 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700119 self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700120 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 11:51:36 -0700121 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700122 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700123 self.static_image_dir, 'update.gz')).AndReturn(self.size)
124 autoupdate.Autoupdate.GetUpdatePayload(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700125 self.sha1, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700126 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700127
128 self.mox.ReplayAll()
129 au_mock = self._DummyAutoupdateConstructor()
130 au_mock.forced_image = self.forced_image_path
131 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
132 self.mox.VerifyAll()
133
134 def testHandleUpdatePingForLatestImage(self):
135 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
136
137 test_data = _TEST_REQUEST % self.test_dict
138
139 autoupdate.Autoupdate.GenerateLatestUpdateImage(
Don Garrettf90edf02010-11-16 17:36:14 -0800140 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(
141 'update.gz')
Gilad Arnold55a2a372012-10-02 09:46:32 -0700142 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700143 self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700144 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 11:51:36 -0700145 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700146 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700147 self.static_image_dir, 'update.gz')).AndReturn(self.size)
148 autoupdate.Autoupdate.GetUpdatePayload(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700149 self.sha1, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700150 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700151
152 self.mox.ReplayAll()
153 au_mock = self._DummyAutoupdateConstructor()
154 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800155 curr_host_info = au_mock.host_infos.GetHostInfo('127.0.0.1');
156 self.assertEqual(curr_host_info.GetAttr('last_known_version'),
157 'ForcedUpdate')
158 self.assertEqual(curr_host_info.GetAttr('last_event_type'),
159 self.test_dict['event_type'])
160 self.assertEqual(curr_host_info.GetAttr('last_event_status'),
161 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 15:46:22 -0700162 self.mox.VerifyAll()
163
Don Garrett0ad09372010-12-06 16:20:30 -0800164 def testChangeUrlPort(self):
165 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
166 self.assertEqual(r, 'http://fuzzy:8085/static')
167
168 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
169 self.assertEqual(r, 'http://fuzzy:8085/static')
170
171 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
172 self.assertEqual(r, 'ftp://fuzzy:8085/static')
173
174 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
175 self.assertEqual(r, 'ftp://fuzzy:8085')
176
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700177 def testHandleHostInfoPing(self):
178 au_mock = self._DummyAutoupdateConstructor()
179 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
180
Gilad Arnold286a0062012-01-12 13:47:02 -0800181 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700182 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -0800183 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700184 self.assertEqual(
185 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
186
187 def testHandleSetUpdatePing(self):
188 au_mock = self._DummyAutoupdateConstructor()
189 test_ip = '1.2.3.4'
190 test_label = 'test/old-update'
191 self.assertRaises(
192 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
193 self.assertRaises(
194 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
195 self.assertRaises(
196 AssertionError, au_mock.HandleSetUpdatePing, None, None)
197
198 au_mock.HandleSetUpdatePing(test_ip, test_label)
199 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800200 au_mock.host_infos.GetHostInfo(test_ip).GetAttr('forced_update_label'),
201 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700202
203 def testHandleUpdatePingWithSetUpdate(self):
204 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
205
206 test_data = _TEST_REQUEST % self.test_dict
207 test_label = 'new_update-test/the-new-update'
208 new_image_dir = os.path.join(self.static_image_dir, test_label)
209 new_url = self.url.replace('update.gz', test_label + '/update.gz')
210
211 autoupdate.Autoupdate.GenerateLatestUpdateImage(
212 self.test_board, 'ForcedUpdate', new_image_dir).AndReturn(
213 'update.gz')
Gilad Arnold55a2a372012-10-02 09:46:32 -0700214 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700215 new_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700216 common_util.GetFileSha256(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700217 new_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700218 common_util.GetFileSize(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700219 new_image_dir, 'update.gz')).AndReturn(self.size)
220 autoupdate.Autoupdate.GetUpdatePayload(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700221 self.sha1, self.sha256, self.size, new_url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700222 self.payload)
223
224 self.mox.ReplayAll()
225 au_mock = self._DummyAutoupdateConstructor()
226 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
227 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800228 au_mock.host_infos.GetHostInfo('127.0.0.1').
229 GetAttr('forced_update_label'),
230 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700231 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800232 self.assertFalse('forced_update_label' in
233 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700234
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700235 def testGetVersionFromDir(self):
236 au = self._DummyAutoupdateConstructor()
237
238 # New-style version number.
239 self.assertEqual(
240 au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
241 '1102.0.2011_09_30_0806')
242
243 # Old-style version number.
244 self.assertEqual(
245 au._GetVersionFromDir('/foo/x86-alex/0.15.938.2011_08_23_0941-a1'),
246 '0.15.938.2011_08_23_0941')
247
248 def testCanUpdate(self):
249 au = self._DummyAutoupdateConstructor()
250
251 # When both the client and the server have new-style versions, we should
252 # just compare the tokens directly.
253 self.assertTrue(
254 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
255 self.assertTrue(
256 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
257 self.assertFalse(
258 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
259 self.assertFalse(
260 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
261
262 # When the device has an old four-token version number, we should skip the
263 # first two tokens and compare the rest. If there's a tie, go with the
264 # server's version.
265 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.1'))
266 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.0'))
267 self.assertFalse(au._CanUpdate('0.16.892.0', '890.0.0'))
268
269 # Test the case where both the client and the server have old-style
270 # versions.
271 self.assertTrue(au._CanUpdate('0.16.892.0', '0.16.892.1'))
272 self.assertFalse(au._CanUpdate('0.16.892.0', '0.16.892.0'))
273
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700274 def testHandleUpdatePingRemotePayload(self):
275 remote_urlbase = 'http://remotehost:6666'
276 remote_payload_path = 'static/path/to/update.gz'
277 remote_url = '/'.join([remote_urlbase, remote_payload_path, 'update.gz'])
278
279 test_data = _TEST_REQUEST % self.test_dict
280
281 autoupdate.Autoupdate._GetRemotePayloadAttrs(remote_url).AndReturn(
282 (self.sha1, self.sha256, self.size, False))
283 autoupdate.Autoupdate.GetUpdatePayload(
284 self.sha1, self.sha256, self.size, remote_url, False).AndReturn(
285 self.payload)
286
287 self.mox.ReplayAll()
288 au_mock = self._DummyAutoupdateConstructor(urlbase=remote_urlbase,
289 payload_path=remote_payload_path,
290 remote_payload=True)
291 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
292 self.mox.VerifyAll()
293
Chris Sosa0356d3b2010-09-16 15:46:22 -0700294
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700295if __name__ == '__main__':
296 unittest.main()