]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
binman: Complete elf test coverage
authorSimon Glass <sjg@chromium.org>
Sat, 19 Mar 2022 01:19:49 +0000 (19:19 -0600)
committerSimon Glass <sjg@chromium.org>
Sat, 19 Mar 2022 01:21:49 +0000 (19:21 -0600)
Add coverage for the new elf functions needed for the event_dump.py
script.

Signed-off-by: Simon Glass <sjg@chromium.org>
tools/binman/elf.py
tools/binman/elf_test.py

index 35971731d05ee1bf77d7e32c37271dee60d6096d..f24ccd7fb93dc3e6f4e721425e3a1c2dd4a9be7a 100644 (file)
@@ -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)
index 5084838b910d1c394bb329cff225ed697780379b..d401b5b52d37ecc411860d049ef3b9a27ce95d51 100644 (file)
@@ -284,6 +284,54 @@ class TestElf(unittest.TestCase):
             elf.read_segments(tools.get_bytes(100, 100))
         self.assertIn('Magic number does not match', str(e.exception))
 
+    def test_get_file_offset(self):
+        """Test GetFileOffset() gives the correct file offset for a symbol"""
+        fname = self.ElfTestFile('embed_data')
+        syms = elf.GetSymbols(fname, ['embed'])
+        addr = syms['embed'].address
+        offset = elf.GetFileOffset(fname, addr)
+        data = tools.read_file(fname)
+
+        # Just use the first 4 bytes and assume it is little endian
+        embed_data = data[offset:offset + 4]
+        embed_value = struct.unpack('<I', embed_data)[0]
+        self.assertEqual(0x1234, embed_value)
+
+    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('elf_sections')
+        sym_name = 'calculate'
+        syms = elf.GetSymbols(fname, [sym_name])
+        addr = syms[sym_name].address
+        sym = elf.GetSymbolFromAddress(fname, addr)
+        self.assertEqual(sym_name, 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()