
    Vhv}                     v   d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ 	 ddlmZ eZ	 ddlmZ eZ	 ddlZeZ	 ddl m!Z" e"Z!	 ddl$m%Z%m&Z&m'Z'm(Z(  G d de!      Z) G d de)      Z* G d d      Z+eddl,Z, e,jZ                  ded       n) G d de      Z. ej^                  e.e)e'j`                          G d de      Z1 ee d       G d de1             Z2 G d d       Z3 ee d       G d! d"e             Z4 ee d#       G d$ d%e             Z5 ee d       ee d#       G d& d'e                    Z6 ee d#       G d( d)e             Z7y# e$ r dZY 1w xY w# e$ r dZY 6w xY w# e$ r dZY =w xY w# e$ r e#Z!Y Bw xY w# e$ r Y Aw xY w)*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                       e Zd Zd Zd Zy)
TestAvatarc                     t         j                  |        t        j                  | j                  d<   t
        j                  | j                  d<   y )N   session   sftp)r   __init__r   
SSHSessionchannelLookupr   FileTransferServersubsystemLookupselfs    T/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/test/test_filetransfer.pyr   zTestAvatar.__init__>   s<    4 )0););:&(4(G(GW%    c                     	 t        |      }|D ]A  }|d   }t        |      dkD  xr |d   xs d}t        |      dkD  xr |d   xs i } ||i |}C S # t        $ r	 |||fg}Y Yw xY w)Nr          )iter	TypeErrorlen)r   fargskwifuncrs          r    
_runAsUserzTestAvatar._runAsUserC   s    	 QA  	"AQ4Dq6A:&!A$,"DQ!$!*Bd!b!A		"
   	 T2A	 s   A A'&A'N)__name__
__module____qualname__r   r/   r$   r!   r    r   r   =   s    H

r!   r   c                       e Zd Zd Zd Zy)FileTransferTestAvatarc                 <    t         j                  |        || _        y N)r   r   homeDir)r   r7   s     r    r   zFileTransferTestAvatar.__init__Q   s    D!r!   c                     t        t        j                               j                  | j                  j
                        S r6   )r   osgetcwdpreauthChildr7   pathr   s    r    
getHomeDirz!FileTransferTestAvatar.getHomeDirU   s(    		$11$,,2C2CDDr!   N)r0   r1   r2   r   r=   r$   r!   r    r4   r4   P   s    Er!   r4   c                       e Zd Zd Zy)ConchSessionForTestAvatarc                     || _         y r6   avatar)r   rB   s     r    r   z"ConchSessionForTestAvatar.__init__Z   s	    r!   N)r0   r1   r2   r   r$   r!   r    r?   r?   Y   s    r!   r?   ztwisted.conch.unix imported z0, but doesn't define SFTPServerForUnixConchUser'c                       e Zd Zd Zd Zy)FileTransferForTestAvatarc                 
    ddiS )N	   conchTest   ext datar$   )r   versionotherExts      r    
