def test_parse_pubkey_payload_errors(self):
        """ Test parse_pubkey_payload errors with manually crafted data. """
        # passed data | expected error | expected error message
        test_data = [(None, ValueError, "empty pubkey"),
                     (bytearray([0x03]), PacketVersionNotSupportedError,
                      "packet version '3' not supported"),
                     (bytearray([0x04, 0, 0, 0, 0,
                                 255]), SignatureAlgorithmNotSupportedError,
                      "Signature algorithm '255' not supported")]

        for data, error, error_str in test_data:
            with self.assertRaises(error) as ctx:
                parse_pubkey_payload(data)
            self.assertTrue(error_str in str(ctx.exception))
    def test_parse_pubkey_bundle(self):
        """Assert presence of packets expected returned from `parse_pubkey_bundle`
    for specific test key). See
    ```
    gpg --homedir tests/gpg_keyrings/rsa/ --export 9EA70BD13D883381 | \
        gpg --list-packets
    ```
    """
        # Expect parsed primary key matching GPG_PUBKEY_SCHEMA
        self.assertTrue(
            GPG_PUBKEY_SCHEMA.matches(
                self.raw_key_bundle[PACKET_TYPE_PRIMARY_KEY]["key"]))

        # Parse corresponding raw packet for comparison
        _, header_len, _, _ = parse_packet_header(
            self.raw_key_bundle[PACKET_TYPE_PRIMARY_KEY]["packet"])

        # pylint: disable=unsubscriptable-object
        parsed_raw_packet = parse_pubkey_payload(
            bytearray(self.raw_key_bundle[PACKET_TYPE_PRIMARY_KEY]["packet"]
                      [header_len:]))

        # And compare
        self.assertDictEqual(
            self.raw_key_bundle[PACKET_TYPE_PRIMARY_KEY]["key"],
            parsed_raw_packet)

        # Expect one primary key signature (revocation signature)
        self.assertEqual(
            len(self.raw_key_bundle[PACKET_TYPE_PRIMARY_KEY]["signatures"]), 1)

        # Expect one User ID packet, one User Attribute packet and one Subkey,
        # each with correct data
        for _type in [
                PACKET_TYPE_USER_ID, PACKET_TYPE_USER_ATTR, PACKET_TYPE_SUB_KEY
        ]:
            # Of each type there is only one packet
            self.assertTrue(len(self.raw_key_bundle[_type]) == 1)
            # The raw packet is stored as key in the per-packet type collection
            raw_packet = next(iter(self.raw_key_bundle[_type]))
            # Its values are the raw packets header and body length
            self.assertEqual(
                len(raw_packet),
                self.raw_key_bundle[_type][raw_packet]["header_len"] +
                self.raw_key_bundle[_type][raw_packet]["body_len"])
            # and one self-signature
            self.assertEqual(
                len(self.raw_key_bundle[_type][raw_packet]["signatures"]), 1)