o
    Òó·iÆÆ  ã                   @   sì  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ƒZ0e-dƒZ1e0rÒz.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? W n	 e@yÑ   Y nw dZAde1e0e Bed¡fv rádZAeeAdƒG d d!„ d!e/ƒƒZCG d"d#„ d#e/ƒZDG d$d%„ d%e!ƒZEG d&d'„ d'ƒZFG d(d)„ d)ƒZGeeƒG d*d+„ d+e	ƒƒZHeeAdƒG d,d-„ d-e/ƒƒZIG d.d/„ d/ƒZJG d0d1„ d1ejKƒZLG d2d3„ d3eƒZMeeAdƒG d4d5„ d5eMƒƒZNeeAdƒG d6d7„ d7eMƒƒZOeeAdƒee+d8ƒ d9ƒee+d:ƒ d;ƒG d<d=„ d=eMƒƒƒƒZPd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)Ú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                   @   s.   e Zd ZdZd
dd„Zd
dd„Zd
dd	„ZdS )ÚSSHSessionTestsz=
    Tests for L{twisted.conch.scripts.cftp.SSHSession}.
    ÚreturnNc                 C   s<   t ƒ | _tƒ | _| j| j_tƒ | _t| jƒ| _| j| j_d S ©N)r    Ústdior   Úchannelr   ÚstderrBufferr   Ústderr©Úself© r+   úbC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/conch/test/test_cftp.pyÚsetUp>   s   
zSSHSessionTests.setUpc                 C   s   | j  ¡  |  | jj¡ dS )z|
        L{twisted.conch.scripts.cftp.SSHSession.eofReceived} loses the write
        half of its stdio connection.
        N)r&   ZeofReceivedÚ
assertTruer%   ZwriteConnLostr)   r+   r+   r,   Útest_eofReceivedF   s   
z SSHSessionTests.test_eofReceivedc                 C   s:   d}|  d¡}| j t|d ¡ |  | j ¡ |d ¡ 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&   ZextReceivedr   ÚassertEqualr'   Úgetvalue)r*   Z	errorTextZ
errorBytesr+   r+   r,   Útest_extReceivedStderrN   s   
þþz&SSHSessionTests.test_extReceivedStderr)r#   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r/   r5   r+   r+   r+   r,   r"   8   s
    

r"   c                
   @   sÂ   e Zd ZdZeeddƒdu rdZdd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Ze ¡ Zz!z	e ejd¡ W n ejy>   dZY nw dZW e eje¡ ne eje¡ w eedƒdd„ ƒZd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                    sj   dˆ _ ‡ fdd„}ˆ  td|¡ dtjv r*ˆ  tjtjdtjd ¡ ˆ  tj	¡ dS dd„ }ˆ  |¡ dS )	zo
        Patch the L{ls} module's time function so the results of L{lsLine} are
        deterministic.
        iÍ[c                      s   ˆ j S r$   )Únowr+   r)   r+   r,   ÚfakeTimep   s   z$ListingTests.setUp.<locals>.fakeTimeÚtimeÚTZc                   S   s,   zt jd= W n	 ty   Y nw t ¡  d S )Nr?   )ÚosÚenvironÚKeyErrorr>   r;   r+   r+   r+   r,   Úcleanup|   s   ÿz#ListingTests.setUp.<locals>.cleanupN)
r<   Úpatchr   r@   rA   Ú
addCleanupÚoperatorÚsetitemr>   r;   )r*   r=   rC   r+   r)   r,   r-   i   s   
zListingTests.setUpc                 C   s   |t jd< t ¡  t d|¡S )zl
        Call L{ls.lsLine} after setting the timezone to C{timezone} and return
        the result.
        r?   ÚfooN)r@   rA   r>   r;   r   ZlsLine)r*   ÚtimezoneÚstatr+   r+   r,   Ú_lsInTimezone†   s   
zListingTests._lsInTimezonec                 C   óT   | j d }t 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©r<   r@   Ústat_resultr3   rK   ©r*   ÚthenrJ   r+   r+   r,   Útest_oldFile‘   ó   

þ
þzListingTests.test_oldFilec                 C   óX   | j d d }t 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.
        rM   i€— r   rN   z;!---------    0 0        0               0 May 01  1973 foorO   z;!---------    0 0        0               0 May 02  1973 fooNrP   rR   r+   r+   r,   Útest_oldSingleDigitDayOfMonth£   ó   
þ
þz*ListingTests.test_oldSingleDigitDayOfMonthc                 C   rL   )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   rN   ú;!---------    0 0        0               0 Aug 28 17:33 foorO   ú;!---------    0 0        0               0 Aug 29 09:33 fooNrP   rR   r+   r+   r,   Útest_newFile·   rU   zListingTests.test_newFileú
es_AR.UTF8TFz'The es_AR.UTF8 locale is not installed.c                 C   s|   | j d }t dddddddd|df
¡}t ¡ }t tjd¡ |  tjtj|¡ |  |  	d|¡d¡ |  |  	d|¡d¡ dS )	zC
        The month name in the date is locale independent.
        rZ   r   r^   rN   r[   rO   r\   N)
r<   r@   rQ   ÚlocaleÚ	getlocaleÚ	setlocaleÚLC_ALLrE   r3   rK   )r*   rS   rJ   ÚcurrentLocaler+   r+   r,   Útest_localeIndependentÖ   s   

