blob: 40d74d401a2001bd3520d1f68174668593b262e9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass57454f42016-11-25 20:15:52 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass57454f42016-11-25 20:15:52 -07005# Test for the Entry class
6
7import collections
Simon Glass2a0fa982022-02-11 13:23:21 -07008import importlib
Simon Glass8f521362017-11-12 21:52:21 -07009import os
10import sys
Simon Glass57454f42016-11-25 20:15:52 -070011import unittest
12
Simon Glassc585dd42020-04-17 18:09:03 -060013from binman import entry
Simon Glassb9028bc2021-11-23 21:09:49 -070014from binman.etype.blob import Entry_blob
Simon Glassc585dd42020-04-17 18:09:03 -060015from dtoc import fdt
16from dtoc import fdt_util
Simon Glass131444f2023-02-23 18:18:04 -070017from u_boot_pylib import tools
Simon Glass8f521362017-11-12 21:52:21 -070018
Simon Glass57454f42016-11-25 20:15:52 -070019class TestEntry(unittest.TestCase):
Simon Glass752e7552018-10-01 21:12:41 -060020 def setUp(self):
Simon Glass80025522022-01-29 14:14:04 -070021 tools.prepare_output_dir(None)
Simon Glass752e7552018-10-01 21:12:41 -060022
23 def tearDown(self):
Simon Glass80025522022-01-29 14:14:04 -070024 tools.finalise_output_dir()
Simon Glass752e7552018-10-01 21:12:41 -060025
Simon Glass8f521362017-11-12 21:52:21 -070026 def GetNode(self):
27 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass8f521362017-11-12 21:52:21 -070028 fname = fdt_util.EnsureCompiled(
Simon Glass511f6582018-10-01 12:22:30 -060029 os.path.join(binman_dir,('test/005_simple.dts')))
Simon Glass8f521362017-11-12 21:52:21 -070030 dtb = fdt.FdtScan(fname)
31 return dtb.GetNode('/binman/u-boot')
32
Simon Glass97011e12019-07-08 14:25:23 -060033 def _ReloadEntry(self):
Simon Glass8f521362017-11-12 21:52:21 -070034 global entry
Simon Glass7057d022018-10-01 21:12:47 -060035 if entry:
Simon Glass2a0fa982022-02-11 13:23:21 -070036 importlib.reload(entry)
Simon Glass7057d022018-10-01 21:12:47 -060037 else:
Simon Glassc585dd42020-04-17 18:09:03 -060038 from binman import entry
Simon Glass97011e12019-07-08 14:25:23 -060039
Simon Glass57454f42016-11-25 20:15:52 -070040 def testEntryContents(self):
41 """Test the Entry bass class"""
Simon Glassc585dd42020-04-17 18:09:03 -060042 from binman import entry
Simon Glass2c360cf2019-07-20 12:23:45 -060043 base_entry = entry.Entry(None, None, None)
Simon Glass57454f42016-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 Glass57454f42016-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 Glassac599912017-11-12 21:52:22 -070054
Simon Glass639505b2018-09-14 04:57:11 -060055 def testUniqueName(self):
56 """Test Entry.GetUniqueName"""
Simon Glass639505b2018-09-14 04:57:11 -060057 Node = collections.namedtuple('Node', ['name', 'parent'])
58 base_node = Node('root', None)
Simon Glass2c360cf2019-07-20 12:23:45 -060059 base_entry = entry.Entry(None, None, base_node)
Simon Glass639505b2018-09-14 04:57:11 -060060 self.assertEqual('root', base_entry.GetUniqueName())
61 sub_node = Node('subnode', base_node)
Simon Glass2c360cf2019-07-20 12:23:45 -060062 sub_entry = entry.Entry(None, None, sub_node)
Simon Glass639505b2018-09-14 04:57:11 -060063 self.assertEqual('root.subnode', sub_entry.GetUniqueName())
64
Simon Glass3732ec32018-09-14 04:57:18 -060065 def testGetDefaultFilename(self):
66 """Trivial test for this base class function"""
Simon Glass2c360cf2019-07-20 12:23:45 -060067 base_entry = entry.Entry(None, None, None)
Simon Glass3732ec32018-09-14 04:57:18 -060068 self.assertIsNone(base_entry.GetDefaultFilename())
Simon Glassac599912017-11-12 21:52:22 -070069
Simon Glass684a4f12019-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 Glass37fdd142019-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 Glassb9ba4e02019-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 Glass2f859412021-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 Glass684a4f12019-07-20 12:23:31 -0600100
Simon Glassb9028bc2021-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))
Brandon Maiera657bc62024-06-04 16:16:05 +0000106 self.assertEqual('missing', ent.etype)
Simon Glassb9028bc2021-11-23 21:09:49 -0700107
Stefan Herbrechtsmeier94813a02022-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')
Brandon Maiera657bc62024-06-04 16:16:05 +0000114 self.assertEqual(tools.get_bytes(0, 1024), base.CompressData(b'abc'))
115 self.assertEqual(tools.get_bytes(0, 1024), base.DecompressData(b'abc'))
Stefan Herbrechtsmeier94813a02022-08-19 16:25:31 +0200116
Simon Glass6490d4b2023-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 Glassb9028bc2021-11-23 21:09:49 -0700136
Simon Glassac599912017-11-12 21:52:22 -0700137if __name__ == "__main__":
138 unittest.main()