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 7from contextlib import contextmanager 8import os 9import sys 10import unittest 11 12try: 13 from StringIO import StringIO 14except ImportError: 15 from io import StringIO 16 17import elf 18 19binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) 20 21# Use this to suppress stdout/stderr output: 22# with capture_sys_output() as (stdout, stderr) 23# ...do something... 24@contextmanager 25def 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 35class 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 43class FakeSection: 44 def __init__(self, sym_value=1): 45 self.sym_value = sym_value 46 47 def GetPath(self): 48 return 'section_path' 49 50 def LookupSymbol(self, name, weak, msg): 51 return self.sym_value 52 53class 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) 58 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) 67 68 def testMissingFile(self): 69 entry = FakeEntry(10) 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", 74 str(e.exception)) 75 76 def testOutsideFile(self): 77 entry = FakeEntry(10) 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)) 84 85 def testMissingImageStart(self): 86 entry = FakeEntry(10) 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), 90 None) 91 92 def testBadSymbolSize(self): 93 entry = FakeEntry(10) 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', 99 str(e.exception)) 100 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) 107 108 def testDebug(self): 109 elf.debug = True 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) 115 elf.debug = False 116 self.assertTrue(len(stdout.getvalue()) > 0) 117 118 119if __name__ == '__main__': 120 unittest.main() 121