@@ -3124,6 +3124,73 @@ def test_public_bytes(self):
31243124 assert ext .public_bytes () == b"\x30 \x03 \x81 \x01 \x00 "
31253125
31263126
3127+ class TestPolicyMappings :
3128+ issuer_policy = ObjectIdentifier ("1.2.3.4" )
3129+ subject_policy = ObjectIdentifier ("1.2.3.5" )
3130+ mappings = [(issuer_policy , subject_policy )]
3131+
3132+ def test_invalid_mappings (self ):
3133+ with pytest .raises (TypeError ):
3134+ x509 .PolicyMappings (
3135+ [(self .issuer_policy , typing .cast (typing .Any , "invalid" ))]
3136+ )
3137+ with pytest .raises (TypeError ):
3138+ x509 .PolicyMappings (
3139+ [typing .cast (typing .Any , (self .issuer_policy ,))]
3140+ )
3141+
3142+ def test_empty_mappings (self ):
3143+ with pytest .raises (ValueError ):
3144+ x509 .PolicyMappings ([])
3145+
3146+ @pytest .mark .parametrize ("position" , [0 , 1 ])
3147+ def test_any_policy (self , position ):
3148+ mapping = list (self .mappings [0 ])
3149+ mapping [position ] = x509 .CertificatePoliciesOID .ANY_POLICY
3150+ with pytest .raises (ValueError , match = "must not contain anyPolicy" ):
3151+ x509 .PolicyMappings ([tuple (mapping )]) # type: ignore[list-item]
3152+
3153+ def test_iter_len_index (self ):
3154+ mappings = x509 .PolicyMappings (iter (self .mappings ))
3155+ assert len (mappings ) == 1
3156+ assert list (mappings ) == self .mappings
3157+ assert mappings [0 ] == self .mappings [0 ]
3158+
3159+ def test_repr (self ):
3160+ assert repr (x509 .PolicyMappings (self .mappings )) == (
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+ mappings = x509 .PolicyMappings (self .mappings )
3167+ mappings2 = x509 .PolicyMappings (self .mappings )
3168+ mappings3 = x509 .PolicyMappings (
3169+ [(self .subject_policy , self .issuer_policy )]
3170+ )
3171+ assert mappings == mappings2
3172+ assert mappings != mappings3
3173+ assert mappings != object ()
3174+ assert hash (mappings ) == hash (mappings2 )
3175+ assert hash (mappings ) != hash (mappings3 )
3176+
3177+ def test_public_bytes (self ):
3178+ mappings = x509 .PolicyMappings (self .mappings )
3179+ assert mappings .public_bytes () == (
3180+ b"\x30 \x0c \x30 \x0a \x06 \x03 \x2a \x03 \x04 \x06 \x03 \x2a \x03 \x05 "
3181+ )
3182+
3183+ def test_certbuilder (self , rsa_key_2048 : rsa .RSAPrivateKey ):
3184+ cert = (
3185+ _make_certbuilder (rsa_key_2048 )
3186+ .add_extension (x509 .PolicyMappings (self .mappings ), critical = True )
3187+ .sign (rsa_key_2048 , hashes .SHA256 ())
3188+ )
3189+ ext = cert .extensions .get_extension_for_class (x509 .PolicyMappings )
3190+ assert ext .critical is True
3191+ assert list (ext .value ) == self .mappings
3192+
3193+
31273194class TestAuthorityInformationAccess :
31283195 def test_invalid_descriptions (self ):
31293196 with pytest .raises (TypeError ):
0 commit comments