[PATCH] binman: Complete elf test coverage

Simon Glass sjg at chromium.org
Sat Mar 19 00:24:29 CET 2022


Add coverage for the new elf functions needed for the event_dump.py
script.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 tools/binman/elf.py      |  4 ++--
 tools/binman/elf_test.py | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/tools/binman/elf.py b/tools/binman/elf.py
index 940a476b931..afa05e58fdd 100644
--- a/tools/binman/elf.py
+++ b/tools/binman/elf.py
@@ -112,7 +112,7 @@ def GetFileOffset(fname, addr):
         int: Offset of that address in the ELF file, or None if not valid
     """
     if not ELF_TOOLS:
-        raise ValueError('Python elftools package is not available')
+        raise ValueError("Python: No module named 'elftools'")
     with open(fname, 'rb') as fd:
         elf = ELFFile(fd)
         return _GetFileOffset(elf, addr)
@@ -128,7 +128,7 @@ def GetSymbolFromAddress(fname, addr):
         str: Symbol name, or None if no symbol at that address
     """
     if not ELF_TOOLS:
-        raise ValueError('Python elftools package is not available')
+        raise ValueError("Python: No module named 'elftools'")
     with open(fname, 'rb') as fd:
         elf = ELFFile(fd)
         syms = GetSymbols(fname, None)
diff --git a/tools/binman/elf_test.py b/tools/binman/elf_test.py
index 555117d33ac..9d4fb3aa3b3 100644
--- a/tools/binman/elf_test.py
+++ b/tools/binman/elf_test.py
@@ -284,6 +284,43 @@ class TestElf(unittest.TestCase):
             elf.read_loadable_segments(tools.get_bytes(100, 100))
         self.assertIn('Magic number does not match', str(e.exception))
 
+    def test_get_file_offset(self):
+        """Test GetFileOffset()"""
+        fname = self.ElfTestFile('embed_data')
+        self.assertEqual(0x1000, elf.GetFileOffset(fname, 0))
+
+    def test_get_file_offset_fail(self):
+        """Test calling GetFileOffset() without elftools"""
+        try:
+            old_val = elf.ELF_TOOLS
+            elf.ELF_TOOLS = False
+            fname = self.ElfTestFile('embed_data')
+            with self.assertRaises(ValueError) as e:
+                elf.GetFileOffset(fname, 0)
+            self.assertIn("Python: No module named 'elftools'",
+                      str(e.exception))
+        finally:
+            elf.ELF_TOOLS = old_val
+
+    def test_get_symbol_from_address(self):
+        """Test GetSymbolFromAddress()"""
+        fname = self.ElfTestFile('embed_data')
+        sym = elf.GetSymbolFromAddress(fname, 0x1000)
+        self.assertEqual('main', sym)
+
+    def test_get_symbol_from_address_fail(self):
+        """Test calling GetSymbolFromAddress() without elftools"""
+        try:
+            old_val = elf.ELF_TOOLS
+            elf.ELF_TOOLS = False
+            fname = self.ElfTestFile('embed_data')
+            with self.assertRaises(ValueError) as e:
+                elf.GetSymbolFromAddress(fname, 0x1000)
+            self.assertIn("Python: No module named 'elftools'",
+                          str(e.exception))
+        finally:
+            elf.ELF_TOOLS = old_val
+
 
 if __name__ == '__main__':
     unittest.main()
-- 
2.35.1.894.gb6a874cedc-goog



More information about the U-Boot mailing list