blob: ac6582cf86a80144780350ff72877f5efa283ce9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass4f443042016-11-25 20:15:52 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass4f443042016-11-25 20:15:52 -07005# Test for the Entry class
6
7import collections
Simon Glass8d2ef3e2022-02-11 13:23:21 -07008import importlib
Simon Glass934cdcf2017-11-12 21:52:21 -07009import os
10import sys
Simon Glass4f443042016-11-25 20:15:52 -070011import unittest
12
Simon Glass16287932020-04-17 18:09:03 -060013from binman import entry
Simon Glass858436d2021-11-23 21:09:49 -070014from binman.etype.blob import Entry_blob
Simon Glass16287932020-04-17 18:09:03 -060015from dtoc import fdt
16from dtoc import fdt_util
Simon Glass4583c002023-02-23 18:18:04 -070017from u_boot_pylib import tools
Simon Glass934cdcf2017-11-12 21:52:21 -070018
Simon Glass4f443042016-11-25 20:15:52 -070019class TestEntry(unittest.TestCase):
Simon Glasse0e62752018-10-01 21:12:41 -060020 def setUp(self):
Simon Glassc1aa66e2022-01-29 14:14:04 -070021 tools.prepare_output_dir(None)
Simon Glasse0e62752018-10-01 21:12:41 -060022
23 def tearDown(self):
Simon Glassc1aa66e2022-01-29 14:14:04 -070024 tools.finalise_output_dir()
Simon Glasse0e62752018-10-01 21:12:41 -060025
Simon Glass934cdcf2017-11-12 21:52:21 -070026 def GetNode(self):
27 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass934cdcf2017-11-12 21:52:21 -070028 fname = fdt_util.EnsureCompiled(
Simon Glass741f2d62018-10-01 12:22:30 -060029 os.path.join(binman_dir,('test/005_simple.dts')))
Simon Glass934cdcf2017-11-12 21:52:21 -070030 dtb = fdt.FdtScan(fname)
31 return dtb.GetNode('/binman/u-boot')
32
Simon Glasse90fe1b2019-07-08 14:25:23 -060033 def _ReloadEntry(self):
Simon Glass934cdcf2017-11-12 21:52:21 -070034 global entry
Simon Glass11ae93e2018-10-01 21:12:47 -060035 if entry:
Simon Glass8d2ef3e2022-02-11 13:23:21 -070036 importlib.reload(entry)
Simon Glass11ae93e2018-10-01 21:12:47 -060037 else:
Simon Glass16287932020-04-17 18:09:03 -060038 from binman import entry
Simon Glasse90fe1b2019-07-08 14:25:23 -060039
Simon Glass4f443042016-11-25 20:15:52 -070040 def testEntryContents(self):
41 """Test the Entry bass class"""
Simon Glass16287932020-04-17 18:09:03 -060042 from binman import entry
Simon Glassc6bd6e22019-07-20 12:23:45 -060043 base_entry = entry.Entry(None, None, None)
Simon Glass4f443042016-11-25 20:15:52 -070044 self.assertEqual(True, base_entry.ObtainContents())
45
46 def testUnknownEntry(self):
47 """Test that unknown entry types are detected"""
Simon Glass4f443042016-11-25 20:15:52 -070048 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 Glass9fc60b42017-11-12 21:52:22 -070054
Simon Glassa326b492018-09-14 04:57:11 -060055 def testUniqueName(self):
56 """Test Entry.GetUniqueName"""
Simon Glassa326b492018-09-14 04:57:11 -060057 Node = collections.namedtuple('Node', ['name', 'parent'])
58 base_node = Node('root', None)
Simon Glassc6bd6e22019-07-20 12:23:45 -060059 base_entry = entry.Entry(None, None, base_node)
Simon Glassa326b492018-09-14 04:57:11 -060060 self.assertEqual('root', base_entry.GetUniqueName())
61 sub_node = Node('subnode', base_node)
Simon Glassc6bd6e22019-07-20 12:23:45 -060062 sub_entry = entry.Entry(None, None, sub_node)
Simon Glassa326b492018-09-14 04:57:11 -060063 self.assertEqual('root.subnode', sub_entry.GetUniqueName())
64
Simon Glass6c234bf2018-09-14 04:57:18 -060065 def testGetDefaultFilename(self):
66 """Trivial test for this base class function"""
Simon Glassc6bd6e22019-07-20 12:23:45 -060067 base_entry = entry.Entry(None, None, None)
Simon Glass6c234bf2018-09-14 04:57:18 -060068 self.assertIsNone(base_entry.GetDefaultFilename())
Simon Glass9fc60b42017-11-12 21:52:22 -070069
Simon Glass4bdd3002019-07-20 12:23:31 -060070 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 Glasseb0f4a42019-07-20 12:24:06 -060078 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 Glass8dbb7442019-08-24 07:22:44 -060083 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 Glassb35fb172021-03-18 20:25:04 +130088 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 Glass4bdd3002019-07-20 12:23:31 -0600100
Simon Glass858436d2021-11-23 21:09:49 -0700101 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 Herbrechtsmeierc3665a82022-08-19 16:25:31 +0200108 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 Glass060a65e2023-01-23 11:29:41 -0700117 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 Glass858436d2021-11-23 21:09:49 -0700136
Simon Glass9fc60b42017-11-12 21:52:22 -0700137if __name__ == "__main__":
138 unittest.main()