ó
î;,Uc           @   s–   d  Z  d d l m Z d d l Z d d l Z d d l Z d d l m Z 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 :py:obj:`OpenSSL.rand`.
iÿÿÿÿ(   t   mainN(   t	   NON_ASCIIt   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 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¤   
        :py:obj:`OpenSSL.rand.bytes` raises :py:obj:`TypeError` if called with the wrong
        number of arguments or with a non-:py:obj:`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   |  j  t t j t j ƒ d S(   sˆ   
        :py:obj:`OpenSSL.rand.bytes` raises :py:obj:`MemoryError` if more bytes
        are requested than will fit in memory.
        N(   R   t   MemoryErrorR   R   t   syst   maxsize(   R
   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_insufficientMemory   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 :py:obj:`str` and :py:obj:`int`, :py:obj:`OpenSSL.rand.add` raises :py:obj:`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_args4   s    c         C   s   t  j t d ƒ d ƒ d S(   sF   
        :py:obj:`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_add?   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-:py:obj:`str`
        argument, :py:obj:`OpenSSL.rand.seed` raises :py:obj:`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_argsF   s    c         C   s   t  j t d ƒ ƒ d S(   sG   
        :py:obj:`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_seedP   s    c         C   s   |  j  t t j d ƒ d S(   sr   
        :py:obj:`OpenSSL.rand.status` raises :py:obj:`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_argsW   s    c         C   s   |  j  t j ƒ  d k ƒ d S(   s   
        :py:obj:`OpenSSL.rand.status` returns :py:obj:`True` if the PRNG has sufficient
        entropy, :py:obj:`False` otherwise.
        i   i   N(   i   i   (   t
   assertTrueR   R#   (   R
   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_status_   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½   
        :py:obj:`OpenSSL.rand.egd` raises :py:obj:`TypeError` when called with the wrong
        number of arguments or with arguments not of type :py:obj:`str` and :py:obj:`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_argsj   s
    c         C   s?   t  j |  j ƒ  ƒ } d } |  j | | k d | | f ƒ d S(   s‡   
        :py:obj:`OpenSSL.rand.egd` returns :py:obj:`0` or :py:obj:`-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_missingv   s
    	c         C   sB   t  j |  j ƒ  d ƒ } d } |  j | | k d | | f ƒ d S(   s¼   
        :py:obj:`OpenSSL.rand.egd` returns :py:obj:`0` or :py:obj:`-1` if the
        EGD socket passed to it does not exist even if a size argument is
        explicitly passed.
        i   iÿÿÿÿi    s   %r not in %rN(   iÿÿÿÿi    (   R   R'   R)   R%   (   R
   R*   R+   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_egd_missing_and_bytes‚   s
    	c         C   s   |  j  t t j d ƒ d S(   ss   
        :py:obj:`OpenSSL.rand.cleanup` raises :py:obj:`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_args   s    c         C   s   |  j  t j ƒ  d ƒ d S(   sy   
        :py:obj:`OpenSSL.rand.cleanup` releases the memory used by the PRNG and returns
        :py:obj:`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¹   
        :py:obj:`OpenSSL.rand.load_file` raises :py:obj:`TypeError` when called the wrong
        number of arguments or arguments not of type :py:obj:`str` and :py:obj:`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¦   
        :py:obj:`OpenSSL.rand.write_file` raises :py:obj:`TypeError` when called with the
        wrong number of arguments or a non-:py:obj:`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   t  | d ƒ  Wd QXzT t j | ƒ t j | ƒ t j } |  j d | ƒ t j | ƒ t j | d ƒ Wd t j | ƒ Xd S(   sU   
        Verify that ``rand.write_file`` and ``rand.load_file`` can be used.
        t   wNi   i   (	   t   openR   R4   t   ost   statt   ST_SIZER   R2   t   unlink(   R
   t   patht   size(    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   _read_write_test³   s    c         C   s6   |  j  ƒ  } | t j t j ƒ  ƒ 7} |  j | ƒ d S(   se   
        Random data can be saved and loaded to files with paths specified as
        bytes.
        N(   R)   R   t   encodeR   t   getfilesystemencodingR>   (   R
   R<   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_bytes_pathsË   s    c         C   s*   |  j  ƒ  j d ƒ t } |  j | ƒ d S(   sg   
        Random data can be saved and loaded to files with paths specified as
        unicode.
        s   utf-8N(   R)   t   decodeR   R>   (   R
   R<   (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   test_unicode_pathsÕ   s    (   t   __name__t
   __module__R   R   R   R   R   R!   R"   R$   R&   R(   R,   R-   R/   R1   R3   R5   R>   RA   RC   (    (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyR      s&   	
					
													
t   __main__(   t   __doc__t   unittestR    R8   R9   R   t   OpenSSL.test.utilR   R   R   t   OpenSSLR   R   RD   (    (    (    s:   /usr/lib/python2.7/dist-packages/OpenSSL/test/test_rand.pyt   <module>   s   Í