1"""
2Test that elftools does not fail to load debug symbol ELF files
3"""
4import unittest
5import os
6
7from elftools.elf.elffile import ELFFile, DynamicSection
8from elftools.dwarf.callframe import ZERO
9
10class TestDBGFile(unittest.TestCase):
11    def test_dynamic_segment(self):
12        """ Test that the degenerate case for the dynamic segment does not crash
13        """
14        with open(os.path.join('test', 'testfiles_for_unittests',
15                               'debug_info.elf'), 'rb') as f:
16            elf = ELFFile(f)
17
18            seen_dynamic_segment = False
19            for segment in elf.iter_segments():
20                if segment.header.p_type == 'PT_DYNAMIC':
21                    self.assertEqual(segment.num_tags(), 0, "The dynamic segment in this file should be empty")
22                    seen_dynamic_segment = True
23                    break
24
25            self.assertTrue(seen_dynamic_segment, "There should be a dynamic segment in this file")
26
27    def test_dynamic_section(self):
28        """ Test that the degenerate case for the dynamic section does not crash
29        """
30        with open(os.path.join('test', 'testfiles_for_unittests',
31                               'debug_info.elf'), 'rb') as f:
32            elf = ELFFile(f)
33            section = DynamicSection(elf.get_section_by_name('.dynamic').header, '.dynamic', elf)
34
35            self.assertEqual(section.num_tags(), 0, "The dynamic section in this file should be empty")
36
37    def test_eh_frame(self):
38        """ Test that parsing .eh_frame with SHT_NOBITS does not crash
39        """
40        with open(os.path.join('test', 'testfiles_for_unittests',
41                               'debug_info.elf'), 'rb') as f:
42            elf = ELFFile(f)
43            dwarf = elf.get_dwarf_info()
44            eh_frame = list(dwarf.EH_CFI_entries())
45            self.assertEqual(len(eh_frame), 1, "There should only be the ZERO entry in eh_frame")
46            self.assertIs(type(eh_frame[0]), ZERO, "The only eh_frame entry should be the terminator")
47
48if __name__ == '__main__':
49    unittest.main()
50