HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/conch/test/__pycache__/test_cftp.cpython-38.pyc
U

s�@g���@s�dZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZddlm
Z
ddlmZe
d�Ze
d�Ze
d	�ZdZer�er�zPdd
lmZddlmZddlmZdd
lmZddlmZmZddlm Z Wne!k
r�YnXddl"m#Z#ddl$m%Z%ddlm&Z&ddlm'Z'ddl(m)Z)ddl*m+Z+m,Z,m-Z-m.Z.m/Z/ddl0m1Z1m2Z2ddl3m4Z4ddl5m6Z6m7Z7ddl8m9Z9ddl:m;Z;ddl<m=Z=ddl>m?Z?Gdd�de?�Z@Gdd �d e?�ZAGd!d"�d"e;eB�ZCGd#d$�d$eB�ZDGd%d&�d&eB�ZEee%�Gd'd(�d(e	��ZFGd)d*�d*e?�ZGGd+d,�d,�ZHGd-d.�d.e,jI�ZJGd/d0�d0e&�ZKGd1d2�d2eK�ZLGd3d4�d4eK�ZMGd5d6�d6eK�ZNdeeee-�Oe+d�fk�r�edk�r�d7ZeeL_PeeM_PeeN_PeeG_Pee@_Pndd8lQmRZReRd9��s�d:eN_PdS);z*
Tests for L{twisted.conch.scripts.cftp}.
�N)�BytesIO)�FilePath)�
requireModule)�implementer�pyasn1�cryptographyztwisted.conch.unix)�cftp)�
SSHSession)�filetransfer)�FileTransferForTestAvatar)�test_ssh�
test_conch)�	FakeStdio)�ls)�	ISFTPFile)�SFTPTestBase)�FileTransferTestAvatar)�portal)�reactor�protocol�
interfaces�defer�error)�getProcessOutputAndValue�getProcessValue)�log)�_PY3�unicode)�UserDatabase)�StringTransport)�Clock)�TestCasec@seZdZdZdd�ZdS)�SSHSessionTestsz=
    Tests for L{twisted.conch.scripts.cftp.SSHSession}.
    cCs*t�}t�}||_|��|�|j�dS)z|
        L{twisted.conch.scripts.cftp.SSHSession.eofReceived} loses the write
        half of its stdio connection.
        N)rr	�stdioZeofReceived�
assertTrueZ
writeConnLost)�selfr#Zchannel�r&�>/usr/lib/python3/dist-packages/twisted/conch/test/test_cftp.py�test_eofReceived6s
z SSHSessionTests.test_eofReceivedN)�__name__�
__module__�__qualname__�__doc__r(r&r&r&r'r"2sr"c@s�eZdZdZeedd�dkr dZdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Ze
��ZzBze
�e
jd�Wne
jk
r�de_YnXe
�e
je�WnYnXdd�ZdS)�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.tzsetcshd�_�fdd�}��td|�dtjkrR��tjtjdtjd���tj	�ndd�}��|�dS)	zo
        Patch the L{ls} module's time function so the results of L{lsLine} are
        deterministic.
        i�[cs�jS�N)�nowr&�r%r&r'�fakeTimeRsz$ListingTests.setUp.<locals>.fakeTime�time�TZcSs.ztjd=Wntk
r YnXt��dS)Nr4)�os�environ�KeyErrorr3r.r&r&r&r'�cleanup\s
z#ListingTests.setUp.<locals>.cleanupN)
r0�patchrr5r6�
addCleanup�operator�setitemr3r.)r%r2r8r&r1r'�setUpLs
zListingTests.setUpcCs|tjd<t��t�d|�S)zl
        Call L{ls.lsLine} after setting the timezone to C{timezone} and return
        the result.
        r4�foo)r5r6r3r.rZlsLine)r%�timezone�statr&r&r'�
_lsInTimezonefs
zListingTests._lsInTimezonecCsT|jd}t�dddddddd|df
�}|�|�d|�d�|�|�d|�d�dS)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�r0r5�stat_result�assertEqualrA�r%�thenr@r&r&r'�test_oldFilers

�
�zListingTests.test_oldFilecCsX|jdd}t�dddddddd|df
�}|�|�d|�d�|�|�d|�d�dS)	��
        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.
        rBi��rrCz;!---------    0 0        0               0 May 01  1973 foorDz;!---------    0 0        0               0 May 02  1973 fooNrErHr&r&r'�test_oldSingleDigitDayOfMonth�s
�
�z*ListingTests.test_oldSingleDigitDayOfMonthcCsT|jd}t�dddddddd|df
�}|�|�d|�d�|�|�d|�d�dS)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.
        逛zrrC�;!---------    0 0        0               0 Aug 28 17:33 foorD�;!---------    0 0        0               0 Aug 29 09:33 fooNrErHr&r&r'�test_newFile�s

