
    tfv}                        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n# e$ r dZY nw xY w	 ddlmZ eZn# e$ r dZY nw xY w	 ddlZeZn# e$ r dZY nw xY w	 ddl m!Z" e"Z!n# e$ r e#Z!Y nw xY w	 ddl$m%Z%m&Z&m'Z'm(Z( n# e$ r Y nw xY w G d de!          Z) G d de)          Z* G d d          Z+eddl,Z, e,j-        ded           n% G d de          Z. ej/        e.e)e'j0                    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                      Z7dS )*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dS )
TestAvatarc                     t                               |            t          j        | j        d<   t
          j        | j        d<   d S )N   session   sftp)r   __init__r   
SSHSessionchannelLookupr   FileTransferServersubsystemLookupselfs    c/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/conch/test/test_filetransfer.pyr   zTestAvatar.__init__>   s<    4   )0);:&(4(GW%%%    c                     	 t          |          }n# t          $ r	 |||fg}Y nw xY w|D ]L}|d         }t          |          dk    r|d         pd}t          |          dk    r|d         pi } ||i |}M|S )Nr          )iter	TypeErrorlen)r   fargskwifuncrs          r    
_runAsUserzTestAvatar._runAsUserC   s    	 QAA 	  	  	 T2AAA	  	" 	"AQ4Dq66A:&!A$,"DQ!$!*Bd!b!!AAs    %%N)__name__
__module____qualname__r   r/   r$   r!   r    r   r   =   s5        H H H

 
 
 
 
r!   r   c                       e Zd Zd Zd ZdS )FileTransferTestAvatarc                 H    t                               |            || _        d S N)r   r   homeDir)r   r7   s     r    r   zFileTransferTestAvatar.__init__Q   s!    D!!!r!   c                 |    t          t          j                                                  | j        j                  S r6   )r   osgetcwdpreauthChildr7   pathr   s    r    
getHomeDirz!FileTransferTestAvatar.getHomeDirU   s)    	$$11$,2CDDDr!   N)r0   r1   r2   r   r=   r$   r!   r    r4   r4   P   s7          E E E E Er!   r4   c                       e Zd Zd ZdS )ConchSessionForTestAvatarc                     || _         d S 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dS )FileTransferForTestAvatarc                 
    ddiS )N	   conchTest   ext datar$   )r   versionotherExts      r    
gotVersionz$FileTransferForTestAvatar.gotVersionm   s     +..r!   c                      |dk    rdS t           )N   testExtendedRequest   bar)NotImplementedError)r   extNameextDatas      r    extendedRequestz)FileTransferForTestAvatar.extendedRequestp   s    000v%%r!   N)r0   r1   r2   rJ   rQ   r$   r!   r    rD   rD   l   s2        	/ 	/ 	/	& 	& 	& 	& 	&r!   rD   c                       e Zd Zd ZdS )SFTPTestBasec                    t          |                                           | _        | j                            d          | _        | j                            d                              d           | j                            d                              d          5 }|                    d           t          dd	          5 }|                    |                    d
                     d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   | j                            d                              d           | j                            d                              d          5 }|                    d           d d d            n# 1 swxY w Y   | j                            d                              d          5 }|                    d           d d d            n# 1 swxY w Y   | j                            d                              d          5 }|                    d           d d d            d S # 1 swxY w Y   d S )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{   s2   .. |))'22?++44T:::\,,11t1<< 	,GG)***nd++ ,r	**+++, , , , , , , , , , , , , , ,	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	;''--e444\ 01166D6AA 	QGGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	\ 01166D6AA 	QGGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	\ 12277T7BB 	aGGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sl   !&D)C<0D<D 	 DD 	DDD9FF"FG66G:=G:/III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j        d             Zej        d             ZdS )OurServerOurClientTestsc                 *    t                                           t           j                   _        t          j         j                   _        t          j	         j                  }t          j
                     _        d  _        d  _         fd}| j        _        t          j	         j                  } j                            |            j                            |           | _        | _                                          d S )NrA   c                 $    | _         |_        d S r6   )_serverVersion_extData)serverVersionrP   r   s     r    _z(OurServerOurClientTests.setUp.<locals>._   s    "/D#DMMMr!   )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\::"5T[III"0=="577"	$ 	$ 	$ 	$ 	$ ()$"0==""?333""?333..r!   c                     | j         j        s| j        j        rL| j                                          | j                                         | j         j        >| j        j        Jd S d S r6   )rx   bufferrw   clearBufferr   s    r    ry   z%OurServerOurClientTests._emptyBuffers   sx    ") 	/T-A-H 	/ ,,... ,,... ") 	/T-A-H 	/ 	/ 	/ 	/ 	/r!   c                     | j                                          | j                                         | j                                          | j                                         d S r6   )rx   loseConnectionrw   r|   r   s    r    tearDownz OurServerOurClientTests.tearDown   s\    ++---++---((***((*****r!   c                 v    |                      | j        d           |                      | j        ddi           d S )N   rF   rG   )assertEqualrm   rn   r   s    r    test_serverVersionz*OurServerOurClientTests.test_serverVersion   s>    ,a000{(CDDDDDr!   c                     |                      t          j                            | j        j                  d| j        j                   dS )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   sM     	$//0BCCA4;+=AA	
 	
 	
 	
 	
r!   c                 0     j                             dt          j        t          j        z  i           }                                  t          j        g fd}                     t          d|            fd}|	                    |           |S )zW
        A file opened with C{openFile} is closed when the connection is lost.
        	   testfile1c                 H                         |             |            d S r6   append)fdclosedoldCloses    r    closezJOurServerOurClientTests.test_openedFileClosedWithConnection.<locals>.close   s&    MM"HRLLLLLr!   r   c                    j         j        | j        dd                   j        }j                                         j                                         j                                         j                                                             j         j        i            	                    |           d S )N   )
