
    Yf?                        d Z ddlZddlZddlZddlZddlZddlZddl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mZ dd	lmZ dd
lmZmZmZmZmZ ddlm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/m0Z0  e.d          Z1 e.d          Z2e1r9	 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= ddl>m?Z? ddlm@Z@ n# eA$ r Y nw xY wdZBde2e1 ejC        ed          fv rdZB eeBd           G d  d!e0                      ZD G d" d#e0          ZE G d$ d%e!          ZF G d& d'          ZG G d( d)          ZH ee           G d* d+e	                      ZI eeBd           G d, d-e0                      ZJ G d. d/          ZK G d0 d1ejL                  ZM G d2 d3e          ZN eeBd           G d4 d5eN                      ZO eeBd           G d6 d7eN                      ZP eeBd           e e,d8           d9           e e,d:           d;           G d< d=eN                                              ZQdS )>z*
Tests for L{twisted.conch.scripts.cftp}.
    N)BytesIOTextIOWrapper)skipIf)implementer)ls)	ISFTPFile)FileTransferTestAvatarSFTPTestBase)portal)defererror
interfacesprotocolreactor)Clock)StringTransport)getProcessOutputAndValuegetProcessValue)log)UserDatabase)FilePath)which)requireModule)TestCasecryptographyztwisted.conch.unix)cftp)
SSHSession)filetransfer)EXTENDED_DATA_STDERR)
test_conchtest_ssh)	FakeStdio)FileTransferForTestAvatarFTz*don't run w/o spawnProcess or cryptographyc                   *    e Zd ZdZddZddZddZdS )SSHSessionTestsz=
    Tests for L{twisted.conch.scripts.cftp.SSHSession}.
    returnNc                     t                      | _        t                      | _        | j        | j        _        t	                      | _        t          | j                  | _        | j        | j        _        d S N)r"   stdior   channelr   stderrBufferr   stderrselfs    N/var/www/html/env/lib/python3.11/site-packages/twisted/conch/test/test_cftp.pysetUpzSSHSessionTests.setUp>   sT    [[
!||!Z#II#D$566"k    c                 v    | j                                          |                     | j        j                   dS )z|
        L{twisted.conch.scripts.cftp.SSHSession.eofReceived} loses the write
        half of its stdio connection.
        N)r*   eofReceived
assertTruer)   writeConnLostr-   s    r/   test_eofReceivedz SSHSessionTests.test_eofReceivedF   s5    
 	  """
011111r1   c                     d}|                     d          }| j                            t          |dz              |                     | j                                        |dz              dS )z
        L{twisted.conch.scripts.cftp.SSHSession.extReceived} decodes
        stderr data using UTF-8 with the "backslashescape" error handling and
        writes the result to its own stderr.
        u   ☃utf-8   s   \xffN)encoder*   extReceivedr   assertEqualr+   getvalue)r.   	errorText
errorBytess      r/   test_extReceivedStderrz&SSHSessionTests.test_extReceivedStderrN   s     "	%%g..
    	
 	
 	
 	&&((!	
 	
 	
 	
 	
r1   )r&   N)__name__
__module____qualname____doc__r0   r6   r@    r1   r/   r%   r%   8   sZ         * * * *2 2 2 2
 
 
 
 
 
r1   r%   c                   Z   e Zd ZdZ eedd          dZd Zd Zd Z	d Z
d	 Z ej                    Z	 	  ej        ej        d
           dZn# ej        $ r dZY nw xY w ej        ej        e           n#  ej        ej        e           w xY w eed          d             Zd ZdS )ListingTestsz
    Tests for L{lsLine}, the function which generates an entry for a file or
    directory in an SFTP I{ls} command's output.
    tzsetNz8Cannot test timestamp formatting code without time.tzsetc                 ^    d _          fd}                     t          d|           dt          j        v r]                     t          j        t          j        dt          j        d                                         t          j	                   dS d }                     |           dS )zo
        Patch the L{ls} module's time function so the results of L{lsLine} are
        deterministic.
        i[c                       j         S r(   )nowr-   s   r/   fakeTimez$ListingTests.setUp.<locals>.fakeTimep   s
    8Or1   timeTZc                  j    	 t           j        d= n# t          $ r Y nw xY wt          j                     d S )NrN   )osenvironKeyErrorrM   rH   rE   r1   r/   cleanupz#ListingTests.setUp.<locals>.cleanup|   sC    
4((   D
s    
N)
rK   patchr   rP   rQ   
addCleanupoperatorsetitemrM   rH   )r.   rL   rS   s   `  r/   r0   zListingTests.setUpi   s    
 	 	 	 	 	 	

2vx((( 2:OOH,bj$
4@PQQQOODJ'''''   OOG$$$$$r1   c                 p    |t           j        d<   t          j                     t	          j        d|          S )zl
        Call L{ls.lsLine} after setting the timezone to C{timezone} and return
        the result.
        rN   foo)rP   rQ   rM   rH   r   lsLine)r.   timezonestats      r/   _lsInTimezonezListingTests._lsInTimezone   s-     $
4
y%%%r1   c                     | j         dz
  }t          j        dddddddd|df
          }|                     |                     d|          d           |                     |                     d|          d           dS )z
        A file with an mtime six months (approximately) or more in the past has
        a listing including a low-resolution timestamp.
        r   America/New_Yorkz;!---------    0 0        0               0 Apr 26  1973 fooPacific/Aucklandz;!---------    0 0        0               0 Apr 27  1973 fooNrK   rP   stat_resultr<   r]   r.   thenr\   s      r/   test_oldFilezListingTests.test_oldFile        x01~q!Q1aAtQ?@@1488I	
 	
 	
 	1488I	
 	
 	
 	
 	
r1   c                    | j         dz
  dz   }t          j        dddddddd|df
          }|                     |                     d|          d           |                     |                     d|          d           dS )	
        A file with a high-resolution timestamp which falls on a day of the
        month which can be represented by one decimal digit is formatted with
        one padding 0 to preserve the columns which come after it.
        r_   i r   r`   z;!---------    0 0        0               0 May 01  1973 foora   z;!---------    0 0        0               0 May 02  1973 fooNrb   rd   s      r/   test_oldSingleDigitDayOfMonthz*ListingTests.test_oldSingleDigitDayOfMonth        x015EF~q!Q1aAtQ?@@1488I	
 	
 	
 	1488I	
 	
 	
 	
 	
r1   c                     | j         dz
  }t          j        dddddddd|df
          }|                     |                     d|          d           |                     |                     d|          d           dS )z
        A file with an mtime fewer than six months (approximately) in the past
        has a listing including a high-resolution timestamp excluding the year.
        逛z r   r`   ;!---------    0 0        0               0 Aug 28 17:33 foora   ;!---------    0 0        0               0 Aug 29 09:33 fooNrb   rd   s      r/   test_newFilezListingTests.test_newFile   rg   r1   
es_AR.UTF8FTz'The es_AR.UTF8 locale is not installed.c                    | j         dz
  }t          j        dddddddd|df
          }t          j                    }t          j        t          j        d           |                     t          j        t          j        |           |                     | 	                    d|          d           |                     | 	                    d|          d           dS )	zC
        The month name in the date is locale independent.
        rm   r   rq   r`   rn   ra   ro   N)
rK   rP   rc   locale	getlocale	setlocaleLC_ALLrU   r<   r]   )r.   re   r\   currentLocales       r/   test_localeIndependentz#ListingTests.test_localeIndependent   s     x01~q!Q1aAtQ?@@ (**555(&-GGG1488I	
 	
 	
 	1488I	
 	
 	
 	
 	
r1   c                    | j         dz
  dz   }t          j        dddddddd|df
          }|                     |                     d|          d           |                     |                     d|          d           dS )	ri   rm   i F r   r`   z;!---------    0 0        0               0 Sep 01 17:33 foora   z;!---------    0 0        0               0 Sep 02 09:33 fooNrb   rd   s      r/   test_newSingleDigitDayOfMonthz*ListingTests.test_newSingleDigitDayOfMonth   rk   r1   )rA   rB   rC   rD   getattrrM   skipr0   r]   rf   rj   rp   rs   rt   rw   ru   rv   
localeSkipErrorr   rx   rz   rE   r1   r/   rG   rG   `   sY        
 wtWd##+I% % %:	& 	& 	&
 
 
$
 
 
(
 
 
( %F$&&M7	FV]L999 JJ | 	 	 	JJJ	
 	66666666VJABB
 
 CB
,
 
 
 
 
s)   A A8 AA8 AA8 8BrG   c                   "     e Zd ZdZ fdZ xZS )InMemorySSHChannelzp
    Minimal implementation of a L{SSHChannel} like class which only reads and
    writes data from memory.
    c                 d    || _         d| _        t                                                       dS )zt
        @param conn: The SSH connection associated with this channel.
        @type conn: L{SSHConnection}
        r   N)connlocalClosedsuper__init__)r.   r   	__class__s     r/   r   zInMemorySSHChannel.__init__  s0    
 	r1   )rA   rB   rC   rD   r   __classcell__)r   s   @r/   r   r     sB         
        r1   r   c                   $    e Zd ZdZd Zd Zd ZdS )FilesystemAccessExpectationszC
    A test helper used to support expected filesystem access.
    c                     i | _         d S r(   _cacher-   s    r/   r   z%FilesystemAccessExpectations.__init__  s    r1   c                     || j         ||f<   dS )z

        @param path: Path at which the stream is requested.
        @type path: L{str}

        @param path: Flags with which the stream is requested.
        @type path: L{str}

        @param stream: A stream.
        @type stream: C{File}
        Nr   )r.   pathflagsstreams       r/   putz FilesystemAccessExpectations.put  s     &,T5M"""r1   c                 :    | j                             ||f          S )a  
        Remove a stream from the memory.

        @param path: Path at which the stream is requested.
        @type path: L{str}

        @param path: Flags with which the stream is requested.
        @type path: L{str}

        @return: A stream.
        @rtype: C{File}
        )r   pop)r.   r   r   s      r/   r   z FilesystemAccessExpectations.pop(  s     {e}---r1   N)rA   rB   rC   rD   r   r   r   rE   r1   r/   r   r     sK           , , ,. . . . .r1   r   c                       e Zd ZdZd Zd ZdS )InMemorySFTPClienta'  
    A L{filetransfer.FileTransferClient} which does filesystem operations in
    memory, without touching the local disc or the network interface.

    @ivar _availableFiles: File like objects which are available to the SFTP
        client.
    @type _availableFiles: L{FilesystemRegister}
    c                 P    t          |           | _        ddd| _        || _        d S )N   
   )requests
