ó
qxPXc           @   s×   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z e j	 ƒ  Z
 e
 e j d <e j e j e
 ƒ d  d l Z d  d l Z d e  j f d „  ƒ  YZ e  j ƒ  d S(   iÿÿÿÿNt   APPORT_REPORT_DIRt   Tc           B   sh   e  Z d  „  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   c         C   s+   x$ t  j j ƒ  D] } t j | ƒ q Wd  S(   N(   t   apportt	   fileutilst   get_all_reportst   ost   unlink(   t   selft   f(    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt   tearDown   s    t    c         C   s  | r+ | } t  j | t  j t  j Bƒ } n t j d d ƒ \ } } zÃ t  j | d | ƒ t  j | ƒ t  j | d ƒ t	 j
 | d d g d t	 j d t	 j d	 t  j ƒ} | j ƒ  d
 } |  j | j d
 d ƒ | sï |  j d | k | ƒ n  |  j d | k | ƒ Wd t  j | ƒ X| S(   s   Create a test crash.t   dirs   /var/tmps­   #!/usr/bin/python
import apport_python_hook
apport_python_hook.install()

def func(x):
    raise Exception(b'This should happen. \xe2\x99\xa5'.decode('UTF-8'))

%s
func(42)
ií  t   testarg1t   testarg2t   stdoutt   stderrt   envi   s4   crashing test python program exits with failure codes   This should happen.t   OSErrorN(   R   t   opent   O_CREATt   O_WRONLYt   tempfilet   mkstempt   writet   closet   chmodt
   subprocesst   Popent   PIPEt   environt   communicatet   assertEqualt
   returncodet
   assertTruet   assertFalseR   (   R   t	   extracodet
   scriptnamet   scriptt   fdt   pt   err(    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt   _test_crash   s&    		c         C   sZ  |  j  ƒ  } t j j ƒ  } d } |  j t | ƒ d d ƒ |  j t j t	 j | d ƒ j
 ƒ d d ƒ t j ƒ  } | j t | d ƒ ƒ d d d d	 d
 d d d d d d g } |  j t | ƒ j t | j ƒ  ƒ ƒ d ƒ |  j d | d k ƒ |  j | d | ƒ |  j | d d | ƒ |  j | d j d ƒ ƒ |  j d | d k | d ƒ d S(   s+   general operation of the Python crash hook.i   s(   crashed Python program produced a reporti    i   s   report has correct permissionst   InterpreterPatht
   PythonArgst	   Tracebackt   ProblemTypet   ProcEnviront
   ProcStatust   ProcCmdlinet   Datet   ExecutablePatht   ProcMapst
   UserGroupss   report has necessary fieldss
   bin/pythons   ['%s', 'testarg1', 'testarg2']s.   func
    raise Exception(b'This should happen.N(   R)   R   R   t   get_new_reportst   NoneR   t   lent   statt   S_IMODER   t   st_modet   problem_reportt   ProblemReportt   loadR   R!   t   sett   issubsett   keyst
   startswith(   R   R%   t   reportst   prt   expected_keys(    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt   test_general@   s(    "
	$	c         C   s  |  j  ƒ  } t j j ƒ  } |  j t | ƒ d d ƒ |  j t j t j | d ƒ j	 ƒ d d ƒ t j j
 | d ƒ t j j ƒ  } |  j t | ƒ d ƒ |  j  d | ƒ } t j j ƒ  } |  j t | ƒ d ƒ |  j  d | ƒ } t j j ƒ  } |  j t | ƒ d ƒ d S(   s1   Python crash hook overwrites seen existing files.i   s(   crashed Python program produced a reporti    i   s   report has correct permissionsR$   N(   R)   R   R   R5   R   R7   R8   R9   R   R:   t   mark_report_seen(   R   R%   RB   (    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt   test_existing]   s    "
c      
   C   s  |  j  d ƒ t j j ƒ  } d } |  j t | ƒ d d ƒ |  j t j t	 j | d ƒ j
 ƒ d d ƒ t j ƒ  } | j t | d ƒ ƒ d d d	 d
 d d d d d d g
 } |  j t | ƒ j t | j ƒ  ƒ ƒ d ƒ |  j d | d k ƒ |  j | d j d ƒ ƒ d S(   s   with zapped sys.argv.s   import sys
sys.argv = Nonei   s(   crashed Python program produced a reporti    i   s   report has correct permissionsR*   R,   R-   R.   R/   R0   R1   R2   R3   R4   s   report has necessary fieldss
   bin/pythonN(   R)   R   R   R5   R6   R   R7   R8   R9   R   R:   R;   R<   R=   R   R!   R>   R?   R@   RA   (   R   RB   RC   RD   (    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt   test_no_argvw   s     "
	$c         C   s6   t  j j ƒ  } |  j t | ƒ d d t j ƒ  ƒ d S(   s'   Assert that there are no crash reports.i    s"   no crash reports present (cwd: %s)N(   R   R   R5   R   R7   R   t   getcwd(   R   RB   (    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt   _assert_no_reports   s    c      	   C   sÈ   t  j ƒ  } z§ x  d D]˜ } t  j | ƒ t j d g d t j d t j d t j ƒ} | j d ƒ \ } } | j d	 k s€ t ‚ | d
 k s’ t ‚ d | k s¤ t ‚ |  j	 ƒ  q WWd t  j | ƒ Xd S(   s4   interactive Python sessions never generate a report.s   /tmps
   /usr/locals   /usrt   pythont   stdinR   R   s   raise ValueErrori    R
   t
   ValueErrorN(   s   /tmps
   /usr/locals   /usr(
   R   RI   t   chdirR   R   R   R   R    t   AssertionErrorRJ   (   R   t   orig_cwdt   dR'   t   outR(   (    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt   test_interactive—   s    c   	      C   s  t  j d t j j ƒ \ } } t j j t j j	 ƒ } d } zì t j | d ƒ t j | ƒ t j | d ƒ t j j | ƒ r› | d } t j | | ƒ n  t j j ƒ  } | | d <| j ƒ  d } t j | d d g d t j d	 t j ƒ} | j ƒ  d
 } |  j | j d
 d ƒ |  j d | k ƒ Wd t j | ƒ t j j | ƒ rZt j | ƒ n  | rst j | | ƒ n  Xt j j ƒ  } |  j t | ƒ d ƒ d S(   s/   the Python crash hook respects the ignore list.R   sŒ   #!/usr/bin/python
import apport_python_hook
apport_python_hook.install()

def func(x):
    raise Exception, 'This should happen.'

func(42)
ií  s   .apporttestR2   R   R   R   R   i   s4   crashing test python program exits with failure codes   Exception: This should happen.Ni    (   R   R   R   R   t
   report_dirR   t   patht
   expandusert   reportt   _ignore_fileR6   R   R   R   t   existst   renamet   Reportt   mark_ignoreR   R   R   R   R   R    R!   R   R5   R7   (	   R   R&   R%   t   ifpatht   orig_ignore_filet   rR'   R(   RB   (    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt   test_ignoring¨   s8    	


c         C   s£   d } d } xp | | k  r~ |  j  d d ƒ t j j ƒ  } | sD Pn  |  j t | ƒ d d ƒ t j j | d ƒ | d 7} q W|  j | d ƒ |  j | | ƒ d S(   s   limit successive reportsi    i   R$   s   /var/tmp/pytestcrashi   s*   crashed Python program produced one reportN(	   R)   R   R   R5   R   R7   RF   t   assertGreatert
   assertLess(   R   t   countt   limitRB   (    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt   test_no_floodingÛ   s    c         C   s˜   t  j ƒ  j d d ƒ } |  j | d ƒ z* t j d d d g ƒ |  j d d ƒ Wd t j d d	 d g ƒ Xt j	 j
 ƒ  } |  j t | ƒ d
 d ƒ d S(   s)   DBus.Error.NoReply with a running services   org.gtk.vfs.Metadatas   /org/gtk/vfs/metadatat   killalls   -STOPs   gvfsd-metadataR#   s²   import dbus
obj = dbus.SessionBus().get_object('org.gtk.vfs.Metadata', '/org/gtk/vfs/metadata')
assert obj
i = dbus.Interface(obj, 'org.freedesktop.DBus.Peer')
i.Ping(timeout=1)
Ns   -CONTi    s>   NoReply is an useless exception and should not create a report(   t   dbust
   SessionBust
   get_objectt   assertNotEqualR6   R   t   callR)   R   R   R5   R   R7   (   R   t   metadata_objRB   (    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt!   test_dbus_service_timeout_runningì   s    	N(   t   __name__t
   __module__R	   R6   R)   RE   RG   RH   RJ   RS   R`   Re   Rm   (    (    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyR      s   	%						3	(   t   unittestR   R   R   R8   t   syst   shutilt   atexitRg   t   mkdtempt   temp_report_dirR   t   registert   rmtreet   apport.fileutilsR   R;   t   TestCaseR   t   main(    (    (    s2   /usr/share/apport/testsuite/test_python_crashes.pyt   <module>   s   `í