<s> from __future__ import absolute_import , division , print_function <EOL> import certifi <EOL> import pem <EOL> from . data import ( <EOL> CERT_NO_NEW_LINE , <EOL> CERT_PEMS , <EOL> CERT_PEMS_NO_NEW_LINE , <EOL> DH_PEM , <EOL> KEY_PEM , <EOL> ) <EOL> TEST_DIGEST = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> class TestPEMObjects ( object ) : <EOL> def test_cert_has_correct_repr ( self ) : <EOL> """<STR_LIT>""" <EOL> cert = pem . Certificate ( b"<STR_LIT:test>" ) <EOL> assert "<STR_LIT>" . format ( TEST_DIGEST ) == repr ( cert ) <EOL> def test_cert_has_correct_str ( self ) : <EOL> """<STR_LIT>""" <EOL> cert = pem . Certificate ( b"<STR_LIT:test>" ) <EOL> assert str ( cert ) == "<STR_LIT:test>" <EOL> def test_key_has_correct_repr ( self ) : <EOL> """<STR_LIT>""" <EOL> key = pem . Key ( b"<STR_LIT:test>" ) <EOL> assert "<STR_LIT>" . format ( TEST_DIGEST ) == repr ( key ) <EOL> def test_key_has_correct_str ( self ) : <EOL> """<STR_LIT>""" <EOL> key = pem . Key ( b"<STR_LIT:test>" ) <EOL> assert str ( key ) == "<STR_LIT:test>" <EOL> def test_rsa_key_has_correct_repr ( self ) : <EOL> """<STR_LIT>""" <EOL> key = pem . RSAPrivateKey ( b"<STR_LIT:test>" ) <EOL> assert "<STR_LIT>" . format ( TEST_DIGEST ) == repr ( key ) <EOL> def test_rsa_key_has_correct_str ( self ) : <EOL> """<STR_LIT>""" <EOL> key = pem . RSAPrivateKey ( b"<STR_LIT:test>" ) <EOL> assert str ( key ) == "<STR_LIT:test>" <EOL> def test_dh_params_has_correct_repr ( self ) : <EOL> """<STR_LIT>""" <EOL> params = pem . DHParameters ( b"<STR_LIT:test>" ) <EOL> assert "<STR_LIT>" . format ( TEST_DIGEST ) == repr ( params ) <EOL> def test_dh_params_has_correct_str ( self ) : <EOL> """<STR_LIT>""" <EOL> params = pem . DHParameters ( b"<STR_LIT:test>" ) <EOL> assert str ( params ) == "<STR_LIT:test>" <EOL> def test_certificate_unicode ( self ) : <EOL> """<STR_LIT>""" <EOL> cert = pem . Certificate ( u'<STR_LIT>' ) <EOL> assert cert . as_bytes ( ) == b'<STR_LIT>' <EOL> assert str ( cert ) == '<STR_LIT>' <EOL> def test_key_unicode ( self ) : <EOL> """<STR_LIT>""" <EOL> key = pem . Key ( u'<STR_LIT>' ) <EOL> assert key . as_bytes ( ) == b'<STR_LIT>' <EOL> assert str ( key ) == '<STR_LIT>' <EOL> def test_rsa_key_unicode ( self ) : <EOL> """<STR_LIT>""" <EOL> key = pem . RSAPrivateKey ( u'<STR_LIT>' ) <EOL> assert key . as_bytes ( ) == b'<STR_LIT>' <EOL> assert str ( key ) == '<STR_LIT>' <EOL> def test_dhparams_unicode_deprecated ( self ) : <EOL> """<STR_LIT>""" <EOL> params = pem . DHParameters ( u'<STR_LIT>' ) <EOL> assert params . as_bytes ( ) == b'<STR_LIT>' <EOL> assert str ( params ) == '<STR_LIT>' <EOL> def test_certs_equal ( self ) : <EOL> """<STR_LIT>""" <EOL> cert1 = pem . Certificate ( b"<STR_LIT:test>" ) <EOL> cert2 = pem . Certificate ( b"<STR_LIT:test>" ) <EOL> assert cert1 == cert2 <EOL> assert cert2 == cert1 <EOL> assert hash ( cert1 ) == hash ( cert2 ) <EOL> def test_keys_equal ( self ) : <EOL> """<STR_LIT>""" <EOL> key1 = pem . Key ( b"<STR_LIT:test>" ) <EOL> key2 = pem . Key ( b"<STR_LIT:test>" ) <EOL> assert key1 == key2 <EOL> assert key2 == key1 <EOL> assert hash ( key1 ) == hash ( key2 ) <EOL> def test_rsa_keys_equal ( self ) : <EOL> """<STR_LIT>""" <EOL> key1 = pem . RSAPrivateKey ( b"<STR_LIT:test>" ) <EOL> key2 = pem . RSAPrivateKey ( b"<STR_LIT:test>" ) <EOL> assert key1 == key2 <EOL> assert key2 == key1 <EOL> assert hash ( key1 ) == hash ( key2 ) <EOL> def test_dh_params_equal ( self ) : <EOL> """<STR_LIT>""" <EOL> params1 = pem . DHParameters ( b"<STR_LIT:test>" ) <EOL> params2 = pem . DHParameters ( b"<STR_LIT:test>" ) <EOL> assert params1 == params2 <EOL> assert params2 == params1 <EOL> assert hash ( params1 ) == hash ( params2 ) <EOL> def test_cert_contents_unequal ( self ) : <EOL> """<STR_LIT>""" <EOL> cert1 = pem . Certificate ( b"<STR_LIT>" ) <EOL> cert2 = pem . Certificate ( b"<STR_LIT>" ) <EOL> assert cert1 != cert2 <EOL> assert cert2 != cert1 <EOL> def test_different_objects_unequal ( self ) : <EOL> """<STR_LIT>""" <EOL> cert = pem . Certificate ( b"<STR_LIT:test>" ) <EOL> key = pem . Key ( b"<STR_LIT:test>" ) <EOL> rsa_key = pem . RSAPrivateKey ( b"<STR_LIT:test>" ) <EOL> assert cert != key <EOL> assert key != cert <EOL> assert key != rsa_key <EOL> assert rsa_key != key <EOL> def test_incompatible_types ( self ) : <EOL> """<STR_LIT>""" <EOL> cert = pem . Certificate ( b"<STR_LIT:test>" ) <EOL> assert cert != object ( ) <EOL> assert object ( ) != cert <EOL> class TestParse ( object ) : <EOL> def test_key ( self ) : <EOL> """<STR_LIT>""" <EOL> rv = pem . parse ( KEY_PEM ) <EOL> key , = rv <EOL> assert isinstance ( key , pem . RSAPrivateKey ) <EOL> assert KEY_PEM == key . as_bytes ( ) <EOL> def test_certificates ( self ) : <EOL> """<STR_LIT>""" <EOL> certs = pem . parse ( b'<STR_LIT>' . join ( CERT_PEMS ) ) <EOL> assert all ( isinstance ( c , pem . Certificate ) for c in certs ) <EOL> assert CERT_PEMS == [ cert . as_bytes ( ) for cert in certs ] <EOL> def test_certificate_no_new_line ( self ) : <EOL> """<STR_LIT>""" <EOL> cert , = pem . parse ( CERT_NO_NEW_LINE ) <EOL> assert isinstance ( cert , pem . Certificate ) <EOL> assert CERT_NO_NEW_LINE == cert . as_bytes ( ) <EOL> def test_certificates_no_new_line ( self ) : <EOL> """<STR_LIT>""" <EOL> certs = pem . parse ( b'<STR_LIT>' . join ( CERT_PEMS_NO_NEW_LINE ) ) <EOL> assert all ( isinstance ( c , pem . Certificate ) for c in certs ) <EOL> assert CERT_PEMS_NO_NEW_LINE == [ cert . as_bytes ( ) for cert in certs ] <EOL> def test_dh ( self ) : <EOL> """<STR_LIT>""" <EOL> rv = pem . parse ( DH_PEM ) <EOL> dh , = rv <EOL> assert isinstance ( dh , pem . DHParameters ) <EOL> assert DH_PEM == dh . as_bytes ( ) <EOL> def test_file ( self , tmpdir ) : <EOL> """<STR_LIT>""" <EOL> certs_file = tmpdir . join ( '<STR_LIT>' ) <EOL> certs_file . write ( b'<STR_LIT>' . join ( CERT_PEMS ) ) <EOL> certs = pem . parse_file ( str ( certs_file ) ) <EOL> assert all ( isinstance ( c , pem . Certificate ) for c in certs ) <EOL> assert CERT_PEMS == [ cert . as_bytes ( ) for cert in certs ] <EOL> def test_loads_certifi ( self ) : <EOL> """<STR_LIT>""" <EOL> cas = pem . parse_file ( certifi . where ( ) ) <EOL> assert isinstance ( cas , list ) <EOL> assert all ( isinstance ( ca , pem . Certificate ) for ca in cas ) <EOL> def test_allows_lf ( self ) : <EOL> """<STR_LIT>""" <EOL> lf_pem = KEY_PEM . replace ( b"<STR_LIT:\n>" , b"<STR_LIT:\r\n>" ) <EOL> rv , = pem . parse ( lf_pem ) <EOL> assert rv . as_bytes ( ) == lf_pem </s>
