lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright 2016 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
 
import cStringIO
import json
import mock
import os
import shutil
import tempfile
import unittest
import urllib2
 
import common
 
from autotest_lib.site_utils import hwid_lib
 
 
class HwIdUnittests(unittest.TestCase):
    """Unittest for testing get_hwid_info."""
 
    def setUp(self):
        # Create tmp dir and dummy key files.
        self.tmp_dir = tempfile.mkdtemp(prefix='hwid_test')
        self.dummy_key = 'dummy_key'
        self.dummy_key_file = os.path.join(self.tmp_dir, 'dummy_key')
        with open(self.dummy_key_file, 'w') as f:
            f.write(self.dummy_key)
        self.dummy_key_file_spaces = os.path.join(self.tmp_dir,
                                                  'dummy_key_spaces')
        with open(self.dummy_key_file_spaces, 'w') as f:
            f.write('  %s   ' % self.dummy_key)
        self.dummy_key_file_newline = os.path.join(self.tmp_dir,
                                                  'dummy_key_newline')
        with open(self.dummy_key_file_newline, 'w') as f:
            f.write('%s\n' % self.dummy_key)
        self.invalid_dummy_key_file = os.path.join(self.tmp_dir,
                                                   'invalid_dummy_key_file')
 
 
    def tearDown(self):
        mock.patch.stopall()
        if os.path.exists(self.tmp_dir):
            shutil.rmtree(self.tmp_dir)
 
 
    def validate_exception(self, exception, *args):
        """Helper method to validate proper exception is raised.
 
        @param exception: The exception class to check against.
        @param args: The unamed args to pass to func.
        """
        with self.assertRaises(exception):
            hwid_lib.get_hwid_info(*args)
 
 
    def test_none_hwid(self):
        """Test that an empty hwid raises a ValueError."""
        self.validate_exception(ValueError, None, None, None)
 
 
    def test_invalid_info_type(self):
        """Test that an invalid info type raises a ValueError."""
        self.validate_exception(ValueError, 'hwid', 'invalid_info_type', None)
 
 
    def test_fail_open_with_nonexistent_file(self):
        """Test that trying to open non-existent file will raise an IOError."""
        self.validate_exception(IOError, 'hwid', hwid_lib.HWID_INFO_BOM,
                                self.invalid_dummy_key_file)
 
 
    @mock.patch('urllib2.urlopen', side_effect=urllib2.URLError('url error'))
    def test_fail_to_open_url_urlerror(self, *args, **dargs):
        """Test that failing to open a url will raise a HwIdException."""
        self.validate_exception(hwid_lib.HwIdException, 'hwid',
                                hwid_lib.HWID_INFO_BOM, self.dummy_key_file)
 
 
    # pylint: disable=missing-docstring
    @mock.patch('urllib2.urlopen')
    def test_fail_decode_hwid(self, mock_urlopen, *args, **dargs):
        """Test that supplying bad json raises a HwIdException."""
        mock_page_contents = mock.Mock(wraps=cStringIO.StringIO('bad json'))
        mock_urlopen.return_value = mock_page_contents
        self.validate_exception(hwid_lib.HwIdException, 'hwid',
                                hwid_lib.HWID_INFO_BOM, self.dummy_key_file)
        mock_page_contents.close.assert_called_once_with()
 
 
    # pylint: disable=missing-docstring
    @mock.patch('urllib2.urlopen')
    def test_success(self, mock_urlopen, *args, **dargs):
        """Test that get_hwid_info successfully returns a hwid dict.
 
        We want to check that it works on all valid info types.
        """
        returned_json = '{"key1": "data1"}'
        expected_dict = json.loads(returned_json)
        for valid_info_type in hwid_lib.HWID_INFO_TYPES:
            mock_page_contents = mock.Mock(
                    wraps=cStringIO.StringIO(returned_json))
            mock_urlopen.return_value = mock_page_contents
            self.assertEqual(hwid_lib.get_hwid_info('hwid', valid_info_type,
                                                    self.dummy_key_file),
                             expected_dict)
            mock_page_contents.close.assert_called_once_with()
 
 
    # pylint: disable=missing-docstring
    @mock.patch('urllib2.urlopen')
    def test_url_properly_constructed(self, mock_urlopen, *args, **dargs):
        """Test that the url is properly constructed.
 
        Let's make sure that the key is properly cleaned before getting
        inserted into the url by trying all the different dummy_key_files.
        """
        info_type = hwid_lib.HWID_INFO_BOM
        hwid = 'mock_hwid'
        expected_url = ('%s/%s/%s/%s/?key=%s' % (hwid_lib.HWID_BASE_URL,
                                                 hwid_lib.HWID_VERSION,
                                                 info_type, hwid,
                                                 self.dummy_key))
 
        for dummy_key_file in [self.dummy_key_file,
                               self.dummy_key_file_spaces,
                               self.dummy_key_file_newline]:
            mock_page_contents = mock.Mock(wraps=cStringIO.StringIO('{}'))
            mock_urlopen.return_value = mock_page_contents
            hwid_lib.get_hwid_info(hwid, info_type, dummy_key_file)
            mock_urlopen.assert_called_with(expected_url)
 
 
    # pylint: disable=missing-docstring
    @mock.patch('urllib2.urlopen')
    def test_url_properly_constructed_again(self, mock_urlopen, *args, **dargs):
        """Test that the url is properly constructed with special hwid.
 
        Let's make sure that a hwid with a space is properly transformed.
        """
        info_type = hwid_lib.HWID_INFO_BOM
        hwid = 'mock hwid with space'
        hwid_quoted = 'mock%20hwid%20with%20space'
        expected_url = ('%s/%s/%s/%s/?key=%s' % (hwid_lib.HWID_BASE_URL,
                                                 hwid_lib.HWID_VERSION,
                                                 info_type, hwid_quoted,
                                                 self.dummy_key))
 
        mock_page_contents = mock.Mock(wraps=cStringIO.StringIO('{}'))
        mock_urlopen.return_value = mock_page_contents
        hwid_lib.get_hwid_info(hwid, info_type, self.dummy_key_file)
        mock_urlopen.assert_called_with(expected_url)
 
 
    def test_dummy_key_file(self):
        """Test that we get an empty dict with a dummy key file."""
        info_type = hwid_lib.HWID_INFO_BOM
        hwid = 'mock hwid with space'
        key_file = hwid_lib.KEY_FILENAME_NO_HWID
        self.assertEqual(hwid_lib.get_hwid_info(hwid, info_type, key_file), {})
 
 
if __name__ == '__main__':
    unittest.main()