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: //usr/lib/python3/dist-packages/twisted/conch/test/__pycache__/test_connection.cpython-38.pyc
U


W[�m�@s�dZddlmZmZddlZddlmZed�Zddlm	Z	erRddl
mZmZnGdd	�d	�Zdd
l
m
Z
ddlmZddlmZdd
lmZGdd�de
j�ZGdd�d�ZGdd�dej�ZGdd�dej�ZGdd�dej�ZdS)z1
This module tests twisted.conch.ssh.connection.
�)�division�absolute_importN)�
requireModule�cryptography)�error)�common�
connectionc@seZdZGdd�d�ZdS)rc@seZdZdS)zconnection.SSHConnectionN)�__name__�
__module__�__qualname__�rr�D/usr/lib/python3/dist-packages/twisted/conch/test/test_connection.py�
SSHConnectionsrN)r	r
rrrrrr
rsr)�channel)�long)�unittest)�
test_userauthc@s`eZdZdZdZdZdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�ZdS)�TestChannela�
    A mocked-up version of twisted.conch.ssh.channel.SSHChannel.

    @ivar gotOpen: True if channelOpen has been called.
    @type gotOpen: L{bool}
    @ivar specificData: the specific channel open data passed to channelOpen.
    @type specificData: L{bytes}
    @ivar openFailureReason: the reason passed to openFailed.
    @type openFailed: C{error.ConchError}
    @ivar inBuffer: a C{list} of strings received by the channel.
    @type inBuffer: C{list}
    @ivar extBuffer: a C{list} of 2-tuples (type, extended data) of received by
        the channel.
    @type extBuffer: C{list}
    @ivar numberRequests: the number of requests that have been made to this
        channel.
    @type numberRequests: L{int}
    @ivar gotEOF: True if the other side sent EOF.
    @type gotEOF: L{bool}
    @ivar gotOneClose: True if the other side closed the connection.
    @type gotOneClose: L{bool}
    @ivar gotClosed: True if the channel is closed.
    @type gotClosed: L{bool}
    �TestChannelFcCs
d|jS)NzTestChannel %i)�id��selfrrr
�	logPrefix8szTestChannel.logPrefixcCs4d|_||_g|_g|_d|_d|_d|_d|_dS)zF
        The channel is open.  Set up the instance variables.
        TrFN)�gotOpen�specificData�inBuffer�	extBuffer�numberRequests�gotEOF�gotOneClose�	gotClosed)rrrrr
�channelOpen;szTestChannel.channelOpencCs
||_dS)zD
        Opening the channel failed.  Store the reason why.
        N)�openFailureReason)r�reasonrrr
�
openFailedHszTestChannel.openFailedcCs|jd7_|dkS)z_
        A test request.  Return True if data is 'data'.

        @type data: L{bytes}
        ��data)r�r�datarrr
�request_testNszTestChannel.request_testcCs|j�|�dS)z=
        Data was received.  Store it in the buffer.
        N)r�appendr'rrr
�dataReceivedWszTestChannel.dataReceivedcCs|j�||f�dS)zF
        Extended data was received.  Store it in the buffer.
        N)rr*)r�coder(rrr
�extReceived]szTestChannel.extReceivedcCs
d|_dS)z1
        EOF was received.  Remember it.
        TN)rrrrr
�eofReceivedcszTestChannel.eofReceivedcCs
d|_dS)z3
        Close was received.  Remember it.
        TN)rrrrr
�
closeReceivediszTestChannel.closeReceivedcCs
d|_dS)z7
        The channel is closed.  Rembember it.
        TN)r rrrr
�closedoszTestChannel.closedN)r	r
r�__doc__�namerrr!r$r)r+r-r.r/r0rrrr
rs
	rc@s$eZdZdZdZdd�Zdd�ZdS)�
TestAvatarz?
    A mocked-up version of twisted.conch.avatar.ConchUser
    �{cCs4|tjkrt||||d�S|dkr0t�|jd��dS)z�
        The server wants us to return a channel.  If the requested channel is
        our TestChannel, return it, otherwise return None.
        )�remoteWindow�remoteMaxPacketr(�avatar�conch-error-args�error args in wrong orderN)rr2r�
ConchError�_ARGS_ERROR_CODE)rZchannelType�
windowSize�	maxPacketr(rrr
�
lookupChannel{s
��zTestAvatar.lookupChannelcCs$|dkrdS|dkrd|fSdSdS)z�
        The client has made a global request.  If the global request is
        'TestGlobal', return True.  If the global request is 'TestData',
        return True and the request-specific data we received.  Otherwise,
        return False.
        �
TestGlobalT�TestDataFNr)rZrequestTyper(rrr
�gotGlobalRequest�s
zTestAvatar.gotGlobalRequestN)r	r
rr1r;r>rArrrr
r3usr3c@s@eZdZdZesdZdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
S)�TestConnectionz}
    A subclass of SSHConnection for testing.

    @ivar channel: the current channel.
    @type channel. C{TestChannel}
    �Cannot run without cryptographycCsdS)NrBrrrrr
r�szTestConnection.logPrefixcCsdS)zT
        The other side made the 'TestGlobal' global request.  Return True.
        Trr'rrr
�global_TestGlobal�sz TestConnection.global_TestGlobalcCsd|fS)zt
        The other side made the 'Test-Data' global request.  Return True and
        the data we received.
        Trr'rrr
�global_Test_Data�szTestConnection.global_Test_DatacCst|||d�|_|jS)z�
        The other side is requesting the TestChannel.  Create a C{TestChannel}
        instance, store it, and return it.
        )r5r6r()rr�rr<r=r(rrr
�channel_TestChannel�s
�z"TestConnection.channel_TestChannelcCstd��dS)zU
        The other side is requesting the ErrorChannel.  Raise an exception.
        z
no such thingN)�AssertionErrorrFrrr
�channel_ErrorChannel�sz#TestConnection.channel_ErrorChannelN)r	r
rr1r�skiprrDrErGrIrrrr
rB�s	rBc@s2eZdZesdZejdkrdZdd�Zdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Z d8d9�Z!d:d;�Z"d<d=�Z#d>d?�Z$d@dA�Z%dBdC�Z&dDdE�Z'dFdG�Z(dS)H�ConnectionTestsrCN�/Cannot run without both cryptography and pyasn1cCs6t�d�|_t�|j_t�|_|j|j_|j��dS�N)r�
FakeTransport�	transportr3r7rB�conn�serviceStartedrrrr
�setUp�s


zConnectionTests.setUpcCs@|j�|�|jjdd�|j_|j�t�d|jd�d�dS)z?
        Open the channel with the default connection.
        N���z>2L�s�)rP�openChannelrO�packets�ssh_CHANNEL_OPEN_CONFIRMATION�structZpackr�rrrrr
�_openChannel�s��zConnectionTests._openChannelcCs|j��dSrM)rP�serviceStoppedrrrr
�tearDown�szConnectionTests.tearDowncCs|�|jjj|j�dS)z_
        Test that the connection links itself to the avatar in the
        transport.
        N)ZassertIsrOr7rPrrrr
�test_linkAvatar�szConnectionTests.test_linkAvatarcCsbt�}t�}|j�|�|j�|�|j�d�|�|j�|�|j�|j��|�|j�dS)zF
        Test that serviceStopped() closes any open channels.
        sN)	rrPrUrW�
assertTruer�assertFalser[r )rZchannel1�channel2rrr
�test_serviceStopped�s
z#ConnectionTests.test_serviceStoppedcCs�|j�t�d�d�|�|jjtjdfg�g|j_|j�t�d�dd�|�|jjtjdfg�g|j_|j�t�d�d�|�|jjtj	dfg�g|j_|j�t�d�d�|�|jjg�dS)	z�
        Test that global request packets are dispatched to the global_*
        methods and the return values are translated into success or failure
        messages.
        r?���r@s	test datasTestBad�N)
rPZssh_GLOBAL_REQUESTr�NS�assertEqualrOrVrZMSG_REQUEST_SUCCESSZMSG_REQUEST_FAILURErrrr
�test_GLOBAL_REQUEST�s&

��

�

�z#ConnectionTests.test_GLOBAL_REQUESTcsB�j�ddd�}�j�d��fdd�}|�|�|��j�|S)zh
        Test that global request success packets cause the Deferred to be
        called back.
        �requestr&Tcs��|d�dS�Nr&)rf)r(rrr
�checksz3ConnectionTests.test_REQUEST_SUCCESS.<locals>.check)rP�sendGlobalRequestZssh_REQUEST_SUCCESS�addCallback�
addErrback�fail�r�drjrrr
�test_REQUEST_SUCCESSs
z$ConnectionTests.test_REQUEST_SUCCESScsB�j�ddd�}�j�d��fdd�}|��j�|�|�|S)zg
        Test that global request failure packets cause the Deferred to be
        erred back.
        rhr&Tcs��|jjd�dSri)rf�valuer()�frrr
rj"sz3ConnectionTests.test_REQUEST_FAILURE.<locals>.check)rPrkZssh_REQUEST_FAILURErlrnrmrorrr
�test_REQUEST_FAILUREs
z$ConnectionTests.test_REQUEST_FAILUREcCsH|j`|j�t�d�d�|�|jjj�|�	|jjj|j�|�	|jjj
d�|�	|jjjd�|�	|jjjd�|�	|jjj
d�|�	|jjtjdfg�g|j_|j�t�d�d�|��|�	|jjtjdt�d	�t�d
�fg�g|j_|j�t�d�d�|��|�	|jjtjdt�d
�t�d
�fg�dS)z�
        Test that open channel packets cause a channel to be created and
        opened or a failure message to be returned.
        r��r%s��
