elf_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. from contextlib import contextmanager
  7. import os
  8. import sys
  9. import unittest
  10. try:
  11. from StringIO import StringIO
  12. except ImportError:
  13. from io import StringIO
  14. import elf
  15. binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
  16. # Use this to suppress stdout/stderr output:
  17. # with capture_sys_output() as (stdout, stderr)
  18. # ...do something...
  19. @contextmanager
  20. def capture_sys_output():
  21. capture_out, capture_err = StringIO(), StringIO()
  22. old_out, old_err = sys.stdout, sys.stderr
  23. try:
  24. sys.stdout, sys.stderr = capture_out, capture_err
  25. yield capture_out, capture_err
  26. finally:
  27. sys.stdout, sys.stderr = old_out, old_err
  28. class FakeEntry:
  29. def __init__(self, contents_size):
  30. self.contents_size = contents_size
  31. self.data = 'a' * contents_size
  32. def GetPath(self):
  33. return 'entry_path'
  34. class FakeSection:
  35. def __init__(self, sym_value=1):
  36. self.sym_value = sym_value
  37. def GetPath(self):
  38. return 'section_path'
  39. def LookupSymbol(self, name, weak, msg):
  40. return self.sym_value
  41. class TestElf(unittest.TestCase):
  42. def testAllSymbols(self):
  43. fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
  44. syms = elf.GetSymbols(fname, [])
  45. self.assertIn('.ucode', syms)
  46. def testRegexSymbols(self):
  47. fname = os.path.join(binman_dir, 'test', 'u_boot_ucode_ptr')
  48. syms = elf.GetSymbols(fname, ['ucode'])
  49. self.assertIn('.ucode', syms)
  50. syms = elf.GetSymbols(fname, ['missing'])
  51. self.assertNotIn('.ucode', syms)
  52. syms = elf.GetSymbols(fname, ['missing', 'ucode'])
  53. self.assertIn('.ucode', syms)
  54. def testMissingFile(self):
  55. entry = FakeEntry(10)
  56. section = FakeSection()
  57. with self.assertRaises(ValueError) as e:
  58. syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
  59. self.assertIn("Filename 'missing-file' not found in input path",
  60. str(e.exception))
  61. def testOutsideFile(self):
  62. entry = FakeEntry(10)
  63. section = FakeSection()
  64. elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
  65. with self.assertRaises(ValueError) as e:
  66. syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
  67. self.assertIn('entry_path has offset 4 (size 8) but the contents size '
  68. 'is a', str(e.exception))
  69. def testMissingImageStart(self):
  70. entry = FakeEntry(10)
  71. section = FakeSection()
  72. elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_bad')
  73. self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
  74. None)
  75. def testBadSymbolSize(self):
  76. entry = FakeEntry(10)
  77. section = FakeSection()
  78. elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms_size')
  79. with self.assertRaises(ValueError) as e:
  80. syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
  81. self.assertIn('has size 1: only 4 and 8 are supported',
  82. str(e.exception))
  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. def testDebug(self):
  90. elf.debug = True
  91. entry = FakeEntry(20)
  92. section = FakeSection()
  93. elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
  94. with capture_sys_output() as (stdout, stderr):
  95. syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
  96. elf.debug = False
  97. self.assertTrue(len(stdout.getvalue()) > 0)
  98. if __name__ == '__main__':
  99. unittest.main()