blob: 2fd88a7933afbfd91249b2d09c1fa1599aeaecb6 [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')
Chris Sosa7c931362010-10-11 19:49:01 -070037 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070038 self.test_board = 'test-board'
39 self.build_root = '/src_path/build/images'
40 self.latest_dir = '12345_af_12-a1'
41 self.latest_verision = '12345_af_12'
42 self.static_image_dir = '/tmp/static-dir/'
Chris Sosa7c931362010-10-11 19:49:01 -070043 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070044 self.test_dict = {
45 'client': 'ChromeOSUpdateEngine-1.0',
46 'version': 'ForcedUpdate',
47 'track': 'unused_var',
48 'board': self.test_board,
49 'event_result': 2,
50 'event_type': 3
51 }
Chris Sosa0356d3b2010-09-16 15:46:22 -070052 self.test_data = _TEST_REQUEST % self.test_dict
53 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
54 self.hash = 12345
55 self.size = 54321
56 self.url = 'http://%s/static/update.gz' % self.hostname
57 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070058 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070059 cherrypy.request.base = 'http://%s' % self.hostname
60
Chris Sosa0356d3b2010-09-16 15:46:22 -070061 def _DummyAutoupdateConstructor(self):
62 """Creates a dummy autoupdater. Used to avoid using constructor."""
63 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070064 static_dir=self.static_image_dir,
65 port=self.port)
Chris Sosa0356d3b2010-09-16 15:46:22 -070066 return dummy
67
Chris Sosa744e1472011-09-07 19:32:50 -070068 def testGetRightSignedDeltaPayloadDir(self):
69 """Test that our directory is what we expect it to be for signed updates."""
Gilad Arnold55a2a372012-10-02 09:46:32 -070070 self.mox.StubOutWithMock(common_util, 'GetFileMd5')
Chris Sosa744e1472011-09-07 19:32:50 -070071 key_path = 'test_key_path'
72 src_image = 'test_src_image'
73 target_image = 'test_target_image'
Gilad Arnold55a2a372012-10-02 09:46:32 -070074 src_hash = '12345'
75 target_hash = '67890'
76 key_hash = 'abcde'
Chris Sosa744e1472011-09-07 19:32:50 -070077
Gilad Arnold55a2a372012-10-02 09:46:32 -070078 common_util.GetFileMd5(src_image).AndReturn(src_hash)
79 common_util.GetFileMd5(target_image).AndReturn(target_hash)
80 common_util.GetFileMd5(key_path).AndReturn(key_hash)
Chris Sosa744e1472011-09-07 19:32:50 -070081
82 self.mox.ReplayAll()
83 au_mock = self._DummyAutoupdateConstructor()
84 au_mock.private_key = key_path
85 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 15:31:36 -040086 self.assertEqual(os.path.basename(update_dir),
Gilad Arnold55a2a372012-10-02 09:46:32 -070087 '%s_%s+%s+patched_kernel' %
88 (src_hash, target_hash, key_hash))
Chris Sosa744e1472011-09-07 19:32:50 -070089 self.mox.VerifyAll()
90
Chris Sosa0356d3b2010-09-16 15:46:22 -070091 def testGenerateLatestUpdateImageWithForced(self):
Don Garrettf90edf02010-11-16 17:36:14 -080092 self.mox.StubOutWithMock(autoupdate.Autoupdate,
93 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -070094 autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn(
95 '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir))
Don Garrettf90edf02010-11-16 17:36:14 -080096 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosa0356d3b2010-09-16 15:46:22 -070097 '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board,
98 self.latest_dir),
Don Garrettf90edf02010-11-16 17:36:14 -080099 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700100
101 self.mox.ReplayAll()
102 au_mock = self._DummyAutoupdateConstructor()
103 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
104 'ForcedUpdate',
105 self.static_image_dir))
106 self.mox.VerifyAll()
107
108 def testHandleUpdatePingForForcedImage(self):
Don Garrettf90edf02010-11-16 17:36:14 -0800109 self.mox.StubOutWithMock(autoupdate.Autoupdate,
110 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700111
112 test_data = _TEST_REQUEST % self.test_dict
113
Don Garrettf90edf02010-11-16 17:36:14 -0800114 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosaa387a872010-09-29 11:51:36 -0700115 self.forced_image_path,
Don Garrettf90edf02010-11-16 17:36:14 -0800116 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Gilad Arnold55a2a372012-10-02 09:46:32 -0700117 common_util.GetFileSha1(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700118 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700119 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 11:51:36 -0700120 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700121 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700122 self.static_image_dir, 'update.gz')).AndReturn(self.size)
123 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700124 self.hash, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700125 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700126
127 self.mox.ReplayAll()
128 au_mock = self._DummyAutoupdateConstructor()
129 au_mock.forced_image = self.forced_image_path
130 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
131 self.mox.VerifyAll()
132
133 def testHandleUpdatePingForLatestImage(self):
134 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
135
136 test_data = _TEST_REQUEST % self.test_dict
137
138 autoupdate.Autoupdate.GenerateLatestUpdateImage(
Don Garrettf90edf02010-11-16 17:36:14 -0800139 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(
140 'update.gz')
Gilad Arnold55a2a372012-10-02 09:46:32 -0700141 common_util.GetFileSha1(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700142 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700143 common_util.GetFileSha256(os.path.join(
Chris Sosaa387a872010-09-29 11:51:36 -0700144 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700145 common_util.GetFileSize(os.path.join(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700146 self.static_image_dir, 'update.gz')).AndReturn(self.size)
147 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700148 self.hash, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700149 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700150
151 self.mox.ReplayAll()
152 au_mock = self._DummyAutoupdateConstructor()
153 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800154 curr_host_info = au_mock.host_infos.GetHostInfo('127.0.0.1');
155 self.assertEqual(curr_host_info.GetAttr('last_known_version'),
156 'ForcedUpdate')
157 self.assertEqual(curr_host_info.GetAttr('last_event_type'),
158 self.test_dict['event_type'])
159 self.assertEqual(curr_host_info.GetAttr('last_event_status'),
160 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 15:46:22 -0700161 self.mox.VerifyAll()
162
Don Garrett0ad09372010-12-06 16:20:30 -0800163 def testChangeUrlPort(self):
164 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
165 self.assertEqual(r, 'http://fuzzy:8085/static')
166
167 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
168 self.assertEqual(r, 'http://fuzzy:8085/static')
169
170 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
171 self.assertEqual(r, 'ftp://fuzzy:8085/static')
172
173 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
174 self.assertEqual(r, 'ftp://fuzzy:8085')
175
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700176 def testHandleHostInfoPing(self):
177 au_mock = self._DummyAutoupdateConstructor()
178 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
179
Gilad Arnold286a0062012-01-12 13:47:02 -0800180 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700181 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -0800182 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700183 self.assertEqual(
184 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
185
186 def testHandleSetUpdatePing(self):
187 au_mock = self._DummyAutoupdateConstructor()
188 test_ip = '1.2.3.4'
189 test_label = 'test/old-update'
190 self.assertRaises(
191 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
192 self.assertRaises(
193 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
194 self.assertRaises(
195 AssertionError, au_mock.HandleSetUpdatePing, None, None)
196
197 au_mock.HandleSetUpdatePing(test_ip, test_label)
198 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800199 au_mock.host_infos.GetHostInfo(test_ip).GetAttr('forced_update_label'),
200 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700201
202 def testHandleUpdatePingWithSetUpdate(self):
203 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
204
205 test_data = _TEST_REQUEST % self.test_dict
206 test_label = 'new_update-test/the-new-update'
207 new_image_dir = os.path.join(self.static_image_dir, test_label)
208 new_url = self.url.replace('update.gz', test_label + '/update.gz')
209
210 autoupdate.Autoupdate.GenerateLatestUpdateImage(
211 self.test_board, 'ForcedUpdate', new_image_dir).AndReturn(
212 'update.gz')
Gilad Arnold55a2a372012-10-02 09:46:32 -0700213 common_util.GetFileSha1(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700214 new_image_dir, 'update.gz')).AndReturn(self.hash)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700215 common_util.GetFileSha256(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700216 new_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700217 common_util.GetFileSize(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700218 new_image_dir, 'update.gz')).AndReturn(self.size)
219 autoupdate.Autoupdate.GetUpdatePayload(
220 self.hash, self.sha256, self.size, new_url, False).AndReturn(
221 self.payload)
222
223 self.mox.ReplayAll()
224 au_mock = self._DummyAutoupdateConstructor()
225 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
226 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800227 au_mock.host_infos.GetHostInfo('127.0.0.1').
228 GetAttr('forced_update_label'),
229 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700230 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800231 self.assertFalse('forced_update_label' in
232 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700233
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700234 def testGetVersionFromDir(self):
235 au = self._DummyAutoupdateConstructor()
236
237 # New-style version number.
238 self.assertEqual(
239 au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
240 '1102.0.2011_09_30_0806')
241
242 # Old-style version number.
243 self.assertEqual(
244 au._GetVersionFromDir('/foo/x86-alex/0.15.938.2011_08_23_0941-a1'),
245 '0.15.938.2011_08_23_0941')
246
247 def testCanUpdate(self):
248 au = self._DummyAutoupdateConstructor()
249
250 # When both the client and the server have new-style versions, we should
251 # just compare the tokens directly.
252 self.assertTrue(
253 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
254 self.assertTrue(
255 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
256 self.assertFalse(
257 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
258 self.assertFalse(
259 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
260
261 # When the device has an old four-token version number, we should skip the
262 # first two tokens and compare the rest. If there's a tie, go with the
263 # server's version.
264 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.1'))
265 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.0'))
266 self.assertFalse(au._CanUpdate('0.16.892.0', '890.0.0'))
267
268 # Test the case where both the client and the server have old-style
269 # versions.
270 self.assertTrue(au._CanUpdate('0.16.892.0', '0.16.892.1'))
271 self.assertFalse(au._CanUpdate('0.16.892.0', '0.16.892.0'))
272
Chris Sosa0356d3b2010-09-16 15:46:22 -0700273
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700274if __name__ == '__main__':
275 unittest.main()