AnonSec Shell
Server IP : 209.38.156.173  /  Your IP : 216.73.216.122   [ Reverse IP ]
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux lakekumayuhotel 5.15.0-136-generic #147-Ubuntu SMP Sat Mar 15 15:53:30 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.1.2-1ubuntu2.22
Disable Function : NONE
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/lib/python3/dist-packages/twisted/conch/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /usr/lib/python3/dist-packages/twisted/conch/test/__pycache__/test_cftp.cpython-310.pyc
o

5�@gG��@s�dZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZddlm
Z
ddlmZddlmZddlmZmZdd	lmZdd
lmZmZmZmZmZddlmZddlm Z m!Z!dd
l"m#Z#ddl$m%Z%ddl&m'Z'ddl(m)Z)ddl*m+Z+ddl,m-Z-ddl.m/Z/e+d�Z0e+d�Z1e+d�Z2e1r�e0r�z(ddl3m4Z4ddl5m6Z6ddl7m8Z8ddl9m:Z:m;Z;ddl<m=Z=ddlm>Z>Wn	e?y�YnwdZ@de2e1e0e�Aed�fvr�dZ@ee@d�Gd d!�d!e/��ZBGd"d#�d#e/�ZCGd$d%�d%e-�ZDGd&d'�d'�ZEGd(d)�d)�ZFe
e�Gd*d+�d+e	��ZGee@d�Gd,d-�d-e/��ZHGd.d/�d/�ZIGd0d1�d1ejJ�ZKGd2d3�d3e�ZLee@d�Gd4d5�d5eL��ZMee@d�Gd6d7�d7eL��ZNee@d�ee)d8�d9�Gd:d;�d;eL���ZOdS)<z*
Tests for L{twisted.conch.scripts.cftp}.
�N)�BytesIO)�skipIf)�implementer)�ls)�	ISFTPFile)�FileTransferTestAvatar�SFTPTestBase)�portal)�defer�error�
interfaces�protocol�reactor)�Clock)�getProcessOutputAndValue�getProcessValue)�log)�UserDatabase)�FilePath)�which)�
requireModule)�StringTransport)�TestCase�pyasn1�cryptographyztwisted.conch.unix)�cftp)�
SSHSession)�filetransfer)�
test_conch�test_ssh)�	FakeStdio)�FileTransferForTestAvatarFTz4don't run w/o spawnProcess or cryptography or pyasn1c@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)r r�stdio�eofReceived�
assertTrue�
writeConnLost)�selfr#�channel�r)�>/usr/lib/python3/dist-packages/twisted/conch/test/test_cftp.py�test_eofReceived>s
z SSHSessionTests.test_eofReceivedN)�__name__�
__module__�__qualname__�__doc__r+r)r)r)r*r"8sr"c@s�eZdZdZeedd�durdZdd�Zdd�Zd	d
�Z	dd�Z
d
d�Ze�
�Zzz	e�ejd�Wnejy>dZYnwe�eje�WnYeed�dd��Zdd�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.tzsetcsjd�_�fdd�}��td|�dtjvr*��tjtjdtjd���tj	�dSdd�}��|�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*�fakeTimeZsz$ListingTests.setUp.<locals>.fakeTime�time�TZcSs,ztjd=Wn	tyYnwt��dS)Nr7)�os�environ�KeyErrorr6r1r)r)r)r*�cleanupfs�z#ListingTests.setUp.<locals>.cleanupN)
r3�patchrr8r9�
addCleanup�operator�setitemr6r1)r'r5r;r)r4r*�setUpSs
zListingTests.setUpcCs|tjd<t��t�d|�S)zl
        Call L{ls.lsLine} after setting the timezone to C{timezone} and return
        the result.
        r7�foo)r8r9r6r1r�lsLine)r'�timezone�statr)r)r*�
_lsInTimezoneps
zListingTests._lsInTimezonecC�T|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�r3r8�stat_result�assertEqualrE�r'�thenrDr)r)r*�test_oldFile{�

�
�zListingTests.test_oldFilecC�X|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.
        rGi��rrHz;!---------    0 0        0               0 May 01  1973 foorIz;!---------    0 0        0               0 May 02  1973 fooNrJrMr)r)r*�test_oldSingleDigitDayOfMonth��
