Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4f44304 | 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 | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 5 | # Test for the Entry class |
| 6 | |
| 7 | import collections |
Simon Glass | 8d2ef3e | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 8 | import importlib |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 9 | import os |
| 10 | import sys |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 11 | import unittest |
| 12 | |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 13 | from binman import entry |
Simon Glass | 858436d | 2021-11-23 21:09:49 -0700 | [diff] [blame] | 14 | from binman.etype.blob import Entry_blob |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 15 | from dtoc import fdt |
| 16 | from dtoc import fdt_util |
Simon Glass | 4583c00 | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 17 | from u_boot_pylib import tools |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 18 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 19 | class TestEntry(unittest.TestCase): |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 20 | def setUp(self): |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 21 | tools.prepare_output_dir(None) |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 22 | |
| 23 | def tearDown(self): |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 24 | tools.finalise_output_dir() |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 25 | |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 26 | def GetNode(self): |
| 27 | binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 28 | fname = fdt_util.EnsureCompiled( |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 29 | os.path.join(binman_dir,('test/005_simple.dts'))) |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 30 | dtb = fdt.FdtScan(fname) |
| 31 | return dtb.GetNode('/binman/u-boot') |
| 32 | |
Simon Glass | e90fe1b | 2019-07-08 14:25:23 -0600 | [diff] [blame] | 33 | def _ReloadEntry(self): |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 34 | global entry |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 35 | if entry: |
Simon Glass | 8d2ef3e | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 36 | importlib.reload(entry) |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 37 | else: |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 38 | from binman import entry |
Simon Glass | e90fe1b | 2019-07-08 14:25:23 -0600 | [diff] [blame] | 39 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 40 | def testEntryContents(self): |
| 41 | """Test the Entry bass class""" |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 42 | from binman import entry |
Simon Glass | c6bd6e2 | 2019-07-20 12:23:45 -0600 | [diff] [blame] | 43 | base_entry = entry.Entry(None, None, None) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 44 | self.assertEqual(True, base_entry.ObtainContents()) |
| 45 | |
| 46 | def testUnknownEntry(self): |
| 47 | """Test that unknown entry types are detected""" |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 48 | Node = collections.namedtuple('Node', ['name', 'path']) |
| 49 | node = Node('invalid-name', 'invalid-path') |
| 50 | with self.assertRaises(ValueError) as e: |
| 51 | entry.Entry.Create(None, node, node.name) |
| 52 | self.assertIn("Unknown entry type 'invalid-name' in node " |
| 53 | "'invalid-path'", str(e.exception)) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 54 | |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 55 | def testUniqueName(self): |
| 56 | """Test Entry.GetUniqueName""" |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 57 | Node = collections.namedtuple('Node', ['name', 'parent']) |
| 58 | base_node = Node('root', None) |
Simon Glass | c6bd6e2 | 2019-07-20 12:23:45 -0600 | [diff] [blame] | 59 | base_entry = entry.Entry(None, None, base_node) |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 60 | self.assertEqual('root', base_entry.GetUniqueName()) |
| 61 | sub_node = Node('subnode', base_node) |
Simon Glass | c6bd6e2 | 2019-07-20 12:23:45 -0600 | [diff] [blame] | 62 | sub_entry = entry.Entry(None, None, sub_node) |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 63 | self.assertEqual('root.subnode', sub_entry.GetUniqueName()) |
| 64 | |
Simon Glass | 6c234bf | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 65 | def testGetDefaultFilename(self): |
| 66 | """Trivial test for this base class function""" |
Simon Glass | c6bd6e2 | 2019-07-20 12:23:45 -0600 | [diff] [blame] | 67 | base_entry = entry.Entry(None, None, None) |
Simon Glass | 6c234bf | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 68 | self.assertIsNone(base_entry.GetDefaultFilename()) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 69 | |
Simon Glass | 4bdd300 | 2019-07-20 12:23:31 -0600 | [diff] [blame] | 70 | def testBlobFdt(self): |
| 71 | """Test the GetFdtEtype() method of the blob-dtb entries""" |
| 72 | base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb') |
| 73 | self.assertIsNone(base.GetFdtEtype()) |
| 74 | |
| 75 | dtb = entry.Entry.Create(None, self.GetNode(), 'u-boot-dtb') |
| 76 | self.assertEqual('u-boot-dtb', dtb.GetFdtEtype()) |
| 77 | |
Simon Glass | eb0f4a4 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 78 | def testWriteChildData(self): |
| 79 | """Test the WriteChildData() method of the base class""" |
| 80 | base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb') |
| 81 | self.assertTrue(base.WriteChildData(base)) |
| 82 | |
Simon Glass | 8dbb744 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 83 | def testReadChildData(self): |
| 84 | """Test the ReadChildData() method of the base class""" |
| 85 | base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb') |
| 86 | self.assertIsNone(base.ReadChildData(base)) |
| 87 | |
Simon Glass | b35fb17 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 88 | def testExpandedEntry(self): |
| 89 | """Test use of an expanded entry when available""" |
| 90 | base = entry.Entry.Create(None, self.GetNode()) |
| 91 | self.assertEqual('u-boot', base.etype) |
| 92 | |
| 93 | expanded = entry.Entry.Create(None, self.GetNode(), expanded=True) |
| 94 | self.assertEqual('u-boot-expanded', expanded.etype) |
| 95 | |
| 96 | with self.assertRaises(ValueError) as e: |
| 97 | entry.Entry.Create(None, self.GetNode(), 'missing', expanded=True) |
| 98 | self.assertIn("Unknown entry type 'missing' in node '/binman/u-boot'", |
| 99 | str(e.exception)) |
Simon Glass | 4bdd300 | 2019-07-20 12:23:31 -0600 | [diff] [blame] | 100 | |
Simon Glass | 858436d | 2021-11-23 21:09:49 -0700 | [diff] [blame] | 101 | def testMissingEtype(self): |
| 102 | """Test use of a blob etype when the requested one is not available""" |
| 103 | ent = entry.Entry.Create(None, self.GetNode(), 'missing', |
| 104 | missing_etype=True) |
| 105 | self.assertTrue(isinstance(ent, Entry_blob)) |
| 106 | self.assertEquals('missing', ent.etype) |
| 107 | |
Stefan Herbrechtsmeier | c3665a8 | 2022-08-19 16:25:31 +0200 | [diff] [blame] | 108 | def testDecompressData(self): |
| 109 | """Test the DecompressData() method of the base class""" |
| 110 | base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb') |
| 111 | base.compress = 'lz4' |
| 112 | bintools = {} |
| 113 | base.comp_bintool = base.AddBintool(bintools, '_testing') |
| 114 | self.assertEquals(tools.get_bytes(0, 1024), base.CompressData(b'abc')) |
| 115 | self.assertEquals(tools.get_bytes(0, 1024), base.DecompressData(b'abc')) |
| 116 | |
Simon Glass | 060a65e | 2023-01-23 11:29:41 -0700 | [diff] [blame] | 117 | def testLookupOffset(self): |
| 118 | """Test the lookup_offset() method of the base class""" |
| 119 | def MyFindEntryByNode(node): |
| 120 | return self.found |
| 121 | |
| 122 | base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb') |
| 123 | base.FindEntryByNode = MyFindEntryByNode |
| 124 | base.section = base |
| 125 | self.found = None |
| 126 | base.offset_from_elf = [self.GetNode(), 'start', 0] |
| 127 | with self.assertRaises(ValueError) as e: |
| 128 | base.lookup_offset() |
| 129 | self.assertIn("Cannot find entry for node 'u-boot'", str(e.exception)) |
| 130 | |
| 131 | self.found = base |
| 132 | with self.assertRaises(ValueError) as e: |
| 133 | base.lookup_offset() |
| 134 | self.assertIn("Need elf-fname property 'u-boot'", str(e.exception)) |
| 135 | |
Simon Glass | 858436d | 2021-11-23 21:09:49 -0700 | [diff] [blame] | 136 | |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 137 | if __name__ == "__main__": |
| 138 | unittest.main() |