<s> from __future__ import absolute_import , division , print_function <EOL> import pytest <EOL> from cryptography . exceptions import InternalError <EOL> from cryptography . hazmat . bindings . openssl . binding import ( <EOL> Binding , _OpenSSLErrorWithText , _openssl_assert , _verify_openssl_version <EOL> ) <EOL> class TestOpenSSL ( object ) : <EOL> def test_binding_loads ( self ) : <EOL> binding = Binding ( ) <EOL> assert binding <EOL> assert binding . lib <EOL> assert binding . ffi <EOL> def test_crypto_lock_init ( self ) : <EOL> b = Binding ( ) <EOL> b . init_static_locks ( ) <EOL> lock_cb = b . lib . CRYPTO_get_locking_callback ( ) <EOL> assert lock_cb != b . ffi . NULL <EOL> def _skip_if_not_fallback_lock ( self , b ) : <EOL> original_cb = b . lib . CRYPTO_get_locking_callback ( ) <EOL> if original_cb != b . _lock_cb_handle : <EOL> pytest . skip ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_fallback_crypto_lock_via_openssl_api ( self ) : <EOL> b = Binding ( ) <EOL> b . init_static_locks ( ) <EOL> self . _skip_if_not_fallback_lock ( b ) <EOL> lock = b . _locks [ b . lib . CRYPTO_LOCK_SSL ] <EOL> assert lock . acquire ( False ) <EOL> lock . release ( ) <EOL> b . lib . CRYPTO_lock ( <EOL> b . lib . CRYPTO_LOCK | b . lib . CRYPTO_READ , <EOL> b . lib . CRYPTO_LOCK_SSL , b . ffi . NULL , <NUM_LIT:0> <EOL> ) <EOL> assert not lock . acquire ( False ) <EOL> b . lib . CRYPTO_lock ( <EOL> b . lib . CRYPTO_UNLOCK | b . lib . CRYPTO_READ , <EOL> b . lib . CRYPTO_LOCK_SSL , b . ffi . NULL , <NUM_LIT:0> <EOL> ) <EOL> assert lock . acquire ( False ) <EOL> lock . release ( ) <EOL> def test_fallback_crypto_lock_via_binding_api ( self ) : <EOL> b = Binding ( ) <EOL> b . init_static_locks ( ) <EOL> self . _skip_if_not_fallback_lock ( b ) <EOL> lock = b . _locks [ b . lib . CRYPTO_LOCK_SSL ] <EOL> with pytest . raises ( RuntimeError ) : <EOL> b . _lock_cb ( <NUM_LIT:0> , b . lib . CRYPTO_LOCK_SSL , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> assert lock . acquire ( False ) <EOL> lock . release ( ) <EOL> b . _lock_cb ( b . lib . CRYPTO_LOCK | b . lib . CRYPTO_READ , <EOL> b . lib . CRYPTO_LOCK_SSL , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> assert not lock . acquire ( False ) <EOL> b . _lock_cb ( b . lib . CRYPTO_UNLOCK | b . lib . CRYPTO_READ , <EOL> b . lib . CRYPTO_LOCK_SSL , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> assert lock . acquire ( False ) <EOL> lock . release ( ) <EOL> def test_add_engine_more_than_once ( self ) : <EOL> b = Binding ( ) <EOL> b . _register_osrandom_engine ( ) <EOL> assert b . lib . ERR_get_error ( ) == <NUM_LIT:0> <EOL> def test_ssl_ctx_options ( self ) : <EOL> b = Binding ( ) <EOL> assert b . lib . SSL_OP_ALL > <NUM_LIT:0> <EOL> ctx = b . lib . SSL_CTX_new ( b . lib . TLSv1_method ( ) ) <EOL> ctx = b . ffi . gc ( ctx , b . lib . SSL_CTX_free ) <EOL> current_options = b . lib . SSL_CTX_get_options ( ctx ) <EOL> resp = b . lib . SSL_CTX_set_options ( ctx , b . lib . SSL_OP_ALL ) <EOL> expected_options = current_options | b . lib . SSL_OP_ALL <EOL> assert resp == expected_options <EOL> assert b . lib . SSL_CTX_get_options ( ctx ) == expected_options <EOL> def test_ssl_options ( self ) : <EOL> b = Binding ( ) <EOL> assert b . lib . SSL_OP_ALL > <NUM_LIT:0> <EOL> ctx = b . lib . SSL_CTX_new ( b . lib . TLSv1_method ( ) ) <EOL> ctx = b . ffi . gc ( ctx , b . lib . SSL_CTX_free ) <EOL> ssl = b . lib . SSL_new ( ctx ) <EOL> ssl = b . ffi . gc ( ssl , b . lib . SSL_free ) <EOL> current_options = b . lib . SSL_get_options ( ssl ) <EOL> resp = b . lib . SSL_set_options ( ssl , b . lib . SSL_OP_ALL ) <EOL> expected_options = current_options | b . lib . SSL_OP_ALL <EOL> assert resp == expected_options <EOL> assert b . lib . SSL_get_options ( ssl ) == expected_options <EOL> def test_ssl_mode ( self ) : <EOL> b = Binding ( ) <EOL> assert b . lib . SSL_OP_ALL > <NUM_LIT:0> <EOL> ctx = b . lib . SSL_CTX_new ( b . lib . TLSv1_method ( ) ) <EOL> ctx = b . ffi . gc ( ctx , b . lib . SSL_CTX_free ) <EOL> ssl = b . lib . SSL_new ( ctx ) <EOL> ssl = b . ffi . gc ( ssl , b . lib . SSL_free ) <EOL> current_options = b . lib . SSL_get_mode ( ssl ) <EOL> resp = b . lib . SSL_set_mode ( ssl , b . lib . SSL_OP_ALL ) <EOL> expected_options = current_options | b . lib . SSL_OP_ALL <EOL> assert resp == expected_options <EOL> assert b . lib . SSL_get_mode ( ssl ) == expected_options <EOL> def test_conditional_removal ( self ) : <EOL> b = Binding ( ) <EOL> if b . lib . OPENSSL_VERSION_NUMBER >= <NUM_LIT> : <EOL> assert b . lib . X509_V_ERR_DIFFERENT_CRL_SCOPE <EOL> assert b . lib . X509_V_ERR_CRL_PATH_VALIDATION_ERROR <EOL> else : <EOL> with pytest . raises ( AttributeError ) : <EOL> b . lib . X509_V_ERR_DIFFERENT_CRL_SCOPE <EOL> with pytest . raises ( AttributeError ) : <EOL> b . lib . X509_V_ERR_CRL_PATH_VALIDATION_ERROR <EOL> if b . lib . OPENSSL_VERSION_NUMBER >= <NUM_LIT> : <EOL> assert b . lib . CMAC_Init <EOL> else : <EOL> with pytest . raises ( AttributeError ) : <EOL> b . lib . CMAC_Init <EOL> def test_openssl_assert_error_on_stack ( self ) : <EOL> b = Binding ( ) <EOL> b . lib . ERR_put_error ( <EOL> b . lib . ERR_LIB_EVP , <EOL> b . lib . EVP_F_EVP_ENCRYPTFINAL_EX , <EOL> b . lib . EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH , <EOL> b"<STR_LIT>" , <EOL> - <NUM_LIT:1> <EOL> ) <EOL> with pytest . raises ( InternalError ) as exc_info : <EOL> _openssl_assert ( b . lib , False ) <EOL> assert exc_info . value . err_code == [ _OpenSSLErrorWithText ( <EOL> code = <NUM_LIT> , <EOL> lib = b . lib . ERR_LIB_EVP , <EOL> func = b . lib . EVP_F_EVP_ENCRYPTFINAL_EX , <EOL> reason = b . lib . EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH , <EOL> reason_text = ( <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' <EOL> ) <EOL> ) ] <EOL> def test_verify_openssl_version ( self , monkeypatch ) : <EOL> monkeypatch . delenv ( "<STR_LIT>" , raising = False ) <EOL> with pytest . raises ( RuntimeError ) : <EOL> _verify_openssl_version ( <NUM_LIT> ) </s>
