<s> """<STR_LIT>""" <EOL> import os <EOL> from binascii import Error as BinasciiError , b2a_base64 , a2b_base64 <EOL> try : <EOL> import Crypto <EOL> import pyasn1 <EOL> except ImportError : <EOL> skip = "<STR_LIT>" <EOL> else : <EOL> from twisted . conch . ssh . keys import Key , BadKeyError <EOL> from twisted . conch . client . knownhosts import PlainEntry , HashedEntry , KnownHostsFile , UnparsedEntry , ConsoleUI <EOL> from twisted . conch . client import default <EOL> from zope . interface . verify import verifyObject <EOL> from twisted . python . filepath import FilePath <EOL> from twisted . trial . unittest import TestCase <EOL> from twisted . internet . defer import Deferred <EOL> from twisted . conch . interfaces import IKnownHostEntry <EOL> from twisted . conch . error import HostKeyChanged , UserRejectedKey , InvalidEntry <EOL> sampleEncodedKey = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> otherSampleEncodedKey = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> thirdSampleEncodedKey = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> sampleKey = a2b_base64 ( sampleEncodedKey ) <EOL> otherSampleKey = a2b_base64 ( otherSampleEncodedKey ) <EOL> thirdSampleKey = a2b_base64 ( thirdSampleEncodedKey ) <EOL> samplePlaintextLine = ( <EOL> "<STR_LIT>" + sampleEncodedKey + "<STR_LIT:\n>" ) <EOL> otherSamplePlaintextLine = ( <EOL> "<STR_LIT>" + otherSampleEncodedKey + "<STR_LIT:\n>" ) <EOL> sampleHostIPLine = ( <EOL> "<STR_LIT>" + sampleEncodedKey + "<STR_LIT:\n>" ) <EOL> sampleHashedLine = ( <EOL> "<STR_LIT>" + <EOL> sampleEncodedKey + "<STR_LIT:\n>" ) <EOL> class EntryTestsMixin : <EOL> """<STR_LIT>""" <EOL> def test_providesInterface ( self ) : <EOL> """<STR_LIT>""" <EOL> verifyObject ( IKnownHostEntry , self . entry ) <EOL> def test_fromString ( self ) : <EOL> """<STR_LIT>""" <EOL> entry = self . entry <EOL> self . assertEqual ( entry . publicKey , Key . fromString ( sampleKey ) ) <EOL> self . assertEqual ( entry . keyType , "<STR_LIT>" ) <EOL> def test_matchesKey ( self ) : <EOL> """<STR_LIT>""" <EOL> twistedmatrixDotCom = Key . fromString ( sampleKey ) <EOL> divmodDotCom = Key . fromString ( otherSampleKey ) <EOL> self . assertEqual ( <EOL> True , <EOL> self . entry . matchesKey ( twistedmatrixDotCom ) ) <EOL> self . assertEqual ( <EOL> False , <EOL> self . entry . matchesKey ( divmodDotCom ) ) <EOL> def test_matchesHost ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( True , self . entry . matchesHost ( <EOL> "<STR_LIT>" ) ) <EOL> self . assertEqual ( False , self . entry . matchesHost ( <EOL> "<STR_LIT>" ) ) <EOL> class PlainEntryTests ( EntryTestsMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> plaintextLine = samplePlaintextLine <EOL> hostIPLine = sampleHostIPLine <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . entry = PlainEntry . fromString ( self . plaintextLine ) <EOL> def test_matchesHostIP ( self ) : <EOL> """<STR_LIT>""" <EOL> self . entry = PlainEntry . fromString ( self . hostIPLine ) <EOL> self . assertEqual ( True , self . entry . matchesHost ( "<STR_LIT>" ) ) <EOL> self . test_matchesHost ( ) <EOL> def test_toString ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( self . entry . toString ( ) , self . plaintextLine . rstrip ( "<STR_LIT:\n>" ) ) <EOL> multiHostEntry = PlainEntry . fromString ( self . hostIPLine ) <EOL> self . assertEqual ( multiHostEntry . toString ( ) , <EOL> self . hostIPLine . rstrip ( "<STR_LIT:\n>" ) ) <EOL> class PlainTextWithCommentTests ( PlainEntryTests ) : <EOL> """<STR_LIT>""" <EOL> plaintextLine = samplePlaintextLine [ : - <NUM_LIT:1> ] + "<STR_LIT>" <EOL> hostIPLine = sampleHostIPLine [ : - <NUM_LIT:1> ] + "<STR_LIT>" <EOL> class HashedEntryTests ( EntryTestsMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> hashedLine = sampleHashedLine <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . entry = HashedEntry . fromString ( self . hashedLine ) <EOL> def test_toString ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( self . entry . toString ( ) , self . hashedLine . rstrip ( "<STR_LIT:\n>" ) ) <EOL> class HashedEntryWithCommentTests ( HashedEntryTests ) : <EOL> """<STR_LIT>""" <EOL> hashedLine = sampleHashedLine [ : - <NUM_LIT:1> ] + "<STR_LIT>" <EOL> class UnparsedEntryTests ( TestCase , EntryTestsMixin ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . entry = UnparsedEntry ( "<STR_LIT>" ) <EOL> def test_fromString ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( "<STR_LIT>" , <EOL> self . entry . _string ) <EOL> def test_matchesHost ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( False , self . entry . matchesHost ( "<STR_LIT>" ) ) <EOL> def test_matchesKey ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( False , self . entry . matchesKey ( Key . fromString ( sampleKey ) ) ) <EOL> def test_toString ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( "<STR_LIT>" , self . entry . toString ( ) ) <EOL> class ParseErrorTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def invalidEntryTest ( self , cls ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( InvalidEntry , cls . fromString , "<STR_LIT>" ) <EOL> def notBase64Test ( self , cls ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( BinasciiError , cls . fromString , "<STR_LIT>" ) <EOL> def badKeyTest ( self , cls , prefix ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( BadKeyError , cls . fromString , '<STR_LIT:U+0020>' . join ( <EOL> [ prefix , "<STR_LIT>" , b2a_base64 ( <EOL> "<STR_LIT>" ) . strip ( ) ] ) ) <EOL> def test_invalidPlainEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> self . invalidEntryTest ( PlainEntry ) <EOL> def test_invalidHashedEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> self . invalidEntryTest ( HashedEntry ) <EOL> a , b , c = sampleHashedLine . split ( ) <EOL> self . assertRaises ( InvalidEntry , HashedEntry . fromString , '<STR_LIT:U+0020>' . join ( <EOL> [ a + "<STR_LIT>" , b , c ] ) ) <EOL> def test_plainNotBase64 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . notBase64Test ( PlainEntry ) <EOL> def test_hashedNotBase64 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . notBase64Test ( HashedEntry ) <EOL> a , b , c = sampleHashedLine . split ( ) <EOL> self . assertRaises ( <EOL> BinasciiError , HashedEntry . fromString , <EOL> '<STR_LIT:U+0020>' . join ( [ "<STR_LIT>" + b2a_base64 ( "<STR_LIT>" ) . strip ( ) , b , c ] ) ) <EOL> self . assertRaises ( <EOL> BinasciiError , HashedEntry . fromString , <EOL> '<STR_LIT:U+0020>' . join ( [ HashedEntry . MAGIC + b2a_base64 ( "<STR_LIT>" ) . strip ( ) + "<STR_LIT>" , <EOL> b , c ] ) ) <EOL> self . assertRaises ( <EOL> BinasciiError , HashedEntry . fromString , <EOL> '<STR_LIT:U+0020>' . join ( [ "<STR_LIT>" , b , c ] ) ) <EOL> def test_hashedBadKey ( self ) : <EOL> """<STR_LIT>""" <EOL> a , b , c = sampleHashedLine . split ( ) <EOL> self . badKeyTest ( HashedEntry , a ) <EOL> def test_plainBadKey ( self ) : <EOL> """<STR_LIT>""" <EOL> self . badKeyTest ( PlainEntry , "<STR_LIT>" ) <EOL> class KnownHostsDatabaseTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def pathWithContent ( self , content ) : <EOL> """<STR_LIT>""" <EOL> fp = FilePath ( self . mktemp ( ) ) <EOL> fp . setContent ( content ) <EOL> return fp <EOL> def loadSampleHostsFile ( self , content = ( <EOL> sampleHashedLine + otherSamplePlaintextLine + <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) : <EOL> """<STR_LIT>""" <EOL> return KnownHostsFile . fromPath ( self . pathWithContent ( content ) ) <EOL> def test_loadFromPath ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> self . assertEqual ( len ( hostsFile . _entries ) , <NUM_LIT:6> ) <EOL> def test_verifyHashedEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ( sampleHashedLine ) ) <EOL> self . assertIsInstance ( hostsFile . _entries [ <NUM_LIT:0> ] , HashedEntry ) <EOL> self . assertEqual ( True , hostsFile . _entries [ <NUM_LIT:0> ] . matchesHost ( <EOL> "<STR_LIT>" ) ) <EOL> def test_verifyPlainEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ( otherSamplePlaintextLine ) ) <EOL> self . assertIsInstance ( hostsFile . _entries [ <NUM_LIT:0> ] , PlainEntry ) <EOL> self . assertEqual ( True , hostsFile . _entries [ <NUM_LIT:0> ] . matchesHost ( <EOL> "<STR_LIT>" ) ) <EOL> def test_verifyUnparsedEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ( "<STR_LIT:\n>" ) ) <EOL> self . assertIsInstance ( hostsFile . _entries [ <NUM_LIT:0> ] , UnparsedEntry ) <EOL> self . assertEqual ( hostsFile . _entries [ <NUM_LIT:0> ] . toString ( ) , "<STR_LIT>" ) <EOL> def test_verifyUnparsedComment ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ( "<STR_LIT>" ) ) <EOL> self . assertIsInstance ( hostsFile . _entries [ <NUM_LIT:0> ] , UnparsedEntry ) <EOL> self . assertEqual ( hostsFile . _entries [ <NUM_LIT:0> ] . toString ( ) , <EOL> "<STR_LIT>" ) <EOL> def test_verifyUnparsableLine ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ( "<STR_LIT>" ) ) <EOL> self . assertIsInstance ( hostsFile . _entries [ <NUM_LIT:0> ] , UnparsedEntry ) <EOL> self . assertEqual ( hostsFile . _entries [ <NUM_LIT:0> ] . toString ( ) , <EOL> "<STR_LIT>" ) <EOL> def test_verifyUnparsableEncryptionMarker ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ( "<STR_LIT>" ) ) <EOL> self . assertIsInstance ( hostsFile . _entries [ <NUM_LIT:0> ] , UnparsedEntry ) <EOL> self . assertEqual ( hostsFile . _entries [ <NUM_LIT:0> ] . toString ( ) , <EOL> "<STR_LIT>" ) <EOL> def test_loadNonExistent ( self ) : <EOL> """<STR_LIT>""" <EOL> pn = self . mktemp ( ) <EOL> knownHostsFile = KnownHostsFile . fromPath ( FilePath ( pn ) ) <EOL> self . assertEqual ( [ ] , list ( knownHostsFile . _entries ) ) <EOL> self . assertEqual ( False , FilePath ( pn ) . exists ( ) ) <EOL> knownHostsFile . save ( ) <EOL> self . assertEqual ( True , FilePath ( pn ) . exists ( ) ) <EOL> def test_loadNonExistentParent ( self ) : <EOL> """<STR_LIT>""" <EOL> thePath = FilePath ( self . mktemp ( ) ) <EOL> knownHostsPath = thePath . child ( "<STR_LIT:foo>" ) . child ( "<STR_LIT>" ) <EOL> knownHostsFile = KnownHostsFile . fromPath ( knownHostsPath ) <EOL> knownHostsFile . save ( ) <EOL> knownHostsPath . restat ( False ) <EOL> self . assertEqual ( True , knownHostsPath . exists ( ) ) <EOL> def test_savingAddsEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> path = self . pathWithContent ( sampleHashedLine + <EOL> otherSamplePlaintextLine ) <EOL> knownHostsFile = KnownHostsFile . fromPath ( path ) <EOL> newEntry = knownHostsFile . addHostKey ( "<STR_LIT>" , <EOL> Key . fromString ( thirdSampleKey ) ) <EOL> expectedContent = ( <EOL> sampleHashedLine + <EOL> otherSamplePlaintextLine + HashedEntry . MAGIC + <EOL> b2a_base64 ( newEntry . _hostSalt ) . strip ( ) + "<STR_LIT:|>" + <EOL> b2a_base64 ( newEntry . _hostHash ) . strip ( ) + "<STR_LIT>" + <EOL> thirdSampleEncodedKey + "<STR_LIT:\n>" ) <EOL> self . assertEqual ( <NUM_LIT:3> , expectedContent . count ( "<STR_LIT:\n>" ) ) <EOL> knownHostsFile . save ( ) <EOL> self . assertEqual ( expectedContent , path . getContent ( ) ) <EOL> def test_hasPresentKey ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> self . assertEqual ( True , hostsFile . hasHostKey ( <EOL> "<STR_LIT>" , Key . fromString ( sampleKey ) ) ) <EOL> def test_hasNonPresentKey ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> self . assertEqual ( False , hostsFile . hasHostKey ( <EOL> "<STR_LIT>" , Key . fromString ( sampleKey ) ) ) <EOL> def test_hasKeyMismatch ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> exception = self . assertRaises ( <EOL> HostKeyChanged , hostsFile . hasHostKey , <EOL> "<STR_LIT>" , Key . fromString ( otherSampleKey ) ) <EOL> self . assertEqual ( exception . offendingEntry , hostsFile . _entries [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( exception . lineno , <NUM_LIT:1> ) <EOL> self . assertEqual ( exception . path , hostsFile . _savePath ) <EOL> def test_addHostKey ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> aKey = Key . fromString ( thirdSampleKey ) <EOL> self . assertEqual ( False , <EOL> hostsFile . hasHostKey ( "<STR_LIT>" , aKey ) ) <EOL> newEntry = hostsFile . addHostKey ( "<STR_LIT>" , aKey ) <EOL> self . assertEqual ( <NUM_LIT:20> , len ( newEntry . _hostSalt ) ) <EOL> self . assertEqual ( True , <EOL> newEntry . matchesHost ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( newEntry . keyType , "<STR_LIT>" ) <EOL> self . assertEqual ( aKey , newEntry . publicKey ) <EOL> self . assertEqual ( True , <EOL> hostsFile . hasHostKey ( "<STR_LIT>" , aKey ) ) <EOL> def test_randomSalts ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> aKey = Key . fromString ( thirdSampleKey ) <EOL> self . assertNotEqual ( <EOL> hostsFile . addHostKey ( "<STR_LIT>" , aKey ) . _hostSalt , <EOL> hostsFile . addHostKey ( "<STR_LIT>" , aKey ) . _hostSalt ) <EOL> def test_verifyValidKey ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> hostsFile . addHostKey ( "<STR_LIT>" , Key . fromString ( sampleKey ) ) <EOL> ui = FakeUI ( ) <EOL> d = hostsFile . verifyHostKey ( ui , "<STR_LIT>" , "<STR_LIT>" , <EOL> Key . fromString ( sampleKey ) ) <EOL> l = [ ] <EOL> d . addCallback ( l . append ) <EOL> self . assertEqual ( l , [ True ] ) <EOL> def test_verifyInvalidKey ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> wrongKey = Key . fromString ( thirdSampleKey ) <EOL> ui = FakeUI ( ) <EOL> hostsFile . addHostKey ( "<STR_LIT>" , Key . fromString ( sampleKey ) ) <EOL> d = hostsFile . verifyHostKey ( <EOL> ui , "<STR_LIT>" , "<STR_LIT>" , wrongKey ) <EOL> return self . assertFailure ( d , HostKeyChanged ) <EOL> def verifyNonPresentKey ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> absentKey = Key . fromString ( thirdSampleKey ) <EOL> ui = FakeUI ( ) <EOL> l = [ ] <EOL> d = hostsFile . verifyHostKey ( <EOL> ui , "<STR_LIT>" , "<STR_LIT>" , absentKey ) <EOL> d . addBoth ( l . append ) <EOL> self . assertEqual ( [ ] , l ) <EOL> self . assertEqual ( <EOL> ui . promptText , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return ui , l , hostsFile <EOL> def test_verifyNonPresentKey_Yes ( self ) : <EOL> """<STR_LIT>""" <EOL> ui , l , knownHostsFile = self . verifyNonPresentKey ( ) <EOL> ui . promptDeferred . callback ( True ) <EOL> self . assertEqual ( [ True ] , l ) <EOL> reloaded = KnownHostsFile . fromPath ( knownHostsFile . _savePath ) <EOL> self . assertEqual ( <EOL> True , <EOL> reloaded . hasHostKey ( "<STR_LIT>" , Key . fromString ( thirdSampleKey ) ) ) <EOL> self . assertEqual ( <EOL> True , <EOL> reloaded . hasHostKey ( "<STR_LIT>" , <EOL> Key . fromString ( thirdSampleKey ) ) ) <EOL> def test_verifyNonPresentKey_No ( self ) : <EOL> """<STR_LIT>""" <EOL> ui , l , knownHostsFile = self . verifyNonPresentKey ( ) <EOL> ui . promptDeferred . callback ( False ) <EOL> l [ <NUM_LIT:0> ] . trap ( UserRejectedKey ) <EOL> def test_verifyHostIPMismatch ( self ) : <EOL> """<STR_LIT>""" <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> wrongKey = Key . fromString ( thirdSampleKey ) <EOL> ui = FakeUI ( ) <EOL> d = hostsFile . verifyHostKey ( <EOL> ui , "<STR_LIT>" , "<STR_LIT>" , wrongKey ) <EOL> return self . assertFailure ( d , HostKeyChanged ) <EOL> def test_verifyKeyForHostAndIP ( self ) : <EOL> """<STR_LIT>""" <EOL> ui = FakeUI ( ) <EOL> hostsFile = self . loadSampleHostsFile ( ) <EOL> expectedKey = Key . fromString ( sampleKey ) <EOL> hostsFile . verifyHostKey ( <EOL> ui , "<STR_LIT>" , "<STR_LIT>" , expectedKey ) <EOL> self . assertEqual ( <EOL> True , KnownHostsFile . fromPath ( hostsFile . _savePath ) . hasHostKey ( <EOL> "<STR_LIT>" , expectedKey ) ) <EOL> self . assertEqual ( <EOL> [ "<STR_LIT>" <EOL> "<STR_LIT>" ] , <EOL> ui . userWarnings ) <EOL> class FakeFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . inlines = [ ] <EOL> self . outchunks = [ ] <EOL> self . closed = False <EOL> def readline ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . inlines . pop ( <NUM_LIT:0> ) <EOL> def write ( self , chunk ) : <EOL> """<STR_LIT>""" <EOL> if self . closed : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> self . outchunks . append ( chunk ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . closed = True <EOL> class ConsoleUITests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . fakeFile = FakeFile ( ) <EOL> self . ui = ConsoleUI ( self . openFile ) <EOL> def openFile ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . fakeFile <EOL> def newFile ( self , lines ) : <EOL> """<STR_LIT>""" <EOL> self . fakeFile = FakeFile ( ) <EOL> self . fakeFile . inlines = lines <EOL> def test_promptYes ( self ) : <EOL> """<STR_LIT>""" <EOL> for okYes in [ '<STR_LIT:yes>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . newFile ( [ okYes ] ) <EOL> l = [ ] <EOL> self . ui . prompt ( "<STR_LIT>" ) . addCallback ( l . append ) <EOL> self . assertEqual ( [ "<STR_LIT>" ] , self . fakeFile . outchunks ) <EOL> self . assertEqual ( [ True ] , l ) <EOL> self . assertEqual ( True , self . fakeFile . closed ) <EOL> def test_promptNo ( self ) : <EOL> """<STR_LIT>""" <EOL> for okNo in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . newFile ( [ okNo ] ) <EOL> l = [ ] <EOL> self . ui . prompt ( "<STR_LIT>" ) . addCallback ( l . append ) <EOL> self . assertEqual ( [ "<STR_LIT>" ] , self . fakeFile . outchunks ) <EOL> self . assertEqual ( [ False ] , l ) <EOL> self . assertEqual ( True , self . fakeFile . closed ) <EOL> def test_promptRepeatedly ( self ) : <EOL> """<STR_LIT>""" <EOL> self . newFile ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:yes>' ] ) <EOL> l = [ ] <EOL> self . ui . prompt ( "<STR_LIT>" ) . addCallback ( l . append ) <EOL> self . assertEqual ( [ True ] , l ) <EOL> self . assertEqual ( self . fakeFile . outchunks , <EOL> [ "<STR_LIT>" ] + <EOL> [ "<STR_LIT>" ] * <NUM_LIT:3> ) <EOL> self . assertEqual ( True , self . fakeFile . closed ) <EOL> self . newFile ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> l = [ ] <EOL> self . ui . prompt ( "<STR_LIT>" ) . addCallback ( l . append ) <EOL> self . assertEqual ( [ False ] , l ) <EOL> self . assertEqual ( self . fakeFile . outchunks , <EOL> [ "<STR_LIT>" ] + <EOL> [ "<STR_LIT>" ] * <NUM_LIT:3> ) <EOL> self . assertEqual ( True , self . fakeFile . closed ) <EOL> def test_promptOpenFailed ( self ) : <EOL> """<STR_LIT>""" <EOL> def raiseIt ( ) : <EOL> raise IOError ( ) <EOL> ui = ConsoleUI ( raiseIt ) <EOL> d = ui . prompt ( "<STR_LIT>" ) <EOL> return self . assertFailure ( d , IOError ) <EOL> def test_warn ( self ) : <EOL> """<STR_LIT>""" <EOL> self . ui . warn ( "<STR_LIT>" ) <EOL> self . assertEqual ( [ "<STR_LIT>" ] , self . fakeFile . outchunks ) <EOL> self . assertEqual ( True , self . fakeFile . closed ) <EOL> def test_warnOpenFailed ( self ) : <EOL> """<STR_LIT>""" <EOL> def raiseIt ( ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> ui = ConsoleUI ( raiseIt ) <EOL> ui . warn ( "<STR_LIT>" ) <EOL> self . assertEqual ( len ( self . flushLoggedErrors ( ZeroDivisionError ) ) , <NUM_LIT:1> ) <EOL> class FakeUI ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . userWarnings = [ ] <EOL> self . promptDeferred = None <EOL> self . promptText = None <EOL> def prompt ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . promptText = text <EOL> self . promptDeferred = Deferred ( ) <EOL> return self . promptDeferred <EOL> def warn ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . userWarnings . append ( text ) <EOL> class FakeObject ( object ) : <EOL> """<STR_LIT>""" <EOL> class DefaultAPITests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def patchedOpen ( self , fname , mode ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( fname , "<STR_LIT>" ) <EOL> self . assertEqual ( mode , "<STR_LIT>" ) <EOL> return self . fakeFile <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . fakeFile = FakeFile ( ) <EOL> self . patch ( default , "<STR_LIT>" , self . patchedOpen ) <EOL> self . hostsOption = self . mktemp ( ) <EOL> knownHostsFile = KnownHostsFile ( FilePath ( self . hostsOption ) ) <EOL> knownHostsFile . addHostKey ( "<STR_LIT>" , <EOL> Key . fromString ( sampleKey ) ) <EOL> knownHostsFile . addHostKey ( "<STR_LIT>" , Key . fromString ( sampleKey ) ) <EOL> knownHostsFile . save ( ) <EOL> self . fakeTransport = FakeObject ( ) <EOL> self . fakeTransport . factory = FakeObject ( ) <EOL> self . options = self . fakeTransport . factory . options = { <EOL> '<STR_LIT:host>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : self . hostsOption <EOL> } <EOL> def test_verifyOKKey ( self ) : <EOL> """<STR_LIT>""" <EOL> l = [ ] <EOL> default . verifyHostKey ( self . fakeTransport , "<STR_LIT>" , sampleKey , <EOL> "<STR_LIT>" ) . addCallback ( l . append ) <EOL> self . assertEqual ( [ <NUM_LIT:1> ] , l ) <EOL> def replaceHome ( self , tempHome ) : <EOL> """<STR_LIT>""" <EOL> oldHome = os . environ . get ( '<STR_LIT>' ) <EOL> def cleanupHome ( ) : <EOL> if oldHome is None : <EOL> del os . environ [ '<STR_LIT>' ] <EOL> else : <EOL> os . environ [ '<STR_LIT>' ] = oldHome <EOL> self . addCleanup ( cleanupHome ) <EOL> os . environ [ '<STR_LIT>' ] = tempHome <EOL> def test_noKnownHostsOption ( self ) : <EOL> """<STR_LIT>""" <EOL> l = [ ] <EOL> tmpdir = self . mktemp ( ) <EOL> oldHostsOption = self . hostsOption <EOL> hostsNonOption = FilePath ( tmpdir ) . child ( "<STR_LIT>" ) . child ( "<STR_LIT>" ) <EOL> hostsNonOption . parent ( ) . makedirs ( ) <EOL> FilePath ( oldHostsOption ) . moveTo ( hostsNonOption ) <EOL> self . replaceHome ( tmpdir ) <EOL> self . options [ '<STR_LIT>' ] = None <EOL> default . verifyHostKey ( self . fakeTransport , "<STR_LIT>" , sampleKey , <EOL> "<STR_LIT>" ) . addCallback ( l . append ) <EOL> self . assertEqual ( [ <NUM_LIT:1> ] , l ) <EOL> def test_verifyHostButNotIP ( self ) : <EOL> """<STR_LIT>""" <EOL> l = [ ] <EOL> default . verifyHostKey ( self . fakeTransport , "<STR_LIT>" , sampleKey , <EOL> "<STR_LIT>" ) . addCallback ( l . append ) <EOL> self . assertEqual ( <EOL> [ "<STR_LIT>" <EOL> "<STR_LIT>" ] , <EOL> self . fakeFile . outchunks ) <EOL> self . assertEqual ( [ <NUM_LIT:1> ] , l ) <EOL> knownHostsFile = KnownHostsFile . fromPath ( FilePath ( self . hostsOption ) ) <EOL> self . assertEqual ( True , knownHostsFile . hasHostKey ( "<STR_LIT>" , <EOL> Key . fromString ( sampleKey ) ) ) <EOL> def test_verifyQuestion ( self ) : <EOL> """<STR_LIT>""" <EOL> self . fakeTransport . factory . options [ '<STR_LIT:host>' ] = '<STR_LIT>' <EOL> self . fakeFile . inlines . append ( "<STR_LIT>" ) <EOL> d = default . verifyHostKey ( <EOL> self . fakeTransport , "<STR_LIT>" , otherSampleKey , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> [ "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ] , <EOL> self . fakeFile . outchunks ) <EOL> return self . assertFailure ( d , UserRejectedKey ) <EOL> def test_verifyBadKey ( self ) : <EOL> """<STR_LIT>""" <EOL> d = default . verifyHostKey ( <EOL> self . fakeTransport , "<STR_LIT>" , otherSampleKey , <EOL> "<STR_LIT>" ) <EOL> return self . assertFailure ( d , HostKeyChanged ) </s>
