def test_encode_decode_read_multiple_tags(self):
        @types.Explicit(8)
        class NewDummy(Dummy):
            pass

        value = "hello"
        d = NewDummy(value=value)
        enc = d.encode()
        encoded_inner_length = types.encode_length(len(value))

        inner = Dummy.tags[0].value + encoded_inner_length + value
        encoded_length = types.encode_length(len(inner))

        expected = NewDummy.tags[1].value + encoded_length + inner
        self.assertEqual(expected.encode("hex"), enc.encode("hex"))

        decoded_dummy = NewDummy.decode(enc)
        self.assertTrue(isinstance(decoded_dummy, NewDummy))
        self.assertEqual(decoded_dummy.value, value)

        read_dummy, rest = NewDummy.read(enc)
        self.assertTrue(isinstance(read_dummy, NewDummy))
        self.assertEqual(read_dummy.value, value)
        self.assertEqual("", rest)

        indef_encoding = "a880010568656c6c6f0000".decode("hex")
        self.assertRaises(error.ASN1Error, NewDummy.decode, indef_encoding)
        self.assertEqual(NewDummy.decode(indef_encoding, strict=False),
                         NewDummy(value="hello"))
    def test_encode_decode_read_multiple_tags(self):
        @types.Explicit(8)
        class NewDummy(Dummy):
            pass

        value = "hello"
        d = NewDummy(value=value)
        enc = d.encode()
        encoded_inner_length = types.encode_length(len(value))

        inner = Dummy.tags[0].value + encoded_inner_length + value
        encoded_length = types.encode_length(len(inner))

        expected = NewDummy.tags[1].value + encoded_length + inner
        self.assertEqual(expected.encode("hex"), enc.encode("hex"))

        decoded_dummy = NewDummy.decode(enc)
        self.assertTrue(isinstance(decoded_dummy, NewDummy))
        self.assertEqual(decoded_dummy.value, value)

        read_dummy, rest = NewDummy.read(enc)
        self.assertTrue(isinstance(read_dummy, NewDummy))
        self.assertEqual(read_dummy.value, value)
        self.assertEqual("", rest)

        indef_encoding = "a880010568656c6c6f0000".decode("hex")
        self.assertRaises(error.ASN1Error, NewDummy.decode, indef_encoding)
        self.assertEqual(NewDummy.decode(indef_encoding, strict=False),
                         NewDummy(value="hello"))
    def test_encode_read_length(self):
        length_encodings = (
            (0, "00"),
            (1, "01"),
            (38, "26"),
            (127, "7f"),
            (129, "8181"),
            (201, "81c9"),
            (65535, "82ffff"),
            (65536, "83010000")
            )

        for value, enc in length_encodings:
            self.assertEqual(types.encode_length(value).encode("hex"), enc)
            self.assertEqual(types.read_length(enc.decode("hex")), (value, ""))
            # Test that the reader stops after the specified number of bytes.
            longer = enc + "00"
            self.assertEqual(types.read_length(longer.decode("hex")),
                             (value, "\x00"))
            longer = enc + "ff"
            self.assertEqual(types.read_length(longer.decode("hex")),
                             (value, "\xff"))
            # And test that it complains when there are not enough bytes.
            shorter = enc[:-2]
            self.assertRaises(error.ASN1Error,
                              types.read_length, shorter.decode("hex"))
