<s> import mock <EOL> from twisted . internet import defer <EOL> from twisted . trial import unittest <EOL> from OpenSSL import crypto <EOL> import oppy . connection . connectionbuildtask as connectionbuildtask <EOL> from oppy . connection . definitions import ( <EOL> LINK_CERT_TYPE , <EOL> ID_CERT_TYPE , <EOL> OPENSSL_RSA_KEY_TYPE , <EOL> ) <EOL> from oppy . cell . fixedlen import NetInfoCell <EOL> from oppy . cell . varlen import AuthChallengeCell , CertsCell , VersionsCell <EOL> from oppy . cell . util import CertsCellPayloadItem <EOL> from cert_der import test_cert_der <EOL> class ConnectionBuildTaskTest ( unittest . TestCase ) : <EOL> @ mock . patch ( '<STR_LIT>' , autospec = True ) <EOL> def setUp ( self , cm ) : <EOL> self . cm = cm <EOL> self . cbt = connectionbuildtask . ConnectionBuildTask ( cm , mock . Mock ( ) ) <EOL> @ mock . patch ( '<STR_LIT>' , autospec = True ) <EOL> def test_connectionMade ( self , mock_deferred ) : <EOL> mock_sendVersionsCell = mock . Mock ( ) <EOL> mock_sendVersionsCell . return_value = mock_deferred <EOL> self . cbt . _sendVersionsCell = mock_sendVersionsCell <EOL> self . cbt . connectionMade ( ) <EOL> self . assertEqual ( mock_sendVersionsCell . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . cbt . _tasks . addCallback . call_count , <NUM_LIT:6> ) <EOL> self . assertTrue ( mock . call ( self . cbt . _processVersionsCell ) in <EOL> self . cbt . _tasks . addCallback . call_args_list ) <EOL> self . assertTrue ( mock . call ( self . cbt . _processCertsCell ) in <EOL> self . cbt . _tasks . addCallback . call_args_list ) <EOL> self . assertTrue ( mock . call ( self . cbt . _processAuthChallengeCell ) in <EOL> self . cbt . _tasks . addCallback . call_args_list ) <EOL> self . assertTrue ( mock . call ( self . cbt . _processNetInfoCell ) in <EOL> self . cbt . _tasks . addCallback . call_args_list ) <EOL> self . assertTrue ( mock . call ( self . cbt . _sendNetInfoCell ) in <EOL> self . cbt . _tasks . addCallback . call_args_list ) <EOL> self . assertTrue ( mock . call ( self . cbt . _connectionSucceeded ) in <EOL> self . cbt . _tasks . addCallback . call_args_list ) <EOL> self . cbt . _tasks . addErrback . assert_called_once_with ( <EOL> self . cbt . _connectionFailed ) <EOL> def test_connectionMade_send_versions_fail ( self ) : <EOL> self . cbt . _sendVersionsCell = mock . Mock ( ) <EOL> self . cbt . _sendVersionsCell . side_effect = Exception <EOL> self . cbt . _connectionFailed = mock . Mock ( ) <EOL> self . cbt . _connectionSucceeded = mock . Mock ( ) <EOL> self . cbt . connectionMade ( ) <EOL> self . assertEqual ( self . cbt . _connectionFailed . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . cbt . _connectionSucceeded . call_count , <NUM_LIT:0> ) <EOL> def test_connectionMade_callback_fail ( self ) : <EOL> d = defer . Deferred ( ) <EOL> self . cbt . _sendVersionsCell = mock . Mock ( ) <EOL> self . cbt . _sendVersionsCell . return_value = d <EOL> self . cbt . _processVersionsCell = mock . Mock ( ) <EOL> self . cbt . _processVersionsCell . return_value = '<STR_LIT:test>' <EOL> self . cbt . _processCertsCell = mock . Mock ( ) <EOL> self . cbt . _processCertsCell . return_value = '<STR_LIT:test>' <EOL> self . cbt . _processAuthChallengeCell = mock . Mock ( ) <EOL> self . cbt . _processAuthChallengeCell . side_effect = Exception <EOL> self . cbt . _processNetInfoCell = mock . Mock ( ) <EOL> self . cbt . _sendNetInfoCell = mock . Mock ( ) <EOL> self . cbt . _connectionSucceeded = mock . Mock ( ) <EOL> self . cbt . _connectionFailed = mock . Mock ( ) <EOL> self . cbt . connectionMade ( ) <EOL> d . callback ( '<STR_LIT:test>' ) <EOL> self . assertEqual ( self . cbt . _connectionFailed . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . cbt . _connectionSucceeded . call_count , <NUM_LIT:0> ) <EOL> def test_connectionLost_not_failed_with_current_task ( self ) : <EOL> self . cbt . _current_task = mock . Mock ( ) <EOL> self . cbt . _current_task . errback = mock . Mock ( ) <EOL> self . cbt . _connectionFailed = mock . Mock ( ) <EOL> self . cbt . connectionLost ( mock . Mock ( ) ) <EOL> self . assertTrue ( self . cbt . _failed ) <EOL> self . assertEqual ( self . cbt . _connectionFailed . call_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . cbt . _current_task . errback . call_count , <NUM_LIT:1> ) <EOL> def test_connectionLost_not_failed_no_current_task ( self ) : <EOL> self . cbt . _current_task = None <EOL> self . cbt . _connectionFailed = mock . Mock ( ) <EOL> self . cbt . connectionLost ( mock . Mock ( ) ) <EOL> self . assertTrue ( self . cbt . _failed ) <EOL> self . assertEqual ( self . cbt . _connectionFailed . call_count , <NUM_LIT:1> ) <EOL> def test_connectionLost_failed ( self ) : <EOL> self . cbt . _failed = True <EOL> self . cbt . _current_task = mock . Mock ( ) <EOL> self . cbt . _current_task . errback = mock . Mock ( ) <EOL> self . cbt . _connectionFailed = mock . Mock ( ) <EOL> self . cbt . connectionLost ( mock . Mock ( ) ) <EOL> self . assertTrue ( self . cbt . _failed ) <EOL> self . assertEqual ( self . cbt . _connectionFailed . call_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . cbt . _current_task . errback . call_count , <NUM_LIT:0> ) <EOL> def test_recvCell ( self ) : <EOL> self . cbt . _read_queue = mock . Mock ( ) <EOL> self . cbt . _read_queue . get = mock . Mock ( ) <EOL> ret = mock . Mock ( ) <EOL> self . cbt . _read_queue . get . return_value = ret <EOL> r = self . cbt . _recvCell ( ) <EOL> self . assertEqual ( r , ret ) <EOL> self . assertEqual ( ret , self . cbt . _current_task ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> autospec = True ) <EOL> def test_sendVersionsCell ( self , mock_versions ) : <EOL> mock_cell = mock . Mock ( ) <EOL> mock_bytes = mock . Mock ( ) <EOL> mock_cell . getBytes . return_value = mock_bytes <EOL> mock_versions . make . return_value = mock_cell <EOL> self . cbt . transport = mock . Mock ( ) <EOL> self . cbt . transport . write = mock . Mock ( ) <EOL> self . cbt . _recvCell = mock . Mock ( ) <EOL> ret = mock . Mock ( ) <EOL> self . cbt . _recvCell . return_value = ret <EOL> r = self . cbt . _sendVersionsCell ( ) <EOL> self . cbt . transport . write . assert_called_once_with ( mock_bytes ) <EOL> self . assertEqual ( r , ret ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_processVersionsCell ( self , csh ) : <EOL> csh . return_value = True <EOL> cell = VersionsCell . make ( [ <NUM_LIT:3> ] ) <EOL> self . cbt . transport = mock . Mock ( ) <EOL> self . cbt . transport . getPeerCertificate = mock . Mock ( ) <EOL> self . cbt . transport . getPeerCertificate . return_value = '<STR_LIT:test>' <EOL> self . cbt . _recvCell = mock . Mock ( ) <EOL> self . cbt . _recvCell . return_value = '<STR_LIT:t>' <EOL> self . assertEqual ( self . cbt . _processVersionsCell ( cell ) , '<STR_LIT:t>' ) <EOL> self . assertEqual ( self . cbt . _connection_cert , '<STR_LIT:test>' ) <EOL> self . assertEqual ( self . cbt . _link_protocol , <NUM_LIT:3> ) <EOL> self . assertEqual ( self . cbt . _recvCell . call_count , <NUM_LIT:1> ) <EOL> def test_processVersionsCell_wrong_cell_type ( self ) : <EOL> cell = NetInfoCell . make ( <NUM_LIT:0> , '<STR_LIT:127.0.0.1>' , [ '<STR_LIT:127.0.0.1>' ] ) <EOL> self . assertRaises ( TypeError , <EOL> self . cbt . _processVersionsCell , <EOL> cell ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_processVersionsCell_unsupported_handshake ( self , csh ) : <EOL> self . cbt . transport = mock . Mock ( ) <EOL> self . cbt . transport . getPeerCertificate = mock . Mock ( ) <EOL> self . cbt . transport . getPeerCertificate . return_value = '<STR_LIT:test>' <EOL> csh . return_value = False <EOL> cell = VersionsCell . make ( [ <NUM_LIT:3> ] ) <EOL> self . assertRaises ( ValueError , <EOL> self . cbt . _processVersionsCell , <EOL> cell ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_processVersionsCell_no_versions_in_common ( self , csh ) : <EOL> self . cbt . transport = mock . Mock ( ) <EOL> self . cbt . transport . getPeerCertificate = mock . Mock ( ) <EOL> self . cbt . transport . getPeerCertificate . return_value = '<STR_LIT:test>' <EOL> csh . return_value = True <EOL> cell = VersionsCell ( None , [ <NUM_LIT:2> ] ) <EOL> self . assertRaises ( ValueError , <EOL> self . cbt . _processVersionsCell , <EOL> cell ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = ( mock . Mock ( ) , mock . Mock ( ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = True ) <EOL> def test_processCertsCell ( self , gcfc , chvt , ake , irbk , crypto ) : <EOL> self . cbt . _connection_cert = mock . Mock ( ) <EOL> self . cbt . _recvCell = mock . Mock ( ) <EOL> self . cbt . _recvCell . return_value = '<STR_LIT:test>' <EOL> cell = CertsCell ( None ) <EOL> self . assertEqual ( self . cbt . _processCertsCell ( cell ) , '<STR_LIT:test>' ) <EOL> self . assertEqual ( self . cbt . _recvCell . call_count , <NUM_LIT:1> ) <EOL> def test_processCertsCell_wrong_cell_type ( self ) : <EOL> cell = NetInfoCell . make ( <NUM_LIT:0> , '<STR_LIT:127.0.0.1>' , [ '<STR_LIT:127.0.0.1>' ] ) <EOL> self . assertRaises ( TypeError , <EOL> self . cbt . _processCertsCell , <EOL> cell ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = ( mock . Mock ( ) , mock . Mock ( ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = False ) <EOL> def test_processCertsCell_invalid_cert_time ( self , gcfc , chvt ) : <EOL> cell = CertsCell ( None ) <EOL> self . assertRaises ( ValueError , <EOL> self . cbt . _processCertsCell , <EOL> cell ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = ( mock . Mock ( ) , mock . Mock ( ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = False ) <EOL> def test_processCertsCell_keys_neq ( self , gcfc , chvt , ake ) : <EOL> self . cbt . _connection_cert = mock . Mock ( ) <EOL> cell = CertsCell ( None ) <EOL> self . assertRaises ( ValueError , <EOL> self . cbt . _processCertsCell , <EOL> cell ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = ( mock . Mock ( ) , mock . Mock ( ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = False ) <EOL> def test_processCertsCell_not_RSA_1024 ( self , gcfc , chvt , ake , irbk ) : <EOL> self . cbt . _connection_cert = mock . Mock ( ) <EOL> cell = CertsCell ( None ) <EOL> self . assertRaises ( ValueError , <EOL> self . cbt . _processCertsCell , <EOL> cell ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = ( mock . Mock ( ) , mock . Mock ( ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> def test_processCertsCell_cert_not_signed ( self , gcfc , chvt , ake , irbk , c ) : <EOL> self . cbt . _connection_cert = mock . Mock ( ) <EOL> cell = CertsCell ( None ) <EOL> self . assertRaises ( ValueError , <EOL> self . cbt . _processCertsCell , <EOL> cell ) <EOL> def test_processAuthChallengeCell ( self ) : <EOL> cell = AuthChallengeCell ( None ) <EOL> self . cbt . _recvCell = mock . Mock ( ) <EOL> self . cbt . _recvCell . return_value = '<STR_LIT:test>' <EOL> self . assertEqual ( self . cbt . _processAuthChallengeCell ( cell ) , '<STR_LIT:test>' ) <EOL> self . assertEqual ( self . cbt . _recvCell . call_count , <NUM_LIT:1> ) <EOL> def test_processAuthChallengeCell_wrong_cell_type ( self ) : <EOL> cell = CertsCell ( None ) <EOL> self . assertRaises ( TypeError , <EOL> self . cbt . _processAuthChallengeCell , <EOL> cell ) <EOL> def test_processNetInfoCell ( self ) : <EOL> cell = NetInfoCell . make ( <NUM_LIT:0> , '<STR_LIT:127.0.0.1>' , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( self . cbt . _processNetInfoCell ( cell ) , <EOL> ( '<STR_LIT:127.0.0.1>' , '<STR_LIT>' ) ) <EOL> def test_processNetInfoCell_wrong_type ( self ) : <EOL> self . assertRaises ( TypeError , <EOL> self . cbt . _processNetInfoCell , <EOL> CertsCell ( None ) ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = '<STR_LIT:test>' ) <EOL> def test_sendNetInfoCell ( self , cell ) : <EOL> self . cbt . transport = mock . Mock ( ) <EOL> self . cbt . transport . write = mock . Mock ( ) <EOL> self . cbt . _sendNetInfoCell ( ( '<STR_LIT:127.0.0.1>' , '<STR_LIT>' ) ) <EOL> self . cbt . transport . write . assert_called_once_with ( '<STR_LIT:test>' ) <EOL> def test_connectionSucceeded ( self ) : <EOL> self . cbt . _connectionSucceeded ( None ) <EOL> self . cm . connectionTaskSucceeded . assert_called_once_with ( self . cbt ) <EOL> def test_connectionFailed_not_failed_yet ( self ) : <EOL> self . cbt . _failed = False <EOL> self . cbt . transport = mock . Mock ( ) <EOL> self . cbt . transport . abortConnection = mock . Mock ( ) <EOL> self . cbt . _connectionFailed ( None ) <EOL> self . assertTrue ( self . cbt . _failed ) <EOL> self . assertEqual ( self . cbt . transport . abortConnection . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . cm . connectionTaskFailed . call_count , <NUM_LIT:1> ) <EOL> def test_connectionFailed_already_failed ( self ) : <EOL> self . cbt . _failed = True <EOL> self . cbt . transport = mock . Mock ( ) <EOL> self . cbt . transport . abortConnection = mock . Mock ( ) <EOL> self . cbt . _connectionFailed ( None ) <EOL> self . assertTrue ( self . cbt . _failed ) <EOL> self . assertEqual ( self . cbt . transport . abortConnection . call_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . cm . connectionTaskFailed . call_count , <NUM_LIT:1> ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = True ) <EOL> def test_connectionSupportsHandshake_self_signed ( self , _ ) : <EOL> c = mock . Mock ( ) <EOL> mock_issuer = mock . Mock ( ) <EOL> mock_issuer . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_issuer . commonName = '<STR_LIT>' <EOL> c . get_issuer . return_value = mock_issuer <EOL> mock_subject = mock . Mock ( ) <EOL> mock_subject . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_subject . commonName = '<STR_LIT>' <EOL> c . get_subject . return_value = mock_subject <EOL> b = mock . Mock ( ) <EOL> c . get_pubkey . bits = mock . Mock ( return_value = b ) <EOL> b . bits . return_value = <NUM_LIT> <EOL> self . assertTrue ( connectionbuildtask . _connectionSupportsHandshake ( c ) ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> def test_connectionSupportsHandshake_issuer_CN ( self , _ ) : <EOL> c = mock . Mock ( ) <EOL> mock_issuer = mock . Mock ( ) <EOL> mock_issuer . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_issuer . commonName = '<STR_LIT>' <EOL> c . get_issuer . return_value = mock_issuer <EOL> mock_subject = mock . Mock ( ) <EOL> mock_subject . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_subject . commonName = '<STR_LIT>' <EOL> c . get_subject . return_value = mock_subject <EOL> b = mock . Mock ( ) <EOL> c . get_pubkey . bits = mock . Mock ( return_value = b ) <EOL> b . bits . return_value = <NUM_LIT> <EOL> self . assertTrue ( connectionbuildtask . _connectionSupportsHandshake ( c ) ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> def test_connectionSupportsHandshake_subject_CN ( self , c ) : <EOL> c = mock . Mock ( ) <EOL> mock_issuer = mock . Mock ( ) <EOL> mock_issuer . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_issuer . commonName = '<STR_LIT>' <EOL> c . get_issuer . return_value = mock_issuer <EOL> mock_subject = mock . Mock ( ) <EOL> mock_subject . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_subject . commonName = '<STR_LIT>' <EOL> c . get_subject . return_value = mock_subject <EOL> b = mock . Mock ( ) <EOL> c . get_pubkey . bits = mock . Mock ( return_value = b ) <EOL> b . bits . return_value = <NUM_LIT> <EOL> self . assertTrue ( connectionbuildtask . _connectionSupportsHandshake ( c ) ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> def test_connectionSupportsHandshake_issuer_net ( self , c ) : <EOL> c = mock . Mock ( ) <EOL> mock_issuer = mock . Mock ( ) <EOL> mock_issuer . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_issuer . commonName = '<STR_LIT>' <EOL> c . get_issuer . return_value = mock_issuer <EOL> mock_subject = mock . Mock ( ) <EOL> mock_subject . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_subject . commonName = '<STR_LIT>' <EOL> c . get_subject . return_value = mock_subject <EOL> b = mock . Mock ( ) <EOL> c . get_pubkey . bits = mock . Mock ( return_value = b ) <EOL> b . bits . return_value = <NUM_LIT> <EOL> self . assertTrue ( connectionbuildtask . _connectionSupportsHandshake ( c ) ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> def test_connectionSupportsHandshake_subject_net ( self , c ) : <EOL> c = mock . Mock ( ) <EOL> mock_issuer = mock . Mock ( ) <EOL> mock_issuer . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_issuer . commonName = '<STR_LIT>' <EOL> c . get_issuer . return_value = mock_issuer <EOL> mock_subject = mock . Mock ( ) <EOL> mock_subject . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_subject . commonName = '<STR_LIT>' <EOL> c . get_subject . return_value = mock_subject <EOL> b = mock . Mock ( ) <EOL> c . get_pubkey . bits = mock . Mock ( return_value = b ) <EOL> b . bits . return_value = <NUM_LIT> <EOL> self . assertTrue ( connectionbuildtask . _connectionSupportsHandshake ( c ) ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> def test_connectionSupportsHandshake_longer_1024 ( self , c ) : <EOL> c = mock . Mock ( ) <EOL> mock_issuer = mock . Mock ( ) <EOL> mock_issuer . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_issuer . commonName = '<STR_LIT>' <EOL> c . get_issuer . return_value = mock_issuer <EOL> mock_subject = mock . Mock ( ) <EOL> mock_subject . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_subject . commonName = '<STR_LIT>' <EOL> c . get_subject . return_value = mock_subject <EOL> b = mock . Mock ( ) <EOL> c . get_pubkey . bits = mock . Mock ( return_value = b ) <EOL> b . bits . return_value = <NUM_LIT> <EOL> self . assertTrue ( connectionbuildtask . _connectionSupportsHandshake ( c ) ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> def test_connectionSupportsHandshake_all_fail ( self , c ) : <EOL> c = mock . Mock ( ) <EOL> mock_issuer = mock . Mock ( ) <EOL> mock_issuer . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_issuer . commonName = '<STR_LIT>' <EOL> c . get_issuer . return_value = mock_issuer <EOL> mock_subject = mock . Mock ( ) <EOL> mock_subject . get_components . return_value = [ ( '<STR_LIT>' , None ) ] <EOL> mock_subject . commonName = '<STR_LIT>' <EOL> c . get_subject . return_value = mock_subject <EOL> b = mock . Mock ( ) <EOL> c . get_pubkey . bits = mock . Mock ( return_value = b ) <EOL> b . bits . return_value = <NUM_LIT> <EOL> self . assertTrue ( connectionbuildtask . _connectionSupportsHandshake ( c ) ) <EOL> def test_getCertsFromCell ( self ) : <EOL> lc = test_cert_der <EOL> link_cert = CertsCellPayloadItem ( LINK_CERT_TYPE , len ( lc ) , lc ) <EOL> ic = test_cert_der <EOL> id_cert = CertsCellPayloadItem ( ID_CERT_TYPE , len ( ic ) , ic ) <EOL> cell = CertsCell . make ( <NUM_LIT:0> , [ link_cert , id_cert ] ) <EOL> res1 = crypto . load_certificate ( crypto . FILETYPE_ASN1 , lc ) <EOL> res2 = crypto . load_certificate ( crypto . FILETYPE_ASN1 , ic ) <EOL> l , i = connectionbuildtask . _getCertsFromCell ( cell ) <EOL> self . assertEqual ( crypto . dump_certificate ( crypto . FILETYPE_ASN1 , l ) , lc ) <EOL> self . assertEqual ( crypto . dump_certificate ( crypto . FILETYPE_ASN1 , i ) , ic ) <EOL> def test_getCertsFromCell_invalid_count ( self ) : <EOL> lc = test_cert_der <EOL> link_cert = CertsCellPayloadItem ( LINK_CERT_TYPE , len ( lc ) , lc ) <EOL> cell = CertsCell . make ( <NUM_LIT:0> , [ link_cert ] ) <EOL> self . assertRaises ( ValueError , <EOL> connectionbuildtask . _getCertsFromCell , <EOL> cell ) <EOL> def test_getCertsFromCell_malformed_cert ( self ) : <EOL> lc = test_cert_der <EOL> link_cert = CertsCellPayloadItem ( LINK_CERT_TYPE , len ( lc ) , lc ) <EOL> ic = test_cert_der [ : len ( test_cert_der ) - <NUM_LIT:1> ] <EOL> id_cert = CertsCellPayloadItem ( ID_CERT_TYPE , len ( ic ) , ic ) <EOL> cell = CertsCell . make ( <NUM_LIT:0> , [ link_cert , id_cert ] ) <EOL> self . assertRaises ( ValueError , <EOL> connectionbuildtask . _getCertsFromCell , <EOL> cell ) <EOL> def test_getCertsFromCell_invalid_cert_type ( self ) : <EOL> lc = test_cert_der <EOL> link_cert = CertsCellPayloadItem ( LINK_CERT_TYPE , len ( lc ) , lc ) <EOL> ic = test_cert_der <EOL> id_cert = CertsCellPayloadItem ( LINK_CERT_TYPE , len ( ic ) , ic ) <EOL> cell = CertsCell . make ( <NUM_LIT:0> , [ link_cert , id_cert ] ) <EOL> self . assertRaises ( ValueError , <EOL> connectionbuildtask . _getCertsFromCell , <EOL> cell ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = True ) <EOL> def test_certsHaveValidTime_fail ( self , vct ) : <EOL> mock_cert = mock . Mock ( ) <EOL> certs = [ mock_cert ] <EOL> self . assertTrue ( connectionbuildtask . _certsHaveValidTime ( certs ) ) <EOL> vct . assert_called_once_with ( mock_cert ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> def test_certsHaveValidTime_fail ( self , vct ) : <EOL> mock_cert = mock . Mock ( ) <EOL> certs = [ mock_cert ] <EOL> self . assertFalse ( connectionbuildtask . _certsHaveValidTime ( certs ) ) <EOL> vct . assert_called_once_with ( mock_cert ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , autospec = True ) <EOL> def test_ASN1KeysEqual ( self , dpk , cse ) : <EOL> mock_asn1_key = mock . Mock ( ) <EOL> self . assertTrue ( connectionbuildtask . _ASN1KeysEqual ( mock_asn1_key , <EOL> mock_asn1_key ) ) <EOL> self . assertEqual ( cse . call_count , <NUM_LIT:1> ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> @ mock . patch ( '<STR_LIT>' , autospec = True ) <EOL> def test_ASN1KeysEqual_neq ( self , dpk , cse ) : <EOL> mock_asn1_key = mock . Mock ( ) <EOL> self . assertFalse ( connectionbuildtask . _ASN1KeysEqual ( mock_asn1_key , <EOL> mock_asn1_key ) ) <EOL> self . assertEqual ( cse . call_count , <NUM_LIT:1> ) <EOL> def test_isRSA1024BitKey ( self ) : <EOL> key = mock . Mock ( ) <EOL> key . type . return_value = OPENSSL_RSA_KEY_TYPE <EOL> key . bits . return_value = <NUM_LIT> <EOL> self . assertTrue ( connectionbuildtask . _isRSA1024BitKey ( key ) ) <EOL> def test_isRSA1024BitKey_not_RSA ( self ) : <EOL> key = mock . Mock ( ) <EOL> key . type . return_value = OPENSSL_RSA_KEY_TYPE - <NUM_LIT:1> <EOL> key . bits . return_value = <NUM_LIT> <EOL> self . assertFalse ( connectionbuildtask . _isRSA1024BitKey ( key ) ) <EOL> def test_isRSA1024BitKey_not_1024 ( self ) : <EOL> key = mock . Mock ( ) <EOL> key . type . return_value = OPENSSL_RSA_KEY_TYPE <EOL> key . bits . return_value = <NUM_LIT> <EOL> self . assertFalse ( connectionbuildtask . _isRSA1024BitKey ( key ) ) </s>
