blob: 277e10b5859f493361f08961d45b9bccdaf8ce20 [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 Glass934cdcf2017-11-12 21:52:21 -07008import os
9import sys
Simon Glass4f443042016-11-25 20:15:52 -070010import unittest
11
Simon Glasse90fe1b2019-07-08 14:25:23 -060012import entry
Simon Glass934cdcf2017-11-12 21:52:21 -070013import fdt
14import fdt_util
15import tools
16
Simon Glass4f443042016-11-25 20:15:52 -070017class TestEntry(unittest.TestCase):
Simon Glasse0e62752018-10-01 21:12:41 -060018 def setUp(self):
19 tools.PrepareOutputDir(None)
20
21 def tearDown(self):
22 tools.FinaliseOutputDir()
23
Simon Glass934cdcf2017-11-12 21:52:21 -070024 def GetNode(self):
25 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass934cdcf2017-11-12 21:52:21 -070026 fname = fdt_util.EnsureCompiled(
Simon Glass741f2d62018-10-01 12:22:30 -060027 os.path.join(binman_dir,('test/005_simple.dts')))
Simon Glass934cdcf2017-11-12 21:52:21 -070028 dtb = fdt.FdtScan(fname)
29 return dtb.GetNode('/binman/u-boot')
30
Simon Glasse90fe1b2019-07-08 14:25:23 -060031 def _ReloadEntry(self):
Simon Glass934cdcf2017-11-12 21:52:21 -070032 global entry
Simon Glass11ae93e2018-10-01 21:12:47 -060033 if entry:
Simon Glass0199d192019-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 Glass11ae93e2018-10-01 21:12:47 -060039 else:
40 import entry
Simon Glasse90fe1b2019-07-08 14:25:23 -060041
Simon Glass4f443042016-11-25 20:15:52 -070042 def testEntryContents(self):
43 """Test the Entry bass class"""
Simon Glass4d5994f2017-11-12 21:52:20 -070044 import entry
Simon Glassc6bd6e22019-07-20 12:23:45 -060045 base_entry = entry.Entry(None, None, None)
Simon Glass4f443042016-11-25 20:15:52 -070046 self.assertEqual(True, base_entry.ObtainContents())
47
48 def testUnknownEntry(self):
49 """Test that unknown entry types are detected"""
Simon Glass4f443042016-11-25 20:15:52 -070050 Node = collections.namedtuple('Node', ['name', 'path'])
51 node = Node('invalid-name', 'invalid-path')
52 with self.assertRaises(ValueError) as e:
53 entry.Entry.Create(None, node, node.name)
54 self.assertIn("Unknown entry type 'invalid-name' in node "
55 "'invalid-path'", str(e.exception))
Simon Glass9fc60b42017-11-12 21:52:22 -070056
Simon Glassa326b492018-09-14 04:57:11 -060057 def testUniqueName(self):
58 """Test Entry.GetUniqueName"""
Simon Glassa326b492018-09-14 04:57:11 -060059 Node = collections.namedtuple('Node', ['name', 'parent'])
60 base_node = Node('root', None)
Simon Glassc6bd6e22019-07-20 12:23:45 -060061 base_entry = entry.Entry(None, None, base_node)
Simon Glassa326b492018-09-14 04:57:11 -060062 self.assertEqual('root', base_entry.GetUniqueName())
63 sub_node = Node('subnode', base_node)
Simon Glassc6bd6e22019-07-20 12:23:45 -060064 sub_entry = entry.Entry(None, None, sub_node)
Simon Glassa326b492018-09-14 04:57:11 -060065 self.assertEqual('root.subnode', sub_entry.GetUniqueName())
66
Simon Glass6c234bf2018-09-14 04:57:18 -060067 def testGetDefaultFilename(self):
68 """Trivial test for this base class function"""
Simon Glassc6bd6e22019-07-20 12:23:45 -060069 base_entry = entry.Entry(None, None, None)
Simon Glass6c234bf2018-09-14 04:57:18 -060070 self.assertIsNone(base_entry.GetDefaultFilename())
Simon Glass9fc60b42017-11-12 21:52:22 -070071
Simon Glass4bdd3002019-07-20 12:23:31 -060072 def testBlobFdt(self):
73 """Test the GetFdtEtype() method of the blob-dtb entries"""
74 base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
75 self.assertIsNone(base.GetFdtEtype())
76
77 dtb = entry.Entry.Create(None, self.GetNode(), 'u-boot-dtb')
78 self.assertEqual('u-boot-dtb', dtb.GetFdtEtype())
79
Simon Glasseb0f4a42019-07-20 12:24:06 -060080 def testWriteChildData(self):
81 """Test the WriteChildData() method of the base class"""
82 base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
83 self.assertTrue(base.WriteChildData(base))
84
Simon Glass8dbb7442019-08-24 07:22:44 -060085 def testReadChildData(self):
86 """Test the ReadChildData() method of the base class"""
87 base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
88 self.assertIsNone(base.ReadChildData(base))
89
Simon Glass4bdd3002019-07-20 12:23:31 -060090
Simon Glass9fc60b42017-11-12 21:52:22 -070091if __name__ == "__main__":
92 unittest.main()