+
    Dfjv}                        R t ^ RIt^ RIt^ RIt^ RIHt ^ RIHtHt ^ RI	H
t
 ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHt  ^ RIHt ]t ^ RIHt ]t ^ RIt]t ^ RI H!t" ]"t! ^ RI$H%t%H&t&H't'H(t(  ! R R]!4      t) ! R R])4      t* ! R R4      t+]f   ^ RI,t,],PZ                  ! R]: R24       M) ! R R]4      t.]P^                  ! ].])]'P`                  4        ! R R]4      t1]! ]'       * R4       ! R R]14      4       t2 ! R R4      t3]! ]'       * R4       ! R  R!]4      4       t4]! ]'       * R"4       ! R# R$]4      4       t5]! ]'       * R4      ]! ]'       * R"4       ! R% R&]4      4       4       t6]! ]'       * R"4       ! R' R(]4      4       t7R#   ] d    Rt ELOi ; i  ] d    Rt ELVi ; i  ] d    Rt EL_i ; i  ] d    ]#t! ELfi ; i  ] d     ELgi ; i))z.
Tests for L{twisted.conch.ssh.filetransfer}.
N)skipIf)assert_thatequal_to)defer)ConnectionLost)StringTransport)loopback)
components)FilePath)TestCase)unix)SFTPServerForUnixConchUser)	ConchUser)common
connectionfiletransfersessionc                   ,   a  ] tR t^=t o R tR tRtV tR# )
TestAvatarc                    \         P                  V 4       \        P                  V P                  R &   \
        P                  V P                  R&   R# )   session   sftpN)r   __init__r   
SSHSessionchannelLookupr   FileTransferServersubsystemLookupselfs   &F/usr/lib/python3/dist-packages/twisted/conch/test/test_filetransfer.pyr   TestAvatar.__init__>   s<    4 )0););:&(4(G(GW%    c                "    \        V4      pV Ff  pV^ ,          p\        V4      ^8  ;'       d    V^,          ;'       g    Rp\        V4      ^8  ;'       d    V^,          ;'       g    / pV! V/ VB pKh  	  X#   \         d	    YT3.p Li ; i)     )iter	TypeErrorlen)r   fargskwifuncrs   &&*,   r   
_runAsUserTestAvatar._runAsUserC   s    	 QA AQ4Dq6A:&&!A$,,"DQ!$$!**Bd!b!A	 
   	 2A	 s   A; ;BBr$   N)__name__
__module____qualname____firstlineno__r   r.   __static_attributes____classdictcell____classdict__s   @r   r   r   =   s     H

 
r!   r   c                   ,   a  ] tR t^Pt o R tR tRtV tR# )FileTransferTestAvatarc                <    \         P                  V 4       Wn        R # N)r   r   homeDir)r   r<   s   &&r   r   FileTransferTestAvatar.__init__Q   s    D!r!   c                    \        \        P                  ! 4       4      P                  V P                  P
                  4      # r;   )r
   osgetcwdpreauthChildr<   pathr   s   &r   
getHomeDir!FileTransferTestAvatar.getHomeDirU   s(    		$11$,,2C2CDDr!   )r<   N)r0   r1   r2   r3   r   rC   r4   r5   r6   s   @r   r9   r9   P   s     E Er!   r9   c                   &   a  ] tR t^Yt o R tRtV tR# )ConchSessionForTestAvatarc                    Wn         R # r;   avatar)r   rI   s   &&r   r   "ConchSessionForTestAvatar.__init__Z   s    r!   rH   N)r0   r1   r2   r3   r   r4   r5   r6   s   @r   rF   rF   Y   s      r!   rF   ztwisted.conch.unix imported z0, but doesn't define SFTPServerForUnixConchUser'c                   ,   a  ] tR t^lt o R tR tRtV tR# )FileTransferForTestAvatarc                
    R R/# )	   conchTest   ext datar$   )r   versionotherExts   &&&r   
gotVersion$FileTransferForTestAvatar.gotVersionm   s     +..r!   c                     VR 8X  d   R# \         h)   testExtendedRequest   bar)NotImplementedError)r   extNameextDatas   &&&r   extendedRequest)FileTransferForTestAvatar.extendedRequestp   s    00%%r!   r$   N)r0   r1   r2   r3   rR   rZ   r4   r5   r6   s   @r   rL   rL   l   s     	/	& 	&r!   rL   c                   &   a  ] tR t^zt o R tRtV tR# )SFTPTestBasec           	        \        V P                  4       4      V n        V P                  P                  R 4      V n        V P                  P                  R4      P	                  R4       V P                  P                  R4      P                  RR7      ;_uu_ 4       pVP                  R4       \        RR4      ;_uu_ 4       pVP                  VP                  R4      4       R	R	R	4       R	R	R	4       V P                  P                  R4      P                  R
4       V P                  P                  R4      P                  RR7      ;_uu_ 4       pVP                  R4       R	R	R	4       V P                  P                  R4      P                  RR7      ;_uu_ 4       pVP                  R4       R	R	R	4       V P                  P                  R4      P                  RR7      ;_uu_ 4       pVP                  R4       R	R	R	4       R	#   + '       g   i     EL,; i  + '       g   i     EL8; i  + '       g   i     L; i  + '       g   i     L; i  + '       g   i     R	# ; i)extratestDirectoryT	testfile1wb)mode   az/dev/urandomrbNi  testRemoveFiletestRenameFilez.testHiddenFile   aaaaaaaaaabbbbbbbbbbi   )	r
   mktemptestDirchildmakedirsopenwritereadchmod)r   r(   f2s   &  r   setUpSFTPTestBase.setUp{   s   . ||))'2?+44T:\\,11t1<<GG)*nd++r	*+ , = 	;'--e4\\ 0166D6AAQGGDM B\\ 0166D6AAQGGDM B\\ 1277T7BBaGGDM CB ,++ =<<
 BAAABBBsH   &H!H	#HH.I*IH	HH+	.H>	I	I%	)rj   N)r0   r1   r2   r3   rr   r4   r5   r6   s   @r   r]   r]   z   s      r!   r]   z can't run on non-posix computersc                      a  ] tR t^t o R tR tR tR tR tR t	R t