BadChannelsssunknown channelrcsErrorChannelssunknown failureN)rOr7rP�ssh_CHANNEL_OPENrrer^rrrfr(r�remoteWindowLeftr6rVrZMSG_CHANNEL_OPEN_CONFIRMATION�flushLoggedErrors�MSG_CHANNEL_OPEN_FAILURErrrr
�test_CHANNEL_OPEN(sV�
���
�����
����z!ConnectionTests.test_CHANNEL_OPENcCs�||jj_|j�t�d�d�|�tj	�}|�
t|�dd|f�|�
|djj
td�df�|�
|jjtjdt�d	�t�d
�fg�dS)z�
        Deliver a request for a channel open which will result in an exception
        being raised during channel lookup.  Assert that an error response is
        delivered as a result.
        r8rur%zExpected one error, got: %rrr4r9s{serror args in wrong orderrcN)rOr7r;rPrxrrerzrr:rf�lenrr�argsrrVrr{)rr,�errorsrrr
�_lookupChannelErrorTestLs*
������z'ConnectionTests._lookupChannelErrorTestcCs|�d�dS)a�
        If a C{lookupChannel} implementation raises L{error.ConchError} with the
        arguments in the wrong order, a C{MSG_CHANNEL_OPEN} failure is still
        sent in response to the message.

        This is a temporary work-around until L{error.ConchError} is given
        better attributes and all of the Conch code starts constructing
        instances of it properly.  Eventually this functionality should be
        deprecated and then removed.
        r4N)r�rrrr