gotVersionz$FileTransferForTestAvatar.gotVersionm   s     +..r!   c                     |dk(  ryt         )N   testExtendedRequest   bar)NotImplementedError)r   extNameextDatas      r    extendedRequestz)FileTransferForTestAvatar.extendedRequestp   s    00%%r!   N)r0   r1   r2   rJ   rQ   r$   r!   r    rD   rD   l   s    	/	&r!   rD   c                       e Zd Zd Zy)SFTPTestBasec                 6   t        | j                               | _        | j                  j                  d      | _        | j                  j                  d      j	                  d       | j                  j                  d      j                  d      5 }|j                  d       t        dd	      5 }|j                  |j                  d
             d d d        d d d        | j                  j                  d      j                  d       | j                  j                  d      j                  d      5 }|j                  d       d d d        | j                  j                  d      j                  d      5 }|j                  d       d d d        | j                  j                  d      j                  d      5 }|j                  d       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   uxY w# 1 sw Y   y xY w)NextratestDirectoryT	testfile1wb)mode   aaaaaaaaaabbbbbbbbbbz/dev/urandomrbi   i  testRemoveFile   atestRenameFilez.testHiddenFile)	r   mktemptestDirchildmakedirsopenwritereadchmod)r   r)   f2s      r    setUpzSFTPTestBase.setUp{   s   . ||))'2?+44T:\\,11t1< 	,GG)*nd+ ,r	*+,	, 	;'--e4\\ 0166D6A 	QGGDM	\\ 0166D6A 	QGGDM	\\ 1277T7B 	aGGDM	 	, ,	, 	,
	 		 		 	sH   G*2!GG*8G7=HHG'	"G**G47H HHN)r0   r1   r2   rh   r$   r!   r    rS   rS   z   s    r!   rS   z can't run on non-posix computersc                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zej4                  d        Zej4                  d        Zy)OurServerOurClientTestsc                 L    t         j                          t         j                         _        t        j                   j                         _        t        j                   j                        }t        j                          _        d  _        d  _         fd}| j                  _        t        j                   j                        } j                  j                  |        j                  j                  |       | _        | _         j%                          y )NrA   c                 "    | _         |_        y r6   )_serverVersion_extData)serverVersionrP   r   s     r    _z(OurServerOurClientTests.setUp.<locals>._   s    "/D#DMr!   )rS   rh   r4   r`   rB   r   r   serverr	   LoopbackRelayFileTransferClientclientrm   rn   gotServerVersionmakeConnectionclientTransportserverTransport_emptyBuffers)r   rw   rp   rx   s   `   r    rh   zOurServerOurClientTests.setUp   s    4 ,T\\:"55T[[I"00="557"	$ ()$"00=""?3""?3..r!   c                 "   | j                   j                  s| j                  j                  rc| j                   j                          | j                  j                          | j                   j                  rK| j                  j                  rby y r6   )rx   bufferrw   clearBufferr   s    r    ry   z%OurServerOurClientTests._emptyBuffers   sb    ""))T-A-A-H-H  ,,.  ,,. ""))T-A-A-H-Hr!   c                     | j                   j                          | j                  j                          | j                   j                          | j                  j                          y r6   )rx   loseConnectionrw   r|   r   s    r    tearDownz OurServerOurClientTests.tearDown   sJ    ++-++-((*((*r!   c                 x    | j                  | j                  d       | j                  | j                  ddi       y )N   rF   rG   )assertEqualrm   rn   r   s    r    test_serverVersionz*OurServerOurClientTests.test_serverVersion   s1    ,,a0{(CDr!   c                     | j                  t        j                  j                  | j                  j
                        d| j                  j
                         y)z:
        It implements the ISFTPServer interface.
        zISFTPServer not provided by N)
assertTruer   ISFTPServer
providedByrq   rt   r   s    r    test_interface_implementationz5OurServerOurClientTests.test_interface_implementation   sC     	$$//0B0BC*4;;+=+=*@A	
r!   c                 0     j                   j                  dt        j                  t        j                  z  i       } j                          t        j                  g fd} j                  t        d|        fd}|j                  |       |S )zW
        A file opened with C{openFile} is closed when the connection is lost.
        	   testfile1c                 8    j                  |         |        y r6   append)fdclosedoldCloses    r    closezJOurServerOurClientTests.test_openedFileClosedWithConnection.<locals>.close   s    MM"RLr!   r   c                    j                   j                  | j                  dd     j                  }j                  j                          j                  j                          j                  j                          j                  j                          j                  j                   j                  i        j                  |       y )N   )
rq   	openFileshandler   rx   r~   rw   r|   r   assertIn)openFiler   r   r   s     r    _fileOpenedzPOurServerOurClientTests.test_openedFileClosedWithConnection.<locals>._fileOpened   s    &&xqr':;>>B  //1  //1  ,,.  ,,.T[[22B7MM"f%r!   )
rt   r   r   FXF_READ	FXF_WRITEry   r9   r   patchaddCallback)r   dr   r   r   r   s   `   @@r    #test_openedFileClosedWithConnectionz;OurServerOurClientTests.test_openedFileClosedWithConnection   s|     KK  ,//,2H2HH"
 	88	 	

2w&	& 	
k"r!   c                       j                   j                  d      } j                           fd}|j                  |       |S )zh
        A directory opened with C{openDirectory} is close when the connection
        is lost.
         c                 "   j                   j                          j                  j                          j                   j                          j                  j                          j	                  j
                  j                  i        y r6   )rx   r~   rw   r|   r   rq   openDirs)openDirr   s    r    	_getFileszSOurServerOurClientTests.test_openedDirectoryClosedWithConnection.<locals>._getFiles   se      //1  //1  ,,.  ,,.T[[1126r!   rt   openDirectoryry   r   )r   r   r   s   `  r    (test_openedDirectoryClosedWithConnectionz@OurServerOurClientTests.test_openedDirectoryClosedWithConnection   s=    
 KK%%b)	7 	
