|
28 | 28 | ) |
29 | 29 | from cryptography.x509.oid import ( |
30 | 30 | AuthorityInformationAccessOID, |
| 31 | + CertificatePoliciesOID, |
31 | 32 | ExtendedKeyUsageOID, |
32 | 33 | ExtensionOID, |
33 | 34 | NameOID, |
@@ -3124,6 +3125,73 @@ def test_public_bytes(self): |
3124 | 3125 | assert ext.public_bytes() == b"\x30\x03\x81\x01\x00" |
3125 | 3126 |
|
3126 | 3127 |
|
| 3128 | +class TestPolicyMappings: |
| 3129 | + issuer_policy = ObjectIdentifier("1.2.3.4") |
| 3130 | + subject_policy = ObjectIdentifier("1.2.3.5") |
| 3131 | + mapping = ((issuer_policy, subject_policy),) |
| 3132 | + |
| 3133 | + def test_invalid_mappings(self): |
| 3134 | + with pytest.raises(TypeError): |
| 3135 | + x509.PolicyMappings( |
| 3136 | + [(self.issuer_policy, typing.cast(typing.Any, "invalid"))] |
| 3137 | + ) |
| 3138 | + with pytest.raises(TypeError): |
| 3139 | + x509.PolicyMappings( |
| 3140 | + [typing.cast(typing.Any, (self.issuer_policy,))] |
| 3141 | + ) |
| 3142 | + |
| 3143 | + def test_empty_mappings(self): |
| 3144 | + with pytest.raises(ValueError): |
| 3145 | + x509.PolicyMappings([]) |
| 3146 | + |
| 3147 | + @pytest.mark.parametrize("position", [0, 1]) |
| 3148 | + def test_any_policy(self, position): |
| 3149 | + mapping = list(self.mapping[0]) |
| 3150 | + mapping[position] = CertificatePoliciesOID.ANY_POLICY |
| 3151 | + with pytest.raises(ValueError, match="must not contain anyPolicy"): |
| 3152 | + x509.PolicyMappings([tuple(mapping)]) # type: ignore[list-item] |
| 3153 | + |
| 3154 | + def test_iter_len_index(self): |
| 3155 | + mappings = x509.PolicyMappings(self.mapping) |
| 3156 | + assert len(mappings) == 1 |
| 3157 | + assert list(mappings) == list(self.mapping) |
| 3158 | + |
| 3159 | + def test_repr(self): |
| 3160 | + assert repr(x509.PolicyMappings(self.mapping)) == ( |
| 3161 | + "<PolicyMappings([(<ObjectIdentifier(oid=1.2.3.4, name=Unknown " |
| 3162 | + "OID)>, <ObjectIdentifier(oid=1.2.3.5, name=Unknown OID)>)])>" |
| 3163 | + ) |
| 3164 | + |
| 3165 | + def test_eq_hash(self): |
| 3166 | + iss_to_sub = [(self.issuer_policy, self.subject_policy)] |
| 3167 | + sub_to_iss = [(self.subject_policy, self.issuer_policy)] |
| 3168 | + |
| 3169 | + mappings = x509.PolicyMappings(iss_to_sub) |
| 3170 | + mappings2 = x509.PolicyMappings(iss_to_sub) |
| 3171 | + mappings3 = x509.PolicyMappings(sub_to_iss) |
| 3172 | + assert mappings == mappings2 |
| 3173 | + assert mappings != mappings3 |
| 3174 | + assert mappings != object() |
| 3175 | + assert hash(mappings) == hash(mappings2) |
| 3176 | + assert hash(mappings) != hash(mappings3) |
| 3177 | + |
| 3178 | + def test_public_bytes(self): |
| 3179 | + mappings = x509.PolicyMappings(self.mapping) |
| 3180 | + assert mappings.public_bytes() == ( |
| 3181 | + b"\x30\x0c\x30\x0a\x06\x03\x2a\x03\x04\x06\x03\x2a\x03\x05" |
| 3182 | + ) |
| 3183 | + |
| 3184 | + def test_certbuilder(self, rsa_key_2048: rsa.RSAPrivateKey): |
| 3185 | + cert = ( |
| 3186 | + _make_certbuilder(rsa_key_2048) |
| 3187 | + .add_extension(x509.PolicyMappings(self.mapping), critical=True) |
| 3188 | + .sign(rsa_key_2048, hashes.SHA256()) |
| 3189 | + ) |
| 3190 | + ext = cert.extensions.get_extension_for_class(x509.PolicyMappings) |
| 3191 | + assert ext.critical is True |
| 3192 | + assert list(ext.value) == list(self.mapping) |
| 3193 | + |
| 3194 | + |
3127 | 3195 | class TestAuthorityInformationAccess: |
3128 | 3196 | def test_invalid_descriptions(self): |
3129 | 3197 | with pytest.raises(TypeError): |
|
0 commit comments