<s> import os <EOL> import sys <EOL> import os . path <EOL> import socket <EOL> import mock <EOL> from mock import patch <EOL> import libcloud . security <EOL> from libcloud . utils . py3 import reload <EOL> from libcloud . httplib_ssl import LibcloudHTTPSConnection <EOL> from libcloud . test import unittest <EOL> ORIGINAL_CA_CERS_PATH = libcloud . security . CA_CERTS_PATH <EOL> class TestHttpLibSSLTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> libcloud . security . VERIFY_SSL_CERT = False <EOL> libcloud . security . CA_CERTS_PATH = ORIGINAL_CA_CERS_PATH <EOL> self . httplib_object = LibcloudHTTPSConnection ( '<STR_LIT>' ) <EOL> def test_custom_ca_path_using_env_var_doesnt_exist ( self ) : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> reload ( libcloud . security ) <EOL> except ValueError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> msg = '<STR_LIT>' <EOL> self . assertEqual ( str ( e ) , msg ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_custom_ca_path_using_env_var_is_directory ( self ) : <EOL> file_path = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> os . environ [ '<STR_LIT>' ] = file_path <EOL> expected_msg = '<STR_LIT>' <EOL> self . assertRaisesRegexp ( ValueError , expected_msg , <EOL> reload , libcloud . security ) <EOL> def test_custom_ca_path_using_env_var_exist ( self ) : <EOL> file_path = os . path . abspath ( __file__ ) <EOL> os . environ [ '<STR_LIT>' ] = file_path <EOL> reload ( libcloud . security ) <EOL> self . assertEqual ( libcloud . security . CA_CERTS_PATH , [ file_path ] ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_setup_verify ( self , _ ) : <EOL> libcloud . security . CA_CERTS_PATH = [ ] <EOL> libcloud . security . VERIFY_SSL_CERT = True <EOL> expected_msg = libcloud . security . CA_CERTS_UNAVAILABLE_ERROR_MSG <EOL> self . assertRaisesRegexp ( RuntimeError , expected_msg , <EOL> self . httplib_object . _setup_verify ) <EOL> libcloud . security . VERIFY_SSL_CERT = False <EOL> self . httplib_object . _setup_verify ( ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_setup_ca_cert ( self , _ ) : <EOL> self . httplib_object . verify = False <EOL> self . httplib_object . _setup_ca_cert ( ) <EOL> self . assertEqual ( self . httplib_object . ca_cert , None ) <EOL> self . httplib_object . verify = True <EOL> libcloud . security . CA_CERTS_PATH = [ os . path . abspath ( __file__ ) ] <EOL> self . httplib_object . _setup_ca_cert ( ) <EOL> self . assertTrue ( self . httplib_object . ca_cert is not None ) <EOL> libcloud . security . CA_CERTS_PATH = [ ] <EOL> expected_msg = libcloud . security . CA_CERTS_UNAVAILABLE_ERROR_MSG <EOL> self . assertRaisesRegexp ( RuntimeError , expected_msg , <EOL> self . httplib_object . _setup_ca_cert ) <EOL> @ mock . patch ( '<STR_LIT>' , mock . MagicMock ( ) ) <EOL> @ mock . patch ( '<STR_LIT>' , mock . MagicMock ( ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_connect_throws_friendly_error_message_on_ssl_wrap_connection_reset_by_peer ( self , mock_wrap_socket ) : <EOL> libcloud . security . VERIFY_SSL_CERT = True <EOL> self . httplib_object . verify = True <EOL> self . httplib_object . http_proxy_used = False <EOL> mock_wrap_socket . side_effect = Exception ( '<STR_LIT>' ) <EOL> expected_msg = '<STR_LIT>' <EOL> self . assertRaisesRegexp ( Exception , expected_msg , <EOL> self . httplib_object . connect ) <EOL> mock_wrap_socket . side_effect = socket . error ( '<STR_LIT>' ) <EOL> expected_msg = '<STR_LIT>' <EOL> self . assertRaisesRegexp ( socket . error , expected_msg , <EOL> self . httplib_object . connect ) <EOL> with self . assertRaises ( socket . error ) as cm : <EOL> mock_wrap_socket . side_effect = socket . error ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . httplib_object . connect ( ) <EOL> e = cm . exception <EOL> self . assertEqual ( e . errno , <NUM_LIT> ) <EOL> self . assertTrue ( expected_msg in str ( e ) ) <EOL> with self . assertRaises ( socket . error ) as cm : <EOL> mock_wrap_socket . side_effect = socket . error ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . httplib_object . connect ( ) <EOL> e = cm . exception <EOL> self . assertEqual ( e . errno , <NUM_LIT> ) <EOL> self . assertTrue ( '<STR_LIT>' in str ( e ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( unittest . main ( ) ) </s>
