
    Vh                        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# 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-d      Z0 e-d      Z1e0r-	 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z;m<Z< ddl=m>Z> ddlm?Z? dZAde1e0 ej                  ed      fv rdZA eeAd       G d  d!e/             ZC G d" d#e/      ZD G d$ d%e!      ZE G d& d'      ZF G d( d)      ZG ee       G d* d+e	             ZH eeAd       G d, d-e/             ZI G d. d/      ZJ G d0 d1ej                        ZL G d2 d3e      ZM eeAd       G d4 d5eM             ZN eeAd       G d6 d7eM             ZO eeAd       e e+d8       d9       e e+d:       d;       G d< d=eM                           ZPy# e@$ r Y (w xY w)>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)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y)SSHSessionTestsz=
    Tests for L{twisted.conch.scripts.cftp.SSHSession}.
    Nc                     t               | _        t               | _        | j                  | j                  _        t	               | _        t        | j
                        | _        | j                  | j                  _        y N)r!   stdior   channelr   stderrBufferr   stderrselfs    L/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/test/test_cftp.pysetUpzSSHSessionTests.setUp>   sP    [
!|!ZZ#I#D$5$56"kk    c                     | j                   j                          | j                  | j                  j                         y)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   s*    
 	  "

001r/   c                     d}|j                  d      }| j                  j                  t        |dz          | j	                  | j
                  j                         |dz          y)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.
    	
 	&&(!	
r/   )returnN)__name__
__module____qualname____doc__r.   r4   r>    r/   r-   r$   r$   8   s    *2
r/   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 ej                  ej                   e        eed      d        Zd Zy# ej$                  $ r dZY Cw xY w#  ej                  ej                   e       w xY w)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                 Z    d _          fd} j                  t        d|       dt        j                  v ra j                  t        j                  t        j                  dt        j                  d           j                  t        j                         yd } j                  |       y)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    88Or/   timeTZc                  n    	 t         j                  d= t        j                          y # t        $ r Y  w xY w)NrM   )osenvironKeyErrorrL   rG   rD   r/   r-   cleanupz#ListingTests.setUp.<locals>.cleanup|   s2    

4( 

   s   ( 	44N)
rJ   patchr   rO   rP   
addCleanupoperatorsetitemrL   rG   )r,   rK   rR   s   `  r-   r.   zListingTests.setUpi   sy    
 	 	

2vx( 2::OOH,,bjj$

4@PQOODJJ' OOG$r/   c                 |    |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.
        rM   foo)rO   rP   rL   rG   r   lsLine)r,   timezonestats      r-   _lsInTimezonezListingTests._lsInTimezone   s,     $

4

yy%%r/   c                     | j                   dz
  }t        j                  dddddddd|df
      }| j                  | j	                  d|      d       | j                  | j	                  d|      d       y)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rJ   rO   stat_resultr:   r\   r,   thenr[   s      r-   test_oldFilezListingTests.test_oldFile   |     xx01~~q!Q1aAtQ?@148I	
 	148I	
r/   c                     | j                   dz
  dz   }t        j                  dddddddd|df
      }| j                  | j	                  d|      d       | j                  | j	                  d|      d       y)	
        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 foor`   z;!---------    0 0        0               0 May 02  1973 fooNra   rc   s      r-   test_oldSingleDigitDayOfMonthz*ListingTests.test_oldSingleDigitDayOfMonth        xx015EF~~q!Q1aAtQ?@148I	
 	148I	
r/   c                     | j                   dz
  }t        j                  dddddddd|df
      }| j                  | j	                  d|      d       | j                  | j	                  d|      d       y)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 foor`   ;!---------    0 0        0               0 Aug 29 09:33 fooNra   rc   s      r-   test_newFilezListingTests.test_newFile   rf   r/   
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       | j                  t        j
                  t        j                  |       | j                  | j                  d|      d       | j                  | j                  d|      d       y)	zC
        The month name in the date is locale independent.
        rl   r   rp   r_   rm   r`   rn   N)
rJ   rO   rb   locale	getlocale	setlocaleLC_ALLrT   r:   r\   )r,   rd   r[   currentLocales       r-   test_localeIndependentz#ListingTests.test_localeIndependent   s     xx01~~q!Q1aAtQ?@ ((*5((&--G148I	
 	148I	
r/   c                     | j                   dz
  dz   }t        j                  dddddddd|df
      }| j                  | j	                  d|      d       | j                  | j	                  d|      d       y)	rh   rl   i F r   r_   z;!---------    0 0        0               0 Sep 01 17:33 foor`   z;!---------    0 0        0               0 Sep 02 09:33 fooNra   rc   s      r-   test_newSingleDigitDayOfMonthz*ListingTests.test_newSingleDigitDayOfMonth   rj   r/   )r@   rA   rB   rC   getattrrL   skipr.   r\   re   ri   ro   rr   rs   rv   rt   ru   
localeSkipErrorr   rw   ry   rD   r/   r-   rF   rF   `   s    
 tWd#+I%:	&
$
(
( %F$$&M7	FV]]L9 J6JAB
 C
,
= || 	J	
 	6s)   B B BB BB B:rF   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| _        t        |           y)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__  s    
 	r/   )r@   rA   rB   rC   r   __classcell__)r   s   @r-   r   r     s    
 r/   r   c                   "    e Zd ZdZd Zd Zd Zy)FilesystemAccessExpectationszC
    A test helper used to support expected filesystem access.
    c                     i | _         y r&   _cacher+   s    r-   r   z%FilesystemAccessExpectations.__init__  s	    r/   c                 &    || j                   ||f<   y)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"r/   c                 <    | j                   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}--r/   N)r@   rA   rB   rC   r   r   r   rD   r/   r-   r   r     s    ,.r/   r   c                       e Zd ZdZd Zd Zy)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                 F    t        |       | _        ddd| _        || _        y )N   
   )requests
