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
7 from contextlib import contextmanager
13 from StringIO import StringIO
15 from io import StringIO
19 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
21 # Use this to suppress stdout/stderr output:
22 # with capture_sys_output() as (stdout, stderr)
25 def capture_sys_output():
26 capture_out, capture_err = StringIO(), StringIO()
27 old_out, old_err = sys.stdout, sys.stderr
29 sys.stdout, sys.stderr = capture_out, capture_err
30 yield capture_out, capture_err
32 sys.stdout, sys.stderr = old_out, old_err
36 def __init__(self, contents_size):
37 self.contents_size = contents_size
38 self.data = 'a' * contents_size
44 def __init__(self, sym_value=1):
45 self.sym_value = sym_value
50 def LookupSymbol(self, name, weak, msg):
53 class TestElf(unittest.TestCase):
54 def testAllSymbols(self):
55 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
56 syms = elf.GetSymbols(fname, [])
57 self.assertIn('.ucode', syms)
59 def testRegexSymbols(self):
60 fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
61 syms = elf.GetSymbols(fname, ['ucode'])
62 self.assertIn('.ucode', syms)
63 syms = elf.GetSymbols(fname, ['missing'])
64 self.assertNotIn('.ucode', syms)
65 syms = elf.GetSymbols(fname, ['missing', 'ucode'])
66 self.assertIn('.ucode', syms)
68 def testMissingFile(self):
70 section = FakeSection()
71 with self.assertRaises(ValueError) as e:
72 syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
73 self.assertIn("Filename 'missing-file' not found in input path",
76 def testOutsideFile(self):
78 section = FakeSection()
79 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
80 with self.assertRaises(ValueError) as e:
81 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
82 self.assertIn('entry_path has offset 4 (size 8) but the contents size '
83 'is a', str(e.exception))
85 def testMissingImageStart(self):
87 section = FakeSection()
88 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
89 self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
92 def testBadSymbolSize(self):
94 section = FakeSection()
95 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
96 with self.assertRaises(ValueError) as e:
97 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
98 self.assertIn('has size 1: only 4 and 8 are supported',
101 def testNoValue(self):
102 entry = FakeEntry(20)
103 section = FakeSection(sym_value=None)
104 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
105 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
106 self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data)
110 entry = FakeEntry(20)
111 section = FakeSection()
112 elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
113 with capture_sys_output() as (stdout, stderr):
114 syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
116 self.assertTrue(len(stdout.getvalue()) > 0)
119 if __name__ == '__main__':