Simon Glass | fc01782 | 2019-10-31 07:42:54 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 3 | |
| 4 | """ |
| 5 | Tests for the Fdt module |
| 6 | Copyright (c) 2018 Google, Inc |
| 7 | Written by Simon Glass <sjg@chromium.org> |
| 8 | """ |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 9 | |
Simon Glass | 882f632 | 2022-07-30 20:57:09 -0600 | [diff] [blame] | 10 | from argparse import ArgumentParser |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 11 | import os |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 12 | import shutil |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 13 | import sys |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 14 | import tempfile |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 15 | import unittest |
| 16 | |
| 17 | # Bring in the patman libraries |
| 18 | our_path = os.path.dirname(os.path.realpath(__file__)) |
Simon Glass | 4214316 | 2020-04-17 18:09:05 -0600 | [diff] [blame] | 19 | sys.path.insert(1, os.path.join(our_path, '..')) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 20 | |
Simon Glass | 7e10f8a | 2021-11-23 11:03:38 -0700 | [diff] [blame] | 21 | # Bring in the libfdt module |
| 22 | sys.path.insert(2, 'scripts/dtc/pylibfdt') |
| 23 | sys.path.insert(2, os.path.join(our_path, '../../scripts/dtc/pylibfdt')) |
| 24 | sys.path.insert(2, os.path.join(our_path, |
| 25 | '../../build-sandbox_spl/scripts/dtc/pylibfdt')) |
| 26 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 27 | #pylint: disable=wrong-import-position |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 28 | from dtoc import fdt |
| 29 | from dtoc import fdt_util |
Simon Glass | 3b55e3f | 2021-11-23 11:03:39 -0700 | [diff] [blame] | 30 | from dtoc.fdt_util import fdt32_to_cpu, fdt64_to_cpu |
Simon Glass | 2b16007 | 2022-02-11 13:23:20 -0700 | [diff] [blame] | 31 | from dtoc.fdt import Type, BytesToValue |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 32 | import libfdt |
Simon Glass | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 33 | from u_boot_pylib import test_util |
| 34 | from u_boot_pylib import tools |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 35 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 36 | #pylint: disable=protected-access |
| 37 | |
| 38 | def _get_property_value(dtb, node, prop_name): |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 39 | """Low-level function to get the property value based on its offset |
| 40 | |
| 41 | This looks directly in the device tree at the property's offset to find |
| 42 | its value. It is useful as a check that the property is in the correct |
| 43 | place. |
| 44 | |
| 45 | Args: |
| 46 | node: Node to look in |
| 47 | prop_name: Property name to find |
| 48 | |
| 49 | Returns: |
| 50 | Tuple: |
| 51 | Prop object found |
| 52 | Value of property as a string (found using property offset) |
| 53 | """ |
| 54 | prop = node.props[prop_name] |
| 55 | |
| 56 | # Add 12, which is sizeof(struct fdt_property), to get to start of data |
| 57 | offset = prop.GetOffset() + 12 |
| 58 | data = dtb.GetContents()[offset:offset + len(prop.value)] |
Simon Glass | 632b84c | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 59 | return prop, [chr(x) for x in data] |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 60 | |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 61 | def find_dtb_file(dts_fname): |
| 62 | """Locate a test file in the test/ directory |
| 63 | |
| 64 | Args: |
| 65 | dts_fname (str): Filename to find, e.g. 'dtoc_test_simple.dts] |
| 66 | |
| 67 | Returns: |
| 68 | str: Path to the test filename |
| 69 | """ |
| 70 | return os.path.join('tools/dtoc/test', dts_fname) |
| 71 | |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 72 | |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 73 | class TestFdt(unittest.TestCase): |
| 74 | """Tests for the Fdt module |
| 75 | |
| 76 | This includes unit tests for some functions and functional tests for the fdt |
| 77 | module. |
| 78 | """ |
| 79 | @classmethod |
| 80 | def setUpClass(cls): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 81 | tools.prepare_output_dir(None) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 82 | |
| 83 | @classmethod |
| 84 | def tearDownClass(cls): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 85 | tools.finalise_output_dir() |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 86 | |
| 87 | def setUp(self): |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 88 | self.dtb = fdt.FdtScan(find_dtb_file('dtoc_test_simple.dts')) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 89 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 90 | def test_fdt(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 91 | """Test that we can open an Fdt""" |
| 92 | self.dtb.Scan() |
| 93 | root = self.dtb.GetRoot() |
| 94 | self.assertTrue(isinstance(root, fdt.Node)) |
| 95 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 96 | def test_get_node(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 97 | """Test the GetNode() method""" |
| 98 | node = self.dtb.GetNode('/spl-test') |
| 99 | self.assertTrue(isinstance(node, fdt.Node)) |
Simon Glass | 3b9a829 | 2019-07-20 12:23:39 -0600 | [diff] [blame] | 100 | |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 101 | node = self.dtb.GetNode('/i2c@0/pmic@9') |
| 102 | self.assertTrue(isinstance(node, fdt.Node)) |
| 103 | self.assertEqual('pmic@9', node.name) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 104 | self.assertIsNone(self.dtb.GetNode('/i2c@0/pmic@9/missing')) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 105 | |
Simon Glass | 3b9a829 | 2019-07-20 12:23:39 -0600 | [diff] [blame] | 106 | node = self.dtb.GetNode('/') |
| 107 | self.assertTrue(isinstance(node, fdt.Node)) |
| 108 | self.assertEqual(0, node.Offset()) |
| 109 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 110 | def test_flush(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 111 | """Check that we can flush the device tree out to its file""" |
| 112 | fname = self.dtb._fname |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 113 | with open(fname, 'rb') as inf: |
| 114 | inf.read() |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 115 | os.remove(fname) |
| 116 | with self.assertRaises(IOError): |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 117 | with open(fname, 'rb'): |
| 118 | pass |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 119 | self.dtb.Flush() |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 120 | with open(fname, 'rb') as inf: |
| 121 | inf.read() |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 122 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 123 | def test_pack(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 124 | """Test that packing a device tree works""" |
| 125 | self.dtb.Pack() |
| 126 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 127 | def test_get_fdt_raw(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 128 | """Tetst that we can access the raw device-tree data""" |
Simon Glass | 2b16007 | 2022-02-11 13:23:20 -0700 | [diff] [blame] | 129 | self.assertTrue(isinstance(self.dtb.GetContents(), bytes)) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 130 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 131 | def test_get_props(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 132 | """Tests obtaining a list of properties""" |
| 133 | node = self.dtb.GetNode('/spl-test') |
| 134 | props = self.dtb.GetProps(node) |
Simon Glass | fc1aa35 | 2023-02-13 08:56:34 -0700 | [diff] [blame] | 135 | self.assertEqual(['boolval', 'bootph-all', 'bytearray', 'byteval', |
| 136 | 'compatible', 'int64val', 'intarray', 'intval', |
| 137 | 'longbytearray', 'maybe-empty-int', 'notstring', |
| 138 | 'stringarray', 'stringval', ], |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 139 | sorted(props.keys())) |
| 140 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 141 | def test_check_error(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 142 | """Tests the ChecKError() function""" |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 143 | with self.assertRaises(ValueError) as exc: |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 144 | fdt.CheckErr(-libfdt.NOTFOUND, 'hello') |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 145 | self.assertIn('FDT_ERR_NOTFOUND: hello', str(exc.exception)) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 146 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 147 | def test_get_fdt(self): |
| 148 | """Test getting an Fdt object from a node""" |
Simon Glass | e2d6528 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 149 | node = self.dtb.GetNode('/spl-test') |
| 150 | self.assertEqual(self.dtb, node.GetFdt()) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 151 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 152 | def test_bytes_to_value(self): |
| 153 | """Test converting a string list into Python""" |
Simon Glass | 5872f0c | 2019-05-17 22:00:41 -0600 | [diff] [blame] | 154 | self.assertEqual(BytesToValue(b'this\0is\0'), |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 155 | (Type.STRING, ['this', 'is'])) |
Simon Glass | 5872f0c | 2019-05-17 22:00:41 -0600 | [diff] [blame] | 156 | |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 157 | class TestNode(unittest.TestCase): |
| 158 | """Test operation of the Node class""" |
| 159 | |
| 160 | @classmethod |
| 161 | def setUpClass(cls): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 162 | tools.prepare_output_dir(None) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 163 | |
| 164 | @classmethod |
| 165 | def tearDownClass(cls): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 166 | tools.finalise_output_dir() |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 167 | |
| 168 | def setUp(self): |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 169 | self.dtb = fdt.FdtScan(find_dtb_file('dtoc_test_simple.dts')) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 170 | self.node = self.dtb.GetNode('/spl-test') |
Simon Glass | 244548f | 2021-03-21 18:24:37 +1300 | [diff] [blame] | 171 | self.fdt = self.dtb.GetFdtObj() |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 172 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 173 | def test_offset(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 174 | """Tests that we can obtain the offset of a node""" |
| 175 | self.assertTrue(self.node.Offset() > 0) |
| 176 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 177 | def test_delete(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 178 | """Tests that we can delete a property""" |
| 179 | node2 = self.dtb.GetNode('/spl-test2') |
| 180 | offset1 = node2.Offset() |
| 181 | self.node.DeleteProp('intval') |
| 182 | offset2 = node2.Offset() |
| 183 | self.assertTrue(offset2 < offset1) |
| 184 | self.node.DeleteProp('intarray') |
| 185 | offset3 = node2.Offset() |
| 186 | self.assertTrue(offset3 < offset2) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 187 | with self.assertRaises(libfdt.FdtException): |
| 188 | self.node.DeleteProp('missing') |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 189 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 190 | def test_delete_get_offset(self): |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 191 | """Test that property offset update when properties are deleted""" |
| 192 | self.node.DeleteProp('intval') |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 193 | prop, value = _get_property_value(self.dtb, self.node, 'longbytearray') |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 194 | self.assertEqual(prop.value, value) |
| 195 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 196 | def test_find_node(self): |
Simon Glass | aa1a5d7 | 2018-07-17 13:25:41 -0600 | [diff] [blame] | 197 | """Tests that we can find a node using the FindNode() functoin""" |
| 198 | node = self.dtb.GetRoot().FindNode('i2c@0') |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 199 | self.assertEqual('i2c@0', node.name) |
Simon Glass | aa1a5d7 | 2018-07-17 13:25:41 -0600 | [diff] [blame] | 200 | subnode = node.FindNode('pmic@9') |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 201 | self.assertEqual('pmic@9', subnode.name) |
Simon Glass | aa1a5d7 | 2018-07-17 13:25:41 -0600 | [diff] [blame] | 202 | self.assertEqual(None, node.FindNode('missing')) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 203 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 204 | def test_refresh_missing_node(self): |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 205 | """Test refreshing offsets when an extra node is present in dtb""" |
| 206 | # Delete it from our tables, not the device tree |
| 207 | del self.dtb._root.subnodes[-1] |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 208 | with self.assertRaises(ValueError) as exc: |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 209 | self.dtb.Refresh() |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 210 | self.assertIn('Internal error, offset', str(exc.exception)) |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 211 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 212 | def test_refresh_extra_node(self): |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 213 | """Test refreshing offsets when an expected node is missing""" |
| 214 | # Delete it from the device tre, not our tables |
Simon Glass | 244548f | 2021-03-21 18:24:37 +1300 | [diff] [blame] | 215 | self.fdt.del_node(self.node.Offset()) |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 216 | with self.assertRaises(ValueError) as exc: |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 217 | self.dtb.Refresh() |
| 218 | self.assertIn('Internal error, node name mismatch ' |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 219 | 'spl-test != spl-test2', str(exc.exception)) |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 220 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 221 | def test_refresh_missing_prop(self): |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 222 | """Test refreshing offsets when an extra property is present in dtb""" |
| 223 | # Delete it from our tables, not the device tree |
| 224 | del self.node.props['notstring'] |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 225 | with self.assertRaises(ValueError) as exc: |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 226 | self.dtb.Refresh() |
Simon Glass | 93f18a1 | 2021-03-21 18:24:34 +1300 | [diff] [blame] | 227 | self.assertIn("Internal error, node '/spl-test' property 'notstring' missing, offset ", |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 228 | str(exc.exception)) |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 229 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 230 | def test_lookup_phandle(self): |
Simon Glass | e2d6528 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 231 | """Test looking up a single phandle""" |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 232 | dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts')) |
Simon Glass | e2d6528 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 233 | node = dtb.GetNode('/phandle-source2') |
| 234 | prop = node.props['clocks'] |
| 235 | target = dtb.GetNode('/phandle-target') |
| 236 | self.assertEqual(target, dtb.LookupPhandle(fdt32_to_cpu(prop.value))) |
| 237 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 238 | def test_add_node_space(self): |
Simon Glass | 244548f | 2021-03-21 18:24:37 +1300 | [diff] [blame] | 239 | """Test adding a single node when out of space""" |
| 240 | self.fdt.pack() |
| 241 | self.node.AddSubnode('subnode') |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 242 | with self.assertRaises(libfdt.FdtException) as exc: |
Simon Glass | 244548f | 2021-03-21 18:24:37 +1300 | [diff] [blame] | 243 | self.dtb.Sync(auto_resize=False) |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 244 | self.assertIn('FDT_ERR_NOSPACE', str(exc.exception)) |
Simon Glass | 244548f | 2021-03-21 18:24:37 +1300 | [diff] [blame] | 245 | |
| 246 | self.dtb.Sync(auto_resize=True) |
| 247 | offset = self.fdt.path_offset('/spl-test/subnode') |
| 248 | self.assertTrue(offset > 0) |
| 249 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 250 | def test_add_nodes(self): |
Simon Glass | 244548f | 2021-03-21 18:24:37 +1300 | [diff] [blame] | 251 | """Test adding various subnode and properies""" |
| 252 | node = self.dtb.GetNode('/i2c@0') |
| 253 | |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 254 | # Add one more node next to the pmic one |
| 255 | sn1 = node.AddSubnode('node-one') |
| 256 | sn1.AddInt('integer-a', 12) |
| 257 | sn1.AddInt('integer-b', 23) |
| 258 | |
| 259 | # Sync so that everything is clean |
| 260 | self.dtb.Sync(auto_resize=True) |
| 261 | |
| 262 | # Add two subnodes next to pmic and node-one |
| 263 | sn2 = node.AddSubnode('node-two') |
| 264 | sn2.AddInt('integer-2a', 34) |
| 265 | sn2.AddInt('integer-2b', 45) |
| 266 | |
| 267 | sn3 = node.AddSubnode('node-three') |
| 268 | sn3.AddInt('integer-3', 123) |
| 269 | |
Simon Glass | 244548f | 2021-03-21 18:24:37 +1300 | [diff] [blame] | 270 | # Add a property to the node after i2c@0 to check that this is not |
| 271 | # disturbed by adding a subnode to i2c@0 |
| 272 | orig_node = self.dtb.GetNode('/orig-node') |
| 273 | orig_node.AddInt('integer-4', 456) |
| 274 | |
| 275 | # Add a property to the pmic node to check that pmic properties are not |
| 276 | # disturbed |
| 277 | pmic = self.dtb.GetNode('/i2c@0/pmic@9') |
| 278 | pmic.AddInt('integer-5', 567) |
| 279 | |
| 280 | self.dtb.Sync(auto_resize=True) |
| 281 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 282 | def test_add_one_node(self): |
Simon Glass | 0023861 | 2022-02-08 11:49:52 -0700 | [diff] [blame] | 283 | """Testing deleting and adding a subnode before syncing""" |
| 284 | subnode = self.node.AddSubnode('subnode') |
| 285 | self.node.AddSubnode('subnode2') |
| 286 | self.dtb.Sync(auto_resize=True) |
| 287 | |
| 288 | # Delete a node and add a new one |
| 289 | subnode.Delete() |
| 290 | self.node.AddSubnode('subnode3') |
| 291 | self.dtb.Sync() |
| 292 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 293 | def test_refresh_name_mismatch(self): |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 294 | """Test name mismatch when syncing nodes and properties""" |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 295 | self.node.AddInt('integer-a', 12) |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 296 | |
| 297 | wrong_offset = self.dtb.GetNode('/i2c@0')._offset |
| 298 | self.node._offset = wrong_offset |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 299 | with self.assertRaises(ValueError) as exc: |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 300 | self.dtb.Sync() |
| 301 | self.assertIn("Internal error, node '/spl-test' name mismatch 'i2c@0'", |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 302 | str(exc.exception)) |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 303 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 304 | with self.assertRaises(ValueError) as exc: |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 305 | self.node.Refresh(wrong_offset) |
| 306 | self.assertIn("Internal error, node '/spl-test' name mismatch 'i2c@0'", |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 307 | str(exc.exception)) |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 308 | |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 309 | def test_copy_node(self): |
| 310 | """Test copy_node() function""" |
| 311 | def do_copy_checks(dtb, dst, expect_none): |
| 312 | self.assertEqual( |
| 313 | ['/dest/base', '/dest/first@0', '/dest/existing'], |
| 314 | [n.path for n in dst.subnodes]) |
| 315 | |
| 316 | chk = dtb.GetNode('/dest/base') |
| 317 | self.assertTrue(chk) |
| 318 | self.assertEqual( |
| 319 | {'compatible', 'bootph-all', '#address-cells', '#size-cells'}, |
| 320 | chk.props.keys()) |
| 321 | |
| 322 | # Check the first property |
| 323 | prop = chk.props['bootph-all'] |
| 324 | self.assertEqual('bootph-all', prop.name) |
| 325 | self.assertEqual(True, prop.value) |
| 326 | self.assertEqual(chk.path, prop._node.path) |
| 327 | |
| 328 | # Check the second property |
| 329 | prop2 = chk.props['compatible'] |
| 330 | self.assertEqual('compatible', prop2.name) |
| 331 | self.assertEqual('sandbox,i2c', prop2.value) |
| 332 | self.assertEqual(chk.path, prop2._node.path) |
| 333 | |
| 334 | base = chk.FindNode('base') |
| 335 | self.assertTrue(chk) |
| 336 | |
| 337 | first = dtb.GetNode('/dest/base/first@0') |
| 338 | self.assertTrue(first) |
| 339 | over = dtb.GetNode('/dest/base/over') |
| 340 | self.assertTrue(over) |
| 341 | |
| 342 | # Make sure that the phandle for 'over' is not copied |
| 343 | self.assertNotIn('phandle', over.props.keys()) |
| 344 | |
| 345 | second = dtb.GetNode('/dest/base/second') |
| 346 | self.assertTrue(second) |
| 347 | self.assertEqual([over.name, first.name, second.name], |
| 348 | [n.name for n in chk.subnodes]) |
| 349 | self.assertEqual(chk, over.parent) |
| 350 | self.assertEqual( |
| 351 | {'bootph-all', 'compatible', 'reg', 'low-power'}, |
| 352 | over.props.keys()) |
| 353 | |
| 354 | if expect_none: |
| 355 | self.assertIsNone(prop._offset) |
| 356 | self.assertIsNone(prop2._offset) |
| 357 | self.assertIsNone(over._offset) |
| 358 | else: |
| 359 | self.assertTrue(prop._offset) |
| 360 | self.assertTrue(prop2._offset) |
| 361 | self.assertTrue(over._offset) |
| 362 | |
| 363 | # Now check ordering of the subnodes |
| 364 | self.assertEqual( |
| 365 | ['second1', 'second2', 'second3', 'second4'], |
| 366 | [n.name for n in second.subnodes]) |
| 367 | |
| 368 | dtb = fdt.FdtScan(find_dtb_file('dtoc_test_copy.dts')) |
| 369 | tmpl = dtb.GetNode('/base') |
| 370 | dst = dtb.GetNode('/dest') |
| 371 | dst.copy_node(tmpl) |
| 372 | |
| 373 | do_copy_checks(dtb, dst, expect_none=True) |
| 374 | |
| 375 | dtb.Sync(auto_resize=True) |
| 376 | |
| 377 | # Now check that the FDT looks correct |
| 378 | new_dtb = fdt.Fdt.FromData(dtb.GetContents()) |
| 379 | new_dtb.Scan() |
| 380 | dst = new_dtb.GetNode('/dest') |
| 381 | do_copy_checks(new_dtb, dst, expect_none=False) |
| 382 | |
Simon Glass | cd97119 | 2023-07-18 07:24:03 -0600 | [diff] [blame] | 383 | def test_copy_subnodes_from_phandles(self): |
| 384 | """Test copy_node() function""" |
| 385 | dtb = fdt.FdtScan(find_dtb_file('dtoc_test_copy.dts')) |
| 386 | |
| 387 | orig = dtb.GetNode('/') |
| 388 | node_list = fdt_util.GetPhandleList(orig, 'copy-list') |
| 389 | |
| 390 | dst = dtb.GetNode('/dest') |
| 391 | dst.copy_subnodes_from_phandles(node_list) |
| 392 | |
| 393 | pmic = dtb.GetNode('/dest/over') |
| 394 | self.assertTrue(pmic) |
| 395 | |
| 396 | subn = dtb.GetNode('/dest/first@0') |
| 397 | self.assertTrue(subn) |
| 398 | self.assertEqual({'a-prop', 'b-prop', 'reg'}, subn.props.keys()) |
| 399 | |
| 400 | self.assertEqual( |
| 401 | ['/dest/earlier', '/dest/later', '/dest/over', '/dest/first@0', |
| 402 | '/dest/second', '/dest/existing', '/dest/base'], |
| 403 | [n.path for n in dst.subnodes]) |
| 404 | |
| 405 | # Make sure that the phandle for 'over' is not copied |
| 406 | over = dst.FindNode('over') |
| 407 | print('keys', over.props.keys()) |
| 408 | self.assertNotIn('phandle', over.props.keys()) |
| 409 | |
| 410 | # Check the merged properties, first the base ones in '/dest' |
| 411 | expect = {'bootph-all', 'compatible', 'stringarray', 'longbytearray', |
| 412 | 'maybe-empty-int'} |
| 413 | |
| 414 | # Properties from 'base' |
| 415 | expect.update({'#address-cells', '#size-cells'}) |
| 416 | |
| 417 | # Properties from 'another' |
| 418 | expect.add('new-prop') |
| 419 | |
| 420 | self.assertEqual(expect, set(dst.props.keys())) |
| 421 | |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 422 | |
| 423 | class TestProp(unittest.TestCase): |
| 424 | """Test operation of the Prop class""" |
| 425 | |
| 426 | @classmethod |
| 427 | def setUpClass(cls): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 428 | tools.prepare_output_dir(None) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 429 | |
| 430 | @classmethod |
| 431 | def tearDownClass(cls): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 432 | tools.finalise_output_dir() |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 433 | |
| 434 | def setUp(self): |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 435 | self.dtb = fdt.FdtScan(find_dtb_file('dtoc_test_simple.dts')) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 436 | self.node = self.dtb.GetNode('/spl-test') |
| 437 | self.fdt = self.dtb.GetFdtObj() |
| 438 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 439 | def test_missing_node(self): |
| 440 | """Test GetNode() when the node is missing""" |
Simon Glass | c5eddc8 | 2018-07-06 10:27:30 -0600 | [diff] [blame] | 441 | self.assertEqual(None, self.dtb.GetNode('missing')) |
| 442 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 443 | def test_phandle(self): |
| 444 | """Test GetNode() on a phandle""" |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 445 | dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts')) |
Simon Glass | 609e2b1 | 2018-07-06 10:27:31 -0600 | [diff] [blame] | 446 | node = dtb.GetNode('/phandle-source2') |
| 447 | prop = node.props['clocks'] |
| 448 | self.assertTrue(fdt32_to_cpu(prop.value) > 0) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 449 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 450 | def _convert_prop(self, prop_name): |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 451 | """Helper function to look up a property in self.node and return it |
| 452 | |
| 453 | Args: |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 454 | str: Property name to find |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 455 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 456 | Returns: |
| 457 | fdt.Prop: object for this property |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 458 | """ |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 459 | prop = self.fdt.getprop(self.node.Offset(), prop_name) |
| 460 | return fdt.Prop(self.node, -1, prop_name, prop) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 461 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 462 | def test_make_prop(self): |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 463 | """Test we can convert all the the types that are supported""" |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 464 | prop = self._convert_prop('boolval') |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 465 | self.assertEqual(Type.BOOL, prop.type) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 466 | self.assertEqual(True, prop.value) |
| 467 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 468 | prop = self._convert_prop('intval') |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 469 | self.assertEqual(Type.INT, prop.type) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 470 | self.assertEqual(1, fdt32_to_cpu(prop.value)) |
| 471 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 472 | prop = self._convert_prop('int64val') |
Simon Glass | 3b55e3f | 2021-11-23 11:03:39 -0700 | [diff] [blame] | 473 | self.assertEqual(Type.INT, prop.type) |
| 474 | self.assertEqual(0x123456789abcdef0, fdt64_to_cpu(prop.value)) |
| 475 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 476 | prop = self._convert_prop('intarray') |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 477 | self.assertEqual(Type.INT, prop.type) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 478 | val = [fdt32_to_cpu(val) for val in prop.value] |
| 479 | self.assertEqual([2, 3, 4], val) |
| 480 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 481 | prop = self._convert_prop('byteval') |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 482 | self.assertEqual(Type.BYTE, prop.type) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 483 | self.assertEqual(5, ord(prop.value)) |
| 484 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 485 | prop = self._convert_prop('longbytearray') |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 486 | self.assertEqual(Type.BYTE, prop.type) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 487 | val = [ord(val) for val in prop.value] |
| 488 | self.assertEqual([9, 10, 11, 12, 13, 14, 15, 16, 17], val) |
| 489 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 490 | prop = self._convert_prop('stringval') |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 491 | self.assertEqual(Type.STRING, prop.type) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 492 | self.assertEqual('message', prop.value) |
| 493 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 494 | prop = self._convert_prop('stringarray') |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 495 | self.assertEqual(Type.STRING, prop.type) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 496 | self.assertEqual(['multi-word', 'message'], prop.value) |
| 497 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 498 | prop = self._convert_prop('notstring') |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 499 | self.assertEqual(Type.BYTE, prop.type) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 500 | val = [ord(val) for val in prop.value] |
| 501 | self.assertEqual([0x20, 0x21, 0x22, 0x10, 0], val) |
| 502 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 503 | def test_get_empty(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 504 | """Tests the GetEmpty() function for the various supported types""" |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 505 | self.assertEqual(True, fdt.Prop.GetEmpty(Type.BOOL)) |
| 506 | self.assertEqual(chr(0), fdt.Prop.GetEmpty(Type.BYTE)) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 507 | self.assertEqual(tools.get_bytes(0, 4), fdt.Prop.GetEmpty(Type.INT)) |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 508 | self.assertEqual('', fdt.Prop.GetEmpty(Type.STRING)) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 509 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 510 | def test_get_offset(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 511 | """Test we can get the offset of a property""" |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 512 | prop, value = _get_property_value(self.dtb, self.node, 'longbytearray') |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 513 | self.assertEqual(prop.value, value) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 514 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 515 | def test_widen(self): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 516 | """Test widening of values""" |
| 517 | node2 = self.dtb.GetNode('/spl-test2') |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 518 | node3 = self.dtb.GetNode('/spl-test3') |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 519 | prop = self.node.props['intval'] |
| 520 | |
| 521 | # No action |
| 522 | prop2 = node2.props['intval'] |
| 523 | prop.Widen(prop2) |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 524 | self.assertEqual(Type.INT, prop.type) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 525 | self.assertEqual(1, fdt32_to_cpu(prop.value)) |
| 526 | |
Simon Glass | a7d6698 | 2021-07-28 19:23:10 -0600 | [diff] [blame] | 527 | # Convert single value to array |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 528 | prop2 = self.node.props['intarray'] |
| 529 | prop.Widen(prop2) |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 530 | self.assertEqual(Type.INT, prop.type) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 531 | self.assertTrue(isinstance(prop.value, list)) |
| 532 | |
| 533 | # A 4-byte array looks like a single integer. When widened by a longer |
| 534 | # byte array, it should turn into an array. |
| 535 | prop = self.node.props['longbytearray'] |
| 536 | prop2 = node2.props['longbytearray'] |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 537 | prop3 = node3.props['longbytearray'] |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 538 | self.assertFalse(isinstance(prop2.value, list)) |
| 539 | self.assertEqual(4, len(prop2.value)) |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 540 | self.assertEqual(b'\x09\x0a\x0b\x0c', prop2.value) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 541 | prop2.Widen(prop) |
| 542 | self.assertTrue(isinstance(prop2.value, list)) |
| 543 | self.assertEqual(9, len(prop2.value)) |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 544 | self.assertEqual(['\x09', '\x0a', '\x0b', '\x0c', '\0', |
| 545 | '\0', '\0', '\0', '\0'], prop2.value) |
| 546 | prop3.Widen(prop) |
| 547 | self.assertTrue(isinstance(prop3.value, list)) |
| 548 | self.assertEqual(9, len(prop3.value)) |
| 549 | self.assertEqual(['\x09', '\x0a', '\x0b', '\x0c', '\x0d', |
| 550 | '\x0e', '\x0f', '\x10', '\0'], prop3.value) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 551 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 552 | def test_widen_more(self): |
| 553 | """More tests of widening values""" |
| 554 | node2 = self.dtb.GetNode('/spl-test2') |
| 555 | node3 = self.dtb.GetNode('/spl-test3') |
| 556 | prop = self.node.props['intval'] |
| 557 | |
| 558 | # Test widening a single string into a string array |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 559 | prop = self.node.props['stringval'] |
| 560 | prop2 = node2.props['stringarray'] |
| 561 | self.assertFalse(isinstance(prop.value, list)) |
| 562 | self.assertEqual(7, len(prop.value)) |
| 563 | prop.Widen(prop2) |
| 564 | self.assertTrue(isinstance(prop.value, list)) |
| 565 | self.assertEqual(3, len(prop.value)) |
| 566 | |
| 567 | # Enlarging an existing array |
| 568 | prop = self.node.props['stringarray'] |
| 569 | prop2 = node2.props['stringarray'] |
| 570 | self.assertTrue(isinstance(prop.value, list)) |
| 571 | self.assertEqual(2, len(prop.value)) |
| 572 | prop.Widen(prop2) |
| 573 | self.assertTrue(isinstance(prop.value, list)) |
| 574 | self.assertEqual(3, len(prop.value)) |
| 575 | |
Simon Glass | a7d6698 | 2021-07-28 19:23:10 -0600 | [diff] [blame] | 576 | # Widen an array of ints with an int (should do nothing) |
| 577 | prop = self.node.props['intarray'] |
Simon Glass | f42f26e | 2021-08-02 07:37:54 -0600 | [diff] [blame] | 578 | prop2 = node2.props['intval'] |
Simon Glass | a7d6698 | 2021-07-28 19:23:10 -0600 | [diff] [blame] | 579 | self.assertEqual(Type.INT, prop.type) |
| 580 | self.assertEqual(3, len(prop.value)) |
| 581 | prop.Widen(prop2) |
| 582 | self.assertEqual(Type.INT, prop.type) |
| 583 | self.assertEqual(3, len(prop.value)) |
| 584 | |
Simon Glass | 4311832 | 2021-07-28 19:23:11 -0600 | [diff] [blame] | 585 | # Widen an empty bool to an int |
| 586 | prop = self.node.props['maybe-empty-int'] |
| 587 | prop3 = node3.props['maybe-empty-int'] |
| 588 | self.assertEqual(Type.BOOL, prop.type) |
| 589 | self.assertEqual(True, prop.value) |
| 590 | self.assertEqual(Type.INT, prop3.type) |
| 591 | self.assertFalse(isinstance(prop.value, list)) |
| 592 | self.assertEqual(4, len(prop3.value)) |
| 593 | prop.Widen(prop3) |
| 594 | self.assertEqual(Type.INT, prop.type) |
| 595 | self.assertTrue(isinstance(prop.value, list)) |
| 596 | self.assertEqual(1, len(prop.value)) |
| 597 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 598 | def test_add(self): |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 599 | """Test adding properties""" |
| 600 | self.fdt.pack() |
| 601 | # This function should automatically expand the device tree |
| 602 | self.node.AddZeroProp('one') |
| 603 | self.node.AddZeroProp('two') |
| 604 | self.node.AddZeroProp('three') |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 605 | self.dtb.Sync(auto_resize=True) |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 606 | |
| 607 | # Updating existing properties should be OK, since the device-tree size |
| 608 | # does not change |
| 609 | self.fdt.pack() |
| 610 | self.node.SetInt('one', 1) |
| 611 | self.node.SetInt('two', 2) |
| 612 | self.node.SetInt('three', 3) |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 613 | self.dtb.Sync(auto_resize=False) |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 614 | |
| 615 | # This should fail since it would need to increase the device-tree size |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 616 | self.node.AddZeroProp('four') |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 617 | with self.assertRaises(libfdt.FdtException) as exc: |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 618 | self.dtb.Sync(auto_resize=False) |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 619 | self.assertIn('FDT_ERR_NOSPACE', str(exc.exception)) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 620 | self.dtb.Sync(auto_resize=True) |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 621 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 622 | def test_add_more(self): |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 623 | """Test various other methods for adding and setting properties""" |
| 624 | self.node.AddZeroProp('one') |
| 625 | self.dtb.Sync(auto_resize=True) |
| 626 | data = self.fdt.getprop(self.node.Offset(), 'one') |
| 627 | self.assertEqual(0, fdt32_to_cpu(data)) |
| 628 | |
| 629 | self.node.SetInt('one', 1) |
| 630 | self.dtb.Sync(auto_resize=False) |
| 631 | data = self.fdt.getprop(self.node.Offset(), 'one') |
| 632 | self.assertEqual(1, fdt32_to_cpu(data)) |
| 633 | |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 634 | val = 1234 |
| 635 | self.node.AddInt('integer', val) |
| 636 | self.dtb.Sync(auto_resize=True) |
| 637 | data = self.fdt.getprop(self.node.Offset(), 'integer') |
| 638 | self.assertEqual(val, fdt32_to_cpu(data)) |
| 639 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 640 | val = '123' + chr(0) + '456' |
| 641 | self.node.AddString('string', val) |
| 642 | self.dtb.Sync(auto_resize=True) |
| 643 | data = self.fdt.getprop(self.node.Offset(), 'string') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 644 | self.assertEqual(tools.to_bytes(val) + b'\0', data) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 645 | |
| 646 | self.fdt.pack() |
| 647 | self.node.SetString('string', val + 'x') |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 648 | with self.assertRaises(libfdt.FdtException) as exc: |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 649 | self.dtb.Sync(auto_resize=False) |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 650 | self.assertIn('FDT_ERR_NOSPACE', str(exc.exception)) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 651 | self.node.SetString('string', val[:-1]) |
| 652 | |
| 653 | prop = self.node.props['string'] |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 654 | prop.SetData(tools.to_bytes(val)) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 655 | self.dtb.Sync(auto_resize=False) |
| 656 | data = self.fdt.getprop(self.node.Offset(), 'string') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 657 | self.assertEqual(tools.to_bytes(val), data) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 658 | |
| 659 | self.node.AddEmptyProp('empty', 5) |
| 660 | self.dtb.Sync(auto_resize=True) |
| 661 | prop = self.node.props['empty'] |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 662 | prop.SetData(tools.to_bytes(val)) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 663 | self.dtb.Sync(auto_resize=False) |
| 664 | data = self.fdt.getprop(self.node.Offset(), 'empty') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 665 | self.assertEqual(tools.to_bytes(val), data) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 666 | |
Simon Glass | 1cd4008 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 667 | self.node.SetData('empty', b'123') |
| 668 | self.assertEqual(b'123', prop.bytes) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 669 | |
Simon Glass | f67c99c | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 670 | # Trying adding a lot of data at once |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 671 | self.node.AddData('data', tools.get_bytes(65, 20000)) |
Simon Glass | f67c99c | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 672 | self.dtb.Sync(auto_resize=True) |
| 673 | |
Simon Glass | 452be42 | 2022-02-08 11:49:50 -0700 | [diff] [blame] | 674 | def test_string_list(self): |
| 675 | """Test adding string-list property to a node""" |
| 676 | val = ['123', '456'] |
| 677 | self.node.AddStringList('stringlist', val) |
| 678 | self.dtb.Sync(auto_resize=True) |
| 679 | data = self.fdt.getprop(self.node.Offset(), 'stringlist') |
| 680 | self.assertEqual(b'123\x00456\0', data) |
| 681 | |
Simon Glass | 120fa00 | 2022-03-05 20:18:56 -0700 | [diff] [blame] | 682 | val = [] |
| 683 | self.node.AddStringList('stringlist', val) |
| 684 | self.dtb.Sync(auto_resize=True) |
| 685 | data = self.fdt.getprop(self.node.Offset(), 'stringlist') |
| 686 | self.assertEqual(b'', data) |
| 687 | |
Simon Glass | b9b5cb3 | 2022-02-08 11:49:51 -0700 | [diff] [blame] | 688 | def test_delete_node(self): |
| 689 | """Test deleting a node""" |
| 690 | old_offset = self.fdt.path_offset('/spl-test') |
| 691 | self.assertGreater(old_offset, 0) |
| 692 | self.node.Delete() |
| 693 | self.dtb.Sync() |
| 694 | new_offset = self.fdt.path_offset('/spl-test', libfdt.QUIET_NOTFOUND) |
| 695 | self.assertEqual(-libfdt.NOTFOUND, new_offset) |
| 696 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 697 | def test_from_data(self): |
| 698 | """Test creating an FDT from data""" |
Simon Glass | b8a4929 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 699 | dtb2 = fdt.Fdt.FromData(self.dtb.GetContents()) |
| 700 | self.assertEqual(dtb2.GetContents(), self.dtb.GetContents()) |
| 701 | |
| 702 | self.node.AddEmptyProp('empty', 5) |
| 703 | self.dtb.Sync(auto_resize=True) |
| 704 | self.assertTrue(dtb2.GetContents() != self.dtb.GetContents()) |
| 705 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 706 | def test_missing_set_int(self): |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 707 | """Test handling of a missing property with SetInt""" |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 708 | with self.assertRaises(ValueError) as exc: |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 709 | self.node.SetInt('one', 1) |
| 710 | self.assertIn("node '/spl-test': Missing property 'one'", |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 711 | str(exc.exception)) |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 712 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 713 | def test_missing_set_data(self): |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 714 | """Test handling of a missing property with SetData""" |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 715 | with self.assertRaises(ValueError) as exc: |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 716 | self.node.SetData('one', b'data') |
| 717 | self.assertIn("node '/spl-test': Missing property 'one'", |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 718 | str(exc.exception)) |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 719 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 720 | def test_missing_set_string(self): |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 721 | """Test handling of a missing property with SetString""" |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 722 | with self.assertRaises(ValueError) as exc: |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 723 | self.node.SetString('one', 1) |
| 724 | self.assertIn("node '/spl-test': Missing property 'one'", |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 725 | str(exc.exception)) |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 726 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 727 | def test_get_filename(self): |
Simon Glass | 74f5feb | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 728 | """Test the dtb filename can be provided""" |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 729 | self.assertEqual(tools.get_output_filename('source.dtb'), |
Simon Glass | 74f5feb | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 730 | self.dtb.GetFilename()) |
| 731 | |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 732 | |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 733 | class TestFdtUtil(unittest.TestCase): |
| 734 | """Tests for the fdt_util module |
| 735 | |
| 736 | This module will likely be mostly replaced at some point, once upstream |
| 737 | libfdt has better Python support. For now, this provides tests for current |
| 738 | functionality. |
| 739 | """ |
| 740 | @classmethod |
| 741 | def setUpClass(cls): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 742 | tools.prepare_output_dir(None) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 743 | |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 744 | @classmethod |
| 745 | def tearDownClass(cls): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 746 | tools.finalise_output_dir() |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 747 | |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 748 | def setUp(self): |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 749 | self.dtb = fdt.FdtScan(find_dtb_file('dtoc_test_simple.dts')) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 750 | self.node = self.dtb.GetNode('/spl-test') |
| 751 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 752 | def test_get_int(self): |
| 753 | """Test getting an int from a node""" |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 754 | self.assertEqual(1, fdt_util.GetInt(self.node, 'intval')) |
| 755 | self.assertEqual(3, fdt_util.GetInt(self.node, 'missing', 3)) |
| 756 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 757 | with self.assertRaises(ValueError) as exc: |
Simon Glass | 3b55e3f | 2021-11-23 11:03:39 -0700 | [diff] [blame] | 758 | fdt_util.GetInt(self.node, 'intarray') |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 759 | self.assertIn("property 'intarray' has list value: expecting a single " |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 760 | 'integer', str(exc.exception)) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 761 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 762 | def test_get_int64(self): |
| 763 | """Test getting a 64-bit int from a node""" |
Simon Glass | 3b55e3f | 2021-11-23 11:03:39 -0700 | [diff] [blame] | 764 | self.assertEqual(0x123456789abcdef0, |
| 765 | fdt_util.GetInt64(self.node, 'int64val')) |
| 766 | self.assertEqual(3, fdt_util.GetInt64(self.node, 'missing', 3)) |
| 767 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 768 | with self.assertRaises(ValueError) as exc: |
Simon Glass | 3b55e3f | 2021-11-23 11:03:39 -0700 | [diff] [blame] | 769 | fdt_util.GetInt64(self.node, 'intarray') |
| 770 | self.assertIn( |
| 771 | "property 'intarray' should be a list with 2 items for 64-bit values", |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 772 | str(exc.exception)) |
Simon Glass | 3b55e3f | 2021-11-23 11:03:39 -0700 | [diff] [blame] | 773 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 774 | def test_get_string(self): |
| 775 | """Test getting a string from a node""" |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 776 | self.assertEqual('message', fdt_util.GetString(self.node, 'stringval')) |
| 777 | self.assertEqual('test', fdt_util.GetString(self.node, 'missing', |
| 778 | 'test')) |
Simon Glass | 120fa00 | 2022-03-05 20:18:56 -0700 | [diff] [blame] | 779 | self.assertEqual('', fdt_util.GetString(self.node, 'boolval')) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 780 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 781 | with self.assertRaises(ValueError) as exc: |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 782 | self.assertEqual(3, fdt_util.GetString(self.node, 'stringarray')) |
| 783 | self.assertIn("property 'stringarray' has list value: expecting a " |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 784 | 'single string', str(exc.exception)) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 785 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 786 | def test_get_string_list(self): |
| 787 | """Test getting a string list from a node""" |
Simon Glass | b2e8861 | 2021-11-23 21:09:51 -0700 | [diff] [blame] | 788 | self.assertEqual(['message'], |
| 789 | fdt_util.GetStringList(self.node, 'stringval')) |
| 790 | self.assertEqual( |
| 791 | ['multi-word', 'message'], |
| 792 | fdt_util.GetStringList(self.node, 'stringarray')) |
| 793 | self.assertEqual(['test'], |
| 794 | fdt_util.GetStringList(self.node, 'missing', ['test'])) |
Simon Glass | 120fa00 | 2022-03-05 20:18:56 -0700 | [diff] [blame] | 795 | self.assertEqual([], fdt_util.GetStringList(self.node, 'boolval')) |
Simon Glass | b2e8861 | 2021-11-23 21:09:51 -0700 | [diff] [blame] | 796 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 797 | def test_get_args(self): |
| 798 | """Test getting arguments from a node""" |
Simon Glass | 738a54d | 2022-02-08 11:49:53 -0700 | [diff] [blame] | 799 | node = self.dtb.GetNode('/orig-node') |
| 800 | self.assertEqual(['message'], fdt_util.GetArgs(self.node, 'stringval')) |
| 801 | self.assertEqual( |
| 802 | ['multi-word', 'message'], |
| 803 | fdt_util.GetArgs(self.node, 'stringarray')) |
| 804 | self.assertEqual([], fdt_util.GetArgs(self.node, 'boolval')) |
Simon Glass | c6b3cdc | 2022-03-05 20:18:52 -0700 | [diff] [blame] | 805 | self.assertEqual(['-n first', 'second', '-p', '123,456', '-x'], |
Simon Glass | 738a54d | 2022-02-08 11:49:53 -0700 | [diff] [blame] | 806 | fdt_util.GetArgs(node, 'args')) |
Simon Glass | c6b3cdc | 2022-03-05 20:18:52 -0700 | [diff] [blame] | 807 | self.assertEqual(['a space', 'there'], |
| 808 | fdt_util.GetArgs(node, 'args2')) |
| 809 | self.assertEqual(['-n', 'first', 'second', '-p', '123,456', '-x'], |
| 810 | fdt_util.GetArgs(node, 'args3')) |
Simon Glass | 738a54d | 2022-02-08 11:49:53 -0700 | [diff] [blame] | 811 | with self.assertRaises(ValueError) as exc: |
| 812 | fdt_util.GetArgs(self.node, 'missing') |
| 813 | self.assertIn( |
| 814 | "Node '/spl-test': Expected property 'missing'", |
| 815 | str(exc.exception)) |
| 816 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 817 | def test_get_bool(self): |
| 818 | """Test getting a bool from a node""" |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 819 | self.assertEqual(True, fdt_util.GetBool(self.node, 'boolval')) |
| 820 | self.assertEqual(False, fdt_util.GetBool(self.node, 'missing')) |
| 821 | self.assertEqual(True, fdt_util.GetBool(self.node, 'missing', True)) |
| 822 | self.assertEqual(False, fdt_util.GetBool(self.node, 'missing', False)) |
| 823 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 824 | def test_get_byte(self): |
| 825 | """Test getting a byte from a node""" |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 826 | self.assertEqual(5, fdt_util.GetByte(self.node, 'byteval')) |
| 827 | self.assertEqual(3, fdt_util.GetByte(self.node, 'missing', 3)) |
| 828 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 829 | with self.assertRaises(ValueError) as exc: |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 830 | fdt_util.GetByte(self.node, 'longbytearray') |
| 831 | self.assertIn("property 'longbytearray' has list value: expecting a " |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 832 | 'single byte', str(exc.exception)) |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 833 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 834 | with self.assertRaises(ValueError) as exc: |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 835 | fdt_util.GetByte(self.node, 'intval') |
| 836 | self.assertIn("property 'intval' has length 4, expecting 1", |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 837 | str(exc.exception)) |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 838 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 839 | def test_get_bytes(self): |
| 840 | """Test getting multiple bytes from a node""" |
Simon Glass | 0e055bf | 2021-11-23 11:03:40 -0700 | [diff] [blame] | 841 | self.assertEqual(bytes([5]), fdt_util.GetBytes(self.node, 'byteval', 1)) |
| 842 | self.assertEqual(None, fdt_util.GetBytes(self.node, 'missing', 3)) |
| 843 | self.assertEqual( |
| 844 | bytes([3]), fdt_util.GetBytes(self.node, 'missing', 3, bytes([3]))) |
| 845 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 846 | with self.assertRaises(ValueError) as exc: |
Simon Glass | 0e055bf | 2021-11-23 11:03:40 -0700 | [diff] [blame] | 847 | fdt_util.GetBytes(self.node, 'longbytearray', 7) |
| 848 | self.assertIn( |
| 849 | "Node 'spl-test' property 'longbytearray' has length 9, expecting 7", |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 850 | str(exc.exception)) |
Simon Glass | 0e055bf | 2021-11-23 11:03:40 -0700 | [diff] [blame] | 851 | |
| 852 | self.assertEqual( |
| 853 | bytes([0, 0, 0, 1]), fdt_util.GetBytes(self.node, 'intval', 4)) |
| 854 | self.assertEqual( |
| 855 | bytes([3]), fdt_util.GetBytes(self.node, 'missing', 3, bytes([3]))) |
| 856 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 857 | def test_get_phandle_list(self): |
| 858 | """Test getting a list of phandles from a node""" |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 859 | dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts')) |
Simon Glass | e2d6528 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 860 | node = dtb.GetNode('/phandle-source2') |
| 861 | self.assertEqual([1], fdt_util.GetPhandleList(node, 'clocks')) |
| 862 | node = dtb.GetNode('/phandle-source') |
| 863 | self.assertEqual([1, 2, 11, 3, 12, 13, 1], |
| 864 | fdt_util.GetPhandleList(node, 'clocks')) |
| 865 | self.assertEqual(None, fdt_util.GetPhandleList(node, 'missing')) |
| 866 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 867 | def test_get_data_type(self): |
| 868 | """Test getting a value of a particular type from a node""" |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 869 | self.assertEqual(1, fdt_util.GetDatatype(self.node, 'intval', int)) |
| 870 | self.assertEqual('message', fdt_util.GetDatatype(self.node, 'stringval', |
| 871 | str)) |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 872 | with self.assertRaises(ValueError): |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 873 | self.assertEqual(3, fdt_util.GetDatatype(self.node, 'boolval', |
| 874 | bool)) |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 875 | def test_fdt_cells_to_cpu(self): |
| 876 | """Test getting cells with the correct endianness""" |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 877 | val = self.node.props['intarray'].value |
| 878 | self.assertEqual(0, fdt_util.fdt_cells_to_cpu(val, 0)) |
| 879 | self.assertEqual(2, fdt_util.fdt_cells_to_cpu(val, 1)) |
| 880 | |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 881 | dtb2 = fdt.FdtScan(find_dtb_file('dtoc_test_addr64.dts')) |
Simon Glass | b0a34a4 | 2019-05-17 22:00:40 -0600 | [diff] [blame] | 882 | node1 = dtb2.GetNode('/test1') |
| 883 | val = node1.props['reg'].value |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 884 | self.assertEqual(0x1234, fdt_util.fdt_cells_to_cpu(val, 2)) |
| 885 | |
Simon Glass | b0a34a4 | 2019-05-17 22:00:40 -0600 | [diff] [blame] | 886 | node2 = dtb2.GetNode('/test2') |
| 887 | val = node2.props['reg'].value |
| 888 | self.assertEqual(0x1234567890123456, fdt_util.fdt_cells_to_cpu(val, 2)) |
| 889 | self.assertEqual(0x9876543210987654, fdt_util.fdt_cells_to_cpu(val[2:], |
| 890 | 2)) |
| 891 | self.assertEqual(0x12345678, fdt_util.fdt_cells_to_cpu(val, 1)) |
| 892 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 893 | def test_ensure_compiled(self): |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 894 | """Test a degenerate case of this function (file already compiled)""" |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 895 | dtb = fdt_util.EnsureCompiled(find_dtb_file('dtoc_test_simple.dts')) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 896 | self.assertEqual(dtb, fdt_util.EnsureCompiled(dtb)) |
| 897 | |
Simon Glass | 04afdfe | 2022-07-30 20:57:10 -0600 | [diff] [blame] | 898 | def test_ensure_compiled_tmpdir(self): |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 899 | """Test providing a temporary directory""" |
Heinrich Schuchardt | e72dcd4 | 2023-04-20 20:03:43 +0200 | [diff] [blame] | 900 | old_outdir = tools.outdir |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 901 | try: |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 902 | tools.outdir= None |
| 903 | tmpdir = tempfile.mkdtemp(prefix='test_fdt.') |
Simon Glass | 4f4b240 | 2021-02-03 06:00:56 -0700 | [diff] [blame] | 904 | dtb = fdt_util.EnsureCompiled(find_dtb_file('dtoc_test_simple.dts'), |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 905 | tmpdir) |
| 906 | self.assertEqual(tmpdir, os.path.dirname(dtb)) |
| 907 | shutil.rmtree(tmpdir) |
| 908 | finally: |
Heinrich Schuchardt | e72dcd4 | 2023-04-20 20:03:43 +0200 | [diff] [blame] | 909 | tools.outdir = old_outdir |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 910 | |
Simon Glass | e8cea0e | 2023-01-11 16:10:18 -0700 | [diff] [blame] | 911 | def test_get_phandle_name_offset(self): |
| 912 | val = fdt_util.GetPhandleNameOffset(self.node, 'missing') |
| 913 | self.assertIsNone(val) |
| 914 | |
| 915 | dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts')) |
| 916 | node = dtb.GetNode('/phandle-source') |
| 917 | node, name, offset = fdt_util.GetPhandleNameOffset(node, |
| 918 | 'phandle-name-offset') |
| 919 | self.assertEqual('phandle3-target', node.name) |
| 920 | self.assertEqual('fred', name) |
| 921 | self.assertEqual(123, offset) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 922 | |
Simon Glass | 9852bef | 2022-07-30 20:57:05 -0600 | [diff] [blame] | 923 | def run_test_coverage(build_dir): |
| 924 | """Run the tests and check that we get 100% coverage |
| 925 | |
| 926 | Args: |
| 927 | build_dir (str): Directory containing the build output |
| 928 | """ |
Simon Glass | 1b53d90 | 2022-01-29 14:14:14 -0700 | [diff] [blame] | 929 | test_util.run_test_coverage('tools/dtoc/test_fdt.py', None, |
Simon Glass | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 930 | ['tools/patman/*.py', 'tools/u_boot_pylib/*', '*test_fdt.py'], |
| 931 | build_dir) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 932 | |
| 933 | |
Simon Glass | 882f632 | 2022-07-30 20:57:09 -0600 | [diff] [blame] | 934 | def run_tests(names, processes): |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 935 | """Run all the test we have for the fdt model |
| 936 | |
| 937 | Args: |
Simon Glass | 882f632 | 2022-07-30 20:57:09 -0600 | [diff] [blame] | 938 | names (list of str): List of test names provided. Only the first is used |
Simon Glass | 8726f66 | 2022-07-30 20:57:06 -0600 | [diff] [blame] | 939 | processes (int): Number of processes to use (None means as many as there |
| 940 | are CPUs on the system. This must be set to 1 when running under |
| 941 | the python3-coverage tool |
Simon Glass | 77164ee | 2022-03-18 18:01:50 -0600 | [diff] [blame] | 942 | |
| 943 | Returns: |
Simon Glass | 8726f66 | 2022-07-30 20:57:06 -0600 | [diff] [blame] | 944 | int: Return code, 0 on success |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 945 | """ |
Simon Glass | 882f632 | 2022-07-30 20:57:09 -0600 | [diff] [blame] | 946 | test_name = names[0] if names else None |
Alper Nebi Yasak | ca1c588 | 2022-04-02 20:06:06 +0300 | [diff] [blame] | 947 | result = test_util.run_test_suites( |
Simon Glass | b6ec10c | 2022-07-30 20:57:07 -0600 | [diff] [blame] | 948 | 'test_fdt', False, False, False, processes, test_name, None, |
Simon Glass | 77164ee | 2022-03-18 18:01:50 -0600 | [diff] [blame] | 949 | [TestFdt, TestNode, TestProp, TestFdtUtil]) |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 950 | |
Alper Nebi Yasak | ca1c588 | 2022-04-02 20:06:06 +0300 | [diff] [blame] | 951 | return (0 if result.wasSuccessful() else 1) |
| 952 | |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 953 | |
Simon Glass | 9954cda | 2022-07-30 20:57:08 -0600 | [diff] [blame] | 954 | def main(): |
| 955 | """Main program for this tool""" |
Simon Glass | 882f632 | 2022-07-30 20:57:09 -0600 | [diff] [blame] | 956 | parser = ArgumentParser() |
| 957 | parser.add_argument('-B', '--build-dir', type=str, default='b', |
| 958 | help='Directory containing the build output') |
| 959 | parser.add_argument('-P', '--processes', type=int, |
| 960 | help='set number of processes to use for running tests') |
| 961 | parser.add_argument('-t', '--test', action='store_true', dest='test', |
| 962 | default=False, help='run tests') |
| 963 | parser.add_argument('-T', '--test-coverage', action='store_true', |
| 964 | default=False, |
| 965 | help='run tests and check for 100% coverage') |
| 966 | parser.add_argument('name', nargs='*') |
| 967 | args = parser.parse_args() |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 968 | |
Simon Glass | 9954cda | 2022-07-30 20:57:08 -0600 | [diff] [blame] | 969 | # Run our meagre tests |
Simon Glass | 882f632 | 2022-07-30 20:57:09 -0600 | [diff] [blame] | 970 | if args.test: |
| 971 | ret_code = run_tests(args.name, args.processes) |
Simon Glass | 9954cda | 2022-07-30 20:57:08 -0600 | [diff] [blame] | 972 | return ret_code |
Simon Glass | 882f632 | 2022-07-30 20:57:09 -0600 | [diff] [blame] | 973 | if args.test_coverage: |
| 974 | run_test_coverage(args.build_dir) |
Simon Glass | 9954cda | 2022-07-30 20:57:08 -0600 | [diff] [blame] | 975 | return 0 |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 976 | |
Simon Glass | 9954cda | 2022-07-30 20:57:08 -0600 | [diff] [blame] | 977 | if __name__ == '__main__': |
| 978 | sys.exit(main()) |