#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0+
-# Copyright (c) 2018 Google, Inc
-# Written by Simon Glass <sjg@chromium.org>
-#
+
+"""
+Tests for the Fdt module
+Copyright (c) 2018 Google, Inc
+Written by Simon Glass <sjg@chromium.org>
+"""
from argparse import ArgumentParser
-import glob
import os
import shutil
import sys
sys.path.insert(2, os.path.join(our_path,
'../../build-sandbox_spl/scripts/dtc/pylibfdt'))
+#pylint: disable=wrong-import-position
from dtoc import fdt
from dtoc import fdt_util
from dtoc.fdt_util import fdt32_to_cpu, fdt64_to_cpu
from dtoc.fdt import Type, BytesToValue
import libfdt
-from patman import command
from patman import test_util
from patman import tools
-def _GetPropertyValue(dtb, node, prop_name):
+#pylint: disable=protected-access
+
+def _get_property_value(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
def setUp(self):
self.dtb = fdt.FdtScan(find_dtb_file('dtoc_test_simple.dts'))
- def testFdt(self):
+ def test_fdt(self):
"""Test that we can open an Fdt"""
self.dtb.Scan()
root = self.dtb.GetRoot()
self.assertTrue(isinstance(root, fdt.Node))
- def testGetNode(self):
+ def test_get_node(self):
"""Test the GetNode() method"""
node = self.dtb.GetNode('/spl-test')
self.assertTrue(isinstance(node, fdt.Node))
self.assertTrue(isinstance(node, fdt.Node))
self.assertEqual(0, node.Offset())
- def testFlush(self):
+ def test_flush(self):
"""Check that we can flush the device tree out to its file"""
fname = self.dtb._fname
- with open(fname, 'rb') as fd:
- data = fd.read()
+ with open(fname, 'rb') as inf:
+ inf.read()
os.remove(fname)
with self.assertRaises(IOError):
- open(fname, 'rb')
+ with open(fname, 'rb'):
+ pass
self.dtb.Flush()
- with open(fname, 'rb') as fd:
- data = fd.read()
+ with open(fname, 'rb') as inf:
+ inf.read()
- def testPack(self):
+ def test_pack(self):
"""Test that packing a device tree works"""
self.dtb.Pack()
- def testGetFdtRaw(self):
+ def test_get_fdt_raw(self):
"""Tetst that we can access the raw device-tree data"""
self.assertTrue(isinstance(self.dtb.GetContents(), bytes))
- def testGetProps(self):
+ def test_get_props(self):
"""Tests obtaining a list of properties"""
node = self.dtb.GetNode('/spl-test')
props = self.dtb.GetProps(node)
'stringval', 'u-boot,dm-pre-reloc'],
sorted(props.keys()))
- def testCheckError(self):
+ def test_check_error(self):
"""Tests the ChecKError() function"""
- with self.assertRaises(ValueError) as e:
+ with self.assertRaises(ValueError) as exc:
fdt.CheckErr(-libfdt.NOTFOUND, 'hello')
- self.assertIn('FDT_ERR_NOTFOUND: hello', str(e.exception))
+ self.assertIn('FDT_ERR_NOTFOUND: hello', str(exc.exception))
- def testGetFdt(self):
+ def test_get_fdt(self):
+ """Test getting an Fdt object from a node"""
node = self.dtb.GetNode('/spl-test')
self.assertEqual(self.dtb, node.GetFdt())
- def testBytesToValue(self):
+ def test_bytes_to_value(self):
+ """Test converting a string list into Python"""
self.assertEqual(BytesToValue(b'this\0is\0'),
(Type.STRING, ['this', 'is']))
self.node = self.dtb.GetNode('/spl-test')
self.fdt = self.dtb.GetFdtObj()
- def testOffset(self):
+ def test_offset(self):
"""Tests that we can obtain the offset of a node"""
self.assertTrue(self.node.Offset() > 0)
- def testDelete(self):
+ def test_delete(self):
"""Tests that we can delete a property"""
node2 = self.dtb.GetNode('/spl-test2')
offset1 = node2.Offset()
with self.assertRaises(libfdt.FdtException):
self.node.DeleteProp('missing')
- def testDeleteGetOffset(self):
+ def test_delete_get_offset(self):
"""Test that property offset update when properties are deleted"""
self.node.DeleteProp('intval')
- prop, value = _GetPropertyValue(self.dtb, self.node, 'longbytearray')
+ prop, value = _get_property_value(self.dtb, self.node, 'longbytearray')
self.assertEqual(prop.value, value)
- def testFindNode(self):
+ def test_find_node(self):
"""Tests that we can find a node using the FindNode() functoin"""
node = self.dtb.GetRoot().FindNode('i2c@0')
self.assertEqual('i2c@0', node.name)
self.assertEqual('pmic@9', subnode.name)
self.assertEqual(None, node.FindNode('missing'))
- def testRefreshMissingNode(self):
+ def test_refresh_missing_node(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:
+ with self.assertRaises(ValueError) as exc:
self.dtb.Refresh()
- self.assertIn('Internal error, offset', str(e.exception))
+ self.assertIn('Internal error, offset', str(exc.exception))
- def testRefreshExtraNode(self):
+ def test_refresh_extra_node(self):
"""Test refreshing offsets when an expected node is missing"""
# Delete it from the device tre, not our tables
self.fdt.del_node(self.node.Offset())
- with self.assertRaises(ValueError) as e:
+ with self.assertRaises(ValueError) as exc:
self.dtb.Refresh()
self.assertIn('Internal error, node name mismatch '
- 'spl-test != spl-test2', str(e.exception))
+ 'spl-test != spl-test2', str(exc.exception))
- def testRefreshMissingProp(self):
+ def test_refresh_missing_prop(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:
+ with self.assertRaises(ValueError) as exc:
self.dtb.Refresh()
self.assertIn("Internal error, node '/spl-test' property 'notstring' missing, offset ",
- str(e.exception))
+ str(exc.exception))
- def testLookupPhandle(self):
+ def test_lookup_phandle(self):
"""Test looking up a single phandle"""
dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts'))
node = dtb.GetNode('/phandle-source2')
target = dtb.GetNode('/phandle-target')
self.assertEqual(target, dtb.LookupPhandle(fdt32_to_cpu(prop.value)))
- def testAddNodeSpace(self):
+ def test_add_node_space(self):
"""Test adding a single node when out of space"""
self.fdt.pack()
self.node.AddSubnode('subnode')
- with self.assertRaises(libfdt.FdtException) as e:
+ with self.assertRaises(libfdt.FdtException) as exc:
self.dtb.Sync(auto_resize=False)
- self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
+ self.assertIn('FDT_ERR_NOSPACE', str(exc.exception))
self.dtb.Sync(auto_resize=True)
offset = self.fdt.path_offset('/spl-test/subnode')
self.assertTrue(offset > 0)
- def testAddNodes(self):
+ def test_add_nodes(self):
"""Test adding various subnode and properies"""
node = self.dtb.GetNode('/i2c@0')
self.dtb.Sync(auto_resize=True)
- def testAddOneNode(self):
+ def test_add_one_node(self):
"""Testing deleting and adding a subnode before syncing"""
subnode = self.node.AddSubnode('subnode')
self.node.AddSubnode('subnode2')
self.node.AddSubnode('subnode3')
self.dtb.Sync()
- def testRefreshNameMismatch(self):
+ def test_refresh_name_mismatch(self):
"""Test name mismatch when syncing nodes and properties"""
- prop = self.node.AddInt('integer-a', 12)
+ 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:
+ with self.assertRaises(ValueError) as exc:
self.dtb.Sync()
self.assertIn("Internal error, node '/spl-test' name mismatch 'i2c@0'",
- str(e.exception))
+ str(exc.exception))
- with self.assertRaises(ValueError) as e:
+ with self.assertRaises(ValueError) as exc:
self.node.Refresh(wrong_offset)
self.assertIn("Internal error, node '/spl-test' name mismatch 'i2c@0'",
- str(e.exception))
+ str(exc.exception))
class TestProp(unittest.TestCase):
self.node = self.dtb.GetNode('/spl-test')
self.fdt = self.dtb.GetFdtObj()
- def testMissingNode(self):
+ def test_missing_node(self):
+ """Test GetNode() when the node is missing"""
self.assertEqual(None, self.dtb.GetNode('missing'))
- def testPhandle(self):
+ def test_phandle(self):
+ """Test GetNode() on a phandle"""
dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts'))
node = dtb.GetNode('/phandle-source2')
prop = node.props['clocks']
self.assertTrue(fdt32_to_cpu(prop.value) > 0)
- def _ConvertProp(self, prop_name):
+ def _convert_prop(self, prop_name):
"""Helper function to look up a property in self.node and return it
Args:
- Property name to find
+ str: Property name to find
- Return fdt.Prop object for this property
+ Returns:
+ fdt.Prop: object for this property
"""
- p = self.fdt.getprop(self.node.Offset(), prop_name)
- return fdt.Prop(self.node, -1, prop_name, p)
+ prop = self.fdt.getprop(self.node.Offset(), prop_name)
+ return fdt.Prop(self.node, -1, prop_name, prop)
- def testMakeProp(self):
+ def test_make_prop(self):
"""Test we can convert all the the types that are supported"""
- prop = self._ConvertProp('boolval')
+ prop = self._convert_prop('boolval')
self.assertEqual(Type.BOOL, prop.type)
self.assertEqual(True, prop.value)
- prop = self._ConvertProp('intval')
+ prop = self._convert_prop('intval')
self.assertEqual(Type.INT, prop.type)
self.assertEqual(1, fdt32_to_cpu(prop.value))
- prop = self._ConvertProp('int64val')
+ prop = self._convert_prop('int64val')
self.assertEqual(Type.INT, prop.type)
self.assertEqual(0x123456789abcdef0, fdt64_to_cpu(prop.value))
- prop = self._ConvertProp('intarray')
+ prop = self._convert_prop('intarray')
self.assertEqual(Type.INT, prop.type)
val = [fdt32_to_cpu(val) for val in prop.value]
self.assertEqual([2, 3, 4], val)
- prop = self._ConvertProp('byteval')
+ prop = self._convert_prop('byteval')
self.assertEqual(Type.BYTE, prop.type)
self.assertEqual(5, ord(prop.value))
- prop = self._ConvertProp('longbytearray')
+ prop = self._convert_prop('longbytearray')
self.assertEqual(Type.BYTE, prop.type)
val = [ord(val) for val in prop.value]
self.assertEqual([9, 10, 11, 12, 13, 14, 15, 16, 17], val)
- prop = self._ConvertProp('stringval')
+ prop = self._convert_prop('stringval')
self.assertEqual(Type.STRING, prop.type)
self.assertEqual('message', prop.value)
- prop = self._ConvertProp('stringarray')
+ prop = self._convert_prop('stringarray')
self.assertEqual(Type.STRING, prop.type)
self.assertEqual(['multi-word', 'message'], prop.value)
- prop = self._ConvertProp('notstring')
+ prop = self._convert_prop('notstring')
self.assertEqual(Type.BYTE, prop.type)
val = [ord(val) for val in prop.value]
self.assertEqual([0x20, 0x21, 0x22, 0x10, 0], val)
- def testGetEmpty(self):
+ def test_get_empty(self):
"""Tests the GetEmpty() function for the various supported types"""
self.assertEqual(True, fdt.Prop.GetEmpty(Type.BOOL))
self.assertEqual(chr(0), fdt.Prop.GetEmpty(Type.BYTE))
self.assertEqual(tools.get_bytes(0, 4), fdt.Prop.GetEmpty(Type.INT))
self.assertEqual('', fdt.Prop.GetEmpty(Type.STRING))
- def testGetOffset(self):
+ def test_get_offset(self):
"""Test we can get the offset of a property"""
- prop, value = _GetPropertyValue(self.dtb, self.node, 'longbytearray')
+ prop, value = _get_property_value(self.dtb, self.node, 'longbytearray')
self.assertEqual(prop.value, value)
- def testWiden(self):
+ def test_widen(self):
"""Test widening of values"""
node2 = self.dtb.GetNode('/spl-test2')
node3 = self.dtb.GetNode('/spl-test3')
self.assertEqual(['\x09', '\x0a', '\x0b', '\x0c', '\x0d',
'\x0e', '\x0f', '\x10', '\0'], prop3.value)
- # Similarly for a string array
+ def test_widen_more(self):
+ """More tests of widening values"""
+ node2 = self.dtb.GetNode('/spl-test2')
+ node3 = self.dtb.GetNode('/spl-test3')
+ prop = self.node.props['intval']
+
+ # Test widening a single string into a string array
prop = self.node.props['stringval']
prop2 = node2.props['stringarray']
self.assertFalse(isinstance(prop.value, list))
self.assertTrue(isinstance(prop.value, list))
self.assertEqual(1, len(prop.value))
- def testAdd(self):
+ def test_add(self):
"""Test adding properties"""
self.fdt.pack()
# This function should automatically expand the device tree
# This should fail since it would need to increase the device-tree size
self.node.AddZeroProp('four')
- with self.assertRaises(libfdt.FdtException) as e:
+ with self.assertRaises(libfdt.FdtException) as exc:
self.dtb.Sync(auto_resize=False)
- self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
+ self.assertIn('FDT_ERR_NOSPACE', str(exc.exception))
self.dtb.Sync(auto_resize=True)
- def testAddMore(self):
+ def test_add_more(self):
"""Test various other methods for adding and setting properties"""
self.node.AddZeroProp('one')
self.dtb.Sync(auto_resize=True)
self.fdt.pack()
self.node.SetString('string', val + 'x')
- with self.assertRaises(libfdt.FdtException) as e:
+ with self.assertRaises(libfdt.FdtException) as exc:
self.dtb.Sync(auto_resize=False)
- self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
+ self.assertIn('FDT_ERR_NOSPACE', str(exc.exception))
self.node.SetString('string', val[:-1])
prop = self.node.props['string']
new_offset = self.fdt.path_offset('/spl-test', libfdt.QUIET_NOTFOUND)
self.assertEqual(-libfdt.NOTFOUND, new_offset)
- def testFromData(self):
+ def test_from_data(self):
+ """Test creating an FDT from data"""
dtb2 = fdt.Fdt.FromData(self.dtb.GetContents())
self.assertEqual(dtb2.GetContents(), self.dtb.GetContents())
self.dtb.Sync(auto_resize=True)
self.assertTrue(dtb2.GetContents() != self.dtb.GetContents())
- def testMissingSetInt(self):
+ def test_missing_set_int(self):
"""Test handling of a missing property with SetInt"""
- with self.assertRaises(ValueError) as e:
+ with self.assertRaises(ValueError) as exc:
self.node.SetInt('one', 1)
self.assertIn("node '/spl-test': Missing property 'one'",
- str(e.exception))
+ str(exc.exception))
- def testMissingSetData(self):
+ def test_missing_set_data(self):
"""Test handling of a missing property with SetData"""
- with self.assertRaises(ValueError) as e:
+ with self.assertRaises(ValueError) as exc:
self.node.SetData('one', b'data')
self.assertIn("node '/spl-test': Missing property 'one'",
- str(e.exception))
+ str(exc.exception))
- def testMissingSetString(self):
+ def test_missing_set_string(self):
"""Test handling of a missing property with SetString"""
- with self.assertRaises(ValueError) as e:
+ with self.assertRaises(ValueError) as exc:
self.node.SetString('one', 1)
self.assertIn("node '/spl-test': Missing property 'one'",
- str(e.exception))
+ str(exc.exception))
- def testGetFilename(self):
+ def test_get_filename(self):
"""Test the dtb filename can be provided"""
self.assertEqual(tools.get_output_filename('source.dtb'),
self.dtb.GetFilename())
self.dtb = fdt.FdtScan(find_dtb_file('dtoc_test_simple.dts'))
self.node = self.dtb.GetNode('/spl-test')
- def testGetInt(self):
+ def test_get_int(self):
+ """Test getting an int from a node"""
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:
+ with self.assertRaises(ValueError) as exc:
fdt_util.GetInt(self.node, 'intarray')
self.assertIn("property 'intarray' has list value: expecting a single "
- 'integer', str(e.exception))
+ 'integer', str(exc.exception))
- def testGetInt64(self):
+ def test_get_int64(self):
+ """Test getting a 64-bit int from a node"""
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:
+ with self.assertRaises(ValueError) as exc:
fdt_util.GetInt64(self.node, 'intarray')
self.assertIn(
"property 'intarray' should be a list with 2 items for 64-bit values",
- str(e.exception))
+ str(exc.exception))
- def testGetString(self):
+ def test_get_string(self):
+ """Test getting a string from a node"""
self.assertEqual('message', fdt_util.GetString(self.node, 'stringval'))
self.assertEqual('test', fdt_util.GetString(self.node, 'missing',
'test'))
self.assertEqual('', fdt_util.GetString(self.node, 'boolval'))
- with self.assertRaises(ValueError) as e:
+ with self.assertRaises(ValueError) as exc:
self.assertEqual(3, fdt_util.GetString(self.node, 'stringarray'))
self.assertIn("property 'stringarray' has list value: expecting a "
- 'single string', str(e.exception))
+ 'single string', str(exc.exception))
- def testGetStringList(self):
+ def test_get_string_list(self):
+ """Test getting a string list from a node"""
self.assertEqual(['message'],
fdt_util.GetStringList(self.node, 'stringval'))
self.assertEqual(
fdt_util.GetStringList(self.node, 'missing', ['test']))
self.assertEqual([], fdt_util.GetStringList(self.node, 'boolval'))
- def testGetArgs(self):
+ def test_get_args(self):
+ """Test getting arguments from a node"""
node = self.dtb.GetNode('/orig-node')
self.assertEqual(['message'], fdt_util.GetArgs(self.node, 'stringval'))
self.assertEqual(
"Node '/spl-test': Expected property 'missing'",
str(exc.exception))
- def testGetBool(self):
+ def test_get_bool(self):
+ """Test getting a bool from a node"""
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))
- def testGetByte(self):
+ def test_get_byte(self):
+ """Test getting a byte from a node"""
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:
+ with self.assertRaises(ValueError) as exc:
fdt_util.GetByte(self.node, 'longbytearray')
self.assertIn("property 'longbytearray' has list value: expecting a "
- 'single byte', str(e.exception))
+ 'single byte', str(exc.exception))
- with self.assertRaises(ValueError) as e:
+ with self.assertRaises(ValueError) as exc:
fdt_util.GetByte(self.node, 'intval')
self.assertIn("property 'intval' has length 4, expecting 1",
- str(e.exception))
+ str(exc.exception))
- def testGetBytes(self):
+ def test_get_bytes(self):
+ """Test getting multiple bytes from a node"""
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:
+ with self.assertRaises(ValueError) as exc:
fdt_util.GetBytes(self.node, 'longbytearray', 7)
self.assertIn(
"Node 'spl-test' property 'longbytearray' has length 9, expecting 7",
- str(e.exception))
+ str(exc.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])))
- def testGetPhandleList(self):
+ def test_get_phandle_list(self):
+ """Test getting a list of phandles from a node"""
dtb = fdt.FdtScan(find_dtb_file('dtoc_test_phandle.dts'))
node = dtb.GetNode('/phandle-source2')
self.assertEqual([1], fdt_util.GetPhandleList(node, 'clocks'))
fdt_util.GetPhandleList(node, 'clocks'))
self.assertEqual(None, fdt_util.GetPhandleList(node, 'missing'))
- def testGetDataType(self):
+ def test_get_data_type(self):
+ """Test getting a value of a particular type from a node"""
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:
+ with self.assertRaises(ValueError):
self.assertEqual(3, fdt_util.GetDatatype(self.node, 'boolval',
bool))
- def testFdtCellsToCpu(self):
+ def test_fdt_cells_to_cpu(self):
+ """Test getting cells with the correct endianness"""
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))
2))
self.assertEqual(0x12345678, fdt_util.fdt_cells_to_cpu(val, 1))
- def testEnsureCompiled(self):
+ def test_ensure_compiled(self):
"""Test a degenerate case of this function (file already compiled)"""
dtb = fdt_util.EnsureCompiled(find_dtb_file('dtoc_test_simple.dts'))
self.assertEqual(dtb, fdt_util.EnsureCompiled(dtb))
- def testEnsureCompiledTmpdir(self):
+ def test_ensure_compiled_tmpdir(self):
"""Test providing a temporary directory"""
try:
old_outdir = tools.outdir