R tR	 tR
 tR tR tR tR tR tR tR tR tR tR tR tR t]P6                  R 4       t]P6                  R 4       tRtV tR# )OurServerOurClientTestsc                X  a  \         P                  S 4       \        S P                  4      S n        \
        P                  ! S P                  R 7      S n        \        P                  ! S P                  4      p\
        P                  ! 4       S n        RS n        RS n        V 3R lpVS P                  n        \        P                  ! S P                  4      pS P                  P                  V4       S P                  P                  V4       VS n        VS n        S P%                  4        R# )rH   Nc                 $   < V Sn         VSn        R # r;   )_serverVersion_extData)serverVersionrY   r   s   &&r   _(OurServerOurClientTests.setUp.<locals>._   s    "/D#DMr!   )r]   rr   r9   rj   rI   r   r   serverr   LoopbackRelayFileTransferClientclientrx   ry   gotServerVersionmakeConnectionclientTransportserverTransport_emptyBuffers)r   r   r{   r   s   f   r   rr   OurServerOurClientTests.setUp   s    4 ,T\\:"55T[[I"00="557"	$ ()$"00=""?3""?3..r!   c                    V P                   P                  '       g   V P                  P                  '       d7   V P                   P                  4        V P                  P                  4        Kn  R # r;   )r   bufferr   clearBufferr   s   &r   r   %OurServerOurClientTests._emptyBuffers   sO    "")))T-A-A-H-H-H  ,,.  ,,. .Ir!   c                    V P                   P                  4        V P                  P                  4        V P                   P                  4        V P                  P                  4        R # r;   )r   loseConnectionr   r   r   s   &r   tearDown OurServerOurClientTests.tearDown   sJ    ++-++-((*((*r!   c                z    V P                  V P                  ^4       V P                  V P                  RR/4       R# )   rN   rO   N)assertEqualrx   ry   r   s   &r   test_serverVersion*OurServerOurClientTests.test_serverVersion   s1    ,,a0{(CDr!   c                    V P                  \        P                  P                  V P                  P
                  4      RV P                  P
                  : 24       R# )z*
It implements the ISFTPServer interface.
zISFTPServer not provided by N)
assertTruer   ISFTPServer
providedByr}   r   r   s   &r   test_interface_implementation5OurServerOurClientTests.test_interface_implementation   sC     	$$//0B0BC*4;;+=+=*@A	
r!   c                <  a aa S P                   P                  R\        P                  \        P                  ,          / 4      pS P                  4        \        P                  o. oVV3R lpS P                  \        RV4       VV 3R lpVP                  V4       V# )zG
A file opened with C{openFile} is closed when the connection is lost.
	   testfile1c                 :   < SP                  V 4       S! V 4       R # r;   append)fdclosedoldCloses   &r   closeJOurServerOurClientTests.test_openedFileClosedWithConnection.<locals>.close   s    MM"RLr!   r   c                   < SP                   P                  V P                  R ,          ,          P                  pSP                  P                  4        SP                  P                  4        SP                  P                  4        SP                  P                  4        SP                  SP                   P                  / 4       SP                  VS4       R# ):   NNN)
r}   	openFileshandler   r   r   r   r   r   assertIn)openFiler   r   r   s   & r   _fileOpenedPOurServerOurClientTests.test_openedFileClosedWithConnection.<locals>._fileOpened   s    &&xr':;>>B  //1  //1  ,,.  ,,.T[[22B7MM"f%r!   )
r   r   r   FXF_READ	FXF_WRITEr   r?   r   patchaddCallback)r   dr   r   r   r   s   f   @@r   #test_openedFileClosedWithConnection;OurServerOurClientTests.test_openedFileClosedWithConnection   s|     KK  ,//,2H2HH"
 	88	 	

2w&	& 	
k"r!   c                   a  S P                   P                  R4      pS P                  4        V 3R lpVP                  V4       V# )zP
A directory opened with C{openDirectory} is close when the connection
is lost.
 c                 $  < SP                   P                  4        SP                  P                  4        SP                   P                  4        SP                  P                  4        SP	                  SP
                  P                  / 4       R # r;   )r   r   r   r   r   r}   openDirs)openDirr   s   &r   	_getFilesSOurServerOurClientTests.test_openedDirectoryClosedWithConnection.<locals>._getFiles   se      //1  //1  ,,.  ,,.T[[1126r!   r   openDirectoryr   r   )r   r   r   s   f  r   (test_openedDirectoryClosedWithConnection@OurServerOurClientTests.test_openedDirectoryClosedWithConnection   s=    
 KK%%b)	7 	
