2018-05-06 17:58:06 -04:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2016-11-25 20:15:52 -07:00
|
|
|
# Copyright (c) 2016 Google, Inc
|
|
|
|
# Written by Simon Glass <sjg@chromium.org>
|
|
|
|
#
|
|
|
|
# Test for the Entry class
|
|
|
|
|
|
|
|
import collections
|
2022-02-11 13:23:21 -07:00
|
|
|
import importlib
|
2017-11-12 21:52:21 -07:00
|
|
|
import os
|
|
|
|
import sys
|
2016-11-25 20:15:52 -07:00
|
|
|
import unittest
|
|
|
|
|
2020-04-17 18:09:03 -06:00
|
|
|
from binman import entry
|
2021-11-23 21:09:49 -07:00
|
|
|
from binman.etype.blob import Entry_blob
|
2020-04-17 18:09:03 -06:00
|
|
|
from dtoc import fdt
|
|
|
|
from dtoc import fdt_util
|
2023-02-23 18:18:04 -07:00
|
|
|
from u_boot_pylib import tools
|
2017-11-12 21:52:21 -07:00
|
|
|
|
2016-11-25 20:15:52 -07:00
|
|
|
class TestEntry(unittest.TestCase):
|
2018-10-01 21:12:41 -06:00
|
|
|
def setUp(self):
|
2022-01-29 14:14:04 -07:00
|
|
|
tools.prepare_output_dir(None)
|
2018-10-01 21:12:41 -06:00
|
|
|
|
|
|
|
def tearDown(self):
|
2022-01-29 14:14:04 -07:00
|
|
|
tools.finalise_output_dir()
|
2018-10-01 21:12:41 -06:00
|
|
|
|
2017-11-12 21:52:21 -07:00
|
|
|
def GetNode(self):
|
|
|
|
binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
|
|
|
|
fname = fdt_util.EnsureCompiled(
|
2018-10-01 12:22:30 -06:00
|
|
|
os.path.join(binman_dir,('test/005_simple.dts')))
|
2017-11-12 21:52:21 -07:00
|
|
|
dtb = fdt.FdtScan(fname)
|
|
|
|
return dtb.GetNode('/binman/u-boot')
|
|
|
|
|
2019-07-08 14:25:23 -06:00
|
|
|
def _ReloadEntry(self):
|
2017-11-12 21:52:21 -07:00
|
|
|
global entry
|
2018-10-01 21:12:47 -06:00
|
|
|
if entry:
|
2022-02-11 13:23:21 -07:00
|
|
|
importlib.reload(entry)
|
2018-10-01 21:12:47 -06:00
|
|
|
else:
|
2020-04-17 18:09:03 -06:00
|
|
|
from binman import entry
|
2019-07-08 14:25:23 -06:00
|
|
|
|
2016-11-25 20:15:52 -07:00
|
|
|
def testEntryContents(self):
|
|
|
|
"""Test the Entry bass class"""
|
2020-04-17 18:09:03 -06:00
|
|
|
from binman import entry
|
2019-07-20 12:23:45 -06:00
|
|
|
base_entry = entry.Entry(None, None, None)
|
2016-11-25 20:15:52 -07:00
|
|
|
self.assertEqual(True, base_entry.ObtainContents())
|
|
|
|
|
|
|
|
def testUnknownEntry(self):
|
|
|
|
"""Test that unknown entry types are detected"""
|
|
|
|
Node = collections.namedtuple('Node', ['name', 'path'])
|
|
|
|
node = Node('invalid-name', 'invalid-path')
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
entry.Entry.Create(None, node, node.name)
|
|
|
|
self.assertIn("Unknown entry type 'invalid-name' in node "
|
|
|
|
"'invalid-path'", str(e.exception))
|
2017-11-12 21:52:22 -07:00
|
|
|
|
2018-09-14 04:57:11 -06:00
|
|
|
def testUniqueName(self):
|
|
|
|
"""Test Entry.GetUniqueName"""
|
|
|
|
Node = collections.namedtuple('Node', ['name', 'parent'])
|
|
|
|
base_node = Node('root', None)
|
2019-07-20 12:23:45 -06:00
|
|
|
base_entry = entry.Entry(None, None, base_node)
|
2018-09-14 04:57:11 -06:00
|
|
|
self.assertEqual('root', base_entry.GetUniqueName())
|
|
|
|
sub_node = Node('subnode', base_node)
|
2019-07-20 12:23:45 -06:00
|
|
|
sub_entry = entry.Entry(None, None, sub_node)
|
2018-09-14 04:57:11 -06:00
|
|
|
self.assertEqual('root.subnode', sub_entry.GetUniqueName())
|
|
|
|
|
2018-09-14 04:57:18 -06:00
|
|
|
def testGetDefaultFilename(self):
|
|
|
|
"""Trivial test for this base class function"""
|
2019-07-20 12:23:45 -06:00
|
|
|
base_entry = entry.Entry(None, None, None)
|
2018-09-14 04:57:18 -06:00
|
|
|
self.assertIsNone(base_entry.GetDefaultFilename())
|
2017-11-12 21:52:22 -07:00
|
|
|
|
2019-07-20 12:23:31 -06:00
|
|
|
def testBlobFdt(self):
|
|
|
|
"""Test the GetFdtEtype() method of the blob-dtb entries"""
|
|
|
|
base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
|
|
|
|
self.assertIsNone(base.GetFdtEtype())
|
|
|
|
|
|
|
|
dtb = entry.Entry.Create(None, self.GetNode(), 'u-boot-dtb')
|
|
|
|
self.assertEqual('u-boot-dtb', dtb.GetFdtEtype())
|
|
|
|
|
2019-07-20 12:24:06 -06:00
|
|
|
def testWriteChildData(self):
|
|
|
|
"""Test the WriteChildData() method of the base class"""
|
|
|
|
base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
|
|
|
|
self.assertTrue(base.WriteChildData(base))
|
|
|
|
|
2019-08-24 07:22:44 -06:00
|
|
|
def testReadChildData(self):
|
|
|
|
"""Test the ReadChildData() method of the base class"""
|
|
|
|
base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
|
|
|
|
self.assertIsNone(base.ReadChildData(base))
|
|
|
|
|
2021-03-18 20:25:04 +13:00
|
|
|
def testExpandedEntry(self):
|
|
|
|
"""Test use of an expanded entry when available"""
|
|
|
|
base = entry.Entry.Create(None, self.GetNode())
|
|
|
|
self.assertEqual('u-boot', base.etype)
|
|
|
|
|
|
|
|
expanded = entry.Entry.Create(None, self.GetNode(), expanded=True)
|
|
|
|
self.assertEqual('u-boot-expanded', expanded.etype)
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
entry.Entry.Create(None, self.GetNode(), 'missing', expanded=True)
|
|
|
|
self.assertIn("Unknown entry type 'missing' in node '/binman/u-boot'",
|
|
|
|
str(e.exception))
|
2019-07-20 12:23:31 -06:00
|
|
|
|
2021-11-23 21:09:49 -07:00
|
|
|
def testMissingEtype(self):
|
|
|
|
"""Test use of a blob etype when the requested one is not available"""
|
|
|
|
ent = entry.Entry.Create(None, self.GetNode(), 'missing',
|
|
|
|
missing_etype=True)
|
|
|
|
self.assertTrue(isinstance(ent, Entry_blob))
|
|
|
|
self.assertEquals('missing', ent.etype)
|
|
|
|
|
2022-08-19 16:25:31 +02:00
|
|
|
def testDecompressData(self):
|
|
|
|
"""Test the DecompressData() method of the base class"""
|
|
|
|
base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
|
|
|
|
base.compress = 'lz4'
|
|
|
|
bintools = {}
|
|
|
|
base.comp_bintool = base.AddBintool(bintools, '_testing')
|
|
|
|
self.assertEquals(tools.get_bytes(0, 1024), base.CompressData(b'abc'))
|
|
|
|
self.assertEquals(tools.get_bytes(0, 1024), base.DecompressData(b'abc'))
|
|
|
|
|
2023-01-23 11:29:41 -07:00
|
|
|
def testLookupOffset(self):
|
|
|
|
"""Test the lookup_offset() method of the base class"""
|
|
|
|
def MyFindEntryByNode(node):
|
|
|
|
return self.found
|
|
|
|
|
|
|
|
base = entry.Entry.Create(None, self.GetNode(), 'blob-dtb')
|
|
|
|
base.FindEntryByNode = MyFindEntryByNode
|
|
|
|
base.section = base
|
|
|
|
self.found = None
|
|
|
|
base.offset_from_elf = [self.GetNode(), 'start', 0]
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
base.lookup_offset()
|
|
|
|
self.assertIn("Cannot find entry for node 'u-boot'", str(e.exception))
|
|
|
|
|
|
|
|
self.found = base
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
base.lookup_offset()
|
|
|
|
self.assertIn("Need elf-fname property 'u-boot'", str(e.exception))
|
|
|
|
|
2021-11-23 21:09:49 -07:00
|
|
|
|
2017-11-12 21:52:22 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|