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