# 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 importlib
import os
import sys
import unittest

from binman import entry
from binman.etype.blob import Entry_blob
from dtoc import fdt
from dtoc import fdt_util
from u_boot_pylib import tools

class TestEntry(unittest.TestCase):
    def setUp(self):
        tools.prepare_output_dir(None)

    def tearDown(self):
        tools.finalise_output_dir()

    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:
            importlib.reload(entry)
        else:
            from binman import entry

    def testEntryContents(self):
        """Test the Entry bass class"""
        from binman 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))

    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))

    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))

    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.assertEqual('missing', ent.etype)

    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.assertEqual(tools.get_bytes(0, 1024), base.CompressData(b'abc'))
        self.assertEqual(tools.get_bytes(0, 1024), base.DecompressData(b'abc'))

    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))


if __name__ == "__main__":
    unittest.main()