i r!   c                       j                   j                  dt        j                  t        j                  z  i       } j                           fd} fd fd fd|j                  |       |S )Nr   c                     j                  | t        j                  |               |       }|j                  |        |S r6   )r   r   	ISFTPFiler   )r   r   
_readChunk_writeChunkr   s     r    r   z<OurServerOurClientTests.test_openFileIO.<locals>._fileOpened   s;    X|'='=h'GH8$AMM+x0Hr!   c                     | j                  dd      }j                          |j                  j                  d       |S )Nr      rZ   	readChunkry   r   r   )r   r   r   s     r    r   z;OurServerOurClientTests.test_openFileIO.<locals>._readChunk   s;    ""1b)A MM$**,ABHr!   c                 p    |j                  dd      }j                          |j                  |       |S )Nr   s
   cccccccccc)
writeChunkry   r   )rp   r   r   _readChunk2r   s      r    r   z<OurServerOurClientTests.test_openFileIO.<locals>._writeChunk  s4    ##B	2A MM+x0Hr!   c                     |j                  dd      }j                          |j                  j                  d       |S )Nr      s   aaaaaaaaaabbbbbbbbbbccccccccccr   rp   r   r   r   s      r    r   z<OurServerOurClientTests.test_openFileIO.<locals>._readChunk2
  s;    ""1b)A MM$**,MNHr!   rt   r   r   r   r   ry   r   )r   r   r   r   r   r   s   `  @@@r    test_openFileIOz'OurServerOurClientTests.test_openFileIO   sd    KK  ,//,2H2HH"
 					 	
k"r!   c                       j                   j                  dt        j                  t        j                  z  i       } j                           fd fd fd}|j                  |       |S )Nr   c                 H    |j                         }j                          |S r6   )getAttrsry   r   s      r    	_getAttrszBOurServerOurClientTests.test_closedFileGetAttrs.<locals>._getAttrs  s"    !!#A Hr!   c                 (    j                          | S r6   )flushLoggedErrors)r)   r   s    r    _errz=OurServerOurClientTests.test_closedFileGetAttrs.<locals>._err  s    ""$Hr!   c                     | j                         }j                          |j                  |        |j                         j	                  |t
        j                        S r6   )r   ry   r   
addErrbackassertFailurer   	SFTPError)r   r   r   r   r   s     r    _closez?OurServerOurClientTests.test_closedFileGetAttrs.<locals>._close"  sN     A MM)X.LL%%a)?)?@@r!   r   )r   r   r   r   r   s   `  @@r    test_closedFileGetAttrsz/OurServerOurClientTests.test_closedFileGetAttrs  s`    KK  ,//,2H2HH"
 		
		A 	
fr!   c                       j                   j                  dt        j                  t        j                  z  i       } j                           fd} fd|j                  |      S )Nr   c                 j    | j                         }j                          |j                         |S r6   r   ry   r   )r   r   
_getAttrs2r   s     r    r   zBOurServerOurClientTests.test_openFileAttributes.<locals>._getAttrs2  s.    !!#A MM*%Hr!   c                     j                   j                  d      }j                          |j                  j                  |        |S Nr   rt   r   ry   r   r   )attrs1r   r   s     r    r   zCOurServerOurClientTests.test_openFileAttributes.<locals>._getAttrs28  s<    $$\2A MM$**F3Hr!   r   )r   r   r   r   s   `  @r    test_openFileAttributesz/OurServerOurClientTests.test_openFileAttributes,  sX    KK  ,//,2H2HH"
 			 }}Y''r!   c                       j                   j                  dt        j                  t        j                  z  i       } j                           fd} fd fd|j                  |       |S )Nr   c                 j    | j                         }j                          |j                         |S r6   r   )r   r   	_setAttrsr   s     r    r   z@OurServerOurClientTests.test_openFileSetAttrs.<locals>._getAttrsH  s.    !!#A MM)$Hr!   c                     d| d<   j                   j                  d|       }j                          |j                         |j                  j                  |        |S )Nr   atimer   )rt   setAttrsry   r   r   )attrsr   r   r   s     r    r   z@OurServerOurClientTests.test_openFileSetAttrs.<locals>._setAttrsN  sS    E'N$$\59A MM*%MM$**E2Hr!   c                 ^    j                   j                  d      }j                          |S r   rt   r   ry   rp   r   r   s     r    r   zAOurServerOurClientTests.test_openFileSetAttrs.<locals>._getAttrs2V  s(    $$\2A Hr!   r   )r   r   r   r   r   s   `  @@r    test_openFileSetAttrsz-OurServerOurClientTests.test_openFileSetAttrs@  sa     KK  ,//,2H2HH"
 				
 	