�
�zListingTests.test_newFilecCs||jd}t�dddddddd|df
�}t��}t�tjd�|�tjtj|�|�|�	d|�d�|�|�	d|�d�dS)	zC
        The month name in the date is locale independent.
        rMr�
es_AR.UTF8rCrNrDrON)
r0r5rF�locale�	getlocale�	setlocale�LC_ALLr:rGrA)r%rIr@�
currentLocaler&r&r'�test_localeIndependent�s

�
�z#ListingTests.test_localeIndependentrQz'The es_AR.UTF8 locale is not installed.cCsX|jdd}t�dddddddd|df
�}|�|�d|�d�|�|�d|�d�dS)	rKrMiFrrCz;!---------    0 0        0               0 Sep 01 17:33 foorDz;!---------    0 0        0               0 Sep 02 09:33 fooNrErHr&r&r'�test_newSingleDigitDayOfMonth�s
�
�z*ListingTests.test_newSingleDigitDayOfMonth)r)r*r+r,�getattrr3�skipr=rArJrLrPrWrRrSrVrTrU�ErrorrXr&r&r&r'r-Cs&r-cs eZdZdZ�fdd�Z�ZS)�InMemorySSHChannelzp
    Minimal implementation of a L{SSHChannel} like class which only reads and
    writes data from memory.
    cs||_d|_tt|���dS)zt
        @param conn: The SSH connection associated with this channel.
        @type conn: L{SSHConnection}
        rN)�connZlocalClosed�superr\�__init__)r%r]��	__class__r&r'r_�szInMemorySSHChannel.__init__)r)r*r+r,r_�
