blob: 02c667087a506b3c0b44073b2e178fbd67c78ee3 [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
Chris Sosa52148582012-11-15 15:35:58 -080018import autoupdate_lib
Gilad Arnold55a2a372012-10-02 09:46:32 -070019import common_util
Chris Sosa0356d3b2010-09-16 15:46:22 -070020
Gilad Arnoldabb352e2012-09-23 01:24:27 -070021
Chris Sosa0356d3b2010-09-16 15:46:22 -070022_TEST_REQUEST = """
Chris Sosa52148582012-11-15 15:35:58 -080023<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" protocol="3.0">
24 <app version="%(version)s" track="%(track)s" board="%(board)s" />
25 <updatecheck />
26 <event eventresult="%(event_result)d" eventtype="%(event_type)d" />
Chris Sosa0356d3b2010-09-16 15:46:22 -070027</client_test>"""
28
29
30class AutoupdateTest(mox.MoxTestBase):
31 def setUp(self):
32 mox.MoxTestBase.setUp(self)
Gilad Arnold55a2a372012-10-02 09:46:32 -070033 self.mox.StubOutWithMock(common_util, 'GetFileSize')
34 self.mox.StubOutWithMock(common_util, 'GetFileSha1')
35 self.mox.StubOutWithMock(common_util, 'GetFileSha256')
Chris Sosa52148582012-11-15 15:35:58 -080036 self.mox.StubOutWithMock(autoupdate_lib, 'GetUpdateResponse')
Chris Sosa0356d3b2010-09-16 15:46:22 -070037 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
Gilad Arnold0c9c8602012-10-02 23:58:58 -070038 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs')
Chris Sosa7c931362010-10-11 19:49:01 -070039 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070040 self.test_board = 'test-board'
41 self.build_root = '/src_path/build/images'
42 self.latest_dir = '12345_af_12-a1'
43 self.latest_verision = '12345_af_12'
44 self.static_image_dir = '/tmp/static-dir/'
Chris Sosa7c931362010-10-11 19:49:01 -070045 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070046 self.test_dict = {
47 'client': 'ChromeOSUpdateEngine-1.0',
48 'version': 'ForcedUpdate',
49 'track': 'unused_var',
50 'board': self.test_board,
51 'event_result': 2,
52 'event_type': 3
53 }
Chris Sosa0356d3b2010-09-16 15:46:22 -070054 self.test_data = _TEST_REQUEST % self.test_dict
55 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
Gilad Arnold0c9c8602012-10-02 23:58:58 -070056 self.sha1 = 12345
Chris Sosa0356d3b2010-09-16 15:46:22 -070057 self.size = 54321
58 self.url = 'http://%s/static/update.gz' % self.hostname
59 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070060 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070061 cherrypy.request.base = 'http://%s' % self.hostname
62
Gilad Arnold0c9c8602012-10-02 23:58:58 -070063 def _DummyAutoupdateConstructor(self, **kwargs):
Chris Sosa0356d3b2010-09-16 15:46:22 -070064 """Creates a dummy autoupdater. Used to avoid using constructor."""
65 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070066 static_dir=self.static_image_dir,
Gilad Arnold0c9c8602012-10-02 23:58:58 -070067 **kwargs)
Chris Sosa0356d3b2010-09-16 15:46:22 -070068 return dummy
69
Chris Sosa744e1472011-09-07 19:32:50 -070070 def testGetRightSignedDeltaPayloadDir(self):
71 """Test that our directory is what we expect it to be for signed updates."""
Gilad Arnold55a2a372012-10-02 09:46:32 -070072 self.mox.StubOutWithMock(common_util, 'GetFileMd5')
Chris Sosa744e1472011-09-07 19:32:50 -070073 key_path = 'test_key_path'
74 src_image = 'test_src_image'
75 target_image = 'test_target_image'
Gilad Arnold55a2a372012-10-02 09:46:32 -070076 src_hash = '12345'
77 target_hash = '67890'
78 key_hash = 'abcde'
Chris Sosa744e1472011-09-07 19:32:50 -070079
Gilad Arnold55a2a372012-10-02 09:46:32 -070080 common_util.GetFileMd5(src_image).AndReturn(src_hash)
81 common_util.GetFileMd5(target_image).AndReturn(target_hash)
82 common_util.GetFileMd5(key_path).AndReturn(key_hash)
Chris Sosa744e1472011-09-07 19:32:50 -070083
84 self.mox.ReplayAll()
85 au_mock = self._DummyAutoupdateConstructor()
86 au_mock.private_key = key_path
87 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 15:31:36 -040088 self.assertEqual(os.path.basename(update_dir),
Gilad Arnold55a2a372012-10-02 09:46:32 -070089 '%s_%s+%s+patched_kernel' %
90 (src_hash, target_hash, key_hash))
Chris Sosa744e1472011-09-07 19:32:50 -070091 self.mox.VerifyAll()
92
Chris Sosa0356d3b2010-09-16 15:46:22 -070093 def testGenerateLatestUpdateImageWithForced(self):
Don Garrettf90edf02010-11-16 17:36:14 -080094 self.mox.StubOutWithMock(autoupdate.Autoupdate,
95 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -070096 autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn(
97 '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir))
Don Garrettf90edf02010-11-16 17:36:14 -080098 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosa0356d3b2010-09-16 15:46:22 -070099 '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board,
100 self.latest_dir),
Don Garrettf90edf02010-11-16 17:36:14 -0800101 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700102
103 self.mox.ReplayAll()
104 au_mock = self._DummyAutoupdateConstructor()
105 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
106 'ForcedUpdate',
107 self.static_image_dir))
108 self.mox.VerifyAll()
109
110 def testHandleUpdatePingForForcedImage(self):
Don Garrettf90edf02010-11-16 17:36:14 -0800111 self.mox.StubOutWithMock(autoupdate.Autoupdate,
112 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700113
114 test_data = _TEST_REQUEST % self.test_dict
115
Don Garrettf90edf02010-11-16 17:36:14 -0800116 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosaa387a872010-09-29 11:51:36 -0700117 self.forced_image_path,
Don Garrettf90edf02010-11-16 17:36:14 -0800118 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Gilad Arnold55a2a372012-10-02 09:46:32 -0700119 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700120 self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700121 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 11:51:36 -0700122 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700123 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700124 self.static_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa52148582012-11-15 15:35:58 -0800125 autoupdate_lib.GetUpdateResponse(
126 self.sha1, self.sha256, self.size, self.url, False, '3.0',
127 False).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700128
129 self.mox.ReplayAll()
130 au_mock = self._DummyAutoupdateConstructor()
131 au_mock.forced_image = self.forced_image_path
132 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
133 self.mox.VerifyAll()
134
135 def testHandleUpdatePingForLatestImage(self):
136 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
137
138 test_data = _TEST_REQUEST % self.test_dict
139
140 autoupdate.Autoupdate.GenerateLatestUpdateImage(
Don Garrettf90edf02010-11-16 17:36:14 -0800141 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(
142 'update.gz')
Gilad Arnold55a2a372012-10-02 09:46:32 -0700143 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700144 self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700145 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 11:51:36 -0700146 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700147 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700148 self.static_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa52148582012-11-15 15:35:58 -0800149 autoupdate_lib.GetUpdateResponse(
150 self.sha1, self.sha256, self.size, self.url, False, '3.0', False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700151 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700152
153 self.mox.ReplayAll()
154 au_mock = self._DummyAutoupdateConstructor()
155 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800156 curr_host_info = au_mock.host_infos.GetHostInfo('127.0.0.1');
Chris Sosa1885d032012-11-29 17:07:27 -0800157 self.assertEqual(curr_host_info.attrs['last_known_version'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800158 'ForcedUpdate')
Chris Sosa1885d032012-11-29 17:07:27 -0800159 self.assertEqual(curr_host_info.attrs['last_event_type'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800160 self.test_dict['event_type'])
Chris Sosa1885d032012-11-29 17:07:27 -0800161 self.assertEqual(curr_host_info.attrs['last_event_status'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800162 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 15:46:22 -0700163 self.mox.VerifyAll()
164
Don Garrett0ad09372010-12-06 16:20:30 -0800165 def testChangeUrlPort(self):
166 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
167 self.assertEqual(r, 'http://fuzzy:8085/static')
168
169 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
170 self.assertEqual(r, 'http://fuzzy:8085/static')
171
172 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
173 self.assertEqual(r, 'ftp://fuzzy:8085/static')
174
175 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
176 self.assertEqual(r, 'ftp://fuzzy:8085')
177
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700178 def testHandleHostInfoPing(self):
179 au_mock = self._DummyAutoupdateConstructor()
180 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
181
Gilad Arnold286a0062012-01-12 13:47:02 -0800182 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700183 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -0800184 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700185 self.assertEqual(
186 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
187
188 def testHandleSetUpdatePing(self):
189 au_mock = self._DummyAutoupdateConstructor()
190 test_ip = '1.2.3.4'
191 test_label = 'test/old-update'
192 self.assertRaises(
193 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
194 self.assertRaises(
195 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
196 self.assertRaises(
197 AssertionError, au_mock.HandleSetUpdatePing, None, None)
198
199 au_mock.HandleSetUpdatePing(test_ip, test_label)
200 self.assertEqual(
Chris Sosa1885d032012-11-29 17:07:27 -0800201 au_mock.host_infos.GetHostInfo(test_ip).attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800202 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700203
204 def testHandleUpdatePingWithSetUpdate(self):
205 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
206
207 test_data = _TEST_REQUEST % self.test_dict
208 test_label = 'new_update-test/the-new-update'
209 new_image_dir = os.path.join(self.static_image_dir, test_label)
210 new_url = self.url.replace('update.gz', test_label + '/update.gz')
211
212 autoupdate.Autoupdate.GenerateLatestUpdateImage(
213 self.test_board, 'ForcedUpdate', new_image_dir).AndReturn(
214 'update.gz')
Gilad Arnold55a2a372012-10-02 09:46:32 -0700215 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700216 new_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700217 common_util.GetFileSha256(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700218 new_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700219 common_util.GetFileSize(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700220 new_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa52148582012-11-15 15:35:58 -0800221 autoupdate_lib.GetUpdateResponse(
222 self.sha1, self.sha256, self.size, new_url, False, '3.0',
223 False).AndReturn(self.payload)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700224
225 self.mox.ReplayAll()
226 au_mock = self._DummyAutoupdateConstructor()
227 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
228 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800229 au_mock.host_infos.GetHostInfo('127.0.0.1').
Chris Sosa1885d032012-11-29 17:07:27 -0800230 attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800231 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700232 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800233 self.assertFalse('forced_update_label' in
234 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700235
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700236 def testGetVersionFromDir(self):
237 au = self._DummyAutoupdateConstructor()
238
239 # New-style version number.
240 self.assertEqual(
241 au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
242 '1102.0.2011_09_30_0806')
243
244 # Old-style version number.
245 self.assertEqual(
246 au._GetVersionFromDir('/foo/x86-alex/0.15.938.2011_08_23_0941-a1'),
247 '0.15.938.2011_08_23_0941')
248
249 def testCanUpdate(self):
250 au = self._DummyAutoupdateConstructor()
251
252 # When both the client and the server have new-style versions, we should
253 # just compare the tokens directly.
254 self.assertTrue(
255 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
256 self.assertTrue(
257 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
258 self.assertFalse(
259 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
260 self.assertFalse(
261 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
262
263 # When the device has an old four-token version number, we should skip the
264 # first two tokens and compare the rest. If there's a tie, go with the
265 # server's version.
266 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.1'))
267 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.0'))
268 self.assertFalse(au._CanUpdate('0.16.892.0', '890.0.0'))
269
270 # Test the case where both the client and the server have old-style
271 # versions.
272 self.assertTrue(au._CanUpdate('0.16.892.0', '0.16.892.1'))
273 self.assertFalse(au._CanUpdate('0.16.892.0', '0.16.892.0'))
274
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700275 def testHandleUpdatePingRemotePayload(self):
276 remote_urlbase = 'http://remotehost:6666'
277 remote_payload_path = 'static/path/to/update.gz'
278 remote_url = '/'.join([remote_urlbase, remote_payload_path, 'update.gz'])
279
280 test_data = _TEST_REQUEST % self.test_dict
281
282 autoupdate.Autoupdate._GetRemotePayloadAttrs(remote_url).AndReturn(
283 (self.sha1, self.sha256, self.size, False))
Chris Sosa52148582012-11-15 15:35:58 -0800284 autoupdate_lib.GetUpdateResponse(
285 self.sha1, self.sha256, self.size, remote_url, False,
286 '3.0', False).AndReturn(self.payload)
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700287
288 self.mox.ReplayAll()
289 au_mock = self._DummyAutoupdateConstructor(urlbase=remote_urlbase,
290 payload_path=remote_payload_path,
291 remote_payload=True)
292 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
293 self.mox.VerifyAll()
294
Chris Sosa0356d3b2010-09-16 15:46:22 -0700295
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700296if __name__ == '__main__':
297 unittest.main()