i r!   c                  a aaa S P                   P                  R \        P                  \        P                  ,          / 4      pS P                  4        VVV 3R lpV 3R loVV 3R loV 3R loVP                  V4       V# )r   c                    < SP                  V \        P                  ! V 4      4       S! V 4      pVP                  SV 4       V# r;   )r   r   	ISFTPFiler   )r   r   
_readChunk_writeChunkr   s   & r   r   <OurServerOurClientTests.test_openFileIO.<locals>._fileOpened   s;    X|'='=h'GH8$AMM+x0Hr!   c                    < V P                  ^ ^4      pSP                  4        VP                  SP                  R4       V# )r#   rh   	readChunkr   r   r   )r   r   r   s   & r   r   ;OurServerOurClientTests.test_openFileIO.<locals>._readChunk   s;    ""1b)A MM$**,ABHr!   c                 p   < VP                  ^R4      pSP                  4        VP                  SV4       V# )   s
   cccccccccc)
writeChunkr   r   )r{   r   r   _readChunk2r   s   && r   r   <OurServerOurClientTests.test_openFileIO.<locals>._writeChunk  s4    ##B	2A MM+x0Hr!   c                    < VP                  ^ ^4      pSP                  4        VP                  SP                  R4       V# )r#   s   aaaaaaaaaabbbbbbbbbbccccccccccr   r{   r   r   r   s   && r   r   <OurServerOurClientTests.test_openFileIO.<locals>._readChunk2
  s;    ""1b)A MM$**,MNHr!   r   r   r   r   r   r   r   )r   r   r   r   r   r   s   f  @@@r   test_openFileIO'OurServerOurClientTests.test_openFileIO   sd    KK  ,//,2H2HH"
 					 	
k"r!   c                   a aa S P                   P                  R \        P                  \        P                  ,          / 4      pS P                  4        V 3R loV 3R loVVV 3R lpVP                  V4       V# )r   c                 H   < VP                  4       pSP                  4        V# r;   )getAttrsr   r   s   && r   	_getAttrsBOurServerOurClientTests.test_closedFileGetAttrs.<locals>._getAttrs  s"    !!#A Hr!   c                 (   < SP                  4        V # r;   )flushLoggedErrors)r(   r   s   &r   _err=OurServerOurClientTests.test_closedFileGetAttrs.<locals>._err  s    ""$Hr!   c                    < V P                  4       pSP                  4        VP                  SV 4       VP                  S4       SP	                  V\
        P                  4      # r;   )r   r   r   
addErrbackassertFailurer   	SFTPError)r   r   r   r   r   s   & r   _close?OurServerOurClientTests.test_closedFileGetAttrs.<locals>._close"  sN     A MM)X.LL%%a)?)?@@r!   r   )r   r   r   r   r   s   f  @@r   test_closedFileGetAttrs/OurServerOurClientTests.test_closedFileGetAttrs  s`    KK  ,//,2H2HH"
 		
		A 	
fr!   c                   a a S P                   P                  R \        P                  \        P                  ,          / 4      pS P                  4        VV 3R lpV 3R loVP                  V4      # )r   c                 j   < V P                  4       pSP                  4        VP                  S4       V# r;   r   r   r   )r   r   
_getAttrs2r   s   & r   r   BOurServerOurClientTests.test_openFileAttributes.<locals>._getAttrs2  s.    !!#A MM*%Hr!   c                    < SP                   P                  R 4      pSP                  4        VP                  SP                  V 4       V# r   r   r   r   r   r   )attrs1r   r   s   & r   r   COurServerOurClientTests.test_openFileAttributes.<locals>._getAttrs28  s<    $$\2A MM$**F3Hr!   r   )r   r   r   r   s   f  @r   test_openFileAttributes/OurServerOurClientTests.test_openFileAttributes,  sX    KK  ,//,2H2HH"
 			 }}Y''r!   c                   a aa S P                   P                  R \        P                  \        P                  ,          / 4      pS P                  4        VV 3R lpVV 3R loV 3R loVP                  V4       V# )r   c                 j   < V P                  4       pSP                  4        VP                  S4       V# r;   r   )r   r   	_setAttrsr   s   & r   r   @OurServerOurClientTests.test_openFileSetAttrs.<locals>._getAttrsH  s.    !!#A MM)$Hr!   c                    < ^ V R&   SP                   P                  RV 4      pSP                  4        VP                  S4       VP                  SP                  V 4       V# )r#   atimer   )r   setAttrsr   r   r   )attrsr   r   r   s   & r   r   @OurServerOurClientTests.test_openFileSetAttrs.<locals>._setAttrsN  sS    E'N$$\59A MM*%MM$**E2Hr!   c                 ^   < SP                   P                  R 4      pSP                  4        V# r   r   r   r   r{   r   r   s   & r   r   AOurServerOurClientTests.test_openFileSetAttrs.<locals>._getAttrs2V  s(    $$\2A Hr!   r   )r   r   r   r   r   s   f  @@r   test_openFileSetAttrs-OurServerOurClientTests.test_openFileSetAttrs@  sa     KK  ,//,2H2HH"
 				
 	
i r!   c                d  a aa / oS P                   P                  P                  oVV3R lpVS P                   P                  n        S P                  P                  R\        P                  \        P
                  ,          RR/4      pS P                  4        VV 3R lpVP                  V4      # )z
