U
    n5{iJ   ã                   @   s.  d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZmZ e ¡  Zz
ej W n ek
rˆ   dZY nX dZG d	d
„ d
ƒZG dd„ deƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZeedƒG dd„ deƒƒZeedƒG dd„ deeƒƒZdS )é    N)ÚskipIf)ÚHostnameAddressÚIPv4AddressÚIPv6AddressÚUNIXAddress)ÚnativeString)Úplatform)ÚSynchronousTestCaseÚTestCaseTFc                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚAddressTestCaseMixinc                 C   s0   |   |  ¡ |  ¡ k¡ |  |  ¡ |  ¡ k¡ dS )a  
        Two different address instances, sharing the same properties are
        considered equal by C{==} and not considered not equal by C{!=}.

        Note: When applied via UNIXAddress class, this uses the same
        filename for both objects being compared.
        N)Ú
assertTrueÚbuildAddressÚassertFalse©Úself© r   úF/tmp/pip-unpacked-wheel-mnhg7r0q/twisted/internet/test/test_address.pyÚtest_addressComparison   s    z+AddressTestCaseMixin.test_addressComparisonc                 C   s&   |   ¡ }|di}|  ||   ¡  ¡ dS )z“
        C{__hash__} can be used to get a hash of an address, allowing
        addresses to be used as keys in dictionaries, for instance.
        TN©r   r   ©r   ÚaddrÚdr   r   r   Ú	test_hash)   s    zAddressTestCaseMixin.test_hashc                 C   s\   |   |  ¡ |  ¡ k¡ |   |  ¡ |  ¡ k¡ |  |  ¡ |  ¡ k¡ |  |  ¡ |  ¡ k¡ dS )z~
        Check that comparison operators work correctly on address objects
        when a different name is passed in
        N)r   r   ÚbuildDifferentAddressr   r   r   r   r   Útest_differentNamesComparison2   s    z2AddressTestCaseMixin.test_differentNamesComparisonc                 C   sH   |   |g¡}|  |d d t¡ |  |d d |¡ |  t|ƒd¡ dS )z{
        Assert that the a DeprecationWarning with the given message was
        emitted against the given method.
        r   ÚcategoryÚmessageé   N)ZflushWarningsÚassertEqualÚDeprecationWarningÚlen)r   Z
testMethodr   Úwarningsr   r   r   ÚassertDeprecations=   s    z'AddressTestCaseMixin.assertDeprecationsN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r"   r   r   r   r   r      s   	r   c                   @   s   e Zd ZdZdS )ÚIPv4AddressTestCaseMixin©)Útypeú%s)Úhostú%r©Úportz%dN)r#   r$   r%   ÚaddressArgSpecr   r   r   r   r&   H   s   r&   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚHostnameAddressTestsz+
    Test case for L{HostnameAddress}.
    ))Úhostnamer)   r,   c                 C   s
   t ddƒS )zw
        Create an arbitrary new L{HostnameAddress} instance.

        @return: A L{HostnameAddress} instance.
        s   example.comr   ©r   r   r   r   r   r   S   s    z!HostnameAddressTests.buildAddressc                 C   s
   t ddƒS )zw
        Like L{buildAddress}, but with a different hostname.

        @return: A L{HostnameAddress} instance.
        s   example.netr   r1   r   r   r   r   r   [   s    z*HostnameAddressTests.buildDifferentAddressN)r#   r$   r%   Ú__doc__r.   r   r   r   r   r   r   r/   L   s   r/   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚIPv4AddressTCPTestsc                 C   s   t dddƒS )z²
        Create an arbitrary new L{IPv4Address} instance with a C{"TCP"}
        type.  A new instance is created for each call, but always for the
        same address.
        ÚTCPú	127.0.0.1r   ©r   r   r   r   r   r   e   s    z IPv4AddressTCPTests.buildAddressc                 C   s   t dddƒS )úK
        Like L{buildAddress}, but with a different fixed address.
        r4   ú	127.0.0.2r   r6   r   r   r   r   r   m   s    z)IPv4AddressTCPTests.buildDifferentAddressN©r#   r$   r%   r   r   r   r   r   r   r3   d   s   r3   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚIPv4AddressUDPTestsc                 C   s   t dddƒS )z²
        Create an arbitrary new L{IPv4Address} instance with a C{"UDP"}
        type.  A new instance is created for each call, but always for the
        same address.
        ÚUDPr5   r   r6   r   r   r   r   r   u   s    z IPv4AddressUDPTests.buildAddressc                 C   s   t dddƒS )r7   r;   r8   r   r6   r   r   r   r   r   }   s    z)IPv4AddressUDPTests.buildDifferentAddressNr9   r   r   r   r   r:   t   s   r:   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚIPv6AddressTestsr'   c                 C   s   t dddƒS )z²
        Create an arbitrary new L{IPv6Address} instance with a C{"TCP"}
        type.  A new instance is created for each call, but always for the
        same address.
        r4   z::1r   ©r   r   r   r   r   r   ‡   s    zIPv6AddressTests.buildAddressc                 C   s   t dddƒS )r7   r4   z::2r   r=   r   r   r   r   r   �   s    z&IPv6AddressTests.buildDifferentAddressN)r#   r$   r%   r.   r   r   r   r   r   r   r<   „   s   r<   z&Platform doesn't support UNIX sockets.c                   @   sT   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zee	d
