U
    
W[a  ã                   @   s”   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ G dd„ deƒZdS )z!
Tests for L{twisted.names.tap}.
é    )ÚThreadedResolver)ÚResolver)ÚPORT)ÚResolverChain)ÚSecondaryAuthorityService)ÚOptionsÚ_buildResolvers)Ú_PY3)Úplatform)Ú
UsageError)ÚSynchronousTestCasec                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚOptionsTestszf
    Tests for L{Options}, defining how command line arguments for the DNS server
    are parsed.
    c                 C   sZ   t ƒ }|  t|jddg¡ |  t|jddg¡ |  t|jddg¡ |  t|jddg¡ dS )zÏ
        If the value supplied for an I{--secondary} option does not provide a
        server IP address, optional port number, and domain name,
        L{Options.parseOptions} raises L{UsageError}.
        ú--secondaryÚ ú1.2.3.4z1.2.3.4:helloz1.2.3.4:hello/example.comN)r   ZassertRaisesr   ÚparseOptions©ÚselfÚoptions© r   ú=/usr/lib/python3/dist-packages/twisted/names/test/test_tap.pyÚtest_malformedSecondary   s*      ÿ  ÿ  ÿ þz$OptionsTests.test_malformedSecondaryc                 C   s2   t ƒ }| ddg¡ |  dtfdgfg|j¡ dS )zÇ
        An argument of the form C{"ip/domain"} is parsed by L{Options} for the
        I{--secondary} option and added to its list of secondaries, using the
        default DNS port number.
        r   z1.2.3.4/example.comr   úexample.comN)r   r   ÚassertEqualr   Úsecondariesr   r   r   r   Útest_secondary,   s     ÿzOptionsTests.test_secondaryc                 C   s.   t ƒ }| ddg¡ |  ddgfg|j¡ dS )z–
        An argument of the form C{"ip:port/domain"} can be used to specify an
        alternate port number for which to act as a secondary.
        r   ú1.2.3.4:5353/example.com)r   éé  r   N)r   r   r   r   r   r   r   r   Útest_secondaryExplicitPort8   s    
 ÿz'OptionsTests.test_secondaryExplicitPortc                 C   sž   t ƒ }| ddddg¡ |  t|jƒd¡ |jd }|  |jd t¡ |  |jd¡ |  |jd¡ |jd }|  |jd t¡ |  |jd	¡ |  |jd
¡ dS )zœ
        After parsing I{--secondary} options, L{Options} constructs a
        L{SecondaryAuthorityService} instance for each configured secondary.
        r   r   z1.2.3.5:5354/example.comé   r   r   r   é   z1.2.3.5iê  N)	r   r   r   ÚlenZsvcsÚassertIsInstancer   ZprimaryZ_port)r   r   Z	secondaryr   r   r   Útest_secondaryAuthorityServicesC   s     ÿ

z,OptionsTests.test_secondaryAuthorityServicesc                 C   sÀ   t ƒ }| dddg¡ t|ƒ\}}|D ],}t|tƒr&|jd }t|tƒr&|j ¡  q&t	 
¡ dkr¬ddlm} |jD ]8}trŽ|  |jjtj¡ n|  |jjtjj¡ | ¡  qr|  |d t¡ dS )	zÆ
        Recursive DNS lookups, if enabled, should be a last-resort option.
        Any other lookup method (cache, local lookup, etc.) should take
        precedence over recursive lookups
        z--hosts-filez	hosts.txtz--recursiveéÿÿÿÿÚposixr   )ÚreactorN)r   r   r   Ú
isinstancer   Z	resolversr   Z
_parseCallZcancelr
   ZgetTypeZtwisted.internetr&   Z_newTimedCallsr	   r   ÚfuncÚ__func__r   Z_cleanupr"   )r   r   ZcaZclÚxZrecurserr&   r   r   r   Útest_recursiveConfigurationV   s(    




ÿ
ÿ
z(OptionsTests.test_recursiveConfigurationN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r#   r+   r   r   r   r   r      s   r   N)r/   Ztwisted.internet.baser   Ztwisted.names.clientr   Ztwisted.names.dnsr   Ztwisted.names.resolver   Ztwisted.names.secondaryr   Ztwisted.names.tapr   r   Ztwisted.python.compatr	   Ztwisted.python.runtimer
   Ztwisted.python.usager   Ztwisted.trial.unittestr   r   r   r   r   r   Ú<module>   s   