�test_lookupChannelErrorcsz'ConnectionTests.test_lookupChannelErrorcCs|�td��dS)z�
        Like L{test_lookupChannelError}, but for the case where the failure code
        is represented as a L{long} instead of a L{int}.
        r4N)r�rrrrr
�test_lookupChannelErrorLongCodeqsz/ConnectionTests.test_lookupChannelErrorLongCodecCstt�}|j�|�|j�d�|�|jd�|�|jd�|�|jd�|�|jj|d�|�|jj	dd�dS)zv
        Test that channel open confirmation packets cause the channel to be
        notified that it's open.
        sr�N)
rrPrUrWrfryr6rZchannelsToRemoteChannelZlocalToRemoteChannelrYrrr
�test_CHANNEL_OPEN_CONFIRMATIONys�z.ConnectionTests.test_CHANNEL_OPEN_CONFIRMATIONcCsPt�}|j�|�|j�dt�d��|�|jjd�|�	|jj
�|��dS)zz
        Test that channel open failure packets cause the channel to be
        notified that its opening failed.
        ��failure!)r�r%N)rrPrUZssh_CHANNEL_OPEN_FAILURErrerfr"r~ZassertIsNoneZchannels�getrYrrr
�test_CHANNEL_OPEN_FAILURE�s�z)ConnectionTests.test_CHANNEL_OPEN_FAILUREcCs8t�}|�|�|j}|j�d�|�|j|d�dS)zc
        Test that channel window adjust messages add bytes to the channel
        window.
        r�r%N)rrZryrPZssh_CHANNEL_WINDOW_ADJUSTrf)rrZ
