blob: 13f58645168e26d87088453add64454ff404e601 [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 Glass97011e12019-07-08 14:25:23 -060012import entry
Simon Glass8f521362017-11-12 21:52:21 -070013import fdt
14import fdt_util
15import tools
16
Simon Glass57454f42016-11-25 20:15:52 -070017class TestEntry(unittest.TestCase):
Simon Glass752e7552018-10-01 21:12:41 -060018 def setUp(self):
19 tools.PrepareOutputDir(None)
20
21 def tearDown(self):
22 tools.FinaliseOutputDir()
23
Simon Glass8f521362017-11-12 21:52:21 -070024 def GetNode(self):
25 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass8f521362017-11-12 21:52:21 -070026 fname = fdt_util.EnsureCompiled(
Simon Glass511f6582018-10-01 12:22:30 -060027 os.path.join(binman_dir,('test/005_simple.dts')))
Simon Glass8f521362017-11-12 21:52:21 -070028 dtb = fdt.FdtScan(fname)
29 return dtb.GetNode('/binman/u-boot')
30
Simon Glass97011e12019-07-08 14:25:23 -060031 def _ReloadEntry(self):
Simon Glass8f521362017-11-12 21:52:21 -070032 global entry
Simon Glass7057d022018-10-01 21:12:47 -060033 if entry:
Simon Glasscb7b8d72019-05-17 22:00:47 -060034 if sys.version_info[0] >= 3:
35 import importlib
36 importlib.reload(entry)
37 else:
38 reload(entry)
Simon Glass7057d022018-10-01 21:12:47 -060039 else:
40 import entry
Simon Glass97011e12019-07-08 14:25:23 -060041
42 def test1EntryNoImportLib(self):
43 """Test that we can import Entry subclassess successfully"""
44 sys.modules['importlib'] = None
45 global entry
46 self._ReloadEntry()
47 entry.Entry.Create(None, self.GetNode(), 'u-boot')
48 self.assertFalse(entry.have_importlib)
49
50 def test2EntryImportLib(self):
51 del sys.modules['importlib']
52 global entry
53 self._ReloadEntry()
Simon Glass8f521362017-11-12 21:52:21 -070054 entry.Entry.Create(None, self.GetNode(), 'u-boot-spl')
Simon Glass97011e12019-07-08 14:25:23 -060055 self.assertTrue(entry.have_importlib)
Simon Glass8f521362017-11-12 21:52:21 -070056
Simon Glass57454f42016-11-25 20:15:52 -070057 def testEntryContents(self):
58 """Test the Entry bass class"""
Simon Glassb3393262017-11-12 21:52:20 -070059 import entry
Simon Glass2c360cf2019-07-20 12:23:45 -060060 base_entry = entry.Entry(None, None, None)
Simon Glass57454f42016-11-25 20:15:52 -070061 self.assertEqual(True, base_entry.ObtainContents())
62
63 def testUnknownEntry(self):
64 """Test that unknown entry types are detected"""
Simon Glass57454f42016-11-25 20:15:52 -070065 Node = collections.namedtuple('Node', ['name', 'path'])
66 node = Node('invalid-name', 'invalid-path')
67 with self.assertRaises(ValueError) as e:
68 entry.Entry.Create(None, node, node.name)
69 self.assertIn("Unknown entry type 'invalid-name' in node "
70 "'invalid-path'", str(e.exception))
Simon Glassac599912017-11-12 21:52:22 -070071
Simon Glass639505b2018-09-14 04:57:11 -060072 def testUniqueName(self):
73 """Test Entry.GetUniqueName"""
Simon Glass639505b2018-09-14 04:57:11 -060074 Node = collections.namedtuple('Node', ['name', 'parent'])
75 base_node = Node('root', None)
Simon Glass2c360cf2019-07-20 12:23:45 -060076 base_entry = entry.Entry(None, None, base_node)
Simon Glass639505b2018-09-14 04:57:11 -060077 self.assertEqual('root', base_entry.GetUniqueName())
78 sub_node = Node('subnode', base_node)
Simon Glass2c360cf2019-07-20 12:23:45 -060079 sub_entry = entry.Entry(None, None, sub_node)
Simon Glass639505b2018-09-14 04:57:11 -060080 self.assertEqual('root.subnode', sub_entry.GetUniqueName())
81
Simon Glass3732ec32018-09-14 04:57:18 -060082 def testGetDefaultFilename(self):
83 """Trivial test for this base class function"""
Simon Glass2c360cf2019-07-20 12:23:45 -060084 base_entry = entry.Entry(None, None, None)
Simon Glass3732ec32018-09-14 04:57:18 -060085 self.assertIsNone(base_entry.GetDefaultFilename())
Simon Glassac599912017-11-12 21:52:22 -070086
Simon Glass684a4f12019-07-20 12:23:31 -060087 def testBlobFdt(self):
88 """Test the GetFdtEtype() method of the blob-dtb entries"""
89 base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
90 self.assertIsNone(base.GetFdtEtype())
91
92 dtb = entry.Entry.Create(None, self.GetNode(), 'u-boot-dtb')
93 self.assertEqual('u-boot-dtb', dtb.GetFdtEtype())
94
Simon Glass37fdd142019-07-20 12:24:06 -060095 def testWriteChildData(self):
96 """Test the WriteChildData() method of the base class"""
97 base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
98 self.assertTrue(base.WriteChildData(base))
99
Simon Glassb9ba4e02019-08-24 07:22:44 -0600100 def testReadChildData(self):
101 """Test the ReadChildData() method of the base class"""
102 base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
103 self.assertIsNone(base.ReadChildData(base))
104
Simon Glass684a4f12019-07-20 12:23:31 -0600105
Simon Glassac599912017-11-12 21:52:22 -0700106if __name__ == "__main__":
107 unittest.main()