rq   	openFileshandler   rx   r~   rw   r|   r   assertIn)openFiler   r   r   s     r    _fileOpenedzPOurServerOurClientTests.test_openedFileClosedWithConnection.<locals>._fileOpened   s    &xqrr':;>B //111 //111 ,,... ,,...T[2B77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     K  ,/,2HH"
 
 	8	 	 	 	 	 	 	

2w&&&	& 	& 	& 	& 	& 	& 	
k"""r!   c                       j                             d          }                                   fd}|                    |           |S )zh
        A directory opened with C{openDirectory} is close when the connection
        is lost.
         c                    j                                          j                                         j                                          j                                                             j        j        i            d S r6   )rx   r~   rw   r|   r   rq   openDirs)openDirr   s    r    	_getFileszSOurServerOurClientTests.test_openedDirectoryClosedWithConnection.<locals>._getFiles   sx     //111 //111 ,,... ,,...T[1266666r!   rt   openDirectoryry   r   )r   r   r   s   `  r    (test_openedDirectoryClosedWithConnectionz@OurServerOurClientTests.test_openedDirectoryClosedWithConnection   s^    
 K%%b))	7 	7 	7 	7 	7 	
i   r!   c                       j                             dt          j        t          j        z  i           }                                   fd} fd fd fd|                    |           |S )Nr   c                                          | t          j        |                       |           }|                    |            |S r6   )r   r   	ISFTPFiler   )r   r   
_readChunk_writeChunkr   s     r    r   z<OurServerOurClientTests.test_openFileIO.<locals>._fileOpened   sL    X|'=h'G'GHHH
8$$AMM+x000Hr!   c                     |                      dd          }                                 |                    j        d           |S )Nr      rZ   	readChunkry   r   r   )r   r   r   s     r    r   z;OurServerOurClientTests.test_openFileIO.<locals>._readChunk   sG    ""1b))A   MM$*,ABBBHr!   c                     |                     dd          }                                 |                    |           |S )Nr   s
   cccccccccc)
writeChunkry   r   )rp   r   r   _readChunk2r   s      r    r   z<OurServerOurClientTests.test_openFileIO.<locals>._writeChunk  sC    ##B	22A   MM+x000Hr!   c                     |                     dd          }                                 |                    j        d           |S )Nr      s   aaaaaaaaaabbbbbbbbbbccccccccccr   rp   r   r   r   s      r    r   z<OurServerOurClientTests.test_openFileIO.<locals>._readChunk2
  sG    ""1b))A   MM$*,MNNNHr!   rt   r   r   r   r   ry   r   )r   r   r   r   r   r   s   `  @@@r    test_openFileIOz'OurServerOurClientTests.test_openFileIO   s    K  ,/,2HH"
 
 		 	 	 	 	 	 		 	 	 	 		 	 	 	 	 		 	 	 	 	 	
k"""r!   c                       j                             dt          j        t          j        z  i           }                                   fd fd fd}|                    |           |S )Nr   c                 X    |                                 }                                 |S r6   )getAttrsry   r   s      r    	_getAttrszBOurServerOurClientTests.test_closedFileGetAttrs.<locals>._getAttrs  s+    !!##A   Hr!   c                 0                                      | S r6   )flushLoggedErrors)r)   r   s    r    _errz=OurServerOurClientTests.test_closedFileGetAttrs.<locals>._err  s    ""$$$Hr!   c                     |                                  }                                 |                    |            |                                                   |t
          j                  S r6   )r   ry   r   
