|
| 1 | +import sys |
1 | 2 | import unittest
|
2 | 3 |
|
3 | 4 | import pyevmasm as EVMAsm
|
4 | 5 |
|
5 | 6 |
|
| 7 | +def int_to_bytes(i): |
| 8 | + if sys.version_info[0] >= 3: |
| 9 | + return i.to_bytes(1, 'little') |
| 10 | + else: |
| 11 | + return bytes(chr(i)) |
| 12 | + |
| 13 | + |
6 | 14 | # noinspection PyPep8Naming
|
7 | 15 | class EVMTest_Assembler(unittest.TestCase):
|
8 | 16 | _multiprocess_can_split_ = True
|
@@ -83,5 +91,36 @@ def test_constantinople_fork(self):
|
83 | 91 | insn = EVMAsm.disassemble_one(b'\xf5', fork='constantinople')
|
84 | 92 | self.assertTrue(insn.mnemonic == 'CREATE2')
|
85 | 93 |
|
| 94 | + def test_assemble_DUP1_regression(self): |
| 95 | + insn = EVMAsm.assemble_one("DUP1") |
| 96 | + self.assertEqual(insn.mnemonic, "DUP1") |
| 97 | + self.assertEqual(insn.opcode, 0x80) |
| 98 | + |
| 99 | + def test_assemble_LOGX_regression(self): |
| 100 | + inst_table = EVMAsm.instruction_tables[EVMAsm.DEFAULT_FORK] |
| 101 | + log0_opcode = 0xa0 |
| 102 | + for n in range(5): |
| 103 | + opcode = log0_opcode + n |
| 104 | + self.assertTrue(opcode in inst_table, "{!r} not in instruction_table".format(opcode)) |
| 105 | + asm = "LOG" + str(n) |
| 106 | + self.assertTrue(asm in inst_table, "{!r} not in instruction_table".format(asm)) |
| 107 | + insn = EVMAsm.assemble_one(asm) |
| 108 | + self.assertEqual(insn.mnemonic, asm) |
| 109 | + self.assertEqual(insn.opcode, opcode) |
| 110 | + |
| 111 | + def test_consistency_assembler_disassembler(self): |
| 112 | + """ |
| 113 | + Tests whether every opcode that can be disassembled, can also be |
| 114 | + assembled again. |
| 115 | + """ |
| 116 | + inst_table = EVMAsm.instruction_tables[EVMAsm.DEFAULT_FORK] |
| 117 | + for opcode in inst_table.keys(): |
| 118 | + b = int_to_bytes(opcode) + b"\x00" * 32 |
| 119 | + inst_dis = EVMAsm.disassemble_one(b) |
| 120 | + a = str(inst_dis) |
| 121 | + inst_as = EVMAsm.assemble_one(a) |
| 122 | + self.assertEqual(inst_dis, inst_as) |
| 123 | + |
| 124 | + |
86 | 125 | if __name__ == '__main__':
|
87 | 126 | unittest.main()
|
0 commit comments