blob: 13875b9dc2f23bb6a34b4114d5901fef71432f4f [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 dummy.client_prefix = 'ChromeOSUpdateEngine'
Chris Sosa0356d3b2010-09-16 15:46:22 -070064 return dummy
65
Chris Sosa744e1472011-09-07 19:32:50 -070066 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'
72 hashes = ['12345', '67890', 'abcde']
73
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)
82 self.assertEqual(os.path.basename(update_dir), '%s_%s+%s' % tuple(hashes))
83 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)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700148 self.assertEqual(
149 au_mock.host_info['127.0.0.1']['last_known_version'], 'ForcedUpdate')
150 self.assertEqual(
151 au_mock.host_info['127.0.0.1']['last_event_type'],
152 self.test_dict['event_type'])
153 self.assertEqual(
154 au_mock.host_info['127.0.0.1']['last_event_status'],
155 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 15:46:22 -0700156 self.mox.VerifyAll()
157
Don Garrett0ad09372010-12-06 16:20:30 -0800158 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 15:47:40 -0700171 def testHandleHostInfoPing(self):
172 au_mock = self._DummyAutoupdateConstructor()
173 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
174
175 # Setup fake host_info entry and ensure it comes back to us in one piece.
176 test_ip = '1.2.3.4'
177 au_mock.host_info[test_ip] = self.test_dict
178 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(
194 au_mock.host_info[test_ip]['forced_update_label'], test_label)
195
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(
221 au_mock.host_info['127.0.0.1']['forced_update_label'], test_label)
222 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
223 self.assertFalse('forced_update_label' in au_mock.host_info['127.0.0.1'])
224
Chris Sosa0356d3b2010-09-16 15:46:22 -0700225
226if __name__ == '__main__':
227 unittest.main()