blob: 2302cf4c3eac9946b8c5a91ea78596efb46b5c5f [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
Chris Sosa54555862010-10-25 17:26:17 -07009import cherrypy
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070010import json
Chris Sosa0356d3b2010-09-16 15:46:22 -070011import mox
12import os
Chris Sosa7c931362010-10-11 19:49:01 -070013import socket
Chris Sosa0356d3b2010-09-16 15:46:22 -070014import unittest
Chris Sosa0356d3b2010-09-16 15:46:22 -070015
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 11:51:36 -070021 <o:updatecheck />
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070022 <o:event eventresult="%(event_result)d" eventtype="%(event_type)d" />
Chris Sosa0356d3b2010-09-16 15:46:22 -070023</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 11:51:36 -070031 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSHA256')
Chris Sosa0356d3b2010-09-16 15:46:22 -070032 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload')
33 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
Chris Sosa7c931362010-10-11 19:49:01 -070034 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070035 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-11 19:49:01 -070040 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070041 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 15:46:22 -070049 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 11:51:36 -070055 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070056 cherrypy.request.base = 'http://%s' % self.hostname
57
Chris Sosa0356d3b2010-09-16 15:46:22 -070058 def _DummyAutoupdateConstructor(self):
59 """Creates a dummy autoupdater. Used to avoid using constructor."""
60 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070061 static_dir=self.static_image_dir,
62 port=self.port)
Chris Sosa0356d3b2010-09-16 15:46:22 -070063 return dummy
64
Chris Sosa744e1472011-09-07 19:32:50 -070065 def testGetRightSignedDeltaPayloadDir(self):
66 """Test that our directory is what we expect it to be for signed updates."""
67 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetMd5')
68 key_path = 'test_key_path'
69 src_image = 'test_src_image'
70 target_image = 'test_target_image'
Scott Zawalski16954532012-03-20 15:31:36 -040071 hashes = ['12345', '67890', 'abcde', 'patched_kernel']
Chris Sosa744e1472011-09-07 19:32:50 -070072
73 autoupdate.Autoupdate._GetMd5(target_image).AndReturn(hashes[1])
74 autoupdate.Autoupdate._GetMd5(src_image).AndReturn(hashes[0])
75 autoupdate.Autoupdate._GetMd5(key_path).AndReturn(hashes[2])
76
77 self.mox.ReplayAll()
78 au_mock = self._DummyAutoupdateConstructor()
79 au_mock.private_key = key_path
80 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 15:31:36 -040081 self.assertEqual(os.path.basename(update_dir),
82 '%s_%s+%s+%s' % tuple(hashes))
Chris Sosa744e1472011-09-07 19:32:50 -070083 self.mox.VerifyAll()
84
Chris Sosa0356d3b2010-09-16 15:46:22 -070085 def testGenerateLatestUpdateImageWithForced(self):
Don Garrettf90edf02010-11-16 17:36:14 -080086 self.mox.StubOutWithMock(autoupdate.Autoupdate,
87 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -070088 autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn(
89 '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir))
Don Garrettf90edf02010-11-16 17:36:14 -080090 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosa0356d3b2010-09-16 15:46:22 -070091 '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board,
92 self.latest_dir),
Don Garrettf90edf02010-11-16 17:36:14 -080093 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -070094
95 self.mox.ReplayAll()
96 au_mock = self._DummyAutoupdateConstructor()
97 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
98 'ForcedUpdate',
99 self.static_image_dir))
100 self.mox.VerifyAll()
101
102 def testHandleUpdatePingForForcedImage(self):
Don Garrettf90edf02010-11-16 17:36:14 -0800103 self.mox.StubOutWithMock(autoupdate.Autoupdate,
104 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700105
106 test_data = _TEST_REQUEST % self.test_dict
107
Don Garrettf90edf02010-11-16 17:36:14 -0800108 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosaa387a872010-09-29 11:51:36 -0700109 self.forced_image_path,
Don Garrettf90edf02010-11-16 17:36:14 -0800110 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700111 autoupdate.Autoupdate._GetHash(os.path.join(
112 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700113 autoupdate.Autoupdate._GetSHA256(os.path.join(
114 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700115 autoupdate.Autoupdate._GetSize(os.path.join(
116 self.static_image_dir, 'update.gz')).AndReturn(self.size)
117 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700118 self.hash, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700119 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700120
121 self.mox.ReplayAll()
122 au_mock = self._DummyAutoupdateConstructor()
123 au_mock.forced_image = self.forced_image_path
124 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
125 self.mox.VerifyAll()
126
127 def testHandleUpdatePingForLatestImage(self):
128 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
129
130 test_data = _TEST_REQUEST % self.test_dict
131
132 autoupdate.Autoupdate.GenerateLatestUpdateImage(
Don Garrettf90edf02010-11-16 17:36:14 -0800133 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(
134 'update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700135 autoupdate.Autoupdate._GetHash(os.path.join(
136 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700137 autoupdate.Autoupdate._GetSHA256(os.path.join(
138 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700139 autoupdate.Autoupdate._GetSize(os.path.join(
140 self.static_image_dir, 'update.gz')).AndReturn(self.size)
141 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700142 self.hash, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700143 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700144
145 self.mox.ReplayAll()
146 au_mock = self._DummyAutoupdateConstructor()
147 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800148 curr_host_info = au_mock.host_infos.GetHostInfo('127.0.0.1');
149 self.assertEqual(curr_host_info.GetAttr('last_known_version'),
150 'ForcedUpdate')
151 self.assertEqual(curr_host_info.GetAttr('last_event_type'),
152 self.test_dict['event_type'])
153 self.assertEqual(curr_host_info.GetAttr('last_event_status'),
154 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 15:46:22 -0700155 self.mox.VerifyAll()
156
Don Garrett0ad09372010-12-06 16:20:30 -0800157 def testChangeUrlPort(self):
158 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
159 self.assertEqual(r, 'http://fuzzy:8085/static')
160
161 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
162 self.assertEqual(r, 'http://fuzzy:8085/static')
163
164 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
165 self.assertEqual(r, 'ftp://fuzzy:8085/static')
166
167 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
168 self.assertEqual(r, 'ftp://fuzzy:8085')
169
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700170 def testHandleHostInfoPing(self):
171 au_mock = self._DummyAutoupdateConstructor()
172 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
173
Gilad Arnold286a0062012-01-12 13:47:02 -0800174 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700175 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -0800176 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700177 self.assertEqual(
178 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
179
180 def testHandleSetUpdatePing(self):
181 au_mock = self._DummyAutoupdateConstructor()
182 test_ip = '1.2.3.4'
183 test_label = 'test/old-update'
184 self.assertRaises(
185 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
186 self.assertRaises(
187 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
188 self.assertRaises(
189 AssertionError, au_mock.HandleSetUpdatePing, None, None)
190
191 au_mock.HandleSetUpdatePing(test_ip, test_label)
192 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800193 au_mock.host_infos.GetHostInfo(test_ip).GetAttr('forced_update_label'),
194 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700195
196 def testHandleUpdatePingWithSetUpdate(self):
197 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
198
199 test_data = _TEST_REQUEST % self.test_dict
200 test_label = 'new_update-test/the-new-update'
201 new_image_dir = os.path.join(self.static_image_dir, test_label)
202 new_url = self.url.replace('update.gz', test_label + '/update.gz')
203
204 autoupdate.Autoupdate.GenerateLatestUpdateImage(
205 self.test_board, 'ForcedUpdate', new_image_dir).AndReturn(
206 'update.gz')
207 autoupdate.Autoupdate._GetHash(os.path.join(
208 new_image_dir, 'update.gz')).AndReturn(self.hash)
209 autoupdate.Autoupdate._GetSHA256(os.path.join(
210 new_image_dir, 'update.gz')).AndReturn(self.sha256)
211 autoupdate.Autoupdate._GetSize(os.path.join(
212 new_image_dir, 'update.gz')).AndReturn(self.size)
213 autoupdate.Autoupdate.GetUpdatePayload(
214 self.hash, self.sha256, self.size, new_url, False).AndReturn(
215 self.payload)
216
217 self.mox.ReplayAll()
218 au_mock = self._DummyAutoupdateConstructor()
219 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
220 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800221 au_mock.host_infos.GetHostInfo('127.0.0.1').
222 GetAttr('forced_update_label'),
223 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700224 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800225 self.assertFalse('forced_update_label' in
226 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700227
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700228 def testGetVersionFromDir(self):
229 au = self._DummyAutoupdateConstructor()
230
231 # New-style version number.
232 self.assertEqual(
233 au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
234 '1102.0.2011_09_30_0806')
235
236 # Old-style version number.
237 self.assertEqual(
238 au._GetVersionFromDir('/foo/x86-alex/0.15.938.2011_08_23_0941-a1'),
239 '0.15.938.2011_08_23_0941')
240
241 def testCanUpdate(self):
242 au = self._DummyAutoupdateConstructor()
243
244 # When both the client and the server have new-style versions, we should
245 # just compare the tokens directly.
246 self.assertTrue(
247 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
248 self.assertTrue(
249 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
250 self.assertFalse(
251 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
252 self.assertFalse(
253 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
254
255 # When the device has an old four-token version number, we should skip the
256 # first two tokens and compare the rest. If there's a tie, go with the
257 # server's version.
258 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.1'))
259 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.0'))
260 self.assertFalse(au._CanUpdate('0.16.892.0', '890.0.0'))
261
262 # Test the case where both the client and the server have old-style
263 # versions.
264 self.assertTrue(au._CanUpdate('0.16.892.0', '0.16.892.1'))
265 self.assertFalse(au._CanUpdate('0.16.892.0', '0.16.892.0'))
266
Chris Sosa0356d3b2010-09-16 15:46:22 -0700267
Gilad Arnold286a0062012-01-12 13:47:02 -0800268suite = unittest.TestLoader().loadTestsFromTestCase(AutoupdateTest)
269unittest.TextTestRunner(verbosity=3).run(suite)