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