i r!   c                 X    i  j                   j                  j                  fd}| j                   j                  _         j                  j                  dt        j                  t        j
                  z  ddi      } j                           fd}|j                  |      S )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                 :    j                  |        | ||      S r6   )update)filenameflagsr   oldOpenFilesavedAttributess      r    r   zIOurServerOurClientTests.test_openFileExtendedAttributes.<locals>.openFileg  s     ""5)x66r!   r   ext_foorM   c                 .    j                  ddi       y )Nr   rM   r   )ignr   r   s    r    checkzFOurServerOurClientTests.test_openFileExtendedAttributes.<locals>.checkt  s    _y&.ABr!   )rq   rt   r   r   r   r   ry   r   )r   r   r   r   r   r   s   `   @@r    test_openFileExtendedAttributesz7OurServerOurClientTests.test_openFileExtendedAttributes^  s     kk((11	7 '/#KK  !!L$:$::

 		C }}U##r!   c                       j                   j                  d      } j                           fd}|j                  |       |j                  |        j	                  |t
        j                        S )N   testRemoveFilec                 ^    j                   j                  d      }j                          |S )Nr   )rt   
removeFilery   )ignoredr   r   s     r    _removeFilez<OurServerOurClientTests.test_removeFile.<locals>._removeFile}  s)    &&'89A Hr!   )rt   r   ry   r   r   r   r   )r   r   r   s   `  r    test_removeFilez'OurServerOurClientTests.test_removeFiley  s\    KK  !23	
 	
