mirror of
https://github.com/JakeHillion/drgn.git
synced 2024-12-27 02:45:53 +00:00
800ee3ec36
Now, we can get rid of the ELF parsing implementation in CoreReader. Instead, we parse in ElfFile and pass the parsed information to CoreReader.
71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
import contextlib
|
|
import tempfile
|
|
import unittest
|
|
|
|
from drgn.corereader import CoreReader
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def tmpfile(data):
|
|
file = tempfile.TemporaryFile()
|
|
try:
|
|
file.write(data)
|
|
file.flush()
|
|
yield file
|
|
finally:
|
|
file.close()
|
|
|
|
|
|
class TestCoreReader(unittest.TestCase):
|
|
def test_bad_segments(self):
|
|
self.assertRaises(TypeError, CoreReader, 0, 0)
|
|
self.assertRaises(TypeError, CoreReader, 0, [0])
|
|
self.assertRaises(ValueError, CoreReader, 0, [()])
|
|
self.assertRaises(OverflowError, CoreReader, 0, [(2**64, 0, 0, 0, 0)])
|
|
|
|
def test_simple_read(self):
|
|
data = b'hello, world!'
|
|
segments = [(0, 0xffff0000, 0x0, len(data), len(data))]
|
|
with tmpfile(data) as file:
|
|
core_reader = CoreReader(file.fileno(), segments)
|
|
self.assertEqual(core_reader.read(0xffff0000, len(data)), data)
|
|
|
|
def test_bad_address(self):
|
|
data = b'hello, world!'
|
|
segments = [(0, 0xffff0000, 0x0, len(data), len(data))]
|
|
with tmpfile(data) as file:
|
|
core_reader = CoreReader(file.fileno(), segments)
|
|
self.assertRaisesRegex(ValueError, 'could not find memory segment',
|
|
core_reader.read, 0xdeadbeef, 4)
|
|
|
|
def test_segment_overflow(self):
|
|
data = b'hello, world!'
|
|
segments = [(0, 0xffff0000, 0x0, len(data), len(data))]
|
|
with tmpfile(data) as file:
|
|
core_reader = CoreReader(file.fileno(), segments)
|
|
self.assertRaisesRegex(ValueError, 'could not find memory segment',
|
|
core_reader.read, 0xffff0000, len(data) + 1)
|
|
|
|
def test_contiguous_segments(self):
|
|
data = b'hello, world!\0foobar'
|
|
segments = [
|
|
(0, 0xffff0000, 0x0, 4, 4),
|
|
(14, 0xfffff000, 0x0, 6, 6),
|
|
(4, 0xffff0004, 0x0, 10, 10),
|
|
]
|
|
with tmpfile(data) as file:
|
|
core_reader = CoreReader(file.fileno(), segments)
|
|
self.assertEqual(core_reader.read(0xffff0000, 14), data[:14])
|
|
|
|
def test_zero_filled_segment(self):
|
|
data = b'hello, world!'
|
|
segments = [
|
|
(0, 0xffff0000, 0x0, 13, 17),
|
|
]
|
|
with tmpfile(data) as file:
|
|
core_reader = CoreReader(file.fileno(), segments)
|
|
self.assertEqual(core_reader.read(0xffff0000, len(data) + 4),
|
|
data + bytes(4))
|
|
self.assertEqual(core_reader.read(0xffff0000 + len(data), 4),
|
|
bytes(4))
|