ó
ê,Uc           @   sK  d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l	 Z	 d d l
 m Z d d l m Z d d l m Z y d d l Z Wn0 e k
 rÔ d e f d	 „  ƒ  YZ e ƒ  Z n Xd d
 l m Z m Z m Z e d ƒ j d ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e rAd Z n d Z d S(   s`   
Helpers for the OpenSSL test suite, largely copied from
U{Twisted<http://twistedmatrix.com/>}.
iÿÿÿÿN(   t   mktemp(   t   TestCase(   t   PY3(   t   exception_from_error_queue(   t   Errort   _memdbgc           B   s   e  Z d  Z RS(   N(   t   __name__t
   __module__t   Nonet   heap(    (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyR      s    (   t   ffit   libt   byte_strings   â˜ƒs   utf-8R   c           B   s¬   e  Z d  Z d „  Z d „  Z d „  Z d d „ Z d d „ Z e Z	 d d „ Z
 e
 Z d d „ Z e Z Z d d „ Z e Z Z d	 „  Z e Z d Z d
 „  Z d „  Z RS(   s–   
    :py:class:`TestCase` adds useful testing functionality beyond what is available
    from the standard library :py:class:`unittest.TestCase`.
    c         C   sä   t  t |  ƒ j } t j d  k r+ | | ƒ St t j ƒ } | | ƒ t j ƒ  t j	 t
 j ƒ t t j ƒ } | | s{ d  S| j ƒ  rà t t j ƒ } | | ƒ t j ƒ  t j	 t
 j ƒ t t j ƒ } |  j | | | ƒ n  d  S(   N(   t   superR   t   runt   memdbgR	   R   t   setR   t   CRYPTO_cleanup_all_ex_datat   ERR_remove_thread_stateR
   t   NULLt   wasSuccessfult   _reportLeaks(   t   selft   resultR   t   beforet   after(    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyR   (   s"    





c         C   s8  d „  } | r4i  } xu | D]m } t  j | d d } | | ƒ } | | k re | | f g | | <n | | j | | f ƒ t  j | ƒ q Wx¤ | j ƒ  D]“ \ } }	 g  }
 xC |	 D]; \ } } t t j d | ƒ ƒ } |
 j d | | f ƒ q³ Wd j t	 |
 ƒ ƒ } | j
 |  d  t | | f ƒ d  f ƒ qš Wn  d  S(   Nc         S   s  t  j |  } | d \ } } } t j | ƒ d  } t | ƒ } x$ | re d | d k re | j ƒ  qB Wx7 | rŸ d | d k rŸ d | d k rŸ | j d ƒ qi W| r³ | j ƒ  n | d  d  d … } | j g  | D] } | d ^ qÓ ƒ | j d d ƒ d j	 | ƒ S(	   Ni    iÿÿÿÿs   /__pycache__/_cffi__t   CRYPTO_malloct   CRYPTO_reallocs   
s   Leaked (%s) at:
t    (
   R   R	   t	   tracebackt   format_listt   listt   popt   reverset   extendt   insertt   join(   t   pt   stackst   sizet   python_stackt   c_stackt   stackt   savedt   frame(    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   format_leakK   s    #)$iÿÿÿÿi    t	   uintptr_ts   %d@0x%xs   , (   R   R	   t   appendt   freet	   iteritemst   intR
   t   castR$   t   sortedt   addErrorR   t	   Exception(   R   t   leaksR   R-   t   unique_leaksR%   R'   t   new_leakR*   t   allocst   allocs_accumt   pointert   addrt   allocs_report(    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyR   J   s&    	=c         C   sÒ   t  rp |  j d k	 rp xX |  j D]J } t j j | ƒ rG t j | ƒ q t j j | ƒ r t j	 | ƒ q q Wn  y t
 t ƒ WnM t k
 rÍ t j ƒ  d } | j g  f k rÎ |  j d t | ƒ ƒ qÎ n Xd S(   sÃ   
        Clean up any files or directories created using :py:meth:`TestCase.mktemp`.
        Subclasses must invoke this method if they override it or the
        cleanup will not occur.
        i   s)   Left over errors in OpenSSL error queue: N(   t   Falset   _temporaryFilesR   t   ost   patht   isdirt   shutilt   rmtreet   existst   unlinkR   R   t   syst   exc_infot   argst   failt   repr(   R   t   tempt   e(    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   tearDown    s    c         C   sO   t  | | ƒ sK | d k r$ d } n
 d | } |  j d | | | f ƒ n  d S(   s  
        Fail if C{instance} is not an instance of the given class or of
        one of the given classes.

        @param instance: the object to test the type (first argument of the
            C{isinstance} call).
        @type instance: any.
        @param classOrTuple: the class or classes to test against (second
            argument of the C{isinstance} call).
        @type classOrTuple: class, type, or tuple.

        @param message: Custom text to include in the exception text if the
            assertion fails.
        R   s   : s   %r is not an instance of %s%sN(   t
   isinstanceR   RK   (   R   t   instancet   classOrTuplet   messaget   suffix(    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   assertIsInstance´   s    	
	c         C   s2   | | k r. |  j  | p% d | | f ƒ ‚ n  | S(   s¼  
        Fail the test if :py:data:`containee` is not found in :py:data:`container`.

        :param containee: the value that should be in :py:class:`container`
        :param container: a sequence type, or in the case of a mapping type,
                          will follow semantics of 'if key in dict.keys()'
        :param msg: if msg is None, then the failure message will be
                    '%r not in %r' % (first, second)
        s   %r not in %r(   t   failureException(   R   t	   containeet	   containert   msg(    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   failUnlessInÌ   s    
c         C   s2   | | k r. |  j  | p% d | | f ƒ ‚ n  | S(   sŸ  
        Fail the test if C{containee} is found in C{container}.

        @param containee: the value that should not be in C{container}
        @param container: a sequence type, or in the case of a mapping type,
                          will follow semantics of 'if key in dict.keys()'
        @param msg: if msg is None, then the failure message will be
                    '%r in %r' % (first, second)
        s   %r in %r(   RV   (   R   RW   RX   RY   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   assertNotInÜ   s    
c         C   s2   | | k	 r. |  j  | p% d | | f ƒ ‚ n  | S(   s,  
        Fail the test if :py:data:`first` is not :py:data:`second`.  This is an
        obect-identity-equality test, not an object equality
        (i.e. :py:func:`__eq__`) test.

        :param msg: if msg is None, then the failure message will be
        '%r is not %r' % (first, second)
        s   %r is not %r(   RV   (   R   t   firstt   secondRY   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   assertIsí   s    	"c         C   s2   | | k r. |  j  | p% d | | f ƒ ‚ n  | S(   s$  
        Fail the test if :py:data:`first` is :py:data:`second`.  This is an
        obect-identity-equality test, not an object equality
        (i.e. :py:func:`__eq__`) test.

        :param msg: if msg is None, then the failure message will be
        '%r is %r' % (first, second)
        s   %r is %r(   RV   (   R   R\   R]   RY   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   assertIsNotü   s    	"c         O   s„   y | | | Ž  } WnN | k
 r7 t  j ƒ  d } | S|  j d t  j ƒ  d | j f ƒ ‚ n X|  j d | j | f ƒ ‚ d S(   sa  
        Fail the test unless calling the function :py:data:`f` with the given
        :py:data:`args` and :py:data:`kwargs` raises :py:data:`exception`. The
        failure will report the traceback and call stack of the unexpected
        exception.

        :param exception: exception type that is to be expected
        :param f: the function to call

        :return: The raised exception instance, if it is of the given type.
        :raise self.failureException: Raised if the function call does
            not raise an exception or if it raises an exception of a
            different type.
        i   s   %s raised instead of %si    s   %s not raised (%r returned)N(   RH   RI   RV   R   (   R   t	   exceptiont   fRJ   t   kwargsR   t   inst(    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   failUnlessRaises  s    		c         C   sD   |  j  d k r g  |  _  n  t t d d ƒ ƒ } |  j  j | ƒ | S(   sQ   
        Pathetic substitute for twisted.trial.unittest.TestCase.mktemp.
        t   dirt   .N(   R@   R   t   bR    R/   (   R   RM   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyR    +  s
    c         G   sO   |  j  | j | ƒ |  j t | t ƒ ƒ | | Œ  } |  j t | ƒ | ƒ d S(   s"  
        Perform various assertions about :py:data:`theType` to ensure that it is a
        well-defined type.  This is useful for extension types, where it's
        pretty easy to do something wacky.  If something about the type is
        unusual, an exception will be raised.

        :param theType: The type object about which to make assertions.
        :param name: A string giving the name of the type.
        :param constructionArgs: Positional arguments to use with :py:data:`theType` to
            create an instance of it.
        N(   t   assertEqualR   t
   assertTrueRP   t   typet   assertIdentical(   R   t   theTypet   namet   constructionArgsRQ   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   assertConsistentType7  s    N(   R   R   t   __doc__R   R   RO   R   RU   RZ   t   assertInR[   t   failIfInR^   Rk   t   failUnlessIdenticalR_   t   assertNotIdenticalt   failIfIdenticalRd   t   assertRaisesR@   R    Ro   (    (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyR   #   s$   	"	V	

		t   EqualityTestsMixinc           B   sz   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 RS(   sT   
    A mixin defining tests for the standard implementation of C{==} and C{!=}.
    c         C   s   t  ƒ  ‚ d S(   sº   
        Return an instance of the class under test.  Each call to this method
        must return a different object.  All objects returned must be equal to
        each other.
        N(   t   NotImplementedError(   R   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt
   anInstanceN  s    c         C   s   t  ƒ  ‚ d S(   s4  
        Return an instance of the class under test.  Each call to this method
        must return a different object.  The objects must not be equal to the
        objects returned by C{anInstance}.  They may or may not be equal to
        each other (they will not be compared against each other).
        N(   Rx   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   anotherInstanceW  s    c         C   s#   |  j  ƒ  } |  j | | k ƒ d S(   sN   
        An object compares equal to itself using the C{==} operator.
        N(   Ry   Ri   (   R   t   o(    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   test_identicalEqa  s    c         C   s#   |  j  ƒ  } |  j | | k ƒ d S(   sY   
        An object doesn't compare not equal to itself using the C{!=} operator.
        N(   Ry   t   assertFalse(   R   R{   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   test_identicalNei  s    c         C   s/   |  j  ƒ  } |  j  ƒ  } |  j | | k ƒ d S(   sx   
        Two objects that are equal to each other compare equal to each other
        using the C{==} operator.
        N(   Ry   Ri   (   R   t   aRg   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   test_sameEqq  s    c         C   s/   |  j  ƒ  } |  j  ƒ  } |  j | | k ƒ d S(   sƒ   
        Two objects that are equal to each other do not compare not equal to
        each other using the C{!=} operator.
        N(   Ry   R}   (   R   R   Rg   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   test_sameNe{  s    c         C   s/   |  j  ƒ  } |  j ƒ  } |  j | | k ƒ d S(   sƒ   
        Two objects that are not equal to each other do not compare equal to
        each other using the C{==} operator.
        N(   Ry   Rz   R}   (   R   R   Rg   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   test_differentEq…  s    c         C   s/   |  j  ƒ  } |  j ƒ  } |  j | | k ƒ d S(   s€   
        Two objects that are not equal to each other compare not equal to each
        other using the C{!=} operator.
        N(   Ry   Rz   Ri   (   R   R   Rg   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   test_differentNe  s    c         C   s,   |  j  ƒ  } t ƒ  } |  j | | k ƒ d S(   s¡   
        The object does not compare equal to an object of an unrelated type
        (which does not implement the comparison) using the C{==} operator.
        N(   Ry   t   objectR}   (   R   R   Rg   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   test_anotherTypeEq™  s    	c         C   s,   |  j  ƒ  } t ƒ  } |  j | | k ƒ d S(   s   
        The object compares not equal to an object of an unrelated type (which
        does not implement the comparison) using the C{!=} operator.
        N(   Ry   R„   Ri   (   R   R   Rg   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   test_anotherTypeNe£  s    	c         C   sH   d t  f d „  ƒ  Y} |  j ƒ  } | ƒ  } |  j | | k | g ƒ d S(   s„   
        The result of comparison using C{==} is delegated to the right-hand
        operand if it is of an unrelated type.
        t   Delegatec           B   s   e  Z d  „  Z RS(   c         S   s   |  g S(   N(    (   R   t   other(    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   __eq__³  s    (   R   R   R‰   (    (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyR‡   ²  s   N(   R„   Ry   Rh   (   R   R‡   R   Rg   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   test_delegatedEq­  s    	c         C   sH   d t  f d „  ƒ  Y} |  j ƒ  } | ƒ  } |  j | | k | g ƒ d S(   s„   
        The result of comparison using C{!=} is delegated to the right-hand
        operand if it is of an unrelated type.
        R‡   c           B   s   e  Z d  „  Z RS(   c         S   s   |  g S(   N(    (   R   Rˆ   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   __ne__Â  s    (   R   R   R‹   (    (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyR‡   Á  s   N(   R„   Ry   Rh   (   R   R‡   R   Rg   (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   test_delegateNe¼  s    	(   R   R   Rp   Ry   Rz   R|   R~   R€   R   R‚   Rƒ   R…   R†   RŠ   RŒ   (    (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyRw   J  s   			
			
	
	
	
	
	
	t   strt   unicode(   Rp   RD   R   RA   t   os.patht   tempfileR    t   unittestR   RH   t   sixR   t   OpenSSL._utilR   t   OpenSSL.cryptoR   R   R6   R„   R   R
   R   R   Rg   t   decodet	   NON_ASCIIRw   t   WARNING_TYPE_EXPECTED(    (    (    s5   /usr/lib/python2.7/dist-packages/OpenSSL/test/util.pyt   <module>   s,   ÿ (‚	