def test_decode_inner(self):
     dummy = Dummy(value="hello")
     a = types.Any(dummy)
     self.assertTrue(a.decoded)
     self.assertEqual(a.decoded_value, dummy)
     enc = dummy.encode()
     a2 = types.Any(enc)
     self.assertFalse(a2.decoded)
     self.assertEqual(a, a2)
     self.assertEqual(a.value, a2.value)
     a2.decode_inner(value_type=Dummy)
     self.assertTrue(a2.decoded)
     self.assertEqual(a2.decoded_value, dummy)
    def test_modify(self):
        dummy = DummySequence(value={"bool": True, "any": "\x01\x01\xff"})
        a = types.Any(dummy)
        self.assertFalse(a.modified())
        a.decoded_value["bool"] = False
        self.assertTrue(a.modified())

        enc = dummy.encode()
        a2 = types.Any(enc)
        self.assertFalse(a2.modified())
        a2.decode_inner(value_type=DummySequence)
        self.assertFalse(a2.modified())
        a2.decoded_value["bool"] = True
        self.assertTrue(a2.modified())
class AnyTest(type_test_base.TypeTestBase):
    asn1_type = types.Any
    repeated = False
    keyed = False
    initializers = (
        # Decoded and undecoded initializers.
        # Test with a few simple types.
        (types.Boolean(value=True).encode(), types.Boolean(value=True)),
        (types.Integer(value=3).encode(), types.Integer(value=3)),
        (types.OctetString("hello").encode(), types.OctetString("hello")),
        # We don't currently check that the encoded value encodes a valid
        # tag-length-value triplet, so this will also succeed,
        (
            "0000ff", ),
        ("", ))
    bad_initializers = ((types.Any("hello"), TypeError), )
    encode_test_vectors = (
        # A Boolean True.
        ("\x01\x01\xff", "0101ff"),
        # An Integer 3.
        ("\x02\x01\x03", "020103"),
        # An octet string "hello".
        ("\x04\x05\x68\x65\x6c\x6c\x6f", "040568656c6c6f"),
    )
    bad_encodings = ()
    bad_strict_encodings = ()

    def test_decode_inner(self):
        dummy = Dummy(value="hello")
        a = types.Any(dummy)
        self.assertTrue(a.decoded)
        self.assertEqual(a.decoded_value, dummy)
        enc = dummy.encode()
        a2 = types.Any(enc)
        self.assertFalse(a2.decoded)
        self.assertEqual(a, a2)
        self.assertEqual(a.value, a2.value)
        a2.decode_inner(value_type=Dummy)
        self.assertTrue(a2.decoded)
        self.assertEqual(a2.decoded_value, dummy)

    def test_modify(self):
        dummy = DummySequence(value={"bool": True, "any": "\x01\x01\xff"})
        a = types.Any(dummy)
        self.assertFalse(a.modified())
        a.decoded_value["bool"] = False
        self.assertTrue(a.modified())

        enc = dummy.encode()
        a2 = types.Any(enc)
        self.assertFalse(a2.modified())
        a2.decode_inner(value_type=DummySequence)
        self.assertFalse(a2.modified())
        a2.decoded_value["bool"] = True
        self.assertTrue(a2.modified())