Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 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 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame^] | 9 | from contextlib import contextmanager |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 10 | import os |
| 11 | import sys |
| 12 | import unittest |
| 13 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame^] | 14 | try: |
| 15 | from StringIO import StringIO |
| 16 | except ImportError: |
| 17 | from io import StringIO |
| 18 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 19 | import elf |
| 20 | |
| 21 | binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame^] | 22 | |
| 23 | # Use this to suppress stdout/stderr output: |
| 24 | # with capture_sys_output() as (stdout, stderr) |
| 25 | # ...do something... |
| 26 | @contextmanager |
| 27 | def 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 | |
| 37 | class 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 | |
| 45 | class 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 |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 54 | |
| 55 | class TestElf(unittest.TestCase): |
| 56 | def testAllSymbols(self): |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame^] | 57 | fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr') |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 58 | syms = elf.GetSymbols(fname, []) |
| 59 | self.assertIn('.ucode', syms) |
| 60 | |
| 61 | def testRegexSymbols(self): |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame^] | 62 | fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr') |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 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 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame^] | 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 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 121 | if __name__ == '__main__': |
| 122 | unittest.main() |