ϪfdZddlmZmZmZddlmZddlmZddl m Z m Z ddl m Z mZmZddlmZddlmZdd lmZed Zed Zerer dd lmZdd lmZGddeZy)z! Tests for L{twisted.conch.tap}. )AnyTupleUnion)StreamServerEndpointService)error)FilePasswordDBICredentialsChecker)ISSHPrivateKeyIUsernamePasswordUsernamePassword)Deferred) requireModule)TestCase cryptographyztwisted.conch.unix)tap)OpenSSHFactorycreZdZdZesdZesdZdZddZddZ dd Z dd Z dd Z de fd Zdedfd ZddZy)MakeServiceTestsz' Tests for L{tap.makeService}. zcan't run without cryptographyz can't run on non-posix computers)siamusersthisispasswordreturnNc |j|_t|jd5}|jdj |j dddt j|_y#1swY#xYw)z/ Create a file with two users. zwb+:N) mktempfilenameopenwritejoinusernamePasswordrOptionsoptions)selffs =/usr/lib/python3/dist-packages/twisted/conch/test/test_tap.pysetUpzMakeServiceTests.setUp(s^  $-- ' 61 GGDIId334 5 6{{}  6 6s +A99Bctj}tj|}|j|t|j |j jd|j|jty)z L{tap.makeService} returns a L{StreamServerEndpointService} instance running on TCP port 22, and the linked protocol factory is an instance of L{OpenSSHFactory}. N) rr makeServiceassertIsInstancer assertEqualendpoint_portfactoryr)r configservices r" test_basiczMakeServiceTests.test_basic1s^ //&) g'BC ))//4 goo~>cd}|jt|jdd|jt|jdd|j |t |jdd|fzy)z Make sure that if the C{--auth} command-line option is not passed, the default checkers are (for backwards compatibility): SSH and UNIX credInterfacesz)SSH should be one of the default checkersz*UNIX should be one of the default checkers credCheckersz&There should be %d checkers by defaultN)assertInr rr r(len)r numCheckerss r"test_defaultAuthsz"MakeServiceTests.test_defaultAuths=sw    LL) * 7  LL) * 8    ^, - 4 ~ E r/c|jjdd|jzg|jt |jddy)z The C{--auth} command-line option will add a checker to the list of checkers, and it should be the only auth checker --authfile:r3Nr parseOptionsrr(r5r s r"test_authAddedzMakeServiceTests.test_authAddedTsB !!8Wt}}-D"EF T\\.9:A>r/c|jjdd|jzddg|jt |jddy)z Multiple C{--auth} command-line options will add all checkers specified to the list ofcheckers, and there should only be the specified auth checkers (no default checkers). r9r:zmemory:testuser:testpasswordr3r1Nr<r>s r"test_multipleAuthAddedz'MakeServiceTests.test_multipleAuthAdded\sP !!$--'.    T\\.9:A>r/c6|jjdd|jzg|jdd}|j|tt |j dd}|j|j|tjS)z The checker created by the C{--auth} command-line option returns a L{Deferred} that fails with L{UnauthorizedLogin} when presented with credentials that are unknown to that checker. r9r:r3rsfake) rr=rr'rr r assertFailurerequestAvatarIdrUnauthorizedLogin)r checkerinvalids r"test_authFailurez!MakeServiceTests.test_authFailurels !!8Wt}}-D"EF"&,,~">r"B g~6"4#8#8#;WE!!  # #G ,e.E.E  r/c$jjddjzgjdd}tj|j }dt ttdfddffd }|j|S) z The checker created by the C{--auth} command-line option returns a L{Deferred} that returns the avatar id when presented with credentials that are known to that checker. r9r:r3rCusernamerNc>j|jy)N)r(rK)rKcorrectr s r" checkSuccessz7MakeServiceTests.test_authSuccess..checkSuccesss   Xw'7'7 8r/) rr=rr rrErbytesr addCallback)r rGdrOrNs` @r"test_authSuccessz!MakeServiceTests.test_authSuccess{s !!8Wt}}-D"EF'+||N'CB'G"D$9$9:  # #G , 95b )9#: 9t 9}}\**r/ctj}tj|}|jj}|j t |jjtthy)z The L{OpenSSHFactory} built by L{tap.makeService} has a portal with L{ISSHPrivateKey} and L{IUsernamePassword} interfaces registered as checkers. N) rrr&r+portalr(setcheckerskeysr r )r r,r-rUs r" test_checkerszMakeServiceTests.test_checkerssW //&)''  $$& '.:K)L r/)rN)__name__ __module__ __qualname____doc__rskipunixrr#r.r7r?rArrIr rSrYrLr/r"rrsY / 16% ? .??  #  +(4.+  r/rN)r]typingrrrtwisted.application.internetr twisted.credrtwisted.cred.checkersrr twisted.cred.credentialsr r r twisted.internet.deferr twisted.python.reflectrtwisted.trial.unittestrrr_ twisted.conchr$twisted.conch.openssh_compat.factoryrrrLr/r"rjsZ%$DEXX+0+^, )*D!C{ x{ r/