diff options
Diffstat (limited to 'tests/format')
-rw-r--r-- | tests/format/executable.py | 59 | ||||
-rw-r--r-- | tests/format/flat.py | 34 | ||||
-rw-r--r-- | tests/format/format.py | 66 | ||||
-rw-r--r-- | tests/format/known.py | 73 | ||||
-rw-r--r-- | tests/format/program.py | 89 |
5 files changed, 219 insertions, 102 deletions
diff --git a/tests/format/executable.py b/tests/format/executable.py new file mode 100644 index 0000000..ec42ccd --- /dev/null +++ b/tests/format/executable.py @@ -0,0 +1,59 @@ + +from chrysacase import ChrysalideTestCase +from pychrysalide.analysis.contents import MemoryContent +from pychrysalide.arch import vmpa +from pychrysalide.format import ExecutableFormat + + +class TestExecutableFormat(ChrysalideTestCase): + """TestCase for format.ExecutableFormat.""" + + + def testMainAddresses(self): + """Provide several kinds of main addresses.""" + + data = b'\x00\x00\x00\xef' + cnt = MemoryContent(data) + + + class CustomFormatVmpa(ExecutableFormat): + + def _get_main_address(self): + return vmpa(246, 357) + + cf = CustomFormatVmpa(cnt) + + self.assertEqual(cf.main_address.phys, 246) + self.assertEqual(cf.main_address.virt, 357) + + + class CustomFormatInt(ExecutableFormat): + + def _get_main_address(self): + return 123 + + cf = CustomFormatInt(cnt) + + self.assertIsNone(cf.main_address.phys) + self.assertEqual(cf.main_address.virt, 123) + + + class CustomFormatNone(ExecutableFormat): + + def _get_main_address(self): + return None + + cf = CustomFormatNone(cnt) + + self.assertIsNone(cf.main_address) + + + class CustomFormatBad(ExecutableFormat): + + def _get_main_address(self): + return 'bad' + + cf = CustomFormatBad(cnt) + + with self.assertRaisesRegex(Exception, 'unable to define a value for the main address'): + ma = cf.main_address diff --git a/tests/format/flat.py b/tests/format/flat.py index 6924e42..f84f7a4 100644 --- a/tests/format/flat.py +++ b/tests/format/flat.py @@ -1,16 +1,11 @@ -#!/usr/bin/python3-dbg -# -*- coding: utf-8 -*- - - -# Tests minimalistes pour valider la gestion des erreurs relevées. - from chrysacase import ChrysalideTestCase -from pychrysalide.analysis import LoadedBinary +from pychrysalide import SourceEndian +#from pychrysalide.analysis import LoadedBinary from pychrysalide.analysis.contents import MemoryContent -from pychrysalide.arch import vmpa +#from pychrysalide.arch import vmpa from pychrysalide.format import FlatFormat -from pychrysalide.glibext import BinPortion +#from pychrysalide.glibext import BinPortion class TestFlatFormat(ChrysalideTestCase): @@ -24,18 +19,21 @@ class TestFlatFormat(ChrysalideTestCase): cnt = MemoryContent(data) - fmt = FlatFormat(cnt) - fmt.set_machine('armv7') + fmt = FlatFormat(cnt, 'armv7', SourceEndian.LITTLE) + + self.assertEqual(fmt.target_machine, 'armv7') + self.assertEqual(fmt.endianness, SourceEndian.LITTLE) + - base = vmpa(0, 0) + # base = vmpa(0, 0) - p = BinPortion(BinPortion.BPC_CODE, base, cnt.size) - p.rights = BinPortion.PAC_READ | BinPortion.PAC_EXEC + # p = BinPortion(BinPortion.BPC_CODE, base, cnt.size) + # p.rights = BinPortion.PAC_READ | BinPortion.PAC_EXEC - fmt.register_user_portion(p) + # fmt.register_user_portion(p) - binary = LoadedBinary(fmt) + # binary = LoadedBinary(fmt) - binary.analyze_and_wait() + # binary.analyze_and_wait() - self.assertTrue(list(binary.processor.instrs)[0].keyword == 'svc') + # self.assertTrue(list(binary.processor.instrs)[0].keyword == 'svc') diff --git a/tests/format/format.py b/tests/format/format.py deleted file mode 100644 index b6aad8f..0000000 --- a/tests/format/format.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/python3-dbg -# -*- coding: utf-8 -*- - - -# Tests minimalistes pour valider la gestion des erreurs relevées. - - -from chrysacase import ChrysalideTestCase -from pychrysalide.arch import vmpa, mrange -from pychrysalide.format import BinFormat -from pychrysalide.format import BinSymbol -import os -import sys - - -class SimpleFormat(BinFormat): - pass - - -class TestFormatErrors(ChrysalideTestCase): - """TestCase for format.BinFormat.""" - - - def create_fake_symbol(self, index): - saddr = vmpa(index * 0x10, vmpa.VMPA_NO_VIRTUAL) - srange = mrange(saddr, 0x3) - symbol = BinSymbol(BinSymbol.STP_ENTRY_POINT, srange) - return symbol - - - def testBasicSymbolOperations(self): - """Deal with the basic operations related to symbols in a binary format.""" - - sf = SimpleFormat() - - self.assertTrue(len(list(sf.symbols)) == 0) - - symbols = [ self.create_fake_symbol(i) for i in range(4) ] - s0, s1, s2, s3 = symbols - - for s in symbols: - sf.add_symbol(s) - - self.assertTrue(len(list(sf.symbols)) == len(symbols)) - - sf.remove_symbol(s2) - - self.assertTrue(list(sf.symbols) == [s0, s1, s3]) - - - def testBadParamsForAdding(self): - """Check if bad parameters fail for adding a new symbol.""" - - sf = SimpleFormat() - - with self.assertRaises(TypeError): - sf.add_symbol('s') - - - def testWrongRemoval(self): - """Try to remove a wrong symbol from a format.""" - - sf = SimpleFormat() - - s23 = self.create_fake_symbol(23) - sf.remove_symbol(s23) diff --git a/tests/format/known.py b/tests/format/known.py index 056238f..3a51f31 100644 --- a/tests/format/known.py +++ b/tests/format/known.py @@ -1,6 +1,3 @@ -#!/usr/bin/python3-dbg -# -*- coding: utf-8 -*- - from chrysacase import ChrysalideTestCase from pychrysalide.analysis.contents import MemoryContent @@ -11,8 +8,62 @@ class TestKnownFormat(ChrysalideTestCase): """TestCase for format.KnownFormat.""" + def testCustomInstance(self): + """Validate a full custom KnownFormat implementation.""" + + data = b'\x01\x02\x03' + cnt = MemoryContent(data) + + + class CustomFormat(KnownFormat): + + def _get_key(self): + return 'tiny' + + def _get_description(self): + return 'Small description' + + cf = CustomFormat(cnt) + + self.assertEqual(cf.key, 'tiny') + self.assertEqual(cf.description, 'Small description') + + + class EmptyCustomFormat(KnownFormat): + pass + + cf = EmptyCustomFormat(cnt) + + # NotImplementedError: method implementation is missing for '_get_key' + with self.assertRaisesRegex(NotImplementedError, "method implementation is missing for '_get_key'"): + k = cf.key + + # NotImplementedError: method implementation is missing for '_get_description' + with self.assertRaisesRegex(NotImplementedError, "method implementation is missing for '_get_description'"): + k = cf.description + + + class BadCustomFormat(KnownFormat): + + def _get_key(self): + return 123 + + def _get_description(self): + return 456 + + cf = BadCustomFormat(cnt) + + # ValueError: unexpected value type for known format key + with self.assertRaisesRegex(ValueError, 'unexpected value type for known format key'): + k = cf.key + + # ValueError: unexpected value type for known format description + with self.assertRaisesRegex(ValueError, 'unexpected value type for known format description'): + k = cf.description + + def testKnownFormatConstructor(self): - """Build Load a simple content for a flat format.""" + """Load a simple content for a known format.""" with self.assertRaisesRegex(RuntimeError, 'pychrysalide.format.KnownFormat is an abstract class'): fmt = KnownFormat() @@ -28,17 +79,3 @@ class TestKnownFormat(ChrysalideTestCase): with self.assertRaisesRegex(TypeError, 'unable to convert the provided argument to binary content'): fmt = MyKnownFormat2(123) - - class MyKnownFormatReady(KnownFormat): - _key = 'rdy' - def __init2__(self, cnt): - super(MyKnownFormatReady, self).__init2__(cnt) - - data = b'\x00\x00\x00\xef' - - cnt = MemoryContent(data) - fmt = MyKnownFormatReady(cnt) - - self.assertIsNotNone(fmt) - - self.assertEqual(fmt.key, 'rdy') diff --git a/tests/format/program.py b/tests/format/program.py new file mode 100644 index 0000000..7027cdf --- /dev/null +++ b/tests/format/program.py @@ -0,0 +1,89 @@ + +from chrysacase import ChrysalideTestCase +from pychrysalide import SourceEndian +from pychrysalide.analysis.contents import MemoryContent +#from pychrysalide.arch import vmpa, mrange +from pychrysalide.format import ProgramFormat +#from pychrysalide.format import BinSymbol + + +# class SimpleFormat(BinFormat): +# pass + + +class TestProgramFormat(ChrysalideTestCase): + """TestCase for format.ProgramFormat.""" + + + def testCustomInstance(self): + """Validate a full custom ProgramFormat implementation.""" + + data = b'\x00\x00\x00\xef' + cnt = MemoryContent(data) + + + class CustomFormat(ProgramFormat): + + def _get_endianness(self): + return SourceEndian.BIG + + cf = CustomFormat(cnt) + + self.assertEqual(cf.endianness, SourceEndian.BIG) + + + class EmptyCustomFormat(ProgramFormat): + pass + + cf = EmptyCustomFormat(cnt) + + self.assertEqual(cf.endianness, SourceEndian.LITTLE) + + + + + + + # def create_fake_symbol(self, index): + # saddr = vmpa(index * 0x10, vmpa.VMPA_NO_VIRTUAL) + # srange = mrange(saddr, 0x3) + # symbol = BinSymbol(BinSymbol.STP_ENTRY_POINT, srange) + # return symbol + + + # def testBasicSymbolOperations(self): + # """Deal with the basic operations related to symbols in a binary format.""" + + # sf = SimpleFormat() + + # self.assertTrue(len(list(sf.symbols)) == 0) + + # symbols = [ self.create_fake_symbol(i) for i in range(4) ] + # s0, s1, s2, s3 = symbols + + # for s in symbols: + # sf.add_symbol(s) + + # self.assertTrue(len(list(sf.symbols)) == len(symbols)) + + # sf.remove_symbol(s2) + + # self.assertTrue(list(sf.symbols) == [s0, s1, s3]) + + + # def testBadParamsForAdding(self): + # """Check if bad parameters fail for adding a new symbol.""" + + # sf = SimpleFormat() + + # with self.assertRaises(TypeError): + # sf.add_symbol('s') + + + # def testWrongRemoval(self): + # """Try to remove a wrong symbol from a format.""" + + # sf = SimpleFormat() + + # s23 = self.create_fake_symbol(23) + # sf.remove_symbol(s23) |