þ
þz#ListingTests.test_localeIndependentc                 C   rV   )	rW   rZ   i F r   rN   z;!---------    0 0        0               0 Sep 01 17:33 foorO   z;!---------    0 0        0               0 Sep 02 09:33 fooNrP   rR   r+   r+   r,   Útest_newSingleDigitDayOfMonthí   rY   z*ListingTests.test_newSingleDigitDayOfMonth)r6   r7   r8   r9   Úgetattrr>   Úskipr-   rK   rT   rX   r]   r_   r`   rc   ra   rb   ÚErrorZ
localeSkipr   rd   re   r+   r+   r+   r,   r:   `   s,    ÿ€ 
r:   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚInMemorySSHChannelzp
    Minimal implementation of a L{SSHChannel} like class which only reads and
    writes data from memory.
    c                    s   || _ d| _tƒ  ¡  dS )zt
        @param conn: The SSH connection associated with this channel.
        @type conn: L{SSHConnection}
        r   N)ÚconnZlocalClosedÚsuperÚ__init__)r*   rj   ©Ú	__class__r+   r,   rl     s   zInMemorySSHChannel.__init__)r6   r7   r8   r9   rl   Ú__classcell__r+   r+   rm   r,   ri     s    ri   c                   @   ó(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚFilesystemAccessExpectationszC
    A test helper used to support expected filesystem access.
    c                 C   s
   i | _ d S r$   ©Ú_cacher)   r+   r+   r,   rl     ó   
z%FilesystemAccessExpectations.__init__c                 C   s   || 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}
        Nrr   )r*   ÚpathÚflagsÚstreamr+   r+   r,   Úput  s   z FilesystemAccessExpectations.putc                 C   s   | 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}
        N)rs   Úpop)r*   ru   rv   r+   r+   r,   ry   (  s   z FilesystemAccessExpectations.popN)r6   r7   r8   r9   rl   rx   ry   r+   r+   r+   r,   rq     s
    rq   c                   @   s    e Zd ZdZdd„ Zd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                 C   s    t | ƒ| _dddœ| _|| _d S )Né   é
   )ÚrequestsÚ
buffersize)ri   Ú	transportÚoptionsÚ_availableFiles)r*   ZavailableFilesr+   r+   r,   rl   B  s
   
þ
zInMemorySFTPClient.__init__c                 C   s   | j  ||¡S )z}
        @see: L{filetransfer.FileTransferClient.openFile}.

        Retrieve and remove cached file based on flags.
        N)r�   ry   )r*   Úfilenamerv   Úattrsr+   r+   r,   ÚopenFileJ  s   zInMemorySFTPClient.openFileN)r6   r7   r8   r9   rl   r„   r+   r+   r+   r,   rz   8  s    	rz   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚInMemoryRemoteFilez;
    An L{ISFTPFile} which handles all data in memory.
    c                 C   s   || _ t | ¡ dS )zL
        @param name: Name of this file.
        @type name: L{str}
        N)Únamer   rl   )r*   r†   r+   r+   r,   rl   Y  s   zInMemoryRemoteFile.__init__c                 C   s   |   |¡ |  |¡ t | ¡S )z/
        @see: L{ISFTPFile.writeChunk}
        N)ÚseekÚwriter   Úsucceed)r*   ÚstartÚdatar+   r+   r,   Ú
writeChunka  s   


zInMemoryRemoteFile.writeChunkc                 C   s
   d| _ dS )zo
        @see: L{ISFTPFile.writeChunk}

        Keeps data after file was closed to help with testing.
        TN)Ú_closedr)   r+   r+   r,   Úclosei  ó   
zInMemoryRemoteFile.closec                 C   ó   d S r$   r+   r)   r+   r+   r,   ÚgetAttrsq  ó   zInMemoryRemoteFile.getAttrsc                 C   r�   r$   r+   )r*   ÚoffsetÚlengthr+   r+   r,   Ú	readChunku  r’   zInMemoryRemoteFile.readChunkc                 C   r�   r$   r+   )r*   rƒ   r+   r+   r,   ÚsetAttrsy  r’   zInMemoryRemoteFile.setAttrsc                 C   s
   t  | ¡S )zb
        Get current data of file.

        Allow reading data event when file is closed.
        N)r   r4   r)   r+   r+   r,   r4   }  r�   zInMemoryRemoteFile.getvalueN)r6   r7   r8   r9   rl   rŒ   rŽ   r‘   r•   r–   r4   r+   r+   r+   r,   r…   S  s    r…   c                   @   s¤   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd)dd„Zd*dd „Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ ZdS )+ÚStdioClientTestsz(
    Tests for L{cftp.StdioClient}.
    c                 C   sT   t ƒ | _t| jƒ}t |¡| _d| j_tƒ  | _| 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)rq   ÚfakeFilesystemrz   r   ÚStdioClientÚclientZcurrentDirectoryr   ÚdatabaseÚ_pwdÚsetKnownConsoleSizer   )r*   Z
sftpClientr+   r+   r,   r-   Œ  s   
zStdioClientTests.setUpc              	   C   s@   | j  t ¡ dt ¡ 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éÒ  rH   Úbarzexec print(1 + 2)s   3
N)r�   ÚaddUserÚgetpassÚgetuserr@   ÚgetuidÚsysÚ
executablerœ   Ú_dispatchCommandÚaddCallbackr3   ©r*   Údr+   r+   r,   Ú	test_execŸ  s   ÿzStdioClientTests.test_execc              	   C   ó>   | j  t ¡ dt ¡ 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¡   rH   r¢   Ú zexec echo helloó   hello
N©
r�   r£   r¤   r¥   r@   r¦   rœ   r©   rª   r3   r«   r+   r+   r,   Útest_execWithoutShell¬  s   ÿz&StdioClientTests.test_execWithoutShellc              	   C   r®   )	zO
        The I{exec} command is run for lines which start with C{"!"}.
        r    r¡   rH   r¢   z/bin/shz!echo hellor°   Nr±   r«   r+   r+   r,   Ú	test_bang¹  s   ÿzStdioClientTests.test_bangc                    s2   ddl ‰G ‡ ‡‡fd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                       s   e Zd Z‡ ‡‡fdd„ZdS )z7StdioClientTests.setKnownConsoleSize.<locals>.FakeFcntlc                    s&   |ˆj kr
|  d¡ t dˆ ˆdd¡S )Nz#Only window-size queries supported.Z4Hr   )Z
TIOCGWINSZZfailÚstructÚpack)r*   ÚfdÚoptZmutate©ÚheightÚttyÚwidthr+   r,   ÚioctlÓ  s   

z=StdioClientTests.setKnownConsoleSize.<locals>.FakeFcntl.ioctlN)r6   r7   r8   r¼   r+   r¸   r+   r,   Ú	FakeFcntlÒ  s    r½   Úfcntl)rº   rD   r   )r*   r»   r¹   r½   r+   r¸   r,   rŸ   Å  s   z$StdioClientTests.setKnownConsoleSizec                 C   s‚   |   dd¡ tƒ  }| j_tdƒ}d|_t |¡}d|_| 	¡ }| 
d¡ | jd7  _| 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ÚsecondsZadvanceÚtotalÚ_printProgressBarr3   r   Úvalue©r*   ZclockÚwrappedÚwrapperZ	startTimeÚresultr+   r+   r,   Útest_printProgressBarReportingÚ  s   	

z/StdioClientTests.test_printProgressBarReportingc                 C   sd   |   dd¡ tƒ  }| j_tdƒ}d|_t |¡}| ¡ }| 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Æ   r3   r   rÇ   rÈ   r+   r+   r,   Útest_printProgressBarNoProgressò  s   
z0StdioClientTests.test_printProgressBarNoProgressc                 C   sL   |   dd¡ tƒ }d|_t |¡}| 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Æ   r3   r   rÇ   )r*   rÉ   rÊ   rË   r+   r+   r,   Útest_printProgressBarEmptyFile  s   
z/StdioClientTests.test_printProgressBarEmptyFilec                 C   ó   | j  d¡}|  d|¡ dS )zK
        Returns empty value for both filename and remaining data.
        z  )r¯   r¯   N©rœ   Z_getFilenamer3   ©r*   rË   r+   r+   r,   Útest_getFilenameEmpty  s   z&StdioClientTests.test_getFilenameEmptyc                 C   rÏ   )zd
        Returns empty value for remaining data when line contains
        only a filename.
        ú
only-local)rÓ   r¯   NrÐ   rÑ   r+   r+   r,   Útest_getFilenameOnlyLocal  ó   z*StdioClientTests.test_getFilenameOnlyLocalc                 C   rÏ   )ze
        Returns filename and remaining data striped of leading and trailing
        spaces.
        z local  remote file  )Úlocalzremote fileNrÐ   rÑ   r+   r+   r,   Útest_getFilenameNotQuoted#  rÕ   z*StdioClientTests.test_getFilenameNotQuotedc                 C   rÏ   )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Ñ   r+   r+   r,   Útest_getFilenameQuoted,  rÕ   z'StdioClientTests.test_getFilenameQuotedNó    c                 C   sJ   |du r|   ¡ }t|dƒ�}| |¡ W d  ƒ |S 1 s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*   ru   ÚcontentÚfiler+   r+   r,   ÚmakeFile5  s   
ÿþzStdioClientTests.makeFileFc                 C   sh  | j j ¡ }| d¡}| d¡}g }g }|D ]\}}}g }	|D ]}
|	 |› d|
› �¡ q|	 d|› d|› �¡ | |	¡ | d¡ d¡ d¡}|dd	… }|d	  d
¡ d
¡}| |¡ g }|dd	… D ]$}
|
 ¡  	dd¡d }
|
 ¡  dd¡}
| |
d › d|
d  ¡ › �¡ qd| |d	 ¡ | |¡ q|r£|  
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

        r0   z
ú zTransferred z to r   úNéÿÿÿÿÚ
é   r{   z5There are still put responses which were not checked.)rœ   r   rÇ   ÚdecodeÚsplitÚappendry   ÚstripÚextendÚrsplitr3   ÚsortedÚlen)r*   Z	transfersÚrandomOrderÚoutputÚexpectedOutputZactualOutputrÖ   ÚremoteÚexpectedZexpectedTransferÚlineZprogressPartsÚactualÚlastZactualTransferr+   r+   r,   ÚcheckPutMessageI  s<   




"ýz StdioClientTests.checkPutMessagec                 C   s°   d}| j |d�}tjtjB tjB }tj dtj |¡¡}t	|ƒ}| j
 ||t |¡¡ 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.0Brö   N)rß   r   Ú	FXF_WRITEÚ	FXF_CREATÚ	FXF_TRUNCr@   ru   ÚjoinÚbasenamer…   rš   rx   r   r‰   rœ   r€   Úcmd_PUTÚsuccessResultOfr3   r4   r.   r�   rõ   )r*   rÝ   Ú	localPathrv   Ú
remoteNameÚ
remoteFileÚdeferredr+   r+   r,   Útest_cmd_PUTSingleNoRemotePath†  s   
ÿz/StdioClientTests.test_cmd_PUTSingleNoRemotePathc                 C   s’   |   ¡ }tjtjB tjB }d}t|ƒ}| j ||t 	|¡¡ | 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.0BrÙ   N)rß   r   r÷   rø   rù   r…   rš   rx   r   r‰   rœ   rü   rý   rõ   r.   r�   r3   r4   )r*   rþ   rv   rÿ   r   r  r+   r+   r,   Útest_cmd_PUTSingleRemotePathž  s   
z-StdioClientTests.test_cmd_PUTSingleRemotePathc                 C   s  |   ¡ }tj |¡}d}tj |¡}| j tj ||¡d�}tjtjB tj	B }d|› �}d|› �}t
|ƒ}	t
|ƒ}
| j ||t |	¡¡ | j ||t |
¡¡ | j tj |d¡¡}|  |¡ |  |	j¡ |  d|	 ¡ ¡ |  |
j¡ |  d|
 ¡ ¡ | j||dgf||dgfgdd� d	S )
z„
        When a gobbing expression is used local files are transferred with
        remote file names based on local names.
        úsecond-name©ru   r˜   Ú*rÙ   r  T©rí   N)rß   r@   ru   rû   Údirnamerú   r   r÷   rø   rù   r…   rš   rx   r   r‰   rœ   rü   rý   r.   r�   r3   r4   rõ   )r*   ÚfirstÚ	firstNameÚ
secondNameÚparentÚsecondrv   ÚfirstRemotePathÚsecondRemotePathÚfirstRemoteFileÚsecondRemoteFiler  r+   r+   r,   Ú test_cmd_PUTMultipleNoRemotePath±  s4   

ÿ


þ
ûz1StdioClientTests.test_cmd_PUTMultipleNoRemotePathc                 C   s   |   ¡ }tj |¡}d}tj |¡}| j tj ||¡d�}tjtjB tj	B }t
|ƒ}t
|ƒ}d|› �}	d|› �}
| j |	|t |¡¡ | j |
|t |¡¡ | j d tj |d¡¡¡}|  |¡ |  |j¡ |  d| ¡ ¡ |  |j¡ |  d| ¡ ¡ | j||dgf||dgfgd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.
        r  r  z/remote/z	{} remoter  rÙ   r  Tr  N)rß   r@   ru   rû   r	  rú   r   r÷   rø   rù   r…   rš   rx   r   r‰   rœ   rü   Úformatrý   r.   r�   r3   r4   rõ   )r*   r
  r  r  r  r  rv   r  r  r  r  r  r+   r+   r,   Ú"test_cmd_PUTMultipleWithRemotePathÔ  s4   

ÿ


þ
ûz3StdioClientTests.test_cmd_PUTMultipleWithRemotePath)NrÙ   )F)r6   r7   r8   r9   r-   r­   r²   r³   rŸ   rÌ   rÍ   rÎ   rÒ   rÔ   r×   rØ   rß   rõ   r  r  r  r  r+   r+   r+   r,   r—   †  s(    		
	
=#r—   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚFileTransferTestRealmc                 C   s
   || _ d S r$   )ÚtestDir)r*   r  r+   r+   r,   rl   û  rt   zFileTransferTestRealm.__init__c                 G   s   t | jƒ}|d |dd„ fS )Nr   c                   S   r�   r$   r+   r+   r+   r+   r,   Ú<lambda>   s    z5FileTransferTestRealm.requestAvatar.<locals>.<lambda>)r   r  )r*   ZavatarIDZmindr   Úar+   r+   r,   ÚrequestAvatarþ  s   
z#FileTransferTestRealm.requestAvatarN)r6   r7   r8   rl   r  r+   r+   r+   r,   r  ú  s    r  c                   @   s`   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zd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                 C   s$   |   ¡  || _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*   r  r+   r+   r,   rl   
  s
   
