Luis Hector Chavez | a151805 | 2018-06-14 08:19:34 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [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-21 19:12:08 -0700 | [diff] [blame] | 9 | from __future__ import print_function |
| 10 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 11 | import json |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 12 | import shutil |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 13 | import socket |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 14 | import tempfile |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 15 | import unittest |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 16 | |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 17 | import mock |
Achuith Bhandarkar | 662fb72 | 2019-10-31 16:12:49 -0700 | [diff] [blame] | 18 | import cherrypy # pylint: disable=import-error |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 19 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 20 | import autoupdate |
Achuith Bhandarkar | 662fb72 | 2019-10-31 16:12:49 -0700 | [diff] [blame] | 21 | |
| 22 | import setup_chromite # pylint: disable=unused-import |
| 23 | from chromite.lib.xbuddy import common_util |
| 24 | from chromite.lib.xbuddy import xbuddy |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 25 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 26 | |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 27 | _TEST_REQUEST = """<?xml version="1.0" encoding="UTF-8"?> |
| 28 | <request protocol="3.0" updater="ChromeOSUpdateEngine" updaterversion="0.1.0.0"> |
| 29 | <app appid="test-appid" version="%(version)s" track="%(track)s" |
| 30 | board="%(board)s" hardware_class="%(hwclass)s"> |
| 31 | <updatecheck /> |
| 32 | </app> |
| 33 | </request>""" |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 34 | |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 35 | class AutoupdateTest(unittest.TestCase): |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 36 | """Tests for the autoupdate.Autoupdate class.""" |
| 37 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 38 | def setUp(self): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 39 | self.port = 8080 |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 40 | self.test_board = 'test-board' |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 41 | self.build_root = tempfile.mkdtemp('autoupdate_build_root') |
Amin Hassani | c91fc0d | 2019-12-04 11:07:16 -0800 | [diff] [blame] | 42 | self.tempdir = tempfile.mkdtemp('tempdir') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 43 | self.latest_dir = '12345_af_12-a1' |
| 44 | self.latest_verision = '12345_af_12' |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 45 | self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 46 | self.hostname = '%s:%s' % (socket.gethostname(), self.port) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 47 | self.test_dict = { |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 48 | 'version': 'ForcedUpdate', |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 49 | 'track': 'test-channel', |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 50 | 'board': self.test_board, |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 51 | 'hwclass': 'test-hardware-class', |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 52 | } |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 53 | self.test_data = _TEST_REQUEST % self.test_dict |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 54 | self.payload = 'My payload' |
Chris Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 55 | cherrypy.request.base = 'http://%s' % self.hostname |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 56 | common_util.MkDirP(self.static_image_dir) |
| 57 | self._xbuddy = xbuddy.XBuddy(False, |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 58 | static_dir=self.static_image_dir) |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 59 | mock.patch.object(xbuddy.XBuddy, '_GetArtifact') |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 60 | |
| 61 | def tearDown(self): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 62 | shutil.rmtree(self.build_root) |
Amin Hassani | c91fc0d | 2019-12-04 11:07:16 -0800 | [diff] [blame] | 63 | shutil.rmtree(self.tempdir) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 64 | shutil.rmtree(self.static_image_dir) |
Chris Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 65 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 66 | def _DummyAutoupdateConstructor(self, **kwargs): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 67 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 68 | dummy = autoupdate.Autoupdate(self._xbuddy, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 69 | static_dir=self.static_image_dir, |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 70 | **kwargs) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 71 | return dummy |
| 72 | |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 73 | def testChangeUrlPort(self): |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 74 | # pylint: disable=protected-access |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 75 | r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085) |
| 76 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 77 | |
| 78 | r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085) |
| 79 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 80 | |
| 81 | r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085) |
| 82 | self.assertEqual(r, 'ftp://fuzzy:8085/static') |
| 83 | |
| 84 | r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085) |
| 85 | self.assertEqual(r, 'ftp://fuzzy:8085') |
| 86 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 87 | def testHandleHostInfoPing(self): |
| 88 | au_mock = self._DummyAutoupdateConstructor() |
| 89 | self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None) |
| 90 | |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 91 | # Setup fake host_infos entry and ensure it comes back to us in one piece. |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 92 | test_ip = '1.2.3.4' |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 93 | au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 94 | self.assertEqual( |
| 95 | json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict) |
| 96 | |
Amin Hassani | c91fc0d | 2019-12-04 11:07:16 -0800 | [diff] [blame] | 97 | @mock.patch.object(autoupdate.Autoupdate, 'GetPathToPayload') |
| 98 | def testHandleUpdatePing(self, path_to_payload_mock): |
| 99 | """Tests HandleUpdatePing""" |
| 100 | au_mock = self._DummyAutoupdateConstructor() |
| 101 | path_to_payload_mock.return_value = self.tempdir |
| 102 | request = """<?xml version="1.0" encoding="UTF-8"?> |
| 103 | <request protocol="3.0"> |
| 104 | <os version="Indy" platform="Chrome OS" sp="10323.52.0_x86_64"></os> |
| 105 | <app appid="platform" version="1.0.0" delta_okay="true" |
| 106 | track="stable-channel" board="eve"> |
| 107 | <ping active="1" a="1" r="1"></ping> |
| 108 | <updatecheck targetversionprefix=""></updatecheck> |
| 109 | </app> |
| 110 | </request>""" |
| 111 | |
| 112 | self.assertIn('error-unknownApplication', au_mock.HandleUpdatePing(request)) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 113 | |
Amin Hassani | aef2b29 | 2020-01-10 10:44:16 -0800 | [diff] [blame^] | 114 | |
| 115 | class MaxUpdatesTableTest(unittest.TestCase): |
| 116 | """Tests MaxUpdatesTable""" |
| 117 | |
| 118 | def testSessionTable(self): |
| 119 | """Tests that SessionData() method correctly returns requested data.""" |
| 120 | table = autoupdate.SessionTable() |
| 121 | g_data = {'foo': 0} |
| 122 | |
| 123 | table.SetSessionData('id-1', g_data) |
| 124 | with table.SessionData('id-1') as data: |
| 125 | data.update({'foo': data.get('foo') + 1}) |
| 126 | # Value of the global data should be increased by now. |
| 127 | self.assertTrue(g_data['foo'], 1) |
| 128 | |
| 129 | # Increase again. |
| 130 | with table.SessionData('id-1') as data: |
| 131 | data.update({'foo': data.get('foo') + 1}) |
| 132 | self.assertTrue(g_data['foo'], 2) |
| 133 | |
| 134 | # Make sure multiple sessions can be set and used. |
| 135 | g_data2 = {'foo': 10} |
| 136 | table.SetSessionData('id-2', g_data2) |
| 137 | with table.SessionData('id-2') as data: |
| 138 | data.update({'foo': data.get('foo') + 1}) |
| 139 | self.assertTrue(g_data2['foo'], 11) |
| 140 | |
| 141 | def testNoneSession(self): |
| 142 | """Tests if a session is not set, it should be returned as None.""" |
| 143 | table = autoupdate.SessionTable() |
| 144 | # A session ID that has never been set should not return anything. |
| 145 | with table.SessionData('foo-id') as data: |
| 146 | self.assertDictEqual(data, {}) |
| 147 | |
| 148 | def testOverrideSession(self): |
| 149 | """Tests that a session can be overriden..""" |
| 150 | table = autoupdate.SessionTable() |
| 151 | |
| 152 | table.SetSessionData('id-1', {'foo': 0}) |
| 153 | table.SetSessionData('id-1', {'bar': 1}) |
| 154 | with table.SessionData('id-1') as data: |
| 155 | self.assertEqual(data.get('bar'), 1) |
| 156 | |
| 157 | @mock.patch.object(autoupdate.SessionTable, '_IsSessionExpired', |
| 158 | side_effect=lambda s: 'foo' in s.data) |
| 159 | @mock.patch.object(autoupdate.SessionTable, '_ShouldPurge', |
| 160 | return_value=True) |
| 161 | # pylint: disable=unused-argument |
| 162 | def testPurge(self, p, ps): |
| 163 | """Tests that data is being purged correctly.""" |
| 164 | table = autoupdate.SessionTable() |
| 165 | |
| 166 | table.SetSessionData('id-1', {'foo': 1}) |
| 167 | table.SetSessionData('id-2', {'bar': 2}) |
| 168 | |
| 169 | # Set a random session to make _Purge() be called. |
| 170 | table.SetSessionData('blah', {'blah': 1}) |
| 171 | # Only id-1 should be purged by now. |
| 172 | with table.SessionData('id-1') as data: |
| 173 | self.assertDictEqual(data, {}) |
| 174 | with table.SessionData('id-2') as data: |
| 175 | self.assertDictEqual(data, {'bar': 2}) |
| 176 | |
| 177 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 178 | if __name__ == '__main__': |
| 179 | unittest.main() |