addErrbackassertFailurer   	SFTPError)r   r   r   r   r   s     r    _closez?OurServerOurClientTests.test_closedFileGetAttrs.<locals>._close"  sc      A   MM)X...LL%%a)?@@@r!   r   )r   r   r   r   r   s   `  @@r    test_closedFileGetAttrsz/OurServerOurClientTests.test_closedFileGetAttrs  s    K  ,/,2HH"
 
 		 	 	 	 	
	 	 	 	 		A 	A 	A 	A 	A 	A 	A 	
fr!   c                       j                             dt          j        t          j        z  i           }                                   fd} fd|                    |          S )Nr   c                     |                                  }                                 |                               |S r6   r   ry   r   )r   r   
_getAttrs2r   s     r    r   zBOurServerOurClientTests.test_openFileAttributes.<locals>._getAttrs2  s=    !!##A   MM*%%%Hr!   c                     j                             d          }                                 |                    j        |            |S Nr   rt   r   ry   r   r   )attrs1r   r   s     r    r   zCOurServerOurClientTests.test_openFileAttributes.<locals>._getAttrs28  sF    $$\22A   MM$*F333Hr!   r   )r   r   r   r   s   `  @r    test_openFileAttributesz/OurServerOurClientTests.test_openFileAttributes,  s    K  ,/,2HH"
 
 		 	 	 	 	 		 	 	 	 	 }}Y'''r!   c                       j                             dt          j        t          j        z  i           }                                   fd} fd fd|                    |           |S )Nr   c                     |                                  }                                 |                               |S r6   r   )r   r   	_setAttrsr   s     r    r   z@OurServerOurClientTests.test_openFileSetAttrs.<locals>._getAttrsH  s=    !!##A   MM)$$$Hr!   c                     d| d<   j                             d|           }                                 |                               |                    j        |            |S )Nr   atimer   )rt   setAttrsry   r   r   )attrsr   r   r   s     r    r   z@OurServerOurClientTests.test_openFileSetAttrs.<locals>._setAttrsN  sc    E'N$$\599A   MM*%%%MM$*E222Hr!   c                 d    j                             d          }                                 |S r   rt   r   ry   rp   r   r   s     r    r   zAOurServerOurClientTests.test_openFileSetAttrs.<locals>._getAttrs2V  s/    $$\22A   Hr!   r   )r   r   r   r   r   s   `  @@r    test_openFileSetAttrsz-OurServerOurClientTests.test_openFileSetAttrs@  s     K  ,/,2HH"
 
 		 	 	 	 	 		 	 	 	 	 		 	 	 	 	
 	
i   r!   c                 $    i  j         j        j        fd}| j         j        _         j                            dt          j        t          j        z  ddi          }                                   fd}|                    |          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                 H                         |            | ||          S r6   )update)filenameflagsr   oldOpenFilesavedAttributess      r    r   zIOurServerOurClientTests.test_openFileExtendedAttributes.<locals>.openFileg  s+    ""5)));x666r!   r   ext_foorM   c                 8                         ddi           d S )Nr   rM   r   )ignr   r   s    r    checkzFOurServerOurClientTests.test_openFileExtendedAttributes.<locals>.checkt  s$    _y&.ABBBBBr!   )rq   rt   r   r   r   r   ry   r   )r   r   r   r   r   r   s   `   @@r    test_openFileExtendedAttributesz7OurServerOurClientTests.test_openFileExtendedAttributes^  s     k(1	7 	7 	7 	7 	7 	7 '/#K  !L$::
 

 		C 	C 	C 	C 	C 	C }}U###r!   c                       j                             d          }                                   fd}|                    |           |                    |                                |t
          j                  S )N   testRemoveFilec                 d    j                             d          }                                 |S )Nr   )rt   
removeFilery   )ignoredr   r   s     r    _removeFilez<OurServerOurClientTests.test_removeFile.<locals>._removeFile}  s0    &&'899A   Hr!   )rt   r   ry   r   r   r   r   )r   r   r   s   `  r    test_removeFilez'OurServerOurClientTests.test_removeFiley  s    K  !233	 	 	 	 	
 	
k"""	k"""!!!\%;<<<r!   c                       j                             d          }                                   fd} fd|                    |          S )N   testRenameFilec                     j                             dd          }                                 |                    |            |S )Nr      testRenamedFile)rt   
renameFilery   r   )r   r   _testRenamedr   s     r    _renamez8OurServerOurClientTests.test_renameFile.<locals>._rename  sG    &&'8:LMMA   MM,...Hr!   c                     j                             d          }                                 |                    j        |           d S )Nr   r   )rp   r   r   r   s      r    r   z=OurServerOurClientTests.test_renameFile.<locals>._testRenamed  sH    $$%788A   MM$*E22222r!   )rt   r   ry   r   )r   r   r   r   s   `  @r    test_renameFilez'OurServerOurClientTests.test_renameFile  sw    K  !233	 	 	 	 	 		3 	3 	3 	3 	3
 }}W%%%r!   c                     | j                             d          }|                                  |                     |t          j                  S N   testMakeDirectoryrt   r   ry   r   r   r   r   r   s     r    test_directoryBadz)OurServerOurClientTests.test_directoryBad  sC    K  !566!!!\%;<<<r!   c                 4     j                             di           }                                   fd} fd}|                    |           |                    |           |                    |                                |t
          j                  S )Nr   c                 d    j                             d          }                                 |S r   r   r   s     r    r   zAOurServerOurClientTests.test_directoryCreation.<locals>._getAttrs  s0    $$%9::A   Hr!   c                 d    j                             d          }                                 |S r   )rt   removeDirectoryry   r   s     r    _removeDirectoryzHOurServerOurClientTests.test_directoryCreation.<locals>._removeDirectory  s0    ++,@AAA   Hr!   )rt   makeDirectoryry   r   r   r   r   )r   r   r   r  s   `   r    test_directoryCreationz.OurServerOurClientTests.test_directoryCreation  s    K%%&:B??	 	 	 	 		 	 	 	 	
 	
i   	&'''	i   !!!\%;<<<r!   c                       j                             d          }                                  g  fd fd} fd|                               |                    |           |S )Nr!   c                       fd}t          j         j                  }                                 |                    |           |                               |                                |S )Nc                 2                         |            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  s          #GL11A   MM&!!!MM)$$$LL)))Hr!   c                     t          t          t                     d                   }|                                                     |g d           d S )Nr   )s   .testHiddenFiles   testDirectoryr   r   r   )listzipsortr   )r   fsr  r   s     r    _checkFilesz?OurServerOurClientTests.test_openDirectory.<locals>._checkFiles  sb    d3;''*++BGGIII  	 	 	 	 	r!   c                 X    |                                 }                                 |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  s    K%%c**
	 
	 
	 
	 
	 
	 
	 
		 	 	 	 	 		 	 	 	 	
 	
