diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2024-11-15 10:01:42 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2024-11-15 10:01:42 (GMT) |
commit | 671f61ec87f5e8d762ea890aabbc62c777e1c4f9 (patch) | |
tree | 1517f5fc5d00c8fd04872b35571556b021866f14 /tests | |
parent | 3e21278480a25552401644cd0dc168e3588508dd (diff) |
Restore a skeleton of support for file formats.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/format/flat.py | 34 | ||||
-rw-r--r-- | tests/format/known.py | 73 | ||||
-rw-r--r-- | tests/format/program.py | 96 |
3 files changed, 128 insertions, 75 deletions
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/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 index b6aad8f..7a649b8 100644 --- a/tests/format/program.py +++ b/tests/format/program.py @@ -1,66 +1,84 @@ -#!/usr/bin/python3-dbg -# -*- coding: utf-8 -*- +from chrysacase import ChrysalideTestCase +from pychrysalide import SourceEndian +#from pychrysalide.arch import vmpa, mrange +from pychrysalide.format import ProgramFormat +#from pychrysalide.format import BinSymbol -# Tests minimalistes pour valider la gestion des erreurs relevées. +# class SimpleFormat(BinFormat): +# pass -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 TestProgramFormat(ChrysalideTestCase): + """TestCase for format.ProgramFormat.""" + + + def testCustomInstance(self): + """Validate a full custom ProgramFormat implementation.""" + + class CustomFormat(ProgramFormat): + + def _get_endianness(self): + return SourceEndian.BIG + + cf = CustomFormat() + + self.assertEqual(cf.endianness, SourceEndian.BIG) + + + class EmptyCustomFormat(ProgramFormat): + pass + + cf = EmptyCustomFormat() + + self.assertEqual(cf.endianness, SourceEndian.LITTLE) -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 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.""" + # def testBasicSymbolOperations(self): + # """Deal with the basic operations related to symbols in a binary format.""" - sf = SimpleFormat() + # sf = SimpleFormat() - self.assertTrue(len(list(sf.symbols)) == 0) + # self.assertTrue(len(list(sf.symbols)) == 0) - symbols = [ self.create_fake_symbol(i) for i in range(4) ] - s0, s1, s2, s3 = symbols + # symbols = [ self.create_fake_symbol(i) for i in range(4) ] + # s0, s1, s2, s3 = symbols - for s in symbols: - sf.add_symbol(s) + # for s in symbols: + # sf.add_symbol(s) - self.assertTrue(len(list(sf.symbols)) == len(symbols)) + # self.assertTrue(len(list(sf.symbols)) == len(symbols)) - sf.remove_symbol(s2) + # sf.remove_symbol(s2) - self.assertTrue(list(sf.symbols) == [s0, s1, s3]) + # self.assertTrue(list(sf.symbols) == [s0, s1, s3]) - def testBadParamsForAdding(self): - """Check if bad parameters fail for adding a new symbol.""" + # def testBadParamsForAdding(self): + # """Check if bad parameters fail for adding a new symbol.""" - sf = SimpleFormat() + # sf = SimpleFormat() - with self.assertRaises(TypeError): - sf.add_symbol('s') + # with self.assertRaises(TypeError): + # sf.add_symbol('s') - def testWrongRemoval(self): - """Try to remove a wrong symbol from a format.""" + # def testWrongRemoval(self): + # """Try to remove a wrong symbol from a format.""" - sf = SimpleFormat() + # sf = SimpleFormat() - s23 = self.create_fake_symbol(23) - sf.remove_symbol(s23) + # s23 = self.create_fake_symbol(23) + # sf.remove_symbol(s23) |