Exemplo n.º 1
0
    def test_op_map13(self):
        self.econtext.EvaluationStack.PushT(Map(dict={StackItem.New('a'): StackItem.New(4), StackItem.New('b'): StackItem.New(5)}))
        self.econtext.EvaluationStack.PushT(StackItem.New('c'))
        self.econtext.Script._value = OpCode.HASKEY
        self.engine.ExecuteInstruction()

        self.assertEqual(self.econtext.EvaluationStack.Items[0].GetBoolean(), False)
    def test_serialize_array(self):

        my_array = Array([
            StackItem.New(12),
            StackItem.New(b'Hello World'),
            StackItem.New(True),
            Array([StackItem.New(113442),
                   StackItem.New(2),
                   StackItem.New(3)])
        ])
        self.engine.EvaluationStack.PushT(my_array)
        self.state_reader.Runtime_Serialize(self.engine)
        self.state_reader.Runtime_Deserialize(self.engine)

        deserialized = self.engine.EvaluationStack.Pop()
        self.assertIsInstance(deserialized, Array)
        self.assertEqual(deserialized.Count, 4)
        self.assertEqual(deserialized.GetArray()[0], StackItem.New(12))
        self.assertEqual(deserialized.GetArray()[1],
                         StackItem.New(b'Hello World'))
        self.assertEqual(deserialized.GetArray()[2], StackItem.New(True))
        subarray = deserialized.GetArray()[3]
        self.assertIsInstance(subarray, Array)
        self.assertEqual(subarray.Count, 3)
        self.assertEqual(subarray.GetArray()[0], StackItem.New(113442))
    def test_serialize_bytearray(self):
        my_ba = StackItem.New(b'a0f03a')
        self.engine.EvaluationStack.PushT(my_ba)
        self.state_reader.Runtime_Serialize(self.engine)
        res = self.state_reader.Runtime_Deserialize(self.engine)

        deserialized = self.engine.EvaluationStack.Pop()
        self.assertEqual(deserialized.GetByteArray(), bytearray(b'a0f03a'))

        my_ba = StackItem.New(b'Hello Serialization')
        self.engine.EvaluationStack.PushT(my_ba)
        self.state_reader.Runtime_Serialize(self.engine)
        self.state_reader.Runtime_Deserialize(self.engine)

        deserialized = self.engine.EvaluationStack.Pop()
        self.assertEqual(deserialized.GetByteArray(),
                         bytearray(b'Hello Serialization'))

        my_ba = StackItem.New(bytearray(b'\x01\x03\xfa\x99\x42'))
        self.engine.EvaluationStack.PushT(my_ba)
        self.state_reader.Runtime_Serialize(self.engine)
        self.state_reader.Runtime_Deserialize(self.engine)

        deserialized = self.engine.EvaluationStack.Pop()
        self.assertEqual(deserialized.GetByteArray(),
                         bytearray(b'\x01\x03\xfa\x99\x42'))
    def test_op_map11(self):
        self.econtext.EvaluationStack.PushT(Map(dict={StackItem.New('a'): StackItem.New(4), StackItem.New('b'): StackItem.New(5)}))
        self.engine.ExecuteOp(OpCode.VALUES, self.econtext)

        self.assertIsInstance(self.econtext.EvaluationStack.Items[0], Array)
        items = self.econtext.EvaluationStack.Items[0].GetArray()
        self.assertEqual(items, [StackItem.New(4), StackItem.New(5)])
    def test_op_map6(self):
        # we can pick an item from a dict
        self.econtext.EvaluationStack.PushT(Map(dict={StackItem.New('a'): StackItem.New(4)}))
        self.econtext.EvaluationStack.PushT(StackItem.New('a'))
        self.engine.ExecuteOp(OpCode.PICKITEM, self.econtext)

        self.assertEqual(len(self.econtext.EvaluationStack.Items), 1)
        self.assertEqual(self.econtext.EvaluationStack.Items[0].GetBigInteger(), 4)
Exemplo n.º 6
0
    def test_op_map2(self):
        self.econtext.Script._value = OpCode.NEWMAP + OpCode.SETITEM
        self.engine.ExecuteInstruction()

        self.econtext.EvaluationStack.PushT(StackItem.New('mykey'))
        self.econtext.EvaluationStack.PushT(StackItem.New('myVal'))
        self.engine.ExecuteInstruction()

        self.assertEqual(len(self.econtext.EvaluationStack.Items), 0)