__classcell__r&r&r`r'r\�sr\c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�FilesystemAccessExpectationszC
    A test helper used to support expected filesystem access.
    cCs
i|_dSr/��_cacher1r&r&r'r_�sz%FilesystemAccessExpectations.__init__cCs||j||f<dS)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}
        Nrd)r%�path�flags�streamr&r&r'�put�sz FilesystemAccessExpectations.putcCs|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}
        )re�pop)r%rfrgr&r&r'rjs
z FilesystemAccessExpectations.popN)r)r*r+r,r_rirjr&r&r&r'rc�srcc@s eZdZdZdd�Zdd�ZdS)�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}
    cCs t|�|_ddd�|_||_dS)N��
)Zrequests�
buffersize)r\�	transport�options�_availableFiles)r%ZavailableFilesr&r&r'r_!s

�zInMemorySFTPClient.__init__cCs|j�||�S)z}
        @see: L{filetransfer.FileTransferClient.openFile}.

        Retrieve and remove cached file based on flags.
        )rqrj)r%�filenamerg�attrsr&r&r'�openFile*szInMemorySFTPClient.openFileN)r)r*r+r,r_rtr&r&r&r'rks		rkc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�InMemoryRemoteFilez;
    An L{ISFTPFile} which handles all data in memory.
    cCs||_t�|�dS)zL
        @param name: Name of this file.
        @type name: L{str}
        N)�namerr_)r%rvr&r&r'r_:szInMemoryRemoteFile.__init__cCs|�|�|�|�t�|�S)z/
        @see: L{ISFTPFile.writeChunk}
        )�seek�writer�succeed)r%�start�datar&r&r'�
writeChunkCs

zInMemoryRemoteFile.writeChunkcCs
d|_dS)zo
        @see: L{ISFTPFile.writeChunk}

        Keeps data after file was closed to help with testing.
        TN)�_closedr1r&r&r'�closeLszInMemoryRemoteFile.closecCs
t�|�S)zb
        Get current data of file.

        Allow reading data event when file is closed.
        )r�getvaluer1r&r&r'rUszInMemoryRemoteFile.getvalueN)r)r*r+r,r_r|r~rr&r&r&r'ru4s
			ruc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zd)dd�Zd*dd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�ZdS)+�StdioClientTestsz(
    Tests for L{cftp.StdioClient}.
    cCsTt�|_t|j�}t�|�|_d|j_t�|_|j_	|�
dd�|jjj|j_dS)zm
        Create a L{cftp.StdioClient} hooked up to dummy transport and a fake
        user database.
        �/i��N)rc�fakeFilesystemrkr�StdioClient�clientZcurrentDirectoryr�databaseZ_pwd�setKnownConsoleSizero)r%Z
sftpClientr&r&r'r=cs
zStdioClientTests.setUpc	Cs@|j�t��dt��dddtj�|j�	d�}|�
|jd�|S)zs
        The I{exec} command runs its arguments locally in a child process
        using the user's shell.
        �secret��r>�barzexec print(1 + 2)s3
)r��addUser�getpass�getuserr5�getuid�sys�
executabler��_dispatchCommand�addCallbackrG�r%�dr&r&r'�	test_execws�zStdioClientTests.test_execc	Cs>|j�t��dt��dddd�|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�r>r��zexec echo hello�hello
�
r�r�r�r�r5r�r�r�r�rGr�r&r&r'�test_execWithoutShell�s�z&StdioClientTests.test_execWithoutShellc	Cs>|j�t��dt��dddd�|j�d�}|�|j	d�|S)zO
        The I{exec} command is run for lines which start with C{"!"}.
        r�r�r>r�z/bin/shz!echo hellor�r�r�r&r&r'�	test_bang�s�zStdioClientTests.test_bangcs4ddl�G���fdd�dt�}|�td|��dS)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}
        rNcseZdZ���fdd�ZdS)z7StdioClientTests.setKnownConsoleSize.<locals>.FakeFcntlcs&|�jkr|�d�t�d��dd�S)Nz#Only window-size queries supported.Z4Hr)Z
TIOCGWINSZZfail�structZpack)r%�fdZoptZmutate��height�tty�widthr&r'�ioctl�s

z=StdioClientTests.setKnownConsoleSize.<locals>.FakeFcntl.ioctlN)r)r*r+r�r&r�r&r'�	FakeFcntl�sr�Zfcntl)r��objectr9r)r%r�r�r�r&r�r'r��sz$StdioClientTests.setKnownConsoleSizecCs�|�dd�t�}|j_td�}d|_t�|�}d|_|�	�}|�
d�|jd7_|j�||�t
rpd}nd	}|�|jj��|�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.
        rm�"�x�samplei(g@is#
b'sample' 40% 4.0kB 2.0kBps 00:03 z 
sample 40% 4.0kB 2.0kBps 00:03 N)r�r r�rrrvr�FileWrapper�size�secondsZadvanceZtotal�_printProgressBarrrGro�value�r%Zclock�wrapped�wrapperZ	startTime�resultr&r&r'�test_printProgressBarReporting�s	

z/StdioClientTests.test_printProgressBarReportingcCsn|�dd�t�}|j_td�}d|_t�|�}|��}|j�	||�t
rRd}nd}|�|jj�
�|�dS)z�
        L{StdioClient._printProgressBar} prints a progress description that
        indicates 0 bytes transferred if no bytes have been transferred and no
        time has passed.
        rmr�r�r�s!
b'sample'  0% 0.0B 0.0Bps 00:00 z
sample  0% 0.0B 0.0Bps 00:00 N)r�r r�rrrvrr�r�r�rrGror�r�r&r&r'�test_printProgressBarNoProgress�s
z0StdioClientTests.test_printProgressBarNoProgresscCsV|�dd�t�}d|_t�|�}|j�|d�tr:d}nd}|�||jj	�
��dS)z5
        Print the progress for empty files.
        rmr�s
empty-filers%
b'empty-file'100% 0.0B 0.0Bps 00:00 z"
empty-file100% 0.0B 0.0Bps 00:00 N)r�rrvrr�r�r�rrGror�)r%r�r�r�r&r&r'�test_printProgressBarEmptyFile�s
z/StdioClientTests.test_printProgressBarEmptyFilecCs|j�d�}|�d|�dS)zK
        Returns empty value for both filename and remaining data.
        z  )r�r�N�r�Z_getFilenamerG�r%r�r&r&r'�test_getFilenameEmpty�sz&StdioClientTests.test_getFilenameEmptycCs|j�d�}|�d|�dS)zd
        Returns empty value for remaining data when line contains
        only a filename.
        �
only-local)r�r�Nr�r�r&r&r'�test_getFilenameOnlyLocalsz*StdioClientTests.test_getFilenameOnlyLocalcCs|j�d�}|�d|�dS)ze
        Returns filename and remaining data striped of leading and trailing
        spaces.
        z local  remote file  )�localzremote fileNr�r�r&r&r'�test_getFilenameNotQuoted
sz*StdioClientTests.test_getFilenameNotQuotedcCs|j�d�}|�d|�dS)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_getFilenameQuotedsz'StdioClientTests.test_getFilenameQuotedN�c	Cs4|dkr|��}t|d��}|�|�W5QRX|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�openrx)r%rf�content�filer&r&r'�makeFiles
zStdioClientTests.makeFileFcCsf|jj��}tr|�d�}|�d�}g}g}|D]�\}}}g}	|D]}
|	�d||
f�qB|	�d||f�|�|	�|�d��d��d�}|dd�}|d�d	��d	�}
|�	|
�g}|dd�D]F}
|
���
d
d�d}
|
���d
d�}
|�d|
d|
d��f�q�|�|d�|�|�q0|�rD|�t|�t|��n|�||�|�dt
|�d
�dS)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

        �utf-8z

z%s %szTransferred %s to %sr�
N����
� �rlz5There are still put responses which were not checked.)r�ror�r�decode�split�appendrj�strip�extend�rsplitrG�sorted�len)r%Z	transfers�randomOrder�output�expectedOutputZactualOutputr�ZremoteZexpectedZexpectedTransfer�lineZ
progressPartsZactualZlastZactualTransferr&r&r'�checkPutMessage3s>




 �z StdioClientTests.checkPutMessagecCs�d}|j|d�}tjtjBtjB}tj�dtj�|��}t	|�}|j
�||t�
|��d|jjjd<|j�|�}|�|�|�||���|�|j�|�||dddgfg�dS)	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�rmrnz	76% 10.0Bz
100% 13.0BN)r�r
�	FXF_WRITE�	FXF_CREAT�	FXF_TRUNCr5rf�join�basenamerur�rirryr�rp�cmd_PUT�successResultOfrGrr$r}r�)r%r��	localPathrg�
remoteName�
remoteFile�deferredr&r&r'�test_cmd_PUTSingleNoRemotePathqs*���
��z/StdioClientTests.test_cmd_PUTSingleNoRemotePathcCs�|��}tjtjBtjB}d}t|�}|j�||t�	|��|j
�d||f�}|�|�|�
||dgfg�|�|j�|�d|���dS)z�
        Remote path is extracted from first filename after local file.

        Any other data in the line is ignored.
        z/remote-pathz
%s %s ignored�	100% 0.0Br�N)r�r
r�r�r�rur�rirryr�r�r�r�r$r}rGr)r%r�rgr�r�r�r&r&r'�test_cmd_PUTSingleRemotePath�s"���
�
z-StdioClientTests.test_cmd_PUTSingleRemotePathcCs|��}tj�|�}d}tj�|�}|jtj�||�d�}tjtjBtj	B}d|f}d|f}t
|�}	t
|�}
|j�||t
�|	��|j�||t
�|
��|j�tj�|d��}|�|�|�|	j�|�d|	���|�|
j�|�d|
���|j||dgf||dgfgdd�d	S)
z�
        When a gobbing expression is used local files are transferred with
        remote file names based on local names.
        �second-name�rfz/%s�*r�r�T�r�N�r�r5rfr��dirnamer�r
r�r�r�rur�rirryr�r�r�r$r}rGrr�)r%�first�	firstName�
secondName�parent�secondrg�firstRemotePath�secondRemotePath�firstRemoteFile�secondRemoteFiler�r&r&r'� test_cmd_PUTMultipleNoRemotePath�sJ���

��


��z1StdioClientTests.test_cmd_PUTMultipleNoRemotePathcCs |��}tj�|�}d}tj�|�}|jtj�||�d�}tjtjBtj	B}t
|�}t
|�}d|f}	d|f}
|j�|	|t
�|��|j�|
|t
�|��|j�dtj�|d�f�}|�|�|�|j�|�d|���|�|j�|�d|���|j||dgf||dgfgdd	�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/%sz	%s remoter�r�r�Tr�Nr�)r%r�r�r�r�r�rgr�r�r�r�r�r&r&r'�"test_cmd_PUTMultipleWithRemotePath�sN���

���


��z3StdioClientTests.test_cmd_PUTMultipleWithRemotePath)Nr�)F)r)r*r+r,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@seZdZdd�Zdd�ZdS)�FileTransferTestRealmcCs
||_dSr/)�testDir)r%r�r&r&r'r_�szFileTransferTestRealm.__init__cGst|j�}|d|dd�fS)NrcSsdSr/r&r&r&r&r'�<lambda>r�z5FileTransferTestRealm.requestAvatar.<locals>.<lambda>)rr�)r%ZavatarIDZmindr�ar&r&r'�
requestAvatar�s
z#FileTransferTestRealm.requestAvatarN)r)r*r+r_r�r&r&r&r'r��sr�c@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�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).
    cCs$|��||_d|_d|_d|_dS)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'r_s
zSFTPTestProcess.__init__cCsd|_g|_d|_dS)zR
        Clear any buffered data received from stdout. Should be private.
        r�N)�buffer�_linesReceived�_lineBufferr1r&r&r'r�szSFTPTestProcess.clearBuffercCsrt�d|�|j|�d�}|�d�|_|j�|�|jdk	rX|jd}|_|�|�|j	|7_	|�
�dS)zO
        Called by Twisted when the cftp client prints data to stdout.
        zgot %r�
r�N)r�msgrr�rjrr�r��callbackr��_checkForCommand)r%r{�linesr�r&r&r'�outReceived s

zSFTPTestProcess.outReceivedcCs^d}|jrZ|j|krZd�|j�}|�|�r:|t|�d�}|��|jd}|_|�|�dS)Nscftp> r)r�rr�r�
startswithr�r�r)r%�promptZbufr�r&r&r'r2s
z SFTPTestProcess._checkForCommandcCst�d|�dS)zO
        Called by Twisted when the cftp client prints data to stderr.
        zerr: %sN)rr)r%r{r&r&r'�errReceived=szSFTPTestProcess.errReceivedcCs|jS)zQ
        Return the contents of the buffer of data received from stdout.
        )r�r1r&r&r'�	getBufferDszSFTPTestProcess.getBuffercCs<t��|_|��t|t�r&|�d�}|j�|d�|jS)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.
        r�r)	r�Deferredr�r��
isinstancer�encoderorx�r%�commandr&r&r'�
runCommandKs


zSFTPTestProcess.runCommandcs(t�d����fdd�|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.
        rlcsg|]}���j|��qSr&)�runr)�.0r�r%Zsemr&r'�
<listcomp>jsz-SFTPTestProcess.runScript.<locals>.<listcomp>)rZDeferredSemaphoreZ
gatherResults)r%�commandsZdlr&rr'�	runScript^s
zSFTPTestProcess.runScriptcCs,|jrt�d�St��|_|j�d�|jS)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�rryrr�ro�
signalProcessr1r&r&r'�killProcessns
	

zSFTPTestProcess.killProcesscCs(d|_|jr$|jd}|_|�d�dS)zF
        Called by Twisted when the cftp client process ends.
        TN)r�r�r)r%�reasonr�r&r&r'�processEnded~szSFTPTestProcess.processEndedN)r)r*r+r,r_r�rrr
rrrrrr&r&r&r'r�s	r�c@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�CFTPClientTestBasec	Cs�tdd��}|�tj�W5QRXtdd��}|�tj�W5QRXt�dd�tdd��}|�dtj�W5QRXt�	|�S)N�dsa_test.pubr��dsa_testi���kh_tests
127.0.0.1 )
r�rxrZpublicDSA_opensshZprivateDSA_opensshr5�chmodZpublicRSA_opensshrr=�r%�fr&r&r'r=�szCFTPClientTestBase.setUpcCsFt|j�}t�|�}|�t���t��}||_tj	d|dd�|_
dS)Nrz	127.0.0.1)Z	interface)r�r�rZPortalZregisterCheckerrZconchTestPublicKeyCheckerZConchTestServerFactoryrZ	listenTCP�server)r%Zrealm�pZfacr&r&r'�startServer�s

zCFTPClientTestBase.startServercCsHt|jjd�s|�d�Sd|jjj_t�|jjjjj	�}|�
|j�|S)N�protorl)�hasattrr$�factory�
_cbStopServerr'�expectedLoseConnectionr�
maybeDeferredroZloseConnectionr�r�r&r&r'�
stopServer�s
�zCFTPClientTestBase.stopServercCst�|jj�Sr/)rr,r$Z
stopListening�r%�ignoredr&r&r'r*�sz CFTPClientTestBase._cbStopServercCs0dD] }zt�|�WqYqXqt�|�S)N)rrr )r5�remover�tearDownr"r&r&r'r1�szCFTPClientTestBase.tearDownN)r)r*r+r=r&r-r*r1r&r&r&r'r�s
	
rc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zd-dd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�ZdS).�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
s,t������d}�j��j}tj||��dd�}t	�
dtj|f�t
��}t|��_|��fdd��tj��}tj�tj�|d<g}i}|D]"}t|t�r�|�d�}|�|�q�|D]<}||}	t|t�r�|�d�}t|	t�r�|	�d�}	|	||<q�t	�
|�t	�
|�tj�jtj||d	�|S)
Nz�-p %i -l testuser --known-hosts kh_test --user-authentications publickey --host-key-algorithms ssh-rsa -i dsa_test -a -v 127.0.0.1r��mod�
running %s %scs
�j��Sr/)�processProtocolr���_r1r&r'r��r�z3OurServerCmdLineClientTests.setUp.<locals>.<lambda>�
PYTHONPATHr���env)rr=r&r$�getHost�portr
�	_makeArgsr�rrr�r�rrr�r6r�r5r6�copy�pathsepr�rfr
rrr�rZspawnProcess)
r%�cmdsr=r�r;ZencodedCmdsZ
encodedEnv�cmd�var�valr&r1r'r=�s<











�z!OurServerCmdLineClientTests.setUpcs���}|��fdd��|S)Ncs
�j��Sr/)r6rr7r1r&r'r��r�z6OurServerCmdLineClientTests.tearDown.<locals>.<lambda>)r-r�r�r&r1r'r1�sz$OurServerCmdLineClientTests.tearDowncCs.z|jj�d�Wntjk
r(YnXdS)Nr)r6rorrZProcessExitedAlreadyr.r&r&r'�_killProcess�sz(OurServerCmdLineClientTests._killProcesscCs|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.
        )r6rrr&r&r'r�sz&OurServerCmdLineClientTests.runCommandcGs|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.
        )r6r)r%rr&r&r'r�sz%OurServerCmdLineClientTests.runScriptcCsL|j}|�ddddd�}dd�}|�|�|�|j|jt��d|jg�|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.
        �pwdZlpwdzcd testDirectory�cd ..cSsHg}|D]&}tr$t|t�r$|�d�}|�|�q|dd�|dd�S)zH
            Callback function for handling command output.
            r�N��)rr
�bytesr�r�)r�rArBr&r&r'�	cmdOutputs
z8OurServerCmdLineClientTests.testCdPwd.<locals>.cmdOutputr�)r�rr�rGrfr5�getcwd)r%ZhomeDirr�rKr&r&r'�	testCdPwds
�z%OurServerCmdLineClientTests.testCdPwdcs&�fdd�}��dddd�}|�|�S)z�
        Check that 'ls -l' output includes the access permissions and that
        this output changes appropriately with 'chmod'.
        csZ�����|d�d����|dd���|d�d�|d���|dd�dS)Nrs
-rw-r--r--rlr�r�s
----------rH)�flushLoggedErrorsr$rrG�Zresultsr1r&r'�_check's
z7OurServerCmdLineClientTests.testChAttrs.<locals>._checkzls -l testfile1zchmod 0 testfile1zchmod 644 testfile1�rr��r%rPr�r&r1r'�testChAttrs"s�z'OurServerCmdLineClientTests.testChAttrscs@�fdd�}��dd�j��ddd�}|�dd	��|�|�S)
z�
        Check 'ls' works as expected. Checks for wildcards, hidden files,
        listing directories and listing empty directories.
        csp��|dddddg���|dddddg���|dddg���|dd	ddg���|d
dg�dS)Nr�
testDirectory�testRemoveFile�testRenameFile�	testfile1rlr�rHs.testHiddenFilerIr��rGrOr1r&r'rP9s���z4OurServerCmdLineClientTests.testList.<locals>._checkrzls ../zls *Filezls -a *Filezls -l testDirectorycSsdd�|D�S)NcSsg|]}|�d��qS)r)r�)r�xr&r&r'rDszJOurServerCmdLineClientTests.testList.<locals>.<lambda>.<locals>.<listcomp>r&)Zxsr&r&r'r�Dr�z6OurServerCmdLineClientTests.testList.<locals>.<lambda>)rr�r�r�rRr&r1r'�testList4s	�z$OurServerCmdLineClientTests.testListcCsD|�d�}t�d��d���}t|t�r2|�d�}|�|j	|�|S)zB
        Check that running the '?' command returns help.
        �?Nr�r�)
rrr�Zcmd_HELPr�r
rrr�rG)r%r�ZhelpTextr&r&r'�testHelpHs


z$OurServerCmdLineClientTests.testHelpNcCs|�|��|��|�dS)zg
        Assert that the files at C{name1} and C{name2} contain exactly the
        same data.
        N)rGZ
getContent)r%Zname1Zname2rr&r&r'�assertFilesEqualUsz,OurServerCmdLineClientTests.assertFilesEqualcsjd�jj�jjf�t�t�r(��d����fdd�}��d�jjf�}|�|�|��fdd��|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 %s/testfile1 to %s/test file2r�cs8��|�������j�d��j�d�d���d�S)N�	testfile1�
test file2z
get failedzrm "test file2")r$�endswithr]r��childr�r��r�r%r&r'�	_checkGeths
�z6OurServerCmdLineClientTests.testGet.<locals>._checkGetzget testfile1 "%s/test file2"cs���j�d����S)Nr_�ZassertFalser�ra�existsr7r1r&r'r�qs�z5OurServerCmdLineClientTests.testGet.<locals>.<lambda>)r�rfr
rrrr�)r%rdr�r&rcr'�testGet]s�


z#OurServerCmdLineClientTests.testGetcs �fdd�}��d�}|�|�S)zQ
        Test that 'get' works correctly when given wildcard parameters.
        cs8���j�d�td�d����j�d�td�d�dS)N�testRemoveFile�testRemoveFile get failed�testRenameFile�testRenameFile get failed)r]r�rar)r/r1r&r'rPzs��z;OurServerCmdLineClientTests.testWildcardGet.<locals>._checkz
get testR*)rr�rRr&r1r'�testWildcardGetvs
z+OurServerCmdLineClientTests.testWildcardGetcsfd�j��jd�j��jd���fdd�}��d�jjf�}|�|�|��fdd��|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"file2cs6���j�d��j�d����|������d�S)Nr^�
test"file2zrm "test\"file2")r]r�rar$r`rrbrcr&r'�	_checkPut�s