oldWindowSizerrr
�test_CHANNEL_WINDOW_ADJUST�s

z*ConnectionTests.test_CHANNEL_WINDOW_ADJUSTcCstddd�}|�|�|j�dt�d��|�|jdg�|�|jj	t
jdfg�g|j_	d|jd}|j�dt�|��|�|jdg�|�|jj	t
j
d	fg�t�}|�|�d|jd}g|j_	|j�d
t�|��|�|jg�|�|jj	t
j
d	fg�dS)z�
        Test that channel data messages are passed up to the channel, or
        cause the channel to be closed if the data is too large.
        ����localWindow�localMaxPacketr�r&���ar%��rvN)rrZrPZssh_CHANNEL_DATArrerfrrOrVr�MSG_CHANNEL_WINDOW_ADJUST�localWindowLeft�MSG_CHANNEL_CLOSEr��rrZlongDataZbigDatarrr
�test_CHANNEL_DATA�s.


�

�


�z!ConnectionTests.test_CHANNEL_DATAcCstddd�}|�|�|j�dt�d��|�|jdg�|�|jj	t
jdfg�g|j_	d|jd	}|j�dt�|��|�|jdg�|�|jj	t
j
d
fg�t�}|�|�d|jd	}g|j_	|j�dt�|��|�|jg�|�|jj	t
j
d
fg�dS)
z�
        Test that channel extended data messages are passed up to the channel,
        or cause the channel to be closed if they're too big.
        r�r�r�sr&)rr&r�r�r%r�sN)rrZrPZssh_CHANNEL_EXTENDED_DATArrerfrrOrVrr�r�r�r�r�rrr
�test_CHANNEL_EXTENDED_DATA�s:
�

��

�
�

�z*ConnectionTests.test_CHANNEL_EXTENDED_DATAcCs,t�}|�|�|j�d�|�|j�dS)zN
        Test that channel eof messages are passed up to the channel.
        r�N)rrZrPZssh_CHANNEL_EOFr^rrYrrr
�test_CHANNEL_EOF�s
z ConnectionTests.test_CHANNEL_EOFcCsDt�}|�|�|j�|�|j�d�|�|j�|�|j�dS)z�
        Test that channel close messages are passed up to the channel.  Also,
        test that channel.close() is called if both sides are closed when this
        message is received.
        r�N)rrZrP�	sendCloseZssh_CHANNEL_CLOSEr^rr rYrrr
�test_CHANNEL_CLOSE�s
z"ConnectionTests.test_CHANNEL_CLOSEcspt�}��|��j�dt�d�d���|jd��j�dt�d�dd�}�fdd�}|�|�|S)	zS
        Test that channel requests that succeed send MSG_CHANNEL_SUCCESS.
        r��testrdr%rbr&cs���jjtjdfg�dS�Nr�)rfrOrVrZMSG_CHANNEL_SUCCESS��resultrrr
rj�s

�z;ConnectionTests.test_CHANNEL_REQUEST_success.<locals>.check)	rrZrP�ssh_CHANNEL_REQUESTrrerfrrl�rrrprjrrr
�test_CHANNEL_REQUEST_success�s 
����
z,ConnectionTests.test_CHANNEL_REQUEST_successcsPt�}��|��j�dt�d�d�}�fdd�}|��j�|�|�|S)zP
        Test that channel requests that fail send MSG_CHANNEL_FAILURE.
        r�r�rbcs���jjtjdfg�dSr�)rfrOrVrZMSG_CHANNEL_FAILUREr�rrr
