o
    b                     @   sj   d Z ddlmZ ddlmZ G dd dZG dd deejZG dd	 d	eejZ	G d
d deejZ
dS )z-
Test cases for L{twisted.python.randbytes}.
    )	randbytes)unittestc                   @      e Zd ZdZdd ZdS )SecureRandomTestCaseBasez1
    Base class for secureRandom test cases.
    c                 C   sP   t ddD ] }||}| t|| ||}| t|| | || qdS )a|  
        The given random bytes source should return the number of bytes
        requested each time it is called and should probably not return the
        same bytes on two consecutive calls (although this is a perfectly
        legitimate occurrence and rejecting it may generate a spurious failure
        -- maybe we'll get lucky and the heat death with come first).
              N)rangeassertEquallenassertNotEqual)selfsourcenbytesss2 r   =/usr/lib/python3/dist-packages/twisted/test/test_randbytes.py_check   s   zSecureRandomTestCaseBase._checkN)__name__
__module____qualname____doc__r   r   r   r   r   r          r   c                   @   r   )SecureRandomTestsz4
    Test secureRandom under normal conditions.
    c                 C      |  tj dS )z
        L{randbytes.secureRandom} should return a string of the requested
        length and make some effort to make its result otherwise unpredictable.
        N)r   r   secureRandomr   r   r   r   test_normal(   s   zSecureRandomTests.test_normalN)r   r   r   r   r   r   r   r   r   r   #   r   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ConditionalSecureRandomTestsz<
    Test random sources one by one, then remove it to.
    c                 C   s   t  | _dS )zJ
        Create a L{randbytes.RandomFactory} to use in the tests.
        N)r   RandomFactoryfactoryr   r   r   r   setUp7   s   z"ConditionalSecureRandomTests.setUpc                 C   s   t  )zL
        A factory raising an error when a source is not available.
        )r   SourceNotAvailable)r   r   r   r   r   errorFactory=   s   z)ConditionalSecureRandomTests.errorFactoryc                 C   s   |  | jj dS )zy
        L{RandomFactory._osUrandom} should work as a random source whenever
        L{os.urandom} is available.
        N)r   r    
_osUrandomr   r   r   r   test_osUrandomC   s   z+ConditionalSecureRandomTests.test_osUrandomc                    sN    j  j_ tj jjd  fdd} tdt	|} 
t|d dS )zt
        Remove all secure sources and assert it raises a failure. Then try the
        fallback parameter.
           c                      s    j jdddS )Nr&   T)fallback)r    r   r   r   r   r   wrapperT   s   zBConditionalSecureRandomTests.test_withoutAnything.<locals>.wrapperzPurandom unavailable - proceeding with non-cryptographically secure random sourceN)r#   r    r$   assertRaisesr   SecureRandomNotAvailabler   assertWarnsRuntimeWarning__file__r	   r
   )r   r(   r   r   r   r   test_withoutAnythingJ   s   
z1ConditionalSecureRandomTests.test_withoutAnythingN)r   r   r   r   r!   r#   r%   r.   r   r   r   r   r   0   s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )RandomBaseTestsz%
    'Normal' random test cases.
    c                 C   r   )z"
        Test basic case.
        N)r   r   insecureRandomr   r   r   r   r   f   s   zRandomBaseTests.test_normalc                 C   s   t  }d|_| |j dS )zG
        Test C{insecureRandom} without C{random.getrandbits}.
        N)r   r   getrandbitsr   r0   )r   r    r   r   r   test_withoutGetrandbitsl   s   z'RandomBaseTests.test_withoutGetrandbitsN)r   r   r   r   r   r2   r   r   r   r   r/   a   s    r/   N)r   twisted.pythonr   twisted.trialr   r   TestCaser   SynchronousTestCaser   r/   r   r   r   r   <module>   s   
1