1# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
5# Test for the Entry class
6
7import collections
8import os
9import sys
10import unittest
11
12from binman import entry
13from dtoc import fdt
14from dtoc import fdt_util
15from patman import tools
16
17class TestEntry(unittest.TestCase):
18    def setUp(self):
19        tools.PrepareOutputDir(None)
20
21    def tearDown(self):
22        tools.FinaliseOutputDir()
23
24    def GetNode(self):
25        binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
26        fname = fdt_util.EnsureCompiled(
27            os.path.join(binman_dir,('test/005_simple.dts')))
28        dtb = fdt.FdtScan(fname)
29        return dtb.GetNode('/binman/u-boot')
30
31    def _ReloadEntry(self):
32        global entry
33        if entry:
34            if sys.version_info[0] >= 3:
35                import importlib
36                importlib.reload(entry)
37            else:
38                reload(entry)
39        else:
40            from binman import entry
41
42    def testEntryContents(self):
43        """Test the Entry bass class"""
44        from binman import entry
45        base_entry = entry.Entry(None, None, None)
46        self.assertEqual(True, base_entry.ObtainContents())
47
48    def testUnknownEntry(self):
49        """Test that unknown entry types are detected"""
50        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))
56
57    def testUniqueName(self):
58        """Test Entry.GetUniqueName"""
59        Node = collections.namedtuple('Node', ['name', 'parent'])
60        base_node = Node('root', None)
61        base_entry = entry.Entry(None, None, base_node)
62        self.assertEqual('root', base_entry.GetUniqueName())
63        sub_node = Node('subnode', base_node)
64        sub_entry = entry.Entry(None, None, sub_node)
65        self.assertEqual('root.subnode', sub_entry.GetUniqueName())
66
67    def testGetDefaultFilename(self):
68        """Trivial test for this base class function"""
69        base_entry = entry.Entry(None, None, None)
70        self.assertIsNone(base_entry.GetDefaultFilename())
71
72    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
80    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
85    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
90
91if __name__ == "__main__":
92    unittest.main()
93