xref: /openbmc/u-boot/tools/binman/elf_test.py (revision 85231c08)
1#
2# Copyright (c) 2017 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
5# SPDX-License-Identifier:      GPL-2.0+
6#
7# Test for the elf module
8
9from contextlib import contextmanager
10import os
11import sys
12import unittest
13
14try:
15  from StringIO import StringIO
16except ImportError:
17  from io import StringIO
18
19import elf
20
21binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
22
23# Use this to suppress stdout/stderr output:
24# with capture_sys_output() as (stdout, stderr)
25#   ...do something...
26@contextmanager
27def capture_sys_output():
28  capture_out, capture_err = StringIO(), StringIO()
29  old_out, old_err = sys.stdout, sys.stderr
30  try:
31    sys.stdout, sys.stderr = capture_out, capture_err
32    yield capture_out, capture_err
33  finally:
34    sys.stdout, sys.stderr = old_out, old_err
35
36
37class FakeEntry:
38    def __init__(self, contents_size):
39        self.contents_size = contents_size
40        self.data = 'a' * contents_size
41
42    def GetPath(self):
43        return 'entry_path'
44
45class FakeImage:
46    def __init__(self, sym_value=1):
47        self.sym_value = sym_value
48
49    def GetPath(self):
50        return 'image_path'
51
52    def LookupSymbol(self, name, weak, msg):
53        return self.sym_value
54
55class TestElf(unittest.TestCase):
56    def testAllSymbols(self):
57        fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
58        syms = elf.GetSymbols(fname, [])
59        self.assertIn('.ucode', syms)
60
61    def testRegexSymbols(self):
62        fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
63        syms = elf.GetSymbols(fname, ['ucode'])
64        self.assertIn('.ucode', syms)
65        syms = elf.GetSymbols(fname, ['missing'])
66        self.assertNotIn('.ucode', syms)
67        syms = elf.GetSymbols(fname, ['missing', 'ucode'])
68        self.assertIn('.ucode', syms)
69
70    def testMissingFile(self):
71        entry = FakeEntry(10)
72        image = FakeImage()
73        with self.assertRaises(ValueError) as e:
74            syms = elf.LookupAndWriteSymbols('missing-file', entry, image)
75        self.assertIn("Filename 'missing-file' not found in input path",
76                      str(e.exception))
77
78    def testOutsideFile(self):
79        entry = FakeEntry(10)
80        image = FakeImage()
81        elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
82        with self.assertRaises(ValueError) as e:
83            syms = elf.LookupAndWriteSymbols(elf_fname, entry, image)
84        self.assertIn('entry_path has offset 4 (size 8) but the contents size '
85                      'is a', str(e.exception))
86
87    def testMissingImageStart(self):
88        entry = FakeEntry(10)
89        image = FakeImage()
90        elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
91        self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, image),
92                         None)
93
94    def testBadSymbolSize(self):
95        entry = FakeEntry(10)
96        image = FakeImage()
97        elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
98        with self.assertRaises(ValueError) as e:
99            syms = elf.LookupAndWriteSymbols(elf_fname, entry, image)
100        self.assertIn('has size 1: only 4 and 8 are supported',
101                      str(e.exception))
102
103    def testNoValue(self):
104        entry = FakeEntry(20)
105        image = FakeImage(sym_value=None)
106        elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
107        syms = elf.LookupAndWriteSymbols(elf_fname, entry, image)
108        self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data)
109
110    def testDebug(self):
111        elf.debug = True
112        entry = FakeEntry(20)
113        image = FakeImage()
114        elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
115        with capture_sys_output() as (stdout, stderr):
116            syms = elf.LookupAndWriteSymbols(elf_fname, entry, image)
117        elf.debug = False
118        self.assertTrue(len(stdout.getvalue()) > 0)
119
120
121if __name__ == '__main__':
122    unittest.main()
123