zSFTPTestProcess.__init__c                 C   s   d| _ g | _d| _dS )zR
        Clear any buffered data received from stdout. Should be private.
        rÙ   N)ÚbufferÚ_linesReceivedÚ_lineBufferr)   r+   r+   r,   r    s   
zSFTPTestProcess.clearBufferc                 C   sr   t  d| ¡ | j|  d¡}| d¡| _| j |¡ | jdur,| jd}| _| |¡ |  j	|7  _	|  
¡  dS )zO
        Called by Twisted when the cftp client prints data to stdout.
        zgot %ró   
râ   N)r   Úmsgr#  ræ   ry   r"  ré   r  Úcallbackr!  Ú_checkForCommand)r*   r‹   Úlinesr¬   r+   r+   r,   ÚoutReceived  s   

zSFTPTestProcess.outReceivedc                 C   sf   d}| j r/| j|kr1d | j¡}| |¡r|t|ƒd … }|  ¡  | j d }| _ | |¡ d S d S d S )Ns   cftp> r$  )r  r#  rú   r"  Ú
startswithrì   r  r&  )r*   ÚpromptÚbufr¬   r+   r+   r,   r'  .  s   
úz SFTPTestProcess._checkForCommandc                 C   s   t  d| ¡ dS )zO
        Called by Twisted when the cftp client prints data to stderr.
        zerr: %sN)r   r%  )r*   r‹   r+   r+   r,   ÚerrReceived8  s   zSFTPTestProcess.errReceivedc                 C   s   | j S )zQ
        Return the contents of the buffer of data received from stdout.
        N)r!  r)   r+   r+   r,   Ú	getBuffer>  s   zSFTPTestProcess.getBufferc                 C   s<   t  ¡ | _|  ¡  t|tƒr| d¡}| j |d ¡ | 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.
        r0   r$  N)	r   ÚDeferredr  r  Ú
