elf_test.py 3.9 KB

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