elf_test.py 3.4 KB

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