Ejemplo n.º 1
0
    def test_decode_from_elf_file(self):
        detok = detokenize.Detokenizer(io.BytesIO(ELF_WITH_TOKENIZER_SECTIONS))
        expected_tokens = frozenset(detok.database.token_to_entries.keys())

        elf = tempfile.NamedTemporaryFile('wb', delete=False)
        try:
            elf.write(ELF_WITH_TOKENIZER_SECTIONS)
            elf.close()

            # Open ELF by file object
            with open(elf.name, 'rb') as fd:
                detok = detokenize.Detokenizer(fd)

            self.assertEqual(expected_tokens,
                             frozenset(detok.database.token_to_entries.keys()))

            # Open ELF by path
            detok = detokenize.Detokenizer(elf.name)
            self.assertEqual(expected_tokens,
                             frozenset(detok.database.token_to_entries.keys()))

            # Open ELF by elf_reader.Elf
            with open(elf.name, 'rb') as fd:
                detok = detokenize.Detokenizer(elf_reader.Elf(fd))

            self.assertEqual(expected_tokens,
                             frozenset(detok.database.token_to_entries.keys()))
        finally:
            os.unlink(elf.name)
Ejemplo n.º 2
0
    def test_elf_reader_read_values(self):
        elf = elf_reader.Elf(self._archive)
        address = next(elf.sections_with_name('.test_section_1')).address
        self.assertEqual(elf.read_value(address), b'You cannot pass')

        int32_address = next(elf.sections_with_name('.test_section_2')).address
        self.assertEqual(elf.read_value(int32_address, 4), b'\xef\xbe\xed\xfe')
Ejemplo n.º 3
0
    def test_decode_from_elf_file(self):
        detok = detokenize.Detokenizer(io.BytesIO(ELF_WITH_TOKENIZER_SECTIONS))
        expected_tokens = frozenset(detok.database.token_to_entries.keys())

        with tempfile.NamedTemporaryFile() as elf:
            elf.write(ELF_WITH_TOKENIZER_SECTIONS)
            elf.seek(0)

            # Open ELF by file object
            detok = detokenize.Detokenizer(elf)
            self.assertEqual(expected_tokens,
                             frozenset(detok.database.token_to_entries.keys()))

            # Open ELF by path
            detok = detokenize.Detokenizer(elf.name)
            self.assertEqual(expected_tokens,
                             frozenset(detok.database.token_to_entries.keys()))

            # Open ELF by elf_reader.Elf
            elf.seek(0)
            detok = detokenize.Detokenizer(elf_reader.Elf(elf))
            self.assertEqual(expected_tokens,
                             frozenset(detok.database.token_to_entries.keys()))
Ejemplo n.º 4
0
def _elf_reader(elf) -> elf_reader.Elf:
    return elf if isinstance(elf, elf_reader.Elf) else elf_reader.Elf(elf)
Ejemplo n.º 5
0
 def setUp(self):
     super().setUp()
     self._elf_file = open(TEST_ELF_PATH, 'rb')
     self._elf = elf_reader.Elf(self._elf_file)
Ejemplo n.º 6
0
 def test_elf_reader_dump_single_section(self):
     elf = elf_reader.Elf(self._archive)
     self.assertEqual(elf.dump_sections(r'\.test_section_1'),
                      b'You cannot pass\0')
     self.assertEqual(elf.dump_sections(r'\.test_section_2'),
                      b'\xef\xbe\xed\xfe')