1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#-------------------------------------------------------------------------------
# elftools tests
#
# Eli Bendersky (eliben@gmail.com)
# This code is in the public domain
#-------------------------------------------------------------------------------
try:
import unittest2 as unittest
except ImportError:
import unittest
import os
from utils import setup_syspath
setup_syspath()
from elftools.elf.elffile import ELFFile
from elftools.common.exceptions import ELFError
from elftools.elf.dynamic import DynamicTag
class TestDynamicTag(unittest.TestCase):
"""Tests for the DynamicTag class."""
def test_requires_stringtable(self):
with self.assertRaises(ELFError):
dt = DynamicTag('', None)
class TestDynamic(unittest.TestCase):
"""Tests for the Dynamic class."""
def test_missing_sections(self):
"""Verify we can get dynamic strings w/out section headers"""
libs = []
with open(os.path.join('test', 'testfiles_for_unittests',
'aarch64_super_stripped.elf'), 'rb') as f:
elf = ELFFile(f)
for segment in elf.iter_segments():
if segment.header.p_type != 'PT_DYNAMIC':
continue
for t in segment.iter_tags():
if t.entry.d_tag == 'DT_NEEDED':
libs.append(t.needed.decode('utf-8'))
exp = ['libc.so.6']
self.assertEqual(libs, exp)
def test_reading_symbols(self):
"""Verify we can read symbol table without SymbolTableSection"""
with open(os.path.join('test', 'testfiles_for_unittests',
'aarch64_super_stripped.elf'), 'rb') as f:
elf = ELFFile(f)
for segment in elf.iter_segments():
if segment.header.p_type != 'PT_DYNAMIC':
continue
symbol_names = [x.name for x in segment.iter_symbols()]
exp = [b'', b'__libc_start_main', b'__gmon_start__', b'abort']
self.assertEqual(symbol_names, exp)
if __name__ == '__main__':
unittest.main()
|