Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 1 | #!/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 Sosa | 5455586 | 2010-10-26 00:26:17 | [diff] [blame] | 9 | import cherrypy |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 10 | import json |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 11 | import mox |
| 12 | import os |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 13 | import socket |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 14 | import unittest |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 15 | |
| 16 | import 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 Sosa | a387a87 | 2010-09-29 18:51:36 | [diff] [blame] | 21 | <o:updatecheck /> |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 22 | <o:event eventresult="%(event_result)d" eventtype="%(event_type)d" /> |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 23 | </client_test>""" |
| 24 | |
| 25 | |
| 26 | class 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 Sosa | a387a87 | 2010-09-29 18:51:36 | [diff] [blame] | 31 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSHA256') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 32 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload') |
| 33 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir') |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 34 | self.port = 8080 |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 35 | 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 Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 40 | self.hostname = '%s:%s' % (socket.gethostname(), self.port) |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 41 | 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 Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 49 | 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 Sosa | a387a87 | 2010-09-29 18:51:36 | [diff] [blame] | 55 | self.sha256 = 'SHA LA LA' |
Chris Sosa | 5455586 | 2010-10-26 00:26:17 | [diff] [blame] | 56 | cherrypy.request.base = 'http://%s' % self.hostname |
| 57 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 58 | def _DummyAutoupdateConstructor(self): |
| 59 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
| 60 | dummy = autoupdate.Autoupdate(root_dir=None, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 61 | static_dir=self.static_image_dir, |
| 62 | port=self.port) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 63 | dummy.client_prefix = 'ChromeOSUpdateEngine' |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 64 | return dummy |
| 65 | |
Chris Sosa | 744e147 | 2011-09-08 02:32:50 | [diff] [blame] | 66 | 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 Zawalski | 1695453 | 2012-03-20 19:31:36 | [diff] [blame] | 72 | hashes = ['12345', '67890', 'abcde', 'patched_kernel'] |
Chris Sosa | 744e147 | 2011-09-08 02:32:50 | [diff] [blame] | 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) |
Scott Zawalski | 1695453 | 2012-03-20 19:31:36 | [diff] [blame] | 82 | self.assertEqual(os.path.basename(update_dir), |
| 83 | '%s_%s+%s+%s' % tuple(hashes)) |
Chris Sosa | 744e147 | 2011-09-08 02:32:50 | [diff] [blame] | 84 | self.mox.VerifyAll() |
| 85 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 86 | def testGenerateLatestUpdateImageWithForced(self): |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 87 | self.mox.StubOutWithMock(autoupdate.Autoupdate, |
| 88 | 'GenerateUpdateImageWithCache') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 89 | autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn( |
| 90 | '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir)) |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 91 | autoupdate.Autoupdate.GenerateUpdateImageWithCache( |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 92 | '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board, |
| 93 | self.latest_dir), |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 94 | static_image_dir=self.static_image_dir).AndReturn('update.gz') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 95 | |
| 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 Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 104 | self.mox.StubOutWithMock(autoupdate.Autoupdate, |
| 105 | 'GenerateUpdateImageWithCache') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 106 | |
| 107 | test_data = _TEST_REQUEST % self.test_dict |
| 108 | |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 109 | autoupdate.Autoupdate.GenerateUpdateImageWithCache( |
Chris Sosa | a387a87 | 2010-09-29 18:51:36 | [diff] [blame] | 110 | self.forced_image_path, |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 111 | static_image_dir=self.static_image_dir).AndReturn('update.gz') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 112 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 113 | self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
Chris Sosa | a387a87 | 2010-09-29 18:51:36 | [diff] [blame] | 114 | autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 115 | self.static_image_dir, 'update.gz')).AndReturn(self.sha256) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 116 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 117 | self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 118 | autoupdate.Autoupdate.GetUpdatePayload( |
Andrew de los Reyes | 5679b97 | 2010-10-26 00:34:49 | [diff] [blame] | 119 | self.hash, self.sha256, self.size, self.url, False).AndReturn( |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 120 | self.payload) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 121 | |
| 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 Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 134 | self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn( |
| 135 | 'update.gz') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 136 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 137 | self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
Chris Sosa | a387a87 | 2010-09-29 18:51:36 | [diff] [blame] | 138 | autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 139 | self.static_image_dir, 'update.gz')).AndReturn(self.sha256) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 140 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 141 | self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 142 | autoupdate.Autoupdate.GetUpdatePayload( |
Andrew de los Reyes | 5679b97 | 2010-10-26 00:34:49 | [diff] [blame] | 143 | self.hash, self.sha256, self.size, self.url, False).AndReturn( |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 144 | self.payload) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 145 | |
| 146 | self.mox.ReplayAll() |
| 147 | au_mock = self._DummyAutoupdateConstructor() |
| 148 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 149 | 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 Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 156 | self.mox.VerifyAll() |
| 157 | |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 158 | 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 Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 171 | def testHandleHostInfoPing(self): |
| 172 | au_mock = self._DummyAutoupdateConstructor() |
| 173 | self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None) |
| 174 | |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 175 | # Setup fake host_infos entry and ensure it comes back to us in one piece. |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 176 | test_ip = '1.2.3.4' |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 177 | au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 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( |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 194 | au_mock.host_infos.GetHostInfo(test_ip).GetAttr('forced_update_label'), |
| 195 | test_label) |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 196 | |
| 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 Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 222 | au_mock.host_infos.GetHostInfo('127.0.0.1'). |
| 223 | GetAttr('forced_update_label'), |
| 224 | test_label) |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 225 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 226 | self.assertFalse('forced_update_label' in |
| 227 | au_mock.host_infos.GetHostInfo('127.0.0.1').attrs) |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 228 | |
Daniel Erat | 8a0bc4a | 2011-09-30 15:52:52 | [diff] [blame] | 229 | 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 Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 268 | |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 269 | suite = unittest.TestLoader().loadTestsFromTestCase(AutoupdateTest) |
| 270 | unittest.TextTestRunner(verbosity=3).run(suite) |