blob: 1b59c9056ec8757e7388238279617991ed95b132 [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 Glass8f521362017-11-12 21:52:21 -07008import os
9import sys
Simon Glass57454f42016-11-25 20:15:52 -070010import unittest
11
Simon Glassc585dd42020-04-17 18:09:03 -060012from binman import entry
Simon Glassb9028bc2021-11-23 21:09:49 -070013from binman.etype.blob import Entry_blob
Simon Glassc585dd42020-04-17 18:09:03 -060014from dtoc import fdt
15from dtoc import fdt_util
Simon Glassa997ea52020-04-17 18:09:04 -060016from patman import tools
Simon Glass8f521362017-11-12 21:52:21 -070017
Simon Glass57454f42016-11-25 20:15:52 -070018class TestEntry(unittest.TestCase):
Simon Glass752e7552018-10-01 21:12:41 -060019 def setUp(self):
20 tools.PrepareOutputDir(None)
21
22 def tearDown(self):
23 tools.FinaliseOutputDir()
24
Simon Glass8f521362017-11-12 21:52:21 -070025 def GetNode(self):
26 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass8f521362017-11-12 21:52:21 -070027 fname = fdt_util.EnsureCompiled(
Simon Glass511f6582018-10-01 12:22:30 -060028 os.path.join(binman_dir,('test/005_simple.dts')))
Simon Glass8f521362017-11-12 21:52:21 -070029 dtb = fdt.FdtScan(fname)
30 return dtb.GetNode('/binman/u-boot')
31
Simon Glass97011e12019-07-08 14:25:23 -060032 def _ReloadEntry(self):
Simon Glass8f521362017-11-12 21:52:21 -070033 global entry
Simon Glass7057d022018-10-01 21:12:47 -060034 if entry:
Simon Glasscb7b8d72019-05-17 22:00:47 -060035 if sys.version_info[0] >= 3:
36 import importlib
37 importlib.reload(entry)
38 else:
39 reload(entry)
Simon Glass7057d022018-10-01 21:12:47 -060040 else:
Simon Glassc585dd42020-04-17 18:09:03 -060041 from binman import entry
Simon Glass97011e12019-07-08 14:25:23 -060042
Simon Glass57454f42016-11-25 20:15:52 -070043 def testEntryContents(self):
44 """Test the Entry bass class"""
Simon Glassc585dd42020-04-17 18:09:03 -060045 from binman import entry
Simon Glass2c360cf2019-07-20 12:23:45 -060046 base_entry = entry.Entry(None, None, None)
Simon Glass57454f42016-11-25 20:15:52 -070047 self.assertEqual(True, base_entry.ObtainContents())
48
49 def testUnknownEntry(self):
50 """Test that unknown entry types are detected"""
Simon Glass57454f42016-11-25 20:15:52 -070051 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 Glassac599912017-11-12 21:52:22 -070057
Simon Glass639505b2018-09-14 04:57:11 -060058 def testUniqueName(self):
59 """Test Entry.GetUniqueName"""
Simon Glass639505b2018-09-14 04:57:11 -060060 Node = collections.namedtuple('Node', ['name', 'parent'])
61 base_node = Node('root', None)
Simon Glass2c360cf2019-07-20 12:23:45 -060062 base_entry = entry.Entry(None, None, base_node)
Simon Glass639505b2018-09-14 04:57:11 -060063 self.assertEqual('root', base_entry.GetUniqueName())
64 sub_node = Node('subnode', base_node)
Simon Glass2c360cf2019-07-20 12:23:45 -060065 sub_entry = entry.Entry(None, None, sub_node)
Simon Glass639505b2018-09-14 04:57:11 -060066 self.assertEqual('root.subnode', sub_entry.GetUniqueName())
67
Simon Glass3732ec32018-09-14 04:57:18 -060068 def testGetDefaultFilename(self):
69 """Trivial test for this base class function"""
Simon Glass2c360cf2019-07-20 12:23:45 -060070 base_entry = entry.Entry(None, None, None)
Simon Glass3732ec32018-09-14 04:57:18 -060071 self.assertIsNone(base_entry.GetDefaultFilename())
Simon Glassac599912017-11-12 21:52:22 -070072
Simon Glass684a4f12019-07-20 12:23:31 -060073 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 Glass37fdd142019-07-20 12:24:06 -060081 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 Glassb9ba4e02019-08-24 07:22:44 -060086 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 Glass2f859412021-03-18 20:25:04 +130091 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 Glass684a4f12019-07-20 12:23:31 -0600103
Simon Glassb9028bc2021-11-23 21:09:49 -0700104 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 Glassac599912017-11-12 21:52:22 -0700112if __name__ == "__main__":
113 unittest.main()