�
�z*ListingTests.test_oldSingleDigitDayOfMonthcCrF)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.
        逛zrrH�;!---------    0 0        0               0 Aug 28 17:33 foorI�;!---------    0 0        0               0 Aug 29 09:33 fooNrJrMr)r)r*�test_newFile�rPzListingTests.test_newFile�
es_AR.UTF8Tz'The es_AR.UTF8 locale is not installed.cCs||jd}t�dddddddd|df
�}t��}t�tjd�|�tjtj|�|�|�	d|�d�|�|�	d|�d�dS)	zC
        The month name in the date is locale independent.
        rUrrYrHrVrIrWN)
r3r8rK�locale�	getlocale�	setlocale�LC_ALLr=rLrE)r'rNrD�
currentLocaler)r)r*�test_localeIndependent�s

�
�z#ListingTests.test_localeIndependentcCrQ)	rRrUiFrrHz;!---------    0 0        0               0 Sep 01 17:33 foorIz;!---------    0 0        0               0 Sep 02 09:33 fooNrJrMr)r)r*�test_newSingleDigitDayOfMonth�rTz*ListingTests.test_newSingleDigitDayOfMonth)r,r-r.r/�getattrr6�skipr@rErOrSrXrZr[r^r\r]�Error�
localeSkiprr_r`r)r)r)r*r0Js.��
r0cs eZdZdZ�fdd�Z�ZS)�InMemorySSHChannelzp
    Minimal implementation of a L{SSHChannel} like class which only reads and
    writes data from memory.
    cs||_d|_t���dS)zt
        @param conn: The SSH connection associated with this channel.
        @type conn: L{SSHConnection}
        rN)�conn�localClosed�super�__init__)r'rf��	__class__r)r*ri�szInMemorySSHChannel.__init__)r,r-r.r/ri�
__classcell__r)r)rjr*re�srec@�(eZdZdZdd�Zdd�Zdd�ZdS)	�FilesystemAccessExpectationszC
    A test helper used to support expected filesystem access.
    cCs
i|_dSr2��_cacher4r)r)r*ri�
z%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}
        Nro)r'�path�flags�streamr)r)r*�puts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}
        )rp�pop)r'rrrsr)r)r*rvs
z FilesystemAccessExpectations.popN)r,r-r.r/rirurvr)r)r)r*rn�s
rnc@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��
)�requests�
buffersize)re�	transport�options�_availableFiles)r'�availableFilesr)r)r*ri,s

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

        Retrieve and remove cached file based on flags.
        )r~rv)r'�filenamers�attrsr)r)r*�openFile4szInMemorySFTPClient.openFileN)r,r-r.r/rir�r)r)r)r*rw"s	rwc@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)�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)�namerri)r'r�r)r)r*riCszInMemoryRemoteFile.__init__cCs|�|�|�|�t�|�S)z/
        @see: L{ISFTPFile.writeChunk}
        )�seek�writer
�succeed)r'�start�datar)r)r*�
writeChunkKs


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

        Keeps data after file was closed to help with testing.
        TN)�_closedr4r)r)r*�closeS�
zInMemoryRemoteFile.closecC�dSr2r)r4r)r)r*�getAttrs[�zInMemoryRemoteFile.getAttrscCr�r2r))r'�offset�lengthr)r)r*�	readChunk_r�zInMemoryRemoteFile.readChunkcCr�r2r))r'r�r)r)r*�setAttrscr�zInMemoryRemoteFile.setAttrscCs
t�|�S)zb
        Get current data of file.

        Allow reading data event when file is closed.
        )r�getvaluer4r)r)r*r�gr�zInMemoryRemoteFile.getvalueN)r,r-r.r/rir�r�r�r�r�r�r)r)r)r*r�=sr�c@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)rn�fakeFilesystemrwr�StdioClient�client�currentDirectoryr�database�_pwd�setKnownConsoleSizer|)r'�
sftpClientr)r)r*r@vs
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��rA�barzexec print(1 + 2)s3
)r��addUser�getpass�getuserr8�getuid�sys�
executabler��_dispatchCommand�addCallbackrL�r'�dr)r)r*�	test_exec�s�zStdioClientTests.test_execc	C�>|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�rAr��zexec echo hello�hello
�
r�r�r�r�r8r�r�r�r�rLr�r)r)r*�test_execWithoutShell�s�z&StdioClientTests.test_execWithoutShellc	Cr�)zO
        The I{exec} command is run for lines which start with C{"!"}.
        r�r�rAr�z/bin/shz!echo hellor�r�r�r)r)r*�	test_bang�s�zStdioClientTests.test_bangcs2ddl�G���fdd�d�}|�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.�4Hr)�
TIOCGWINSZ�fail�struct�pack)r'�fd�opt�mutate��height�tty�widthr)r*�ioctl�s

z=StdioClientTests.setKnownConsoleSize.<locals>.FakeFcntl.ioctlN)r,r-r.r�r)r�r)r*�	FakeFcntl�sr��fcntl)r�r<r)r'r�r�r�r)r�r*r��sz$StdioClientTests.setKnownConsoleSizecCs�|�dd�t�}|j_td�}d|_t�|�}d|_|�	�}|�
d�|jd7_|j�||�d}|�
|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.
        ry�"�x�samplei(g@is#
b'sample' 40% 4.0kB 2.0kBps 00:03 N)r�rr�rrr�r�FileWrapper�size�seconds�advance�total�_printProgressBarrLr|�value�r'�clock�wrapped�wrapper�	startTime�resultr)r)r*�test_printProgressBarReporting�s	

z/StdioClientTests.test_printProgressBarReportingcCsd|�dd�t�}|j_td�}d|_t�|�}|��}|j�	||�d}|�
|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.
        ryr�r�r�s!
b'sample'  0% 0.0B 0.0Bps 00:00 N)
r�rr�rrr�rr�r�r�rLr|r�r�r)r)r*�test_printProgressBarNoProgress�s
z0StdioClientTests.test_printProgressBarNoProgresscCsL|�dd�t�}d|_t�|�}|j�|d�d}|�||jj�	��dS)z5
        Print the progress for empty files.
        ryr�s
empty-filers%
b'empty-file'100% 0.0B 0.0Bps 00:00 N)
r�rr�rr�r�r�rLr|r�)r'r�r�r�r)r)r*�test_printProgressBarEmptyFile�s
z/StdioClientTests.test_printProgressBarEmptyFilecC�|j�d�}|�d|�dS)zK
        Returns empty value for both filename and remaining data.
        z  )r�r�N�r��_getFilenamerL�r'r�r)r)r*�test_getFilenameEmpty�sz&StdioClientTests.test_getFilenameEmptycCr�)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_getFilenameOnlyLocalcCr�)ze
        Returns filename and remaining data striped of leading and trailing
        spaces.
        z local  remote file  )�localzremote fileNr�r�r)r)r*�test_getFilenameNotQuoted
r�z*StdioClientTests.test_getFilenameNotQuotedcCr�)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_getFilenameQuotedr�z'StdioClientTests.test_getFilenameQuotedN�cCsJ|dur|��}t|d��}|�|�Wd�|S1swY|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'rr�content�filer)r)r*�makeFiles
��zStdioClientTests.makeFileFcCsh|jj��}|�d�}|�d�}g}g}|D]\}}}g}	|D]}
|	�|�d|
���q|	�d|�d|���|�|	�|�d��d��d�}|dd	�}|d	�d
��d
�}
|�|
�g}|dd	�D]$}
|
���	dd�d}
|
���dd�}
|�|
d�d|
d�����qd|�|d	�|�|�q|r�|�
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Transferred z to r�
N����
�rxz5There are still put responses which were not checked.)
r�r|r��decode�split�appendrv�strip�extend�rsplitrL�sorted�len)r'�	transfers�randomOrder�output�expectedOutput�actualOutputr��remote�expected�expectedTransfer�line�
progressParts�actual�last�actualTransferr)r)r*�checkPutMessage3s<




"�z StdioClientTests.checkPutMessagecCs�d}|j|d�}tjtjBtjB}tj�dtj�|��}t	|�}|j
�||t�
|��d|jjjd<|j�|�}|�|�|�||���|�|j�|�||gd�fg�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�ryr{)z	76% 10.0B�
100% 13.0BrN)r�r�	FXF_WRITE�	FXF_CREAT�	FXF_TRUNCr8rr�join�basenamer�r�rur
r�r�r}�cmd_PUT�successResultOfrLr�r%r�r)r'r��	localPathrs�
remoteName�
remoteFile�deferredr)r)r*�test_cmd_PUTSingleNoRemotePathps
�z/StdioClientTests.test_cmd_PUTSingleNoRemotePathcCs�|��}tjtjBtjB}d}t|�}|j�||t�	|��|j
�|�d|�d��}|�|�|�
||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-pathr�z ignored�	100% 0.0Br�N)r�rrrrr�r�rur
r�r�rrrr%r�rLr�)r'r
rsrrrr)r)r*�test_cmd_PUTSingleRemotePath�s
z-StdioClientTests.test_cmd_PUTSingleRemotePathcCs|��}tj�|�}d}tj�|�}|jtj�||�d�}tjtjBtj	B}d|��}d|��}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�rrr��*r�rT�r�N)r�r8rrr
�dirnamer	rrrrr�r�rur
r�r�rrr%r�rLr�r)r'�first�	firstName�
secondName�parent�secondrs�firstRemotePath�secondRemotePath�firstRemoteFile�secondRemoteFilerr)r)r*� test_cmd_PUTMultipleNoRemotePath�s4

�


�
�z1StdioClientTests.test_cmd_PUTMultipleNoRemotePathcCs |��}tj�|�}d}tj�|�}|jtj�||�d�}tjtjBtj	B}t
|�}t
|�}d|��}	d|��}
|j�|	|t
�|��|j�|
|t
�|��|j�d�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.
        when a remote folder is requested remote paths are composed from
        remote path and local filename.
        rrz/remote/z	{} remoterr�rTrN)r�r8rrr
rr	rrrrr�r�rur
r�r�r�formatrr%r�rLr�r)r'rrrrrrsr r!rrrr)r)r*�"test_cmd_PUTMultipleWithRemotePath�s4

�


�
�z3StdioClientTests.test_cmd_PUTMultipleWithRemotePath)Nr�)F)r,r-r.r/r@r�r�r�r�r�r�r�r�r�r�r�r�rrrr"r$r)r)r)r*r�ps(

		
	
=#r�c@seZdZdd�Zdd�ZdS)�FileTransferTestRealmcCs
||_dSr2)�testDir)r'r&r)r)r*ri�rqzFileTransferTestRealm.__init__cGst|j�}|d|dd�fS)NrcSr�r2r)r)r)r)r*�<lambda>�sz5FileTransferTestRealm.requestAvatar.<locals>.<lambda>)rr&)r'�avatarID�mindr�ar)r)r*�
requestAvatar�s
z#FileTransferTestRealm.requestAvatarN)r,r-r.rir+r)r)r)r*r%�sr%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*ri�s

zSFTPTestProcess.__init__cCsd|_g|_d|_dS)zR
        Clear any buffered data received from stdout. Should be private.
        r�N)�buffer�_linesReceived�_lineBufferr4r)r)r*r-�s
zSFTPTestProcess.clearBuffercCsrt�d|�|j|�d�}|�d�|_|j�|�|jdur,|jd}|_|�|�|j	|7_	|�
�dS)zO
        Called by Twisted when the cftp client prints data to stdout.
        zgot %r�
r�N)r�msgr4r�rvr3r�r.�callbackr2�_checkForCommand)r'r��linesr�r)r)r*�outReceiveds

zSFTPTestProcess.outReceivedcCsfd}|jr/|j|kr1d�|j�}|�|�r|t|�d�}|��|jd}|_|�|�dSdSdS)Nscftp> r5)r0r4r	r3�
startswithr�r-r7)r'�prompt�bufr�r)r)r*r8s
�z SFTPTestProcess._checkForCommandcCst�d|�dS)zO
        Called by Twisted when the cftp client prints data to stderr.
        zerr: %sN)rr6)r'r�r)r)r*�errReceived"szSFTPTestProcess.errReceivedcCs|jS)zQ
        Return the contents of the buffer of data received from stdout.
        )r2r4r)r)r*�	getBuffer(szSFTPTestProcess.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�r5)	r
�Deferredr0r-�
isinstance�str�encoder|r��r'�commandr)r)r*�
runCommand.s


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.
        rxcsg|]	}���j|��qSr))�runrF)�.0rE�r'�semr)r*�
<listcomp>Lsz-SFTPTestProcess.runScript.<locals>.<listcomp>)r
�DeferredSemaphore�
gatherResults)r'�commands�dlr)rIr*�	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)r1r
r�r@r/r|�
signalProcessr4r)r)r*�killProcessOs
	

zSFTPTestProcess.killProcesscCs,d|_|jr|jd}|_|�d�dSdS)zF
        Called by Twisted when the cftp client process ends.
        TN)r1r/r7)r'�reasonr�r)r)r*�processEnded^s
�zSFTPTestProcess.processEndedN)r,r-r.r/rir-r:r8r>r?rFrPrSrUr)r)r)r*r,�s
r,c@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�CFTPClientTestBasecCs�tdd��}|�tj�Wd�n1swYtdd��}|�tj�Wd�n1s1wYt�dd�tdd��}|�dtj�Wd�n1sTwYt�	|�S)N�dsa_test.pubr��dsa_testi���kh_tests
127.0.0.1 )
r�r�r�publicDSA_openssh�privateDSA_opensshr8�chmod�publicRSA_opensshrr@�r'�fr)r)r*r@is���
zCFTPClientTestBase.setUpcCsFt|j�}t�|�}|�t���t��}||_tj	d|dd�|_
dS)Nrz	127.0.0.1)�	interface)r%r&r	�Portal�registerCheckerr�conchTestPublicKeyChecker�ConchTestServerFactoryr�	listenTCP�server)r'�realm�p�facr)r)r*�startServerss

zCFTPClientTestBase.startServercCsHt|jjd�s|�d�Sd|jjj_t�|jjjjj	�}|�
|j�|S)N�protorx)�hasattrrf�factory�
_cbStopServerrk�expectedLoseConnectionr
�
maybeDeferredr|�loseConnectionr�r�r)r)r*�
stopServer{s
zCFTPClientTestBase.stopServercCst�|jj�Sr2)r
rprf�
stopListening�r'�ignoredr)r)r*rn�sz CFTPClientTestBase._cbStopServerc	Cs4dD]}zt�|�WqtyYqwt�|�S)N)rWrXrY)r8�remove�
BaseExceptionr�tearDownr^r)r)r*rx�s�
zCFTPClientTestBase.tearDownN)r,r-r.r@rjrrrnrxr)r)r)r*rVhs
rVc@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
s0t������d}�j��j}tj||��dd�}t	�
dtj�d|���t
��}t|��_|��fdd��tj��}tj�tj�|d<g}i}|D]}t|t�rZ|�d	�}|�|�qN|D]}||}	t|t�rr|�d	�}t|	t�r||	�d	�}	|	||<qbt	�
|�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 r�c�
�j��Sr2)�processProtocolr-��_r4r)r*r'��
z3OurServerCmdLineClientTests.setUp.<locals>.<lambda>�
PYTHONPATHr���env)rVr@rjrf�getHost�portr�	_makeArgsr�rr6r�r�r
r@r,r~r�r8r9�copy�pathsepr	rrrArBrCr�r�spawnProcess)
r'�cmdsr�r�r��encodedCmds�
encodedEnv�cmd�var�valr)r4r*r@�s>
�











�z!OurServerCmdLineClientTests.setUpcs���}|��fdd��|S)Ncr}r2)r~rSrr4r)r*r'�r�z6OurServerCmdLineClientTests.tearDown.<locals>.<lambda>)rrr�r�r)r4r*rx�sz$OurServerCmdLineClientTests.tearDowncCs,z
|jj�d�WdStjyYdSw)NrQ)r~r|rRr�ProcessExitedAlreadyrtr)r)r*�_killProcess�s
�z(OurServerCmdLineClientTests._killProcesscC�|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~rFrDr)r)r*rF��z&OurServerCmdLineClientTests.runCommandcGr�)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~rP)r'rNr)r)r*rP�r�z%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.
        �pwd�lpwdzcd testDirectory�cd ..cSsDg}|D]}t|t�r|�d�}|�|�q|dd�|dd�S)zH
            Callback function for handling command output.
            r�N��)rA�bytesr�r�)r�r�r�r)r)r*�	cmdOutput�s

z8OurServerCmdLineClientTests.testCdPwd.<locals>.cmdOutputr�)r&rPr�rLrrr8�getcwd)r'�homeDirr�r�r)r)r*�	testCdPwd�s
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--rxr�r�s
----------r�)�flushLoggedErrorsr%r;rL��resultsr4r)r*�_check�s
z7OurServerCmdLineClientTests.testChAttrs.<locals>._checkzls -l testfile1zchmod 0 testfile1zchmod 644 testfile1�rPr��r'r�r�r)r4r*�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.
        csf��|dgd����|dgd����|dddg���|dgd����|d	d
g�dS)Nr�s
testDirectory�testRemoveFile�testRenameFiles	testfile1rxr�r�r�r�)s.testHiddenFiler�r�r�r��rLr�r4r)r*r�s���z4OurServerCmdLineClientTests.testList.<locals>._checkrzls ../zls *Filezls -a *Filezls -l testDirectorycSsdd�|D�S)NcSsg|]}|�d��qS)r5)r�)rH�xr)r)r*rK*�zJOurServerCmdLineClientTests.testList.<locals>.<lambda>.<locals>.<listcomp>r))�xsr)r)r*r'*sz6OurServerCmdLineClientTests.testList.<locals>.<lambda>)rPr&r
r�r�r)r4r*�testLists�
z$OurServerCmdLineClientTests.testListcCsD|�d�}t�d��d���}t|t�r|�d�}|�|j	|�|S)zB
        Check that running the '?' command returns help.
        �?Nr�r�)
rFrr��cmd_HELPr�rArBrCr�rL)r'r��helpTextr)r)r*�testHelp-s


z$OurServerCmdLineClientTests.testHelpNcCs|�|��|��|�dS)zg
        Assert that the files at C{name1} and C{name2} contain exactly the
        same data.
        N)rL�
getContent)r'�name1�name2r6r)r)r*�assertFilesEqual9sz,OurServerCmdLineClientTests.assertFilesEqualcsld��jj�jj��t�t�r��d����fdd�}��d�jj�d��}|�|�|��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 {}/testfile1 to {}/test file2r�cs8��|�������j�d��j�d�d���d�S)N�	testfile1�
test file2z
get failedzrm "test file2")r%�endswithr�r&�childrF�r��r�r'r)r*�	_checkGetNs

�
z6OurServerCmdLineClientTests.testGet.<locals>._checkGetzget testfile1 "z/test file2"c����j�d����S)Nr���assertFalser&r��existsrr4r)r*r'Zr�z5OurServerCmdLineClientTests.testGet.<locals>.<lambda>)r#r&rrrArBrCrFr�)r'r�r�r)r�r*�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&r�r)rur4r)r*r�cs
�
�z;OurServerCmdLineClientTests.testWildcardGet.<locals>._checkz
get testR*)rFr�r�r)r4r*�testWildcardGet^s

z+OurServerCmdLineClientTests.testWildcardGetcshd�j��jd�j��jd���fdd�}��d�jj�d��}|�|�|��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&r�r%r�rFr�r�r)r*�	_checkPut�s
�
z6OurServerCmdLineClientTests.testPut.<locals>._checkPut�put z/testfile1 "test\"file2"cr�)Nr�r�rr4r)r*r'�r�z5OurServerCmdLineClientTests.testPut.<locals>.<lambda>)r&�asBytesModerrrFr�)r'r�r�r)r�r*�testPutrs"
��
���

�z#OurServerCmdLineClientTests.testPutcs��j�d�jdd��
}|�d�Wd�n1swY�j�d�jdd��
}|�d�Wd�n1s9wY�fdd	�}��d
�jj�d��}|�|�|S)zb
        Check that 'put' uploads files correctly when overwriting a longer
        file.
        �shorterFile�w��mode�aN�
longerFile�bbcs ���j�d��j�d��dS)Nr�r�)r�r&r�r�r4r)r*r��s�zEOurServerCmdLineClientTests.test_putOverLongerFile.<locals>._checkPutr�z/shorterFile longerFile)r&r�r�r�rFrrr��r'r_r�r�r)r4r*�test_putOverLongerFile�s��
z2OurServerCmdLineClientTests.test_putOverLongerFilecs��j�d�������d�jdd��
}|�d�Wd�n1s#wY�j�d�jdd��
}|�d�Wd�n1sBwY��fdd	�}��d
�jj�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�cs����d��j�d��dS)Nr�)r�r�r&r��r'�someDirr)r*r��szMOurServerCmdLineClientTests.test_putMultipleOverLongerFile.<locals>._checkPutr�z/dir/*)r&r��createDirectoryr�r�rFrrr�r�r)r�r*�test_putMultipleOverLongerFile�s��
z:OurServerCmdLineClientTests.test_putMultipleOverLongerFilecs>�fdd�}��dd�jj�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.
        csh��|dd���|dd����j�d��j���d�d����j�d��j���d�d�dS)Nrr�r�r�r�r�r�)rLr�r&r�rr�r4r)r*�check�s
�
�z:OurServerCmdLineClientTests.testWildcardPut.<locals>.checkr�r�z/testR*zcd %s)rPr&rrr
r�)r'r�r�r)r4r*�testWildcardPut�s�
z+OurServerCmdLineClientTests.testWildcardPutc�4�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�rx�lzlink failedzrm testLink)r�rLr%r;rFr�r4r)r*r��s
z4OurServerCmdLineClientTests.testLink.<locals>._checkzln testLink testfile1zls -l testLinkr��rPr�rLr�r)r4r*�testLink�s

z$OurServerCmdLineClientTests.testLinkcr�)zV
        Test that we can create and remove directories with the cftp client.
        cs.��|dd���|d�d����d�S)Nrr�rx�dzrmdir testMakeDirectory)rLr%r;rFr�r4r)r*r��s
z?OurServerCmdLineClientTests.testRemoteDirectory.<locals>._check�mkdir testMakeDirectoryzls -l testMakeDirector?r�r�r�r)r4r*�testRemoteDirectory��

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�rxsremote error 11: mkdir failedr�r�r4r)r*r��szHOurServerCmdLineClientTests.test_existingRemoteDirectory.<locals>._checkr�r�r�r)r4r*�test_existingRemoteDirectory�s
z8OurServerCmdLineClientTests.test_existingRemoteDirectorycsH��d�jj�d��}|��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 z/testLocalDirectoryr�cs
��d�S)Nzrmdir testLocalDirectory)rFrr4r)r*r'
r�z@OurServerCmdLineClientTests.testLocalDirectory.<locals>.<lambda>)rFr&rrr�rLr�r)r4r*�testLocalDirectorys
z.OurServerCmdLineClientTests.testLocalDirectorycr�)z1
        Test that we can rename a file.
        cs*��|dd���|dd���d�S)Nrr�rxs	testfile2zrename testfile2 testfile1)rLrFr�r4r)r*r�s
z6OurServerCmdLineClientTests.testRename.<locals>._checkzrename testfile1 testfile2zls testfile?r�r�r�r)r4r*�
testRenamer�z&OurServerCmdLineClientTests.testRenamer2)r,r-r.r/r@rxr�rFrPr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r)r)r)r*ry�s.*
ryc@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�|�|��dSr2�rVr@rjr4r)r)r*r@%s
zOurServerBatchFileTests.setUpcC�t�|�|��Sr2)rVrxrrr4r)r)r*rx)�
z OurServerBatchFileTests.tearDowncs�|���t�d��
}|�|�Wd�n1swY|j��j}d|�f}tj|��dd�dd�}t	�
dtj�d|���t
j��}t
j�tj�|d<d|jj_ttj||d	�}�fd
d�}|�dd
��|�|�|S)Nr�z�-p %i -l testuser --known-hosts kh_test --user-authentications publickey --host-key-algorithms ssh-rsa -i dsa_test -a -v -b %s 127.0.0.1rrzrxr|r�r�r�cst���|Sr2)r8rv��res��fnr)r*�_cleanupDs
z9OurServerBatchFileTests._getBatchOutput.<locals>._cleanupcSs|dS)Nrr)r�r)r)r*r'Hsz9OurServerBatchFileTests._getBatchOutput.<locals>.<lambda>)r�r�r�rfr�r�rr�r�rr6r�r�r8r9r�r�r	rrrmrorr��addBoth)r'r_�fpr�r�r�r�r�r)r�r*�_getBatchOutput-s$��	


z'OurServerBatchFileTests._getBatchOutputc�(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
csP|�d�}t�dt|�����j��j|d���|dd�gd��dS)Nr5zRES %srxr����r�)	r�rr6�repr�assertInr&r�rrrLr�r4r)r*�_cbCheckResultWs

�z=OurServerBatchFileTests.testBatchFile.<locals>._cbCheckResult�r�r��r'r�r�r�r)r4r*�
testBatchFileMs

	
z%OurServerBatchFileTests.testBatchFilecr�)zO
        Test that an error in the batch file stops running the batch.
        zchown 0 missingFile
pwd
exit
c����j��j|�dSr2)�assertNotInr&r�rrr�r4r)r*r�m�z9OurServerBatchFileTests.testError.<locals>._cbCheckResultr�r�r)r4r*�	testErrords


z!OurServerBatchFileTests.testErrorcr�)z_
        Test that a minus sign '-' at the front of a line ignores
        any errors.
        z-chown 0 missingFile
pwd
exit
cr�r2)r�r&r�rrr�r4r)r*r�~r�z@OurServerBatchFileTests.testIgnoredError.<locals>._cbCheckResultr�r�r)r4r*�testIgnoredErrorts


z(OurServerBatchFileTests.testIgnoredErrorN)
r,r-r.r/r@rxr�r�r�r�r)r)r)r*r�s r��sftpz%no sftp command-line client availablec@rm)	�OurServerSftpClientTestsz@
    Test the sftp server against sftp command line client.
    cCr�r2r�r4r)r)r*r@�r�zOurServerSftpClientTests.setUpcCs|��Sr2)rrr4r)r)r*rx�sz!OurServerSftpClientTests.tearDowncs�����t�d��
}|�d�Wd�n1swY�j��j�tj��fdd�}��td|�d�jj	_
tdd	�}��fd
d�}�fdd
�}|�|�|�dd��|�|�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 .
exitNcs�||�}d|d<|S)Nr��ext_foor))r'�sr�)�
oldGetAttrr)r*�	_getAttrs�s
zCOurServerSftpClientTests.test_extendedAttributes.<locals>._getAttrsrT�ssh)�-o�PubkeyAcceptedKeyTypes=ssh-dssz-Vcs>|dkrd}nd}|dddddddd	dd
�fd�df
7}|S)
Nr)rrr)z-Fz	/dev/nullrzIdentityFile=dsa_testzUserKnownHostsFile=kh_testzHostKeyAlgorithms=ssh-rsazPort=%iz-bztestuser@127.0.0.1r))�status�args)r�r�r)r*�hasPAKT�s&�zAOurServerSftpClientTests.test_extendedAttributes.<locals>.hasPAKTcs.��|dd�dD]
}��||d�q
dS)Nr�rr�)rLr�)r��ir4r)r*r��s�z?OurServerSftpClientTests.test_extendedAttributes.<locals>.checkcSs
td|�S)Nr)r)r
r)r)r*r'�r�zBOurServerSftpClientTests.test_extendedAttributes.<locals>.<lambda>)
r�r�r�rfr�r�r!rr<rmrorr�)r'r_rr�rr�r))r�rr�r'r*�test_extendedAttributes�s�




z0OurServerSftpClientTests.test_extendedAttributesN)r,r-r.r/r@rxr
r)r)r)r*r�s
r)Pr/r�rZr>r8r�r�r6�ior�unittestr�zope.interfacer�
twisted.conchr�twisted.conch.interfacesr�$twisted.conch.test.test_filetransferrr�twisted.credr	�twisted.internetr
rrr
r�twisted.internet.taskr�twisted.internet.utilsrr�twisted.pythonr�twisted.python.fakepwdr�twisted.python.filepathr�twisted.python.procutilsr�twisted.python.reflectr�twisted.test.proto_helpersr�twisted.trial.unittestrrr�unix�twisted.conch.scriptsr�twisted.conch.scripts.cftpr�twisted.conch.sshr�twisted.conch.testrr�twisted.conch.test.test_conchr r!�ImportError�	skipTests�IReactorProcessr"r0rernrwr�r�r%�ProcessProtocolr,rVryr�rr)r)r)r*�<module>s��#&2u	{'g

Anon7 - 2022
AnonSec Team