isinstanceÚstrr2   r   rˆ   ©r*   Úcommandr+   r+   r,   Ú
runCommandD  s   


zSFTPTestProcess.runCommandc                    s(   t  d¡‰‡ ‡fdd„|D ƒ}t  |¡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                    s   g | ]	}ˆ  ˆ j|¡‘qS r+   )Úrunr4  )Ú.0r3  ©r*   Zsemr+   r,   Ú
<listcomp>b  s    z-SFTPTestProcess.runScript.<locals>.<listcomp>N)r   ZDeferredSemaphoreZgatherResults)r*   ÚcommandsÚdlr+   r7  r,   Ú	runScriptV  s   

zSFTPTestProcess.runScriptc                 C   s,   | j rt d¡S t ¡ | _| 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)r   r   r‰   r/  r  r   ÚsignalProcessr)   r+   r+   r,   ÚkillProcesse  s
   	

zSFTPTestProcess.killProcessc                 C   s,   d| _ | jr| jd}| _| d¡ dS dS )zF
        Called by Twisted when the cftp client process ends.
        TN)r   r  r&  )r*   Úreasonr¬   r+   r+   r,   ÚprocessEndedt  s
   þzSFTPTestProcess.processEndedN)r6   r7   r8   r9   rl   r  r)  r'  r-  r.  r4  r;  r>  r@  r+   r+   r+   r,   r    s    
