diff options
Diffstat (limited to 'tests/analysis/contents')
-rw-r--r-- | tests/analysis/contents/custom.py | 134 | ||||
-rw-r--r-- | tests/analysis/contents/endian.py | 27 | ||||
-rw-r--r-- | tests/analysis/contents/memory.py | 7 | ||||
-rw-r--r-- | tests/analysis/contents/restricted.py | 21 |
4 files changed, 163 insertions, 26 deletions
diff --git a/tests/analysis/contents/custom.py b/tests/analysis/contents/custom.py new file mode 100644 index 0000000..2153967 --- /dev/null +++ b/tests/analysis/contents/custom.py @@ -0,0 +1,134 @@ + +from chrysacase import ChrysalideTestCase +from pychrysalide import SourceEndian +from pychrysalide.analysis import BinContent +from pychrysalide.arch import vmpa + + +class CustomContent(BinContent): + + def __init__(self, size): + super(CustomContent, self).__init__() + self._start = 10 + self._size = size * 8 + + def _describe(self, full): + return 'my_desc' + ('_full' if full else '') + + def _compute_checksum(self, checksum): + checksum.update(b'xxxxx') + + def _compute_size(self): + return int(self._size / 8) + + def _compute_start_pos(self): + return vmpa(self._start, vmpa.VmpaSpecialValue.NO_VIRTUAL) + + def _compute_end_pos(self): + return vmpa(self._start + self._size, vmpa.VmpaSpecialValue.NO_VIRTUAL) + + def _seek(self, addr, length): + addr += length + return True + + def _read_uxxx(self, addr, sizeof): + + assert(addr >= self.start_pos and addr < self.end_pos) + + val = int((addr - self.start_pos).phys / sizeof) + + addr += sizeof + + return val + + def _read_u8(self, addr): + return self._read_uxxx(addr, 1) + + def _read_u16(self, addr, endian): + return self._read_uxxx(addr, 2) + + def _read_u32(self, addr, endian): + return self._read_uxxx(addr, 4) + + def _read_u64(self, addr, endian): + return self._read_uxxx(addr, 8) + + def _read_uleb128(self, addr): + return 128 + + def _read_leb128(self, addr): + return -128 + + +class TestCustomContent(ChrysalideTestCase): + """TestCase for custom implementation of analysis.BinContent.""" + + def testBasicImplementations(self): + """Involve all implemented basic wrappers for a custom content.""" + + cnt = CustomContent(1) + + self.assertEqual(cnt._describe(False), 'my_desc') + + self.assertEqual(cnt._describe(True), 'my_desc_full') + + # $ echo -n 'xxxxx' | sha256sum + # eaf16bc07968e013f3f94ab1342472434a39fc3475f11cf341a6c3965974f8e9 - + + expected = 'eaf16bc07968e013f3f94ab1342472434a39fc3475f11cf341a6c3965974f8e9' + + self.assertEqual(cnt.checksum, expected) + + self.assertEqual(cnt.size, 1) + + addr = cnt.start_pos + offset = 13 + + cnt.seek(addr, offset) + + self.assertEqual(addr.phys, cnt.start_pos.phys + offset) + + + def testReadImplementations(self): + """Involve main implemented read wrappers for a custom content.""" + + cnt = CustomContent(8) + + def _run_check_read_implem(fn, args): + + last = None + + pos = cnt.start_pos + + while pos < cnt.end_pos: + + val = fn(pos, *args) + + if not(last is None): + self.assertEqual(last + 1, val) + else: + self.assertEqual(val, 0) + + last = val + + checks = [ + [ cnt.read_u8, [] ], + [ cnt.read_u16, [ SourceEndian.LITTLE ] ], + [ cnt.read_u32, [ SourceEndian.LITTLE ] ], + [ cnt.read_u64, [ SourceEndian.LITTLE ] ], + ] + + for f, a in checks: + _run_check_read_implem(f, a) + + + def testLEB128Implementations(self): + """Involve [U]LEB128 implemented wrappers for a custom content.""" + + cnt = CustomContent(1) + + addr = cnt.start_pos + + self.assertEqual(cnt.read_uleb128(addr), 128) + + self.assertEqual(cnt.read_leb128(addr), -128) diff --git a/tests/analysis/contents/endian.py b/tests/analysis/contents/endian.py index b7c8bec..0557174 100644 --- a/tests/analysis/contents/endian.py +++ b/tests/analysis/contents/endian.py @@ -6,8 +6,9 @@ from chrysacase import ChrysalideTestCase +from pychrysalide import SourceEndian from pychrysalide.analysis import BinContent -from pychrysalide.analysis.contents import FileContent, RestrictedContent +from pychrysalide.analysis.contents import FileContent from pychrysalide.arch import vmpa import tempfile @@ -55,36 +56,36 @@ class TestEndianness(ChrysalideTestCase): start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u16(start, BinContent.SourceEndian.LITTLE_WORD) + val = fcnt.read_u16(start, SourceEndian.LITTLE_WORD) self.assertEqual(val, 0x1516) start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u16(start, BinContent.SourceEndian.BIG_WORD) + val = fcnt.read_u16(start, SourceEndian.BIG_WORD) self.assertEqual(val, 0x1615) # 32 bits start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u32(start, BinContent.SourceEndian.LITTLE_WORD) + val = fcnt.read_u32(start, SourceEndian.LITTLE_WORD) self.assertEqual(val, 0x17181516) start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u32(start, BinContent.SourceEndian.BIG_WORD) + val = fcnt.read_u32(start, SourceEndian.BIG_WORD) self.assertEqual(val, 0x16151817) # 64 bits start = vmpa(0, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u64(start, BinContent.SourceEndian.LITTLE_WORD) + val = fcnt.read_u64(start, SourceEndian.LITTLE_WORD) self.assertEqual(val, 0x0708050603040102) start = vmpa(0, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u64(start, BinContent.SourceEndian.BIG_WORD) + val = fcnt.read_u64(start, SourceEndian.BIG_WORD) self.assertEqual(val, 0x0201040306050807) @@ -97,34 +98,34 @@ class TestEndianness(ChrysalideTestCase): start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u16(start, BinContent.SourceEndian.LITTLE) + val = fcnt.read_u16(start, SourceEndian.LITTLE) self.assertEqual(val, 0x1615) start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u16(start, BinContent.SourceEndian.BIG) + val = fcnt.read_u16(start, SourceEndian.BIG) self.assertEqual(val, 0x1516) # 32 bits start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u32(start, BinContent.SourceEndian.LITTLE) + val = fcnt.read_u32(start, SourceEndian.LITTLE) self.assertEqual(val, 0x18171615) start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u32(start, BinContent.SourceEndian.BIG) + val = fcnt.read_u32(start, SourceEndian.BIG) self.assertEqual(val, 0x15161718) # 64 bits start = vmpa(0, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u64(start, BinContent.SourceEndian.LITTLE) + val = fcnt.read_u64(start, SourceEndian.LITTLE) self.assertEqual(val, 0x0807060504030201) start = vmpa(0, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = fcnt.read_u64(start, BinContent.SourceEndian.BIG) + val = fcnt.read_u64(start, SourceEndian.BIG) self.assertEqual(val, 0x0102030405060708) diff --git a/tests/analysis/contents/memory.py b/tests/analysis/contents/memory.py index f99e607..d9daad2 100644 --- a/tests/analysis/contents/memory.py +++ b/tests/analysis/contents/memory.py @@ -7,6 +7,7 @@ from chrysacase import ChrysalideTestCase +from pychrysalide import SourceEndian from pychrysalide.analysis import BinContent from pychrysalide.analysis.contents import MemoryContent from pychrysalide.arch import vmpa, mrange @@ -39,12 +40,12 @@ class TestMemoryContent(ChrysalideTestCase): start = vmpa(14, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = cnt.read_u16(start, BinContent.SourceEndian.LITTLE) + val = cnt.read_u16(start, SourceEndian.LITTLE) self.assertEqual(val, 0x1817) start = vmpa(10, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = cnt.read_u32(start, BinContent.SourceEndian.LITTLE) + val = cnt.read_u32(start, SourceEndian.LITTLE) self.assertEqual(val, 0x16150013) @@ -63,4 +64,4 @@ class TestMemoryContent(ChrysalideTestCase): with self.assertRaisesRegex(Exception, 'Invalid read access.'): start = vmpa(0, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = cnt.read_u16(start, BinContent.SourceEndian.LITTLE) + val = cnt.read_u16(start, SourceEndian.LITTLE) diff --git a/tests/analysis/contents/restricted.py b/tests/analysis/contents/restricted.py index 023e600..8c3cb50 100644 --- a/tests/analysis/contents/restricted.py +++ b/tests/analysis/contents/restricted.py @@ -7,6 +7,7 @@ from chrysacase import ChrysalideTestCase +from pychrysalide import SourceEndian from pychrysalide.analysis import BinContent from pychrysalide.analysis.contents import FileContent, RestrictedContent from pychrysalide.arch import vmpa, mrange @@ -64,10 +65,10 @@ class TestRestrictedContent(ChrysalideTestCase): val = rcnt.read_u8(start) self.assertEqual(val, 0x16) - val = rcnt.read_u16(start, BinContent.SourceEndian.LITTLE) + val = rcnt.read_u16(start, SourceEndian.LITTLE) self.assertEqual(val, 0x1817) - val = rcnt.read_u32(start, BinContent.SourceEndian.LITTLE) + val = rcnt.read_u32(start, SourceEndian.LITTLE) self.assertEqual(val, 0x24232221) @@ -111,15 +112,15 @@ class TestRestrictedContent(ChrysalideTestCase): self.assertEqual(val, 0x15) start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = rcnt.read_u16(start, BinContent.SourceEndian.LITTLE) + val = rcnt.read_u16(start, SourceEndian.LITTLE) self.assertEqual(val, 0x1615) start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = rcnt.read_u32(start, BinContent.SourceEndian.LITTLE) + val = rcnt.read_u32(start, SourceEndian.LITTLE) self.assertEqual(val, 0x18171615) start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = rcnt.read_u64(start, BinContent.SourceEndian.LITTLE) + val = rcnt.read_u64(start, SourceEndian.LITTLE) self.assertEqual(val, 0x2423222118171615) start = vmpa(23, vmpa.VmpaSpecialValue.NO_VIRTUAL) @@ -127,15 +128,15 @@ class TestRestrictedContent(ChrysalideTestCase): self.assertEqual(val, 0x28) start = vmpa(22, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = rcnt.read_u16(start, BinContent.SourceEndian.LITTLE) + val = rcnt.read_u16(start, SourceEndian.LITTLE) self.assertEqual(val, 0x2827) start = vmpa(20, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = rcnt.read_u32(start, BinContent.SourceEndian.LITTLE) + val = rcnt.read_u32(start, SourceEndian.LITTLE) self.assertEqual(val, 0x28272625) start = vmpa(16, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = rcnt.read_u64(start, BinContent.SourceEndian.LITTLE) + val = rcnt.read_u64(start, SourceEndian.LITTLE) self.assertEqual(val, 0x2827262524232221) @@ -202,12 +203,12 @@ class TestRestrictedContent(ChrysalideTestCase): with self.assertRaisesRegex(Exception, 'Invalid read access.'): start = vmpa(11, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = rcnt.read_u16(start, BinContent.SourceEndian.LITTLE) + val = rcnt.read_u16(start, SourceEndian.LITTLE) with self.assertRaisesRegex(Exception, 'Invalid read access.'): start = vmpa(23, vmpa.VmpaSpecialValue.NO_VIRTUAL) - val = rcnt.read_u16(start, BinContent.SourceEndian.LITTLE) + val = rcnt.read_u16(start, SourceEndian.LITTLE) def testDescription(self): |