Fix for mixed version loclists, tests (#521)
[pyelftools.git] / test / test_corrupt_files.py
1 """
2 Test that elftools does not fail to load corrupted ELF files
3 """
4 import unittest
5 import os
6
7 from elftools.elf.elffile import ELFFile
8 from elftools.common.exceptions import ELFParseError
9
10
11 class TestCorruptFile(unittest.TestCase):
12 def test_elffile_init(self):
13 """ Test that ELFFile does not crash when parsing an ELF file with corrupt e_shoff and/or e_shnum
14 """
15 filepath = os.path.join('test', 'testfiles_for_unittests', 'corrupt_sh.elf')
16 with open(filepath, 'rb') as f:
17 elf = None
18
19 try:
20 elf = ELFFile(f)
21 except ELFParseError:
22 pass
23
24 self.assertIsInstance(elf, ELFFile, "ELFFile initialization should have detected the out of bounds read")
25
26
27 if __name__ == '__main__':
28 unittest.main()