Ϫf-dZddlZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z dd lmZdd lmZdd lmZd Zd ZedrddlmZddlmZddlmZddlmZddlmZndZdZeZ ejBrdZdZejDjGsdZdZejHjGsdZdZGddeZ%GddeZ&y)z, Tests for L{twisted.conch.client.default}. N)skipIf) ConchError)keydata)StringTransport) nativeString)FilePath) requireModule)platform)TestCaseF cryptography)default)SSHAgentClient)SSHUserAuthClient) ConchOptions)KeyTz7cryptography required for twisted.conch.client.default.zlgenericAnswers and getPassword does not work on Windows. Should be fixed as part of fixing bug 6409 and 6410z#sys.stdin is not an interactive ttyz$sys.stdout is not an interactive ttyceZdZdZdZdZdZdZdZdZ dZ d Z e e ed Ze e ed Ze e ed Ze e ed Zy)SSHUserAuthClientTestszm Tests for L{SSHUserAuthClient}. @type rsaPublic: L{Key} @ivar rsaPublic: A public RSA key. ctjtj|_t |j |_|jj|jjd|_ |jjtj|jjdjtjy)Nid_rsa id_rsa.pub) r fromStringrpublicRSA_openssh rsaPublicrmktemptmpdirmakedirschildrsaFile setContentprivateRSA_opensshselfs A/usr/lib/python3/dist-packages/twisted/conch/test/test_default.pysetUpzSSHUserAuthClientTests.setUp:s(A(ABt{{}-  {{((2   : :; ,'2273L3LMcJtdtd}t}t}|j |||_d}|j |j||j|jd|jjzdz|zdzy)z When connected to an agent, L{SSHUserAuthClient} can use it to request signatures of particular data with a particular L{Key}. userNs Sign heres - s s) rrrrmakeConnectionkeyAgentsignDatar assertEqualvalueblob)r#clientagent transport cleartexts r$test_signDataWithAgentz-SSHUserAuthClientTests.test_signDataWithAgentBs #7LNDA #%  Y'  2  OO  1nn!!# $  "  " r&c,t}|jjg|_|j }|j |j |j||j|j|j y)a L{SSHUserAuthClient} looks up public keys from the agent using the L{SSHAgentClient} class. That L{SSHAgentClient.getPublicKey} returns a L{Key} object with one of the public keys in the agent. If no more keys are present, it returns L{None}. N) rrr.blobs getPublicKey assertTrueisPublicr, assertIsNone)r#r0keys r$test_agentGetPublicKeyz-SSHUserAuthClientTests.test_agentGetPublicKeyWsn ~~**,-   "  ' dnn- %,,./r&ct}|jjg|_t d|d}|j }|j |j|j||jy)z L{SSHUserAuthClient.getPublicKey()} is able to get a public key from the first file described by its options' C{identitys} list, and return the corresponding public L{Key} object. r(N) rrpath identitysrr6r7r8r,r)r#optionsr/r:s r$test_getPublicKeyFromFilez0SSHUserAuthClientTests.test_getPublicKeyFromFileesb .!\\../"7GT:!!#  ' dnn-r&c"t}|jjg|_t }t d|d}||_|j}|j|j|j||jy)z If an agent is present, but doesn't return a key, L{SSHUserAuthClient.getPublicKey} continue with the normal key lookup. r(N) rrr=r>rrr*r6r7r8r,r)r#r?r0r/r:s r$test_getPublicKeyAgentFallbackz5SSHUserAuthClientTests.test_getPublicKeyAgentFallbackrsq .!\\../ "7GT:!!#  ' dnn-r&ct}|jjdjtj |jjd}|jtj |jj|jg|_ |jjdjdtd|d}|j}|j|j|j|tj tj |j|j"|jj|jgy)z If L{keys.Key.fromFile} raises a L{keys.BadKeyError}, the L{SSHUserAuthClient.getPublicKey} tries again to get a public key by calling itself recursively. z id_dsa.pubid_dsars not a key!r(N)rrrr rpublicDSA_opensshprivateDSA_opensshrr=r>rr6r7r8r,rr usedFiles)r#r?dsaFiler/r:s r$test_getPublicKeyBadKeyErrorz3SSHUserAuthClientTests.test_getPublicKeyBadKeyErrors . ,'2273L3LM++##H-7556!\\.. = ,'22=A"7GT:!!#  ' cnnW-F-FGH ))DLL,=,=w||+LMr&ctjtjt }j j g|_td|d}|jfd}|jj|S)z L{SSHUserAuthClient.getPrivateKey} will load a private key from the last used file populated by L{SSHUserAuthClient.getPublicKey}, and return a L{Deferred} which fires with the corresponding private L{Key}. r(Nchj|jj|yN assertFalser8r,r: rsaPrivater#s r$_cbGetPrivateKeyzCSSHUserAuthClientTests.test_getPrivateKey.._cbGetPrivateKey'   S\\^ ,   S* -r&) rrrr!rrr=r>rr6 getPrivateKey addCallback)r#r?r/rQrPs` @r$test_getPrivateKeyz)SSHUserAuthClientTests.test_getPrivateKeysq ^^G$>$>? .!\\../"7GT: .##%112BCCr&ctjtjdjj j dt}jjg|_ td|d}|jfd}fd}j|d||jj|S) z L{SSHUserAuthClient} can get a private key from a file, and return a Deferred called back with a private L{Key} object, even if the key is encrypted. sthis is the passphraseopenssh) passphraser(Ncnj|djjdtS)NzEnter passphrase for key 'z': )r,rr=r)promptrXr#s r$ _getPasswordzISSHUserAuthClientTests.test_getPrivateKeyPassphrase.._getPasswords8   4T\\5F5F4GsK  + +r&chj|jj|yrLrMrOs r$rQzMSSHUserAuthClientTests.test_getPrivateKeyPassphrase.._cbGetPrivateKeyrRr&r[)rrrr!rr toStringrr=r>rr6patchrSrT)r#r?r/r[rQrXrPs` @@r$test_getPrivateKeyPassphrasez3SSHUserAuthClientTests.test_getPrivateKeyPassphrases ^^G$>$>? .   3 3I* 3 UV.!\\../"7GT: ,  . 6><8##%112BCCr&cGdd}t}td|d}|d|_fd}jtj d||j }|jjd|S) zn Get the password using L{twisted.conch.client.default.SSHUserAuthClient.getPassword} ceZdZdZdZy)>SSHUserAuthClientTests.test_getPassword..FakeTransportc ||_||_yrL)r1host)r#rds r$__init__zGSSHUserAuthClientTests.test_getPassword..FakeTransport.__init__s!%  r&c|SrLr"s r$getPeerzFSSHUserAuthClientTests.test_getPassword..FakeTransport.getPeers r&N)__name__ __module__ __qualname__rerhrgr&r$ FakeTransportrbs  ! r&rlr(Nz 127.0.0.1c*j|dy)Nzuser@127.0.0.1's password: bad passwordr,rZr#s r$getpassz8SSHUserAuthClientTests.test_getPassword..getpasss   V%B C!r&rq bad password) rrr1r^rrq getPasswordrTr,)r#rlr?r/rqds` r$test_getPasswordz'SSHUserAuthClientTests.test_getPasswordss  ."7GT:(5 " 7??Iw7     d&&8r&ct}td|d}dfd}jtjd||j }|j jd|S)z Get the password using L{twisted.conch.client.default.SSHUserAuthClient.getPassword} using a different prompt. r(NsGive up your passwordc<j|ty)Nrn)r,r)prZr#s r$rqz>SSHUserAuthClientTests.test_getPasswordPrompt..getpasss   Q V 4 5!r&rqrr)rrr^rrqrsrTr,)r#r?r/rqrtrZs` @r$test_getPasswordPromptz-SSHUserAuthClientTests.test_getPasswordPromptsc."7GT:) " 7??Iw7   v & d&&8r&c<t}td|d}d}jtjd|t j t jc|jd}|jfd}j|ty)z Get the password using L{twisted.conch.client.default.SSHUserAuthClient.getPassword} and trigger a {twisted.conch.error import ConchError}. r(Nctd)NzUser pressed CTRL-C)KeyboardInterrupt)rZs r$rqzBSSHUserAuthClientTests.test_getPasswordConchError..getpasss#$9: :r&rq?cljgtjtjg|SrL)r,sysstdoutstdin)failr#rrs r$ check_syszDSSHUserAuthClientTests.test_getPasswordConchError..check_syss)   fe_szz399.E FKr&) rrr^rrqrrrrs addErrback assertFailurer)r#r?r/rqrtrrrs` @@r$test_getPasswordConchErrorz1SSHUserAuthClientTests.test_getPasswordConchErrors."7GT: ; 7??Iw7 CII    t $     1j)r&c t}td|d}fd}jtjd|fd}jtd||j ddd d g}|j jdd g|S) zU L{twisted.conch.client.default.SSHUserAuthClient.getGenericAnswers} r(Nc*j|dy)Nz pass promptrqrorps r$rqz>SSHUserAuthClientTests.test_getGenericAnswers..getpasss   V] 3r&rqc*j|dy)Nzraw_input prompt raw_inputrorps r$rz@SSHUserAuthClientTests.test_getGenericAnswers..raw_inputs   V%7 8r&_inputsNames Instruction)s pass promptF)sraw_input promptTr)rrr^rrqgetGenericAnswersrTassertListEqual)r#r?r/rqrrts` r$test_getGenericAnswersz-SSHUserAuthClientTests.test_getGenericAnswers s ."7GT:  7??Iw7  7Hi0  $ $   $&A B  d**Y ,DEr&N)rirjrk__doc__r%r3r;r@rBrIrUr_rdoSkip skipReasonruryrrrgr&r$rr2sN * 0 . .N$D&D8 FJ 6 FJ & FJ* *. FJ r&rceZdZdZdZdZy)ConchOptionsParsingz Options parsing. ct}|jt|jd}|j d|j t}|jd|j |ddg|jd|j |ddg|jd|j |dgdy) z) Specify MAC algorithms. z invalid-maczUnknown mac typez hmac-sha2-512macss hmac-sha2-512z hmac-sha2-256,hmac-sha1,hmac-md5)s hmac-sha2-256s hmac-sha1shmac-md5N)r assertRaises SystemExitopt_macsassertIncoder,r#optses r$ test_macszConchOptionsParsing.test_macs*s~   j$-- G (!&&1~ o& f(8'9: &' f(8'9: 89 f'TUr&ct}|jt|jd}|j d|j t}|jd|j |ddg|jd|j |ddg|jd|j |dddgy) z. Specify host key algorithms. z invalid-keyzUnknown host key typezssh-rsazhost-key-algorithmssssh-rsasssh-dsszssh-rsa,ssh-dssN)rrropt_host_key_algorithmsrrr,rs r$test_host_key_algorithmsz,ConchOptionsParsing.test_host_key_algorithms9s~   j$*F*F V -qvv6~ $$Y/ 34zlC $$Z0 34zlC $$%67 34z:6NOr&N)rirjrkrrrrgr&r$rr%s V Pr&r)'rrunittestrtwisted.conch.errorrtwisted.conch.testrtwisted.internet.testingrtwisted.python.compatrtwisted.python.filepathrtwisted.python.reflectr twisted.python.runtimer twisted.trial.unittestr rrtwisted.conch.clientrtwisted.conch.client.agentrtwisted.conch.client.defaultrtwisted.conch.client.optionsrtwisted.conch.ssh.keysrskip isWindowsrisattyrrrrgr&r$rs  *&4.,0++    ,9>9* FJJ D8 F ?  yy F6J zz F7JpXpf!P(!Pr&