blob: 833a174dd22bf60779314ef817f3b58dc8690b4d [file] [log] [blame]
Luis Hector Chaveza1518052018-06-14 08:19:34 -07001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
Chris Sosa0356d3b2010-09-16 15:46:22 -07003# 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 Garrettfb15e322016-06-21 19:12:08 -07009from __future__ import print_function
10
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070011import json
Chris Sosa0356d3b2010-09-16 15:46:22 -070012import os
Chris Sosa6a3697f2013-01-29 16:44:43 -080013import shutil
Chris Sosa7c931362010-10-11 19:49:01 -070014import socket
joychen121fc9b2013-08-02 14:30:30 -070015import tempfile
Chris Sosa0356d3b2010-09-16 15:46:22 -070016import unittest
Chris Sosa0356d3b2010-09-16 15:46:22 -070017
Gilad Arnoldabb352e2012-09-23 01:24:27 -070018import cherrypy
19import mox
20
Chris Sosa0356d3b2010-09-16 15:46:22 -070021import autoupdate
Chris Sosa52148582012-11-15 15:35:58 -080022import autoupdate_lib
Gilad Arnold55a2a372012-10-02 09:46:32 -070023import common_util
joychen7c2054a2013-07-25 11:14:07 -070024import devserver_constants as constants
joychen121fc9b2013-08-02 14:30:30 -070025import xbuddy
Chris Sosa0356d3b2010-09-16 15:46:22 -070026
Gilad Arnoldabb352e2012-09-23 01:24:27 -070027
Chris Sosa0356d3b2010-09-16 15:46:22 -070028_TEST_REQUEST = """
Chris Sosa52148582012-11-15 15:35:58 -080029<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 Sosa0356d3b2010-09-16 15:46:22 -070033</client_test>"""
34
Chris Sosa4b951602014-04-09 20:26:07 -070035# 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 Sosa6a3697f2013-01-29 16:44:43 -080044#pylint: disable=W0212
Chris Sosa0356d3b2010-09-16 15:46:22 -070045class AutoupdateTest(mox.MoxTestBase):
Chris Sosa4b951602014-04-09 20:26:07 -070046 """Tests for the autoupdate.Autoupdate class."""
47
Chris Sosa0356d3b2010-09-16 15:46:22 -070048 def setUp(self):
49 mox.MoxTestBase.setUp(self)
Gilad Arnold55a2a372012-10-02 09:46:32 -070050 self.mox.StubOutWithMock(common_util, 'GetFileSize')
51 self.mox.StubOutWithMock(common_util, 'GetFileSha1')
52 self.mox.StubOutWithMock(common_util, 'GetFileSha256')
Chris Sosac4e87842013-08-16 18:04:14 -070053 self.mox.StubOutWithMock(common_util, 'IsInsideChroot')
Chris Sosa52148582012-11-15 15:35:58 -080054 self.mox.StubOutWithMock(autoupdate_lib, 'GetUpdateResponse')
Chris Sosa7c931362010-10-11 19:49:01 -070055 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070056 self.test_board = 'test-board'
joychen121fc9b2013-08-02 14:30:30 -070057 self.build_root = tempfile.mkdtemp('autoupdate_build_root')
Chris Sosa0356d3b2010-09-16 15:46:22 -070058 self.latest_dir = '12345_af_12-a1'
59 self.latest_verision = '12345_af_12'
joychen121fc9b2013-08-02 14:30:30 -070060 self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir')
Chris Sosa7c931362010-10-11 19:49:01 -070061 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070062 self.test_dict = {
63 'client': 'ChromeOSUpdateEngine-1.0',
64 'version': 'ForcedUpdate',
Chris Sosa4b951602014-04-09 20:26:07 -070065 'track': 'test-channel',
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070066 'board': self.test_board,
67 'event_result': 2,
68 'event_type': 3
69 }
Chris Sosa0356d3b2010-09-16 15:46:22 -070070 self.test_data = _TEST_REQUEST % self.test_dict
Gilad Arnold0c9c8602012-10-02 23:58:58 -070071 self.sha1 = 12345
Chris Sosa0356d3b2010-09-16 15:46:22 -070072 self.size = 54321
73 self.url = 'http://%s/static/update.gz' % self.hostname
74 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070075 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070076 cherrypy.request.base = 'http://%s' % self.hostname
joychen121fc9b2013-08-02 14:30:30 -070077 common_util.MkDirP(self.static_image_dir)
78 self._xbuddy = xbuddy.XBuddy(False,
joychen121fc9b2013-08-02 14:30:30 -070079 static_dir=self.static_image_dir)
80 self.mox.StubOutWithMock(xbuddy.XBuddy, '_GetArtifact')
Chris Sosa6a3697f2013-01-29 16:44:43 -080081
82 def tearDown(self):
joychen121fc9b2013-08-02 14:30:30 -070083 shutil.rmtree(self.build_root)
Chris Sosa6a3697f2013-01-29 16:44:43 -080084 shutil.rmtree(self.static_image_dir)
Chris Sosa54555862010-10-25 17:26:17 -070085
Gilad Arnold0c9c8602012-10-02 23:58:58 -070086 def _DummyAutoupdateConstructor(self, **kwargs):
Chris Sosa0356d3b2010-09-16 15:46:22 -070087 """Creates a dummy autoupdater. Used to avoid using constructor."""
joychen121fc9b2013-08-02 14:30:30 -070088 dummy = autoupdate.Autoupdate(self._xbuddy,
Chris Sosa7c931362010-10-11 19:49:01 -070089 static_dir=self.static_image_dir,
Gilad Arnold0c9c8602012-10-02 23:58:58 -070090 **kwargs)
Chris Sosa0356d3b2010-09-16 15:46:22 -070091 return dummy
92
Amin Hassaniabedfaa2019-06-02 21:30:48 -070093 def testGetRightDeltaPayloadDir(self):
94 """Test that our directory is what we expect it to be for updates."""
Gilad Arnold55a2a372012-10-02 09:46:32 -070095 self.mox.StubOutWithMock(common_util, 'GetFileMd5')
Chris Sosa744e1472011-09-07 19:32:50 -070096 src_image = 'test_src_image'
97 target_image = 'test_target_image'
Gilad Arnold55a2a372012-10-02 09:46:32 -070098 src_hash = '12345'
99 target_hash = '67890'
Chris Sosa744e1472011-09-07 19:32:50 -0700100
Gilad Arnold55a2a372012-10-02 09:46:32 -0700101 common_util.GetFileMd5(src_image).AndReturn(src_hash)
102 common_util.GetFileMd5(target_image).AndReturn(target_hash)
Chris Sosa744e1472011-09-07 19:32:50 -0700103
104 self.mox.ReplayAll()
105 au_mock = self._DummyAutoupdateConstructor()
Chris Sosa744e1472011-09-07 19:32:50 -0700106 update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
Scott Zawalski16954532012-03-20 15:31:36 -0400107 self.assertEqual(os.path.basename(update_dir),
Amin Hassaniabedfaa2019-06-02 21:30:48 -0700108 '%s_%s' % (src_hash, target_hash))
Chris Sosa744e1472011-09-07 19:32:50 -0700109 self.mox.VerifyAll()
110
joychen121fc9b2013-08-02 14:30:30 -0700111 def testGenerateLatestUpdateImage(self):
112 """Test default behavior in response to plain update call."""
113 latest_label = os.path.join(self.test_board, self.latest_dir)
114 # Generate a fake latest image
115 latest_image_dir = os.path.join(self.static_image_dir, latest_label)
116 common_util.MkDirP(latest_image_dir)
117 image = os.path.join(latest_image_dir, constants.TEST_IMAGE_FILE)
118 with open(image, 'w') as fh:
119 fh.write('')
120
Don Garrettf90edf02010-11-16 17:36:14 -0800121 self.mox.StubOutWithMock(autoupdate.Autoupdate,
122 'GenerateUpdateImageWithCache')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800123 au_mock = self._DummyAutoupdateConstructor()
joychen121fc9b2013-08-02 14:30:30 -0700124
Chris Sosac4e87842013-08-16 18:04:14 -0700125 common_util.IsInsideChroot().AndReturn(True)
Bertrand SIMONNETb3ddc292015-04-30 17:26:02 -0700126 self._xbuddy._GetArtifact(
127 [''], board=self.test_board, lookup_only=True, image_dir=None,
128 version=None).AndReturn((latest_label, constants.TEST_IMAGE_FILE))
joychen121fc9b2013-08-02 14:30:30 -0700129
Chris Sosa6a3697f2013-01-29 16:44:43 -0800130 au_mock.GenerateUpdateImageWithCache(
joychen121fc9b2013-08-02 14:30:30 -0700131 os.path.join(self.static_image_dir, self.test_board, self.latest_dir,
Chris Sosa75490802013-09-30 17:21:45 -0700132 constants.TEST_IMAGE_FILE)).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700133
134 self.mox.ReplayAll()
joychen121fc9b2013-08-02 14:30:30 -0700135 test_data = _TEST_REQUEST % self.test_dict
136 self.assertTrue(au_mock.HandleUpdatePing(test_data))
Chris Sosa0356d3b2010-09-16 15:46:22 -0700137 self.mox.VerifyAll()
138
139 def testHandleUpdatePingForForcedImage(self):
joychen121fc9b2013-08-02 14:30:30 -0700140 """Test update response to having a forced image."""
Don Garrettf90edf02010-11-16 17:36:14 -0800141 self.mox.StubOutWithMock(autoupdate.Autoupdate,
142 'GenerateUpdateImageWithCache')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800143 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
144 au_mock = self._DummyAutoupdateConstructor()
Chris Sosa0356d3b2010-09-16 15:46:22 -0700145 test_data = _TEST_REQUEST % self.test_dict
146
joychen121fc9b2013-08-02 14:30:30 -0700147 # Generate a fake image
148 forced_image_dir = '/tmp/path_to_force/'
149 forced_image = forced_image_dir + constants.IMAGE_FILE
150 common_util.MkDirP(forced_image_dir)
151 with open(forced_image, 'w') as fh:
Chris Sosa6a3697f2013-01-29 16:44:43 -0800152 fh.write('')
153
joychen18737f32013-08-16 17:18:12 -0700154 cache_image_dir = os.path.join(self.static_image_dir, 'cache')
joychen121fc9b2013-08-02 14:30:30 -0700155
156 # Mock out GenerateUpdateImageWithCache to make an update file in cache
Chris Sosa75490802013-09-30 17:21:45 -0700157 def mock_fn(_image):
Don Garrettfb15e322016-06-21 19:12:08 -0700158 print('mock_fn')
joychen121fc9b2013-08-02 14:30:30 -0700159 # No good way to introduce an update file during execution.
Chris Sosa75490802013-09-30 17:21:45 -0700160 cache_dir = os.path.join(self.static_image_dir, 'cache')
joychen121fc9b2013-08-02 14:30:30 -0700161 common_util.MkDirP(cache_dir)
162 update_image = os.path.join(cache_dir, constants.UPDATE_FILE)
163 with open(update_image, 'w') as fh:
164 fh.write('')
165
Chris Sosac4e87842013-08-16 18:04:14 -0700166 common_util.IsInsideChroot().AndReturn(True)
Chris Sosa75490802013-09-30 17:21:45 -0700167 au_mock.GenerateUpdateImageWithCache(forced_image).WithSideEffects(
joychen121fc9b2013-08-02 14:30:30 -0700168 mock_fn).AndReturn('cache')
169
Gilad Arnold55a2a372012-10-02 09:46:32 -0700170 common_util.GetFileSha1(os.path.join(
joychen121fc9b2013-08-02 14:30:30 -0700171 cache_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700172 common_util.GetFileSha256(os.path.join(
joychen121fc9b2013-08-02 14:30:30 -0700173 cache_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700174 common_util.GetFileSize(os.path.join(
joychen121fc9b2013-08-02 14:30:30 -0700175 cache_image_dir, 'update.gz')).AndReturn(self.size)
176 au_mock._StoreMetadataToFile(cache_image_dir,
Chris Sosa6a3697f2013-01-29 16:44:43 -0800177 mox.IsA(autoupdate.UpdateMetadata))
joychen121fc9b2013-08-02 14:30:30 -0700178 forced_url = 'http://%s/static/%s/update.gz' % (self.hostname,
joychen18737f32013-08-16 17:18:12 -0700179 'cache')
Chris Sosa52148582012-11-15 15:35:58 -0800180 autoupdate_lib.GetUpdateResponse(
David Zeuthen52ccd012013-10-31 12:58:26 -0700181 self.sha1, self.sha256, self.size, forced_url, False, 0, None, None,
Luis Hector Chaveza1518052018-06-14 08:19:34 -0700182 u'3.0', '', False).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700183
184 self.mox.ReplayAll()
joychen121fc9b2013-08-02 14:30:30 -0700185 au_mock.forced_image = forced_image
Chris Sosa0356d3b2010-09-16 15:46:22 -0700186 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
187 self.mox.VerifyAll()
188
joychendbfe6c92013-08-16 20:03:49 -0700189 def testHandleForcePregenerateXBuddy(self):
190 """Check pregenerating an xbuddy path.
191
192 A forced image that starts with 'xbuddy:' uses the following path to
193 obtain an update.
194 """
195 self.mox.StubOutWithMock(autoupdate.Autoupdate,
196 'GetUpdateForLabel')
197 au_mock = self._DummyAutoupdateConstructor()
198 au_mock.forced_image = "xbuddy:b/v/a"
199
Don Garrettfb15e322016-06-21 19:12:08 -0700200 self._xbuddy._GetArtifact(
201 ['b', 'v', 'a'],
Gabe Black77f3c202014-09-05 00:43:36 -0700202 image_dir=None).AndReturn(('label', constants.TEST_IMAGE_FILE))
joychen365a5742013-08-21 10:41:18 -0700203
joychendbfe6c92013-08-16 20:03:49 -0700204 au_mock.GetUpdateForLabel(
205 autoupdate.FORCED_UPDATE, 'b/v/a').AndReturn('p')
206 self.mox.ReplayAll()
207
208 au_mock.PreGenerateUpdate()
209 self.mox.VerifyAll()
210
Don Garrett0ad09372010-12-06 16:20:30 -0800211 def testChangeUrlPort(self):
212 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
213 self.assertEqual(r, 'http://fuzzy:8085/static')
214
215 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
216 self.assertEqual(r, 'http://fuzzy:8085/static')
217
218 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
219 self.assertEqual(r, 'ftp://fuzzy:8085/static')
220
221 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
222 self.assertEqual(r, 'ftp://fuzzy:8085')
223
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700224 def testHandleHostInfoPing(self):
225 au_mock = self._DummyAutoupdateConstructor()
226 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
227
Gilad Arnold286a0062012-01-12 13:47:02 -0800228 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700229 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -0800230 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700231 self.assertEqual(
232 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
233
234 def testHandleSetUpdatePing(self):
235 au_mock = self._DummyAutoupdateConstructor()
236 test_ip = '1.2.3.4'
237 test_label = 'test/old-update'
238 self.assertRaises(
239 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
240 self.assertRaises(
241 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
242 self.assertRaises(
243 AssertionError, au_mock.HandleSetUpdatePing, None, None)
244
245 au_mock.HandleSetUpdatePing(test_ip, test_label)
246 self.assertEqual(
Chris Sosa1885d032012-11-29 17:07:27 -0800247 au_mock.host_infos.GetHostInfo(test_ip).attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800248 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700249
250 def testHandleUpdatePingWithSetUpdate(self):
joychen7c2054a2013-07-25 11:14:07 -0700251 """If update is set, it should use the update found in that directory."""
Chris Sosa6a3697f2013-01-29 16:44:43 -0800252 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
253 au_mock = self._DummyAutoupdateConstructor()
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700254
255 test_data = _TEST_REQUEST % self.test_dict
256 test_label = 'new_update-test/the-new-update'
257 new_image_dir = os.path.join(self.static_image_dir, test_label)
258 new_url = self.url.replace('update.gz', test_label + '/update.gz')
259
Chris Sosa6a3697f2013-01-29 16:44:43 -0800260 # Generate a fake payload.
joychen121fc9b2013-08-02 14:30:30 -0700261 common_util.MkDirP(new_image_dir)
joychen7c2054a2013-07-25 11:14:07 -0700262 update_gz = os.path.join(new_image_dir, constants.UPDATE_FILE)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800263 with open(update_gz, 'w') as fh:
264 fh.write('')
265
Gilad Arnold55a2a372012-10-02 09:46:32 -0700266 common_util.GetFileSha1(os.path.join(
Gilad Arnold0c9c8602012-10-02 23:58:58 -0700267 new_image_dir, 'update.gz')).AndReturn(self.sha1)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700268 common_util.GetFileSha256(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700269 new_image_dir, 'update.gz')).AndReturn(self.sha256)
Gilad Arnold55a2a372012-10-02 09:46:32 -0700270 common_util.GetFileSize(os.path.join(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700271 new_image_dir, 'update.gz')).AndReturn(self.size)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800272 au_mock._StoreMetadataToFile(new_image_dir,
273 mox.IsA(autoupdate.UpdateMetadata))
Chris Sosa52148582012-11-15 15:35:58 -0800274 autoupdate_lib.GetUpdateResponse(
David Zeuthen52ccd012013-10-31 12:58:26 -0700275 self.sha1, self.sha256, self.size, new_url, False, 0, None, None,
Luis Hector Chaveza1518052018-06-14 08:19:34 -0700276 u'3.0', '', False).AndReturn(self.payload)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700277
278 self.mox.ReplayAll()
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700279 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
280 self.assertEqual(
Gilad Arnold286a0062012-01-12 13:47:02 -0800281 au_mock.host_infos.GetHostInfo('127.0.0.1').
Chris Sosa1885d032012-11-29 17:07:27 -0800282 attrs['forced_update_label'],
Gilad Arnold286a0062012-01-12 13:47:02 -0800283 test_label)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700284 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Don Garrettfb15e322016-06-21 19:12:08 -0700285 self.assertFalse(
286 'forced_update_label' in
Gilad Arnold286a0062012-01-12 13:47:02 -0800287 au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700288
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700289 def testGetVersionFromDir(self):
290 au = self._DummyAutoupdateConstructor()
291
292 # New-style version number.
293 self.assertEqual(
294 au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
295 '1102.0.2011_09_30_0806')
296
Daniel Erat8a0bc4a2011-09-30 08:52:52 -0700297 def testCanUpdate(self):
298 au = self._DummyAutoupdateConstructor()
299
300 # When both the client and the server have new-style versions, we should
301 # just compare the tokens directly.
302 self.assertTrue(
303 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
304 self.assertTrue(
305 au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
306 self.assertFalse(
307 au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
308 self.assertFalse(
309 au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
310
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700311if __name__ == '__main__':
312 unittest.main()