ó
Ê£Mc           @   s„   d  Z  d d l m Z d d l Z d d l Z d d l m Z m Z d d l m	 Z	 d e f d „  ƒ  YZ
 e d k r€ e ƒ  n  d S(	   s!   
Unit tests for L{OpenSSL.rand}.
iÿÿÿÿ(   t   mainN(   t   TestCaset   b(   t   randt	   RandTestsc           B   s   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   sF   |  j  t t j ƒ |  j  t t j d ƒ |  j  t t j d d ƒ d S(   s   
        L{OpenSSL.rand.bytes} raises L{TypeError} if called with the wrong
        number of arguments or with a non-C{int} argument.
        i   N(   t   assertRaisest	   TypeErrorR   t   bytest   None(   t   self(    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_bytes_wrong_args   s    c         C   s¡   t  j d ƒ } |  j t | ƒ d ƒ t  j d d ƒ } |  j | | ƒ t  j d d ƒ } |  j t | ƒ d ƒ |  j t t  j d ƒ } |  j t | ƒ d ƒ d S(   s®   
        Verify that we can obtain bytes from rand_bytes() and
        that they are different each time.  Test the parameter
        of rand_bytes() for bad values.
        i2   t	   num_bytesi    iÿÿÿÿs   num_bytes must not be negativeN(   R   R   t   assertEqualt   lent   assertNotEqualR   t
   ValueErrort   str(   R	   t   b1t   b2t   b3t   exc(    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt
   test_bytes   s    c         C   sq   |  j  t t j ƒ |  j  t t j t d ƒ d ƒ |  j  t t j d d ƒ |  j  t t j t d ƒ d d ƒ d S(   s£   
        When called with the wrong number of arguments, or with arguments not of
        type C{str} and C{int}, L{OpenSSL.rand.add} raises L{TypeError}.
        t   fooi   N(   R   R   R   t   addR   R   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_add_wrong_args,   s    c         C   s   t  j t d ƒ d ƒ d S(   s?   
        L{OpenSSL.rand.add} adds entropy to the PRNG.
        t	   hamburgeri   N(   R   R   R   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_add7   s    c         C   sL   |  j  t t j ƒ |  j  t t j d ƒ |  j  t t j t d ƒ d ƒ d S(   s’   
        When called with the wrong number of arguments, or with a non-C{str}
        argument, L{OpenSSL.rand.seed} raises L{TypeError}.
        R   N(   R   R   R   t   seedR   R   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_seed_wrong_args>   s    c         C   s   t  j t d ƒ ƒ d S(   s@   
        L{OpenSSL.rand.seed} adds entropy to the PRNG.
        s
   milk shakeN(   R   R   R   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt	   test_seedH   s    c         C   s   |  j  t t j d ƒ d S(   sd   
        L{OpenSSL.rand.status} raises L{TypeError} when called with any
        arguments.
        N(   R   R   R   t   statusR   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_status_wrong_argsO   s    c         C   s   |  j  t j ƒ  d k ƒ d S(   sx   
        L{OpenSSL.rand.status} returns C{True} if the PRNG has sufficient
        entropy, C{False} otherwise.
        i   i   N(   i   i   (   t
   assertTrueR   R   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_statusW   s    c         C   s{   |  j  t t j ƒ |  j  t t j d ƒ |  j  t t j d d ƒ |  j  t t j d d ƒ |  j  t t j d d d ƒ d S(   s¡   
        L{OpenSSL.rand.egd} raises L{TypeError} when called with the wrong
        number of arguments or with arguments not of type C{str} and C{int}.
        R   i   N(   R   R   R   t   egdR   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_egd_wrong_argsb   s
    c         C   s?   t  j |  j ƒ  ƒ } d } |  j | | k d | | f ƒ d S(   sr   
        L{OpenSSL.rand.egd} returns C{0} or C{-1} if the EGD socket passed
        to it does not exist.
        iÿÿÿÿi    s   %r not in %rN(   iÿÿÿÿi    (   R   R"   t   mktempR    (   R	   t   resultt   expected(    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_egd_missingn   s
    	c         C   s   |  j  t t j d ƒ d S(   se   
        L{OpenSSL.rand.cleanup} raises L{TypeError} when called with any
        arguments.
        N(   R   R   R   t   cleanupR   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_cleanup_wrong_argsz   s    c         C   s   |  j  t j ƒ  d ƒ d S(   sk   
        L{OpenSSL.rand.cleanup} releases the memory used by the PRNG and returns
        C{None}.
        N(   t   assertIdenticalR   R(   R   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_cleanup‚   s    c         C   se   |  j  t t j ƒ |  j  t t j d d ƒ |  j  t t j d d ƒ |  j  t t j d d d ƒ d S(   s   
        L{OpenSSL.rand.load_file} raises L{TypeError} when called the wrong
        number of arguments or arguments not of type C{str} and C{int}.
        R   i   N(   R   R   R   t	   load_fileR   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_load_file_wrong_argsŠ   s    c         C   sF   |  j  t t j ƒ |  j  t t j d ƒ |  j  t t j d d ƒ d S(   s‘   
        L{OpenSSL.rand.write_file} raises L{TypeError} when called with the
        wrong number of arguments or a non-C{str} argument.
        R   N(   R   R   R   t
   write_fileR   (   R	   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_write_file_wrong_args•   s    c         C   sŽ   |  j  ƒ  } t | d ƒ } | j ƒ  zT t j | ƒ t j | ƒ t j } |  j | d ƒ t j	 | ƒ t j	 | d ƒ Wd t j
 | ƒ Xd S(   sG   
        Test reading and writing of files via rand functions.
        t   wi   i   N(   R$   t   opent   closeR   R.   t   ost   statt   ST_SIZEt   assertEqualsR,   t   unlink(   R	   t   tmpfilet   fObjt   size(    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt
   test_filesŸ   s    
(   t   __name__t
   __module__R
   R   R   R   R   R   R   R!   R#   R'   R)   R+   R-   R/   R;   (    (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyR      s   					
									
t   __main__(   t   __doc__t   unittestR    R3   R4   t   OpenSSL.test.utilR   R   t   OpenSSLR   R   R<   (    (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   <module>   s   ¥