buffersize)r   	transportoptions_availableFiles)r,   availableFiless     r-   r   zInMemorySFTPClient.__init__B  s'    +D1
  .r/   c                 :    | j                   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8r/   N)r@   rA   rB   rC   r   r   rD   r/   r-   r   r   8  s    .9r/   r   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
InMemoryRemoteFilez;
    An L{ISFTPFile} which handles all data in memory.
    c                 <    || _         t        j                  |        y)zL
        @param name: Name of this file.
        @type name: L{str}
        N)namer   r   )r,   r   s     r-   r   zInMemoryRemoteFile.__init__Y  s    
 	r/   c                 p    | j                  |       | j                  |       t        j                  |       S )z/
        @see: L{ISFTPFile.writeChunk}
        )seekwriter   succeed)r,   startdatas      r-   
writeChunkzInMemoryRemoteFile.writeChunka  s+     			%

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

        Keeps data after file was closed to help with testing.
        TN)_closedr+   s    r-   closezInMemoryRemoteFile.closei  s     r/   c                      y r&   rD   r+   s    r-   getAttrszInMemoryRemoteFile.getAttrsq      r/   c                      y r&   rD   )r,   offsetlengths      r-   	readChunkzInMemoryRemoteFile.readChunku  r   r/   c                      y r&   rD   )r,   r   s     r-   setAttrszInMemoryRemoteFile.setAttrsy  r   r/   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     %%r/   N)r@   rA   rB   rC   r   r   r   r   r   r   r;   rD   r/   r-   r   r   S  s*    #&r/   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y)StdioClientTestsz(
    Tests for L{cftp.StdioClient}.
    c                 d   t               | _        t        | j                        }t        j                  |      | _        d| j
                  _        t               x| _        | j
                  _	        | j                  dd       | j
                  j
                  j                  | j
                  _        y)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-   r.   zStdioClientTests.setUp  s    
 ;<'(;(;<
&&z2'*$+7>9(
 	  b) !% 2 2 < <r/   c           	         | j                   j                  t        j                         dt	        j
                         dddt        j                         | j                  j                  d      }|j                  | j                  d       |S )zs
        The I{exec} command runs its arguments locally in a child process
        using the user's shell.
        secret  rX   barzexec print(1 + 2)s   3
)r   addUsergetpassgetuserrO   getuidsys
executabler   _dispatchCommandaddCallbackr:   r,   ds     r-   	test_execzStdioClientTests.test_exec  sf    
 	OOxdE5#..	
 KK(()<=	d&&/r/   c           	          | j                   j                  t        j                         dt	        j
                         dddd       | j                  j                  d      }|j                  | j                  d       |S )zr
        If the local user has no shell, the I{exec} command runs its arguments
        using I{/bin/sh}.
        r   r   rX   r    zexec echo hello   hello

r   r   r   r   rO   r   r   r   r   r:   r   s     r-   test_execWithoutShellz&StdioClientTests.test_execWithoutShell  sb    
 	OOxdE5"	
 KK(():;	d&&
3r/   c           	          | j                   j                  t        j                         dt	        j
                         dddd       | j                  j                  d      }|j                  | j                  d       |S )zO
        The I{exec} command is run for lines which start with C{"!"}.
        r   r   rX   r   z/bin/shz!echo hellor   r   r   s     r-   	test_bangzStdioClientTests.test_bang  sa     	OOxdE5)	
 KK((7	d&&
3r/   c                 f    ddl  G fdd      }| j                  t        d |              y)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y)7StdioClientTests.setKnownConsoleSize.<locals>.FakeFcntlc                 v    |j                   k7  r| j                  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  s3    #..(IICD{{41==r/   N)r@   rA   rB   r   )r   r   r   s   r-   	FakeFcntlr     s    >r/   r   fcntl)r   rS   r   )r,   r   r   r   r   s    `` @r-   r   z$StdioClientTests.setKnownConsoleSize  s&     		> 	> 	

4)+.r/   c                    | j                  dd       t               x}| j                  _        t	        d      }d|_        t        j                  |      }d|_        |j                         }|j                  d       |xj                  dz  c_        | j                  j                  ||       d}| j                  | j                  j                  j                         |       y	)
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(&+g-#$- ""7+ MMO	c%%gy98..446?r/   c                 p   | j                  dd       t               x}| j                  _        t	        d      }d|_        t        j                  |      }|j                         }| j                  j                  ||       d}| j                  | j                  j                  j                         |       y)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(&+g-#$- ""7+MMO	%%gy96..446?r/   c                    | j                  dd       t               }d|_        t        j                  |      }| j
                  j                  |d       d}| j                  || j
                  j                  j                                y)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  sn     	  R()$""7+%%gq1:!6!6!<!<!>?r/   c                 ^    | j                   j                  d      }| j                  d|       y)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  s(     ))$/6*r/   c                 ^    | j                   j                  d      }| j                  d|       y)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  s)    
 )),7+V4r/   c                 ^    | j                   j                  d      }| j                  d|       y)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#  s*    
 ))*AB16:r/   c                 ^    | j                   j                  d      }| j                  d|       y)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,  s*    
 ))*NO=vFr/   Nc                     || j                         }t        |d      5 }|j                  |       ddd       |S # 1 sw Y   |S xY w)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  sF     <;;=D$ 	 JJw	 	 s	   ;Ac                    | j                   j                  j                         }|j                  d      }|j	                  d      }g }g }|D ]T  \  }}}g }	|D ]  }