r  c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚCFTPClientTestBasec                 C   s¼   t ddƒ�}| tj¡ W d   ƒ n1 sw   Y  t ddƒ�}| tj¡ W d   ƒ n1 s1w   Y  t dd¡ t ddƒ�}| dtj ¡ W d   ƒ n1 sTw   Y  t | ¡S )Núrsa_test.pubrÚ   Úrsa_testi€�  Úkh_tests
   127.0.0.1 )	rÜ   rˆ   r   ZpublicRSA_opensshZprivateRSA_opensshr@   Úchmodr	   r-   ©r*   Úfr+   r+   r,   r-     s   ÿÿÿ
zCFTPClientTestBase.setUpc                 C   sF   t | jƒ}t |¡}| t ¡ ¡ t ¡ }||_tj	d|dd�| _
d S )Nr   z	127.0.0.1)Z	interface)r  r  r
   ZPortalZregisterCheckerr   ZconchTestPublicKeyCheckerZConchTestServerFactoryr   Z	listenTCPÚserver)r*   ÚrealmÚpZfacr+   r+   r,   ÚstartServer‰  s   

zCFTPClientTestBase.startServerc                 C   sH   t | jjdƒs|  d ¡S d| jjj_t | jjjjj	¡}| 
| j¡ |S )NÚprotor{   )ÚhasattrrH  ÚfactoryÚ_cbStopServerrL  ÚexpectedLoseConnectionr   ÚmaybeDeferredr   ZloseConnectionrª   r«   r+   r+   r,   Ú
stopServer‘  s   
zCFTPClientTestBase.stopServerc                 C   s   t  | jj¡S r$   )r   rQ  rH  ÚstopListening©r*   Úignoredr+   r+   r,   rO  ™  s   z CFTPClientTestBase._cbStopServerc              	   C   s4   dD ]}zt  |¡ W q ty   Y qw t | ¡S )N)rB  rC  rD  )r@   ÚremoveÚBaseExceptionr	   ÚtearDownrF  r+   r+   r,   rX  œ  s   ÿ
zCFTPClientTestBase.tearDownN)r6   r7   r8   r-   rK  rR  rO  rX  r+   r+   r+   r,   rA  ~  s    
rA  c                   @   sº   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zd-dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+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           
         s0  t  ˆ ¡ ˆ  ¡  d}ˆ j ¡ j}tj||  ¡ dd�}t	 
dtj› d|› �¡ t ¡ }t|ƒˆ _| ‡ fdd„¡ tj ¡ }tj tj¡|d< g }i }|D ]}t|tƒrZ| d	¡}| |¡ qN|D ]}|| }	t|tƒrr| d	¡}t|	tƒr||	 d	¡}	|	||< qbt	 
|¡ t	 
|¡ tjˆ jtj||d
� |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                    ó
   ˆ j  ¡ S r$   )ÚprocessProtocolr  ©Ú_r)   r+   r,   r  Â  ó   
 z3OurServerCmdLineClientTests.setUp.<locals>.<lambda>Ú
PYTHONPATHr0   ©Úenv)rA  r-   rK  rH  ÚgetHostÚportr   Ú	_makeArgsræ   r   r%  r§   r¨   r   r/  r  r^  rª   r@   rA   ÚcopyÚpathseprú   ru   r0  r1  r2   rç   r   ZspawnProcess)
r*   Úcmdsrf  r¬   rd  ZencodedCmdsZ
encodedEnvÚcmdÚvarÚvalr+   r)   r,   r-   ¯  s>   
ÿ











ÿz!OurServerCmdLineClientTests.setUpc                    s   ˆ   ¡ }| ‡ fdd„¡ |S )Nc                    r]  r$   )r^  r>  r_  r)   r+   r,   r  Û  ra  z6OurServerCmdLineClientTests.tearDown.<locals>.<lambda>)rR  rª   r«   r+   r)   r,   rX  Ù  s   z$OurServerCmdLineClientTests.tearDownc                 C   s,   z
| j j d¡ W d S  tjy   Y d S w )Nr<  )r^  r   r=  r   ZProcessExitedAlreadyrT  r+   r+   r,   Ú_killProcessÞ  s
   ÿz(OurServerCmdLineClientTests._killProcessc                 C   ó   | 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.
        N)r^  r4  r2  r+   r+   r,   r4  ä  ó   z&OurServerCmdLineClientTests.runCommandc                 G   ro  )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.
        N)r^  r;  )r*   r9  r+   r+   r,   r;  ì  rp  z%OurServerCmdLineClientTests.runScriptc                 C   sL   | j }|  ddddd¡}dd„ }| |¡ | | j|jt ¡ 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.
        ÚpwdZlpwdzcd testDirectoryúcd ..c                 S   sD   g }| D ]}t |tƒr| d¡}| |¡ q|dd… |dd…  S )zH
            Callback function for handling command output.
            r0   Né   é   )r0  Úbytesrå   rç   )rî   rj  rk  r+   r+   r,   Ú	cmdOutputÿ  s   

z8OurServerCmdLineClientTests.testCdPwd.<locals>.cmdOutputr¯   N)r  r;  rª   r3   ru   r@   Úgetcwd)r*   ZhomeDirr¬   rv  r+   r+   r,   Ú	testCdPwdô  s   
z%OurServerCmdLineClientTests.testCdPwdc                    s&   ‡ fd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                    sZ   ˆ   ¡  ˆ  | d  d¡¡ ˆ  | d d¡ ˆ  | d  d¡| d ¡ ˆ  | d d¡ d S )Nr   s
   -rw-r--r--r{   rÙ   rä   s
   ----------rs  )ÚflushLoggedErrorsr.   r*  r3   ©Úresultsr)   r+   r,   Ú_check  s
   z7OurServerCmdLineClientTests.testChAttrs.<locals>._checkzls -l testfile1zchmod 0 testfile1zchmod 644 testfile1N©r;  rª   ©r*   r|  r¬   r+   r)   r,   ÚtestChAttrs  s   ü