rj	s

�z;ConnectionTests.test_CHANNEL_REQUEST_failure.<locals>.check)	rrZrPr�rrerlrnrmr�rrr
�test_CHANNEL_REQUEST_failures
��
z,ConnectionTests.test_CHANNEL_REQUEST_failurecs>t�}��|��j�|ddd�}�j�d��fdd�}|S)zj
        Test that channel request success messages cause the Deferred to be
        called back.
        r�r&Tr�cs��|�dSrM)r^r�rrr
rjsz;ConnectionTests.test_CHANNEL_REQUEST_SUCCESS.<locals>.check)rrZrP�sendRequestZssh_CHANNEL_SUCCESSr�rrr
�test_CHANNEL_REQUEST_SUCCESSs
z,ConnectionTests.test_CHANNEL_REQUEST_SUCCESScsTt�}��|��j�|ddd�}�j�d��fdd�}|��j�|�|�|S)zi
        Test that channel request failure messages cause the Deferred to be
        erred back.
        r�rcTr�cs��|jjd�dS)Nzchannel request failed)rfrrr�rrr
rj'sz;ConnectionTests.test_CHANNEL_REQUEST_FAILURE.<locals>.check)rrZrPr�Zssh_CHANNEL_FAILURErlrnrmr�rrr
�test_CHANNEL_REQUEST_FAILUREs

z,ConnectionTests.test_CHANNEL_REQUEST_FAILUREcCs||j�ddd�}|�dd��|j�ddd�|�|jjtjt�	d�d	ftjt�	d�d
fg�|�|jj
d|gi�dS)
zQ
        Test that global request messages are sent in the right format.
        s	wantReplyr&TcSsdSrMr�Zfailurerrr
�<lambda>3rcz8ConnectionTests.test_sendGlobalRequest.<locals>.<lambda>snoReplyrcFs�datard�globalN)rPrkrmrfrOrVrZMSG_GLOBAL_REQUESTrre�	deferreds�rrprrr
�test_sendGlobalRequest-s
����z&ConnectionTests.test_sendGlobalRequestcCsXt�}|j�|d�|�|jjtjt�	d�dfg�|�|j
d�|�|jjd�dS)zO
        Test that open channel messages are sent in the right format.
        saaaars�aaaarr%N)rrPrUrfrOrVrZMSG_CHANNEL_OPENrrerZlocalChannelIDrYrrr
�test_openChannel<s
��z ConnectionTests.test_openChannelc	Cs�t�}|�|�|j�|ddd�}|�dd��|j�|ddd�d|_|j�|ddd�|�|jjt	j
d	t�d�d
ft	j
d	t�d�dfg�|�|jj
d|g�d
S)zR
        Test that channel request messages are sent in the right format.
        r�TcSsdSrMrr�rrr
r�Prcz2ConnectionTests.test_sendRequest.<locals>.<lambda>�test2rcFstest3r�stestrdrN)rrZrPr�rm�localClosedrfrOrVrZMSG_CHANNEL_REQUESTrrer��rrrprrr
�test_sendRequestHs*

������z ConnectionTests.test_sendRequestcCsttdd�}|�|�d|_|j�|d�|�|jd�d|_|j�|d�|�|jd�|�|jjt	j
dfg�dS)	zi
        Test that channel window adjust messages cause bytes to be added
        to the window.
        r�)r�rr%T���N)rrZr�rPZadjustWindowrfr�rOrVrr�rYrrr
�test_adjustWindow[s



�z!ConnectionTests.test_adjustWindowcCsXt�}|�|�|j�|d�d|_|j�|d�|�|jjtj	dt
�d�fg�dS)zO
        Test that channel data messages are sent in the right format.
        r�T�br�N)rrZrPZsendDatar�rfrOrVrZMSG_CHANNEL_DATArrerYrrr
�
test_sendDatals

