2017-11-01 16:36:35 +01:00
|
|
|
#!/usr/bin/env python
|
2019-11-28 07:53:27 +01:00
|
|
|
import io
|
2020-11-09 21:02:50 +01:00
|
|
|
import logging
|
|
|
|
import unittest
|
2019-11-28 07:53:27 +01:00
|
|
|
from io import open as io_open
|
|
|
|
from unittest import TestCase
|
2020-11-09 21:02:50 +01:00
|
|
|
|
|
|
|
import lief
|
2019-11-28 07:53:27 +01:00
|
|
|
from utils import get_sample
|
2017-11-01 16:36:35 +01:00
|
|
|
|
2020-11-09 21:02:50 +01:00
|
|
|
lief.logging.set_level(lief.logging.LOGGING_LEVEL.INFO)
|
2017-11-01 16:36:35 +01:00
|
|
|
|
|
|
|
class TestPythonApi(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
def test_io(self):
|
|
|
|
lspath = get_sample('ELF/ELF64_x86-64_binary_ls.bin')
|
|
|
|
|
2019-11-28 07:53:27 +01:00
|
|
|
ls = lief.parse(lspath)
|
|
|
|
self.assertIsNotNone(ls.abstract.header)
|
2017-11-01 16:36:35 +01:00
|
|
|
|
2019-11-28 07:53:27 +01:00
|
|
|
with io_open(lspath, 'r') as f:
|
|
|
|
ls = lief.parse(f)
|
2017-11-01 16:36:35 +01:00
|
|
|
self.assertIsNotNone(ls.abstract.header)
|
|
|
|
|
2019-11-28 07:53:27 +01:00
|
|
|
with io_open(lspath, 'rb') as f:
|
|
|
|
ls = lief.parse(f)
|
|
|
|
self.assertIsNotNone(ls.abstract.header)
|
2017-11-01 16:36:35 +01:00
|
|
|
|
2019-11-28 07:53:27 +01:00
|
|
|
with io_open(lspath, 'rb') as f:
|
|
|
|
ls = lief.ELF.parse(f)
|
2017-11-01 16:36:35 +01:00
|
|
|
self.assertIsNotNone(ls.abstract.header)
|
|
|
|
|
2019-11-28 07:53:27 +01:00
|
|
|
with io_open(get_sample('PE/PE64_x86-64_binary_HelloWorld.exe'), 'rb') as f:
|
|
|
|
binary = lief.PE.parse(f)
|
2017-11-01 16:36:35 +01:00
|
|
|
self.assertIsNotNone(binary.abstract.header)
|
|
|
|
|
2019-11-28 07:53:27 +01:00
|
|
|
with io_open(get_sample('MachO/MachO64_x86-64_binary_dd.bin'), 'rb') as f:
|
|
|
|
binary = lief.MachO.parse(f)[0]
|
2017-11-01 16:36:35 +01:00
|
|
|
self.assertIsNotNone(binary.abstract.header)
|
|
|
|
|
2019-11-28 07:53:27 +01:00
|
|
|
with open(lspath, 'rb') as f: # As bytes
|
|
|
|
ls = lief.parse(f.read())
|
|
|
|
self.assertIsNotNone(ls.abstract.header)
|
|
|
|
|
|
|
|
with open(lspath, 'rb') as f: # As io.BufferedReader
|
|
|
|
ls = lief.parse(f)
|
|
|
|
self.assertIsNotNone(ls.abstract.header)
|
|
|
|
|
|
|
|
with open(lspath, 'rb') as f: # As io.BytesIO object
|
|
|
|
bytes_stream = io.BytesIO(f.read())
|
|
|
|
self.assertIsNotNone(bytes_stream)
|
2017-11-01 16:36:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
root_logger = logging.getLogger()
|
|
|
|
root_logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setLevel(logging.DEBUG)
|
|
|
|
root_logger.addHandler(ch)
|
|
|
|
|
|
|
|
unittest.main(verbosity=2)
|