[U-Boot] [PATCH 18/29] dtoc: Update fdt tests to increase code coverage

Simon Glass sjg at chromium.org
Wed Jun 6 00:46:54 UTC 2018


At present only some of the fdt functionality is tests. Add more tests to
bring code coverage up further.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 tools/dtoc/dtoc_test_simple.dts |  1 +
 tools/dtoc/fdt.py               | 16 ++++++---------
 tools/dtoc/test_dtoc.py         |  3 +++
 tools/dtoc/test_fdt.py          | 35 +++++++++++++++++++++++++++++++--
 4 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/tools/dtoc/dtoc_test_simple.dts b/tools/dtoc/dtoc_test_simple.dts
index 895cc1fea28..a6e2ba8bef7 100644
--- a/tools/dtoc/dtoc_test_simple.dts
+++ b/tools/dtoc/dtoc_test_simple.dts
@@ -21,6 +21,7 @@
 		longbytearray = [09 0a 0b 0c 0d 0e 0f 10 11];
 		stringval = "message";
 		stringarray = "multi-word", "message";
+		stringbad = "message", [01], "junk";
 	};
 
 	spl-test2 {
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py
index fc8aeb81de5..3595c63a07f 100644
--- a/tools/dtoc/fdt.py
+++ b/tools/dtoc/fdt.py
@@ -245,8 +245,8 @@ class Node:
         offset = fdt_obj.first_subnode(self._offset, QUIET_NOTFOUND)
         for subnode in self.subnodes:
             if subnode.name != fdt_obj.get_name(offset):
-                raise ValueError('Node name mismatch %s != %s' %
-                                 (subnode.name != fdt_obj.get_name(offset)))
+                raise ValueError('Internal error, node name mismatch %s != %s' %
+                                 (subnode.name, fdt_obj.get_name(offset)))
             subnode.Refresh(offset)
             offset = fdt_obj.next_subnode(offset, QUIET_NOTFOUND)
         if offset != -libfdt.FDT_ERR_NOTFOUND:
@@ -255,11 +255,12 @@ class Node:
         poffset = fdt_obj.first_property_offset(self._offset, QUIET_NOTFOUND)
         while poffset >= 0:
             p = fdt_obj.get_property_by_offset(poffset)
-            prop = self.props[p.name]
+            prop = self.props.get(p.name)
+            if not prop:
+                raise ValueError("Internal error, property '%s' missing, "
+                                 'offset %d' % (p.name, poffset))
             prop.RefreshOffset(poffset)
             poffset = fdt_obj.next_property_offset(poffset, QUIET_NOTFOUND)
-        if poffset != -libfdt.FDT_ERR_NOTFOUND:
-            raise ValueError('Internal error, poffset == %d' % poffset)
 
     def DeleteProp(self, prop_name):
         """Delete a property of a node
@@ -366,11 +367,6 @@ class Fdt:
         """
         return self._fdt_obj
 
-    def CheckErr(self, errnum, msg):
-        if errnum:
-            raise ValueError('Error %d: %s: %s' %
-                (errnum, libfdt.fdt_strerror(errnum), msg))
-
     def GetProps(self, node):
         """Get all properties from a node.
 
diff --git a/tools/dtoc/test_dtoc.py b/tools/dtoc/test_dtoc.py
index ce6d2585a40..95818928b32 100644
--- a/tools/dtoc/test_dtoc.py
+++ b/tools/dtoc/test_dtoc.py
@@ -181,6 +181,7 @@ struct dtd_sandbox_spl_test {
 \tfdt32_t\t\tintval;
 \tunsigned char\tlongbytearray[9];
 \tconst char *\tstringarray[3];
+\tunsigned char\tstringbad[14];
 \tconst char *\tstringval;
 };
 struct dtd_sandbox_spl_test_2 {
@@ -195,6 +196,8 @@ static struct dtd_sandbox_spl_test dtv_spl_test = {
 \t.bytearray\t\t= {0x6, 0x0, 0x0},
 \t.byteval\t\t= 0x5,
 \t.intval\t\t\t= 0x1,
+\t.stringbad\t\t= {0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x0,
+\t\t0x1, 0x6a, 0x75, 0x6e, 0x6b, 0x0},
 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
 \t\t0x11},
 \t.stringval\t\t= "message",
diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py
index 78afb33b056..d6ea1951001 100755
--- a/tools/dtoc/test_fdt.py
+++ b/tools/dtoc/test_fdt.py
@@ -105,13 +105,13 @@ class TestFdt(unittest.TestCase):
         props = self.dtb.GetProps(node)
         self.assertEqual(['boolval', 'bytearray', 'byteval', 'compatible',
                           'intarray', 'intval', 'longbytearray',
-                          'stringarray', 'stringval', 'u-boot,dm-pre-reloc'],
+                          'stringarray', 'stringbad', 'stringval', 'u-boot,dm-pre-reloc'],
                          sorted(props.keys()))
 
     def testCheckError(self):
         """Tests the ChecKError() function"""
         with self.assertRaises(ValueError) as e:
-            self.dtb.CheckErr(-libfdt.NOTFOUND, 'hello')
+            fdt.CheckErr(-libfdt.NOTFOUND, 'hello')
         self.assertIn('FDT_ERR_NOTFOUND: hello', str(e.exception))
 
 
@@ -161,6 +161,32 @@ class TestNode(unittest.TestCase):
         self.assertEqual('pmic at 9', subnode.name)
         self.assertEqual(None, node._FindNode('missing'))
 
+    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
+        self.dtb.GetFdtObj().del_node(self.node.Offset())
+        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['stringbad']
+        with self.assertRaises(ValueError) as e:
+            self.dtb.Refresh()
+        self.assertIn("Internal error, property 'stringbad' missing, offset ",
+                      str(e.exception))
+
 
 class TestProp(unittest.TestCase):
     """Test operation of the Prop class"""
@@ -228,6 +254,11 @@ class TestProp(unittest.TestCase):
         self.assertEqual(fdt.TYPE_STRING, prop.type)
         self.assertEqual(['multi-word', 'message'], prop.value)
 
+        prop = self._ConvertProp('stringbad')
+        self.assertEqual(fdt.TYPE_BYTE, prop.type)
+        val = [ch for ch in 'message\x00\x01junk\0']
+        self.assertEqual(val, prop.value)
+
     def testGetEmpty(self):
         """Tests the GetEmpty() function for the various supported types"""
         self.assertEqual(True, fdt.Prop.GetEmpty(fdt.TYPE_BOOL))
-- 
2.17.1.1185.g55be947832-goog



More information about the U-Boot mailing list