<s> import hashlib <EOL> import mock <EOL> from twisted . trial import unittest <EOL> from oppy . crypto import ntor <EOL> class NTorTest ( unittest . TestCase ) : <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_NTorState ( self , mock_sk , mock_b64 , mock_dmi ) : <EOL> mock_sk_g = mock . Mock ( ) <EOL> mock_sk_g . public_key = '<STR_LIT>' <EOL> mock_sk . return_value = mock_sk_g <EOL> mock_sk . public_key = '<STR_LIT>' <EOL> mock_md = mock . Mock ( ) <EOL> mock_md . ntor_onion_key = '<STR_LIT:test>' <EOL> n = ntor . NTorState ( mock_md ) <EOL> mock_dmi . assert_called_once_with ( mock_md ) <EOL> mock_b64 . assert_called_once_with ( '<STR_LIT:test>' ) <EOL> self . assertEqual ( mock_sk . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( n . relay_identity , '<STR_LIT>' ) <EOL> self . assertEqual ( n . relay_ntor_onion_key , '<STR_LIT>' ) <EOL> self . assertEqual ( n . secret_key , mock_sk_g ) <EOL> self . assertEqual ( n . public_key , '<STR_LIT>' ) <EOL> def test_createOnionSkin ( self ) : <EOL> mock_nts = mock . Mock ( ) <EOL> mock_nts . relay_identity = '<STR_LIT>' <EOL> mock_nts . relay_ntor_onion_key = '<STR_LIT>' <EOL> mock_nts . public_key = '<STR_LIT>' <EOL> self . assertEqual ( ntor . createOnionSkin ( mock_nts ) , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = ( '<STR_LIT>' , True ) ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> def test_deriveRelayCrypto_buildSecreInput_bad ( self , mock_mrc , mock_cse , <EOL> mock_bai , mock_mhs , mock_bsi ) : <EOL> mock_nts = mock . Mock ( ) <EOL> mock_nts . relay_identity = '<STR_LIT>' <EOL> mock_nts . relay_ntor_onion_key = '<STR_LIT>' <EOL> mock_nts . public_key = '<STR_LIT>' <EOL> mock_cell = mock . Mock ( ) <EOL> mock_cell . hdata = '<STR_LIT:\x00>' * <NUM_LIT> <EOL> self . assertRaises ( ntor . KeyDerivationFailed , <EOL> ntor . deriveRelayCrypto , <EOL> mock_nts , <EOL> mock_cell ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = ( '<STR_LIT>' , False ) ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> def test_deriveRelayCrypto_auth_bad ( self , mock_mrc , mock_cse , mock_bai , <EOL> mock_mhs , mock_bsi ) : <EOL> mock_nts = mock . Mock ( ) <EOL> mock_nts . relay_identity = '<STR_LIT>' <EOL> mock_nts . relay_ntor_onion_key = '<STR_LIT>' <EOL> mock_nts . public_key = '<STR_LIT>' <EOL> mock_cell = mock . Mock ( ) <EOL> mock_cell . hdata = '<STR_LIT:\x00>' * <NUM_LIT> <EOL> self . assertRaises ( ntor . KeyDerivationFailed , <EOL> ntor . deriveRelayCrypto , <EOL> mock_nts , <EOL> mock_cell ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = ( '<STR_LIT>' , False ) ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> def test_deriveRelayCrypto_ok ( self , mock_mrc , mock_cse , mock_bai , mock_mhs , <EOL> mock_bsi ) : <EOL> mock_nts = mock . Mock ( ) <EOL> mock_nts . relay_identity = '<STR_LIT>' <EOL> mock_nts . relay_ntor_onion_key = '<STR_LIT>' <EOL> mock_nts . public_key = '<STR_LIT>' <EOL> mock_cell = mock . Mock ( ) <EOL> mock_cell . hdata = [ chr ( i ) for i in range ( <NUM_LIT> ) ] <EOL> hdata = mock_cell . hdata <EOL> ret = ntor . deriveRelayCrypto ( mock_nts , mock_cell ) <EOL> mock_bsi . assert_called_once_with ( mock_nts , hdata [ : <NUM_LIT:32> ] ) <EOL> self . assertEqual ( mock_mhs . call_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( mock_mhs . call_args_list , <EOL> [ mock . call ( msg = '<STR_LIT>' , key = '<STR_LIT>' ) , <EOL> mock . call ( msg = '<STR_LIT>' , key = '<STR_LIT>' ) ] ) <EOL> mock_cse . assert_called_once_with ( hdata [ <NUM_LIT:32> : <NUM_LIT:32> + <NUM_LIT:20> ] , '<STR_LIT>' ) <EOL> mock_mrc . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( ret , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = ( '<STR_LIT:\x00>' , True ) ) <EOL> def test_buildSecretInput_bad_EXP ( self , mock_exp ) : <EOL> mock_nts = mock . Mock ( ) <EOL> mock_nts . relay_identity = '<STR_LIT>' <EOL> mock_nts . relay_ntor_onion_key = '<STR_LIT>' <EOL> mock_nts . public_key = '<STR_LIT>' <EOL> relay_pk = '<STR_LIT>' <EOL> v , b = ntor . _buildSecretInput ( mock_nts , relay_pk ) <EOL> self . assertTrue ( b ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = ( '<STR_LIT>' , False ) ) <EOL> def test_buildAuthInput ( self , mock_exp ) : <EOL> mock_nts = mock . Mock ( ) <EOL> mock_nts . relay_identity = '<STR_LIT>' <EOL> mock_nts . relay_ntor_onion_key = '<STR_LIT>' <EOL> mock_nts . public_key = '<STR_LIT>' <EOL> relay_pk = '<STR_LIT>' <EOL> v , b = ntor . _buildSecretInput ( mock_nts , relay_pk ) <EOL> self . assertEqual ( mock_exp . call_count , <NUM_LIT:2> ) <EOL> self . assertFalse ( b ) <EOL> self . assertEqual ( v , <EOL> '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = [ chr ( i ) for i in range ( <NUM_LIT> ) ] ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> def test_makeRelayCrypto ( self , mock_rc , mock_maes , mock_sha1 , mock_hexp , <EOL> mock_hext ) : <EOL> secret_input = '<STR_LIT>' <EOL> km = [ chr ( i ) for i in range ( <NUM_LIT> ) ] <EOL> ret = ntor . _makeRelayCrypto ( secret_input ) <EOL> mock_hext . assert_called_once_with ( <EOL> salt = '<STR_LIT>' , <EOL> input_key_material = '<STR_LIT>' , <EOL> hash = hashlib . sha256 ) <EOL> mock_hexp . assert_called_once_with ( <EOL> pseudo_random_key = '<STR_LIT>' , <EOL> info = '<STR_LIT>' , <EOL> length = <NUM_LIT> , <EOL> hash = hashlib . sha256 ) <EOL> self . assertEqual ( mock_sha1 . call_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( mock_sha1 . call_args_list , <EOL> [ mock . call ( km [ : <NUM_LIT:20> ] ) , mock . call ( km [ <NUM_LIT:20> : <NUM_LIT> ] ) ] ) <EOL> self . assertEqual ( mock_maes . call_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( mock_maes . call_args_list , <EOL> [ mock . call ( km [ <NUM_LIT> : <NUM_LIT> ] ) , mock . call ( km [ <NUM_LIT> : <NUM_LIT> ] ) ] ) <EOL> mock_rc . assert_called_once_with ( <EOL> forward_cipher = '<STR_LIT>' , forward_digest = '<STR_LIT>' , <EOL> backward_cipher = '<STR_LIT>' , backward_digest = '<STR_LIT>' ) <EOL> self . assertEqual ( ret , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = True ) <EOL> def test_EXP_bad ( self , mock_az , mock_pk , mock_csm ) : <EOL> ret , bad = ntor . _EXP ( '<STR_LIT:n>' , '<STR_LIT:p>' ) <EOL> mock_pk . assert_called_once_with ( '<STR_LIT:p>' ) <EOL> mock_csm . assert_called_once_with ( '<STR_LIT:n>' , '<STR_LIT>' ) <EOL> self . assertTrue ( bad ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = False ) <EOL> def test_EXP ( self , mock_az , mock_pk , mock_csm ) : <EOL> ret , bad = ntor . _EXP ( '<STR_LIT:n>' , '<STR_LIT:p>' ) <EOL> mock_pk . assert_called_once_with ( '<STR_LIT:p>' ) <EOL> mock_csm . assert_called_once_with ( '<STR_LIT:n>' , '<STR_LIT>' ) <EOL> mock_az . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( ret , '<STR_LIT>' ) <EOL> self . assertFalse ( bad ) </s>