Exemplo n.º 7
0
    def test_op_map10(self):
        # pick item key not found
        self.econtext.EvaluationStack.PushT(Map(dict={StackItem.New('a'): StackItem.New(4), StackItem.New('b'): StackItem.New(5)}))
        self.econtext.Script._value = OpCode.KEYS
        self.engine.ExecuteInstruction()

        self.assertIsInstance(self.econtext.EvaluationStack.Items[0], Array)
        items = self.econtext.EvaluationStack.Items[0].GetArray()
        self.assertEqual(items, [StackItem.New('a'), StackItem.New('b')])
    def test_op_map9(self):
        with self.assertLogHandler('vm', logging.DEBUG) as log_context:
            # pick item key not found
            self.econtext.EvaluationStack.PushT(Map(dict={StackItem.New('a'): StackItem.New(4)}))
            self.econtext.EvaluationStack.PushT(StackItem.New('b'))
            self.engine.ExecuteOp(OpCode.PICKITEM, self.econtext)

            self.assertEqual(self.engine.State, VMState.FAULT | VMState.BREAK)
            self.assertTrue(len(log_context.output) > 0)
            self.assertTrue('VMFault.DICT_KEY_NOT_FOUND' in log_context.output[0])
    def test_op_map7(self):
        with self.assertLogHandler('vm', logging.DEBUG) as log_context:
            # pick item with key is collection causes error
            self.econtext.EvaluationStack.PushT(Map(dict={StackItem.New('a'): StackItem.New(4)}))
            self.econtext.EvaluationStack.PushT(Map(dict={StackItem.New('a'): StackItem.New(4)}))
            self.engine.ExecuteOp(OpCode.PICKITEM, self.econtext)

            self.assertEqual(self.engine.State, VMState.FAULT | VMState.BREAK)
            self.assertTrue(len(log_context.output) > 0)
            self.assertTrue('VMFault.KEY_IS_COLLECTION' in log_context.output[0])
Exemplo n.º 10
0
    def test_op_map4(self, mocked_logger):
        # set item should fail if these are out of order
        self.econtext.EvaluationStack.PushT(StackItem.New('mykey'))
        self.engine.ExecuteOp(OpCode.NEWMAP, self.econtext)
        self.econtext.EvaluationStack.PushT(StackItem.New('myVal'))
        self.engine.ExecuteOp(OpCode.SETITEM, self.econtext)

        self.assertEqual(self.engine.State, VMState.FAULT | VMState.BREAK)

        mocked_logger.assert_called_with(StringIn('VMFault.KEY_IS_COLLECTION'))
Exemplo n.º 11
0
    def test_sub_operations(self):
        self.econtext.EvaluationStack.PushT(StackItem.New(2))
        self.econtext.EvaluationStack.PushT(StackItem.New(3))

        self.engine.ExecuteOp(OpCode.SUB, self.econtext)

        self.assertEqual(len(self.econtext.EvaluationStack.Items), 1)

        self.assertEqual(self.econtext.EvaluationStack.Items[0],
                         StackItem.New(-1))
    def test_op_map4(self):
        with self.assertLogHandler('vm', logging.DEBUG) as log_context:
            # set item should fail if these are out of order
            self.econtext.EvaluationStack.PushT(StackItem.New('mykey'))
            self.engine.ExecuteOp(OpCode.NEWMAP, self.econtext)
            self.econtext.EvaluationStack.PushT(StackItem.New('myVal'))
            self.engine.ExecuteOp(OpCode.SETITEM, self.econtext)

            self.assertEqual(self.engine.State, VMState.FAULT | VMState.BREAK)
            self.assertTrue(len(log_context.output) > 0)
            self.assertTrue('VMFault.KEY_IS_COLLECTION' in log_context.output[0])
Exemplo n.º 13
0
    def test_sub_operations(self):
        self.econtext.EvaluationStack.PushT(StackItem.New(2))
        self.econtext.EvaluationStack.PushT(StackItem.New(3))
        self.econtext.Script._value = OpCode.SUB

        self.engine.ExecuteInstruction()

        self.assertEqual(len(self.econtext.EvaluationStack.Items), 1)

        self.assertEqual(self.econtext.EvaluationStack.Items[0],
                         StackItem.New(-1))
    def test_op_map8(self):
        with self.assertLogHandler('vm', logging.DEBUG) as log_context:
            # pick item on non collection causes error
            self.econtext.EvaluationStack.PushT(StackItem.New('a'))
            self.econtext.EvaluationStack.PushT(StackItem.New('a'))
            self.engine.ExecuteOp(OpCode.PICKITEM, self.econtext)

            self.assertTrue(len(log_context.output) > 0)
            log_msg = log_context.output[0]
            self.assertTrue('Cannot access item at index' in log_msg and 'Item is not an array or dict' in log_msg)

            self.assertEqual(self.engine.State, VMState.FAULT | VMState.BREAK)
