ó
qxPXc           @   s  d  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 y d d l	 m
 Z
 Wn! e k
 r d d l m
 Z
 n Xd d l Z d d l Z e j d ƒ Z e j j d d ƒ Z d e j f d „  ƒ  YZ e j ƒ  d S(	   s   Test the various package hooks.iÿÿÿÿN(   t   StringIOs   %prog [options]t   APPORT_DATA_DIRs   /usr/share/apportt   Tc           B   sq   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e	 d „  ƒ Z
 d „  Z d	 „  Z d
 „  Z RS(   c         C   sG   t  j j |  _ t j ƒ  t  j _ t  j j t j d <t j ƒ  |  _ d  S(   Nt   APPORT_REPORT_DIR(	   t   apportt	   fileutilst
   report_dirt   orig_report_dirt   tempfilet   mkdtempt   ost   environt   workdir(   t   self(    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   setUp   s    c         C   s6   t  j t j j ƒ |  j t j _ t  j |  j ƒ d  S(   N(   t   shutilt   rmtreeR   R   R   R   R   (   R   (    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   tearDown"   s    c         C   sç   t  j d t d d g d t  j ƒ} | j d ƒ |  j | j d d ƒ t j j	 ƒ  } |  j t
 | ƒ d d	 ƒ t j ƒ  } | j t | d ƒ ƒ |  j | d
 d ƒ |  j | d d ƒ |  j | d d ƒ |  j | d d ƒ d S(   s#   package_hook without any log files.s   %s/package_hooks   -pt   basht   stdins   something is wrongi    s"   package_hook finished successfullyi   s   package_hook created a reportt   ProblemTypet   Packaget   SourcePackaget   ErrorMessageN(   t
   subprocesst   Popent   datadirt   PIPEt   communicatet   assertEqualt
   returncodeR   R   t   get_new_reportst   lent   Reportt   loadt   open(   R   t   pht   repst   r(    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   test_package_hook_nologs(   s    c         C   s  t  j j ƒ  } t j d t d | g d t j ƒ} | j d ƒ |  j | j	 d d ƒ t  j
 j ƒ  } |  j t | ƒ d d ƒ t  j ƒ  } | j t | d ƒ ƒ |  j | d	 d
 ƒ |  j | d
 | ƒ |  j | d t  j j | ƒ ƒ |  j | d d ƒ d S(   s?   package_hook on an uninstalled package (might fail to install).s   %s/package_hooks   -pR   s   something is wrongi    s"   package_hook finished successfullyi   s   package_hook created a reportR   R   R   R   N(   R   t	   packagingt   get_uninstalled_packageR   R   R   R   R   R   R   R   R   R    R!   R"   R#   t
   get_source(   R   t   pkgR$   R%   R&   (    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   test_package_hook_uninstalled;   s     c         C   ss  t  t j j |  j d ƒ d ƒ j d ƒ t  t j j |  j d ƒ d ƒ j d ƒ t j t j j |  j d ƒ ƒ t  t j j |  j d d ƒ d ƒ j d ƒ t j d	 t	 d
 d d t j j
 t j d ƒ d |  j g d t j ƒ} | j d ƒ |  j | j d d ƒ t j j ƒ  } |  j t | ƒ d d ƒ t j ƒ  } | j t  | d ƒ ƒ d } d } d } xx | j ƒ  D]j } | j d ƒ rŒ| } qn| j d ƒ r¤| } qn| j d ƒ r¼| } qnd | k rn|  j d ƒ qnqnW|  j | ƒ |  j | ƒ |  j | ƒ |  j d | | k ƒ |  j t | | ƒ t j j t j d ƒ ƒ |  j | | d ƒ |  j | | d ƒ d S(   s+   package_hook with a log dir and a log file.s	   log_1.logt   ws	   Log 1
blat   log2s   Yet
another
logt   logsubs	   notme.logs   not me!s   %s/package_hooks   -pR   s   -li    R   s   something is wrongs"   package_hook finished successfullyi   s   package_hook created a reportt   Testhookspyt   Log1logt   Log2t   subs#   logsub should not go into log filest	   0234lkjasN(   R#   R
   t   patht   joinR   t   writet   mkdirR   R   R   t   realpatht   syst   argvR   R   R   R   R   R   R   R    R!   R"   t   Nonet   keyst   endswitht   failt
   assertTruet   getsize(   R   R$   R%   R&   t   filekeyt   log1keyt   log2keyt   k(    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   test_package_hook_logsO   s@    ((+%			-c         C   sÐ  t  t j j t j j d ƒ d ƒ } | j d d ƒ | j ƒ  t  t j j t j j d ƒ d ƒ } | j d ƒ | j ƒ  |  j	 t
 j d t ƒ d d	 ƒ t j j ƒ  } |  j	 t | ƒ d
 d ƒ t j ƒ  } | j t  | d ƒ ƒ |  j	 t | j ƒ  ƒ t d d d d d d d d g ƒ ƒ |  j	 | d d ƒ |  j	 | d d ƒ |  j	 | d d d ƒ |  j d | d k ƒ |  j t j ƒ  d j d ƒ d | d k ƒ | j | d ƒ |  j d | d k ƒ d S(   s   kernel_crashdump.t   vmcoreR-   s   id   s
   vmcore.logs   vmcore successfully dumpeds   %s/kernel_crashdumpi    s&   kernel_crashdump finished successfullyi   s!   kernel_crashdump created a reportt   DateR   R   t   VmCoret	   VmCoreLogt   Unamet   Architecturet   DistroReleaset   KernelCrasht   linuxi   t   -t    N(   R#   R
   R5   R6   R   R   R   R7   t   closeR   R   t   callR   R   R    R!   R"   t   setR=   R@   t   unamet   splitt   add_package_info(   R   t   fR%   R&   (    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   test_kernel_crashdumpx   s*    $
$
!.c         C   sª   t  t j j t j j d ƒ d ƒ } | j d d ƒ | j ƒ  t j	 d t j j t j j d ƒ ƒ |  j
 t j d t d t j ƒd d	 ƒ |  j t j j ƒ  g  ƒ d
 S(   sÁ   attempted DoS with vmcore.log symlink

        We must only accept plain files, otherwise vmcore.log might be a
        symlink to the .crash file, which would recursively fill itself.
        RG   t   wbs   id   s
   vmcore.logs   %s/kernel_crashdumpt   stderri    s'   kernel_crashdump unexpectedly succeededN(   R#   R
   R5   R6   R   R   R   R7   RR   t   symlinkt   assertNotEqualR   RS   R   R   R   R   (   R   RX   (    (    s)   /usr/share/apport/testsuite/test_hooks.pyt!   test_kernel_crashdump_log_symlink—   s    $
%	
c         C   sÁ   t  j d d g d t  j ƒ} | j ƒ  d } | j d k sI t d ƒ ‚ d j | j ƒ  d j ƒ  d j d ƒ d  ƒ } d | } t  j	 | d g d t  j ƒd k s· t | d	 ƒ ‚ | | f S(
   sV   Determine a valid version and executable path of gcc and return it
        as a tuple.t   gccs	   --versiont   stdouti    s-   "gcc --version" must work for this test suitet   .i   s   /usr/bin/gcc-s(    must exist and work for this test suite(
   R   R   R   R   R   t   AssertionErrorR6   t
   splitlinesRV   RS   (   t   klassR_   t   outt   gcc_vert   gcc_path(    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   _gcc_version_path¨   s    0
'c         C   sE  |  j  ƒ  \ } } t j ƒ  } | j d ƒ | j ƒ  | j d ƒ |  j t j d t	 | | j
 g ƒ d d ƒ t j j ƒ  } |  j t | ƒ d d ƒ t j ƒ  } | j t | d ƒ ƒ |  j | d d ƒ |  j | d	 | ƒ |  j | d
 | j ƒ  ƒ | j ƒ  |  j | d j ƒ  d d | ƒ |  j | d j d ƒ ƒ d S(   s#   gcc_ice_hook with a temporary file.s   int f(int x);i    s   %s/gcc_ice_hooks"   gcc_ice_hook finished successfullyi   s   gcc_ice_hook created a reportR   t   Crasht   ExecutablePatht   PreprocessedSourceR   s   gcc-R   R_   N(   Rh   R   t   NamedTemporaryFileR7   t   flusht   seekR   R   RS   R   t   nameR   R   R   R    R!   R"   R#   t   readRW   RV   R@   t
   startswith(   R   t   gcc_versionRg   t   test_sourceR%   R&   (    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   test_gcc_ide_hook_file¹   s"    

"c         C   s1  |  j  ƒ  \ } } d } t j d t | d g d t j ƒ} | j | ƒ |  j | j d d ƒ t j	 j
 ƒ  } |  j t | ƒ d d ƒ t j ƒ  } | j t | d ƒ ƒ |  j | d	 d
 ƒ |  j | d | ƒ |  j | d | ƒ | j ƒ  |  j | d j ƒ  d d | ƒ |  j | d j d ƒ ƒ d S(   s   gcc_ice_hook with piping.s   int f(int x);s   %s/gcc_ice_hookRP   R   i    s"   gcc_ice_hook finished successfullyi   s   gcc_ice_hook created a reportR   Ri   Rj   Rk   R   s   gcc-R   R_   N(   Rh   R   R   R   R   R   R   R   R   R   R   R    R!   R"   R#   RW   RV   R@   Rq   (   R   Rr   Rg   Rs   t   hookR%   R&   (    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   test_gcc_ide_hook_pipeÕ   s     
"c         C   sê   d } t  j d t g d t  j ƒ} | j | ƒ |  j | j d d ƒ t j j	 ƒ  } |  j t
 | ƒ d d ƒ t j ƒ  } | j t | d ƒ ƒ |  j | d d	 ƒ |  j | d
 | ƒ | j | d ƒ |  j | d j d ƒ ƒ d  S(   Ns`  ------------[ cut here ]------------
kernel BUG at /tmp/oops.c:5!
invalid opcode: 0000 [#1] SMP
Modules linked in: oops cpufreq_stats ext2 i915 drm nf_conntrack_ipv4 ipt_REJECT iptable_filter ip_tables nf_conntrack_ipv6 xt_state nf_conntrack xt_tcpudp ip6t_ipv6header ip6t_REJECT ip6table_filter ip6_tables x_tables ipv6 loop dm_multipath rtc_cmos iTCO_wdt iTCO_vendor_support pcspkr i2c_i801 i2c_core battery video ac output power_supply button sg joydev usb_storage dm_snapshot dm_zero dm_mirror dm_mod ahci pata_acpi ata_generic ata_piix libata sd_mod scsi_mod ext3 jbd mbcache uhci_hcd ohci_hcd ehci_hcd
s   %s/kernel_oopsR   i    s!   kernel_oops finished successfullyi   s   kernel_oops created a reportR   t
   KernelOopst   OopsTextR   s   linux-image-(   R   R   R   R   R   R   R   R   R   R   R    R!   R"   R#   RW   R@   Rq   (   R   Rs   Ru   R%   R&   (    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   test_kernel_oops_hookð   s    (   t   __name__t
   __module__R   R   R'   R,   RF   RY   R^   t   classmethodRh   Rt   Rv   Ry   (    (    (    s)   /usr/share/apport/testsuite/test_hooks.pyR      s   					)				(   t   __doc__t   unittestR   R   R
   R   t   os.pathR:   t   optparset	   cStringIOR    t   ImportErrort   ioR   t   apport.fileutilst   OptionParsert	   optparserR   t   getR   t   TestCaseR   t   main(    (    (    s)   /usr/share/apport/testsuite/test_hooks.pyt   <module>   s   `î