|	j                  | d|
         |	j                  d| d|        |j                  |	       |j                  d      j                  d      j	                  d      }|dd	 }|d	   j                  d
      j	                  d
      }|j                  |       g }|dd	 D ]o  }
|
j                         j                  dd      d   }
|
j                         j	                  dd      }
|j                  |
d    d|
d   j                                 q |j                  |d	          |j                  |       W |r%| j                  t        |      t        |             n| j                  ||       | j                  dt        |      d       y)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

        r6   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#E68  "  ; ''5'4&(9:;##l5'fX$FG!!"23"JJqM//5;;DAM"3B'F $**4066t<DMM$N s F zz|**3215 zz|))#q1%%a	47==?2C&DEF !!&*-/?	0B VN3VL5IJ^\:KC	
r/   c                    d}| j                  |      }t        j                  t        j                  z  t        j                  z  }t
        j                  j                  dt
        j                  j                  |            }t        |      }| j                  j                  ||t        j                  |             d| j                  j                  j                  d<   | j                  j!                  |      }| j#                  |       | j%                  ||j'                                | j)                  |j*                         | j-                  ||g dfg       y)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.0Br6  N)r  r   	FXF_WRITE	FXF_CREAT	FXF_TRUNCrO   r   joinbasenamer   r   r   r   r   r   r   cmd_PUTsuccessResultOfr:   r;   r2   r   r4  )r,   r  	localPathr   
remoteName
remoteFiledeferreds          r-   test_cmd_PUTSingleNoRemotePathz/StdioClientTests.test_cmd_PUTSingleNoRemotePath  s    %MM'M2	&&)?)??,BXBXXWW\\#rww'7'7	'BC
'
3

E5==3LM35""<0;;&&y1X&*"5"5"78
**+%NOP	
r/   c                    | j                         }t        j                  t        j                  z  t        j                  z  }d}t        |      }| j                  j                  ||t        j                  |             | j                  j                  | d| d      }| j                  |       | j                  ||dgfg       | j                  |j                         | j!                  d|j#                                y)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.0Br/   N)r  r   r7  r8  r9  r   r   r   r   r   r   r<  r=  r4  r2   r   r:   r;   )r,   r>  r   r?  r@  rA  s         r-   test_cmd_PUTSingleRemotePathz-StdioClientTests.test_cmd_PUTSingleRemotePath  s     MMO	&&)?)??,BXBXX#
'
3

E5==3LM;;&&)Aj\'JKX&y*{mDEF
**+j1134r/   c                 "   | j                         }t        j                  j                  |      }d}t        j                  j	                  |      }| j                  t        j                  j                  ||            }t        j                  t        j                  z  t        j                  z  }d| }d| }t        |      }	t        |      }
| j                  j                  ||t        j                  |	             | j                  j                  ||t        j                  |
             | j                  j!                  t        j                  j                  |d            }| j#                  |       | j%                  |	j&                         | j)                  d|	j+                                | j%                  |
j&                         | j)                  d|
j+                                | j-                  ||dgf||dgfgd       y	)
z
        When a gobbing expression is used local files are transferred with
        remote file names based on local names.
        second-namer   r   *r/   rD  Tr(  N)r  rO   r   r;  dirnamer:  r   r7  r8  r9  r   r   r   r   r   r   r<  r=  r2   r   r:   r;   r4  )r,   first	firstName
secondNameparentsecondr   firstRemotePathsecondRemotePathfirstRemoteFilesecondRemoteFilerA  s               r-    test_cmd_PUTMultipleNoRemotePathz1StdioClientTests.test_cmd_PUTMultipleNoRemotePath  s   
 GG$$U+	"
'BGGLL$DE&&)?)??,BXBXXi[/zl+,_=-.>?o8VWeU]]3C%D	
 ;;&&rww||FC'@AX&//0o6689(001.779:+7)K=9  	 	
r/   c                 @   | j                         }t        j                  j                  |      }d}t        j                  j	                  |      }| j                  t        j                  j                  ||            }t        j                  t        j                  z  t        j                  z  }t        |      }t        |      }d| }	d| }
| j                  j                  |	|t        j                  |             | j                  j                  |
|t        j                  |             | j                  j!                  dj#                  t        j                  j                  |d                  }| j%                  |       | j'                  |j(                         | j+                  d|j-                                | j'                  |j(                         | j+                  d|j-                                | j/                  ||dgf||dgfgd	       y
)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.
        rG  rH  z/remote/z	{} remoterI  r/   rD  TrJ  N)r  rO   r   r;  rK  r:  r   r7  r8  r9  r   r   r   r   r   r   r<  formatr=  r2   r   r:   r;   r4  )r,   rL  rM  rN  rO  rP  r   rS  rT  rQ  rR  rA  s               r-   "test_cmd_PUTMultipleWithRemotePathz3StdioClientTests.test_cmd_PUTMultipleWithRemotePath  s    GG$$U+	"
