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""" |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 51 | scan = src_scan.Scanner(None, None) |
Simon Glass | b430e9e | 2020-12-28 20:35:08 -0700 | [diff] [blame] | 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( |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 62 | None, [None, '', 'tools/dtoc/test/dtoc_test_scan_drivers.cxx']) |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 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 | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 79 | scan = src_scan.Scanner(None, [driver_fn]) |
Simon Glass | b430e9e | 2020-12-28 20:35:08 -0700 | [diff] [blame] | 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( |
Simon Glass | eb3c249 | 2021-02-03 06:01:01 -0700 | [diff] [blame] | 100 | "Driver(name='fred', used=False, uclass_id='I2C_UCLASS', " |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 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: |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 128 | scan = src_scan.Scanner(indir, None) |
Simon Glass | df692c3 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 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) |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 143 | scan = src_scan.Scanner(None, None) |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 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} |
Simon Glass | c14fd0c | 2021-02-03 06:01:11 -0700 | [diff] [blame] | 163 | |
| 164 | # get_normalized_compat_name() uses this to check for root node |
| 165 | node.parent = FakeNode() |
| 166 | |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 167 | scan = src_scan.Scanner(None, None) |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 168 | with test_util.capture_sys_output() as (stdout, _): |
| 169 | name, aliases = scan.get_normalized_compat_name(node) |
| 170 | self.assertEqual('rockchip_rk3288_grf', name) |
| 171 | self.assertEqual([], aliases) |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 172 | self.assertEqual(1, len(scan._missing_drivers)) |
| 173 | self.assertEqual({'rockchip_rk3288_grf'}, scan._missing_drivers) |
| 174 | #'WARNING: the driver rockchip_rk3288_grf was not found in the driver list', |
| 175 | #stdout.getvalue().strip()) |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 176 | |
| 177 | i2c = 'I2C_UCLASS' |
| 178 | compat = {'rockchip,rk3288-grf': 'ROCKCHIP_SYSCON_GRF', |
| 179 | 'rockchip,rk3288-srf': None} |
| 180 | drv = src_scan.Driver('fred', 'fred.c') |
| 181 | drv.uclass_id = i2c |
| 182 | drv.compat = compat |
| 183 | scan._drivers['rockchip_rk3288_grf'] = drv |
| 184 | |
| 185 | scan._driver_aliases['rockchip_rk3288_srf'] = 'rockchip_rk3288_grf' |
| 186 | |
| 187 | with test_util.capture_sys_output() as (stdout, _): |
| 188 | name, aliases = scan.get_normalized_compat_name(node) |
| 189 | self.assertEqual('', stdout.getvalue().strip()) |
| 190 | self.assertEqual('rockchip_rk3288_grf', name) |
| 191 | self.assertEqual([], aliases) |
| 192 | |
| 193 | prop.value = 'rockchip,rk3288-srf' |
| 194 | with test_util.capture_sys_output() as (stdout, _): |
| 195 | name, aliases = scan.get_normalized_compat_name(node) |
| 196 | self.assertEqual('', stdout.getvalue().strip()) |
| 197 | self.assertEqual('rockchip_rk3288_grf', name) |
| 198 | self.assertEqual(['rockchip_rk3288_srf'], aliases) |
| 199 | |
| 200 | def test_scan_errors(self): |
| 201 | """Test detection of scanning errors""" |
| 202 | buff = ''' |
| 203 | static const struct udevice_id tegra_i2c_ids2[] = { |
| 204 | { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 }, |
| 205 | { } |
| 206 | }; |
| 207 | |
| 208 | U_BOOT_DRIVER(i2c_tegra) = { |
| 209 | .name = "i2c_tegra", |
| 210 | .id = UCLASS_I2C, |
| 211 | .of_match = tegra_i2c_ids, |
| 212 | }; |
| 213 | ''' |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 214 | scan = src_scan.Scanner(None, None) |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 215 | with self.assertRaises(ValueError) as exc: |
| 216 | scan._parse_driver('file.c', buff) |
| 217 | self.assertIn( |
| 218 | "file.c: Unknown compatible var 'tegra_i2c_ids' (found: tegra_i2c_ids2)", |
| 219 | str(exc.exception)) |
| 220 | |
| 221 | def test_of_match(self): |
| 222 | """Test detection of of_match_ptr() member""" |
| 223 | buff = ''' |
| 224 | static const struct udevice_id tegra_i2c_ids[] = { |
| 225 | { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 }, |
| 226 | { } |
| 227 | }; |
| 228 | |
| 229 | U_BOOT_DRIVER(i2c_tegra) = { |
| 230 | .name = "i2c_tegra", |
| 231 | .id = UCLASS_I2C, |
| 232 | .of_match = of_match_ptr(tegra_i2c_ids), |
| 233 | }; |
| 234 | ''' |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 235 | scan = src_scan.Scanner(None, None) |
Simon Glass | 78933b7 | 2021-02-03 06:00:50 -0700 | [diff] [blame] | 236 | scan._parse_driver('file.c', buff) |
| 237 | self.assertIn('i2c_tegra', scan._drivers) |
| 238 | drv = scan._drivers['i2c_tegra'] |
| 239 | self.assertEqual('i2c_tegra', drv.name) |
Simon Glass | f303ee7 | 2021-02-03 06:01:02 -0700 | [diff] [blame] | 240 | self.assertEqual('', drv.phase) |
Simon Glass | a7b1c77 | 2021-02-03 06:01:04 -0700 | [diff] [blame] | 241 | self.assertEqual([], drv.headers) |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 242 | |
| 243 | def test_priv(self): |
| 244 | """Test collection of struct info from drivers""" |
| 245 | buff = ''' |
| 246 | static const struct udevice_id test_ids[] = { |
| 247 | { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 }, |
| 248 | { } |
| 249 | }; |
| 250 | |
| 251 | U_BOOT_DRIVER(testing) = { |
| 252 | .name = "testing", |
| 253 | .id = UCLASS_I2C, |
| 254 | .of_match = test_ids, |
| 255 | .priv_auto = sizeof(struct some_priv), |
| 256 | .plat_auto = sizeof(struct some_plat), |
| 257 | .per_child_auto = sizeof(struct some_cpriv), |
| 258 | .per_child_plat_auto = sizeof(struct some_cplat), |
Simon Glass | f303ee7 | 2021-02-03 06:01:02 -0700 | [diff] [blame] | 259 | DM_PHASE(tpl) |
Simon Glass | a7b1c77 | 2021-02-03 06:01:04 -0700 | [diff] [blame] | 260 | DM_HEADER(<i2c.h>) |
| 261 | DM_HEADER(<asm/clk.h>) |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 262 | }; |
| 263 | ''' |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 264 | scan = src_scan.Scanner(None, None) |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 265 | scan._parse_driver('file.c', buff) |
| 266 | self.assertIn('testing', scan._drivers) |
| 267 | drv = scan._drivers['testing'] |
| 268 | self.assertEqual('testing', drv.name) |
| 269 | self.assertEqual('UCLASS_I2C', drv.uclass_id) |
| 270 | self.assertEqual( |
| 271 | {'nvidia,tegra114-i2c': 'TYPE_114'}, drv.compat) |
| 272 | self.assertEqual('some_priv', drv.priv) |
| 273 | self.assertEqual('some_plat', drv.plat) |
| 274 | self.assertEqual('some_cpriv', drv.child_priv) |
| 275 | self.assertEqual('some_cplat', drv.child_plat) |
Simon Glass | f303ee7 | 2021-02-03 06:01:02 -0700 | [diff] [blame] | 276 | self.assertEqual('tpl', drv.phase) |
Simon Glass | a7b1c77 | 2021-02-03 06:01:04 -0700 | [diff] [blame] | 277 | self.assertEqual(['<i2c.h>', '<asm/clk.h>'], drv.headers) |
Simon Glass | 0f3b141 | 2021-02-03 06:00:53 -0700 | [diff] [blame] | 278 | self.assertEqual(1, len(scan._drivers)) |
Simon Glass | 2e199ab | 2021-02-03 06:00:54 -0700 | [diff] [blame] | 279 | |
| 280 | def test_uclass_scan(self): |
| 281 | """Test collection of uclass-driver info""" |
| 282 | buff = ''' |
| 283 | UCLASS_DRIVER(i2c) = { |
| 284 | .id = UCLASS_I2C, |
| 285 | .name = "i2c", |
| 286 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 287 | .priv_auto = sizeof(struct some_priv), |
| 288 | .per_device_auto = sizeof(struct per_dev_priv), |
| 289 | .per_device_plat_auto = sizeof(struct per_dev_plat), |
| 290 | .per_child_auto = sizeof(struct per_child_priv), |
| 291 | .per_child_plat_auto = sizeof(struct per_child_plat), |
| 292 | .child_post_bind = i2c_child_post_bind, |
| 293 | }; |
| 294 | |
| 295 | ''' |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 296 | scan = src_scan.Scanner(None, None) |
Simon Glass | 2e199ab | 2021-02-03 06:00:54 -0700 | [diff] [blame] | 297 | scan._parse_uclass_driver('file.c', buff) |
| 298 | self.assertIn('UCLASS_I2C', scan._uclass) |
| 299 | drv = scan._uclass['UCLASS_I2C'] |
| 300 | self.assertEqual('i2c', drv.name) |
| 301 | self.assertEqual('UCLASS_I2C', drv.uclass_id) |
| 302 | self.assertEqual('some_priv', drv.priv) |
| 303 | self.assertEqual('per_dev_priv', drv.per_dev_priv) |
| 304 | self.assertEqual('per_dev_plat', drv.per_dev_plat) |
| 305 | self.assertEqual('per_child_priv', drv.per_child_priv) |
| 306 | self.assertEqual('per_child_plat', drv.per_child_plat) |
| 307 | self.assertEqual(1, len(scan._uclass)) |
| 308 | |
| 309 | drv2 = copy.deepcopy(drv) |
| 310 | self.assertEqual(drv, drv2) |
| 311 | drv2.priv = 'other_priv' |
| 312 | self.assertNotEqual(drv, drv2) |
| 313 | |
| 314 | # The hashes only depend on the uclass ID, so should be equal |
| 315 | self.assertEqual(drv.__hash__(), drv2.__hash__()) |
| 316 | |
| 317 | self.assertEqual("UclassDriver(name='i2c', uclass_id='UCLASS_I2C')", |
| 318 | str(drv)) |
| 319 | |
| 320 | def test_uclass_scan_errors(self): |
| 321 | """Test detection of uclass scanning errors""" |
| 322 | buff = ''' |
| 323 | UCLASS_DRIVER(i2c) = { |
| 324 | .name = "i2c", |
| 325 | }; |
| 326 | |
| 327 | ''' |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 328 | scan = src_scan.Scanner(None, None) |
Simon Glass | 2e199ab | 2021-02-03 06:00:54 -0700 | [diff] [blame] | 329 | with self.assertRaises(ValueError) as exc: |
| 330 | scan._parse_uclass_driver('file.c', buff) |
| 331 | self.assertIn("file.c: Cannot parse uclass ID in driver 'i2c'", |
| 332 | str(exc.exception)) |
Simon Glass | 88bd538 | 2021-02-03 06:00:55 -0700 | [diff] [blame] | 333 | |
| 334 | def test_struct_scan(self): |
| 335 | """Test collection of struct info""" |
| 336 | buff = ''' |
| 337 | /* some comment */ |
| 338 | struct some_struct1 { |
| 339 | struct i2c_msg *msgs; |
| 340 | uint nmsgs; |
| 341 | }; |
| 342 | ''' |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 343 | scan = src_scan.Scanner(None, None) |
Simon Glass | 88bd538 | 2021-02-03 06:00:55 -0700 | [diff] [blame] | 344 | scan._basedir = os.path.join(OUR_PATH, '..', '..') |
| 345 | scan._parse_structs('arch/arm/include/asm/file.h', buff) |
| 346 | self.assertIn('some_struct1', scan._structs) |
| 347 | struc = scan._structs['some_struct1'] |
| 348 | self.assertEqual('some_struct1', struc.name) |
| 349 | self.assertEqual('asm/file.h', struc.fname) |
| 350 | |
| 351 | buff = ''' |
| 352 | /* another comment */ |
| 353 | struct another_struct { |
| 354 | int speed_hz; |
| 355 | int max_transaction_bytes; |
| 356 | }; |
| 357 | ''' |
| 358 | scan._parse_structs('include/file2.h', buff) |
| 359 | self.assertIn('another_struct', scan._structs) |
| 360 | struc = scan._structs['another_struct'] |
| 361 | self.assertEqual('another_struct', struc.name) |
| 362 | self.assertEqual('file2.h', struc.fname) |
| 363 | |
| 364 | self.assertEqual(2, len(scan._structs)) |
| 365 | |
| 366 | self.assertEqual("Struct(name='another_struct', fname='file2.h')", |
| 367 | str(struc)) |
| 368 | |
| 369 | def test_struct_scan_errors(self): |
| 370 | """Test scanning a header file with an invalid unicode file""" |
| 371 | output = tools.GetOutputFilename('output.h') |
| 372 | tools.WriteFile(output, b'struct this is a test \x81 of bad unicode') |
| 373 | |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 374 | scan = src_scan.Scanner(None, None) |
Simon Glass | 88bd538 | 2021-02-03 06:00:55 -0700 | [diff] [blame] | 375 | with test_util.capture_sys_output() as (stdout, _): |
| 376 | scan.scan_header(output) |
| 377 | self.assertIn('due to unicode error', stdout.getvalue()) |
Simon Glass | 9b2eac0 | 2021-02-03 06:01:06 -0700 | [diff] [blame] | 378 | |
| 379 | def setup_dup_drivers(self, name, phase=''): |
| 380 | """Set up for a duplcate test |
| 381 | |
| 382 | Returns: |
| 383 | tuple: |
| 384 | Scanner to use |
| 385 | Driver record for first driver |
| 386 | Text of second driver declaration |
| 387 | Node for driver 1 |
| 388 | """ |
| 389 | driver1 = ''' |
| 390 | static const struct udevice_id test_ids[] = { |
| 391 | { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 }, |
| 392 | { } |
| 393 | }; |
| 394 | |
| 395 | U_BOOT_DRIVER(%s) = { |
| 396 | .name = "testing", |
| 397 | .id = UCLASS_I2C, |
| 398 | .of_match = test_ids, |
| 399 | %s |
| 400 | }; |
| 401 | ''' % (name, 'DM_PHASE(%s)' % phase if phase else '') |
| 402 | driver2 = ''' |
| 403 | static const struct udevice_id test_ids[] = { |
| 404 | { .compatible = "nvidia,tegra114-dvc" }, |
| 405 | { } |
| 406 | }; |
| 407 | |
| 408 | U_BOOT_DRIVER(%s) = { |
| 409 | .name = "testing", |
| 410 | .id = UCLASS_RAM, |
| 411 | .of_match = test_ids, |
| 412 | }; |
| 413 | ''' % name |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 414 | scan = src_scan.Scanner(None, None, phase) |
Simon Glass | 9b2eac0 | 2021-02-03 06:01:06 -0700 | [diff] [blame] | 415 | scan._parse_driver('file1.c', driver1) |
| 416 | self.assertIn(name, scan._drivers) |
| 417 | drv1 = scan._drivers[name] |
| 418 | |
| 419 | prop = FakeProp() |
| 420 | prop.name = 'compatible' |
| 421 | prop.value = 'nvidia,tegra114-i2c' |
| 422 | node = FakeNode() |
| 423 | node.name = 'testing' |
| 424 | node.props = {'compatible': prop} |
| 425 | |
Simon Glass | c14fd0c | 2021-02-03 06:01:11 -0700 | [diff] [blame] | 426 | # get_normalized_compat_name() uses this to check for root node |
| 427 | node.parent = FakeNode() |
| 428 | |
Simon Glass | 9b2eac0 | 2021-02-03 06:01:06 -0700 | [diff] [blame] | 429 | return scan, drv1, driver2, node |
| 430 | |
| 431 | def test_dup_drivers(self): |
| 432 | """Test handling of duplicate drivers""" |
| 433 | name = 'nvidia_tegra114_i2c' |
| 434 | scan, drv1, driver2, node = self.setup_dup_drivers(name) |
| 435 | self.assertEqual('', drv1.phase) |
| 436 | |
| 437 | # The driver should not have a duplicate yet |
| 438 | self.assertEqual([], drv1.dups) |
| 439 | |
| 440 | scan._parse_driver('file2.c', driver2) |
| 441 | |
| 442 | # The first driver should now be a duplicate of the second |
| 443 | drv2 = scan._drivers[name] |
| 444 | self.assertEqual('', drv2.phase) |
| 445 | self.assertEqual(1, len(drv2.dups)) |
| 446 | self.assertEqual([drv1], drv2.dups) |
| 447 | |
| 448 | # There is no way to distinguish them, so we should expect a warning |
| 449 | self.assertTrue(drv2.warn_dups) |
| 450 | |
| 451 | # We should see a warning |
| 452 | with test_util.capture_sys_output() as (stdout, _): |
| 453 | scan.mark_used([node]) |
| 454 | self.assertEqual( |
| 455 | "Warning: Duplicate driver name 'nvidia_tegra114_i2c' (orig=file2.c, dups=file1.c)", |
| 456 | stdout.getvalue().strip()) |
| 457 | |
| 458 | def test_dup_drivers_phase(self): |
| 459 | """Test handling of duplicate drivers but with different phases""" |
| 460 | name = 'nvidia_tegra114_i2c' |
| 461 | scan, drv1, driver2, node = self.setup_dup_drivers(name, 'spl') |
| 462 | scan._parse_driver('file2.c', driver2) |
| 463 | self.assertEqual('spl', drv1.phase) |
| 464 | |
| 465 | # The second driver should now be a duplicate of the second |
| 466 | self.assertEqual(1, len(drv1.dups)) |
| 467 | drv2 = drv1.dups[0] |
| 468 | |
| 469 | # The phase is different, so we should not warn of dups |
| 470 | self.assertFalse(drv1.warn_dups) |
| 471 | |
| 472 | # We should not see a warning |
| 473 | with test_util.capture_sys_output() as (stdout, _): |
| 474 | scan.mark_used([node]) |
| 475 | self.assertEqual('', stdout.getvalue().strip()) |
Simon Glass | 80d782c4 | 2021-02-03 06:01:10 -0700 | [diff] [blame] | 476 | |
| 477 | def test_sequence(self): |
| 478 | """Test assignment of sequence numnbers""" |
Simon Glass | 695077d | 2021-03-26 16:17:25 +1300 | [diff] [blame] | 479 | scan = src_scan.Scanner(None, None, '') |
Simon Glass | 80d782c4 | 2021-02-03 06:01:10 -0700 | [diff] [blame] | 480 | node = FakeNode() |
| 481 | uc = src_scan.UclassDriver('UCLASS_I2C') |
| 482 | node.uclass = uc |
| 483 | node.driver = True |
| 484 | node.seq = -1 |
| 485 | node.path = 'mypath' |
| 486 | uc.alias_num_to_node[2] = node |
| 487 | |
| 488 | # This should assign 3 (after the 2 that exists) |
| 489 | seq = scan.assign_seq(node) |
| 490 | self.assertEqual(3, seq) |
| 491 | self.assertEqual({'mypath': 3}, uc.alias_path_to_num) |
| 492 | self.assertEqual({2: node, 3: node}, uc.alias_num_to_node) |