Luis Hector Chavez | a151805 | 2018-06-14 15:19:34 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 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 | |
Don Garrett | fb15e32 | 2016-06-22 02:12:08 | [diff] [blame] | 9 | from __future__ import print_function |
| 10 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 11 | import json |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 12 | import os |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 13 | import shutil |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 14 | import socket |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 15 | import tempfile |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 16 | import unittest |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 17 | |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 18 | import cherrypy |
| 19 | import mox |
| 20 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 21 | import autoupdate |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 22 | import autoupdate_lib |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 23 | import common_util |
joychen | 7c2054a | 2013-07-25 18:14:07 | [diff] [blame] | 24 | import devserver_constants as constants |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 25 | import xbuddy |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 26 | |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 27 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 28 | _TEST_REQUEST = """ |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 29 | <client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" protocol="3.0"> |
| 30 | <app version="%(version)s" track="%(track)s" board="%(board)s" /> |
| 31 | <updatecheck /> |
| 32 | <event eventresult="%(event_result)d" eventtype="%(event_type)d" /> |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 33 | </client_test>""" |
| 34 | |
Chris Sosa | 4b95160 | 2014-04-10 03:26:07 | [diff] [blame] | 35 | # Test request with additional fields needed for full Omaha protocol. |
| 36 | _FULL_TEST_REQUEST = """ |
| 37 | <client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" protocol="3.0"> |
| 38 | <app version="%(version)s" track="%(track)s" board="%(board)s" |
| 39 | hardware_class="Test Device" /> |
| 40 | <updatecheck /> |
| 41 | <event eventresult="%(event_result)d" eventtype="%(event_type)d" /> |
| 42 | </client_test>""" |
| 43 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 44 | #pylint: disable=W0212 |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 45 | class AutoupdateTest(mox.MoxTestBase): |
Chris Sosa | 4b95160 | 2014-04-10 03:26:07 | [diff] [blame] | 46 | """Tests for the autoupdate.Autoupdate class.""" |
| 47 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 48 | def setUp(self): |
| 49 | mox.MoxTestBase.setUp(self) |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 50 | self.mox.StubOutWithMock(common_util, 'GetFileSize') |
| 51 | self.mox.StubOutWithMock(common_util, 'GetFileSha1') |
| 52 | self.mox.StubOutWithMock(common_util, 'GetFileSha256') |
Chris Sosa | c4e8784 | 2013-08-17 01:04:14 | [diff] [blame] | 53 | self.mox.StubOutWithMock(common_util, 'IsInsideChroot') |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 54 | self.mox.StubOutWithMock(autoupdate_lib, 'GetUpdateResponse') |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 55 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs') |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 56 | self.port = 8080 |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 57 | self.test_board = 'test-board' |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 58 | self.build_root = tempfile.mkdtemp('autoupdate_build_root') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 59 | self.latest_dir = '12345_af_12-a1' |
| 60 | self.latest_verision = '12345_af_12' |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 61 | self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir') |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 62 | self.hostname = '%s:%s' % (socket.gethostname(), self.port) |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 63 | self.test_dict = { |
| 64 | 'client': 'ChromeOSUpdateEngine-1.0', |
| 65 | 'version': 'ForcedUpdate', |
Chris Sosa | 4b95160 | 2014-04-10 03:26:07 | [diff] [blame] | 66 | 'track': 'test-channel', |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 67 | 'board': self.test_board, |
| 68 | 'event_result': 2, |
| 69 | 'event_type': 3 |
| 70 | } |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 71 | self.test_data = _TEST_REQUEST % self.test_dict |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 72 | self.sha1 = 12345 |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 73 | self.size = 54321 |
| 74 | self.url = 'http://%s/static/update.gz' % self.hostname |
| 75 | self.payload = 'My payload' |
Chris Sosa | a387a87 | 2010-09-29 18:51:36 | [diff] [blame] | 76 | self.sha256 = 'SHA LA LA' |
Chris Sosa | 5455586 | 2010-10-26 00:26:17 | [diff] [blame] | 77 | cherrypy.request.base = 'http://%s' % self.hostname |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 78 | common_util.MkDirP(self.static_image_dir) |
| 79 | self._xbuddy = xbuddy.XBuddy(False, |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 80 | static_dir=self.static_image_dir) |
| 81 | self.mox.StubOutWithMock(xbuddy.XBuddy, '_GetArtifact') |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 82 | |
| 83 | def tearDown(self): |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 84 | shutil.rmtree(self.build_root) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 85 | shutil.rmtree(self.static_image_dir) |
Chris Sosa | 5455586 | 2010-10-26 00:26:17 | [diff] [blame] | 86 | |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 87 | def _DummyAutoupdateConstructor(self, **kwargs): |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 88 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 89 | dummy = autoupdate.Autoupdate(self._xbuddy, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 90 | static_dir=self.static_image_dir, |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 91 | **kwargs) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 92 | return dummy |
| 93 | |
Chris Sosa | 744e147 | 2011-09-08 02:32:50 | [diff] [blame] | 94 | def testGetRightSignedDeltaPayloadDir(self): |
| 95 | """Test that our directory is what we expect it to be for signed updates.""" |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 96 | self.mox.StubOutWithMock(common_util, 'GetFileMd5') |
Chris Sosa | 744e147 | 2011-09-08 02:32:50 | [diff] [blame] | 97 | key_path = 'test_key_path' |
| 98 | src_image = 'test_src_image' |
| 99 | target_image = 'test_target_image' |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 100 | src_hash = '12345' |
| 101 | target_hash = '67890' |
| 102 | key_hash = 'abcde' |
Chris Sosa | 744e147 | 2011-09-08 02:32:50 | [diff] [blame] | 103 | |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 104 | common_util.GetFileMd5(src_image).AndReturn(src_hash) |
| 105 | common_util.GetFileMd5(target_image).AndReturn(target_hash) |
| 106 | common_util.GetFileMd5(key_path).AndReturn(key_hash) |
Chris Sosa | 744e147 | 2011-09-08 02:32:50 | [diff] [blame] | 107 | |
| 108 | self.mox.ReplayAll() |
| 109 | au_mock = self._DummyAutoupdateConstructor() |
| 110 | au_mock.private_key = key_path |
| 111 | update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image) |
Scott Zawalski | 1695453 | 2012-03-20 19:31:36 | [diff] [blame] | 112 | self.assertEqual(os.path.basename(update_dir), |
Gabe Black | 7099486 | 2014-09-05 07:50:58 | [diff] [blame] | 113 | '%s_%s+%s' % (src_hash, target_hash, key_hash)) |
Chris Sosa | 744e147 | 2011-09-08 02:32:50 | [diff] [blame] | 114 | self.mox.VerifyAll() |
| 115 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 116 | def testGenerateLatestUpdateImage(self): |
| 117 | """Test default behavior in response to plain update call.""" |
| 118 | latest_label = os.path.join(self.test_board, self.latest_dir) |
| 119 | # Generate a fake latest image |
| 120 | latest_image_dir = os.path.join(self.static_image_dir, latest_label) |
| 121 | common_util.MkDirP(latest_image_dir) |
| 122 | image = os.path.join(latest_image_dir, constants.TEST_IMAGE_FILE) |
| 123 | with open(image, 'w') as fh: |
| 124 | fh.write('') |
| 125 | |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 126 | self.mox.StubOutWithMock(autoupdate.Autoupdate, |
| 127 | 'GenerateUpdateImageWithCache') |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 128 | au_mock = self._DummyAutoupdateConstructor() |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 129 | |
Chris Sosa | c4e8784 | 2013-08-17 01:04:14 | [diff] [blame] | 130 | common_util.IsInsideChroot().AndReturn(True) |
Bertrand SIMONNET | b3ddc29 | 2015-05-01 00:26:02 | [diff] [blame] | 131 | self._xbuddy._GetArtifact( |
| 132 | [''], board=self.test_board, lookup_only=True, image_dir=None, |
| 133 | version=None).AndReturn((latest_label, constants.TEST_IMAGE_FILE)) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 134 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 135 | au_mock.GenerateUpdateImageWithCache( |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 136 | os.path.join(self.static_image_dir, self.test_board, self.latest_dir, |
Chris Sosa | 7549080 | 2013-10-01 00:21:45 | [diff] [blame] | 137 | constants.TEST_IMAGE_FILE)).AndReturn('update.gz') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 138 | |
| 139 | self.mox.ReplayAll() |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 140 | test_data = _TEST_REQUEST % self.test_dict |
| 141 | self.assertTrue(au_mock.HandleUpdatePing(test_data)) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 142 | self.mox.VerifyAll() |
| 143 | |
| 144 | def testHandleUpdatePingForForcedImage(self): |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 145 | """Test update response to having a forced image.""" |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 146 | self.mox.StubOutWithMock(autoupdate.Autoupdate, |
| 147 | 'GenerateUpdateImageWithCache') |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 148 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile') |
| 149 | au_mock = self._DummyAutoupdateConstructor() |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 150 | test_data = _TEST_REQUEST % self.test_dict |
| 151 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 152 | # Generate a fake image |
| 153 | forced_image_dir = '/tmp/path_to_force/' |
| 154 | forced_image = forced_image_dir + constants.IMAGE_FILE |
| 155 | common_util.MkDirP(forced_image_dir) |
| 156 | with open(forced_image, 'w') as fh: |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 157 | fh.write('') |
| 158 | |
joychen | 18737f3 | 2013-08-17 00:18:12 | [diff] [blame] | 159 | cache_image_dir = os.path.join(self.static_image_dir, 'cache') |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 160 | |
| 161 | # Mock out GenerateUpdateImageWithCache to make an update file in cache |
Chris Sosa | 7549080 | 2013-10-01 00:21:45 | [diff] [blame] | 162 | def mock_fn(_image): |
Don Garrett | fb15e32 | 2016-06-22 02:12:08 | [diff] [blame] | 163 | print('mock_fn') |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 164 | # No good way to introduce an update file during execution. |
Chris Sosa | 7549080 | 2013-10-01 00:21:45 | [diff] [blame] | 165 | cache_dir = os.path.join(self.static_image_dir, 'cache') |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 166 | common_util.MkDirP(cache_dir) |
| 167 | update_image = os.path.join(cache_dir, constants.UPDATE_FILE) |
| 168 | with open(update_image, 'w') as fh: |
| 169 | fh.write('') |
David Zeuthen | 52ccd01 | 2013-10-31 19:58:26 | [diff] [blame] | 170 | metadata_hash = os.path.join(cache_dir, constants.METADATA_HASH_FILE) |
| 171 | with open(metadata_hash, 'w') as fh: |
| 172 | fh.write('') |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 173 | |
Chris Sosa | c4e8784 | 2013-08-17 01:04:14 | [diff] [blame] | 174 | common_util.IsInsideChroot().AndReturn(True) |
Chris Sosa | 7549080 | 2013-10-01 00:21:45 | [diff] [blame] | 175 | au_mock.GenerateUpdateImageWithCache(forced_image).WithSideEffects( |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 176 | mock_fn).AndReturn('cache') |
| 177 | |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 178 | common_util.GetFileSha1(os.path.join( |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 179 | cache_image_dir, 'update.gz')).AndReturn(self.sha1) |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 180 | common_util.GetFileSha256(os.path.join( |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 181 | cache_image_dir, 'update.gz')).AndReturn(self.sha256) |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 182 | common_util.GetFileSize(os.path.join( |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 183 | cache_image_dir, 'update.gz')).AndReturn(self.size) |
| 184 | au_mock._StoreMetadataToFile(cache_image_dir, |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 185 | mox.IsA(autoupdate.UpdateMetadata)) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 186 | forced_url = 'http://%s/static/%s/update.gz' % (self.hostname, |
joychen | 18737f3 | 2013-08-17 00:18:12 | [diff] [blame] | 187 | 'cache') |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 188 | autoupdate_lib.GetUpdateResponse( |
David Zeuthen | 52ccd01 | 2013-10-31 19:58:26 | [diff] [blame] | 189 | self.sha1, self.sha256, self.size, forced_url, False, 0, None, None, |
Luis Hector Chavez | a151805 | 2018-06-14 15:19:34 | [diff] [blame] | 190 | u'3.0', '', False).AndReturn(self.payload) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 191 | |
| 192 | self.mox.ReplayAll() |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 193 | au_mock.forced_image = forced_image |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 194 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 195 | self.mox.VerifyAll() |
| 196 | |
joychen | dbfe6c9 | 2013-08-17 03:03:49 | [diff] [blame] | 197 | def testHandleForcePregenerateXBuddy(self): |
| 198 | """Check pregenerating an xbuddy path. |
| 199 | |
| 200 | A forced image that starts with 'xbuddy:' uses the following path to |
| 201 | obtain an update. |
| 202 | """ |
| 203 | self.mox.StubOutWithMock(autoupdate.Autoupdate, |
| 204 | 'GetUpdateForLabel') |
| 205 | au_mock = self._DummyAutoupdateConstructor() |
| 206 | au_mock.forced_image = "xbuddy:b/v/a" |
| 207 | |
Don Garrett | fb15e32 | 2016-06-22 02:12:08 | [diff] [blame] | 208 | self._xbuddy._GetArtifact( |
| 209 | ['b', 'v', 'a'], |
Gabe Black | 77f3c20 | 2014-09-05 07:43:36 | [diff] [blame] | 210 | image_dir=None).AndReturn(('label', constants.TEST_IMAGE_FILE)) |
joychen | 365a574 | 2013-08-21 17:41:18 | [diff] [blame] | 211 | |
joychen | dbfe6c9 | 2013-08-17 03:03:49 | [diff] [blame] | 212 | au_mock.GetUpdateForLabel( |
| 213 | autoupdate.FORCED_UPDATE, 'b/v/a').AndReturn('p') |
| 214 | self.mox.ReplayAll() |
| 215 | |
| 216 | au_mock.PreGenerateUpdate() |
| 217 | self.mox.VerifyAll() |
| 218 | |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 219 | def testChangeUrlPort(self): |
| 220 | r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085) |
| 221 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 222 | |
| 223 | r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085) |
| 224 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 225 | |
| 226 | r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085) |
| 227 | self.assertEqual(r, 'ftp://fuzzy:8085/static') |
| 228 | |
| 229 | r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085) |
| 230 | self.assertEqual(r, 'ftp://fuzzy:8085') |
| 231 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 232 | def testHandleHostInfoPing(self): |
| 233 | au_mock = self._DummyAutoupdateConstructor() |
| 234 | self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None) |
| 235 | |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 236 | # 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] | 237 | test_ip = '1.2.3.4' |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 238 | au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 239 | self.assertEqual( |
| 240 | json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict) |
| 241 | |
| 242 | def testHandleSetUpdatePing(self): |
| 243 | au_mock = self._DummyAutoupdateConstructor() |
| 244 | test_ip = '1.2.3.4' |
| 245 | test_label = 'test/old-update' |
| 246 | self.assertRaises( |
| 247 | AssertionError, au_mock.HandleSetUpdatePing, test_ip, None) |
| 248 | self.assertRaises( |
| 249 | AssertionError, au_mock.HandleSetUpdatePing, None, test_label) |
| 250 | self.assertRaises( |
| 251 | AssertionError, au_mock.HandleSetUpdatePing, None, None) |
| 252 | |
| 253 | au_mock.HandleSetUpdatePing(test_ip, test_label) |
| 254 | self.assertEqual( |
Chris Sosa | 1885d03 | 2012-11-30 01:07:27 | [diff] [blame] | 255 | au_mock.host_infos.GetHostInfo(test_ip).attrs['forced_update_label'], |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 256 | test_label) |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 257 | |
| 258 | def testHandleUpdatePingWithSetUpdate(self): |
joychen | 7c2054a | 2013-07-25 18:14:07 | [diff] [blame] | 259 | """If update is set, it should use the update found in that directory.""" |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 260 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile') |
| 261 | au_mock = self._DummyAutoupdateConstructor() |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 262 | |
| 263 | test_data = _TEST_REQUEST % self.test_dict |
| 264 | test_label = 'new_update-test/the-new-update' |
| 265 | new_image_dir = os.path.join(self.static_image_dir, test_label) |
| 266 | new_url = self.url.replace('update.gz', test_label + '/update.gz') |
| 267 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 268 | # Generate a fake payload. |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 269 | common_util.MkDirP(new_image_dir) |
joychen | 7c2054a | 2013-07-25 18:14:07 | [diff] [blame] | 270 | update_gz = os.path.join(new_image_dir, constants.UPDATE_FILE) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 271 | with open(update_gz, 'w') as fh: |
| 272 | fh.write('') |
David Zeuthen | 52ccd01 | 2013-10-31 19:58:26 | [diff] [blame] | 273 | metadata_hash = os.path.join(new_image_dir, constants.METADATA_HASH_FILE) |
| 274 | with open(metadata_hash, 'w') as fh: |
| 275 | fh.write('') |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 276 | |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 277 | common_util.GetFileSha1(os.path.join( |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 278 | new_image_dir, 'update.gz')).AndReturn(self.sha1) |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 279 | common_util.GetFileSha256(os.path.join( |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 280 | new_image_dir, 'update.gz')).AndReturn(self.sha256) |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 281 | common_util.GetFileSize(os.path.join( |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 282 | new_image_dir, 'update.gz')).AndReturn(self.size) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 283 | au_mock._StoreMetadataToFile(new_image_dir, |
| 284 | mox.IsA(autoupdate.UpdateMetadata)) |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 285 | autoupdate_lib.GetUpdateResponse( |
David Zeuthen | 52ccd01 | 2013-10-31 19:58:26 | [diff] [blame] | 286 | self.sha1, self.sha256, self.size, new_url, False, 0, None, None, |
Luis Hector Chavez | a151805 | 2018-06-14 15:19:34 | [diff] [blame] | 287 | u'3.0', '', False).AndReturn(self.payload) |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 288 | |
| 289 | self.mox.ReplayAll() |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 290 | au_mock.HandleSetUpdatePing('127.0.0.1', test_label) |
| 291 | self.assertEqual( |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 292 | au_mock.host_infos.GetHostInfo('127.0.0.1'). |
Chris Sosa | 1885d03 | 2012-11-30 01:07:27 | [diff] [blame] | 293 | attrs['forced_update_label'], |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 294 | test_label) |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 295 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
Don Garrett | fb15e32 | 2016-06-22 02:12:08 | [diff] [blame] | 296 | self.assertFalse( |
| 297 | 'forced_update_label' in |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 298 | au_mock.host_infos.GetHostInfo('127.0.0.1').attrs) |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 299 | |
Daniel Erat | 8a0bc4a | 2011-09-30 15:52:52 | [diff] [blame] | 300 | def testGetVersionFromDir(self): |
| 301 | au = self._DummyAutoupdateConstructor() |
| 302 | |
| 303 | # New-style version number. |
| 304 | self.assertEqual( |
| 305 | au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'), |
| 306 | '1102.0.2011_09_30_0806') |
| 307 | |
Daniel Erat | 8a0bc4a | 2011-09-30 15:52:52 | [diff] [blame] | 308 | def testCanUpdate(self): |
| 309 | au = self._DummyAutoupdateConstructor() |
| 310 | |
| 311 | # When both the client and the server have new-style versions, we should |
| 312 | # just compare the tokens directly. |
| 313 | self.assertTrue( |
| 314 | au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806')) |
| 315 | self.assertTrue( |
| 316 | au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000')) |
| 317 | self.assertFalse( |
| 318 | au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000')) |
| 319 | self.assertFalse( |
| 320 | au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000')) |
| 321 | |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 322 | def testHandleUpdatePingRemotePayload(self): |
| 323 | remote_urlbase = 'http://remotehost:6666' |
| 324 | remote_payload_path = 'static/path/to/update.gz' |
| 325 | remote_url = '/'.join([remote_urlbase, remote_payload_path, 'update.gz']) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 326 | au_mock = self._DummyAutoupdateConstructor(urlbase=remote_urlbase, |
| 327 | payload_path=remote_payload_path, |
| 328 | remote_payload=True) |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 329 | |
Chris Sosa | 4b95160 | 2014-04-10 03:26:07 | [diff] [blame] | 330 | incomplete_test_data = _TEST_REQUEST % self.test_dict |
| 331 | complete_test_data = _FULL_TEST_REQUEST % self.test_dict |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 332 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 333 | au_mock._GetRemotePayloadAttrs(remote_url).AndReturn( |
David Zeuthen | 52ccd01 | 2013-10-31 19:58:26 | [diff] [blame] | 334 | autoupdate.UpdateMetadata(self.sha1, self.sha256, self.size, False, |
| 335 | 0, '')) |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 336 | autoupdate_lib.GetUpdateResponse( |
David Zeuthen | 52ccd01 | 2013-10-31 19:58:26 | [diff] [blame] | 337 | self.sha1, self.sha256, self.size, remote_url, False, 0, None, None, |
Luis Hector Chavez | a151805 | 2018-06-14 15:19:34 | [diff] [blame] | 338 | u'3.0', '', False).AndReturn(self.payload) |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 339 | |
| 340 | self.mox.ReplayAll() |
Chris Sosa | 4b95160 | 2014-04-10 03:26:07 | [diff] [blame] | 341 | # This should fail because of missing fields. |
| 342 | self.assertRaises(common_util.DevServerHTTPError, |
| 343 | au_mock.HandleUpdatePing, incomplete_test_data) |
| 344 | # This should have enough information. |
| 345 | self.assertEqual(au_mock.HandleUpdatePing(complete_test_data), self.payload) |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 346 | self.mox.VerifyAll() |
| 347 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 348 | |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 349 | if __name__ == '__main__': |
| 350 | unittest.main() |