blob: 317dc42407e4e47d7b956354f58b163285370153 [file] [log] [blame]
Chris Sosa0356d3b2010-09-16 22:46:221#!/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
Chris Sosa54555862010-10-26 00:26:179import cherrypy
Dale Curtisc9aaf3a2011-08-09 22:47:4010import json
Chris Sosa0356d3b2010-09-16 22:46:2211import mox
12import os
Chris Sosa7c931362010-10-12 02:49:0113import socket
Chris Sosa0356d3b2010-09-16 22:46:2214import unittest
Chris Sosa0356d3b2010-09-16 22:46:2215
16import autoupdate
17
18_TEST_REQUEST = """
19<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" >
20 <o:app version="%(version)s" track="%(track)s" board="%(board)s" />
Chris Sosaa387a872010-09-29 18:51:3621 <o:updatecheck />
Dale Curtisc9aaf3a2011-08-09 22:47:4022 <o:event eventresult="%(event_result)d" eventtype="%(event_type)d" />
Chris Sosa0356d3b2010-09-16 22:46:2223</client_test>"""
24
25
26class AutoupdateTest(mox.MoxTestBase):
27 def setUp(self):
28 mox.MoxTestBase.setUp(self)
29 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSize')
30 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetHash')
Chris Sosaa387a872010-09-29 18:51:3631 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSHA256')
Chris Sosa0356d3b2010-09-16 22:46:2232 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload')
33 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
Chris Sosa7c931362010-10-12 02:49:0134 self.port = 8080
Chris Sosa0356d3b2010-09-16 22:46:2235 self.test_board = 'test-board'
36 self.build_root = '/src_path/build/images'
37 self.latest_dir = '12345_af_12-a1'
38 self.latest_verision = '12345_af_12'
39 self.static_image_dir = '/tmp/static-dir/'
Chris Sosa7c931362010-10-12 02:49:0140 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 22:47:4041 self.test_dict = {
42 'client': 'ChromeOSUpdateEngine-1.0',
43 'version': 'ForcedUpdate',
44 'track': 'unused_var',
45 'board': self.test_board,
46 'event_result': 2,
47 'event_type': 3
48 }
Chris Sosa0356d3b2010-09-16 22:46:2249 self.test_data = _TEST_REQUEST % self.test_dict
50 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
51 self.hash = 12345
52 self.size = 54321
53 self.url = 'http://%s/static/update.gz' % self.hostname
54 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 18:51:3655 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-26 00:26:1756 cherrypy.request.base = 'http://%s' % self.hostname
57
Chris Sosa0356d3b2010-09-16 22:46:2258 def _DummyAutoupdateConstructor(self):
59 """Creates a dummy autoupdater. Used to avoid using constructor."""
60 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-12 02:49:0161 static_dir=self.static_image_dir,
62 port=self.port)
Chris Sosa0356d3b2010-09-16 22:46:2263 dummy.client_prefix = 'ChromeOSUpdateEngine'
Chris Sosa0356d3b2010-09-16 22:46:2264 return dummy
65
Chris Sosa744e1472011-09-08 02:32:5066 def testGetRightSignedDeltaPayloadDir(self):
67 """Test that our directory is what we expect it to be for signed updates."""
68 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetMd5')
69 key_path = 'test_key_path'
70 src_image = 'test_src_image'
71 target_image = 'test_target_image'
Scott Zawalski16954532012-03-20 19:31:3672 hashes = ['12345', '67890', 'abcde', 'patched_kernel']
Chris Sosa744e1472011-09-08 02:32:5073
74 autoupdate.Autoupdate._GetMd5(target_image).AndReturn(hashes[1])
75 autoupdate.Autoupdate._GetMd5(src_image).AndReturn(hashes[0])
76 autoupdate.Autoupdate._GetMd5(key_path).AndReturn(hashes[2])
77
78 self.mox.ReplayAll()
79 au_mock = self._DummyAutoupdateConstructor()
80 au_mock.private_key = key_path
81 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 19:31:3682 self.assertEqual(os.path.basename(update_dir),
83 '%s_%s+%s+%s' % tuple(hashes))
Chris Sosa744e1472011-09-08 02:32:5084 self.mox.VerifyAll()
85
Chris Sosa0356d3b2010-09-16 22:46:2286 def testGenerateLatestUpdateImageWithForced(self):
Don Garrettf90edf02010-11-17 01:36:1487 self.mox.StubOutWithMock(autoupdate.Autoupdate,
88 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 22:46:2289 autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn(
90 '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir))
Don Garrettf90edf02010-11-17 01:36:1491 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosa0356d3b2010-09-16 22:46:2292 '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board,
93 self.latest_dir),
Don Garrettf90edf02010-11-17 01:36:1494 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 22:46:2295
96 self.mox.ReplayAll()
97 au_mock = self._DummyAutoupdateConstructor()
98 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
99 'ForcedUpdate',
100 self.static_image_dir))
101 self.mox.VerifyAll()
102
103 def testHandleUpdatePingForForcedImage(self):
Don Garrettf90edf02010-11-17 01:36:14104 self.mox.StubOutWithMock(autoupdate.Autoupdate,
105 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 22:46:22106
107 test_data = _TEST_REQUEST % self.test_dict
108
Don Garrettf90edf02010-11-17 01:36:14109 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosaa387a872010-09-29 18:51:36110 self.forced_image_path,
Don Garrettf90edf02010-11-17 01:36:14111 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 22:46:22112 autoupdate.Autoupdate._GetHash(os.path.join(
113 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 18:51:36114 autoupdate.Autoupdate._GetSHA256(os.path.join(
115 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 22:46:22116 autoupdate.Autoupdate._GetSize(os.path.join(
117 self.static_image_dir, 'update.gz')).AndReturn(self.size)
118 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-26 00:34:49119 self.hash, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 22:47:40120 self.payload)
Chris Sosa0356d3b2010-09-16 22:46:22121
122 self.mox.ReplayAll()
123 au_mock = self._DummyAutoupdateConstructor()
124 au_mock.forced_image = self.forced_image_path
125 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
126 self.mox.VerifyAll()
127
128 def testHandleUpdatePingForLatestImage(self):
129 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
130
131 test_data = _TEST_REQUEST % self.test_dict
132
133 autoupdate.Autoupdate.GenerateLatestUpdateImage(
Don Garrettf90edf02010-11-17 01:36:14134 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(
135 'update.gz')
Chris Sosa0356d3b2010-09-16 22:46:22136 autoupdate.Autoupdate._GetHash(os.path.join(
137 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 18:51:36138 autoupdate.Autoupdate._GetSHA256(os.path.join(
139 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 22:46:22140 autoupdate.Autoupdate._GetSize(os.path.join(
141 self.static_image_dir, 'update.gz')).AndReturn(self.size)
142 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-26 00:34:49143 self.hash, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 22:47:40144 self.payload)
Chris Sosa0356d3b2010-09-16 22:46:22145
146 self.mox.ReplayAll()
147 au_mock = self._DummyAutoupdateConstructor()
148 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 21:47:02149 curr_host_info = au_mock.host_infos.GetHostInfo('127.0.0.1');
150 self.assertEqual(curr_host_info.GetAttr('last_known_version'),
151 'ForcedUpdate')
152 self.assertEqual(curr_host_info.GetAttr('last_event_type'),
153 self.test_dict['event_type'])
154 self.assertEqual(curr_host_info.GetAttr('last_event_status'),
155 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 22:46:22156 self.mox.VerifyAll()
157
Don Garrett0ad09372010-12-07 00:20:30158 def testChangeUrlPort(self):
159 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
160 self.assertEqual(r, 'http://fuzzy:8085/static')
161
162 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
163 self.assertEqual(r, 'http://fuzzy:8085/static')
164
165 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
166 self.assertEqual(r, 'ftp://fuzzy:8085/static')
167
168 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
169 self.assertEqual(r, 'ftp://fuzzy:8085')
170
Dale Curtisc9aaf3a2011-08-09 22:47:40171 def testHandleHostInfoPing(self):
172 au_mock = self._DummyAutoupdateConstructor()
173 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
174
Gilad Arnold286a0062012-01-12 21:47:02175 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 22:47:40176 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 21:47:02177 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 22:47:40178 self.assertEqual(
179 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
180
181 def testHandleSetUpdatePing(self):
182 au_mock = self._DummyAutoupdateConstructor()
183 test_ip = '1.2.3.4'
184 test_label = 'test/old-update'
185 self.assertRaises(
186 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
187 self.assertRaises(
188 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
189 self.assertRaises(
190 AssertionError, au_mock.HandleSetUpdatePing, None, None)
191
192 au_mock.HandleSetUpdatePing(test_ip, test_label)
193 self.assertEqual(
Gilad Arnold286a0062012-01-12 21:47:02194 au_mock.host_infos.GetHostInfo(test_ip).GetAttr('forced_update_label'),
195 test_label)
Dale Curtisc9aaf3a2011-08-09 22:47:40196
197 def testHandleUpdatePingWithSetUpdate(self):
198 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
199
200 test_data = _TEST_REQUEST % self.test_dict
201 test_label = 'new_update-test/the-new-update'
202 new_image_dir = os.path.join(self.static_image_dir, test_label)
203 new_url = self.url.replace('update.gz', test_label + '/update.gz')
204
205 autoupdate.Autoupdate.GenerateLatestUpdateImage(
206 self.test_board, 'ForcedUpdate', new_image_dir).AndReturn(
207 'update.gz')
208 autoupdate.Autoupdate._GetHash(os.path.join(
209 new_image_dir, 'update.gz')).AndReturn(self.hash)
210 autoupdate.Autoupdate._GetSHA256(os.path.join(
211 new_image_dir, 'update.gz')).AndReturn(self.sha256)
212 autoupdate.Autoupdate._GetSize(os.path.join(
213 new_image_dir, 'update.gz')).AndReturn(self.size)
214 autoupdate.Autoupdate.GetUpdatePayload(
215 self.hash, self.sha256, self.size, new_url, False).AndReturn(
216 self.payload)
217
218 self.mox.ReplayAll()
219 au_mock = self._DummyAutoupdateConstructor()
220 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
221 self.assertEqual(
Gilad Arnold286a0062012-01-12 21:47:02222 au_mock.host_infos.GetHostInfo('127.0.0.1').
223 GetAttr('forced_update_label'),
224 test_label)
Dale Curtisc9aaf3a2011-08-09 22:47:40225 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 21:47:02226 self.assertFalse('forced_update_label' in
227 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 22:47:40228
Daniel Erat8a0bc4a2011-09-30 15:52:52229 def testGetVersionFromDir(self):
230 au = self._DummyAutoupdateConstructor()
231
232 # New-style version number.
233 self.assertEqual(
234 au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
235 '1102.0.2011_09_30_0806')
236
237 # Old-style version number.
238 self.assertEqual(
239 au._GetVersionFromDir('/foo/x86-alex/0.15.938.2011_08_23_0941-a1'),
240 '0.15.938.2011_08_23_0941')
241
242 def testCanUpdate(self):
243 au = self._DummyAutoupdateConstructor()
244
245 # When both the client and the server have new-style versions, we should
246 # just compare the tokens directly.
247 self.assertTrue(
248 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
249 self.assertTrue(
250 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
251 self.assertFalse(
252 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
253 self.assertFalse(
254 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
255
256 # When the device has an old four-token version number, we should skip the
257 # first two tokens and compare the rest. If there's a tie, go with the
258 # server's version.
259 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.1'))
260 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.0'))
261 self.assertFalse(au._CanUpdate('0.16.892.0', '890.0.0'))
262
263 # Test the case where both the client and the server have old-style
264 # versions.
265 self.assertTrue(au._CanUpdate('0.16.892.0', '0.16.892.1'))
266 self.assertFalse(au._CanUpdate('0.16.892.0', '0.16.892.0'))
267
Chris Sosa0356d3b2010-09-16 22:46:22268
Gilad Arnold286a0062012-01-12 21:47:02269suite = unittest.TestLoader().loadTestsFromTestCase(AutoupdateTest)
270unittest.TextTestRunner(verbosity=3).run(suite)