z'OurServerCmdLineClientTests.testChAttrsc                    s@   ‡ fdd„}ˆ   ddˆ j ¡  ddd¡}| dd	„ ¡ | |¡S )zŽ
        Check 'ls' works as expected. Checks for wildcards, hidden files,
        listing directories and listing empty directories.
        c                    sf   ˆ   | 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‚  rs  )s   .testHiddenFiler�  r‚  rt  rÙ   ©r3   rz  r)   r+   r,   r|  *  s   þþÿz4OurServerCmdLineClientTests.testList.<locals>._checkr   zls ../zls *Filezls -a *Filezls -l testDirectoryc                 S   s   dd„ | D ƒS )Nc                 S   s   g | ]}|  d ¡‘qS )r$  )ræ   )r6  Úxr+   r+   r,   r8  @  ó    zJOurServerCmdLineClientTests.testList.<locals>.<lambda>.<locals>.<listcomp>r+   )Úxsr+   r+   r,   r  @  s    z6OurServerCmdLineClientTests.testList.<locals>.<lambda>N)r;  r  rû   rª   r~  r+   r)   r,   ÚtestList$  s   û
z$OurServerCmdLineClientTests.testListc                 C   sD   |   d¡}t d¡ d¡ ¡ }t|tƒr| d¡}| | j	|¡ |S )zB
        Check that running the '?' command returns help.
        ú?Nr¯   r0   )
r4  r   r›   Zcmd_HELPrè   r0  r1  r2   rª   r3   )r*   r¬   ZhelpTextr+   r+   r,   ÚtestHelpC  s   


z$OurServerCmdLineClientTests.testHelpNc                 C   s   |   | ¡ | ¡ |¡ dS )zg
        Assert that the files at C{name1} and C{name2} contain exactly the
        same data.
        N)r3   Z
getContent)r*   Zname1Zname2r%  r+   r+   r,   ÚassertFilesEqualO  s   z,OurServerCmdLineClientTests.assertFilesEqualc                    sl   d  ˆjjˆjj¡‰ tˆ tƒrˆ  d¡‰ ‡ ‡fdd„}ˆ dˆjj› d�¡}| |¡ | ‡fd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 file2r0   c                    s8   ˆ  |  ˆ ¡¡ ˆ ˆj d¡ˆj d¡d¡ ˆ d¡S )NÚ	testfile1ú
test file2z
get failedzrm "test file2")r.   ÚendswithrŠ  r  Úchildr4  ©rË   ©rï   r*   r+   r,   Ú	_checkGetd  s   

ý
z6OurServerCmdLineClientTests.testGet.<locals>._checkGetzget testfile1 "z/test file2"c                    ó   ˆ   ˆ j d¡ ¡ ¡S )NrŒ  ©ZassertFalser  rŽ  Úexistsr_  r)   r+   r,   r  p  r…  z5OurServerCmdLineClientTests.testGet.<locals>.<lambda>N)r  r  ru   r0  r1  r2   r4  rª   )r*   r‘  r¬   r+   r�  r,   ÚtestGetV  s   þ

	

ÿz#OurServerCmdLineClientTests.testGetc                    s    ‡ fdd„}ˆ   d¡}| |¡S )zQ
        Test that 'get' works correctly when given wildcard parameters.
        c                    s8   ˆ   ˆ 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   )rU  r)   r+   r,   r|  y  s   
ý
ýz;OurServerCmdLineClientTests.testWildcardGet.<locals>._checkz
get testR*N)r4  rª   r~  r+   r)   r,   ÚtestWildcardGett  s   

z+OurServerCmdLineClientTests.testWildcardGetc                    sh   dˆj  ¡ j d ˆj  ¡ j d ‰ ‡ ‡fdd„}ˆ dˆj j› d�¡}| |¡ | ‡fd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                    s6   ˆ  ˆj d¡ˆj d¡¡ ˆ |  ˆ ¡¡ ˆ d¡S )Nr‹  ú
test"file2zrm "test\"file2")rŠ  r  rŽ  r.   r�  r4  r�  r�  r+   r,   Ú	_checkPut–  s
   ÿ
z6OurServerCmdLineClientTests.testPut.<locals>._checkPutúput z/testfile1 "test\"file2"c                    r’  )Nr›  r“  r_  r)   r+   r,   r     r…  z5OurServerCmdLineClientTests.testPut.<locals>.<lambda>N)r  ÚasBytesModeru   r4  rª   )r*   rœ  r¬   r+   r�  r,   ÚtestPutˆ  s"   
ÿþ
ýüÿ

ÿz#OurServerCmdLineClientTests.testPutc                    s¬   ˆ j  d¡jdd��}| d¡ W d  ƒ n1 sw   Y  ˆ j  d¡jdd��}| d¡ W d  ƒ n1 s9w   Y  ‡ fdd	„}ˆ  d
ˆ j j› d�¡}| |¡ |S )zb
        Check that 'put' uploads files correctly when overwriting a longer
        file.
        ÚshorterFileÚw©Úmodeó   aNÚ
