def test_arithmetic(self): """Test that arithmetic operations result in integers.""" for test in [ TypeTest(sir.BinOpCode(name = 'OP_ADD', left = sir.Int(5), right = sir.Int(6)), SymbolType.Integer), TypeTest(sir.BinOpCode(name = 'OP_ADD', left = sir.Bytes('05'), right = sir.Bytes('06')), SymbolType.Integer), ]: self._test(test)
def test_basic_types(self): for test in [ TypeTest(sir.Int(5), SymbolType.Integer), TypeTest(sir.Bytes('05'), SymbolType.ByteArray), TypeTest(sir.Symbol('foo'), SymbolType.Symbol), ]: self._test(test)
def test_assignments(self): for node, expected in [ (sir.Declaration(name='foo', value=sir.Int(5), type_=SymbolType.Integer, mutable=True), 'let mutable foo = 5'), (sir.Declaration(name='foo', value=sir.Int(5), type_=SymbolType.Integer, mutable=False), 'let foo = 5'), (sir.Assignment(name='foo', value=sir.Int(5), type_=SymbolType.Integer), 'foo = 5'), (sir.Assignment(name='foo', value=sir.Bytes('05'), type_=SymbolType.Integer), 'foo = 05'), ]: self.assertEqual(expected, SInstructions.format_op(node))
def test_function_call(self): for node, expected in [ (sir.FunctionCall(name='foo', args=[]), 'foo()'), (sir.FunctionCall(name='foo', args=[sir.Int(5)]), 'foo(5)'), (sir.FunctionCall(name='foo', args=[sir.Int(5), sir.Int(6)]), 'foo(5, 6)'), (sir.FunctionCall(name='foo', args=[sir.Int(5), sir.Bytes('06')]), 'foo(5, 06)'), ]: self.assertEqual(expected, SInstructions.format_op(node))
def test_op(self): op = sir.OpCode(name='OP_FOO') op.lineno = 0 push = sir.Bytes('01') push.lineno = 0 s = sir.Script(statements=[op, push]) ops = self._linearize(s) self.assertEqual("['OP_FOO', 'OP_1']", str(ops))
def test_unary_op(self): for name, expected in [ ('OP_1ADD', '05++'), ('OP_1SUB', '05--'), ('OP_2MUL', '05 * 2'), ('OP_2DIV', '05 / 2'), ('OP_NEGATE', '-05'), ('OP_ABS', '|05|'), ]: op = sir.UnaryOpCode(name = name, operand = sir.Bytes('05')) self.assertEqual(expected, SInstructions.format_op(op))
def test_binary_op(self): for name, expected in [ ('OP_ADD', '05 + 06'), ('OP_SUB', '05 - 06'), ('OP_MUL', '05 * 06'), ('OP_DIV', '05 / 06'), ('OP_MOD', '05 % 06'), ('OP_LSHIFT', '05 << 06'), ('OP_RSHIFT', '05 >> 06'), ('OP_BOOLAND', '05 and 06'), ('OP_BOOLOR', '05 or 06'), ('OP_NUMEQUAL', '05 == 06'), ('OP_NUMNOTEQUAL', '05 != 06'), ('OP_LESSTHAN', '05 < 06'), ('OP_GREATERTHAN', '05 > 06'), ('OP_LESSTHANOREQUAL', '05 <= 06'), ('OP_GREATERTHANOREQUAL', '05 >= 06'), ]: op = sir.BinOpCode(name = name, left = sir.Bytes('05'), right = sir.Bytes('06')) self.assertEqual(expected, SInstructions.format_op(op))
def visit_List(self, node): """Transform array of bytes to bytes.""" op = types.Bytes(hexs.format_hex(''.join(node.elts))) op.lineno = node.lineno return self.visit(op)
def visit_Str(self, node): hex_strs = [hex(ord(i)) for i in node.s] hex_strs = map(hexs.format_hex, hex_strs) return types.Bytes(''.join(hex_strs))
def visit_list(self, node): return self.visit(structural_nodes.Bytes(''.join(node)))
def test_operations(self): for test in [ TypeTest(sir.UnaryOpCode(name = 'OP_SHA1', operand = sir.Int(5)), SymbolType.ByteArray), TypeTest(sir.UnaryOpCode(name = 'OP_SHA1', operand = sir.Symbol('foo')), SymbolType.Expr), TypeTest(sir.VariableArgsOpCode(name='OP_CHECKMULTISIG', operands=[sir.Int(1), sir.Bytes('111111'), sir.Bytes('222222'), sir.Int(2)]), SymbolType.ByteArray), ]: self._test(test)