Rebuild readelf locally and add more instructions
[pyelftools.git] / test / test_double_dynstr_section.py
1 #------------------------------------------------------------------------------
2 # elftools tests
3 #
4 # Yann Rouillard (yann@pleiades.fr.eu.org)
5 # This code is in the public domain
6 #------------------------------------------------------------------------------
7 import unittest
8 import os
9
10 from elftools.elf.elffile import ELFFile
11 from elftools.elf.dynamic import DynamicSection, DynamicTag
12
13
14 class TestDoubleDynstrSections(unittest.TestCase):
15 """ This test make sure than dynamic tags
16 are properly analyzed when two .dynstr
17 sections are present in an elf file
18 """
19
20 reference_data = [
21 'libz.so.1',
22 'libc.so.6',
23 'lib_versioned.so.1',
24 ]
25
26 def _test_double_dynstr_section_generic(self, testfile):
27
28 with open(os.path.join('test', 'testfiles_for_unittests', testfile),
29 'rb') as f:
30 elf = ELFFile(f)
31 for section in elf.iter_sections():
32 if isinstance(section, DynamicSection):
33 d_tags = [getattr(x, x.entry.d_tag[3:].lower())
34 for x in section.iter_tags()
35 if x.entry.d_tag in DynamicTag._HANDLED_TAGS]
36 self.assertListEqual(
37 TestDoubleDynstrSections.reference_data,
38 d_tags)
39 return
40 self.fail('No dynamic section found !!')
41
42
43 def test_double_dynstr_section(self):
44 """ First test with the good dynstr section first
45 """
46 self._test_double_dynstr_section_generic(
47 'lib_with_two_dynstr_sections.so.1.elf')
48
49 def test_double_dynstr_section_reverse(self):
50 """ Second test with the good dynstr section last
51 """
52 self._test_double_dynstr_section_generic(
53 'lib_with_two_dynstr_sections_reversed.so.1.elf')
54
55
56 if __name__ == '__main__':
57 unittest.main()