longerFileó   bbc                    s    ˆ   ˆ j d¡ˆ j d¡¡ d S )Nr   r¥  )rŠ  r  rŽ  r�  r)   r+   r,   rœ  ¯  s   ÿzEOurServerCmdLineClientTests.test_putOverLongerFile.<locals>._checkPutr�  z/shorterFile longerFile)r  rŽ  rÜ   rˆ   r4  ru   rª   ©r*   rG  rœ  r¬   r+   r)   r,   Útest_putOverLongerFile¤  s   ÿÿ
z2OurServerCmdLineClientTests.test_putOverLongerFilec                    sÀ   ˆ j  d¡‰ˆ ¡  ˆ d¡jdd��}| d¡ W d  ƒ n1 s#w   Y  ˆ j  d¡jdd��}| d¡ W d  ƒ n1 sBw   Y  ‡ ‡fd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¤  Nr¦  c                    s   ˆ   ˆ d¡ˆ j d¡¡ d S )NrÞ   )rŠ  rŽ  r  r�  ©r*   ZsomeDirr+   r,   rœ  Å  s   zMOurServerCmdLineClientTests.test_putMultipleOverLongerFile.<locals>._checkPutr�  z/dir/*)r  rŽ  ZcreateDirectoryrÜ   rˆ   r4  ru   rª   r§  r+   rª  r,   Útest_putMultipleOverLongerFile¸  s   ÿÿ
z:OurServerCmdLineClientTests.test_putMultipleOverLongerFilec                    s>   ‡ fdd„}ˆ   ddˆ jj› d�dˆ 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                    sh   ˆ   | d d¡ ˆ   | d d¡ ˆ  ˆ j d¡ˆ j ¡  d¡d¡ ˆ  ˆ j d¡ˆ j ¡  d¡d¡ d S )Nr   rÙ   rä   r–  r—  r˜  r™  )r3   rŠ  r  rŽ  r  rz  r)   r+   r,   ÚcheckÓ  s   
ý
ýz:OurServerCmdLineClientTests.testWildcardPut.<locals>.checkrr  r�  z/testR*zcd %sN)r;  r  ru   rû   rª   )r*   r¬  r¬   r+   r)   r,   ÚtestWildcardPutÌ  s   ý
z+OurServerCmdLineClientTests.testWildcardPutc                    ó4   ‡ fd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                    s8   ˆ   ¡  ˆ  | d d¡ ˆ  | d  d¡d¡ ˆ  d¡S )Nr   rÙ   r{   ó   lzlink failedzrm testLink)ry  r3   r.   r*  r4  rz  r)   r+   r,   r|  ð  s   
z4OurServerCmdLineClientTests.testLink.<locals>._checkzln testLink testfile1zls -l testLinkrÙ   N©r;  rª   r3   r~  r+   r)   r,   ÚtestLinkê  s
   
z$OurServerCmdLineClientTests.testLinkc                    r®  )zV
        Test that we can create and remove directories with the cftp client.
        c                    s.   ˆ   | d d¡ ˆ  | d  d¡¡ ˆ  d¡S )Nr   rÙ   r{   ó   dzrmdir testMakeDirectory)r3   r.   r*  r4  rz  r)   r+   r,   r|     s   
z?OurServerCmdLineClientTests.testRemoteDirectory.<locals>._checkúmkdir testMakeDirectoryzls -l testMakeDirector?rÙ   Nr°  r~  r+   r)   r,   ÚtestRemoteDirectoryû  ó
   
z/OurServerCmdLineClientTests.testRemoteDirectoryc                    s&   ‡ fd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                    s$   ˆ   | d d¡ ˆ   | d d¡ d S )Nr   rÙ   r{   s   remote error 11: mkdir failedrƒ  rz  r)   r+   r,   r|    s   zHOurServerCmdLineClientTests.test_existingRemoteDirectory.<locals>._checkr³  Nr}  r~  r+   r)   r,   Útest_existingRemoteDirectory
  s   
z8OurServerCmdLineClientTests.test_existingRemoteDirectoryc                    sH   ˆ   dˆ jj› d�¡}| ˆ jd¡ | ‡ fd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/testLocalDirectoryrÙ   c                    s
   ˆ   d¡S )Nzrmdir testLocalDirectory)r4  r_  r)   r+   r,   r     ra  z@OurServerCmdLineClientTests.testLocalDirectory.<locals>.<lambda>N)r4  r  ru   rª   r3   r«   r+   r)   r,   ÚtestLocalDirectory  s
   z.OurServerCmdLineClientTests.testLocalDirectoryc                    r®  )z1
        Test that we can rename a file.
        c                    s*   ˆ   | d d¡ ˆ   | d d¡ ˆ  d¡S )Nr   rÙ   r{   s	   testfile2zrename testfile2 testfile1)r3   r4  rz  r)   r+   r,   r|  )  s   
z6OurServerCmdLineClientTests.testRename.<locals>._checkzrename testfile1 testfile2zls testfile?rÙ   Nr°  r~  r+   r)   r,   Ú
testRename$  rµ  z&OurServerCmdLineClientTests.testRenamer$   )r6   r7   r8   r9   r-   rX  rn  r4  r;  rx  r  r‡  r‰  rŠ  r•  rš  rŸ  r¨  r«  r­  r±  r´  r¶  r·  r¸  r+   r+   r+   r,   rY  ¥  s.    *
rY  c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚOurServerBatchFileTestszm
    Functional tests which launch a SFTP server over localhost and checks csftp
    in batch interface.
    c                 C   s   t  | ¡ |  ¡  d S r$   ©rA  r-   rK  r)   r+   r+   r,   r-   ;  s   
zOurServerBatchFileTests.setUpc                 C   ó   t  | ¡ |  ¡ S r$   )rA  rX  rR  r)   r+   r+   r,   rX  ?  ó   
z OurServerBatchFileTests.tearDownc                    sä   |   ¡ ‰ tˆ dƒ�}| |¡ W d   ƒ n1 sw   Y  | j ¡ j}d|ˆ f }tj| ¡ dd�dd … }t	 
dtj› d|› �¡ tj ¡ }tj tj¡|d< d| jj_ttj||d	�}‡ fd
d„}| dd„ ¡ | |¡ |S )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   rZ  r{   r\  rà   rb  rc  c                    s   t  ˆ ¡ | S r$   )r@   rV  ©Úres©Úfnr+   r,   Ú_cleanupZ  s   
z9OurServerBatchFileTests._getBatchOutput.<locals>._cleanupc                 S   s   | d S )Nr   r+   r½  r+   r+   r,   r  ^  s    z9OurServerBatchFileTests._getBatchOutput.<locals>.<lambda>)rÛ   rÜ   rˆ   rH  re  rf  r   rg  ræ   r   r%  r§   r¨   r@   rA   rh  ri  rú   ru   rN  rP  r   rª   ZaddBoth)r*   rG  Úfprf  rj  rd  r¬   rÁ  r+   r¿  r,   Ú_getBatchOutputC  s$   ÿø	