Exemplo n.º 15
0
    def test_op_map8(self):
        with self.assertLogHandler('vm', logging.DEBUG) as log_context:
            # pick item out of bounds
            self.econtext.EvaluationStack.PushT(StackItem.New('a'))
            self.econtext.EvaluationStack.PushT(StackItem.New('a'))
            self.engine.ExecuteOp(OpCode.PICKITEM, self.econtext)

            self.assertTrue(len(log_context.output) > 0)
            log_msg = log_context.output[0]
            expected_msg = "Array index is less than zero or 97 exceeds list length 1"
            self.assertTrue(expected_msg in log_msg)

            self.assertEqual(self.engine.State, VMState.FAULT)
    def test_op_map5(self):
        # need to set vm logging level to DEBUG or we will immediately exit `VM_FAULT_and_report()`
        with self.assertLogHandler('vm', logging.DEBUG) as log_context:
            # set item should fail if these are out of order
            self.econtext.EvaluationStack.PushT(StackItem.New('mykey'))
            self.econtext.EvaluationStack.PushT(StackItem.New('mykey'))
            self.econtext.EvaluationStack.PushT(StackItem.New('myVal'))
            self.engine.ExecuteOp(OpCode.SETITEM, self.econtext)

            self.assertEqual(self.engine.State, VMState.FAULT | VMState.BREAK)

            self.assertTrue(len(log_context.output) > 0)
            self.assertEqual(log_context.records[0].levelname, 'DEBUG')
            self.assertTrue('VMFault.SETITEM_INVALID_TYPE' in log_context.output[0])
Exemplo n.º 17
0
    def PushT(self, item):
        if not type(item) is StackItem and not issubclass(
                type(item), StackItem):
            item = StackItem.New(item)

        self._list.append(item)
        self._size += 1
Exemplo n.º 18
0
 def test_cant_deserialize_item(self, mocked_logger):
     self.econtext.EvaluationStack.PushT(StackItem.New(b'abc'))
     self.engine.InvocationStack.PushT(self.econtext)
     self.state_reader.Runtime_Deserialize(self.engine)
     item = self.econtext.EvaluationStack.Pop()
     self.assertIsNone(item)
     mocked_logger.assert_called_with(StringIn('Could not deserialize stack item with type:'))
Exemplo n.º 19
0
    def Value(self):
        """
        Get the value from the storage tuple of the current iteration.

        Returns:
            StackItem: with the storage value.
        """
        return StackItem.New(self.value)
Exemplo n.º 20
0
    def test_iter_map_bad(self):
        my_item = StackItem.New(12)
        self.econtext.EvaluationStack.PushT(my_item)
        self.engine.InvocationStack.PushT(self.econtext)
        result = self.service.Iterator_Create(self.engine)

        self.assertEqual(result, False)
        self.assertEqual(self.econtext.EvaluationStack.Count, 0)
Exemplo n.º 21
0
    def test_iter_concat(self):

        my_array = Array([
            StackItem.New(12),
            StackItem.New(b'Hello World'),
            StackItem.New(True),
            Array([StackItem.New(113442),
                   StackItem.New(2),
                   StackItem.New(3)])
        ])

        my_array2 = Array([
            StackItem.New(b'a'),
            StackItem.New(b'b'),
            StackItem.New(4),
            StackItem.New(100)
        ])

        self.engine.EvaluationStack.PushT(my_array2)

        self.state_reader.Enumerator_Create(self.engine)

        self.engine.EvaluationStack.PushT(my_array)

        self.state_reader.Enumerator_Create(self.engine)

        result = self.state_reader.Enumerator_Concat(self.engine)

        self.assertEqual(result, True)

        concatted_enum = self.engine.EvaluationStack.Peek().GetInterface()

        self.assertIsInstance(concatted_enum, ConcatenatedEnumerator)

        values = []
        count = 0

        while concatted_enum.Next():

            count += 1
            values.append(concatted_enum.Value())

        self.assertEqual(count, 8)

        self.assertEqual(values, my_array.GetArray() + my_array2.GetArray())
    def test_serialize_zero(self):
        # create integer, serialize it via state reader
        my_integer = StackItem.New(0)
        self.engine.EvaluationStack.PushT(my_integer)
        self.state_reader.Runtime_Serialize(self.engine)
        self.state_reader.Runtime_Deserialize(self.engine)

        deserialized = self.engine.EvaluationStack.Pop()
        self.assertEqual(deserialized.GetBigInteger(), 0)
Exemplo n.º 23
0
 def test_cant_deserialize_item(self):
     with self.assertLogHandler('vm', logging.DEBUG) as log_context:
         self.econtext.EvaluationStack.PushT(StackItem.New(b'abc'))
         self.engine.InvocationStack.PushT(self.econtext)
         success = self.state_reader.Runtime_Deserialize(self.engine)
         self.assertFalse(success)
         self.assertTrue(len(log_context.output) > 0)
         expected_msg = 'Could not deserialize stack item with type:'
         self.assertTrue(expected_msg in log_context.output[0])
Exemplo n.º 24
0
    def test_iter_array_keys_bad(self):

        my_item = StackItem.New(12)
        self.econtext.EvaluationStack.PushT(my_item)
        self.engine.InvocationStack.PushT(self.econtext)
        result = self.state_reader.Iterator_Keys(self.engine)

        self.assertEqual(result, False)
        self.assertEqual(self.econtext.EvaluationStack.Count, 0)