k"	k"!!!\%;%;<<r!   c                       j                   j                  d      } j                           fd} fd|j                  |      S )N   testRenameFilec                     j                   j                  dd      }j                          |j                  |        |S )Nr      testRenamedFile)rt   
renameFilery   r   )r   r   _testRenamedr   s     r    _renamez8OurServerOurClientTests.test_renameFile.<locals>._rename  s:    &&'8:LMA MM,.Hr!   c                     j                   j                  d      }j                          |j                  j                  |       y )Nr   r   )rp   r   r   r   s      r    r   z=OurServerOurClientTests.test_renameFile.<locals>._testRenamed  s8    $$%78A MM$**E2r!   )rt   r   ry   r   )r   r   r   r   s   `  @r    test_renameFilez'OurServerOurClientTests.test_renameFile  s?    KK  !23		3
 }}W%%r!   c                     | j                   j                  d      }| j                          | j                  |t        j
                        S N   testMakeDirectoryrt   r   ry   r   r   r   r   r   s     r    test_directoryBadz)OurServerOurClientTests.test_directoryBad  s<    KK  !56!!!\%;%;<<r!   c                      j                   j                  di       } j                           fd} fd}|j                  |       |j                  |       |j                  |        j	                  |t
        j                        S )Nr   c                 ^    j                   j                  d      }j                          |S r   r   r   s     r    r   zAOurServerOurClientTests.test_directoryCreation.<locals>._getAttrs  s)    $$%9:A Hr!   c                 ^    j                   j                  d      }j                          |S r   )rt   removeDirectoryry   r   s     r    _removeDirectoryzHOurServerOurClientTests.test_directoryCreation.<locals>._removeDirectory  s)    ++,@AA Hr!   )rt   makeDirectoryry   r   r   r   r   )r   r   r   r  s   `   r    test_directoryCreationz.OurServerOurClientTests.test_directoryCreation  sp    KK%%&:B?		
 	
i 	&'	i !!!\%;%;<<r!   c                       j                   j                  d      } j                          g  fd fd} fd|j                         |j                  |       |S )Nr!   c                       fd}t        j                   j                        }j                          |j	                  |       |j	                         |j                          |S )Nc                 *    j                  |        S r6   r   )r)   filesr   s    r    r   zMOurServerOurClientTests.test_openDirectory.<locals>._getFiles.<locals>.append  s    Qr!   )r   maybeDeferrednextry   r   r   )r   r   r   r   r   r  r   s   `  r    r   z=OurServerOurClientTests.test_openDirectory.<locals>._getFiles  sU     ##GLL1A MM&!MM)$LL)Hr!   c                     t        t        t               d         }|j                          j                  |g d       y )Nr   )s   .testHiddenFiles   testDirectoryr   r   r   )listzipsortr   )r   fsr  r   s     r    _checkFilesz?OurServerOurClientTests.test_openDirectory.<locals>._checkFiles  s:    d3;'*+BGGI	r!   c                 H    |j                         }j                          |S r6   )r   ry   )rp   r   r   r   s      r    r   z:OurServerOurClientTests.test_openDirectory.<locals>._close  s    A Hr!   r   )r   r   r  r   r   r  s   `  @@@r    test_openDirectoryz*OurServerOurClientTests.test_openDirectory  sV    KK%%c*
			
 	
i 	k"r!   c                     | j                   j                  d      }| j                          | j                  |t        j
                        S )N   testLinkr   r   s     r    test_linkDoesntExistz,OurServerOurClientTests.test_linkDoesntExist  s;    KK  -!!!\%;%;<<r!   c                       j                   j                  dd      } j                           fd} fd}|j                  |       |j                  |      S )Nr  r   c                 `    j                   j                  dd      }j                          |S )Nr  r#   r   r   s     r    _getFirstAttrszDOurServerOurClientTests.test_linkSharesAttrs.<locals>._getFirstAttrs  s*    $$[!4A Hr!   c                     j                   j                  d      }j                          |j                  j                  |        |S r   r   )
firstAttrsr   r   s     r    _getSecondAttrszEOurServerOurClientTests.test_linkSharesAttrs.<locals>._getSecondAttrs  s<    $$\2A MM$**J7Hr!   rt   makeLinkry   r   )r   r   r  r  s   `   r    test_linkSharesAttrsz,OurServerOurClientTests.test_linkSharesAttrs  sL    KK  l;	
	 	
n%}}_--r!   c                       j                   j                  dd      } j                           fd} fd}|j                  |       |j                  |       |S )Nr  r   c                 L   j                   j                  d      }j                          t        t	        j
                               j                  j                  j                        }|j                  d      }|j                  j                  |j                         |S Nr  rW   )rt   readLinkry   r   r9   r:   r;   r`   r<   ra   r   r   )rp   r   testFiler   s      r    	_readLinkz8OurServerOurClientTests.test_linkPath.<locals>._readLink  s    $$[1A 		,99$,,:K:KLH~~k2HMM$**HMM:Hr!   c                 L   j                   j                  d      }j                          t        t	        j
                               j                  j                  j                        }|j                  d      }|j                  j                  |j                         |S r   )rt   realPathry   r   r9   r:   r;   r`   r<   ra   r   r   )rp   r   testLinkr   s      r    	_realPathz8OurServerOurClientTests.test_linkPath.<locals>._realPath  r$  r!   r  )r   r   r#  r(  s   `   r    test_linkPathz%OurServerOurClientTests.test_linkPath  sN    KK  l;		 	