Check that L{filetransfer.FileTransferClient.openFile} can send
extended attributes, that should be extracted server side. By default,
they are ignored, so we just verify they are correctly parsed.
c                 8   < SP                  V4       S! WV4      # r;   )update)filenameflagsr   oldOpenFilesavedAttributess   &&&r   r   IOurServerOurClientTests.test_openFileExtendedAttributes.<locals>.openFileg  s    ""5)x66r!   r   ext_foorV   c                 0   < SP                  SR R/4       R# )r  rV   Nr   )ignr  r   s   &r   checkFOurServerOurClientTests.test_openFileExtendedAttributes.<locals>.checkt  s    _y&.ABr!   )r}   r   r   r   r   r   r   r   )r   r   r   r  r  r  s   f   @@r   test_openFileExtendedAttributes7OurServerOurClientTests.test_openFileExtendedAttributes^  s     kk((11	7 '/#KK  !!L$:$::

 		C }}U##r!   c                   a  S P                   P                  R 4      pS P                  4        V 3R lpVP                  V4       VP                  V4       S P	                  V\
        P                  4      # )   testRemoveFilec                 ^   < SP                   P                  R 4      pSP                  4        V# )r  )r   
removeFiler   )ignoredr   r   s   & r   _removeFile<OurServerOurClientTests.test_removeFile.<locals>._removeFile}  s)    &&'89A Hr!   )r   r   r   r   r   r   r   )r   r   r  s   f  r   test_removeFile'OurServerOurClientTests.test_removeFiley  s\    KK  !23	
 	
k"	k"!!!\%;%;<<r!   c                   a a S P                   P                  R 4      pS P                  4        VV 3R lpV 3R loVP                  V4      # )   testRenameFilec                    < SP                   P                  R R4      pSP                  4        VP                  SV 4       V# )r     testRenamedFile)r   
renameFiler   r   )r   r   _testRenamedr   s   & r   _rename8OurServerOurClientTests.test_renameFile.<locals>._rename  s:    &&'8:LMA MM,.Hr!   c                    < SP                   P                  R 4      pSP                  4        VP                  SP                  V4       R# )r  Nr   )r{   r   r   r   s   && r   r  =OurServerOurClientTests.test_renameFile.<locals>._testRenamed  s8    $$%78A MM$**E2r!   )r   r   r   r   )r   r   r  r  s   f  @r   test_renameFile'OurServerOurClientTests.test_renameFile  s?    KK  !23		3
 }}W%%r!   c                    V P                   P                  R 4      pV P                  4        V P                  V\        P
                  4      #    testMakeDirectoryr   r   r   r   r   r   r   r   s   & r   test_directoryBad)OurServerOurClientTests.test_directoryBad  s<    KK  !56!!!\%;%;<<r!   c                  a  S P                   P                  R / 4      pS P                  4        V 3R lpV 3R lpVP                  V4       VP                  V4       VP                  V4       S P	                  V\
        P                  4      # )r&  c                 ^   < SP                   P                  R 4      pSP                  4        V# r%  r   r   s   & r   r   AOurServerOurClientTests.test_directoryCreation.<locals>._getAttrs  s)    $$%9:A Hr!   c                 ^   < SP                   P                  R 4      pSP                  4        V# r%  )r   removeDirectoryr   r   s   & r   _removeDirectoryHOurServerOurClientTests.test_directoryCreation.<locals>._removeDirectory  s)    ++,@AA Hr!   )r   makeDirectoryr   r   r   r   r   )r   r   r   r0  s   f   r   test_directoryCreation.OurServerOurClientTests.test_directoryCreation  sp    KK%%&:B?		
 	
i 	&'	i !!!\%;%;<<r!   c                   a aaa S P                   P                  R 4      pS P                  4        . oVVVV 3R loVV 3R lpV 3R loVP                  S4       VP                  V4       V# )r!   c                    <a  VV 3R  lp\         P                  ! S P                  4      pSP                  4        VP	                  V4       VP	                  S4       VP                  SS 4       V# )c                 *   < SP                  V 4       S# r;   r   )r(   filesr   s   &r   r   MOurServerOurClientTests.test_openDirectory.<locals>._getFiles.<locals>.append  s    Qr!   )r   maybeDeferrednextr   r   r   )r   r   r   r   r   r8  r   s   f  r   r   =OurServerOurClientTests.test_openDirectory.<locals>._getFiles  sU     ##GLL1A MM&!MM)$LL)Hr!   c                    < \        \        \        S!  4      ^ ,          4      pVP                  4        SP                  V. RO4       R# )r#   N)s   .testHiddenFiles   testDirectoryr  r  r   )listzipsortr   )r  fsr8  r   s   & r   _checkFiles?OurServerOurClientTests.test_openDirectory.<locals>._checkFiles  s:    d3;'*+BGGI	r!   c                 H   < VP                  4       pSP                  4        V# r;   )r   r   )r{   r   r   r   s   && r   r   :OurServerOurClientTests.test_openDirectory.<locals>._close  s    A Hr!   r   )r   r   rB  r   r   r8  s   f  @@@r   test_openDirectory*OurServerOurClientTests.test_openDirectory  s[    KK%%c*
	 
			
 	
