u-boot/tools/binman/entry_test.py
Simon Glass eb0f4a4cb4 binman: Support replacing data in a cbfs
At present binman cannot replace data within a CBFS since it does not
allow rewriting of the files in that CBFS. Implement this by using the
new WriteData() method to handle the case.

Add a header to compressed data so that the amount of compressed data can
be determined without reference to the size of the containing entry. This
allows the entry to be larger that the contents, without causing errors in
decompression. This is necessary to cope with a compressed device tree
being updated in such a way that it shrinks after the entry size is
already set (an obscure case). It is not used with CBFS since it has its
own metadata for this. Increase the number of passes allowed to resolve
the position of entries, to handle this case.

Add a test for this new logic.

Signed-off-by: Simon Glass <sjg@chromium.org>
2019-07-29 09:38:06 -06:00

102 lines
3.3 KiB
Python

# SPDX-License-Identifier: GPL-2.0+
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# Test for the Entry class
import collections
import os
import sys
import unittest
import entry
import fdt
import fdt_util
import tools
class TestEntry(unittest.TestCase):
def setUp(self):
tools.PrepareOutputDir(None)
def tearDown(self):
tools.FinaliseOutputDir()
def GetNode(self):
binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
fname = fdt_util.EnsureCompiled(
os.path.join(binman_dir,('test/005_simple.dts')))
dtb = fdt.FdtScan(fname)
return dtb.GetNode('/binman/u-boot')
def _ReloadEntry(self):
global entry
if entry:
if sys.version_info[0] >= 3:
import importlib
importlib.reload(entry)
else:
reload(entry)
else:
import entry
def test1EntryNoImportLib(self):
"""Test that we can import Entry subclassess successfully"""
sys.modules['importlib'] = None
global entry
self._ReloadEntry()
entry.Entry.Create(None, self.GetNode(), 'u-boot')
self.assertFalse(entry.have_importlib)
def test2EntryImportLib(self):
del sys.modules['importlib']
global entry
self._ReloadEntry()
entry.Entry.Create(None, self.GetNode(), 'u-boot-spl')
self.assertTrue(entry.have_importlib)
def testEntryContents(self):
"""Test the Entry bass class"""
import entry
base_entry = entry.Entry(None, None, None)
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))
def testUniqueName(self):
"""Test Entry.GetUniqueName"""
Node = collections.namedtuple('Node', ['name', 'parent'])
base_node = Node('root', None)
base_entry = entry.Entry(None, None, base_node)
self.assertEqual('root', base_entry.GetUniqueName())
sub_node = Node('subnode', base_node)
sub_entry = entry.Entry(None, None, sub_node)
self.assertEqual('root.subnode', sub_entry.GetUniqueName())
def testGetDefaultFilename(self):
"""Trivial test for this base class function"""
base_entry = entry.Entry(None, None, None)
self.assertIsNone(base_entry.GetDefaultFilename())
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())
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))
if __name__ == "__main__":
unittest.main()