2019-10-31 13:42:54 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-07-06 16:27:24 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
|
|
|
# Copyright (c) 2018 Google, Inc
|
|
|
|
# Written by Simon Glass <sjg@chromium.org>
|
|
|
|
#
|
|
|
|
|
|
|
|
from optparse import OptionParser
|
|
|
|
import glob
|
|
|
|
import os
|
2019-07-20 18:23:49 +00:00
|
|
|
import shutil
|
2018-07-06 16:27:24 +00:00
|
|
|
import sys
|
2019-07-20 18:23:49 +00:00
|
|
|
import tempfile
|
2018-07-06 16:27:24 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
# Bring in the patman libraries
|
|
|
|
our_path = os.path.dirname(os.path.realpath(__file__))
|
2020-04-18 00:09:05 +00:00
|
|
|
sys.path.insert(1, os.path.join(our_path, '..'))
|
2018-07-06 16:27:24 +00:00
|
|
|
|
2021-11-23 18:03:38 +00:00
|
|
|
# Bring in the libfdt module
|
|
|
|
sys.path.insert(2, 'scripts/dtc/pylibfdt')
|
|
|
|
sys.path.insert(2, os.path.join(our_path, '../../scripts/dtc/pylibfdt'))
|
|
|
|
sys.path.insert(2, os.path.join(our_path,
|
|
|
|
'../../build-sandbox_spl/scripts/dtc/pylibfdt'))
|
|
|
|
|
2020-04-18 00:09:04 +00:00
|
|
|
from dtoc import fdt
|
|
|
|
from dtoc import fdt_util
|
2021-11-23 18:03:39 +00:00
|
|
|
from dtoc.fdt_util import fdt32_to_cpu, fdt64_to_cpu
|
2022-02-11 20:23:20 +00:00
|
|
|
from dtoc.fdt import Type, BytesToValue
|
2018-07-06 16:27:24 +00:00
|
|
|
import libfdt
|
2020-04-18 00:09:04 +00:00
|
|
|
from patman import command
|
|
|
|
from patman import test_util
|
|
|
|
from patman import tools
|
2018-07-06 16:27:24 +00:00
|
|
|
|
2018-07-06 16:27:29 +00:00
|
|
|
def _GetPropertyValue(dtb, node, prop_name):
|
|
|
|
"""Low-level function to get the property value based on its offset
|
|
|
|
|
|
|
|
This looks directly in the device tree at the property's offset to find
|
|
|
|
its value. It is useful as a check that the property is in the correct
|
|
|
|
place.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
node: Node to look in
|
|
|
|
prop_name: Property name to find
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple:
|
|
|
|
Prop object found
|
|
|
|
Value of property as a string (found using property offset)
|
|
|
|
"""
|
|
|
|
prop = node.props[prop_name]
|
|
|
|
|
|
|
|
# Add 12, which is sizeof(struct fdt_property), to get to start of data
|
|
|
|
offset = prop.GetOffset() + 12
|
|
|
|
data = dtb.GetContents()[offset:offset + len(prop.value)]
|
2020-11-09 03:36:20 +00:00
|
|
|
return prop, [chr(x) for x in data]
|
2018-07-06 16:27:29 +00:00
|
|
|
|
2021-02-03 13:00:56 +00:00
|
|
|
def find_dtb_file(dts_fname):
|
|
|
|
"""Locate a test file in the test/ directory
|
|
|
|
|
|
|
|
Args:
|
|
|
|
dts_fname (str): Filename to find, e.g. 'dtoc_test_simple.dts]
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: Path to the test filename
|
|
|
|
"""
|
|
|
|
return os.path.join('tools/dtoc/test', dts_fname)
|
|
|
|
|
2018-07-06 16:27:29 +00:00
|
|
|
|
2018-07-06 16:27:24 +00:00
|
|
|
class TestFdt(unittest.TestCase):
|
|
|
|
"""Tests for the Fdt module
|
|
|
|
|
|
|
|
This includes unit tests for some functions and functional tests for the fdt
|
|
|
|
module.
|
|
|
|
"""
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.prepare_output_dir(None)
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.finalise_output_dir()
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-02-03 13:00:56 +00:00
|
|
|
self.dtb = fdt.FdtScan(find_dtb_file('dtoc_test_simple.dts'))
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
def testFdt(self):
|
|
|
|
"""Test that we can open an Fdt"""
|
|
|
|
self.dtb.Scan()
|
|
|
|
root = self.dtb.GetRoot()
|
|
|
|
self.assertTrue(isinstance(root, fdt.Node))
|
|
|
|
|
|
|
|
def testGetNode(self):
|
|
|
|
"""Test the GetNode() method"""
|
|
|
|
node = self.dtb.GetNode('/spl-test')
|
|
|
|
self.assertTrue(isinstance(node, fdt.Node))
|
2019-07-20 18:23:39 +00:00
|
|
|
|
2018-07-06 16:27:24 +00:00
|
|
|
node = self.dtb.GetNode('/i2c@0/pmic@9')
|
|
|
|
self.assertTrue(isinstance(node, fdt.Node))
|
|
|
|
self.assertEqual('pmic@9', node.name)
|
2018-07-06 16:27:28 +00:00
|
|
|
self.assertIsNone(self.dtb.GetNode('/i2c@0/pmic@9/missing'))
|
2018-07-06 16:27:24 +00:00
|
|
|
|
2019-07-20 18:23:39 +00:00
|
|
|
node = self.dtb.GetNode('/')
|
|
|
|
self.assertTrue(isinstance(node, fdt.Node))
|
|
|
|
self.assertEqual(0, node.Offset())
|
|
|
|
|
2018-07-06 16:27:24 +00:00
|
|
|
def testFlush(self):
|
|
|
|
"""Check that we can flush the device tree out to its file"""
|
|
|
|
fname = self.dtb._fname
|
2019-05-18 04:00:39 +00:00
|
|
|
with open(fname, 'rb') as fd:
|
2018-07-06 16:27:24 +00:00
|
|
|
data = fd.read()
|
|
|
|
os.remove(fname)
|
|
|
|
with self.assertRaises(IOError):
|
2019-05-18 04:00:39 +00:00
|
|
|
open(fname, 'rb')
|
2018-07-06 16:27:24 +00:00
|
|
|
self.dtb.Flush()
|
2019-05-18 04:00:39 +00:00
|
|
|
with open(fname, 'rb') as fd:
|
2018-07-06 16:27:24 +00:00
|
|
|
data = fd.read()
|
|
|
|
|
|
|
|
def testPack(self):
|
|
|
|
"""Test that packing a device tree works"""
|
|
|
|
self.dtb.Pack()
|
|
|
|
|
2022-02-11 20:23:20 +00:00
|
|
|
def testGetFdtRaw(self):
|
2018-07-06 16:27:24 +00:00
|
|
|
"""Tetst that we can access the raw device-tree data"""
|
2022-02-11 20:23:20 +00:00
|
|
|
self.assertTrue(isinstance(self.dtb.GetContents(), bytes))
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
def testGetProps(self):
|
|
|
|
"""Tests obtaining a list of properties"""
|
|
|
|
node = self.dtb.GetNode('/spl-test')
|
|
|
|
props = self.dtb.GetProps(node)
|
|
|
|
self.assertEqual(['boolval', 'bytearray', 'byteval', 'compatible',
|
2021-11-23 18:03:39 +00:00
|
|
|
'int64val', 'intarray', 'intval', 'longbytearray',
|
2021-07-29 01:23:11 +00:00
|
|
|
'maybe-empty-int', 'notstring', 'stringarray',
|
|
|
|
'stringval', 'u-boot,dm-pre-reloc'],
|
2018-07-06 16:27:24 +00:00
|
|
|
sorted(props.keys()))
|
|
|
|
|
|
|
|
def testCheckError(self):
|
|
|
|
"""Tests the ChecKError() function"""
|
|
|
|
with self.assertRaises(ValueError) as e:
|
2018-07-06 16:27:28 +00:00
|
|
|
fdt.CheckErr(-libfdt.NOTFOUND, 'hello')
|
2018-07-06 16:27:24 +00:00
|
|
|
self.assertIn('FDT_ERR_NOTFOUND: hello', str(e.exception))
|
|
|
|
|
2018-07-17 19:25:46 +00:00
|
|
|
def testGetFdt(self):
|
|
|
|
node = self.dtb.GetNode('/spl-test')
|
|
|
|
self.assertEqual(self.dtb, node.GetFdt())
|
2018-07-06 16:27:24 +00:00
|
|
|
|
2019-05-18 04:00:41 +00:00
|
|
|
def testBytesToValue(self):
|
|
|
|
self.assertEqual(BytesToValue(b'this\0is\0'),
|
2020-11-09 03:36:17 +00:00
|
|
|
(Type.STRING, ['this', 'is']))
|
2019-05-18 04:00:41 +00:00
|
|
|
|
2018-07-06 16:27:24 +00:00
|
|
|
class TestNode(unittest.TestCase):
|
|
|
|
"""Test operation of the Node class"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.prepare_output_dir(None)
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.finalise_output_dir()
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-02-03 13:00:56 +00:00
|
|
|
self.dtb = fdt.FdtScan(find_dtb_file('dtoc_test_simple.dts'))
|
2018-07-06 16:27:24 +00:00
|
|
|
self.node = self.dtb.GetNode('/spl-test')
|
2021-03-21 05:24:37 +00:00
|
|
|
self.fdt = self.dtb.GetFdtObj()
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
def testOffset(self):
|
|
|
|
"""Tests that we can obtain the offset of a node"""
|
|
|
|
self.assertTrue(self.node.Offset() > 0)
|
|
|
|
|
|
|
|
def testDelete(self):
|
|
|
|
"""Tests that we can delete a property"""
|
|
|
|
node2 = self.dtb.GetNode('/spl-test2')
|
|
|
|
offset1 = node2.Offset()
|
|
|
|
self.node.DeleteProp('intval')
|
|
|
|
offset2 = node2.Offset()
|
|
|
|
self.assertTrue(offset2 < offset1)
|
|
|
|
self.node.DeleteProp('intarray')
|
|
|
|
offset3 = node2.Offset()
|
|
|
|
self.assertTrue(offset3 < offset2)
|
2018-07-06 16:27:28 +00:00
|
|
|
with self.assertRaises(libfdt.FdtException):
|
|
|
|
self.node.DeleteProp('missing')
|
2018-07-06 16:27:24 +00:00
|
|
|
|
2018-07-06 16:27:29 +00:00
|
|
|
def testDeleteGetOffset(self):
|
|
|
|
"""Test that property offset update when properties are deleted"""
|
|
|
|
self.node.DeleteProp('intval')
|
|
|
|
prop, value = _GetPropertyValue(self.dtb, self.node, 'longbytearray')
|
|
|
|
self.assertEqual(prop.value, value)
|
|
|
|
|
2018-07-06 16:27:24 +00:00
|
|
|
def testFindNode(self):
|
2018-07-17 19:25:41 +00:00
|
|
|
"""Tests that we can find a node using the FindNode() functoin"""
|
|
|
|
node = self.dtb.GetRoot().FindNode('i2c@0')
|
2018-07-06 16:27:24 +00:00
|
|
|
self.assertEqual('i2c@0', node.name)
|
2018-07-17 19:25:41 +00:00
|
|
|
subnode = node.FindNode('pmic@9')
|
2018-07-06 16:27:24 +00:00
|
|
|
self.assertEqual('pmic@9', subnode.name)
|
2018-07-17 19:25:41 +00:00
|
|
|
self.assertEqual(None, node.FindNode('missing'))
|
2018-07-06 16:27:24 +00:00
|
|
|
|
2018-07-06 16:27:29 +00:00
|
|
|
def testRefreshMissingNode(self):
|
|
|
|
"""Test refreshing offsets when an extra node is present in dtb"""
|
|
|
|
# Delete it from our tables, not the device tree
|
|
|
|
del self.dtb._root.subnodes[-1]
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.dtb.Refresh()
|
|
|
|
self.assertIn('Internal error, offset', str(e.exception))
|
|
|
|
|
|
|
|
def testRefreshExtraNode(self):
|
|
|
|
"""Test refreshing offsets when an expected node is missing"""
|
|
|
|
# Delete it from the device tre, not our tables
|
2021-03-21 05:24:37 +00:00
|
|
|
self.fdt.del_node(self.node.Offset())
|
2018-07-06 16:27:29 +00:00
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.dtb.Refresh()
|
|
|
|
self.assertIn('Internal error, node name mismatch '
|
|
|
|
'spl-test != spl-test2', str(e.exception))
|
|
|
|
|
|
|
|
def testRefreshMissingProp(self):
|
|
|
|
"""Test refreshing offsets when an extra property is present in dtb"""
|
|
|
|
# Delete it from our tables, not the device tree
|
|
|
|
del self.node.props['notstring']
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.dtb.Refresh()
|
2021-03-21 05:24:34 +00:00
|
|
|
self.assertIn("Internal error, node '/spl-test' property 'notstring' missing, offset ",
|
2018-07-06 16:27:29 +00:00
|
|
|
str(e.exception))
|
|
|
|
|
2018-07-17 19:25:46 +00:00
|
|
|
def testLookupPhandle(self):
|
|
|
|
"""Test looking up a single phandle"""
|
2021-02-03 13:00:56 +00:00
|
|
|
dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts'))
|
2018-07-17 19:25:46 +00:00
|
|
|
node = dtb.GetNode('/phandle-source2')
|
|
|
|
prop = node.props['clocks']
|
|
|
|
target = dtb.GetNode('/phandle-target')
|
|
|
|
self.assertEqual(target, dtb.LookupPhandle(fdt32_to_cpu(prop.value)))
|
|
|
|
|
2021-03-21 05:24:37 +00:00
|
|
|
def testAddNodeSpace(self):
|
|
|
|
"""Test adding a single node when out of space"""
|
|
|
|
self.fdt.pack()
|
|
|
|
self.node.AddSubnode('subnode')
|
|
|
|
with self.assertRaises(libfdt.FdtException) as e:
|
|
|
|
self.dtb.Sync(auto_resize=False)
|
|
|
|
self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
|
|
|
|
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
offset = self.fdt.path_offset('/spl-test/subnode')
|
|
|
|
self.assertTrue(offset > 0)
|
|
|
|
|
|
|
|
def testAddNodes(self):
|
|
|
|
"""Test adding various subnode and properies"""
|
|
|
|
node = self.dtb.GetNode('/i2c@0')
|
|
|
|
|
2021-03-21 05:24:38 +00:00
|
|
|
# Add one more node next to the pmic one
|
|
|
|
sn1 = node.AddSubnode('node-one')
|
|
|
|
sn1.AddInt('integer-a', 12)
|
|
|
|
sn1.AddInt('integer-b', 23)
|
|
|
|
|
|
|
|
# Sync so that everything is clean
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
|
|
|
|
# Add two subnodes next to pmic and node-one
|
|
|
|
sn2 = node.AddSubnode('node-two')
|
|
|
|
sn2.AddInt('integer-2a', 34)
|
|
|
|
sn2.AddInt('integer-2b', 45)
|
|
|
|
|
|
|
|
sn3 = node.AddSubnode('node-three')
|
|
|
|
sn3.AddInt('integer-3', 123)
|
|
|
|
|
2021-03-21 05:24:37 +00:00
|
|
|
# Add a property to the node after i2c@0 to check that this is not
|
|
|
|
# disturbed by adding a subnode to i2c@0
|
|
|
|
orig_node = self.dtb.GetNode('/orig-node')
|
|
|
|
orig_node.AddInt('integer-4', 456)
|
|
|
|
|
|
|
|
# Add a property to the pmic node to check that pmic properties are not
|
|
|
|
# disturbed
|
|
|
|
pmic = self.dtb.GetNode('/i2c@0/pmic@9')
|
|
|
|
pmic.AddInt('integer-5', 567)
|
|
|
|
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
|
2022-02-08 18:49:52 +00:00
|
|
|
def testAddOneNode(self):
|
|
|
|
"""Testing deleting and adding a subnode before syncing"""
|
|
|
|
subnode = self.node.AddSubnode('subnode')
|
|
|
|
self.node.AddSubnode('subnode2')
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
|
|
|
|
# Delete a node and add a new one
|
|
|
|
subnode.Delete()
|
|
|
|
self.node.AddSubnode('subnode3')
|
|
|
|
self.dtb.Sync()
|
|
|
|
|
2021-03-21 05:24:39 +00:00
|
|
|
def testRefreshNameMismatch(self):
|
|
|
|
"""Test name mismatch when syncing nodes and properties"""
|
|
|
|
prop = self.node.AddInt('integer-a', 12)
|
|
|
|
|
|
|
|
wrong_offset = self.dtb.GetNode('/i2c@0')._offset
|
|
|
|
self.node._offset = wrong_offset
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.dtb.Sync()
|
|
|
|
self.assertIn("Internal error, node '/spl-test' name mismatch 'i2c@0'",
|
|
|
|
str(e.exception))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.node.Refresh(wrong_offset)
|
|
|
|
self.assertIn("Internal error, node '/spl-test' name mismatch 'i2c@0'",
|
|
|
|
str(e.exception))
|
|
|
|
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
class TestProp(unittest.TestCase):
|
|
|
|
"""Test operation of the Prop class"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.prepare_output_dir(None)
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.finalise_output_dir()
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-02-03 13:00:56 +00:00
|
|
|
self.dtb = fdt.FdtScan(find_dtb_file('dtoc_test_simple.dts'))
|
2018-07-06 16:27:24 +00:00
|
|
|
self.node = self.dtb.GetNode('/spl-test')
|
|
|
|
self.fdt = self.dtb.GetFdtObj()
|
|
|
|
|
2018-07-06 16:27:30 +00:00
|
|
|
def testMissingNode(self):
|
|
|
|
self.assertEqual(None, self.dtb.GetNode('missing'))
|
|
|
|
|
2018-07-06 16:27:28 +00:00
|
|
|
def testPhandle(self):
|
2021-02-03 13:00:56 +00:00
|
|
|
dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts'))
|
2018-07-06 16:27:31 +00:00
|
|
|
node = dtb.GetNode('/phandle-source2')
|
|
|
|
prop = node.props['clocks']
|
|
|
|
self.assertTrue(fdt32_to_cpu(prop.value) > 0)
|
2018-07-06 16:27:28 +00:00
|
|
|
|
|
|
|
def _ConvertProp(self, prop_name):
|
|
|
|
"""Helper function to look up a property in self.node and return it
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Property name to find
|
|
|
|
|
|
|
|
Return fdt.Prop object for this property
|
|
|
|
"""
|
2018-07-26 20:02:13 +00:00
|
|
|
p = self.fdt.getprop(self.node.Offset(), prop_name)
|
2018-07-06 16:27:28 +00:00
|
|
|
return fdt.Prop(self.node, -1, prop_name, p)
|
|
|
|
|
|
|
|
def testMakeProp(self):
|
|
|
|
"""Test we can convert all the the types that are supported"""
|
|
|
|
prop = self._ConvertProp('boolval')
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(Type.BOOL, prop.type)
|
2018-07-06 16:27:28 +00:00
|
|
|
self.assertEqual(True, prop.value)
|
|
|
|
|
|
|
|
prop = self._ConvertProp('intval')
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(Type.INT, prop.type)
|
2018-07-06 16:27:28 +00:00
|
|
|
self.assertEqual(1, fdt32_to_cpu(prop.value))
|
|
|
|
|
2021-11-23 18:03:39 +00:00
|
|
|
prop = self._ConvertProp('int64val')
|
|
|
|
self.assertEqual(Type.INT, prop.type)
|
|
|
|
self.assertEqual(0x123456789abcdef0, fdt64_to_cpu(prop.value))
|
|
|
|
|
2018-07-06 16:27:28 +00:00
|
|
|
prop = self._ConvertProp('intarray')
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(Type.INT, prop.type)
|
2018-07-06 16:27:28 +00:00
|
|
|
val = [fdt32_to_cpu(val) for val in prop.value]
|
|
|
|
self.assertEqual([2, 3, 4], val)
|
|
|
|
|
|
|
|
prop = self._ConvertProp('byteval')
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(Type.BYTE, prop.type)
|
2018-07-06 16:27:28 +00:00
|
|
|
self.assertEqual(5, ord(prop.value))
|
|
|
|
|
|
|
|
prop = self._ConvertProp('longbytearray')
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(Type.BYTE, prop.type)
|
2018-07-06 16:27:28 +00:00
|
|
|
val = [ord(val) for val in prop.value]
|
|
|
|
self.assertEqual([9, 10, 11, 12, 13, 14, 15, 16, 17], val)
|
|
|
|
|
|
|
|
prop = self._ConvertProp('stringval')
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(Type.STRING, prop.type)
|
2018-07-06 16:27:28 +00:00
|
|
|
self.assertEqual('message', prop.value)
|
|
|
|
|
|
|
|
prop = self._ConvertProp('stringarray')
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(Type.STRING, prop.type)
|
2018-07-06 16:27:28 +00:00
|
|
|
self.assertEqual(['multi-word', 'message'], prop.value)
|
|
|
|
|
|
|
|
prop = self._ConvertProp('notstring')
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(Type.BYTE, prop.type)
|
2018-07-06 16:27:28 +00:00
|
|
|
val = [ord(val) for val in prop.value]
|
|
|
|
self.assertEqual([0x20, 0x21, 0x22, 0x10, 0], val)
|
|
|
|
|
2018-07-06 16:27:24 +00:00
|
|
|
def testGetEmpty(self):
|
|
|
|
"""Tests the GetEmpty() function for the various supported types"""
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(True, fdt.Prop.GetEmpty(Type.BOOL))
|
|
|
|
self.assertEqual(chr(0), fdt.Prop.GetEmpty(Type.BYTE))
|
2022-01-29 21:14:04 +00:00
|
|
|
self.assertEqual(tools.get_bytes(0, 4), fdt.Prop.GetEmpty(Type.INT))
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual('', fdt.Prop.GetEmpty(Type.STRING))
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
def testGetOffset(self):
|
|
|
|
"""Test we can get the offset of a property"""
|
2018-07-06 16:27:29 +00:00
|
|
|
prop, value = _GetPropertyValue(self.dtb, self.node, 'longbytearray')
|
|
|
|
self.assertEqual(prop.value, value)
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
def testWiden(self):
|
|
|
|
"""Test widening of values"""
|
|
|
|
node2 = self.dtb.GetNode('/spl-test2')
|
2020-10-03 17:31:27 +00:00
|
|
|
node3 = self.dtb.GetNode('/spl-test3')
|
2018-07-06 16:27:24 +00:00
|
|
|
prop = self.node.props['intval']
|
|
|
|
|
|
|
|
# No action
|
|
|
|
prop2 = node2.props['intval']
|
|
|
|
prop.Widen(prop2)
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(Type.INT, prop.type)
|
2018-07-06 16:27:24 +00:00
|
|
|
self.assertEqual(1, fdt32_to_cpu(prop.value))
|
|
|
|
|
2021-07-29 01:23:10 +00:00
|
|
|
# Convert single value to array
|
2018-07-06 16:27:24 +00:00
|
|
|
prop2 = self.node.props['intarray']
|
|
|
|
prop.Widen(prop2)
|
2020-11-09 03:36:17 +00:00
|
|
|
self.assertEqual(Type.INT, prop.type)
|
2018-07-06 16:27:24 +00:00
|
|
|
self.assertTrue(isinstance(prop.value, list))
|
|
|
|
|
|
|
|
# A 4-byte array looks like a single integer. When widened by a longer
|
|
|
|
# byte array, it should turn into an array.
|
|
|
|
prop = self.node.props['longbytearray']
|
|
|
|
prop2 = node2.props['longbytearray']
|
2020-10-03 17:31:27 +00:00
|
|
|
prop3 = node3.props['longbytearray']
|
2018-07-06 16:27:24 +00:00
|
|
|
self.assertFalse(isinstance(prop2.value, list))
|
|
|
|
self.assertEqual(4, len(prop2.value))
|
2020-10-03 17:31:27 +00:00
|
|
|
self.assertEqual(b'\x09\x0a\x0b\x0c', prop2.value)
|
2018-07-06 16:27:24 +00:00
|
|
|
prop2.Widen(prop)
|
|
|
|
self.assertTrue(isinstance(prop2.value, list))
|
|
|
|
self.assertEqual(9, len(prop2.value))
|
2020-10-03 17:31:27 +00:00
|
|
|
self.assertEqual(['\x09', '\x0a', '\x0b', '\x0c', '\0',
|
|
|
|
'\0', '\0', '\0', '\0'], prop2.value)
|
|
|
|
prop3.Widen(prop)
|
|
|
|
self.assertTrue(isinstance(prop3.value, list))
|
|
|
|
self.assertEqual(9, len(prop3.value))
|
|
|
|
self.assertEqual(['\x09', '\x0a', '\x0b', '\x0c', '\x0d',
|
|
|
|
'\x0e', '\x0f', '\x10', '\0'], prop3.value)
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
# Similarly for a string array
|
|
|
|
prop = self.node.props['stringval']
|
|
|
|
prop2 = node2.props['stringarray']
|
|
|
|
self.assertFalse(isinstance(prop.value, list))
|
|
|
|
self.assertEqual(7, len(prop.value))
|
|
|
|
prop.Widen(prop2)
|
|
|
|
self.assertTrue(isinstance(prop.value, list))
|
|
|
|
self.assertEqual(3, len(prop.value))
|
|
|
|
|
|
|
|
# Enlarging an existing array
|
|
|
|
prop = self.node.props['stringarray']
|
|
|
|
prop2 = node2.props['stringarray']
|
|
|
|
self.assertTrue(isinstance(prop.value, list))
|
|
|
|
self.assertEqual(2, len(prop.value))
|
|
|
|
prop.Widen(prop2)
|
|
|
|
self.assertTrue(isinstance(prop.value, list))
|
|
|
|
self.assertEqual(3, len(prop.value))
|
|
|
|
|
2021-07-29 01:23:10 +00:00
|
|
|
# Widen an array of ints with an int (should do nothing)
|
|
|
|
prop = self.node.props['intarray']
|
2021-08-02 13:37:54 +00:00
|
|
|
prop2 = node2.props['intval']
|
2021-07-29 01:23:10 +00:00
|
|
|
self.assertEqual(Type.INT, prop.type)
|
|
|
|
self.assertEqual(3, len(prop.value))
|
|
|
|
prop.Widen(prop2)
|
|
|
|
self.assertEqual(Type.INT, prop.type)
|
|
|
|
self.assertEqual(3, len(prop.value))
|
|
|
|
|
2021-07-29 01:23:11 +00:00
|
|
|
# Widen an empty bool to an int
|
|
|
|
prop = self.node.props['maybe-empty-int']
|
|
|
|
prop3 = node3.props['maybe-empty-int']
|
|
|
|
self.assertEqual(Type.BOOL, prop.type)
|
|
|
|
self.assertEqual(True, prop.value)
|
|
|
|
self.assertEqual(Type.INT, prop3.type)
|
|
|
|
self.assertFalse(isinstance(prop.value, list))
|
|
|
|
self.assertEqual(4, len(prop3.value))
|
|
|
|
prop.Widen(prop3)
|
|
|
|
self.assertEqual(Type.INT, prop.type)
|
|
|
|
self.assertTrue(isinstance(prop.value, list))
|
|
|
|
self.assertEqual(1, len(prop.value))
|
|
|
|
|
2018-07-06 16:27:38 +00:00
|
|
|
def testAdd(self):
|
|
|
|
"""Test adding properties"""
|
|
|
|
self.fdt.pack()
|
|
|
|
# This function should automatically expand the device tree
|
|
|
|
self.node.AddZeroProp('one')
|
|
|
|
self.node.AddZeroProp('two')
|
|
|
|
self.node.AddZeroProp('three')
|
2018-09-14 10:57:13 +00:00
|
|
|
self.dtb.Sync(auto_resize=True)
|
2018-07-06 16:27:38 +00:00
|
|
|
|
|
|
|
# Updating existing properties should be OK, since the device-tree size
|
|
|
|
# does not change
|
|
|
|
self.fdt.pack()
|
|
|
|
self.node.SetInt('one', 1)
|
|
|
|
self.node.SetInt('two', 2)
|
|
|
|
self.node.SetInt('three', 3)
|
2018-09-14 10:57:13 +00:00
|
|
|
self.dtb.Sync(auto_resize=False)
|
2018-07-06 16:27:38 +00:00
|
|
|
|
|
|
|
# This should fail since it would need to increase the device-tree size
|
2018-09-14 10:57:13 +00:00
|
|
|
self.node.AddZeroProp('four')
|
2018-07-06 16:27:38 +00:00
|
|
|
with self.assertRaises(libfdt.FdtException) as e:
|
2018-09-14 10:57:13 +00:00
|
|
|
self.dtb.Sync(auto_resize=False)
|
2018-07-06 16:27:38 +00:00
|
|
|
self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
|
2018-09-14 10:57:16 +00:00
|
|
|
self.dtb.Sync(auto_resize=True)
|
2018-07-06 16:27:38 +00:00
|
|
|
|
2018-09-14 10:57:16 +00:00
|
|
|
def testAddMore(self):
|
|
|
|
"""Test various other methods for adding and setting properties"""
|
|
|
|
self.node.AddZeroProp('one')
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
data = self.fdt.getprop(self.node.Offset(), 'one')
|
|
|
|
self.assertEqual(0, fdt32_to_cpu(data))
|
|
|
|
|
|
|
|
self.node.SetInt('one', 1)
|
|
|
|
self.dtb.Sync(auto_resize=False)
|
|
|
|
data = self.fdt.getprop(self.node.Offset(), 'one')
|
|
|
|
self.assertEqual(1, fdt32_to_cpu(data))
|
|
|
|
|
2021-01-07 04:35:18 +00:00
|
|
|
val = 1234
|
|
|
|
self.node.AddInt('integer', val)
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
data = self.fdt.getprop(self.node.Offset(), 'integer')
|
|
|
|
self.assertEqual(val, fdt32_to_cpu(data))
|
|
|
|
|
2018-09-14 10:57:16 +00:00
|
|
|
val = '123' + chr(0) + '456'
|
|
|
|
self.node.AddString('string', val)
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
data = self.fdt.getprop(self.node.Offset(), 'string')
|
2022-01-29 21:14:04 +00:00
|
|
|
self.assertEqual(tools.to_bytes(val) + b'\0', data)
|
2018-09-14 10:57:16 +00:00
|
|
|
|
|
|
|
self.fdt.pack()
|
|
|
|
self.node.SetString('string', val + 'x')
|
|
|
|
with self.assertRaises(libfdt.FdtException) as e:
|
|
|
|
self.dtb.Sync(auto_resize=False)
|
|
|
|
self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
|
|
|
|
self.node.SetString('string', val[:-1])
|
|
|
|
|
|
|
|
prop = self.node.props['string']
|
2022-01-29 21:14:04 +00:00
|
|
|
prop.SetData(tools.to_bytes(val))
|
2018-09-14 10:57:16 +00:00
|
|
|
self.dtb.Sync(auto_resize=False)
|
|
|
|
data = self.fdt.getprop(self.node.Offset(), 'string')
|
2022-01-29 21:14:04 +00:00
|
|
|
self.assertEqual(tools.to_bytes(val), data)
|
2018-09-14 10:57:16 +00:00
|
|
|
|
|
|
|
self.node.AddEmptyProp('empty', 5)
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
prop = self.node.props['empty']
|
2022-01-29 21:14:04 +00:00
|
|
|
prop.SetData(tools.to_bytes(val))
|
2018-09-14 10:57:16 +00:00
|
|
|
self.dtb.Sync(auto_resize=False)
|
|
|
|
data = self.fdt.getprop(self.node.Offset(), 'empty')
|
2022-01-29 21:14:04 +00:00
|
|
|
self.assertEqual(tools.to_bytes(val), data)
|
2018-09-14 10:57:16 +00:00
|
|
|
|
2019-05-18 04:00:36 +00:00
|
|
|
self.node.SetData('empty', b'123')
|
|
|
|
self.assertEqual(b'123', prop.bytes)
|
2018-09-14 10:57:16 +00:00
|
|
|
|
2020-07-10 00:39:44 +00:00
|
|
|
# Trying adding a lot of data at once
|
2022-01-29 21:14:04 +00:00
|
|
|
self.node.AddData('data', tools.get_bytes(65, 20000))
|
2020-07-10 00:39:44 +00:00
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
|
2022-02-08 18:49:50 +00:00
|
|
|
def test_string_list(self):
|
|
|
|
"""Test adding string-list property to a node"""
|
|
|
|
val = ['123', '456']
|
|
|
|
self.node.AddStringList('stringlist', val)
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
data = self.fdt.getprop(self.node.Offset(), 'stringlist')
|
|
|
|
self.assertEqual(b'123\x00456\0', data)
|
|
|
|
|
2022-03-06 03:18:56 +00:00
|
|
|
val = []
|
|
|
|
self.node.AddStringList('stringlist', val)
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
data = self.fdt.getprop(self.node.Offset(), 'stringlist')
|
|
|
|
self.assertEqual(b'', data)
|
|
|
|
|
2022-02-08 18:49:51 +00:00
|
|
|
def test_delete_node(self):
|
|
|
|
"""Test deleting a node"""
|
|
|
|
old_offset = self.fdt.path_offset('/spl-test')
|
|
|
|
self.assertGreater(old_offset, 0)
|
|
|
|
self.node.Delete()
|
|
|
|
self.dtb.Sync()
|
|
|
|
new_offset = self.fdt.path_offset('/spl-test', libfdt.QUIET_NOTFOUND)
|
|
|
|
self.assertEqual(-libfdt.NOTFOUND, new_offset)
|
|
|
|
|
2018-09-14 10:57:17 +00:00
|
|
|
def testFromData(self):
|
|
|
|
dtb2 = fdt.Fdt.FromData(self.dtb.GetContents())
|
|
|
|
self.assertEqual(dtb2.GetContents(), self.dtb.GetContents())
|
|
|
|
|
|
|
|
self.node.AddEmptyProp('empty', 5)
|
|
|
|
self.dtb.Sync(auto_resize=True)
|
|
|
|
self.assertTrue(dtb2.GetContents() != self.dtb.GetContents())
|
|
|
|
|
2019-07-20 18:23:37 +00:00
|
|
|
def testMissingSetInt(self):
|
|
|
|
"""Test handling of a missing property with SetInt"""
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.node.SetInt('one', 1)
|
|
|
|
self.assertIn("node '/spl-test': Missing property 'one'",
|
|
|
|
str(e.exception))
|
|
|
|
|
|
|
|
def testMissingSetData(self):
|
|
|
|
"""Test handling of a missing property with SetData"""
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.node.SetData('one', b'data')
|
|
|
|
self.assertIn("node '/spl-test': Missing property 'one'",
|
|
|
|
str(e.exception))
|
|
|
|
|
|
|
|
def testMissingSetString(self):
|
|
|
|
"""Test handling of a missing property with SetString"""
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.node.SetString('one', 1)
|
|
|
|
self.assertIn("node '/spl-test': Missing property 'one'",
|
|
|
|
str(e.exception))
|
|
|
|
|
2019-07-20 18:24:08 +00:00
|
|
|
def testGetFilename(self):
|
|
|
|
"""Test the dtb filename can be provided"""
|
2022-01-29 21:14:04 +00:00
|
|
|
self.assertEqual(tools.get_output_filename('source.dtb'),
|
2019-07-20 18:24:08 +00:00
|
|
|
self.dtb.GetFilename())
|
|
|
|
|
2018-07-06 16:27:24 +00:00
|
|
|
|
2018-07-06 16:27:28 +00:00
|
|
|
class TestFdtUtil(unittest.TestCase):
|
|
|
|
"""Tests for the fdt_util module
|
|
|
|
|
|
|
|
This module will likely be mostly replaced at some point, once upstream
|
|
|
|
libfdt has better Python support. For now, this provides tests for current
|
|
|
|
functionality.
|
|
|
|
"""
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.prepare_output_dir(None)
|
2018-07-06 16:27:28 +00:00
|
|
|
|
2018-10-02 03:12:41 +00:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.finalise_output_dir()
|
2018-10-02 03:12:41 +00:00
|
|
|
|
2018-07-06 16:27:28 +00:00
|
|
|
def setUp(self):
|
2021-02-03 13:00:56 +00:00
|
|
|
self.dtb = fdt.FdtScan(find_dtb_file('dtoc_test_simple.dts'))
|
2018-07-06 16:27:28 +00:00
|
|
|
self.node = self.dtb.GetNode('/spl-test')
|
|
|
|
|
|
|
|
def testGetInt(self):
|
|
|
|
self.assertEqual(1, fdt_util.GetInt(self.node, 'intval'))
|
|
|
|
self.assertEqual(3, fdt_util.GetInt(self.node, 'missing', 3))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as e:
|
2021-11-23 18:03:39 +00:00
|
|
|
fdt_util.GetInt(self.node, 'intarray')
|
2018-07-06 16:27:28 +00:00
|
|
|
self.assertIn("property 'intarray' has list value: expecting a single "
|
|
|
|
'integer', str(e.exception))
|
|
|
|
|
2021-11-23 18:03:39 +00:00
|
|
|
def testGetInt64(self):
|
|
|
|
self.assertEqual(0x123456789abcdef0,
|
|
|
|
fdt_util.GetInt64(self.node, 'int64val'))
|
|
|
|
self.assertEqual(3, fdt_util.GetInt64(self.node, 'missing', 3))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
fdt_util.GetInt64(self.node, 'intarray')
|
|
|
|
self.assertIn(
|
|
|
|
"property 'intarray' should be a list with 2 items for 64-bit values",
|
|
|
|
str(e.exception))
|
|
|
|
|
2018-07-06 16:27:28 +00:00
|
|
|
def testGetString(self):
|
|
|
|
self.assertEqual('message', fdt_util.GetString(self.node, 'stringval'))
|
|
|
|
self.assertEqual('test', fdt_util.GetString(self.node, 'missing',
|
|
|
|
'test'))
|
2022-03-06 03:18:56 +00:00
|
|
|
self.assertEqual('', fdt_util.GetString(self.node, 'boolval'))
|
2018-07-06 16:27:28 +00:00
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.assertEqual(3, fdt_util.GetString(self.node, 'stringarray'))
|
|
|
|
self.assertIn("property 'stringarray' has list value: expecting a "
|
|
|
|
'single string', str(e.exception))
|
|
|
|
|
2021-11-24 04:09:51 +00:00
|
|
|
def testGetStringList(self):
|
|
|
|
self.assertEqual(['message'],
|
|
|
|
fdt_util.GetStringList(self.node, 'stringval'))
|
|
|
|
self.assertEqual(
|
|
|
|
['multi-word', 'message'],
|
|
|
|
fdt_util.GetStringList(self.node, 'stringarray'))
|
|
|
|
self.assertEqual(['test'],
|
|
|
|
fdt_util.GetStringList(self.node, 'missing', ['test']))
|
2022-03-06 03:18:56 +00:00
|
|
|
self.assertEqual([], fdt_util.GetStringList(self.node, 'boolval'))
|
2021-11-24 04:09:51 +00:00
|
|
|
|
2022-02-08 18:49:53 +00:00
|
|
|
def testGetArgs(self):
|
|
|
|
node = self.dtb.GetNode('/orig-node')
|
|
|
|
self.assertEqual(['message'], fdt_util.GetArgs(self.node, 'stringval'))
|
|
|
|
self.assertEqual(
|
|
|
|
['multi-word', 'message'],
|
|
|
|
fdt_util.GetArgs(self.node, 'stringarray'))
|
|
|
|
self.assertEqual([], fdt_util.GetArgs(self.node, 'boolval'))
|
2022-03-06 03:18:52 +00:00
|
|
|
self.assertEqual(['-n first', 'second', '-p', '123,456', '-x'],
|
2022-02-08 18:49:53 +00:00
|
|
|
fdt_util.GetArgs(node, 'args'))
|
2022-03-06 03:18:52 +00:00
|
|
|
self.assertEqual(['a space', 'there'],
|
|
|
|
fdt_util.GetArgs(node, 'args2'))
|
|
|
|
self.assertEqual(['-n', 'first', 'second', '-p', '123,456', '-x'],
|
|
|
|
fdt_util.GetArgs(node, 'args3'))
|
2022-02-08 18:49:53 +00:00
|
|
|
with self.assertRaises(ValueError) as exc:
|
|
|
|
fdt_util.GetArgs(self.node, 'missing')
|
|
|
|
self.assertIn(
|
|
|
|
"Node '/spl-test': Expected property 'missing'",
|
|
|
|
str(exc.exception))
|
|
|
|
|
2018-07-06 16:27:28 +00:00
|
|
|
def testGetBool(self):
|
|
|
|
self.assertEqual(True, fdt_util.GetBool(self.node, 'boolval'))
|
|
|
|
self.assertEqual(False, fdt_util.GetBool(self.node, 'missing'))
|
|
|
|
self.assertEqual(True, fdt_util.GetBool(self.node, 'missing', True))
|
|
|
|
self.assertEqual(False, fdt_util.GetBool(self.node, 'missing', False))
|
|
|
|
|
2018-07-17 19:25:40 +00:00
|
|
|
def testGetByte(self):
|
|
|
|
self.assertEqual(5, fdt_util.GetByte(self.node, 'byteval'))
|
|
|
|
self.assertEqual(3, fdt_util.GetByte(self.node, 'missing', 3))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
fdt_util.GetByte(self.node, 'longbytearray')
|
|
|
|
self.assertIn("property 'longbytearray' has list value: expecting a "
|
|
|
|
'single byte', str(e.exception))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
fdt_util.GetByte(self.node, 'intval')
|
|
|
|
self.assertIn("property 'intval' has length 4, expecting 1",
|
|
|
|
str(e.exception))
|
|
|
|
|
2021-11-23 18:03:40 +00:00
|
|
|
def testGetBytes(self):
|
|
|
|
self.assertEqual(bytes([5]), fdt_util.GetBytes(self.node, 'byteval', 1))
|
|
|
|
self.assertEqual(None, fdt_util.GetBytes(self.node, 'missing', 3))
|
|
|
|
self.assertEqual(
|
|
|
|
bytes([3]), fdt_util.GetBytes(self.node, 'missing', 3, bytes([3])))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
fdt_util.GetBytes(self.node, 'longbytearray', 7)
|
|
|
|
self.assertIn(
|
|
|
|
"Node 'spl-test' property 'longbytearray' has length 9, expecting 7",
|
|
|
|
str(e.exception))
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
bytes([0, 0, 0, 1]), fdt_util.GetBytes(self.node, 'intval', 4))
|
|
|
|
self.assertEqual(
|
|
|
|
bytes([3]), fdt_util.GetBytes(self.node, 'missing', 3, bytes([3])))
|
|
|
|
|
2018-07-17 19:25:46 +00:00
|
|
|
def testGetPhandleList(self):
|
2021-02-03 13:00:56 +00:00
|
|
|
dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts'))
|
2018-07-17 19:25:46 +00:00
|
|
|
node = dtb.GetNode('/phandle-source2')
|
|
|
|
self.assertEqual([1], fdt_util.GetPhandleList(node, 'clocks'))
|
|
|
|
node = dtb.GetNode('/phandle-source')
|
|
|
|
self.assertEqual([1, 2, 11, 3, 12, 13, 1],
|
|
|
|
fdt_util.GetPhandleList(node, 'clocks'))
|
|
|
|
self.assertEqual(None, fdt_util.GetPhandleList(node, 'missing'))
|
|
|
|
|
2018-07-17 19:25:32 +00:00
|
|
|
def testGetDataType(self):
|
|
|
|
self.assertEqual(1, fdt_util.GetDatatype(self.node, 'intval', int))
|
|
|
|
self.assertEqual('message', fdt_util.GetDatatype(self.node, 'stringval',
|
|
|
|
str))
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.assertEqual(3, fdt_util.GetDatatype(self.node, 'boolval',
|
|
|
|
bool))
|
2018-07-06 16:27:28 +00:00
|
|
|
def testFdtCellsToCpu(self):
|
|
|
|
val = self.node.props['intarray'].value
|
|
|
|
self.assertEqual(0, fdt_util.fdt_cells_to_cpu(val, 0))
|
|
|
|
self.assertEqual(2, fdt_util.fdt_cells_to_cpu(val, 1))
|
|
|
|
|
2021-02-03 13:00:56 +00:00
|
|
|
dtb2 = fdt.FdtScan(find_dtb_file('dtoc_test_addr64.dts'))
|
2019-05-18 04:00:40 +00:00
|
|
|
node1 = dtb2.GetNode('/test1')
|
|
|
|
val = node1.props['reg'].value
|
2018-07-06 16:27:28 +00:00
|
|
|
self.assertEqual(0x1234, fdt_util.fdt_cells_to_cpu(val, 2))
|
|
|
|
|
2019-05-18 04:00:40 +00:00
|
|
|
node2 = dtb2.GetNode('/test2')
|
|
|
|
val = node2.props['reg'].value
|
|
|
|
self.assertEqual(0x1234567890123456, fdt_util.fdt_cells_to_cpu(val, 2))
|
|
|
|
self.assertEqual(0x9876543210987654, fdt_util.fdt_cells_to_cpu(val[2:],
|
|
|
|
2))
|
|
|
|
self.assertEqual(0x12345678, fdt_util.fdt_cells_to_cpu(val, 1))
|
|
|
|
|
2018-07-06 16:27:28 +00:00
|
|
|
def testEnsureCompiled(self):
|
2019-07-20 18:23:49 +00:00
|
|
|
"""Test a degenerate case of this function (file already compiled)"""
|
2021-02-03 13:00:56 +00:00
|
|
|
dtb = fdt_util.EnsureCompiled(find_dtb_file('dtoc_test_simple.dts'))
|
2018-07-06 16:27:28 +00:00
|
|
|
self.assertEqual(dtb, fdt_util.EnsureCompiled(dtb))
|
|
|
|
|
2019-07-20 18:23:49 +00:00
|
|
|
def testEnsureCompiledTmpdir(self):
|
|
|
|
"""Test providing a temporary directory"""
|
|
|
|
try:
|
|
|
|
old_outdir = tools.outdir
|
|
|
|
tools.outdir= None
|
|
|
|
tmpdir = tempfile.mkdtemp(prefix='test_fdt.')
|
2021-02-03 13:00:56 +00:00
|
|
|
dtb = fdt_util.EnsureCompiled(find_dtb_file('dtoc_test_simple.dts'),
|
2019-07-20 18:23:49 +00:00
|
|
|
tmpdir)
|
|
|
|
self.assertEqual(tmpdir, os.path.dirname(dtb))
|
|
|
|
shutil.rmtree(tmpdir)
|
|
|
|
finally:
|
|
|
|
tools.outdir= old_outdir
|
|
|
|
|
2018-07-06 16:27:28 +00:00
|
|
|
|
2022-07-31 02:57:05 +00:00
|
|
|
def run_test_coverage(build_dir):
|
|
|
|
"""Run the tests and check that we get 100% coverage
|
|
|
|
|
|
|
|
Args:
|
|
|
|
build_dir (str): Directory containing the build output
|
|
|
|
"""
|
2022-01-29 21:14:14 +00:00
|
|
|
test_util.run_test_coverage('tools/dtoc/test_fdt.py', None,
|
2022-07-31 02:57:05 +00:00
|
|
|
['tools/patman/*.py', '*test_fdt.py'], build_dir)
|
2018-07-06 16:27:28 +00:00
|
|
|
|
|
|
|
|
2022-07-31 02:57:06 +00:00
|
|
|
def run_tests(args, processes):
|
2018-07-06 16:27:24 +00:00
|
|
|
"""Run all the test we have for the fdt model
|
|
|
|
|
|
|
|
Args:
|
2022-07-31 02:57:06 +00:00
|
|
|
args (list or str): List of positional args provided. This can hold a
|
|
|
|
test name to execute (as in 'test_fdt -t testFdt', for example)
|
|
|
|
processes (int): Number of processes to use (None means as many as there
|
|
|
|
are CPUs on the system. This must be set to 1 when running under
|
|
|
|
the python3-coverage tool
|
2022-03-19 00:01:50 +00:00
|
|
|
|
|
|
|
Returns:
|
2022-07-31 02:57:06 +00:00
|
|
|
int: Return code, 0 on success
|
2018-07-06 16:27:24 +00:00
|
|
|
"""
|
2022-07-31 02:57:06 +00:00
|
|
|
test_name = args[0] if args else None
|
2022-04-02 17:06:06 +00:00
|
|
|
result = test_util.run_test_suites(
|
|
|
|
'test_fdt', False, False, False, None, test_name, None,
|
2022-03-19 00:01:50 +00:00
|
|
|
[TestFdt, TestNode, TestProp, TestFdtUtil])
|
|
|
|
|
2022-04-02 17:06:06 +00:00
|
|
|
return (0 if result.wasSuccessful() else 1)
|
|
|
|
|
2018-07-06 16:27:24 +00:00
|
|
|
|
|
|
|
if __name__ != '__main__':
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
parser = OptionParser()
|
2018-07-06 16:27:28 +00:00
|
|
|
parser.add_option('-B', '--build-dir', type='string', default='b',
|
|
|
|
help='Directory containing the build output')
|
2018-10-02 03:12:47 +00:00
|
|
|
parser.add_option('-P', '--processes', type=int,
|
|
|
|
help='set number of processes to use for running tests')
|
2018-07-06 16:27:24 +00:00
|
|
|
parser.add_option('-t', '--test', action='store_true', dest='test',
|
|
|
|
default=False, help='run tests')
|
2018-07-06 16:27:28 +00:00
|
|
|
parser.add_option('-T', '--test-coverage', action='store_true',
|
|
|
|
default=False, help='run tests and check for 100% coverage')
|
2018-07-06 16:27:24 +00:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
# Run our meagre tests
|
|
|
|
if options.test:
|
2022-07-31 02:57:06 +00:00
|
|
|
ret_code = run_tests(args, options.processes)
|
2022-03-19 00:01:50 +00:00
|
|
|
sys.exit(ret_code)
|
2018-07-06 16:27:28 +00:00
|
|
|
elif options.test_coverage:
|
2022-07-31 02:57:05 +00:00
|
|
|
run_test_coverage(options.build_dir)
|