Ejemplo n.º 4
0
    def test_encode_read_length(self):
        length_encodings = (
            (0, "00"),
            (1, "01"),
            (38, "26"),
            (127, "7f"),
            (129, "8181"),
            (201, "81c9"),
            (65535, "82ffff"),
            (65536, "83010000")
            )

        for value, enc in length_encodings:
            self.assertEqual(types.encode_length(value).encode("hex"), enc)
            self.assertEqual(types.read_length(enc.decode("hex")), (value, ""))
            # Test that the reader stops after the specified number of bytes.
            longer = enc + "00"
            self.assertEqual(types.read_length(longer.decode("hex")),
                             (value, "\x00"))
            longer = enc + "ff"
            self.assertEqual(types.read_length(longer.decode("hex")),
                             (value, "\xff"))
            # And test that it complains when there are not enough bytes.
            shorter = enc[:-2]
            self.assertRaises(error.ASN1Error,
                              types.read_length, shorter.decode("hex"))
    def test_encode_decode_read_multiple_tags(self):
        @types.Explicit(8)
        class NewDummy(Dummy):
            pass

        value = "hello"
        d = NewDummy(value=value)
        enc = d.encode()
        encoded_inner_length = types.encode_length(len(value))

        inner = Dummy.tags[0].value + encoded_inner_length + value
        encoded_length = types.encode_length(len(inner))

        expected = NewDummy.tags[1].value + encoded_length + inner
        self.assertEqual(expected.encode("hex"), enc.encode("hex"))

        decoded_dummy = NewDummy.decode(enc)
        self.assertTrue(isinstance(decoded_dummy, NewDummy))
        self.assertEqual(decoded_dummy.value, value)

        read_dummy, rest = NewDummy.read(enc)
        self.assertTrue(isinstance(read_dummy, NewDummy))
        self.assertEqual(read_dummy.value, value)
        self.assertEqual("", rest)
    def test_encode_decode_read(self):
        value = "hello"
        d = Dummy(value=value)
        enc = d.encode()
        encoded_length = types.encode_length(len(value))

        expected = Dummy.tags[0].value + encoded_length + value
        self.assertEqual(expected.encode("hex"), enc.encode("hex"))

        decoded_dummy = Dummy.decode(enc)
        self.assertTrue(isinstance(decoded_dummy, Dummy))
        self.assertEqual(decoded_dummy.value, value)

        read_dummy, rest = Dummy.read(enc)
        self.assertTrue(isinstance(read_dummy, Dummy))
        self.assertEqual(read_dummy.value, value)
        self.assertEqual("", rest)
    def test_encode_decode_read(self):
        value = "hello"
        d = Dummy(value=value)
        enc = d.encode()
        encoded_length = types.encode_length(len(value))

        expected = Dummy.tags[0].value + encoded_length + value
        self.assertEqual(expected.encode("hex"), enc.encode("hex"))

        decoded_dummy = Dummy.decode(enc)
        self.assertTrue(isinstance(decoded_dummy, Dummy))
        self.assertEqual(decoded_dummy.value, value)

        read_dummy, rest = Dummy.read(enc)
        self.assertTrue(isinstance(read_dummy, Dummy))
        self.assertEqual(read_dummy.value, value)
        self.assertEqual("", rest)
    def test_read_from_beginning(self):
        value = "hello"
        d = Dummy(value=value)
        self.assertEqual("hello", d.value)
        enc = d.encode()

        encoded_length = types.encode_length(len(d.value))
        expected = Dummy.tags[0].value + encoded_length + d.value
        self.assertEqual(expected.encode("hex"), enc.encode("hex"))

        longer_buffer = enc + "ello"
        # We can't decode because there are leftover bytes...
        self.assertRaises(error.ASN1Error, Dummy.decode, longer_buffer)

        # ... but we can read from the beginning of the buffer.
        read_dummy, rest = Dummy.read(longer_buffer)
        self.assertTrue(isinstance(read_dummy, Dummy))
        self.assertEqual("hello", read_dummy.value)
        self.assertEqual("ello", rest)
    def test_read_from_beginning(self):
        value = "hello"
        d = Dummy(value=value)
        self.assertEqual("hello", d.value)
        enc = d.encode()

        encoded_length = types.encode_length(len(d.value))
        expected = Dummy.tags[0].value + encoded_length + d.value
        self.assertEqual(expected.encode("hex"), enc.encode("hex"))

        longer_buffer = enc + "ello"
        # We can't decode because there are leftover bytes...
        self.assertRaises(error.ASN1Error, Dummy.decode, longer_buffer)

        # ... but we can read from the beginning of the buffer.
        read_dummy, rest = Dummy.read(longer_buffer)
        self.assertTrue(isinstance(read_dummy, Dummy))
        self.assertEqual("hello", read_dummy.value)
        self.assertEqual("ello", rest)