ƒdd„ ƒZ
ee	d
ƒdd„ ƒZdS )ÚUNIXAddressTests©)Únamer+   c                 C   s   |   ¡ | _|   ¡ | _d S ©N)ÚmktempÚ_socketAddressÚ_otherAddressr   r   r   r   ÚsetUpš   s    
zUNIXAddressTests.setUpc                 C   s
   t | jƒS )z•
        Create an arbitrary new L{UNIXAddress} instance.  A new instance is
        created for each call, but always for the same address.
        ©r   rC   r   r   r   r   r   ž   s    zUNIXAddressTests.buildAddressc                 C   s
   t | jƒS )r7   )r   rD   r   r   r   r   r   ¥   s    z&UNIXAddressTests.buildDifferentAddressc                 C   s"   |   t|  ¡ ƒdt| jƒ ¡ dS )zn
        The repr of L{UNIXAddress} returns with the filename that the
        L{UNIXAddress} is for.
        zUNIXAddress('%s')N)r   Úreprr   r   rC   r   r   r   r   Ú	test_repr«   s    
þzUNIXAddressTests.test_reprú"Platform does not support symlinksc              	   C   sh   |   ¡ }t| jdƒ�J| _t tj | j¡|¡ |  t	| jƒt	|ƒ¡ |  t	|ƒt	| jƒ¡ W 5 Q R X dS )zU
        UNIXAddress objects compare as equal if they link to the same file.
        ÚwN)
rB   ÚopenrC   ÚfdÚosÚsymlinkÚpathÚabspathr   r   ©r   ZlinkNamer   r   r   Útest_comparisonOfLinkedFilesµ   s
    z-UNIXAddressTests.test_comparisonOfLinkedFilesc              	   C   sZ   |   ¡ }t| jdƒ�<| _t tj | j¡|¡ |  t	t
| jƒƒt	t
|ƒƒ¡ W 5 Q R X dS )zU
        UNIXAddress Objects that compare as equal have the same hash value.
        rJ   N)rB   rK   rC   rL   rM   rN   rO   rP   r   Úhashr   rQ   r   r   r   Útest_hashOfLinkedFilesÀ   s     
ÿz'UNIXAddressTests.test_hashOfLinkedFilesN)r#   r$   r%   r.   rE   r   r   rH   r   ÚsymlinkSkiprR   rT   r   r   r   r   r>   –   s   


r>   z&platform doesn't support UNIX sockets.c                   @   sF   e Zd ZdZdZdd„ Zdd„ Zdd„ Zee	d	ƒd
d„ ƒZ
dd„ ZdS )ÚEmptyUNIXAddressTestszJ
    Tests for L{UNIXAddress} operations involving a L{None} address.
    r?   c                 C   s   |   ¡ | _d S rA   )rB   rC   r   r   r   r   rE   Õ   s    zEmptyUNIXAddressTests.setUpc                 C   s   t dƒS )zÍ
        Create an arbitrary new L{UNIXAddress} instance.  A new instance is
        created for each call, but always for the same address. This builds it
        with a fixed address of L{None}.
        N)r   r   r   r   r   r   Ø   s    z"EmptyUNIXAddressTests.buildAddressc                 C   s
   t | jƒS )zN
        Like L{buildAddress}, but with a random temporary directory.
        rF   r   r   r   r   r   à   s    z+EmptyUNIXAddressTests.buildDifferentAddressrI   c              	   C   sh   |   ¡ }t| jdƒ�J| _t tj | j¡|¡ |  t	| jƒt	dƒ¡ |  t	dƒt	| jƒ¡ W 5 Q R X dS )z†
        A UNIXAddress referring to a L{None} address does not
        compare equal to a UNIXAddress referring to a symlink.
        rJ   N)
rB   rK   rC   rL   rM   rN   rO   rP   ZassertNotEqualr   rQ   r   r   r   rR   æ   s
    z2EmptyUNIXAddressTests.test_comparisonOfLinkedFilesc                 C   s&   |   ¡ }|di}|  ||   ¡  ¡ dS )z…
        C{__hash__} can be used to get a hash of an address, even one referring
        to L{None} rather than a real path.
        TNr   r   r   r   r   Útest_emptyHashò   s    z$EmptyUNIXAddressTests.test_emptyHashN)r#   r$   r%   r2   r.   rE   r   r   r   rU   rR   rW   r   r   r   r   rV   Í   s   
rV   )rM   ÚsocketZunittestr   Ztwisted.internet.addressr   r   r   r   Ztwisted.python.compatr   Ztwisted.python.runtimer   Ztwisted.trial.unittestr	   r
   Z_supportsSymlinksrU   ÚAF_UNIXÚAttributeErrorZunixSkipr   r&   r/   r3   r:   r<   r>   rV   r   r   r   r   Ú<module>   s,   


+6