Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # Copyright 2020 Google LLC |
| 3 | # |
| 4 | |
| 5 | """Tests for the src_scan module |
| 6 | |
| 7 | This includes unit tests for scanning of the source code |
| 8 | """ |
| 9 | |
Simon Glass | 2e199ab | 2021-02-03 06:00:54 -0700 | [diff] [blame^] | 10 | import copy |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 11 | import os |
| 12 | import shutil |
| 13 | import tempfile |
| 14 | import unittest |
| 15 | from unittest import mock |
| 16 | |
| 17 | from dtoc import src_scan |
Simon Glass | b430e9e | 2020-12-28 20:35:08 -0700 | [diff] [blame] | 18 | from patman import test_util |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 19 | from patman import tools |
| 20 | |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 21 | OUR_PATH = os.path.dirname(os.path.realpath(__file__)) |
| 22 | |
| 23 | class FakeNode: |
| 24 | """Fake Node object for testing""" |
| 25 | def __init__(self): |
| 26 | self.name = None |
| 27 | self.props = {} |
| 28 | |
| 29 | class FakeProp: |
| 30 | """Fake Prop object for testing""" |
| 31 | def __init__(self): |
| 32 | self.name = None |
| 33 | self.value = None |
| 34 | |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 35 | # This is a test so is allowed to access private things in the module it is |
| 36 | # testing |
| 37 | # pylint: disable=W0212 |
| 38 | |
| 39 | class TestSrcScan(unittest.TestCase): |
| 40 | """Tests for src_scan""" |
| 41 | @classmethod |
| 42 | def setUpClass(cls): |
| 43 | tools.PrepareOutputDir(None) |
| 44 | |
| 45 | @classmethod |
| 46 | def tearDownClass(cls): |
| 47 | tools.FinaliseOutputDir() |
| 48 | |
Simon Glass | b430e9e | 2020-12-28 20:35:08 -0700 | [diff] [blame] | 49 | def test_simple(self): |
| 50 | """Simple test of scanning drivers""" |
| 51 | scan = src_scan.Scanner(None, True, None) |
| 52 | scan.scan_drivers() |
| 53 | self.assertIn('sandbox_gpio', scan._drivers) |
| 54 | self.assertIn('sandbox_gpio_alias', scan._driver_aliases) |
| 55 | self.assertEqual('sandbox_gpio', |
| 56 | scan._driver_aliases['sandbox_gpio_alias']) |
| 57 | self.assertNotIn('sandbox_gpio_alias2', scan._driver_aliases) |
| 58 | |
| 59 | def test_additional(self): |
| 60 | """Test with additional drivers to scan""" |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 61 | scan = src_scan.Scanner( |
| 62 | None, True, [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx']) |
| 63 | scan.scan_drivers() |
Simon Glass | b430e9e | 2020-12-28 20:35:08 -0700 | [diff] [blame] | 64 | self.assertIn('sandbox_gpio_alias2', scan._driver_aliases) |
| 65 | self.assertEqual('sandbox_gpio', |
| 66 | scan._driver_aliases['sandbox_gpio_alias2']) |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 67 | |
Simon Glass | b430e9e | 2020-12-28 20:35:08 -0700 | [diff] [blame] | 68 | def test_unicode_error(self): |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 69 | """Test running dtoc with an invalid unicode file |
| 70 | |
| 71 | To be able to perform this test without adding a weird text file which |
| 72 | would produce issues when using checkpatch.pl or patman, generate the |
| 73 | file at runtime and then process it. |
| 74 | """ |
| 75 | driver_fn = '/tmp/' + next(tempfile._get_candidate_names()) |
| 76 | with open(driver_fn, 'wb+') as fout: |
| 77 | fout.write(b'\x81') |
| 78 | |
Simon Glass | b430e9e | 2020-12-28 20:35:08 -0700 | [diff] [blame] | 79 | scan = src_scan.Scanner(None, True, [driver_fn]) |
| 80 | with test_util.capture_sys_output() as (stdout, _): |
| 81 | scan.scan_drivers() |
| 82 | self.assertRegex(stdout.getvalue(), |
| 83 | r"Skipping file '.*' due to unicode error\s*") |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 84 | |
| 85 | def test_driver(self): |
| 86 | """Test the Driver class""" |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 87 | i2c = 'I2C_UCLASS' |
| 88 | compat = {'rockchip,rk3288-grf': 'ROCKCHIP_SYSCON_GRF', |
| 89 | 'rockchip,rk3288-srf': None} |
| 90 | drv1 = src_scan.Driver('fred', 'fred.c') |
| 91 | drv2 = src_scan.Driver('mary', 'mary.c') |
| 92 | drv3 = src_scan.Driver('fred', 'fred.c') |
| 93 | drv1.uclass_id = i2c |
| 94 | drv1.compat = compat |
| 95 | drv2.uclass_id = i2c |
| 96 | drv2.compat = compat |
| 97 | drv3.uclass_id = i2c |
| 98 | drv3.compat = compat |
| 99 | self.assertEqual( |
| 100 | "Driver(name='fred', uclass_id='I2C_UCLASS', " |
| 101 | "compat={'rockchip,rk3288-grf': 'ROCKCHIP_SYSCON_GRF', " |
| 102 | "'rockchip,rk3288-srf': None}, priv=)", str(drv1)) |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 103 | self.assertEqual(drv1, drv3) |
| 104 | self.assertNotEqual(drv1, drv2) |
| 105 | self.assertNotEqual(drv2, drv3) |
| 106 | |
| 107 | def test_scan_dirs(self): |
| 108 | """Test scanning of source directories""" |
| 109 | def add_file(fname): |
| 110 | pathname = os.path.join(indir, fname) |
| 111 | dirname = os.path.dirname(pathname) |
| 112 | os.makedirs(dirname, exist_ok=True) |
| 113 | tools.WriteFile(pathname, '', binary=False) |
| 114 | fname_list.append(pathname) |
| 115 | |
| 116 | try: |
| 117 | indir = tempfile.mkdtemp(prefix='dtoc.') |
| 118 | |
| 119 | fname_list = [] |
| 120 | add_file('fname.c') |
Simon Glass | dc37c81 | 2021-02-03 06:00:52 -0700 | [diff] [blame] | 121 | add_file('.git/ignoreme.c') |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 122 | add_file('dir/fname2.c') |
Simon Glass | dc37c81 | 2021-02-03 06:00:52 -0700 | [diff] [blame] | 123 | add_file('build-sandbox/ignoreme2.c') |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 124 | |
| 125 | # Mock out scan_driver and check that it is called with the |
| 126 | # expected files |
| 127 | with mock.patch.object(src_scan.Scanner, "scan_driver") as mocked: |
| 128 | scan = src_scan.Scanner(indir, True, None) |
| 129 | scan.scan_drivers() |
| 130 | self.assertEqual(2, len(mocked.mock_calls)) |
| 131 | self.assertEqual(mock.call(fname_list[0]), |
| 132 | mocked.mock_calls[0]) |
Simon Glass | dc37c81 | 2021-02-03 06:00:52 -0700 | [diff] [blame] | 133 | # .git file should be ignored |
| 134 | self.assertEqual(mock.call(fname_list[2]), |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 135 | mocked.mock_calls[1]) |
| 136 | finally: |
| 137 | shutil.rmtree(indir) |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 138 | |
| 139 | def test_scan(self): |
| 140 | """Test scanning of a driver""" |
| 141 | fname = os.path.join(OUR_PATH, '..', '..', 'drivers/i2c/tegra_i2c.c') |
| 142 | buff = tools.ReadFile(fname, False) |
| 143 | scan = src_scan.Scanner(None, False, None) |
| 144 | scan._parse_driver(fname, buff) |
| 145 | self.assertIn('i2c_tegra', scan._drivers) |
| 146 | drv = scan._drivers['i2c_tegra'] |
| 147 | self.assertEqual('i2c_tegra', drv.name) |
| 148 | self.assertEqual('UCLASS_I2C', drv.uclass_id) |
| 149 | self.assertEqual( |
| 150 | {'nvidia,tegra114-i2c': 'TYPE_114', |
| 151 | 'nvidia,tegra20-i2c': 'TYPE_STD', |
| 152 | 'nvidia,tegra20-i2c-dvc': 'TYPE_DVC'}, drv.compat) |
| 153 | self.assertEqual('i2c_bus', drv.priv) |
| 154 | self.assertEqual(1, len(scan._drivers)) |
| 155 | |
| 156 | def test_normalized_name(self): |
| 157 | """Test operation of get_normalized_compat_name()""" |
| 158 | prop = FakeProp() |
| 159 | prop.name = 'compatible' |
| 160 | prop.value = 'rockchip,rk3288-grf' |
| 161 | node = FakeNode() |
| 162 | node.props = {'compatible': prop} |
| 163 | scan = src_scan.Scanner(None, False, None) |
| 164 | with test_util.capture_sys_output() as (stdout, _): |
| 165 | name, aliases = scan.get_normalized_compat_name(node) |
| 166 | self.assertEqual('rockchip_rk3288_grf', name) |
| 167 | self.assertEqual([], aliases) |
| 168 | self.assertEqual( |
| 169 | 'WARNING: the driver rockchip_rk3288_grf was not found in the driver list', |
| 170 | stdout.getvalue().strip()) |
| 171 | |
| 172 | i2c = 'I2C_UCLASS' |
| 173 | compat = {'rockchip,rk3288-grf': 'ROCKCHIP_SYSCON_GRF', |
| 174 | 'rockchip,rk3288-srf': None} |
| 175 | drv = src_scan.Driver('fred', 'fred.c') |
| 176 | drv.uclass_id = i2c |
| 177 | drv.compat = compat |
| 178 | scan._drivers['rockchip_rk3288_grf'] = drv |
| 179 | |
| 180 | scan._driver_aliases['rockchip_rk3288_srf'] = 'rockchip_rk3288_grf' |
| 181 | |
| 182 | with test_util.capture_sys_output() as (stdout, _): |
| 183 | name, aliases = scan.get_normalized_compat_name(node) |
| 184 | self.assertEqual('', stdout.getvalue().strip()) |
| 185 | self.assertEqual('rockchip_rk3288_grf', name) |
| 186 | self.assertEqual([], aliases) |
| 187 | |
| 188 | prop.value = 'rockchip,rk3288-srf' |
| 189 | with test_util.capture_sys_output() as (stdout, _): |
| 190 | name, aliases = scan.get_normalized_compat_name(node) |
| 191 | self.assertEqual('', stdout.getvalue().strip()) |
| 192 | self.assertEqual('rockchip_rk3288_grf', name) |
| 193 | self.assertEqual(['rockchip_rk3288_srf'], aliases) |
| 194 | |
| 195 | def test_scan_errors(self): |
| 196 | """Test detection of scanning errors""" |
| 197 | buff = ''' |
| 198 | static const struct udevice_id tegra_i2c_ids2[] = { |
| 199 | { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 }, |
| 200 | { } |
| 201 | }; |
| 202 | |
| 203 | U_BOOT_DRIVER(i2c_tegra) = { |
| 204 | .name = "i2c_tegra", |
| 205 | .id = UCLASS_I2C, |
| 206 | .of_match = tegra_i2c_ids, |
| 207 | }; |
| 208 | ''' |
| 209 | scan = src_scan.Scanner(None, False, None) |
| 210 | with self.assertRaises(ValueError) as exc: |
| 211 | scan._parse_driver('file.c', buff) |
| 212 | self.assertIn( |
| 213 | "file.c: Unknown compatible var 'tegra_i2c_ids' (found: tegra_i2c_ids2)", |
| 214 | str(exc.exception)) |
| 215 | |
| 216 | def test_of_match(self): |
| 217 | """Test detection of of_match_ptr() member""" |
| 218 | buff = ''' |
| 219 | static const struct udevice_id tegra_i2c_ids[] = { |
| 220 | { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 }, |
| 221 | { } |
| 222 | }; |
| 223 | |
| 224 | U_BOOT_DRIVER(i2c_tegra) = { |
| 225 | .name = "i2c_tegra", |
| 226 | .id = UCLASS_I2C, |
| 227 | .of_match = of_match_ptr(tegra_i2c_ids), |
| 228 | }; |
| 229 | ''' |
| 230 | scan = src_scan.Scanner(None, False, None) |
| 231 | scan._parse_driver('file.c', buff) |
| 232 | self.assertIn('i2c_tegra', scan._drivers) |
| 233 | drv = scan._drivers['i2c_tegra'] |
| 234 | self.assertEqual('i2c_tegra', drv.name) |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 235 | |
| 236 | def test_priv(self): |
| 237 | """Test collection of struct info from drivers""" |
| 238 | buff = ''' |
| 239 | static const struct udevice_id test_ids[] = { |
| 240 | { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 }, |
| 241 | { } |
| 242 | }; |
| 243 | |
| 244 | U_BOOT_DRIVER(testing) = { |
| 245 | .name = "testing", |
| 246 | .id = UCLASS_I2C, |
| 247 | .of_match = test_ids, |
| 248 | .priv_auto = sizeof(struct some_priv), |
| 249 | .plat_auto = sizeof(struct some_plat), |
| 250 | .per_child_auto = sizeof(struct some_cpriv), |
| 251 | .per_child_plat_auto = sizeof(struct some_cplat), |
| 252 | }; |
| 253 | ''' |
| 254 | scan = src_scan.Scanner(None, False, None) |
| 255 | scan._parse_driver('file.c', buff) |
| 256 | self.assertIn('testing', scan._drivers) |
| 257 | drv = scan._drivers['testing'] |
| 258 | self.assertEqual('testing', drv.name) |
| 259 | self.assertEqual('UCLASS_I2C', drv.uclass_id) |
| 260 | self.assertEqual( |
| 261 | {'nvidia,tegra114-i2c': 'TYPE_114'}, drv.compat) |
| 262 | self.assertEqual('some_priv', drv.priv) |
| 263 | self.assertEqual('some_plat', drv.plat) |
| 264 | self.assertEqual('some_cpriv', drv.child_priv) |
| 265 | self.assertEqual('some_cplat', drv.child_plat) |
| 266 | self.assertEqual(1, len(scan._drivers)) |
Simon Glass | 2e199ab | 2021-02-03 06:00:54 -0700 | [diff] [blame^] | 267 | |
| 268 | def test_uclass_scan(self): |
| 269 | """Test collection of uclass-driver info""" |
| 270 | buff = ''' |
| 271 | UCLASS_DRIVER(i2c) = { |
| 272 | .id = UCLASS_I2C, |
| 273 | .name = "i2c", |
| 274 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 275 | .priv_auto = sizeof(struct some_priv), |
| 276 | .per_device_auto = sizeof(struct per_dev_priv), |
| 277 | .per_device_plat_auto = sizeof(struct per_dev_plat), |
| 278 | .per_child_auto = sizeof(struct per_child_priv), |
| 279 | .per_child_plat_auto = sizeof(struct per_child_plat), |
| 280 | .child_post_bind = i2c_child_post_bind, |
| 281 | }; |
| 282 | |
| 283 | ''' |
| 284 | scan = src_scan.Scanner(None, False, None) |
| 285 | scan._parse_uclass_driver('file.c', buff) |
| 286 | self.assertIn('UCLASS_I2C', scan._uclass) |
| 287 | drv = scan._uclass['UCLASS_I2C'] |
| 288 | self.assertEqual('i2c', drv.name) |
| 289 | self.assertEqual('UCLASS_I2C', drv.uclass_id) |
| 290 | self.assertEqual('some_priv', drv.priv) |
| 291 | self.assertEqual('per_dev_priv', drv.per_dev_priv) |
| 292 | self.assertEqual('per_dev_plat', drv.per_dev_plat) |
| 293 | self.assertEqual('per_child_priv', drv.per_child_priv) |
| 294 | self.assertEqual('per_child_plat', drv.per_child_plat) |
| 295 | self.assertEqual(1, len(scan._uclass)) |
| 296 | |
| 297 | drv2 = copy.deepcopy(drv) |
| 298 | self.assertEqual(drv, drv2) |
| 299 | drv2.priv = 'other_priv' |
| 300 | self.assertNotEqual(drv, drv2) |
| 301 | |
| 302 | # The hashes only depend on the uclass ID, so should be equal |
| 303 | self.assertEqual(drv.__hash__(), drv2.__hash__()) |
| 304 | |
| 305 | self.assertEqual("UclassDriver(name='i2c', uclass_id='UCLASS_I2C')", |
| 306 | str(drv)) |
| 307 | |
| 308 | def test_uclass_scan_errors(self): |
| 309 | """Test detection of uclass scanning errors""" |
| 310 | buff = ''' |
| 311 | UCLASS_DRIVER(i2c) = { |
| 312 | .name = "i2c", |
| 313 | }; |
| 314 | |
| 315 | ''' |
| 316 | scan = src_scan.Scanner(None, False, None) |
| 317 | with self.assertRaises(ValueError) as exc: |
| 318 | scan._parse_uclass_driver('file.c', buff) |
| 319 | self.assertIn("file.c: Cannot parse uclass ID in driver 'i2c'", |
| 320 | str(exc.exception)) |