i 	i r!   c                     | j                   j                  dd      }| j                          |j                  | j                  d       |j                  | j
                         |S )NrL   s   foorM   )rt   rQ   ry   r   r   _cbTestExtendedRequestr   s     r    test_extendedRequestz,OurServerOurClientTests.test_extendedRequest
  sP    KK''(>G	d&&/	d112r!   c                     | j                   j                  dd      }| j                          | j                  |t              S )Ns   testBadRequestr!   )rt   rQ   ry   r   rN   )r   r   r   s      r    r+  z.OurServerOurClientTests._cbTestExtendedRequest  s9    KK''(93?!!!%899r!   c              #   t  K   | j                   j                  d      }| j                          |}|j                         }| j                          | | j	                         }d}| j                  dt        |             | j                  t        |d   d          | j                  ||d   d          yw)zJ
        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.r#   r   categorymessageN)rt   r   ry   r	  flushWarningsr   r(   DeprecationWarning)r   r   r   oneFilewarningsr0  s         r    $test_openDirectoryIteratorDeprecatedz<OurServerOurClientTests.test_openDirectoryIteratorDeprecated  s     
 KK%%c*',,.%%'@ 	 	CM*+Xa[-DE(1+i"89s   B6B8c              #      K    j                   j                  dt        j                  i       } j	                          |}g  fd} j
                  j                  j                         \  }||_        ~|j                  dd      } j	                           j                  t              d        j                  |        j                  j                           j                  j                           j                  j                           j	                           j!                   j                   j"                          j%                  |t&               |j)                         } j%                  |t&               yw)z{
        If there are requests outstanding when the connection
        is closed for any reason, they should fail.
        r   c                 v    j                  g        t        j                         }j                  |        |S r6   )r   r   Deferredr   )offsetlengthr   gotReadRequestr   s      r    	_slowReadzOOurServerOurClientTests.test_closedConnectionCancelsRequests.<locals>._slowRead9  s2    ^R0 A!!&)Hr!   d      r#   N)rt   r   r   r   ry   rq   r   valuesr   r   r(   assertNoResultrx   r~   r|   rw   assertFalse	connectedfailureResultOfr   r   )r   r   fhr<  serverSideFhr;  s   `    @r    $test_closedConnectionCancelsRequestsz<OurServerOurClientTests.test_closedConnectionCancelsRequests+  s7     KK  |/D/DbIW 	 ..557!* LLc"^,a0A 	++-((*((*../Q/ KKMQ/s   FFN)r0   r1   r2   rh   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r)  r,  r+  r   inlineCallbacksr5  rF  r$   r!   r    rj   rj      s    2/
+E
<$B2((<$6=&"=
=.&P=
.$0:
 : :( *0 *0r!   rj   c                       e Zd Zd Zy)FakeConnc                      y r6   r$   )r   channels     r    	sendClosezFakeConn.sendCloseZ  s    r!   N)r0   r1   r2   rL  r$   r!   r    rI  rI  Y  s    r!   rI  c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	FileTransferCloseTestsc                 "    t               | _        y r6   )r   rB   r   s    r    rh   zFileTransferCloseTests.setUp`  s     lr!   c                     t        j                         } G d d      } |       |_        | j                  |j                  _        |S )Nc                       e Zd Zd Zd Zd Zy)DFileTransferCloseTests.buildServerConnection.<locals>.DummyTransportc                     | | _         y r6   )	transportr   s    r    r   zMFileTransferCloseTests.buildServerConnection.<locals>.DummyTransport.__init__i  s	    !%r!   c                      y r6   r$   )r   kinddatas      r    
