blob: 69bbfaacb8b105655b18a6c18fa612ce84d232bf [file] [log] [blame]
Jae Hoon Kimb3c8cab2021-11-10 22:57:531#!/usr/bin/env python3
Amin Hassani2aa34282020-11-18 01:18:192# -*- coding: utf-8 -*-
Mike Frysinger8b0fc372022-09-08 07:24:243# Copyright 2010 The ChromiumOS Authors
Amin Hassani2aa34282020-11-18 01:18:194# 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
9from __future__ import print_function
10
Amin Hassani2aa34282020-11-18 01:18:1911import shutil
12import socket
13import tempfile
14import unittest
15
Jae Hoon Kimb3c8cab2021-11-10 22:57:5316import unittest.mock as mock
Amin Hassani2aa34282020-11-18 01:18:1917import cherrypy # pylint: disable=import-error
18
19import autoupdate
20
21import setup_chromite # pylint: disable=unused-import
22from chromite.lib.xbuddy import common_util
23from chromite.lib.xbuddy import xbuddy
24
25
26_TEST_REQUEST = """<?xml version="1.0" encoding="UTF-8"?>
27<request protocol="3.0" updater="ChromeOSUpdateEngine" updaterversion="0.1.0.0">
28 <app appid="test-appid" version="%(version)s" track="%(track)s"
29 board="%(board)s" hardware_class="%(hwclass)s">
30 <updatecheck />
31 </app>
32</request>"""
33
34class AutoupdateTest(unittest.TestCase):
35 """Tests for the autoupdate.Autoupdate class."""
36
37 def setUp(self):
38 self.port = 8080
39 self.test_board = 'test-board'
40 self.build_root = tempfile.mkdtemp('autoupdate_build_root')
41 self.tempdir = tempfile.mkdtemp('tempdir')
42 self.latest_dir = '12345_af_12-a1'
43 self.latest_verision = '12345_af_12'
44 self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir')
45 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
46 self.test_dict = {
47 'version': 'ForcedUpdate',
48 'track': 'test-channel',
49 'board': self.test_board,
50 'hwclass': 'test-hardware-class',
51 }
52 self.test_data = _TEST_REQUEST % self.test_dict
53 self.payload = 'My payload'
54 cherrypy.request.base = 'http://%s' % self.hostname
55 common_util.MkDirP(self.static_image_dir)
56 self._xbuddy = xbuddy.XBuddy(False,
57 static_dir=self.static_image_dir)
58 mock.patch.object(xbuddy.XBuddy, '_GetArtifact')
59
60 def tearDown(self):
61 shutil.rmtree(self.build_root)
62 shutil.rmtree(self.tempdir)
63 shutil.rmtree(self.static_image_dir)
64
65 def _DummyAutoupdateConstructor(self, **kwargs):
66 """Creates a dummy autoupdater. Used to avoid using constructor."""
67 dummy = autoupdate.Autoupdate(self._xbuddy,
68 static_dir=self.static_image_dir,
69 **kwargs)
70 return dummy
71
72 def testChangeUrlPort(self):
73 # pylint: disable=protected-access
74 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
75 self.assertEqual(r, 'http://fuzzy:8085/static')
76
77 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
78 self.assertEqual(r, 'http://fuzzy:8085/static')
79
80 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
81 self.assertEqual(r, 'ftp://fuzzy:8085/static')
82
83 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
84 self.assertEqual(r, 'ftp://fuzzy:8085')
85
Jae Hoon Kimb3c8cab2021-11-10 22:57:5386 @mock.patch.object(autoupdate.Autoupdate, 'GetBuildID')
87 def testHandleUpdatePing(self, get_build_id):
Amin Hassani2aa34282020-11-18 01:18:1988 """Tests HandleUpdatePing"""
89 au_mock = self._DummyAutoupdateConstructor()
Jae Hoon Kimb3c8cab2021-11-10 22:57:5390 get_build_id.return_value = ''
Amin Hassani2aa34282020-11-18 01:18:1991 request = """<?xml version="1.0" encoding="UTF-8"?>
92<request protocol="3.0">
93 <os version="Indy" platform="Chrome OS" sp="10323.52.0_x86_64"></os>
94 <app appid="platform" version="1.0.0" delta_okay="true"
95 track="stable-channel" board="eve">
96 <ping active="1" a="1" r="1"></ping>
97 <updatecheck targetversionprefix=""></updatecheck>
98 </app>
99</request>"""
100
Jae Hoon Kimb3c8cab2021-11-10 22:57:53101 self.assertIn(b'<updatecheck status="noupdate"',
Amin Hassani8b503582020-12-03 04:47:01102 au_mock.HandleUpdatePing(request))
Amin Hassani2aa34282020-11-18 01:18:19103
104if __name__ == '__main__':
105 unittest.main()