��zConnectionTests.test_sendDatacCs\t�}|�|�|j�|dd�d|_|j�|dd�|�|jjtj	dt
�d�fg�dS)zX
        Test that channel extended data messages are sent in the right format.
        r%r�Tr�r�r�N)rrZrPZsendExtendedDatar�rfrOrVrZMSG_CHANNEL_EXTENDED_DATArrerYrrr
�test_sendExtendedDatays

��z%ConnectionTests.test_sendExtendedDatacCsbt�}|�|�|j�|�|�|jjtjdfg�d|_	|j�|�|�|jjtjdfg�dS)zN
        Test that channel EOF messages are sent in the right format.
        r�TN)
rrZrPZsendEOFrfrOrVrZMSG_CHANNEL_EOFr�rYrrr
�test_sendEOF�s


�

�zConnectionTests.test_sendEOFcCs�t�}|�|�|j�|�|�|j�|�|jjt	j
dfg�|j�|�|�|jjt	j
dfg�t�}|�|�d|_|j�|�|�|j�dS)zP
        Test that channel close messages are sent in the right format.
        r�TN)
rrZrPr�r^r�rfrOrVrr�ZremoteClosedr )rrr`rrr
�test_sendClose�s 


�

�
zConnectionTests.test_sendClosecCsZ|j�dddd�}|�|jd�|�|jd�|�|jd�|�tj|jjdddd�dS)z�
        Test that getChannel dispatches to the avatar when an avatar is
        present. Correct functioning without the avatar is verified in
        test_CHANNEL_OPEN.
        r�2�r&rwN)	rPZ
getChannelrfr(ryr6ZassertRaisesrr:rYrrr
�test_getChannelWithAvatar�s�z)ConnectionTests.test_getChannelWithAvatarcCsH|j`|�|j�dd��|�|j�dd�d�|�|j�dd��dS)zW
        Test that gotGlobalRequests dispatches to global_* without an avatar.
        r?r&s	Test-Data)Tr&s	BadGlobalN)rOr7r^rPrArfr_rrrr
�"test_gotGlobalRequestWithoutAvatar�s�z2ConnectionTests.test_gotGlobalRequestWithoutAvatarcCsBt�}|�|�|jj|dddd�}|�|tj�}|j�|�|S)z�
        Whenever an SSH channel gets closed any Deferred that was returned by a
        sendRequest() on its parent connection must be errbacked.
        �dummyrequest�	dummydatar%�Z	wantReply)rrZrPr��
assertFailurerr:Z
channelClosedr�rrr
�9test_channelClosedCausesLeftoverChannelDeferredsToErrback�s
�zIConnectionTests.test_channelClosedCausesLeftoverChannelDeferredsToErrback))r	r
rrrJrrOrRrZr\r]rargrqrtr|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rr
rK�sL
	

$
	




rKc@s6eZdZdZesdZejdkr"dZdd�Zdd�Z	dS)	�CleanConnectionShutdownTestszL
    Check whether correct cleanup is performed on connection shutdown.
    rCNrLcCs,t�d�|_t�|j_t�|_|j|j_dSrM)rrNrOr3r7rBrPrrrr
rR�s
z"CleanConnectionShutdownTests.setUpcCs8|j��|jjdddd�}|�|tj�}|j��|S)z�
        Once the service is stopped any leftover global deferred returned by
        a sendGlobalRequest() call must be errbacked.
        r�r�r%r�)rPrQrkr�rr:r[r�rrr
�9test_serviceStoppedCausesLeftoverGlobalDeferredsToErrback�s
�
zVCleanConnectionShutdownTests.test_serviceStoppedCausesLeftoverGlobalDeferredsToErrback)
r	r
rr1rrJrrOrRr�rrrr
r��s
r�)r1Z
__future__rrrXZtwisted.python.reflectrrZ
twisted.conchrZtwisted.conch.sshrrrZtwisted.python.compatrZ
twisted.trialrZtwisted.conch.testrZ
SSHChannelrr3rrBZTestCaserKr�rrrr
�<module>s*Y(,