i 	k"r!   c                    V P                   P                  R 4      pV P                  4        V P                  V\        P
                  4      #    testLinkr'  r(  s   & r   test_linkDoesntExist,OurServerOurClientTests.test_linkDoesntExist  s;    KK  -!!!\%;%;<<r!   c                   a  S P                   P                  R R4      pS P                  4        V 3R lpV 3R lpVP                  V4       VP                  V4      # )rJ  r   c                 `   < SP                   P                  R ^4      pSP                  4        V# rI  r   r   s   & r   _getFirstAttrsDOurServerOurClientTests.test_linkSharesAttrs.<locals>._getFirstAttrs  s*    $$[!4A Hr!   c                    < SP                   P                  R 4      pSP                  4        VP                  SP                  V 4       V# r   r   )
firstAttrsr   r   s   & r   _getSecondAttrsEOurServerOurClientTests.test_linkSharesAttrs.<locals>._getSecondAttrs  s<    $$\2A MM$**J7Hr!   r   makeLinkr   r   )r   r   rO  rS  s   f   r   test_linkSharesAttrs,OurServerOurClientTests.test_linkSharesAttrs  sL    KK  l;	
	 	
n%}}_--r!   c                   a  S P                   P                  R R4      pS P                  4        V 3R lpV 3R lpVP                  V4       VP                  V4       V# )rJ  r   c                 N  < SP                   P                  R 4      pSP                  4        \        \        P
                  ! 4       4      P                  SP                  P                  4      pVP                  R4      pVP                  SP                  VP                  4       V# rJ  ra   )r   readLinkr   r
   r?   r@   rA   rj   rB   rk   r   r   )r{   r   testFiler   s   &  r   	_readLink8OurServerOurClientTests.test_linkPath.<locals>._readLink  s    $$[1A 		,99$,,:K:KLH~~k2HMM$**HMM:Hr!   c                 N  < SP                   P                  R 4      pSP                  4        \        \        P
                  ! 4       4      P                  SP                  P                  4      pVP                  R4      pVP                  SP                  VP                  4       V# r[  )r   realPathr   r
   r?   r@   rA   rj   rB   rk   r   r   )r{   r   testLinkr   s   &  r   	_realPath8OurServerOurClientTests.test_linkPath.<locals>._realPath  r`  r!   rU  )r   r   r^  rd  s   f   r   test_linkPath%OurServerOurClientTests.test_linkPath  sN    KK  l;		 	
i 	i r!   c                    V P                   P                  R R4      pV P                  4        VP                  V P                  R4       VP                  V P
                  4       V# )rU   s   foorV   )r   rZ   r   r   r   _cbTestExtendedRequestr(  s   & r   test_extendedRequest,OurServerOurClientTests.test_extendedRequest
  sP    KK''(>G	d&&/	d112r!   c                    V P                   P                  R R4      pV P                  4        V P                  V\        4      # )s   testBadRequestr!   )r   rZ   r   r   rW   )r   r  r   s   && r   ri  .OurServerOurClientTests._cbTestExtendedRequest  s9    KK''(93?!!!%899r!   c              #    "   V P                   P                  R4      pV P                  4        Vx pVP                  4       pV P                  4        Vx  V P	                  4       pRpV P                  ^\        V4      4       V P                  \        V^ ,          R,          4       V P                  WT^ ,          R,          4       R# 5i)z:
Using client.openDirectory as an iterator is deprecated.
r!   zeUsing twisted.conch.ssh.filetransfer.ClientDirectory as an iterator was deprecated in Twisted 18.9.0.categorymessageN)r   r   r   r;  flushWarningsr   r'   DeprecationWarning)r   r   r   oneFilewarningsrp  s   &     r   $test_openDirectoryIteratorDeprecated<OurServerOurClientTests.test_openDirectoryIteratorDeprecated  s     
 KK%%c*',,.%%'@ 	 	CM*+Xa[-DE1+i"89s   CCc              #    a a"   S P                   P                  R\        P                  / 4      pS P	                  4        Vx p. oVV 3R lpS P
                  P                  P                  4       w  pW4n        ?VP                  ^d^4      pS P	                  4        S P                  \        S4      ^4       S P                  V4       S P                  P                  4        S P                  P                  4        S P                  P                  4        S P	                  4        S P!                  S P                   P"                  4       S P%                  V\&        4       VP)                  4       pS P%                  V\&        4       R# 5i)zc
If there are requests outstanding when the connection
is closed for any reason, they should fail.
r   c                 x   < SP                  S. 4       \        P                  ! 4       pSP                  V 4       V# r;   )r   r   Deferredr   )offsetlengthr   gotReadRequestr   s   && r   	_slowReadOOurServerOurClientTests.test_closedConnectionCancelsRequests.<locals>._slowRead9  s2    ^R0 A!!&)Hr!   N)r   r   r   r   r   r}   r   valuesr   r   r'   assertNoResultr   r   r   r   assertFalse	connectedfailureResultOfr   r   )r   r   fhr}  serverSideFhr|  s   f    @r   $test_closedConnectionCancelsRequests<OurServerOurClientTests.test_closedConnectionCancelsRequests+  s4     KK  |/D/DbIW 	 ..557!* LLc"^,a0A 	++-((*((*../Q/ KKMQ/s   FF	)ry   rx   rI   r   r   r}   r   N) r0   r1   r2   r3   rr   r   r   r   r   r   r   r   r   r   r   r  r  r"  r)  r3  rF  rK  rW  rf  rj  ri  r   inlineCallbacksru  r  r4   r5   r6   s   @r   ru   ru      s     2/
+E
<$B2((<$6=&"=
=.&P=
.$0:
 : :( *0 *0r!   ru   c                   &   a  ] tR tRt o R tRtV tR# )FakeConniY  c                    R # r;   r$   )r   channels   &&r   	sendCloseFakeConn.sendCloseZ  s    r!   r$   N)r0   r1   r2   r3   r  r4   r5   r6   s   @r   r  r  Y  s      r!   r  c                   J   a  ] tR tRt o R tR tR tR tR tR t	R t
R	tV tR
# )FileTransferCloseTestsi^  c                $    \        4       V n        R # r;   )r   rI   r   s   &r   rr   FileTransferCloseTests.setUp`  s     lr!   c                    \         P                  ! 4       p ! R  R4      pV! 4       Vn        V P                  VP                  n        V# )c                   2   a  ] tR tRt o R tR tR tRtV tR# )DFileTransferCloseTests.buildServerConnection.<locals>.DummyTransportih  c                    W n         R # r;   	transportr   s   &r   r   MFileTransferCloseTests.buildServerConnection.<locals>.DummyTransport.__init__i  s    !%r!   c                    R # r;   r$   )r   kinddatas   &&&r   
sendPacketOFileTransferCloseTests.buildServerConnection.<locals>.DummyTransport.sendPacketl  s    r!   c                    R # )zdummy transportr$   r   s   &r   	logPrefixNFileTransferCloseTests.buildServerConnection.<locals>.DummyTransport.logPrefixo  s    (r!   r  N)	r0   r1   r2   r3   r   r  r  r4   r5   r6   s   @r   DummyTransportr  h  s     &) )r!   r  )r   SSHConnectionr  rI   )r   connr  s   &  r   buildServerConnection,FileTransferCloseTests.buildServerConnectionc  s;    '')	) 	) () $r!   c                J   a a R S n         VP                  oVV 3R lpW!n        R# )Fc                 &   < R Sn         S! V 4       R# )TN)connectionLostFired)reasonorigConnectionLostr   s   &r   connectionLostFFileTransferCloseTests.interceptConnectionLost.<locals>.connectionLostz  s    '+D$v&r!   N)r  r  )r   
sftpServerr  r  s   f& @r   interceptConnectionLost.FileTransferCloseTests.interceptConnectionLostv  s$    #( '66	' %3!r!   c                >    V P                  V P                  R 4       R# )z*sftpServer's connectionLost was not calledN)r   r  r   s   &r   assertSFTPConnectionLost/FileTransferCloseTests.assertSFTPConnectionLost  s    $$&R	
r!   c                F   \         P                  ! \        4       V P                  R7      pVP	                  \
        P                  ! R4      4       VP                  P                  P                  pV P                  V4       VP                  4        V P                  4        R# )zM
Closing a session should notify an SFTP subsystem launched by that
session.
)r  rI   r   N)r   r   r  rI   request_subsystemr   NSr   r  protor  closeReceivedr  )r   testSessionr  s   &  r   test_sessionClose(FileTransferCloseTests.test_sessionClose  sv     ((hjM 	%%fii&89 ''1177
 	$$Z0 	!!#%%'r!   c                   V P                  4       p\        P                  ! R4      \        P                  ! R^ 4      ^,          ,           pVP                  V4       VP                  ^ ,          pVP                  \        P                  ! R4      4       VP                  P                  P                  pV P                  V4       V P                  V4       VP                  \        P                  ! R^ 4      4       V P                  4        R# )za
A client sending CHANNEL_CLOSE should trigger closeReceived on the
associated channel instance.
r   >Lr   N)r  r   r  structpackssh_CHANNEL_OPENchannelsr  r   r  r  r  ssh_CHANNEL_CLOSEr  r   r  packetsessionChannelr  s   &    r   %test_clientClosesChannelOnConnnection<FileTransferCloseTests.test_clientClosesChannelOnConnnection  s    
 ))+ :&T1)=)AAf%q)((7);<#**44::
$$Z0 	$$Z0 	v{{434%%'r!   c                   V P                  4       p\        P                  ! R4      \        P                  ! R^ 4      ^,          ,           pVP                  V4       VP                  ^ ,          pVP                  \        P                  ! R4      4       VP                  P                  P                  pV P                  V4       VP                  4        V P                  4        R# )z@
Closing an SSH connection should close all sessions within it.
r   r  r   N)r  r   r  r  r  r  r  r  r   r  r  r  serviceStoppedr  r  s   &    r   'test_stopConnectionServiceClosesChannel>FileTransferCloseTests.test_stopConnectionServiceClosesChannel  s     ))+ :&T1)=)AAf%q)((7);<#**44::
$$Z0 	%%'r!   )rI   r  N)r0   r1   r2   r3   rr   r  r  r  r  r  r  r4   r5   r6   s   @r   r  r  ^  s-     #&3

(((0( (r!   r  zCannot run without cryptographyc                   2   a  ] tR tRt o Rt. ROtR tRtV tR# )ConstantsTestsi  aO  
Tests for the constants used by the SFTP protocol implementation.

@ivar filexferSpecExcerpts: Excerpts from the
    draft-ietf-secsh-filexfer-02.txt (draft) specification of the SFTP
    protocol.  There are more recent drafts of the specification, but this
    one describes version 3, which is what conch (and OpenSSH) implements.
c                   / pV P                    Fg  pVP                  4        FP  p\        P                  ! RV4      pV'       g   K$  \	        VP                  ^4      ^ 4      WP                  ^4      &   KR  	  Ki  	  V P                  \        V4      ^ 8  R4       VP                  4        F%  w  rVV P                  V\        \        V4      4       K'  	  R# )zi
The constants used by the SFTP protocol implementation match those
found by searching through the spec.
z)^\s*#define SSH_([A-Z_]+)\s+([0-9x]*)\s*$z,No constants found (the test must be buggy).N)filexferSpecExcerpts
splitlinesrematchintgroupr   r'   itemsr   getattrr   )r   	constantsexcerptlinemkvs   &      r   test_constantsAgainstSpec(ConstantsTests.test_constantsAgainstSpec  s    
 	00G**,HHI4P1,/
A,>Iggaj) - 1
 		NQ N	
 OO%DAQa 89 &r!   r$   N)a  
           The following values are defined for packet types.

                #define SSH_FXP_INIT                1
                #define SSH_FXP_VERSION             2
                #define SSH_FXP_OPEN                3
                #define SSH_FXP_CLOSE               4
                #define SSH_FXP_READ                5
                #define SSH_FXP_WRITE               6
                #define SSH_FXP_LSTAT               7
                #define SSH_FXP_FSTAT               8
                #define SSH_FXP_SETSTAT             9
                #define SSH_FXP_FSETSTAT           10
                #define SSH_FXP_OPENDIR            11
                #define SSH_FXP_READDIR            12
                #define SSH_FXP_REMOVE             13
                #define SSH_FXP_MKDIR              14
                #define SSH_FXP_RMDIR              15
                #define SSH_FXP_REALPATH           16
                #define SSH_FXP_STAT               17
                #define SSH_FXP_RENAME             18
                #define SSH_FXP_READLINK           19
                #define SSH_FXP_SYMLINK            20
                #define SSH_FXP_STATUS            101
                #define SSH_FXP_HANDLE            102
                #define SSH_FXP_DATA              103
                #define SSH_FXP_NAME              104
                #define SSH_FXP_ATTRS             105
                #define SSH_FXP_EXTENDED          200
                #define SSH_FXP_EXTENDED_REPLY    201

           Additional packet types should only be defined if the protocol
           version number (see Section ``Protocol Initialization'') is
           incremented, and their use MUST be negotiated using the version
           number.  However, the SSH_FXP_EXTENDED and SSH_FXP_EXTENDED_REPLY
           packets can be used to implement vendor-specific extensions.  See
           Section ``Vendor-Specific-Extensions'' for more details.
        a  
            The flags bits are defined to have the following values:

                #define SSH_FILEXFER_ATTR_SIZE          0x00000001
                #define SSH_FILEXFER_ATTR_UIDGID        0x00000002
                #define SSH_FILEXFER_ATTR_PERMISSIONS   0x00000004
                #define SSH_FILEXFER_ATTR_ACMODTIME     0x00000008
                #define SSH_FILEXFER_ATTR_EXTENDED      0x80000000

        a  
            The `pflags' field is a bitmask.  The following bits have been
           defined.

                #define SSH_FXF_READ            0x00000001
                #define SSH_FXF_WRITE           0x00000002
                #define SSH_FXF_APPEND          0x00000004
                #define SSH_FXF_CREAT           0x00000008
                #define SSH_FXF_TRUNC           0x00000010
                #define SSH_FXF_EXCL            0x00000020
        a  
            Currently, the following values are defined (other values may be
           defined by future versions of this protocol):

                #define SSH_FX_OK                            0
                #define SSH_FX_EOF                           1
                #define SSH_FX_NO_SUCH_FILE                  2
                #define SSH_FX_PERMISSION_DENIED             3
                #define SSH_FX_FAILURE                       4
                #define SSH_FX_BAD_MESSAGE                   5
                #define SSH_FX_NO_CONNECTION                 6
                #define SSH_FX_CONNECTION_LOST               7
                #define SSH_FX_OP_UNSUPPORTED                8
        )	r0   r1   r2   r3   __doc__r  r  r4   r5   r6   s   @r   r  r    s     JX: :r!   r  c                   0   a  ] tR tRt o RtR tR tRtV tR# )RawPacketDataServerTestsi1  z
Tests for L{filetransfer.FileTransferServer} which explicitly craft
certain less common situations to exercise their handling.
c                N    \         P                  ! \        4       R 7      V n        R# )rH   N)r   r   r   ftsr   s   &r   rr   RawPacketDataServerTests.setUp9  s    22*,Gr!   c                6   \        4       pV P                  P                  V4       RpRp\        P                  ! \        ^.4      V,           \        P                  ! V4      ,           4      pV P                  P                  V4       \        P                  ! \        ^e.4      V,           \        . RO4      ,           \        P                  ! R4      ,           \        P                  ! R4      ,           4      p\        VP                  4       \        V4      4       R# )zT
A close request with an unknown handle receives an FX_NO_SUCH_FILE error
response.
s   1234s   invalid handles   No such file or directoryr!   N)r#   r#   r#      )
r   r  r   r   r  bytesdataReceivedr   valuer   )r   r  	requestIdr   r   expecteds   &     r   test_closeInvalidHandle0RawPacketDataServerTests.test_closeInvalidHandle<  s    
 $%		* 	" 		 1#Jii 
 	e$ 99 3%L  ,  II23	4 IIcN
& 	OOX	
r!   )r  N)	r0   r1   r2   r3   r  rr   r  r4   r5   r6   s   @r   r  r  1  s     
H1
 1
r!   r  c                   N   a  ] tR tRt o RtR tR tR tR tR t	R t
R	 tR
tV tR# )RawPacketDataTestsip  z
Tests for L{filetransfer.FileTransferClient} which explicitly craft certain
less common protocol messages to exercise their handling.
c                :    \         P                  ! 4       V n        R # r;   )r   r   ftcr   s   &r   rr   RawPacketDataTests.setUpw  s    224r!   c                   \         P                  ! 4       pVP                  V P                  4       WP                  P
                  ^&   \        P                  ! R^\        P                  4      \        P                  ! R4      ,           \        P                  ! R4      ,           pV P                  P                  V4       V# )a  
A STATUS packet containing a result code, a message, and a language is
parsed to produce the result of an outstanding request L{Deferred}.

@see: U{section 9.1<http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.1>}
    of the SFTP Internet-Draft.
!LL   msg   lang)r   ry  r   _cbTestPacketSTATUSr  openRequestsr  r  r   FX_OKr   r  packet_STATUSr   r   r  s   &  r   test_packetSTATUS$RawPacketDataTests.test_packetSTATUSz  s     NN	d../#$a KKq,"4"45ii ii ! 	
 	t$r!   c                j    V P                  V^ ,          R4       V P                  V^,          R4       R# )zc
Assert that the result is a two-tuple containing the message and
language from the STATUS packet.
r  r  Nr	  r   results   &&r   r  &RawPacketDataTests._cbTestPacketSTATUS  s,    
 	F+G,r!   c                   \         P                  ! 4       pVP                  V P                  4       WP                  P
                  ^&   \        P                  ! R^\        P                  4      pV P                  P                  V4       V# )aJ  
A STATUS packet containing only a result code can also be parsed to
produce the result of an outstanding request L{Deferred}.  Such packets
are sent by some SFTP implementations, though not strictly legal.

@see: U{section 9.1<http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.1>}
    of the SFTP Internet-Draft.
r  )r   ry  r   _cbTestPacketSTATUSShortr  r  r  r  r   r  r  r  s   &  r   test_packetSTATUSShort)RawPacketDataTests.test_packetSTATUSShort  sc     NN	d334#$a {{5!\%7%78t$r!   c                j    V P                  V^ ,          R4       V P                  V^,          R4       R# )z
Assert that the result is a two-tuple containing empty strings, since
the STATUS packet had neither a message nor a language.
r!   Nr	  r  s   &&r   r  +RawPacketDataTests._cbTestPacketSTATUSShort  s,    
 	C(C(r!   c                N   \         P                  ! 4       pVP                  V P                  4       WP                  P
                  ^&   \        P                  ! R^\        P                  4      \        P                  ! R4      ,           pV P                  P                  V4       V# )ac  
A STATUS packet containing a result code and a message but no language
can also be parsed to produce the result of an outstanding request
L{Deferred}.  Such packets are sent by some SFTP implementations, though
not strictly legal.

@see: U{section 9.1<http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.1>}
    of the SFTP Internet-Draft.
r  r  )r   ry  r   _cbTestPacketSTATUSWithoutLangr  r  r  r  r   r  r   r  r  r  s   &  r   test_packetSTATUSWithoutLang/RawPacketDataTests.test_packetSTATUSWithoutLang  sq     NN	d99:#$a {{5!\%7%78699V;LLt$r!   c                j    V P                  V^ ,          R4       V P                  V^,          R4       R# )z
Assert that the result is a two-tuple containing the message from the
STATUS packet and an empty string, since the language was missing.
r  r!   Nr	  r  s   &&r   r	  1RawPacketDataTests._cbTestPacketSTATUSWithoutLang  s,    
 	F+C(r!   )r  N)r0   r1   r2   r3   r  rr   r  r  r  r  r
  r	  r4   r5   r6   s   @r   r  r  p  s2     
5&- )") )r!   r  )8r  r?   r  r  unittestr   hamcrestr   r   twisted.internetr   twisted.internet.errorr   twisted.internet.testingr   twisted.protocolsr   twisted.pythonr	   twisted.python.filepathr
   twisted.trial.unittestr   twisted.conchr   _unixImportErrortwisted.conch.unixr   _SFTPServerForUnixConchUsercryptography_cryptographytwisted.conch.avatarr   
_ConchUserobjecttwisted.conch.sshr   r   r   r   r   r9   rF   rt  warnrL   registerAdapterr   r]   ru   r  r  r  r  r  r$   r!   r   <module>r$     sI  

 
 	   * " 1 4 & % , ++ D= "=!( !L< I	KK
 &EZ E 
 % MM
 		&$> & !:|/G/G
8 * D45F0l F0 6F0R 
 D45d(X d( 6d(N L;<e:X e: =e:T D45L;<:
x :
 = 6:
z L;<S) S) =S)m  D  &!%&  L  I  		sZ   F F# F3 !G *G 	F F #	F0/F03	G ?G 	GGGG