1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2017 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
5 # Test for the elf module
14 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
18 def __init__(self, contents_size):
19 self.contents_size = contents_size
20 self.data = 'a' * contents_size
26 def __init__(self, sym_value=1):
27 self.sym_value = sym_value
32 def LookupSymbol(self, name, weak, msg):
35 class TestElf(unittest.TestCase):
36 def testAllSymbols(self):
37 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
38 syms = elf.GetSymbols(fname, [])
39 self.assertIn('.ucode', syms)
41 def testRegexSymbols(self):
42 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
43 syms = elf.GetSymbols(fname, ['ucode'])
44 self.assertIn('.ucode', syms)
45 syms = elf.GetSymbols(fname, ['missing'])
46 self.assertNotIn('.ucode', syms)
47 syms = elf.GetSymbols(fname, ['missing', 'ucode'])
48 self.assertIn('.ucode', syms)
50 def testMissingFile(self):
52 section = FakeSection()
53 with self.assertRaises(ValueError) as e:
54 syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
55 self.assertIn("Filename 'missing-file' not found in input path",
58 def testOutsideFile(self):
60 section = FakeSection()
61 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
62 with self.assertRaises(ValueError) as e:
63 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
64 self.assertIn('entry_path has offset 4 (size 8) but the contents size '
65 'is a', str(e.exception))
67 def testMissingImageStart(self):
69 section = FakeSection()
70 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
71 self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
74 def testBadSymbolSize(self):
76 section = FakeSection()
77 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
78 with self.assertRaises(ValueError) as e:
79 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
80 self.assertIn('has size 1: only 4 and 8 are supported',
83 def testNoValue(self):
85 section = FakeSection(sym_value=None)
86 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
87 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
88 self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data)
93 section = FakeSection()
94 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
95 with test_util.capture_sys_output() as (stdout, stderr):
96 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
98 self.assertTrue(len(stdout.getvalue()) > 0)
101 if __name__ == '__main__':