'BGGLL$DE&&)?)??,BXBXX,Y7-j9$YK0%j\2o8VWeU]]3C%D	
 ;;&&{'9'9"'',,vs:S'TUX&//0o6689(001.779:	K=1k]3  	 	
r/   )Nr/   )F)r@   rA   rB   rC   r.   r   r   r   r   r   r  r  r	  r  r  r  r  r4  rB  rE  rU  rX  rD   r/   r-   r   r     sg    =&
/*@0@$@+5;G(;
z
05&!
F#
r/   r   c                       e Zd Zd Zd Zy)FileTransferTestRealmc                     || _         y r&   )testDir)r,   r\  s     r-   r   zFileTransferTestRealm.__init__  s	    r/   c                 >    t        | j                        }|d   |d fS )Nr   c                       y r&   rD   rD   r/   r-   <lambda>z5FileTransferTestRealm.requestAvatar.<locals>.<lambda>   s    r/   )r	   r\  )r,   avatarIDmindr   as        r-   requestAvatarz#FileTransferTestRealm.requestAvatar  s!    "4<<0!}a--r/   N)r@   rA   rB   r   rc  rD   r/   r-   rZ  rZ    s    .r/   rZ  c                   L    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y)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                 \    | j                          || _        d| _        d| _        d| _        y)zr
        @param onOutReceived: A L{Deferred} to be fired as soon as data is
        received from stdout.
        NF)clearBufferonOutReceivedonProcessEnd_expectingCommand_processEnded)r,   rh  s     r-   r   zSFTPTestProcess.__init__
  s0    
 	* !%"r/   c                 .    d| _         g | _        d| _        y)zR
        Clear any buffered data received from stdout. Should be private.
        r/   N)buffer_linesReceived_lineBufferr+   s    r-   rg  zSFTPTestProcess.clearBuffer  s      r/   c                 ~   t        j                  d|z         | j                  |z   j                  d      }|j	                  d      | _        | j
                  j                  |       | j                  %| j                  dc}| _        |j                  |       | xj                  |z  c_	        | j                          y)zO
        Called by Twisted when the cftp client prints data to stdout.
        zgot %r   
r  N)r   msgro  r   r   rn  r#  rh  callbackrm  _checkForCommand)r,   r   linesr   s       r-   outReceivedzSFTPTestProcess.outReceived  s     	4 !!D(//6 99R=""5) )$($6$6!At!JJttr/   c                     d}| j                   r| j                  |k(  rpdj                  | j                        }|j	                  |      r|t        |      d  }| j                          | j                   d c}| _         |j                  |       y y y )Ns   cftp> rq  )rj  ro  r:  rn  
startswithr&  rg  rs  )r,   promptbufr   s       r-   rt  z SFTPTestProcess._checkForCommand.  s    !!d&6&6&&@**T001C~~f%#f+-((,(>(>%At%JJsO 'A!r/   c                 4    t        j                  d|z         y)zO
        Called by Twisted when the cftp client prints data to stderr.
        zerr: %sN)r   rr  )r,   r   s     r-   errReceivedzSFTPTestProcess.errReceived8  s     		D !r/   c                     | j                   S )zQ
        Return the contents of the buffer of data received from stdout.
        )rm  r+   s    r-   	getBufferzSFTPTestProcess.getBuffer>  s     {{r/   c                     t        j                         | _        | j                          t	        |t
              r|j                  d      }| j                  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.
        r6   rq  )	r   Deferredrj  rg  
isinstancestrr8   r   r   r,   commands     r-   
runCommandzSFTPTestProcess.runCommandD  sY     "'!1gs#nnW-GWu_-%%%r/   c                     t        j                  d      }|D cg c]  }|j                  | j                  |        }}t        j                  |      S c c}w )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   )r   DeferredSemaphorerunr  gatherResults)r,   commandssemr  dls        r-   	runScriptzSFTPTestProcess.runScriptV  sM     %%a(?GHGcggdoow/HH""2&& Is   #Ac                     | j                   rt        j                  d      S t        j                         | _        | 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)rk  r   r   r  ri  r   signalProcessr+   s    r-   killProcesszSFTPTestProcess.killProcesse  sI     ==&&!NN,$$V,   r/   c                 v    d| _         | j                  r&| j                  dc}| _        |j                  d       yy)zF
        Called by Twisted when the cftp client process ends.
        TN)rk  ri  rs  )r,   reasonr   s      r-   processEndedzSFTPTestProcess.processEndedt  s;     "#'#4#4d At JJt r/   N)r@   rA   rB   rC   r   rg  rv  rt  r|  r~  r  r  r  r  rD   r/   r-   re  re    s9    	# ""&$'!r/   re  c                   *    e Zd Zd Zd Zd Zd Zd Zy)CFTPClientTestBasec                    t        dd      5 }|j                  t        j                         d d d        t        dd      5 }|j                  t        j                         d d d        t        j                  dd       t        dd      5 }|j                  dt        j                  z          d d d        t        j                  |       S # 1 sw Y   xY w# 1 sw Y   wxY w# 1 sw Y   6xY w)Nrsa_test.pubr  rsa_testi  kh_tests
   127.0.0.1 )	r  r   r    publicRSA_opensshprivateRSA_opensshrO   chmodr
   r.   r,   fs     r-   r.   zCFTPClientTestBase.setUp  s    .$' 	01GGH../	0*d# 	1qGGH//0	1
U#)T" 	@aGGMH$>$>>?	@!!$''	0 	0	1 	1	@ 	@s#    C C#C#CC #C,c                    t        | j                        }t        j                  |      }|j	                  t        j                                t        j                         }||_        t        j                  d|d      | _
        y )Nr   z	127.0.0.1)	interface)rZ  r\  r   PortalregisterCheckerr    conchTestPublicKeyCheckerConchTestServerFactoryr   	listenTCPserver)r,   realmpfacs       r-   startServerzCFTPClientTestBase.startServer  s`    %dll3MM% 	(<<>?--/
''3+Fr/   c                 v   t        | j                  j                  d      s| j                  d       S d| j                  j                  j                  _        t        j                  | j                  j                  j                  j                  j                        }|j                  | j                         |S )Nprotor   )hasattrr  factory_cbStopServerr  expectedLoseConnectionr   maybeDeferredr   loseConnectionr   r   s     r-   
stopServerzCFTPClientTestBase.stopServer  s    t{{**G4%%d++;<!!8 3 3 9 9 C C R RS	d(()r/   c                 T    t        j                  | j                  j                        S r&   )r   r  r  stopListeningr,   ignoreds     r-   r  z CFTPClientTestBase._cbStopServer  s    ""4;;#<#<==r/   c                     dD ]  }	 t        j                  |        t        j                  |       S # t        $ r Y ;w xY w)N)r  r  r  )rO   removeBaseExceptionr
   tearDownr  s     r-   r  zCFTPClientTestBase.tearDown  sH    8 	A		!	
 $$T** ! s   3	??N)r@   rA   rB   r.   r  r  r  r  rD   r/   r-   r  r  ~  s    (G>+r/   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y)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                 "    t         j                           j                          d} j                  j	                         j
                  }t        j                  ||z  j                         d      }t        j                  dt        j                   d|        t        j                         }t        |       _        |j#                   fd       t$        j&                  j)                         }t$        j*                  j-                  t        j.                        |d<   g }i }|D ]4  }t1        |t2              r|j5                  d      }|j7                  |       6 |D ]N  }||   }	t1        |t2              r|j5                  d      }t1        |	t2              r|	j5                  d      }	|	||<   P t        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 rsa_test -a -v 127.0.0.1r   modrunning r  c                 8    j                   j                         S r&   )processProtocolrg  _r,   s    r-   r_  z3OurServerCmdLineClientTests.setUp.<locals>.<lambda>       4 4 @ @ B r/   
PYTHONPATHr6   env)r  r.   r  r  getHostportr   	_makeArgsr   r   rr  r   r   r   r  re  r  r   rO   rP   copypathsepr:  r   r  r  r8   r!  r   spawnProcess)
r,   cmdsr  r   r  encodedCmds
encodedEnvcmdvarvals
   `         r-   r.   z!OurServerCmdLineClientTests.setUp  s     & 	 {{""$))##TD[$7$7$9vF(3>>*!D623NN.q1	BCjjooJJOOCHH5L
 	$C#s#jj)s#	$  	"Cc(C#s#jj)#s#jj)!JsO	" 	
  #..+:	
 r/   c                 P      j                         }|j                   fd       |S )Nc                 8    j                   j                         S r&   )r  r  r  s    r-   r_  z6OurServerCmdLineClientTests.tearDown.<locals>.<lambda>  r  r/   )r  r   r   s   ` r-   r  z$OurServerCmdLineClientTests.tearDown  s!    OO	BCr/   c                     	 | j                   j                  j                  d       y # t        j                  $ r Y y w xY w)Nr  )r  r   r  r   ProcessExitedAlreadyr  s     r-   _killProcessz(OurServerCmdLineClientTests._killProcess  s9    	  **88@)) 		s   %( >>c                 8    | j                   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7r/   c                 8    | j                   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7r/   c                     | j                   }| j                  ddddd      }d }|j                  |       |j                  | 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 ]4  }t        |t              r|j                  d      }|j                  |       6 |dd |dd z   S )zH
            Callback function for handling command output.
            r6   N      )r  bytesr  r!  )r)  r  r  s      r-   	cmdOutputz8OurServerCmdLineClientTests.testCdPwd.<locals>.cmdOutput  sU     D !c5)**W-CC ! 8d12h&&r/   r   )r\  r  r   r:   r   rO   getcwd)r,   homeDirr   r  s       r-   	testCdPwdz%OurServerCmdLineClientTests.testCdPwd  sg     ,,NN5&*<guM		' 	
i 	d&&ryy{B(UVr/   c                 X      fd} j                  dddd      }|j                  |      S )z
        Check that 'ls -l' output includes the access permissions and that
        this output changes appropriately with 'chmod'.
        c                    j                          j                  | d   j                  d             j                  | d   d       j                  | d   j                  d      | d          j                  | d   d       y )Nr   s
   -rw-r--r--r   r/   r  s
   ----------r  )flushLoggedErrorsr2   rx  r:   resultsr,   s    r-   _checkz7OurServerCmdLineClientTests.testChAttrs.<locals>._check  ss    ""$OOGAJ11-@AWQZ-OOGAJ11-@'!*MWQZ-r/   zls -l testfile1zchmod 0 testfile1zchmod 644 testfile1r  r   r,   r  r   s   `  r-   testChAttrsz'OurServerCmdLineClientTests.testChAttrs  s5    	. NN!	
 }}V$$r/   c                       fd} j                  dd j                  j                         z   ddd      }|j                  d        |j                  |      S )z
        Check 'ls' works as expected. Checks for wildcards, hidden files,
        listing directories and listing empty directories.
        c                     j                  | d   g d       j                  | d   g d       j                  | d   ddg       j                  | d   g d       j                  | d	   d
g       y )Nr   s   testDirectory   testRemoveFile   testRenameFiles	   testfile1r   r  r  r  r  )s   .testHiddenFiler  r  r  r/   r:   r  s    r-   r  z4OurServerCmdLineClientTests.testList.<locals>._check*  s    
V 
V WQZ*;=N)OP
V WQZ#/r/   r   zls ../zls *Filezls -a *Filezls -l testDirectoryc                 J    | D cg c]  }|j                  d       c}S c c}w )Nrq  )r   )xsxs     r-   r_  z6OurServerCmdLineClientTests.testList.<locals>.<lambda>@  s    "!=Q!''%.!= !=s    )r  r\  r;  r   r  s   `  r-   testListz$OurServerCmdLineClientTests.testList$  sW    	0 NNt||,,..!
 	
=>}}V$$r/   c                    | j                  d      }t        j                  d      j                  d      j	                         }t        |t              r|j                  d      }|j                  | j                  |       |S )zB
        Check that running the '?' command returns help.
        ?Nr   r6   )
r  r   r   cmd_HELPr"  r  r  r8   r   r:   )r,   r   helpTexts      r-   testHelpz$OurServerCmdLineClientTests.testHelpC  sg     OOC ##D)2226<<>h$w/H	d&&1r/   Nc                 b    | j                  |j                         |j                         |       y)zg
        Assert that the files at C{name1} and C{name2} contain exactly the
        same data.
        N)r:   
getContent)r,   name1name2rr  s       r-   assertFilesEqualz,OurServerCmdLineClientTests.assertFilesEqualO  s(    
 	))+U-=-=-?Er/   c                 h    dj                   j                  j                   j                  j                        t        t              rj                  d       fd} j                  d j                  j                   d      }|j                  |       |j                   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 file2r6   c                     j                  | j                               j                  j                  j	                  d      j                  j	                  d      d       j                  d      S )N	testfile1
test file2z
get failedzrm "test file2")r2   endswithr  r\  childr  r   r*  r,   s    r-   	_checkGetz6OurServerCmdLineClientTests.testGet.<locals>._checkGetd  s_    OOFOON;<!!"";/""<0
 ??#455r/   zget testfile1 "z/test file2"c                 t    j                  j                  j                  d      j                               S )Nr
  assertFalser\  r  existsr  s    r-   r_  z5OurServerCmdLineClientTests.testGet.<locals>.<lambda>p  *    d&&t||'9'9,'G'N'N'PQ r/   )rW  r\  r   r  r  r8   r  r   )r,   r  r   r*  s   `  @r-   testGetz#OurServerCmdLineClientTests.testGetV  s     EKKLLLL
 nc*+227;N	6 OOodll.?.?-@MN	i 	Q	
 r/   c                 R      fd} j                  d      }|j                  |      S )zQ
        Test that 'get' works correctly when given wildcard parameters.
        c                     j                  j                  j                  d      t        d      d       j                  j                  j                  d      t        d      d       y )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)*+
 !!""#34)*+r/   z
get testR*)r  r   r  s   `  r-   testWildcardGetz+OurServerCmdLineClientTests.testWildcardGett  s'    

	 OOL)}}V$$r/   c                 V    d j                   j                         j                  z   dz    j                   j                         j                  z   dz    fd} j                  d j                   j                   d      }|j	                  |       |j	                   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                  j                  j                  d      j                  j                  d             j                  | j	                               j                  d      S )Nr	  
test"file2zrm "test\"file2")r  r\  r  r2   r  r  r  s    r-   	_checkPutz6OurServerCmdLineClientTests.testPut.<locals>._checkPut  s[    !!"";/1C1CL1Q OOFOON;<??#677r/   put z/testfile1 "test\"file2"c                 t    j                  j                  j                  d      j                               S )Nr  r  r  s    r-   r_  z5OurServerCmdLineClientTests.testPut.<locals>.<lambda>  r  r/   )r\  asBytesModer   r  r   )r,   r  r   r*  s   `  @r-   testPutz#OurServerCmdLineClientTests.testPut  s     ll&&(--.  ll&&(--. 	 		8 OOd4<<#4#4"55NOP	i 	Q	
 r/   c                      j                   j                  d      j                  d      5 }|j                  d       ddd        j                   j                  d      j                  d      5 }|j                  d       ddd        fd} j	                  d	 j                   j
                   d
      }|j                  |       |S # 1 sw Y   xY w# 1 sw Y   VxY w)zb
        Check that 'put' uploads files correctly when overwriting a longer
        file.
        shorterFilewmode   aN
longerFile   bbc                     j                  j                  j                  d      j                  j                  d             y )Nr%  r*  )r  r\  r  )r   r,   s    r-   r  zEOurServerCmdLineClientTests.test_putOverLongerFile.<locals>._checkPut  s4    !!""=14<<3E3El3Sr/   r   z/shorterFile longerFile)r\  r  r  r   r  r   r   )r,   r  r  r   s   `   r-   test_putOverLongerFilez2OurServerCmdLineClientTests.test_putOverLongerFile  s     \\.333= 	GGDM	\\-222< 	GGEN		
 OOd4<<#4#4"55LMN	i 	 		 	s   C2CCC"c                      j                   j                  d      j                          j                  d      j                  d      5 }|j	                  d       ddd        j                   j                  d      j                  d      5 }|j	                  d       ddd        fd} j                  d	 j                   j                   d
      }|j                  |       |S # 1 sw Y   xY w# 1 sw Y   WxY w)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)  Nr+  c                 z    j                  j                  d      j                  j                  d             y )Nr  )r  r  r\  )r   r,   someDirs    r-   r  zMOurServerCmdLineClientTests.test_putMultipleOverLongerFile.<locals>._checkPut  s+    !!'--"79K9KF9STr/   r   z/dir/*)r\  r  createDirectoryr  r   r  r   r   )r,   r  r  r   r1  s   `   @r-   test_putMultipleOverLongerFilez:OurServerCmdLineClientTests.test_putMultipleOverLongerFile  s     ,,$$U+!]]6"''S'1 	QGGDM	\\',,#,6 	!GGEN		U OOd4<<#4#4"5V<=	i 	 		 	s   C0C<0C9<Dc                       fd} j                  dd j                  j                   dd j                  j                         z        }|j	                  |       |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                    j                  | d   d       j                  | d   d       j                  j                  j                  d      j                  j	                         j                  d      d       j                  j                  j                  d      j                  j	                         j                  d      d       y )Nr   r/   r  r  r  r  r  )r:   r  r\  r  rO  r  s    r-   checkz:OurServerCmdLineClientTests.testWildcardPut.<locals>.check  s    WQZ-WQZ-!!""#34##%++,<=+
 !!""#34##%++,<=+r/   r  r   z/testR*zcd %s)r  r\  r   r;  r   )r,   r6  r   s   `  r-   testWildcardPutz+OurServerCmdLineClientTests.testWildcardPut  sZ    	 NN4<<$$%W-dll++--

 	
er/   c                       fd} j                  dd      }|j                  |       |j                   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                     j                          j                  | d   d       j                  | d   j                  d      d       j	                  d      S )Nr   r/   r      lzlink failedzrm testLink)r  r:   r2   rx  r  r  s    r-   r  z4OurServerCmdLineClientTests.testLink.<locals>._check  sO    ""$WQZ-OOGAJ11$7G??=11r/   zln testLink testfile1zls -l testLinkr/   r  r   r:   r  s   `  r-   testLinkz$OurServerCmdLineClientTests.testLink  sA    	2 NN24DE	f	d&&,r/   c                       fd} j                  dd      }|j                  |       |j                   j                  d       |S )zV
        Test that we can create and remove directories with the cftp client.
        c                     j                  | d   d       j                  | d   j                  d             j                  d      S )Nr   r/   r      dzrmdir testMakeDirectory)r:   r2   rx  r  r  s    r-   r  z?OurServerCmdLineClientTests.testRemoteDirectory.<locals>._check   sB    WQZ-OOGAJ11$78??#<==r/   mkdir testMakeDirectoryzls -l testMakeDirector?r/   r;  r  s   `  r-   testRemoteDirectoryz/OurServerCmdLineClientTests.testRemoteDirectory  sA    
	>
 NN46OP	f	d&&,r/   c                 X      fd} j                  dd      }|j                  |       |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                 Z    j                  | d   d       j                  | d   d       y )Nr   r/   r   s   remote error 11: mkdir failedr  r  s    r-   r  zHOurServerCmdLineClientTests.test_existingRemoteDirectory.<locals>._check  s,    WQZ-WQZ)IJr/   r@  r  r  s   `  r-   test_existingRemoteDirectoryz8OurServerCmdLineClientTests.test_existingRemoteDirectory
  s.    	K NN46OP	fr/   c                       j                  d j                  j                   d      }|j                   j                  d       |j                   fd       |j                   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/testLocalDirectoryr/   c                 &    j                  d      S )Nzrmdir testLocalDirectory)r  r  s    r-   r_  z@OurServerCmdLineClientTests.testLocalDirectory.<locals>.<lambda>   s    0J K r/   )r  r\  r   r   r:   r   s   ` r-   testLocalDirectoryz.OurServerCmdLineClientTests.testLocalDirectory  sb     OOgdll&7&7%88KLM	d&&,	KL	d&&,r/   c                       fd} j                  dd      }|j                  |       |j                   j                  d       |S )z1
        Test that we can rename a file.
        c                 z    j                  | d   d       j                  | d   d       j                  d      S )Nr   r/   r   s	   testfile2zrename testfile2 testfile1)r:   r  r  s    r-   r  z6OurServerCmdLineClientTests.testRename.<locals>._check)  s;    WQZ-WQZ6??#?@@r/   zrename testfile1 testfile2zls testfile?r/   r;  r  s   `  r-   
testRenamez&OurServerCmdLineClientTests.testRename$  sA    
	A
 NN7H	f	d&&,r/   r&   )r@   rA   rB   rC   r.   r  r  r  r  r  r  r  r  r  r  r  r#  r-  r3  r7  r<  rA  rD  rG  rJ  rD   r/   r-   r  r    sr    (T
884%,%>
F<%(8((<"
r/   r  c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	OurServerBatchFileTestszm
    Functional tests which launch a SFTP server over localhost and checks csftp
    in batch interface.
    c                 N    t         j                  |        | j                          y r&   r  r.   r  r+   s    r-   r.   zOurServerBatchFileTests.setUp;  s      &r/   c                 L    t         j                  |        | j                         S r&   )r  r  r  r+   s    r-   r  z OurServerBatchFileTests.tearDown?  s    ##D)  r/   c                    | j                         t        d      5 }|j                  |       d d d        | j                  j	                         j
                  }d|fz  }t        j                  |j                         d      dd  }t        j                  dt        j                   d|        t        j                  j                         }t        j                   j#                  t        j$                        |d<   d| j                  j&                  _        t+        t        j                  ||	      }fd
}|j-                  d        |j/                  |       |S # 1 sw Y   5xY w)Nr&  z-p %i -l testuser --known-hosts kh_test --user-authentications publickey --host-key-algorithms ssh-rsa -i rsa_test -a -v -b %s 127.0.0.1r   r  r   r  r  r  r  c                 2    t        j                         | S r&   )rO   r  )resfns    r-   _cleanupz9OurServerBatchFileTests._getBatchOutput.<locals>._cleanupZ  s    IIbMJr/   c                     | d   S )Nr   rD   )rR  s    r-   r_  z9OurServerBatchFileTests._getBatchOutput.<locals>.<lambda>^  s
    #a& r/   )r  r  r   r  r  r  r   r  r   r   rr  r   r   rO   rP   r  r  r:  r   r  r  r   r   addBoth)	r,   r  fpr  r  r  r   rT  rS  s	           @r-   _getBatchOutputz'OurServerBatchFileTests._getBatchOutputC  s   [[]"c] 	bHHQK	{{""$))! 2J ##DJJLf=abA(3>>*!D623jjooJJOOCHH5L562$S^^TsC	 	
()			(9	 	s   E##E-c                 Z     d} fd} j                  |      }|j                  |       |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                    | j                  d      } t        j                  dt        |       z         j	                  j
                  j                         j                  | d          j                  | dd g d       y )Nrq  zRES %sr   r  r  )	r   r   rr  reprassertInr\  r"  r   r:   rR  r,   s    r-   _cbCheckResultz=OurServerBatchFileTests.testBatchFile.<locals>._cbCheckResultm  se    ))E"CGGHtCy()MM$,,224993q6BAb	Vr/   rX  r   r,   r  r_  r   s   `   r-   testBatchFilez%OurServerBatchFileTests.testBatchFilec  s2    

	   &	n%r/   c                 Z     d} fd} j                  |      }|j                  |       |S )zO
        Test that an error in the batch file stops running the batch.
        zchown 0 missingFile
pwd
exit
c                 n    j                  j                  j                         j                  |        y r&   )assertNotInr\  r"  r   r^  s    r-   r_  z9OurServerBatchFileTests.testError.<locals>._cbCheckResult  s&    T\\557<<cBr/   r`  ra  s   `   r-   	testErrorz!OurServerBatchFileTests.testErrorz  s3    
	C   &	n%r/   c                 Z     d} fd} j                  |      }|j                  |       |S )z_
        Test that a minus sign '-' at the front of a line ignores
        any errors.
        z-chown 0 missingFile
pwd
exit
c                 n    j                  j                  j                         j                  |        y r&   )r]  r\  r"  r   r^  s    r-   r_  z@OurServerBatchFileTests.testIgnoredError.<locals>._cbCheckResult  s$    MM$,,224993?r/   r`  ra  s   `   r-   testIgnoredErrorz(OurServerBatchFileTests.testIgnoredError  s3    

	@   &	n%r/   N)
r@   rA   rB   rC   r.   r  rX  rb  rf  ri  rD   r/   r-   rL  rL  4  s&    
!@. r/   rL  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y)OurServerSftpClientTestsz@
    Test the sftp server against sftp command line client.
    c                 L    t         j                  |        | j                         S r&   rN  r+   s    r-   r.   zOurServerSftpClientTests.setUp  s      &!!r/   c                 "    | j                         S r&   )r  r+   s    r-   r  z!OurServerSftpClientTests.tearDown  s      r/   c                    	 t        t        j                        } j                         }t	        |d      5 }|j                  d       ddd        j                  j                         j                  }t        j                  		fd} j                  t        d|       d j                  j                  _        ddd	d
d	dd	dd	dd	d|fz  d|df} fd}t        d||      }|j                  |      S # 1 sw Y   xY w)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_foorD   )r,   sr   
oldGetAttrs      r-   	_getAttrszCOurServerSftpClientTests.test_extendedAttributes.<locals>._getAttrs  s    tQ'E$E)Lr/   ru  Tz-Fz	/dev/nullz-ozIdentitiesOnly=yeszIdentityFile=rsa_testzUserKnownHostsFile=kh_testzHostKeyAlgorithms=ssh-rsazPort=%iz-bztestuser@127.0.0.1c                     j                  | d   d| d   j                  d             dD ]  }j                  || d           y )Nr  r   r   asciir  )r:   r  r]  )r   ir,   s     r-   r6  z?OurServerSftpClientTests.test_extendedAttributes.<locals>.check  sM    VAY6!9+;+;G+DE , a+,r/   rk  )dictrO   rP   r  r  r   r  r  r  r"   ru  rS   r  r  r   r   )
r,   r  rS  r  r  ru  argsr6  r   rt  s
   `        @r-   test_extendedAttributesz0OurServerSftpClientTests.test_extendedAttributes  s     2::[[]"c] 	"aGGL!	"{{""$)).88
	
 	

,k9E592  #(' 
$	, %VT37}}U##]	" 	"s   C55C>N)r@   rA   rB   rC   r.   r  r{  rD   r/   r-   rm  rm    s    "!;$r/   rm  )QrC   r   rr   rU   rO   r   r   rL   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   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$   rF   r   r   r   r   r   rZ  ProcessProtocolre  r  r  rL  rm  rD   r/   r-   <module>r     s=  
    	  
  %  &  . U  H H ' 4 ;  / , * 0 +^,)*	.92E;;R 	D, :
 : :7D IJJI 	?@$
h $
 A$
N_
8 _
D  #. #.L9 96 Y/& /& /&d 	?@p
x p
 Ap
f. .xh.. xv$+ $+N 	?@K"4 K AK\ 	?@d0 d AdN 	?@E%L@AE&MBCG$1 G$ D B AG$_+  s   $,G0 0G98G9