z'OurServerBatchFileTests._getBatchOutputc                    ó(   d}‡ fd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                    sP   |   d¡} t dt| ƒ ¡ ˆ  ˆ j ¡ j| d ¡ ˆ  | dd… g d¢¡ d S )Nr$  zRES %sr{   rs  éþÿÿÿr€  )	ræ   r   r%  ÚreprÚassertInr  rž  ru   r3   r½  r)   r+   r,   Ú_cbCheckResultm  s   

þz=OurServerBatchFileTests.testBatchFile.<locals>._cbCheckResultN©rÃ  rª   ©r*   rj  rÈ  r¬   r+   r)   r,   ÚtestBatchFilec  s
   
	
z%OurServerBatchFileTests.testBatchFilec                    rÄ  )zO
        Test that an error in the batch file stops running the batch.
        zchown 0 missingFile
pwd
exit
c                    ó   ˆ   ˆ j ¡ j| ¡ d S r$   )ZassertNotInr  rž  ru   r½  r)   r+   r,   rÈ  ƒ  ó   z9OurServerBatchFileTests.testError.<locals>._cbCheckResultNrÉ  rÊ  r+   r)   r,   Ú	testErrorz  s
   

z!OurServerBatchFileTests.testErrorc                    rÄ  )z_
        Test that a minus sign '-' at the front of a line ignores
        any errors.
        z-chown 0 missingFile
pwd
exit
c                    rÌ  r$   )rÇ  r  rž  ru   r½  r)   r+   r,   rÈ  ”  rÍ  z@OurServerBatchFileTests.testIgnoredError.<locals>._cbCheckResultNrÉ  rÊ  r+   r)   r,   ÚtestIgnoredErrorŠ  s
   

z(OurServerBatchFileTests.testIgnoredErrorN)
r6   r7   r8   r9   r-   rX  rÃ  rË  rÎ  rÏ  r+   r+   r+   r,   r¹  4  s     r¹  Ússhz$no ssh command-line client availableÚsftpz%no sftp command-line client availablec                   @   rp   )	ÚOurServerSftpClientTestsz@
    Test the sftp server against sftp command line client.
    c                 C   r»  r$   rº  r)   r+   r+   r,   r-   ¤  r¼  zOurServerSftpClientTests.setUpc                 C   s   |   ¡ S r$   )rR  r)   r+   r+   r,   rX  ¨  s   z!OurServerSftpClientTests.tearDownc           	         sÆ   t tjƒ}ˆ ¡ }t|dƒ�}| d¡ W d  ƒ n1 sw   Y  ˆj ¡ j}t	j
‰ ‡ fdd„}ˆ t	d|¡ dˆjj_dd	d
dd
dd
dd
dd
d|f d|df}‡fdd„}t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                    s   ˆ | |ƒ}d|d< |S )Nr¢   Zext_foor+   )r*   Úsrƒ   )Ú
oldGetAttrr+   r,   Ú	_getAttrs¾  s   
zCOurServerSftpClientTests.test_extendedAttributes.<locals>._getAttrsrÕ  Tz-Fz	/dev/nullz-ozIdentitiesOnly=yeszIdentityFile=rsa_testzUserKnownHostsFile=kh_testzHostKeyAlgorithms=ssh-rsazPort=%iz-bztestuser@127.0.0.1c                    s:   ˆ   | d d| d  d¡¡ dD ]
}ˆ  || d ¡ qd S )Nrä   r   r{   Úasciir€  )r3   rå   rÇ  )rË   Úir)   r+   r,   r¬  Û  s   úz?OurServerSftpClientTests.test_extendedAttributes.<locals>.checkrÑ  )Údictr@   rA   rÛ   rÜ   rˆ   rH  re  rf  r!   rÕ  rD   rN  rP  r   rª   )	r*   rd  rÀ  rG  rf  rÕ  Úargsr¬  r¬   r+   )rÔ  r*   r,   Útest_extendedAttributes«  s:   

ÿ
ñ

z0OurServerSftpClientTests.test_extendedAttributesN)r6   r7   r8   r9   r-   rX  rÚ  r+   r+   r+   r,   rÒ  œ  s
    rÒ  )Qr9   r¤   r_   rF   r@   r´   r§   r>   Úior   r   Zunittestr   Zzope.interfacer   Ztwisted.conchr   Ztwisted.conch.interfacesr   Z$twisted.conch.test.test_filetransferr   r	   Ztwisted.credr
   Ztwisted.internetr   r   r   r   r   Ztwisted.internet.taskr   Ztwisted.internet.testingr   Ztwisted.internet.utilsr   Ztwisted.pythonr   Ztwisted.python.fakepwdr   Ztwisted.python.filepathr   Ztwisted.python.procutilsr   Ztwisted.python.reflectr   Ztwisted.trial.unittestr   r   ÚunixZtwisted.conch.scriptsr   Ztwisted.conch.scripts.cftpr   Ztwisted.conch.sshr   Ztwisted.conch.ssh.connectionr   Ztwisted.conch.testr   r   Ztwisted.conch.test.test_conchr    r!   ÚImportErrorZ	skipTestsZIReactorProcessr"   r:   ri   rq   rz   r…   r—   r  ZProcessProtocolr  rA  rY  r¹  rÒ  r+   r+   r+   r,   Ú<module>   sŠ   ÿ' #&2  u	{'   g