Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 2 | # Copyright (c) 2016 Google, Inc |
| 3 | # Written by Simon Glass <sjg@chromium.org> |
| 4 | # |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 5 | # Test for the Entry class |
| 6 | |
| 7 | import collections |
Simon Glass | 8f52136 | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 8 | import os |
| 9 | import sys |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 10 | import unittest |
| 11 | |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 12 | from binman import entry |
| 13 | from dtoc import fdt |
| 14 | from dtoc import fdt_util |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 15 | from patman import tools |
Simon Glass | 8f52136 | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 16 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 17 | class TestEntry(unittest.TestCase): |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 18 | def setUp(self): |
| 19 | tools.PrepareOutputDir(None) |
| 20 | |
| 21 | def tearDown(self): |
| 22 | tools.FinaliseOutputDir() |
| 23 | |
Simon Glass | 8f52136 | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 24 | def GetNode(self): |
| 25 | binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
Simon Glass | 8f52136 | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 26 | fname = fdt_util.EnsureCompiled( |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 27 | os.path.join(binman_dir,('test/005_simple.dts'))) |
Simon Glass | 8f52136 | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 28 | dtb = fdt.FdtScan(fname) |
| 29 | return dtb.GetNode('/binman/u-boot') |
| 30 | |
Simon Glass | 97011e1 | 2019-07-08 14:25:23 -0600 | [diff] [blame] | 31 | def _ReloadEntry(self): |
Simon Glass | 8f52136 | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 32 | global entry |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 33 | if entry: |
Simon Glass | cb7b8d7 | 2019-05-17 22:00:47 -0600 | [diff] [blame] | 34 | if sys.version_info[0] >= 3: |
| 35 | import importlib |
| 36 | importlib.reload(entry) |
| 37 | else: |
| 38 | reload(entry) |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 39 | else: |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 40 | from binman import entry |
Simon Glass | 97011e1 | 2019-07-08 14:25:23 -0600 | [diff] [blame] | 41 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 42 | def testEntryContents(self): |
| 43 | """Test the Entry bass class""" |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 44 | from binman import entry |
Simon Glass | 2c360cf | 2019-07-20 12:23:45 -0600 | [diff] [blame] | 45 | base_entry = entry.Entry(None, None, None) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 46 | self.assertEqual(True, base_entry.ObtainContents()) |
| 47 | |
| 48 | def testUnknownEntry(self): |
| 49 | """Test that unknown entry types are detected""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 50 | Node = collections.namedtuple('Node', ['name', 'path']) |
| 51 | node = Node('invalid-name', 'invalid-path') |
| 52 | with self.assertRaises(ValueError) as e: |
| 53 | entry.Entry.Create(None, node, node.name) |
| 54 | self.assertIn("Unknown entry type 'invalid-name' in node " |
| 55 | "'invalid-path'", str(e.exception)) |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 56 | |
Simon Glass | 639505b | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 57 | def testUniqueName(self): |
| 58 | """Test Entry.GetUniqueName""" |
Simon Glass | 639505b | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 59 | Node = collections.namedtuple('Node', ['name', 'parent']) |
| 60 | base_node = Node('root', None) |
Simon Glass | 2c360cf | 2019-07-20 12:23:45 -0600 | [diff] [blame] | 61 | base_entry = entry.Entry(None, None, base_node) |
Simon Glass | 639505b | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 62 | self.assertEqual('root', base_entry.GetUniqueName()) |
| 63 | sub_node = Node('subnode', base_node) |
Simon Glass | 2c360cf | 2019-07-20 12:23:45 -0600 | [diff] [blame] | 64 | sub_entry = entry.Entry(None, None, sub_node) |
Simon Glass | 639505b | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 65 | self.assertEqual('root.subnode', sub_entry.GetUniqueName()) |
| 66 | |
Simon Glass | 3732ec3 | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 67 | def testGetDefaultFilename(self): |
| 68 | """Trivial test for this base class function""" |
Simon Glass | 2c360cf | 2019-07-20 12:23:45 -0600 | [diff] [blame] | 69 | base_entry = entry.Entry(None, None, None) |
Simon Glass | 3732ec3 | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 70 | self.assertIsNone(base_entry.GetDefaultFilename()) |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 71 | |
Simon Glass | 684a4f1 | 2019-07-20 12:23:31 -0600 | [diff] [blame] | 72 | def testBlobFdt(self): |
| 73 | """Test the GetFdtEtype() method of the blob-dtb entries""" |
| 74 | base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb') |
| 75 | self.assertIsNone(base.GetFdtEtype()) |
| 76 | |
| 77 | dtb = entry.Entry.Create(None, self.GetNode(), 'u-boot-dtb') |
| 78 | self.assertEqual('u-boot-dtb', dtb.GetFdtEtype()) |
| 79 | |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 80 | def testWriteChildData(self): |
| 81 | """Test the WriteChildData() method of the base class""" |
| 82 | base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb') |
| 83 | self.assertTrue(base.WriteChildData(base)) |
| 84 | |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 85 | def testReadChildData(self): |
| 86 | """Test the ReadChildData() method of the base class""" |
| 87 | base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb') |
| 88 | self.assertIsNone(base.ReadChildData(base)) |
| 89 | |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 90 | def testExpandedEntry(self): |
| 91 | """Test use of an expanded entry when available""" |
| 92 | base = entry.Entry.Create(None, self.GetNode()) |
| 93 | self.assertEqual('u-boot', base.etype) |
| 94 | |
| 95 | expanded = entry.Entry.Create(None, self.GetNode(), expanded=True) |
| 96 | self.assertEqual('u-boot-expanded', expanded.etype) |
| 97 | |
| 98 | with self.assertRaises(ValueError) as e: |
| 99 | entry.Entry.Create(None, self.GetNode(), 'missing', expanded=True) |
| 100 | self.assertIn("Unknown entry type 'missing' in node '/binman/u-boot'", |
| 101 | str(e.exception)) |
Simon Glass | 684a4f1 | 2019-07-20 12:23:31 -0600 | [diff] [blame] | 102 | |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 103 | if __name__ == "__main__": |
| 104 | unittest.main() |