buffersize)r   	transportoptions_availableFiles)r.   availableFiless     r/   r   zInMemorySFTPClient.__init__B  s5    +D11
 
  .r1   c                 8    | j                             ||          S )z}
        @see: L{filetransfer.FileTransferClient.openFile}.

        Retrieve and remove cached file based on flags.
        )r   r   )r.   filenamer   attrss       r/   openFilezInMemorySFTPClient.openFileJ  s     #''%888r1   N)rA   rB   rC   rD   r   r   rE   r1   r/   r   r   8  s<         . . .9 9 9 9 9r1   r   c                   <    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	S )
InMemoryRemoteFilez;
    An L{ISFTPFile} which handles all data in memory.
    c                 <    || _         t          j        |            dS )zL
        @param name: Name of this file.
        @type name: L{str}
        N)namer   r   )r.   r   s     r/   r   zInMemoryRemoteFile.__init__Y  s"    
 	r1   c                 ~    |                      |           |                     |           t          j        |           S )z/
        @see: L{ISFTPFile.writeChunk}
        )seekwriter   succeed)r.   startdatas      r/   
writeChunkzInMemoryRemoteFile.writeChunka  s8     			%

4}T"""r1   c                     d| _         dS )zo
        @see: L{ISFTPFile.writeChunk}

        Keeps data after file was closed to help with testing.
        TN)_closedr-   s    r/   closezInMemoryRemoteFile.closei  s     r1   c                     d S r(   rE   r-   s    r/   getAttrszInMemoryRemoteFile.getAttrsq      r1   c                     d S r(   rE   )r.   offsetlengths      r/   	readChunkzInMemoryRemoteFile.readChunku  r   r1   c                     d S r(   rE   )r.   r   s     r/   setAttrszInMemoryRemoteFile.setAttrsy  r   r1   c                 *    t          j        |           S )zb
        Get current data of file.

        Allow reading data event when file is closed.
        )r   r=   r-   s    r/   r=   zInMemoryRemoteFile.getvalue}  s     %%%r1   N)rA   rB   rC   rD   r   r   r   r   r   r   r=   rE   r1   r/   r   r   S  s           # # #        & & & & &r1   r   c                       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dZddZd Zd Zd Zd ZdS )StdioClientTestsz(
    Tests for L{cftp.StdioClient}.
    c                 >   t                      | _        t          | j                  }t          j        |          | _        d| j        _        t                      x| _        | j        _	        | 
                    dd           | j        j        j        | j        _        dS )zm
        Create a L{cftp.StdioClient} hooked up to dummy transport and a fake
        user database.
        /i     N)r   fakeFilesystemr   r   StdioClientclientcurrentDirectoryr   database_pwdsetKnownConsoleSizer   )r.   
sftpClients     r/   r0   zStdioClientTests.setUp  s    
 ;<<'(;<<
&z22'*$+7>>9(
 	  b))) !% 2 <r1   c           	         | j                             t          j                    dt	          j                    dddt          j                   | j        	                    d          }|
                    | j        d           |S )zs
        The I{exec} command runs its arguments locally in a child process
        using the user's shell.
        secret  rY   barzexec print(1 + 2)s   3
)r   addUsergetpassgetuserrP   getuidsys
executabler   _dispatchCommandaddCallbackr<   r.   ds     r/   	test_execzStdioClientTests.test_exec  sq    
 	OxdE5#.	
 	
 	
 K(()<==	d&///r1   c           	          | j                             t          j                    dt	          j                    dddd           | j                            d          }|                    | j	        d           |S )zr
        If the local user has no shell, the I{exec} command runs its arguments
        using I{/bin/sh}.
        r   r   rY   r    zexec echo hello   hello

r   r   r   r   rP   r   r   r   r   r<   r   s     r/   test_execWithoutShellz&StdioClientTests.test_execWithoutShell  so    
 	OxdE5"	
 	
 	
 K(():;;	d&
333r1   c           	          | j                             t          j                    dt	          j                    dddd           | j                            d          }|                    | j	        d           |S )zO
        The I{exec} command is run for lines which start with C{"!"}.
        r   r   rY   r   z/bin/shz!echo hellor   r   r   s     r/   	test_bangzStdioClientTests.test_bang  sn     	OxdE5)	
 	
 	
 K((77	d&
333r1   c                 ~    ddl  G fdd          }|                     t          d |                       dS )a  
        For the duration of this test, patch C{cftp}'s C{fcntl} module to return
        a fixed width and height.

        @param width: the width in characters
        @type width: L{int}
        @param height: the height in characters
        @type height: L{int}
        r   Nc                       e Zd Z fdZdS )7StdioClientTests.setKnownConsoleSize.<locals>.FakeFcntlc                 t    |j         k    r|                     d           t          j        ddd          S )Nz#Only window-size queries supported.4Hr   )
TIOCGWINSZfailstructpack)r.   fdoptmutateheightttywidths       r/   ioctlz=StdioClientTests.setKnownConsoleSize.<locals>.FakeFcntl.ioctl  s;    #.((IICDDD{41===r1   N)rA   rB   rC   r   )r   r   r   s   r/   	FakeFcntlr     s8        > > > > > > > > >r1   r   fcntl)r   rT   r   )r.   r   r   r   r   s    `` @r/   r   z$StdioClientTests.setKnownConsoleSize  sp     	


	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	

4))++.....r1   c                    |                      dd           t                      x}| j        _        t	          d          }d|_        t          j        |          }d|_        |	                                }|
                    d           |xj        dz  c_        | j                            ||           d}|                     | j        j                                        |           d	S )
a(  
        L{StdioClient._printProgressBar} prints a progress description,
        including percent done, amount transferred, transfer rate, and time
        remaining, all based the given start time, the given L{FileWrapper}'s
        progress information and the reactor's current time.
        r   "      x   samplei (  g       @i   s#   b'sample' 40% 4.0kB 2.0kBps 00:03 N)r   r   r   r   r   r   r   FileWrappersizesecondsadvancetotal_printProgressBarr<   r   valuer.   clockwrappedwrapper	startTimeresults         r/   test_printProgressBarReportingz/StdioClientTests.test_printProgressBarReporting  s     	  R(((&+gg-#$-- "7++ MMOO	c%%gy9998.4466?????r1   c                    |                      dd           t                      x}| j        _        t	          d          }d|_        t          j        |          }|                                }| j        	                    ||           d}| 
                    | j        j                                        |           dS )z
        L{StdioClient._printProgressBar} prints a progress description that
        indicates 0 bytes transferred if no bytes have been transferred and no
        time has passed.
        r   r   r   r   s!   b'sample'  0% 0.0B 0.0Bps 00:00 N)r   r   r   r   r   r   r   r   r   r   r<   r   r   r   s         r/   test_printProgressBarNoProgressz0StdioClientTests.test_printProgressBarNoProgress  s     	  R(((&+gg-#$-- "7++MMOO	%%gy9996.4466?????r1   c                 "   |                      dd           t                      }d|_        t          j        |          }| j                            |d           d}|                     || j        j        	                                           dS )z5
        Print the progress for empty files.
        r   r   s
   empty-filer   s%   b'empty-file'100% 0.0B 0.0Bps 00:00 N)
r   r   r   r   r   r   r   r<   r   r   )r.   r   r   r   s       r/   test_printProgressBarEmptyFilez/StdioClientTests.test_printProgressBarEmptyFile  s     	  R((())$"7++%%gq111:!6!<!<!>!>?????r1   c                 f    | j                             d          }|                     d|           dS )zK
        Returns empty value for both filename and remaining data.
        z  )r   r   Nr   _getFilenamer<   r.   r   s     r/   test_getFilenameEmptyz&StdioClientTests.test_getFilenameEmpty  s5     ))$//6*****r1   c                 f    | j                             d          }|                     d|           dS )zd
        Returns empty value for remaining data when line contains
        only a filename.
        
only-local)r  r   Nr  r	  s     r/   test_getFilenameOnlyLocalz*StdioClientTests.test_getFilenameOnlyLocal  s6    
 )),77+V44444r1   c                 f    | j                             d          }|                     d|           dS )ze
        Returns filename and remaining data striped of leading and trailing
        spaces.
        z local  remote file  )localzremote fileNr  r	  s     r/   test_getFilenameNotQuotedz*StdioClientTests.test_getFilenameNotQuoted#  s7    
 ))*ABB16:::::r1   c                 f    | j                             d          }|                     d|           dS )z
        Returns filename and remaining data not striped of leading and trailing
        spaces when quoted paths are requested.
        z" " local file "  " remote  file " )z local file z" remote  file "Nr  r	  s     r/   test_getFilenameQuotedz'StdioClientTests.test_getFilenameQuoted,  s7    
 ))*NOO=vFFFFFr1   Nr1   c                     ||                                  }t          |d          5 }|                    |           ddd           n# 1 swxY w Y   |S )ab  
        Create a local file and return its path.

        When `path` is L{None}, it will create a new temporary file.

        @param path: Optional path for the new file.
        @type path: L{str}

        @param content: Content to be written in the new file.
        @type content: L{bytes}

        @return: Path to the newly create file.
        Nwb)mktempopenr   )r.   r   contentfiles       r/   makeFilezStdioClientTests.makeFile5  s     <;;==D$ 	 JJw	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   A		AAFc                    | j         j                                        }|                    d          }|                    d          }g }g }|D ]\  }}}g }	|D ]}
|	                    | d|
            |	                    d| d|            |                    |	           |                    d                              d                              d          }|dd	         }|d	                             d
                              d
          }|                    |           g }|dd	         D ]}
|
                                	                    dd          d         }
|
                                                    dd          }
|                    |
d          d|
d                                                     |                    |d	                    |                    |           |r1| 
                    t          |          t          |                     n| 
                    ||           | 
                    dt          |          d           dS )a  
        Check output of cftp client for a put request.


        @param transfers: List with tuple of (local, remote, progress).
        @param randomOrder: When set to C{True}, it will ignore the order
            in which put reposes are received

        r8   z
 zTransferred z to r   N
   r   z5There are still put responses which were not checked.)r   r   r   decodesplitappendr   stripextendrsplitr<   sortedlen)r.   	transfersrandomOrderoutputexpectedOutputactualOutputr  remoteexpectedexpectedTransferlineprogressPartsactuallastactualTransfers                  r/   checkPutMessagez StdioClientTests.checkPutMessageI  s    &,,..w''f%%'0 	0 	0#E68  "  ; ; ''5(9(94(9(9::::##$F5$F$Ff$F$FGGG!!"2333"JJqMM//55;;DAAM"3B3'F $**40066t<<DMM$N ss F F zz||**32215 zz||))#q11%%a&D&D47==??&D&DEEEE!!&*---//// 	;VN33VL5I5IJJJJ^\:::KKC	
 	
 	
 	
 	
r1   c                    d}|                      |          }t          j        t          j        z  t          j        z  }t
          j                            dt
          j                            |                    }t          |          }| j
                            ||t          j        |                     d| j        j        j        d<   | j                            |          }|                     |           |                     ||                                           |                     |j                   |                     ||g dfg           dS )z
        A name based on local path is used when remote path is not
        provided.

        The progress is updated while chunks are transferred.
        s   Test
Content)r  r   r   r   )z	76% 10.0B
100% 13.0Br7  N)r  r   	FXF_WRITE	FXF_CREAT	FXF_TRUNCrP   r   joinbasenamer   r   r   r   r   r   r   cmd_PUTsuccessResultOfr<   r=   r4   r   r5  )r.   r  	localPathr   
remoteName
remoteFiledeferreds          r/   test_cmd_PUTSingleNoRemotePathz/StdioClientTests.test_cmd_PUTSingleNoRemotePath  s3    %MM'M22	&)??,BXXW\\#rw'7'7	'B'BCC
'
33

E5=3L3LMMM35"<0;&&y11X&&&*"5"5"7"7888
*+++%N%N%NOP	
 	
 	
 	
 	
r1   c                    |                                  }t          j        t          j        z  t          j        z  }d}t          |          }| j                            ||t          j	        |                     | j
                            | d| d          }|                     |           |                     ||dgfg           |                     |j                   |                     d|                                           dS )z
        Remote path is extracted from first filename after local file.

        Any other data in the line is ignored.
        z/remote-pathr  z ignored	100% 0.0Br1   N)r  r   r8  r9  r:  r   r   r   r   r   r   r=  r>  r5  r4   r   r<   r=   )r.   r?  r   r@  rA  rB  s         r/   test_cmd_PUTSingleRemotePathz-StdioClientTests.test_cmd_PUTSingleRemotePath  s     MMOO	&)??,BXX#
'
33

E5=3L3LMMM;&&)'J'Jj'J'J'JKKX&&&y*{mDEFFF
*+++j113344444r1   c                 N   |                                  }t          j                            |          }d}t          j                            |          }|                      t          j                            ||                    }t          j        t          j        z  t          j	        z  }d| }d| }t          |          }	t          |          }
| j                            ||t          j        |	                     | j                            ||t          j        |
                     | j                            t          j                            |d                    }|                     |           |                     |	j                   |                     d|	                                           |                     |
j                   |                     d|
                                           |                     ||dgf||dgfgd           d	S )
z
        When a gobbing expression is used local files are transferred with
        remote file names based on local names.
        second-namer   r   *r1   rE  Tr)  N)r  rP   r   r<  dirnamer;  r   r8  r9  r:  r   r   r   r   r   r   r=  r>  r4   r   r<   r=   r5  )r.   first	firstName
secondNameparentsecondr   firstRemotePathsecondRemotePathfirstRemoteFilesecondRemoteFilerB  s               r/    test_cmd_PUTMultipleNoRemotePathz1StdioClientTests.test_cmd_PUTMultipleNoRemotePath  s   
 G$$U++	"
''BGLL$D$DEE&)??,BXX)i//+z++,_==-.>??o8V8VWWWeU]3C%D%D	
 	
 	
 ;&&rw||FC'@'@AAX&&&/000o6688999(0111.7799:::+7)K=9  	 	
 	
 	
 	
 	
r1   c                 t   |                                  }t          j                            |          }d}t          j                            |          }|                      t          j                            ||                    }t          j        t          j        z  t          j	        z  }t          |          }t          |          }d| }	d| }
| j                            |	|t          j        |                     | j                            |
|t          j        |                     | j                            d                    t          j                            |d                              }|                     |           |                     |j                   |                     d|                                           |                     |j                   |                     d|                                           |                     ||dgf||dgfgd	           d
S )z
        When a gobbing expression is used local files are transferred with
        remote file names based on local names.
        when a remote folder is requested remote paths are composed from
        remote path and local filename.
        rH  rI  z/remote/z	{} remoterJ  r1   rE  TrK  N)r  rP   r   r<  rL  r;  r   r8  r9  r:  r   r   r   r   r   r   r=  formatr>  r4   r   r<   r=   r5  )r.   rM  rN  rO  rP  rQ  r   rT  rU  rR  rS  rB  s               r/   "test_cmd_PUTMultipleWithRemotePathz3StdioClientTests.test_cmd_PUTMultipleWithRemotePath  s    G$$U++	"
''BGLL$D$DEE&)??,BXX,Y77-j990Y002j22o8V8VWWWeU]3C%D%D	
 	
 	
 ;&&{'9'9"',,vs:S:S'T'TUUX&&&/000o6688999(0111.7799:::	K=1k]3  	 	
 	
 	
 	
 	
r1   )Nr1   )F)rA   rB   rC   rD   r0   r   r   r   r   r  r  r  r
  r  r  r  r  r5  rC  rF  rV  rY  rE   r1   r/   r   r     sD        = = =&    
 
 
/ / /*@ @ @0@ @ @$@ @ @+ + +5 5 5; ; ;G G G   (;
 ;
 ;
 ;
z
 
 
05 5 5&!
 !
 !
F#
 #
 #
 #
 #
r1   r   c                       e Zd Zd Zd ZdS )FileTransferTestRealmc                     || _         d S r(   )testDir)r.   r]  s     r/   r   zFileTransferTestRealm.__init__  s    r1   c                 B    t          | j                  }|d         |d fS )Nr   c                      d S r(   rE   rE   r1   r/   <lambda>z5FileTransferTestRealm.requestAvatar.<locals>.<lambda>   s     r1   )r	   r]  )r.   avatarIDmindr   as        r/   requestAvatarz#FileTransferTestRealm.requestAvatar  s$    "4<00!}a--r1   N)rA   rB   rC   r   rd  rE   r1   r/   r[  r[    s2          . . . . .r1   r[  c                   N    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S )SFTPTestProcessz
    Protocol for testing cftp. Provides an interface between Python (where all
    the tests are) and the cftp client process (which does the work that is
    being tested).
    c                 f    |                                   || _        d| _        d| _        d| _        dS )zr
        @param onOutReceived: A L{Deferred} to be fired as soon as data is
        received from stdout.
        NF)clearBufferonOutReceivedonProcessEnd_expectingCommand_processEnded)r.   ri  s     r/   r   zSFTPTestProcess.__init__
  s<    
 	* !%"r1   c                 0    d| _         g | _        d| _        dS )zR
        Clear any buffered data received from stdout. Should be private.
        r1   N)buffer_linesReceived_lineBufferr-   s    r/   rh  zSFTPTestProcess.clearBuffer  s!      r1   c                 t   t          j        d|z             | j        |z                       d          }|                    d          | _        | j                            |           | j        $| j        dc}| _        |                    |           | xj	        |z  c_	        | 
                                 dS )zO
        Called by Twisted when the cftp client prints data to stdout.
        zgot %r   
r  N)r   msgrp  r!  r   ro  r$  ri  callbackrn  _checkForCommand)r.   r   linesr   s       r/   outReceivedzSFTPTestProcess.outReceived  s     	4   !D(//66 99R==""5))) )$($6!At!JJttr1   c                 2   d}| j         r| j        |k    rd                    | j                  }|                    |          r|t          |          d          }|                                  | j         d c}| _         |                    |           d S d S d S )Ns   cftp> rr  )rk  rp  r;  ro  
startswithr'  rh  rt  )r.   promptbufr   s       r/   ru  z SFTPTestProcess._checkForCommand.  s    ! 	d&6&&@&@**T011C~~f%% )#f++--((,(>%At%JJsOOOOO	 	&@&@r1   c                 4    t          j        d|z             dS )zO
        Called by Twisted when the cftp client prints data to stderr.
        zerr: %sN)r   rs  )r.   r   s     r/   errReceivedzSFTPTestProcess.errReceived8  s     		D !!!!!r1   c                     | j         S )zQ
        Return the contents of the buffer of data received from stdout.
        )rn  r-   s    r/   	getBufferzSFTPTestProcess.getBuffer>  s     {r1   c                     t          j                    | _        |                                  t	          |t
                    r|                    d          }| j                            |dz              | j        S )a  
        Issue the given command via the cftp client. Return a C{Deferred} that
        fires when the server returns a result. Note that the C{Deferred} will
        callback even if the server returns some kind of error.

        @param command: A string containing an sftp command.

        @return: A C{Deferred} that fires when the sftp server returns a
        result. The payload is the server's response string.
        r8   rr  )	r   Deferredrk  rh  
isinstancestrr:   r   r   r.   commands     r/   
runCommandzSFTPTestProcess.runCommandD  sl     "'!1!1gs## 	.nnW--GWu_---%%r1   c                 t     t          j        d           fd|D             }t          j        |          S )ax  
        Run each command in sequence and return a Deferred that fires when all
        commands are completed.

        @param commands: A list of strings containing sftp commands.

        @return: A C{Deferred} that fires when all commands are completed. The
        payload is a list of response strings from the server, in the same
        order as the commands.
        r   c                 F    g | ]}                     j        |          S rE   )runr  ).0r  r.   sems     r/   
<listcomp>z-SFTPTestProcess.runScript.<locals>.<listcomp>b  s)    HHHGcggdow//HHHr1   )r   DeferredSemaphoregatherResults)r.   commandsdlr  s   `  @r/   	runScriptzSFTPTestProcess.runScriptV  sC     %a((HHHHHxHHH"2&&&r1   c                     | j         rt          j        d          S t          j                    | _        | j                            d           | j        S )z
        Kill the process if it is still running.

        If the process is still running, sends a KILL signal to the transport
        and returns a C{Deferred} which fires when L{processEnded} is called.

        @return: a C{Deferred}.
        NKILL)rl  r   r   r  rj  r   signalProcessr-   s    r/   killProcesszSFTPTestProcess.killProcesse  sN      	'=&&&!N,,$$V,,,  r1   c                 n    d| _         | j        r&| j        dc}| _        |                    d           dS dS )zF
        Called by Twisted when the cftp client process ends.
        TN)rl  rj  rt  )r.   reasonr   s      r/   processEndedzSFTPTestProcess.processEndedt  sK     " 	#'#4d At JJt	 	r1   N)rA   rB   rC   rD   r   rh  rw  ru  r}  r  r  r  r  r  rE   r1   r/   rf  rf    s         	# 	# 	#       "  " " "  & & &$' ' '! ! !    r1   rf  c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )CFTPClientTestBasec                    t          dd          5 }|                    t          j                   d d d            n# 1 swxY w Y   t          dd          5 }|                    t          j                   d d d            n# 1 swxY w Y   t          j        dd           t          dd          5 }|                    dt          j        z              d d d            n# 1 swxY w Y   t          j	        |           S )Ndsa_test.pubr  dsa_testi  kh_tests
   127.0.0.1 )
r  r   r!   publicDSA_opensshprivateDSA_opensshrP   chmodpublicRSA_opensshr
   r0   r.   fs     r/   r0   zCFTPClientTestBase.setUp  s   .$'' 	01GGH.///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0*d## 	1qGGH/000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1
U###)T"" 	@aGGMH$>>???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@!$'''s3    =AA BBB4#C##C'*C'c                    t          | j                  }t          j        |          }|                    t          j                               t          j                    }||_        t          j	        d|d          | _
        d S )Nr   z	127.0.0.1)	interface)r[  r]  r   PortalregisterCheckerr!   conchTestPublicKeyCheckerConchTestServerFactoryr   	listenTCPserver)r.   realmpfacs       r/   startServerzCFTPClientTestBase.startServer  so    %dl33M%  	(<>>???-//
'3+FFFr1   c                    t          | j        j        d          s|                     d           S d| j        j        j        _        t          j        | j        j        j        j        j	                  }|
                    | j                   |S )Nprotor   )hasattrr  factory_cbStopServerr  expectedLoseConnectionr   maybeDeferredr   loseConnectionr   r   s     r/   
stopServerzCFTPClientTestBase.stopServer  su    t{*G44 	,%%d+++;<!8 3 9 C RSS	d()))r1   c                 >    t          j        | j        j                  S r(   )r   r  r  stopListeningr.   ignoreds     r/   r  z CFTPClientTestBase._cbStopServer  s    "4;#<===r1   c                 ~    dD ]'}	 t          j        |           # t          $ r Y $w xY wt          j        |           S )N)r  r  r  )rP   removeBaseExceptionr
   tearDownr  s     r/   r  zCFTPClientTestBase.tearDown  sV    8 	 	A	!    $T***s   
((N)rA   rB   rC   r0   r  r  r  r  rE   r1   r/   r  r  ~  sb        ( ( (G G G  > > >+ + + + +r1   r  c                       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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S )OurServerCmdLineClientTestsz
    Functional tests which launch a SFTP server over TCP on localhost and check
    cftp command line interface using a spawned process.

    Due to the spawned process you can not add a debugger breakpoint for the
    client code.
    c                 N    t                                                                             d} j                                        j        }t          j        ||z                                  d          }t          j
        dt          j         d|            t          j                    }t          |           _        |                     fd           t$          j                                        }t$          j                            t          j                  |d<   g }i }|D ]A}t1          |t2                    r|                    d          }|                    |           B|D ]c}||         }	t1          |t2                    r|                    d          }t1          |	t2                    r|	                    d          }	|	||<   dt          j
        |           t          j
        |           t9          j         j        t          j        ||	           |S )
Nz-p %i -l testuser --known-hosts kh_test --user-authentications publickey --host-key-algorithms ssh-rsa -i dsa_test -a -v 127.0.0.1r   modrunning r  c                 6    j                                         S r(   )processProtocolrh  _r.   s    r/   r`  z3OurServerCmdLineClientTests.setUp.<locals>.<lambda>       4 @ @ B B r1   
PYTHONPATHr8   env)r  r0   r  r  getHostportr    	_makeArgsr!  r   rs  r   r   r   r  rf  r  r   rP   rQ   copypathsepr;  r   r  r  r:   r"  r   spawnProcess)
r.   cmdsr  r   r  encodedCmds
encodedEnvcmdvarvals
   `         r/   r0   z!OurServerCmdLineClientTests.setUp  s     &&& 	 {""$$)#TD[$7$7$9$9vFFF23>22D22333N.q11	BBBBCCCjooJOOCH55L
 	$ 	$C#s## *jj))s#### 	" 	"Cc(C#s## *jj))#s## *jj))!JsOO
 #.+:	
 	
 	
 	
 r1   c                 `                                       }|                     fd           |S )Nc                 6    j                                         S r(   )r  r  r  s    r/   r`  z6OurServerCmdLineClientTests.tearDown.<locals>.<lambda>  r  r1   )r  r   r   s   ` r/   r  z$OurServerCmdLineClientTests.tearDown  s3    OO	BBBBCCCr1   c                 r    	 | j         j                            d           d S # t          j        $ r Y d S w xY w)Nr  )r  r   r  r   ProcessExitedAlreadyr  s     r/   _killProcessz(OurServerCmdLineClientTests._killProcess  sL    	 *88@@@@@) 	 	 	DD	s   # 66c                 6    | j                             |          S )z
        Run the given command with the cftp client. Return a C{Deferred} that
        fires when the command is complete. Payload is the server's output for
        that command.
        )r  r  r  s     r/   r  z&OurServerCmdLineClientTests.runCommand  s     #..w777r1   c                 6    | j                             |          S )z
        Run the given commands with the cftp client. Returns a C{Deferred}
        that fires when the commands are all complete. The C{Deferred}'s
        payload is a list of output for each command.
        )r  r  )r.   r  s     r/   r  z%OurServerCmdLineClientTests.runScript  s     #--h777r1   c                     | j         }|                     ddddd          }d }|                    |           |                    | j        |j        t          j                    d|j        g           |S )z
        Test that 'pwd' reports the current remote directory, that 'lpwd'
        reports the current local directory, and that changing to a
        subdirectory then changing to its parent leaves you in the original
        remote directory.
        pwdlpwdzcd testDirectorycd ..c                     g }| D ]A}t          |t                    r|                    d          }|                    |           B|dd         |dd         z   S )zH
            Callback function for handling command output.
            r8   N      )r  bytesr   r"  )r*  r  r  s      r/   	cmdOutputz8OurServerCmdLineClientTests.testCdPwd.<locals>.cmdOutput  sk     D ! !c5)) .**W--CC    8d122h&&r1   r   )r]  r  r   r<   r   rP   getcwd)r.   homeDirr   r  s       r/   	testCdPwdz%OurServerCmdLineClientTests.testCdPwd  sw     ,NN5&*<guMM		' 		' 		' 	
i   	d&ry{{B(UVVVr1   c                 h      fd}                      dddd          }|                    |          S )z
        Check that 'ls -l' output includes the access permissions and that
        this output changes appropriately with 'chmod'.
        c                 f                                                         | d                             d                                         | d         d                               | d                             d          | d                                        | d         d           d S )Nr   s
   -rw-r--r--r   r1   r  s
   ----------r  )flushLoggedErrorsr4   ry  r<   resultsr.   s    r/   _checkz7OurServerCmdLineClientTests.testChAttrs.<locals>._check  s    ""$$$OOGAJ11-@@AAAWQZ---OOGAJ11-@@'!*MMMWQZ-----r1   zls -l testfile1zchmod 0 testfile1zchmod 644 testfile1r  r   r.   r  r   s   `  r/   testChAttrsz'OurServerCmdLineClientTests.testChAttrs  sQ    	. 	. 	. 	. 	. NN!	
 
 }}V$$$r1   c                       fd}                      dd j                                        z   ddd          }|                    d            |                    |          S )z
        Check 'ls' works as expected. Checks for wildcards, hidden files,
        listing directories and listing empty directories.
        c                 2                        | d         g d                                | d         g d                                | d         ddg                                | d         g d                                | d	         d
g           d S )Nr   s   testDirectory   testRemoveFile   testRenameFiles	   testfile1r   r  r  r  r  )s   .testHiddenFiler  r  r  r1   r<   r  s    r/   r  z4OurServerCmdLineClientTests.testList.<locals>._check*  s    
VVV   
VVV   WQZ*;=N)OPPP
VVV   WQZ#/////r1   r   zls ../zls *Filezls -a *Filezls -l testDirectoryc                     d | D             S )Nc                 8    g | ]}|                     d           S )rr  )r!  )r  xs     r/   r  zJOurServerCmdLineClientTests.testList.<locals>.<lambda>.<locals>.<listcomp>@  s"    !=!=!=Q!''%..!=!=!=r1   rE   )xss    r/   r`  z6OurServerCmdLineClientTests.testList.<locals>.<lambda>@  s    !=!="!=!=!= r1   )r  r]  r<  r   r  s   `  r/   testListz$OurServerCmdLineClientTests.testList$  s}    	0 	0 	0 	0 	0 NNt|,,...!
 
 	
==>>>}}V$$$r1   c                 ,   |                      d          }t          j        d                              d                                          }t          |t                    r|                    d          }|                    | j	        |           |S )zB
        Check that running the '?' command returns help.
        ?Nr   r8   )
r  r   r   cmd_HELPr#  r  r  r:   r   r<   )r.   r   helpTexts      r/   testHelpz$OurServerCmdLineClientTests.testHelpC  s~     OOC  #D))22266<<>>h$$ 	0w//H	d&111r1   Nc                 |    |                      |                                |                                |           dS )zg
        Assert that the files at C{name1} and C{name2} contain exactly the
        same data.
        N)r<   
getContent)r.   name1name2rs  s       r/   assertFilesEqualz,OurServerCmdLineClientTests.assertFilesEqualO  s:    
 	))++U-=-=-?-?EEEEEr1   c                 ^    d                      j        j         j        j                  t          t                    r                    d           fd}                     d j        j         d          }|                    |           |                     fd           |S )z
        Test that 'get' saves the remote file to the correct local location,
        that the output of 'get' is correct and that 'rm' actually removes
        the file.
        z)Transferred {}/testfile1 to {}/test file2r8   c                                         |                                                              j                            d          j                            d          d                               d          S )N	testfile1
test file2z
get failedzrm "test file2")r4   endswithr  r]  childr  r   r+  r.   s    r/   	_checkGetz6OurServerCmdLineClientTests.testGet.<locals>._checkGetd  sw    OOFOON;;<<<!!"";//""<00  
 ??#4555r1   zget testfile1 "z/test file2"c                                          j                            d                                                    S )Nr  assertFalser]  r  existsr  s    r/   r`  z5OurServerCmdLineClientTests.testGet.<locals>.<lambda>p  1    d&&t|'9'9,'G'G'N'N'P'PQQ r1   )rX  r]  r   r  r  r:   r  r   )r.   r  r   r+  s   `  @r/   testGetz#OurServerCmdLineClientTests.testGetV  s     EKKLL
 
 nc** 	<+227;;N	6 	6 	6 	6 	6 	6 OOMdl.?MMMNN	i   	QQQQ	
 	
 	
 r1   c                 b      fd}                      d          }|                    |          S )zQ
        Test that 'get' works correctly when given wildcard parameters.
        c                                          j                            d          t          d          d                                j                            d          t          d          d           d S )NtestRemoveFiletestRemoveFile get failedtestRenameFiletestRenameFile get failed)r  r]  r  r   )r  r.   s    r/   r  z;OurServerCmdLineClientTests.testWildcardGet.<locals>._checky  s    !!""#344)**+  
 !!""#344)**+    r1   z
get testR*)r  r   r  s   `  r/   testWildcardGetz+OurServerCmdLineClientTests.testWildcardGett  sA    

	 
	 
	 
	 
	 OOL))}}V$$$r1   c                 B    d j                                         j        z   dz    j                                         j        z   dz    fd}                     d j         j         d          }|                    |           |                     fd           |S )z
        Check that 'put' uploads files correctly and that they can be
        successfully removed. Also check the output of the put command.
        s   Transferred s   /testfile1 to s   /test"file2c                 
                        j                            d          j                            d                                         |                                                              d          S )Nr  
test"file2zrm "test\"file2")r  r]  r  r4   r  r  r  s    r/   	_checkPutz6OurServerCmdLineClientTests.testPut.<locals>._checkPut  ss    !!"";//1C1CL1Q1Q   OOFOON;;<<<??#6777r1   put z/testfile1 "test\"file2"c                                          j                            d                                                    S )Nr$  r  r  s    r/   r`  z5OurServerCmdLineClientTests.testPut.<locals>.<lambda>  r  r1   )r]  asBytesModer   r  r   )r.   r%  r   r+  s   `  @r/   testPutz#OurServerCmdLineClientTests.testPut  s     l&&((-.  l&&((-. 	 		8 	8 	8 	8 	8 	8 OOO4<#4OOOPP	i   	QQQQ	
 	
 	
 r1   c                      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    fd}                     d	 j         j         d
          }|                    |           |S )zb
        Check that 'put' uploads files correctly when overwriting a longer
        file.
        shorterFilewmode   aN
longerFile   bbc                                          j                            d          j                            d                     d S )Nr+  r0  )r  r]  r  )r   r.   s    r/   r%  zEOurServerCmdLineClientTests.test_putOverLongerFile.<locals>._checkPut  sJ    !!""=114<3E3El3S3S    r1   r&  z/shorterFile longerFile)r]  r  r  r   r  r   r   )r.   r  r%  r   s   `   r/   test_putOverLongerFilez2OurServerCmdLineClientTests.test_putOverLongerFile  si    \..333== 	GGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	\--222<< 	GGENNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 		 	 	 	 	
 OOM4<#4MMMNN	i   s#   AAAB--B14B1c                 D     j                             d                                                               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    fd}                     d	 j         j         d
          }|                    |           |S )z
        Check that 'put' uploads files correctly when overwriting a longer
        file and you use a wildcard to specify the files to upload.
        dirr  r,  r-  r/  Nr1  c                                                              d          j                            d                     d S )Nr  )r  r  r]  )r   r.   someDirs    r/   r%  zMOurServerCmdLineClientTests.test_putMultipleOverLongerFile.<locals>._checkPut  s;    !!'--"7"79K9KF9S9STTTTTr1   r&  z/dir/*)r]  r  createDirectoryr  r   r  r   r   )r.   r  r%  r   r7  s   `   @r/   test_putMultipleOverLongerFilez:OurServerCmdLineClientTests.test_putMultipleOverLongerFile  s    ,$$U++!!!]]6""''S'11 	QGGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	\'',,#,66 	!GGENNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 		U 	U 	U 	U 	U 	U OO<4<#4<<<==	i   s$   A<<B B 5CCCc                       fd}                      dd j        j         dd j                                        z            }|                    |           |S )z
        What happens if you issue a 'put' command and include a wildcard (i.e.
        '*') in parameter? Check that all files matching the wildcard are
        uploaded to the correct directory.
        c                                         | d         d                                | d         d                               j                            d          j                                                            d          d                               j                            d          j                                                            d          d           d S )Nr   r1   r  r  r  r  r   )r<   r  r]  r  rP  r  s    r/   checkz:OurServerCmdLineClientTests.testWildcardPut.<locals>.check  s    WQZ---WQZ---!!""#344##%%++,<==+  
 !!""#344##%%++,<==+    r1   r  r&  z/testR*zcd %s)r  r]  r   r<  r   )r.   r<  r   s   `  r/   testWildcardPutz+OurServerCmdLineClientTests.testWildcardPut  sv    	 	 	 	 	 NN-4<$---dl++---
 

 	
er1   c                       fd}                      dd          }|                    |           |                     j        d           |S )z
        Test that 'ln' creates a file which appears as a link in the output of
        'ls'. Check that removing the new file succeeds without output.
        c                                                                           | d         d                               | d                             d          d                               d          S )Nr   r1   r      lzlink failedzrm testLink)r  r<   r4   ry  r  r  s    r/   r  z4OurServerCmdLineClientTests.testLink.<locals>._check  sg    ""$$$WQZ---OOGAJ11$77GGG??=111r1   zln testLink testfile1zls -l testLinkr1   r  r   r<   r  s   `  r/   testLinkz$OurServerCmdLineClientTests.testLink  sa    	2 	2 	2 	2 	2 NN24DEE	f	d&,,,r1   c                       fd}                      dd          }|                    |           |                     j        d           |S )zV
        Test that we can create and remove directories with the cftp client.
        c                                          | d         d                               | d                             d                                         d          S )Nr   r1   r      dzrmdir testMakeDirectory)r<   r4   ry  r  r  s    r/   r  z?OurServerCmdLineClientTests.testRemoteDirectory.<locals>._check   sT    WQZ---OOGAJ11$77888??#<===r1   mkdir testMakeDirectoryzls -l testMakeDirector?r1   rA  r  s   `  r/   testRemoteDirectoryz/OurServerCmdLineClientTests.testRemoteDirectory  sa    
	> 	> 	> 	> 	>
 NN46OPP	f	d&,,,r1   c                 h      fd}                      dd          }|                    |           |S )z
        Test that a C{mkdir} on an existing directory fails with the
        appropriate error, and doesn't log an useless error server side.
        c                 x                         | d         d                                | d         d           d S )Nr   r1   r   s   remote error 11: mkdir failedr  r  s    r/   r  zHOurServerCmdLineClientTests.test_existingRemoteDirectory.<locals>._check  s>    WQZ---WQZ)IJJJJJr1   rF  r  r  s   `  r/   test_existingRemoteDirectoryz8OurServerCmdLineClientTests.test_existingRemoteDirectory
  sO    	K 	K 	K 	K 	K NN46OPP	fr1   c                                            d j        j         d          }|                     j        d           |                     fd           |                     j        d           |S )z
        Test that we can create a directory locally and remove it with the
        cftp client. This test works because the 'remote' server is running
        out of a local directory.
        zlmkdir z/testLocalDirectoryr1   c                 .                         d          S )Nzrmdir testLocalDirectory)r  r  s    r/   r`  z@OurServerCmdLineClientTests.testLocalDirectory.<locals>.<lambda>   s    0J K K r1   )r  r]  r   r   r<   r   s   ` r/   testLocalDirectoryz.OurServerCmdLineClientTests.testLocalDirectory  sv     OOLdl&7LLLMM	d&,,,	KKKKLLL	d&,,,r1   c                       fd}                      dd          }|                    |           |                     j        d           |S )z1
        Test that we can rename a file.
        c                                          | d         d                                | d         d                               d          S )Nr   r1   r   s	   testfile2zrename testfile2 testfile1)r<   r  r  s    r/   r  z6OurServerCmdLineClientTests.testRename.<locals>._check)  sJ    WQZ---WQZ666??#?@@@r1   zrename testfile1 testfile2zls testfile?r1   rA  r  s   `  r/   
testRenamez&OurServerCmdLineClientTests.testRename$  se    
	A 	A 	A 	A 	A
 NN7HH	f	d&,,,r1   r(   )rA   rB   rC   rD   r0   r  r  r  r  r  r  r  r  r  r  r!  r)  r3  r9  r=  rB  rG  rJ  rM  rP  rE   r1   r/   r  r    sc        ( ( (T  
  8 8 88 8 8  4% % %,% % %>
 
 
F F F F  <% % %(  8  (  (  <  "    
 
 
    r1   r  c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	OurServerBatchFileTestszm
    Functional tests which launch a SFTP server over localhost and checks csftp
    in batch interface.
    c                 b    t                               |            |                                  d S r(   r  r0   r  r-   s    r/   r0   zOurServerBatchFileTests.setUp;  s.      &&&r1   c                 ^    t                               |            |                                 S r(   )r  r  r  r-   s    r/   r  z OurServerBatchFileTests.tearDown?  s&    ##D)))   r1   c                    |                                  t          d          5 }|                    |           d d d            n# 1 swxY w Y   | j                                        j        }d|fz  }t          j        |                                d          dd          }t          j
        dt          j         d|            t          j                                        }t          j                            t          j                  |d<   d| j        j        _        t+          t          j        ||	          }fd
}|                    d            |                    |           |S )Nr,  z-p %i -l testuser --known-hosts kh_test --user-authentications publickey --host-key-algorithms ssh-rsa -i dsa_test -a -v -b %s 127.0.0.1r   r  r   r  r  r  r  c                 0    t          j                   | S r(   )rP   r  )resfns    r/   _cleanupz9OurServerBatchFileTests._getBatchOutput.<locals>._cleanupZ  s    IbMMMJr1   c                     | d         S )Nr   rE   )rX  s    r/   r`  z9OurServerBatchFileTests._getBatchOutput.<locals>.<lambda>^  s
    #a& r1   )r  r  r   r  r  r  r    r  r!  r   rs  r   r   rP   rQ   r  r  r;  r   r  r  r   r   addBoth)	r.   r  fpr  r  r  r   rZ  rY  s	           @r/   _getBatchOutputz'OurServerBatchFileTests._getBatchOutputC  s   [[]]"c]] 	bHHQKKK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	{""$$)! 2J #DJJLLf===abbA23>22D22333jooJOOCH55L562$S^TsCCC	 	 	 	 	 	
(()))			(s   AAAc                 j     d} fd}                      |          }|                    |           |S )z
        Test whether batch file function of cftp ('cftp -b batchfile').
        This works by treating the file as a list of commands to be run.
        zpwd
ls
exit
c                 *   |                      d          } t          j        dt          |           z                                 j                                        j        | d                                        | dd         g d           d S )Nrr  zRES %sr   r  r  )	r!  r   rs  reprassertInr]  r(  r   r<   rX  r.   s    r/   _cbCheckResultz=OurServerBatchFileTests.testBatchFile.<locals>._cbCheckResultm  s    ))E""CGHtCyy()))MM$,224493q6BBBAbD	VVV    r1   r^  r   r.   r  re  r   s   `   r/   testBatchFilez%OurServerBatchFileTests.testBatchFilec  sO    

	 	 	 	 	   &&	n%%%r1   c                 j     d} fd}                      |          }|                    |           |S )zO
        Test that an error in the batch file stops running the batch.
        zchown 0 missingFile
pwd
exit
c                 l                         j                                        j        |            d S r(   )assertNotInr]  r(  r   rd  s    r/   re  z9OurServerBatchFileTests.testError.<locals>._cbCheckResult  s0    T\5577<cBBBBBr1   rf  rg  s   `   r/   	testErrorz!OurServerBatchFileTests.testErrorz  sT    
	C 	C 	C 	C 	C   &&	n%%%r1   c                 j     d} fd}                      |          }|                    |           |S )z_
        Test that a minus sign '-' at the front of a line ignores
        any errors.
        z-chown 0 missingFile
pwd
exit
c                 l                         j                                        j        |            d S r(   )rc  r]  r(  r   rd  s    r/   re  z@OurServerBatchFileTests.testIgnoredError.<locals>._cbCheckResult  s.    MM$,224493?????r1   rf  rg  s   `   r/   testIgnoredErrorz(OurServerBatchFileTests.testIgnoredError  sT    

	@ 	@ 	@ 	@ 	@   &&	n%%%r1   N)
rA   rB   rC   rD   r0   r  r^  rh  rl  ro  rE   r1   r/   rR  rR  4  sy         
  ! ! !  @  .       r1   rR  sshz$no ssh command-line client availablesftpz%no sftp command-line client availablec                   $    e Zd ZdZd Zd Zd ZdS )OurServerSftpClientTestsz@
    Test the sftp server against sftp command line client.
    c                 ^    t                               |            |                                 S r(   rT  r-   s    r/   r0   zOurServerSftpClientTests.setUp  s(      &&&!!!r1   c                 *    |                                  S r(   )r  r-   s    r/   r  z!OurServerSftpClientTests.tearDown  s       r1   c                 T   	 t          t          j                                                   t	          d          5 }|                    d           ddd           n# 1 swxY w Y    j                                        j        	t          j
        fd}                     t          d|           d j        j        _        t          dd          }	fd	} fd
}|                    |           |                    fd           |                    |          S )a  
        Test the return of extended attributes by the server: the sftp client
        should ignore them, but still be able to parse the response correctly.

        This test is mainly here to check that
        L{filetransfer.FILEXFER_ATTR_EXTENDED} has the correct value.
        r,  z	ls .
exitNc                 *     | |          }d|d<   |S )Nr   ext_foorE   )r.   sr   
oldGetAttrs      r/   	_getAttrszCOurServerSftpClientTests.test_extendedAttributes.<locals>._getAttrs  s#    JtQ''E$E)Lr1   r{  Trp  )-oPubkeyAcceptedKeyTypes=ssh-dssz-Vc                 J    | dk    rd}nd}|dddddddd	dd
fz  ddfz  }|S )Nr   )r|  r}  rE   z-Fz	/dev/nullr|  zIdentityFile=dsa_testzUserKnownHostsFile=kh_testzHostKeyAlgorithms=ssh-rsazPort=%iz-bztestuser@127.0.0.1rE   )statusargsrY  r  s     r/   hasPAKTzAOurServerSftpClientTests.test_extendedAttributes.<locals>.hasPAKT  s^    {{? ',+TG#$ D Kr1   c                                          | d         d| d                             d                     dD ]}                    || d                    d S )Nr  r   r   asciir  )r<   r   rc  )r   ir.   s     r/   r<  z?OurServerSftpClientTests.test_extendedAttributes.<locals>.check  si    VAY6!9+;+;G+D+DEEE , , a++++, ,r1   c                 &    t          d|           S )Nrq  )r   )r  r  s    r/   r`  zBOurServerSftpClientTests.test_extendedAttributes.<locals>.<lambda>  s    #;FD##N#N r1   )dictrP   rQ   r  r  r   r  r  r  r#   r{  rT   r  r  r   r   )
r.   r  r{  r   r  r<  r  rY  rz  r  s
   `     @@@@r/   test_extendedAttributesz0OurServerSftpClientTests.test_extendedAttributes  s    2:[[]]"c]] 	"aGGL!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"{""$$).8
	 	 	 	 	
 	

,k9EEE592 E#QSVWW	 	 	 	 	 	2	, 	, 	, 	, 	, 	
g	NNNNOOO}}U###s   A%%A),A)N)rA   rB   rC   rD   r0   r  r  rE   r1   r/   rs  rs    sP         " " "! ! !F$ F$ F$ F$ F$r1   rs  )RrD   r   rs   rV   rP   r   r   rM   ior   r   unittestr   zope.interfacer   twisted.conchr   twisted.conch.interfacesr   $twisted.conch.test.test_filetransferr	   r
   twisted.credr   twisted.internetr   r   r   r   r   twisted.internet.taskr   twisted.internet.testingr   twisted.internet.utilsr   r   twisted.pythonr   twisted.python.fakepwdr   twisted.python.filepathr   twisted.python.procutilsr   twisted.python.reflectr   twisted.trial.unittestr   r   unixtwisted.conch.scriptsr   twisted.conch.scripts.cftpr   twisted.conch.sshr   twisted.conch.ssh.connectionr   twisted.conch.testr    r!   twisted.conch.test.test_conchr"   r#   ImportError	skipTestsIReactorProcessr%   rG   r   r   r   r   r   r[  ProcessProtocolrf  r  r  rR  rs  rE   r1   r/   <module>r     s  
     				  



  % % % % % % % %       & & & & & &       . . . . . . U U U U U U U U       H H H H H H H H H H H H H H ' ' ' ' ' ' 4 4 4 4 4 4 L L L L L L L L       / / / / / / , , , , , , * * * * * * 0 0 0 0 0 0 + + + + + +}^,,})** 
	......999999222222EEEEEE;;;;;;;;;;;;;;RRRRRRR    	D, :
 :7D I IJJJI 	?@@$
 $
 $
 $
 $
h $
 $
 A@$
N_
 _
 _
 _
 _
8 _
 _
 _
D        #. #. #. #. #. #. #. #.L9 9 9 9 9 9 9 96 Y/& /& /& /& /& /& /& /&d 	?@@p
 p
 p
 p
 p
x p
 p
 A@p
f. . . . . . . .x x x x xh. x x xv$+ $+ $+ $+ $+ $+ $+ $+N 	?@@K K K K K"4 K K A@K\ 	?@@d d d d d0 d d A@dN 	?@@EE%LL@AAEE&MMBCCR$ R$ R$ R$ R$1 R$ R$ DC BA A@R$ R$ R$s   ,,C C! C!