blob: 4274ffb4b3bd7c590905d5b3a1ca4ec1e33f8697 [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
18
Gilad Arnoldabb352e2012-09-23 01:24:27 -070019
Chris Sosa0356d3b2010-09-16 15:46:22 -070020_TEST_REQUEST = """
21<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" >
22 <o:app version="%(version)s" track="%(track)s" board="%(board)s" />
Chris Sosaa387a872010-09-29 11:51:36 -070023 <o:updatecheck />
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070024 <o:event eventresult="%(event_result)d" eventtype="%(event_type)d" />
Chris Sosa0356d3b2010-09-16 15:46:22 -070025</client_test>"""
26
27
28class AutoupdateTest(mox.MoxTestBase):
29 def setUp(self):
30 mox.MoxTestBase.setUp(self)
31 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSize')
32 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetHash')
Chris Sosaa387a872010-09-29 11:51:36 -070033 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSHA256')
Chris Sosa0356d3b2010-09-16 15:46:22 -070034 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload')
35 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
Chris Sosa7c931362010-10-11 19:49:01 -070036 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070037 self.test_board = 'test-board'
38 self.build_root = '/src_path/build/images'
39 self.latest_dir = '12345_af_12-a1'
40 self.latest_verision = '12345_af_12'
41 self.static_image_dir = '/tmp/static-dir/'
Chris Sosa7c931362010-10-11 19:49:01 -070042 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070043 self.test_dict = {
44 'client': 'ChromeOSUpdateEngine-1.0',
45 'version': 'ForcedUpdate',
46 'track': 'unused_var',
47 'board': self.test_board,
48 'event_result': 2,
49 'event_type': 3
50 }
Chris Sosa0356d3b2010-09-16 15:46:22 -070051 self.test_data = _TEST_REQUEST % self.test_dict
52 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
53 self.hash = 12345
54 self.size = 54321
55 self.url = 'http://%s/static/update.gz' % self.hostname
56 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070057 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070058 cherrypy.request.base = 'http://%s' % self.hostname
59
Chris Sosa0356d3b2010-09-16 15:46:22 -070060 def _DummyAutoupdateConstructor(self):
61 """Creates a dummy autoupdater. Used to avoid using constructor."""
62 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070063 static_dir=self.static_image_dir,
64 port=self.port)
Chris Sosa0356d3b2010-09-16 15:46:22 -070065 return dummy
66
Chris Sosa744e1472011-09-07 19:32:50 -070067 def testGetRightSignedDeltaPayloadDir(self):
68 """Test that our directory is what we expect it to be for signed updates."""
69 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetMd5')
70 key_path = 'test_key_path'
71 src_image = 'test_src_image'
72 target_image = 'test_target_image'
Scott Zawalski16954532012-03-20 15:31:36 -040073 hashes = ['12345', '67890', 'abcde', 'patched_kernel']
Chris Sosa744e1472011-09-07 19:32:50 -070074
75 autoupdate.Autoupdate._GetMd5(target_image).AndReturn(hashes[1])
76 autoupdate.Autoupdate._GetMd5(src_image).AndReturn(hashes[0])
77 autoupdate.Autoupdate._GetMd5(key_path).AndReturn(hashes[2])
78
79 self.mox.ReplayAll()
80 au_mock = self._DummyAutoupdateConstructor()
81 au_mock.private_key = key_path
82 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 15:31:36 -040083 self.assertEqual(os.path.basename(update_dir),
84 '%s_%s+%s+%s' % tuple(hashes))
Chris Sosa744e1472011-09-07 19:32:50 -070085 self.mox.VerifyAll()
86
Chris Sosa0356d3b2010-09-16 15:46:22 -070087 def testGenerateLatestUpdateImageWithForced(self):
Don Garrettf90edf02010-11-16 17:36:14 -080088 self.mox.StubOutWithMock(autoupdate.Autoupdate,
89 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -070090 autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn(
91 '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir))
Don Garrettf90edf02010-11-16 17:36:14 -080092 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosa0356d3b2010-09-16 15:46:22 -070093 '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board,
94 self.latest_dir),
Don Garrettf90edf02010-11-16 17:36:14 -080095 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -070096
97 self.mox.ReplayAll()
98 au_mock = self._DummyAutoupdateConstructor()
99 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
100 'ForcedUpdate',
101 self.static_image_dir))
102 self.mox.VerifyAll()
103
104 def testHandleUpdatePingForForcedImage(self):
Don Garrettf90edf02010-11-16 17:36:14 -0800105 self.mox.StubOutWithMock(autoupdate.Autoupdate,
106 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700107
108 test_data = _TEST_REQUEST % self.test_dict
109
Don Garrettf90edf02010-11-16 17:36:14 -0800110 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosaa387a872010-09-29 11:51:36 -0700111 self.forced_image_path,
Don Garrettf90edf02010-11-16 17:36:14 -0800112 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700113 autoupdate.Autoupdate._GetHash(os.path.join(
114 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700115 autoupdate.Autoupdate._GetSHA256(os.path.join(
116 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700117 autoupdate.Autoupdate._GetSize(os.path.join(
118 self.static_image_dir, 'update.gz')).AndReturn(self.size)
119 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700120 self.hash, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700121 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700122
123 self.mox.ReplayAll()
124 au_mock = self._DummyAutoupdateConstructor()
125 au_mock.forced_image = self.forced_image_path
126 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
127 self.mox.VerifyAll()
128
129 def testHandleUpdatePingForLatestImage(self):
130 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
131
132 test_data = _TEST_REQUEST % self.test_dict
133
134 autoupdate.Autoupdate.GenerateLatestUpdateImage(
Don Garrettf90edf02010-11-16 17:36:14 -0800135 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(
136 'update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700137 autoupdate.Autoupdate._GetHash(os.path.join(
138 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700139 autoupdate.Autoupdate._GetSHA256(os.path.join(
140 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700141 autoupdate.Autoupdate._GetSize(os.path.join(
142 self.static_image_dir, 'update.gz')).AndReturn(self.size)
143 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700144 self.hash, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700145 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700146
147 self.mox.ReplayAll()
148 au_mock = self._DummyAutoupdateConstructor()
149 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800150 curr_host_info = au_mock.host_infos.GetHostInfo('127.0.0.1');
151 self.assertEqual(curr_host_info.GetAttr('last_known_version'),
152 'ForcedUpdate')
153 self.assertEqual(curr_host_info.GetAttr('last_event_type'),
154 self.test_dict['event_type'])
155 self.assertEqual(curr_host_info.GetAttr('last_event_status'),
156 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 15:46:22 -0700157 self.mox.VerifyAll()
158
Don Garrett0ad09372010-12-06 16:20:30 -0800159 def testChangeUrlPort(self):
160 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
161 self.assertEqual(r, 'http://fuzzy:8085/static')
162
163 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
164 self.assertEqual(r, 'http://fuzzy:8085/static')
165
166 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
167 self.assertEqual(r, 'ftp://fuzzy:8085/static')
168
169 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
170 self.assertEqual(r, 'ftp://fuzzy:8085')
171
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700172 def testHandleHostInfoPing(self):
173 au_mock = self._DummyAutoupdateConstructor()
174 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
175
Gilad Arnold286a0062012-01-12 13:47:02 -0800176 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700177 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -0800178 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700179 self.assertEqual(
180 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
181
182 def testHandleSetUpdatePing(self):
183 au_mock = self._DummyAutoupdateConstructor()
184 test_ip = '1.2.3.4'
185 test_label = 'test/old-update'
186 self.assertRaises(
187 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
188 self.assertRaises(
189 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
190 self.assertRaises(
191 AssertionError, au_mock.HandleSetUpdatePing, None, None)
192
193 au_mock.HandleSetUpdatePing(test_ip, test_label)
194 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800195 au_mock.host_infos.GetHostInfo(test_ip).GetAttr('forced_update_label'),
196 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700197
198 def testHandleUpdatePingWithSetUpdate(self):
199 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
200
201 test_data = _TEST_REQUEST % self.test_dict
202 test_label = 'new_update-test/the-new-update'
203 new_image_dir = os.path.join(self.static_image_dir, test_label)
204 new_url = self.url.replace('update.gz', test_label + '/update.gz')
205
206 autoupdate.Autoupdate.GenerateLatestUpdateImage(
207 self.test_board, 'ForcedUpdate', new_image_dir).AndReturn(
208 'update.gz')
209 autoupdate.Autoupdate._GetHash(os.path.join(
210 new_image_dir, 'update.gz')).AndReturn(self.hash)
211 autoupdate.Autoupdate._GetSHA256(os.path.join(
212 new_image_dir, 'update.gz')).AndReturn(self.sha256)
213 autoupdate.Autoupdate._GetSize(os.path.join(
214 new_image_dir, 'update.gz')).AndReturn(self.size)
215 autoupdate.Autoupdate.GetUpdatePayload(
216 self.hash, self.sha256, self.size, new_url, False).AndReturn(
217 self.payload)
218
219 self.mox.ReplayAll()
220 au_mock = self._DummyAutoupdateConstructor()
221 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
222 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800223 au_mock.host_infos.GetHostInfo('127.0.0.1').
224 GetAttr('forced_update_label'),
225 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700226 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Gilad Arnold286a0062012-01-12 13:47:02 -0800227 self.assertFalse('forced_update_label' in
228 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700229
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700230 def testGetVersionFromDir(self):
231 au = self._DummyAutoupdateConstructor()
232
233 # New-style version number.
234 self.assertEqual(
235 au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
236 '1102.0.2011_09_30_0806')
237
238 # Old-style version number.
239 self.assertEqual(
240 au._GetVersionFromDir('/foo/x86-alex/0.15.938.2011_08_23_0941-a1'),
241 '0.15.938.2011_08_23_0941')
242
243 def testCanUpdate(self):
244 au = self._DummyAutoupdateConstructor()
245
246 # When both the client and the server have new-style versions, we should
247 # just compare the tokens directly.
248 self.assertTrue(
249 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
250 self.assertTrue(
251 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
252 self.assertFalse(
253 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
254 self.assertFalse(
255 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
256
257 # When the device has an old four-token version number, we should skip the
258 # first two tokens and compare the rest. If there's a tie, go with the
259 # server's version.
260 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.1'))
261 self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.0'))
262 self.assertFalse(au._CanUpdate('0.16.892.0', '890.0.0'))
263
264 # Test the case where both the client and the server have old-style
265 # versions.
266 self.assertTrue(au._CanUpdate('0.16.892.0', '0.16.892.1'))
267 self.assertFalse(au._CanUpdate('0.16.892.0', '0.16.892.0'))
268
Chris Sosa0356d3b2010-09-16 15:46:22 -0700269
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700270if __name__ == '__main__':
271 unittest.main()