i   	k"""r!   c                     | j                             d          }|                                  |                     |t          j                  S )N   testLinkr   r   s     r    test_linkDoesntExistz,OurServerOurClientTests.test_linkDoesntExist  sB    K  --!!!\%;<<<r!   c                       j                             dd          }                                   fd} fd}|                    |           |                    |          S )Nr  r   c                 f    j                             dd          }                                 |S )Nr  r#   r   r   s     r    _getFirstAttrszDOurServerOurClientTests.test_linkSharesAttrs.<locals>._getFirstAttrs  s1    $$[!44A   Hr!   c                     j                             d          }                                 |                    j        |            |S r   r   )
firstAttrsr   r   s     r    _getSecondAttrszEOurServerOurClientTests.test_linkSharesAttrs.<locals>._getSecondAttrs  sF    $$\22A   MM$*J777Hr!   rt   makeLinkry   r   )r   r   r  r  s   `   r    test_linkSharesAttrsz,OurServerOurClientTests.test_linkSharesAttrs  s    K  l;;	 	 	 	 	
	 	 	 	 	 	
n%%%}}_---r!   c                       j                             dd          }                                   fd} fd}|                    |           |                    |           |S )Nr  r   c                 H   j                             d          }                                 t          t	          j                                                  j        j                  }|	                    d          }|
                    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      $$[11A   	,,99$,:KLLH~~k22HMM$*HM:::Hr!   c                 H   j                             d          }                                 t          t	          j                                                  j        j                  }|	                    d          }|
                    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  s    K  l;;	 	 	 	 		 	 	 	 	 	
i   	i   r!   c                     | j                             dd          }|                                  |                    | j        d           |                    | j                   |S )NrL   s   foorM   )rt   rQ   ry   r   r   _cbTestExtendedRequestr   s     r    test_extendedRequestz,OurServerOurClientTests.test_extendedRequest
  s]    K''(>GG	d&///	d1222r!   c                     | j                             dd          }|                                  |                     |t                    S )Ns   testBadRequestr!   )rt   rQ   ry   r   rN   )r   r   r   s      r    r+  z.OurServerOurClientTests._cbTestExtendedRequest  sC    K''(93??!!!%8999r!   c              #     K   | j                             d          }|                                  |V }|                                }|                                  |V  |                                 }d}|                     dt          |                     |                     t          |d         d                    |                     ||d         d                    dS )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      
 K%%c**'',,..%%''@ 	 	CMM***+Xa[-DEEE(1+i"899999r!   c              #   >   K    j                             dt          j        i           }                                  |V }g  fd} j        j                                        \  }||_        ~|                    dd          }                                   	                    t                    d                                |            j                                          j                                          j                                                                                                 j         j                                        |t&                     |                                }                     |t&                     dS )z{
        If there are requests outstanding when the connection
        is closed for any reason, they should fail.
        r   c                                          g            t          j                    }                    |            |S r6   )r   r   Deferredr   )offsetlengthr   gotReadRequestr   s      r    	_slowReadzOOurServerOurClientTests.test_closedConnectionCancelsRequests.<locals>._slowRead9  s?    ^R00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+  s      K  |/DbIIWW 	 	 	 	 	 	 .5577!* LLc""^,,a000A 	++---((***((***.///Q/// KKMMQ/////r!   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 E E
 
 
  <  $  B  2( ( ((  <$ $ $6= = =& & &"= = =
= = =.& & &P= = =
. . .$  0  : : :
 : : :( *0 *0 *0 *0 *0r!   rj   c                       e Zd Zd ZdS )FakeConnc                     d S 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                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	FileTransferCloseTestsc                 ,    t                      | _        d S r6   )r   rB   r   s    r    rh   zFileTransferCloseTests.setUp`  s     llr!   c                     t          j                    } G d d          } |            |_        | j        |j        _        |S )Nc                        e Zd Zd Zd Zd ZdS )DFileTransferCloseTests.buildServerConnection.<locals>.DummyTransportc                     | | _         d S r6   )	transportr   s    r    r   zMFileTransferCloseTests.buildServerConnection.<locals>.DummyTransport.__init__i  s    !%r!   c                     d S r6   r$   )r   kinddatas      r    
sendPacketzOFileTransferCloseTests.buildServerConnection.<locals>.DummyTransport.sendPacketl  s    r!   c                     dS )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  sA        & & &  ) ) ) ) )r!   r[  )r   SSHConnectionrT  rB   )r   connr[  s      r    buildServerConnectionz,FileTransferCloseTests.buildServerConnectionc  sZ    '))	) 	) 	) 	) 	) 	) 	) 	) ()) $r!   c                 @     d _         |j         fd}||_        d S )NFc                 ,    d_          |            d S )NT)connectionLostFired)reasonorigConnectionLostr   s    r    connectionLostzFFileTransferCloseTests.interceptConnectionLost.<locals>.connectionLostz  s"    '+D$v&&&&&r!   )ra  rd  )r   
sftpServerrd  rc  s   `  @r    interceptConnectionLostz.FileTransferCloseTests.interceptConnectionLostv  sC    #( '6	' 	' 	' 	' 	' 	' %3
!!!r!   c                 <    |                      | j        d           d S )Nz*sftpServer's connectionLost was not called)r   ra  r   s    r    assertSFTPConnectionLostz/FileTransferCloseTests.assertSFTPConnectionLost  s+    $&R	
 	
 	
 	
 	
r!   c                 >   t          j        t                      | j                  }|                    t          j        d                     |j        j        j	        }| 
                    |           |                                 |                                  dS )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  s     (hjjMMM 	%%fi&8&8999 '17
 	$$Z000 	!!###%%'''''r!   c                    |                                  }t          j        d          t          j        dd          dz  z   }|                    |           |j        d         }|                    t          j        d                     |j        j	        j
        }|                     |           |                     |           |                    t          j        dd                     |                                  dS )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:
$$Z000 	$$Z000 	v{433444%%'''''r!   c                    |                                  }t          j        d          t          j        dd          dz  z   }|                    |           |j        d         }|                    t          j        d                     |j        j	        j
        }|                     |           |                                 |                                  dS )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:
$$Z000 	%%'''''r!   N)
r0   r1   r2   rh   r^  rf  rh  ro  rz  r}  r$   r!   r    rN  rN  ^  s}        # # #  &3 3 3
 
 

( ( ((( ( (0( ( ( ( (r!   rN  zCannot run without cryptographyc                        e Zd ZdZg dZd ZdS )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 ]i}|                                D ]R}t          j        d|          }|r9t	          |                    d          d          ||                    d          <   Sj|                     t          |          dk    d           |                                D ].\  }}| 	                    |t          t          |                     /dS )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 	? 	?G**,, ? ?HI4PP ?,/

A,>,>Iaggajj)? 		NNQ N	
 	
 	
 OO%% 	: 	:DAqQa 8 89999	: 	:r!   N)r0   r1   r2   __doc__r  r  r$   r!   r    r  r    sC         J J JX: : : : :r!   r  c                       e Zd ZdZd Zd ZdS )RawPacketDataServerTestsz
    Tests for L{filetransfer.FileTransferServer} which explicitly craft
    certain less common situations to exercise their handling.
    c                 R    t          j        t                                | _        d S )NrA   )r   r   r   ftsr   s    r    rh   zRawPacketDataServerTests.setUp9  s    2*,,GGGr!   c                 &   t                      }| j                            |           d}d}t          j        t          dg          |z   t          j        |          z             }| j                            |           t          j        t          dg          |z   t          g d          z   t          j        d          z   t          j        d          z             }t          |                                t          |                     dS )	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#JJi 
 
 	e$$$ 9 3%LL  ,,,  I233	4 IcNN
 
& 	OOX	
 	
 	
 	
 	
r!   N)r0   r1   r2   r  rh   r  r$   r!   r    r  r  1  s?         
H H H1
 1
 1
 1
 1
r!   r  c                   <    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	S )
RawPacketDataTestsz
    Tests for L{filetransfer.FileTransferClient} which explicitly craft certain
    less common protocol messages to exercise their handling.
    c                 6    t          j                    | _        d S r6   )r   rs   ftcr   s    r    rh   zRawPacketDataTests.setUpw  s    244r!   c                 F   t          j                    }|                    | j                   || j        j        d<   t          j        ddt          j	                  t          j        d          z   t          j        d          z   }| 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     N	d.///#$a Kq,"455i i  ! 	
 	t$$$r!   c                 v    |                      |d         d           |                      |d         d           dS )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        d<   t          j        ddt          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  sf     N	d3444#$a {5!\%788t$$$r!   c                 v    |                      |d         d           |                      |d         d           dS )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                    t          j                    }|                    | j                   || j        j        d<   t          j        ddt          j	                  t          j        d          z   }| 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  su     N	d9:::#$a {5!\%78869V;L;LLt$$$r!   c                 v    |                      |d         d           |                      |d         d           dS )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 5 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     sP  
 
 
			 				        * * * * * * * * " " " " " " 1 1 1 1 1 1 4 4 4 4 4 4 & & & & & & % % % % % % , , , , , , + + + + + +++++++ DD    DDD
=      "=  & & &!%&
!(((( !LL    LLL
<<<<<< II    III
	KKKKKKKKKKKKK 	 	 	D	       &E E E E EZ E E E       
 % OOOHMM
 44		   & & & & &$> & & & J!:|/G  
    8   * D455F0 F0 F0 F0 F0l F0 F0 65F0R       
 D455d( d( d( d( d(X d( d( 65d(N L;<<e: e: e: e: e:X e: e: =<e:T D455L;<<:
 :
 :
 :
 :
x :
 :
 =< 65:
z L;<<S) S) S) S) S) S) S) =<S) S) S)sZ   A AAA( (A21A26A= =BBB BB"B/ /B76B7