�z6OurServerCmdLineClientTests.testPut.<locals>._checkPutzput %s/testfile1 "test\"file2"cs���j�d����S)Nrmrer7r1r&r'r��s�z5OurServerCmdLineClientTests.testPut.<locals>.<lambda>)r��asBytesModerfrr�)r%rnr�r&rcr'�testPut�s�
���
z#OurServerCmdLineClientTests.testPutc	s��j�d�jdd��}|�d�W5QRX�j�d�jdd��}|�d�W5QRX�fdd�}��d	�jjf�}|�|�|S)
zb
        Check that 'put' uploads files correctly when overwriting a longer
        file.
        �shorterFile�w��mode�a�
longerFile�bbcs ���j�d��j�d��dS)Nrqrv)r]r�rarbr1r&r'rn�s
�zEOurServerCmdLineClientTests.test_putOverLongerFile.<locals>._checkPutzput %s/shorterFile longerFile)r�rar�rxrrfr��r%r#rnr�r&r1r'�test_putOverLongerFile�s�
z2OurServerCmdLineClientTests.test_putOverLongerFilec	s��j�d�������d�jdd��}|�d�W5QRX�j�d�jdd��}|�d�W5QRX��fdd�}��d	�jjf�}|�|�|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�rrrsrurwcs����d��j�d��dS)Nr�)r]rar�rb�r%ZsomeDirr&r'rn�s
�zMOurServerCmdLineClientTests.test_putMultipleOverLongerFile.<locals>._checkPutzput %s/dir/*)r�raZcreateDirectoryr�rxrrfr�rxr&r{r'�test_putMultipleOverLongerFile�s�
z:OurServerCmdLineClientTests.test_putMultipleOverLongerFilecs<�fdd�}��dd�jjfd�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.
        csh��|dd���|dd����j�d��j���d�d����j�d��j���d�d�dS)Nrr�r�rhrirjrk)rGr]r�rar�rOr1r&r'�check�s��z:OurServerCmdLineClientTests.testWildcardPut.<locals>.checkrGz
put %s/testR*zcd %s)rr�rfr�r�)r%r}r�r&r1r'�testWildcardPut�s�
z+OurServerCmdLineClientTests.testWildcardPutcs4�fdd�}��dd�}|�|�|��jd�|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.
        cs8�����|dd���|d�d�d���d�S)Nrr�rl�lzlink failedzrm testLink)rNrGr$rrrOr1r&r'rP�sz4OurServerCmdLineClientTests.testLink.<locals>._checkzln testLink testfile1zls -l testLinkr��rr�rGrRr&r1r'�testLink�s

z$OurServerCmdLineClientTests.testLinkcs4�fdd�}��dd�}|�|�|��jd�|S)zV
        Test that we can create and remove directories with the cftp client.
        cs.��|dd���|d�d����d�S)Nrr�rl�dzrmdir testMakeDirectory)rGr$rrrOr1r&r'rP�sz?OurServerCmdLineClientTests.testRemoteDirectory.<locals>._check�mkdir testMakeDirectoryzls -l testMakeDirector?r�r�rRr&r1r'�testRemoteDirectory�s�
z/OurServerCmdLineClientTests.testRemoteDirectorycs&�fdd�}��dd�}|�|�|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.
        cs$��|dd���|dd�dS)Nrr�rlsremote error 11: mkdir failedrXrOr1r&r'rPs
�zHOurServerCmdLineClientTests.test_existingRemoteDirectory.<locals>._checkr�rQrRr&r1r'�test_existingRemoteDirectorys�
z8OurServerCmdLineClientTests.test_existingRemoteDirectorycsF��d�jjf�}|��jd�|��fdd��|��jd�|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.
        zlmkdir %s/testLocalDirectoryr�cs
��d�S)Nzrmdir testLocalDirectory)rr7r1r&r'r�r�z@OurServerCmdLineClientTests.testLocalDirectory.<locals>.<lambda>)rr�rfr�rGr�r&r1r'�testLocalDirectorys
z.OurServerCmdLineClientTests.testLocalDirectorycs4�fdd�}��dd�}|�|�|��jd�|S)z1
        Test that we can rename a file.
        cs*��|dd���|dd���d�S)Nrr�rls	testfile2zrename testfile2 testfile1)rGrrOr1r&r'rP!sz6OurServerCmdLineClientTests.testRename.<locals>._checkzrename testfile1 testfile2zls testfile?r�r�rRr&r1r'�
testRenames

z&OurServerCmdLineClientTests.testRename)N)r)r*r+r,r=r1rErrrMrSrZr\r]rgrlrpryr|r~r�r�r�r�r�r&r&r&r'r2�s,(		

r2c@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�OurServerBatchFileTestszm
    Functional tests which launch a SFTP server over localhost and checks csftp
    in batch interface.
    cCst�|�|��dSr/�rr=r&r1r&r&r'r=3s
zOurServerBatchFileTests.setUpcCst�|�|��Sr/)rr1r-r1r&r&r'r18s
z OurServerBatchFileTests.tearDownc	s�|���t�d��}|�|�W5QRX|j��j}d|�f}tj|��dd�dd�}t	�
dtj|f�t
j��}t
j�tj�|d<d|jj_ttj||d�}�fd	d
�}|�dd��|�|�|S)
Nrrz�-p %i -l testuser --known-hosts kh_test --user-authentications publickey --host-key-algorithms ssh-rsa -i dsa_test -a -v -b %s 127.0.0.1rr3rlr5r9r:cst���|Sr/)r5r0��res��fnr&r'�_cleanupRs
z9OurServerBatchFileTests._getBatchOutput.<locals>._cleanupcSs|dS)Nrr&r�r&r&r'r�Vr�z9OurServerBatchFileTests._getBatchOutput.<locals>.<lambda>)r�r�rxr$r<r=r
r>r�rrr�r�r5r6r?r@r�rfr)r+rr�ZaddBoth)r%r#�fpr=rAr;r�r�r&r�r'�_getBatchOutput=s"�


z'OurServerBatchFileTests._getBatchOutputcs(d}�fdd�}��|�}|�|�|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
csT|�d�}t�dt|�����j��j|d���|dd�dddd	g�dS)
NrzRES %srlrH���rTrUrVrW)	r�rr�repr�assertInr�rorfrGr�r1r&r'�_cbCheckResultfs
�z=OurServerBatchFileTests.testBatchFile.<locals>._cbCheckResult�r�r��r%rAr�r�r&r1r'�
testBatchFile\s


z%OurServerBatchFileTests.testBatchFilecs(d}�fdd�}��|�}|�|�|S)zO
        Test that an error in the batch file stops running the batch.
        zchown 0 missingFile
pwd
exit
cs���j��j|�dSr/)ZassertNotInr�rorfr�r1r&r'r�{sz9OurServerBatchFileTests.testError.<locals>._cbCheckResultr�r�r&r1r'�	testErrorrs


z!OurServerBatchFileTests.testErrorcs(d}�fdd�}��|�}|�|�|S)z_
        Test that a minus sign '-' at the front of a line ignores
        any errors.
        z-chown 0 missingFile
pwd
exit
cs���j��j|�dSr/)r�r�rorfr�r1r&r'r��sz@OurServerBatchFileTests.testIgnoredError.<locals>._cbCheckResultr�r�r&r1r'�testIgnoredError�s


z(OurServerBatchFileTests.testIgnoredErrorN)
r)r*r+r,r=r1r�r�r�r�r&r&r&r'r�-sr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�OurServerSftpClientTestsz@
    Test the sftp server against sftp command line client.
    cCst�|�|��Sr/r�r1r&r&r'r=�s
zOurServerSftpClientTests.setUpcCs|��Sr/)r-r1r&r&r'r1�sz!OurServerSftpClientTests.tearDownc	s�����t�d��}|�d�W5QRX�j��j�tj��fdd�}��td|�d�jj	_
tdd�}��fd	d
�}�fdd�}|�|�|�d
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.
        rrz	ls .
exitcs�||�}d|d<|S)Nr�Zext_foor&)r%�srs)�
oldGetAttrr&r'�	_getAttrs�s
zCOurServerSftpClientTests.test_extendedAttributes.<locals>._getAttrsr�TZssh)�-o�PubkeyAcceptedKeyTypes=ssh-dssz-Vcs>|dkrd}nd}|dddddddd	dd
�fd�df
7}|S)
Nr)r�r�r&z-Fz	/dev/nullr�zIdentityFile=dsa_testzUserKnownHostsFile=kh_testzHostKeyAlgorithms=ssh-rsazPort=%iz-bztestuser@127.0.0.1r&)Zstatus�args)r�r=r&r'�hasPAKT�s"�zAOurServerSftpClientTests.test_extendedAttributes.<locals>.hasPAKTcs.��|dd�dD]}��||d�qdS)Nr�r)rTrUrVrW)rGr�)r��ir1r&r'r}�sz?OurServerSftpClientTests.test_extendedAttributes.<locals>.checkcSs
td|�S)N�sftp)r)r�r&r&r'r��r�zBOurServerSftpClientTests.test_extendedAttributes.<locals>.<lambda>)
r�r�rxr$r<r=rr�r9r)r+rr�)r%r#r�r�r�r}r&)r�r�r=r%r'�test_extendedAttributes�s"
�
z0OurServerSftpClientTests.test_extendedAttributesN)r)r*r+r,r=r1r�r&r&r&r'r��sr�z4don't run w/o spawnProcess or cryptography or pyasn1)�whichr�z%no sftp command-line client available)Sr,rRr3r�r5r;r�r��iorZtwisted.python.filepathrZtwisted.python.reflectrZzope.interfacerrrZunixZ_reasonZtwisted.conch.scriptsrZtwisted.conch.scripts.cftpr	Ztwisted.conch.sshr
Z$twisted.conch.test.test_filetransferrZtwisted.conch.testrr
Ztwisted.conch.test.test_conchr�ImportErrorZ
twisted.conchrZtwisted.conch.interfacesrrrZtwisted.credrZtwisted.internetrrrrrZtwisted.internet.utilsrrZtwisted.pythonrZtwisted.python.compatrrZtwisted.python.fakepwdrZtwisted.test.proto_helpersrZtwisted.internet.taskr Ztwisted.trial.unittestr!r"r-r�r\rcrkrur�r�ZProcessProtocolr�rr2r�r�ZIReactorProcessrZZtwisted.python.procutilsr�r&r&r&r'�<module>s�0)*-yhD
