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