xref: /openbmc/u-boot/tools/binman/elf_test.py (revision 23eaf418)
1# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2017 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
5# Test for the elf module
6
7import os
8import sys
9import unittest
10
11import elf
12import test_util
13
14binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
15
16
17class FakeEntry:
18    def __init__(self, contents_size):
19        self.contents_size = contents_size
20        self.data = 'a' * contents_size
21
22    def GetPath(self):
23        return 'entry_path'
24
25class FakeSection:
26    def __init__(self, sym_value=1):
27        self.sym_value = sym_value
28
29    def GetPath(self):
30        return 'section_path'
31
32    def LookupSymbol(self, name, weak, msg):
33        return self.sym_value
34
35class 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)
40
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)
49
50    def testMissingFile(self):
51        entry = FakeEntry(10)
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",
56                      str(e.exception))
57
58    def testOutsideFile(self):
59        entry = FakeEntry(10)
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))
66
67    def testMissingImageStart(self):
68        entry = FakeEntry(10)
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),
72                         None)
73
74    def testBadSymbolSize(self):
75        entry = FakeEntry(10)
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',
81                      str(e.exception))
82
83    def testNoValue(self):
84        entry = FakeEntry(20)
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)
89
90    def testDebug(self):
91        elf.debug = True
92        entry = FakeEntry(20)
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)
97        elf.debug = False
98        self.assertTrue(len(stdout.getvalue()) > 0)
99
100
101if __name__ == '__main__':
102    unittest.main()
103