sendPacketzOFileTransferCloseTests.buildServerConnection.<locals>.DummyTransport.sendPacketl  s    r!   c                      y)Nzdummy transportr$   r   s    r    	logPrefixzNFileTransferCloseTests.buildServerConnection.<locals>.DummyTransport.logPrefixo  s    (r!   N)r0   r1   r2   r   rX  rZ  r$   r!   r    DummyTransportrR  h  s    &)r!   r[  )r   SSHConnectionrT  rB   )r   connr[  s      r    buildServerConnectionz,FileTransferCloseTests.buildServerConnectionc  s;    '')	) 	) () $r!   c                 H     d _         |j                   fd}||_        y )NFc                 $    d_          |        y )NT)connectionLostFired)reasonorigConnectionLostr   s    r    connectionLostzFFileTransferCloseTests.interceptConnectionLost.<locals>.connectionLostz  s    '+D$v&r!   )ra  rd  )r   
sftpServerrd  rc  s   `  @r    interceptConnectionLostz.FileTransferCloseTests.interceptConnectionLostv  s&    #( '66	' %3
!r!   c                 <    | j                  | j                  d       y )Nz*sftpServer's connectionLost was not called)r   ra  r   s    r    assertSFTPConnectionLostz/FileTransferCloseTests.assertSFTPConnectionLost  s    $$&R	
r!   c                 @   t        j                  t               | j                        }|j	                  t        j                  d             |j                  j                  j                  }| j                  |       |j                          | j                          y)ze
        Closing a session should notify an SFTP subsystem launched by that
        session.
        )r]  rB   r   N)r   r   rI  rB   request_subsystemr   NSrt   rT  protorf  closeReceivedrh  )r   testSessionre  s      r    test_sessionClosez(FileTransferCloseTests.test_sessionClose  sv     ((hjM 	%%fii&89 ''1177
 	$$Z0 	!!#%%'r!   c                    | j                         }t        j                  d      t        j                  dd      dz  z   }|j                  |       |j                  d   }|j                  t        j                  d             |j                  j                  j                  }| j                  |       | j                  |       |j                  t        j                  dd             | j                          y)zy
        A client sending CHANNEL_CLOSE should trigger closeReceived on the
        associated channel instance.
        r   >Lr   r   r   N)r^  r   rk  structpackssh_CHANNEL_OPENchannelsrj  rt   rT  rl  rf  ssh_CHANNEL_CLOSErh  r   r]  packetsessionChannelre  s        r    %test_clientClosesChannelOnConnnectionz<FileTransferCloseTests.test_clientClosesChannelOnConnnection  s    
 ))+ :&T1)=)AAf%q)((7);<#**44::
$$Z0 	$$Z0 	v{{434%%'r!   c                    | j                         }t        j                  d      t        j                  dd      dz  z   }|j                  |       |j                  d   }|j                  t        j                  d             |j                  j                  j                  }| j                  |       |j                          | j                          y)zP
        Closing an SSH connection should close all sessions within it.
        r   rq  r   r   r   N)r^  r   rk  rr  rs  rt  ru  rj  rt   rT  rl  rf  serviceStoppedrh  rw  s        r    'test_stopConnectionServiceClosesChannelz>FileTransferCloseTests.test_stopConnectionServiceClosesChannel  s     ))+ :&T1)=)AAf%q)((7);<#**44::
$$Z0 	%%'r!   N)
r0   r1   r2   rh   r^  rf  rh  ro  rz  r}  r$   r!   r    rN  rN  ^  s%    #&3

(((0(r!   rN  zCannot run without cryptographyc                       e Zd ZdZg dZd Zy)ConstantsTestsag  
    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.
    )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
        c                    i }| j                   D ]]  }|j                         D ]H  }t        j                  d|      }|st	        |j                  d      d      ||j                  d      <   J _ | j                  t        |      dkD  d       |j                         D ]%  \  }}| j                  |t        t        |             ' y)z
        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*$r%   r   r#   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_constantsAgainstSpecz(ConstantsTests.test_constantsAgainstSpec  s    
 	00 	?G**, ?HHI4P,/
A,>Iaggaj)?	?
 		NQ N	
 OO% 	:DAqQa 89	:r!   N)r0   r1   r2   __doc__r  r  r$   r!   r    r  r    s    JX:r!   r  c                       e Zd ZdZd Zd Zy)RawPacketDataServerTestsz
    Tests for L{filetransfer.FileTransferServer} which explicitly craft
    certain less common situations to exercise their handling.
    c                 J    t        j                  t                     | _        y )NrA   )r   r   r   ftsr   s    r    rh   zRawPacketDataServerTests.setUp9  s    22*,Gr!   c                    t               }| j                  j                  |       d}d}t        j                  t        dg      |z   t        j                  |      z         }| j                  j                  |       t        j                  t        dg      |z   t        g d      z   t        j                  d      z   t        j                  d      z         }t        |j                         t        |             y)	zl
        A close request with an unknown handle receives an FX_NO_SUCH_FILE error
        response.
        s   1234s   invalid handler   e   )r   r   r   r%   s   No such file or directoryr!   N)
r   r  rv   r   rk  bytesdataReceivedr   valuer   )r   rT  	requestIdr   r   expecteds         r    test_closeInvalidHandlez0RawPacketDataServerTests.test_closeInvalidHandle<  s    
 $%		* 	" 		 1#Jii 
 	e$ 99 3%L  ,  II23	4 IIcN
& 	OOX	
r!   N)r0   r1   r2   r  rh   r  r$   r!   r    r  r  1  s    
H1
r!   r  c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
RawPacketDataTestsz
    Tests for L{filetransfer.FileTransferClient} which explicitly craft certain
    less common protocol messages to exercise their handling.
    c                 6    t        j                         | _        y r6   )r   rs   ftcr   s    r    rh   zRawPacketDataTests.setUpw  s    224r!   c                 n   t        j                         }|j                  | j                         || j                  j
                  d<   t        j                  ddt        j                        t        j                  d      z   t        j                  d      z   }| j                  j                  |       |S )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.
        r#   !LL   msg   lang)r   r8  r   _cbTestPacketSTATUSr  openRequestsrr  rs  r   FX_OKr   rk  packet_STATUSr   r   rW  s      r    test_packetSTATUSz$RawPacketDataTests.test_packetSTATUSz  s     NN	d../#$a KKq,"4"45ii ii ! 	
 	t$r!   c                 X    | j                  |d   d       | j                  |d   d       y)z{
        Assert that the result is a two-tuple containing the message and
        language from the STATUS packet.
        r   r  r#   r  Nr   r   results     r    r  z&RawPacketDataTests._cbTestPacketSTATUS  s,    
 	F+G,r!   c                    t        j                         }|j                  | j                         || j                  j
                  d<   t        j                  ddt        j                        }| j                  j                  |       |S )az  
        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  )r   r8  r   _cbTestPacketSTATUSShortr  r  rr  rs  r   r  r  r  s      r    test_packetSTATUSShortz)RawPacketDataTests.test_packetSTATUSShort  se     NN	d334#$a {{5!\%7%78t$r!   c                 X    | j                  |d   d       | j                  |d   d       y)z
        Assert that the result is a two-tuple containing empty strings, since
        the STATUS packet had neither a message nor a language.
        r   r!   r#   Nr   r  s     r    r  z+RawPacketDataTests._cbTestPacketSTATUSShort  s,    
 	C(C(r!   c                 B   t        j                         }|j                  | j                         || j                  j
                  d<   t        j                  ddt        j                        t        j                  d      z   }| j                  j                  |       |S )a  
        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  )r   r8  r   _cbTestPacketSTATUSWithoutLangr  r  rr  rs  r   r  r   rk  r  r  s      r    test_packetSTATUSWithoutLangz/RawPacketDataTests.test_packetSTATUSWithoutLang  ss     NN	d99:#$a {{5!\%7%78699V;LLt$r!   c                 X    | j                  |d   d       | j                  |d   d       y)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  r#   r!   Nr   r  s     r    r  z1RawPacketDataTests._cbTestPacketSTATUSWithoutLang  s,    
 	F+C(r!   N)r0   r1   r2   r  rh   r  r  r  r  r  r  r$   r!   r    r  r  p  s*    
5&- )")r!   r  )8r  r9   r  rr  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   r4   r?   r4  warnrD   registerAdapterr   rS   rj   rI  rN  r  r  r  r$   r!   r    <module>r     sM  

 
 	   * " 1 4 & % , ++ D= "=!( !L< I	KK
 &EZ E 
 % HMM
 		&$> & J!:|/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   E7 F F !F! *F/ 7FFFFFF!F,+F,/F87F8