ó
âT€Mc           @   sØ   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z m Z d d l	 m
 Z
 d d l m Z d d d „  ƒ  YZ d	 e e j f d
 „  ƒ  YZ d e e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d S(   s'   
Tests for L{twisted.python.zipstream}
iÿÿÿÿN(   t   set(   t	   zipstreamt   filepath(   t   md5(   t   unittestt   FileEntryMixinc           B   sh   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   s?   
    File entry classes should behave as file-like objects
    c         C   s]   |  j  ƒ  } t j | d |  j ƒ } | j d | ƒ | j ƒ  t j | d ƒ } | j d ƒ S(   s6   
        Return an appropriate zip file entry
        t   wt   contentt   r(	   t   mktempt   zipfilet   ZipFilet   compressiont   writestrt   closeR   t   ChunkingZipFilet   readfile(   t   selft   contentst   filenamet   z(    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   getFileEntry   s    
c         C   s#   |  j  |  j d ƒ j ƒ  t ƒ d S(   sK   
        zip files should not be ttys, so isatty() should be false
        t    N(   t   assertEqualsR   t   isattyt   False(   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_isatty    s    c         C   sC   |  j  d ƒ } |  j | j t ƒ | j ƒ  |  j | j t ƒ d S(   sd   
        The C{closed} attribute should reflect whether C{close()} has been
        called.
        R   N(   R   R   t   closedR   R   t   True(   R   t	   fileEntry(    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_closed'   s    
c         C   sU   |  j  d ƒ } |  j | j ƒ  d ƒ |  j | j ƒ  d ƒ |  j | j ƒ  d ƒ d S(   sl   
        C{readline()} should mirror L{file.readline} and return up to a single
        deliminter.
        s   hoho
hos   hoho
t   hoR   N(   R   R   t   readline(   R   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_readline2   s    c         C   sR   |  j  d ƒ } |  j | j ƒ  d ƒ |  j | j ƒ  d ƒ |  j t | j ƒ d S(   sV   
        Zip file entries should implement the iterator protocol as files do.
        s   ho
hohos   ho
t   hohoN(   R   R   t   nextt   assertRaisest   StopIteration(   R   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt	   test_next=   s    c         C   s2   |  j  d ƒ } |  j | j ƒ  d d d g ƒ d S(   sG   
        C{readlines()} should return a list of all the lines.
        s   ho
ho
hos   ho
R   N(   R   R   t	   readlines(   R   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_readlinesG   s    c         C   s?   |  j  d ƒ } |  j t | ƒ | ƒ |  j | j ƒ  | ƒ d S(   sJ   
        C{__iter__()} and C{xreadlines()} should return C{self}.
        R   N(   R   t   assertIdenticalt   itert
   xreadlines(   R   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_iterationO   s    c         C   s/   d } |  j  | ƒ } |  j | j ƒ  | ƒ d S(   s9   
        C{.read()} should read the entire file.
        s   Hello, world!N(   R   R   t   read(   R   R   t   entry(    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_readWholeX   s    c         C   sW   d } |  j  | ƒ } | j d ƒ } | j d ƒ } |  j | d ƒ |  j | d ƒ d S(   sD   
        C{.read(num)} should read num bytes from the file.
        t
   0123456789i   iÈ   t   0123t   456789N(   R   R-   R   (   R   R   R.   t   onet   two(    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_readPartiala   s    c         C   sc   d d } |  j  | ƒ } | j d ƒ |  j | j ƒ  d ƒ | j d ƒ |  j | j ƒ  d ƒ d S(   sb   
        C{.tell()} should return the number of bytes that have been read so
        far.
        t   xid   i   i   i   N(   R   R-   R   t   tell(   R   R   R.   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt	   test_tellm   s    
(   t   __name__t
   __module__t   __doc__R   R   R   R!   R&   R(   R,   R/   R5   R8   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyR      s   					
						t   DeflatedZipFileEntryTestc           B   s   e  Z d  Z e j Z RS(   s2   
    DeflatedZipFileEntry should be file-like
    (   R9   R:   R;   R
   t   ZIP_DEFLATEDR   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyR<   {   s   t   ZipFileEntryTestc           B   s   e  Z d  Z e j Z RS(   s(   
   ZipFileEntry should be file-like
   (   R9   R:   R;   R
   t
   ZIP_STOREDR   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyR>   ƒ   s   t   ZipstreamTestc           B   sæ   e  Z d  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 e
 j d k  rx d Z e e _ e e	 _ n  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(   s,   
    Tests for twisted.python.zipstream
    c         C   sK   t  j |  j ƒ  ƒ |  _ |  j j ƒ  |  j j d ƒ |  _ |  j j ƒ  d S(   sz   
        Creates junk data that can be compressed and a test directory for any
        files that will be created
        t   unzippedN(   R   t   FilePathR	   t   testdirt   makedirst   childt   unzipdir(   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   setUp   s    R   c         C   s…   |  j  j d ƒ j } t j | d ƒ } xM t | ƒ D]? \ } } t | ƒ } | rc | d | } n  | j | | ƒ q4 W| j ƒ  | S(   s¤   
        Makes a zip file archive containing len(contents) files.  Contents
        should be a list of strings, each string being the content of one file.
        s   zipfile.zipR   t   /(	   RC   RE   t   pathR
   R   t	   enumeratet   strR   R   (   R   R   t	   directoryt
   zpfilenamet   zpfilet   iR   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   makeZipFileš   s    
c            sS   |  j  d d d d d g ƒ ‰  |  j t d t ‡  f d †  ƒ } |  j | d ƒ d	 S(
   s   
        Make sure the deprecated L{countZipFileEntries} returns the correct
        number of entries for a zip file.
        R3   R4   t   threet   fourt   fives"   countZipFileEntries is deprecated.c              s   t  j ˆ  ƒ S(   N(   R   t   countZipFileEntries(    (   t   name(    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   <lambda>²   s    i   N(   RP   t   assertWarnst   DeprecationWarningt   __file__R   (   R   t   result(    (   RU   sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_countEntriesª   s
    	c         C   s2   t  j |  j ƒ  d ƒ } |  j t | j d ƒ d S(   s€   
        A ChunkingZipFile opened in write-mode should not allow .readfile(),
        and raise a RuntimeError instead.
        R   t	   somethingN(   R   R   R	   R$   t   RuntimeErrorR   (   R   t   czf(    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_invalidMode·   s    c         C   sB   t  j |  j d g ƒ d ƒ } | j ƒ  |  j t | j d ƒ d S(   sn   
        A closed ChunkingZipFile should raise a L{RuntimeError} when
        .readfile() is invoked.
        R\   R   N(   R   R   RP   R   R$   R]   R   (   R   R^   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_closedArchiveÀ   s    
c         C   sÎ   |  j  d d g ƒ } t j | d ƒ } | j d ƒ j } | j ƒ  t | d ƒ } | j | d ƒ | j t	 d ƒ d ƒ | j ƒ  t
 j | ƒ } |  j t j | j d ƒ |  j | j d ƒ j ƒ  d ƒ d	 S(
   s¤   
        A zipfile entry with the wrong magic number should raise BadZipfile for
        readfile(), but that should not affect other files in the archive.
        s   test contentss   more contentsR   t   0s   r+bi    i   t   1N(   RP   R
   R   t   getinfot   header_offsetR   t   filet   seekt   writet   chrR   R   R$   t
   BadZipfileR   R   R-   (   R   t   fnt   zft
   zeroOffsett   scribbleR^   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_invalidHeaderÊ   s    	

c         C   sÓ   |  j  d d g ƒ } t j | d ƒ } | j d ƒ } d | _ | j ƒ  t | d ƒ } | j | j d ƒ | j	 | j
 ƒ  ƒ | j ƒ  t j | ƒ } |  j t j | j d ƒ |  j | j d ƒ j ƒ  d ƒ d	 S(
   sƒ   
        A zipfile entry with a different filename than is found in the central
        directory should raise BadZipfile.
        s   test contentss   more contentsR   Ra   s   not zeros   r+bi    Rb   N(   RP   R
   R   Rc   R   R   Re   Rf   Rd   Rg   t
   FileHeaderR   R   R$   Ri   R   R   R-   (   R   Rj   Rk   t   infoRm   R^   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_filenameMismatchÞ   s    		

i   i   s+   Consistency-checking only necessary in 2.5.c         C   s|   |  j  ƒ  } t j | d ƒ } t j d ƒ } | j | d ƒ d | _ | j ƒ  t j | ƒ } |  j	 t j
 | j d ƒ d S(   sq   
        A zipfile which describes an unsupported compression mechanism should
        raise BadZipfile.
        R   Ra   s	   some dataiÒ  N(   R	   R
   R   t   ZipInfoR   t   compress_typeR   R   R   R$   Ri   R   (   R   Rj   Rk   t   ziR^   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_unsupportedCompressioný   s    	
c         C   s‚   |  j  ƒ  } t j | d ƒ } t j d ƒ } d | _ | j | d ƒ | j ƒ  t j | ƒ } |  j	 | j
 d ƒ j ƒ  d ƒ d S(   sW   
        readfile() should skip over 'extra' data present in the zip metadata.
        R   Ra   s   hello, extras   the real dataN(   R	   R
   R   Rr   t   extraR   R   R   R   R   R   R-   (   R   Rj   Rk   Rt   R^   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_extraData  s    	
c         C   s  d } g  t  | ƒ D] } d | ^ q } |  j | ƒ } t j | |  j j ƒ } xW t  | ƒ D]I } |  j t t |  j j	 ƒ  ƒ ƒ | ƒ |  j | j
 ƒ  | | d ƒ q] W|  j t t |  j j	 ƒ  ƒ ƒ | ƒ xI |  j j	 ƒ  D]8 } t | j ƒ  ƒ } |  j | j ƒ  j ƒ  | | ƒ qß Wd S(   s¯   
        L{twisted.python.zipstream.unzipIter} should unzip a file for each
        iteration and yield the number of files left to unzip after that
        iteration
        i
   s   This is test file %d!i   N(   t   rangeRP   R   t	   unzipIterRF   RI   R   t   lent   listt   childrenR#   t   intt   basenamet   openR-   (   R   t   numfilesRO   R   RM   t   uziterRE   t   num(    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_unzipIter  s    #%"%c         C   sÒ   d } g  t  | ƒ D] } d | ^ q } |  j | ƒ } t t j | |  j j ƒ ƒ |  j t |  j j	 ƒ  ƒ t t
 t t  | ƒ ƒ ƒ ƒ xC |  j j ƒ  D]2 } t | j ƒ  ƒ } |  j | j ƒ  | | ƒ q˜ Wd S(   s˜   
        L{twisted.python.zipstream.unzipIterChunky} returns an iterator which
        must be exhausted to completely unzip the input archive.
        i
   s   This is test file %d!N(   Rx   RP   R{   R   t   unzipIterChunkyRF   RI   R   R    t   listdirt   mapRK   R|   R}   R~   t
   getContent(   R   R€   RO   R   RM   RE   R‚   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_unzipIterChunky2  s    #c         C   sç   d } g  t  | ƒ D] } d | ^ q } |  j | d ƒ } t t j | |  j j ƒ ƒ |  j t |  j j	 d ƒ j
 ƒ  ƒ t t t t  | ƒ ƒ ƒ ƒ xL |  j j	 d ƒ j ƒ  D]2 } t | j ƒ  ƒ } |  j | j ƒ  | | ƒ q­ Wd S(   sä   
        The path to which a file is extracted by L{zipstream.unzipIterChunky}
        is determined by joining the C{directory} argument to C{unzip} with the
        path within the archive of the file being extracted.
        i
   s   This is test file %d!t   fooN(   Rx   RP   R{   R   R„   RF   RI   R   R    RE   R…   R†   RK   R|   R}   R~   R‡   (   R   R€   RO   R   RM   RE   R‚   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_unzipIterChunkyDirectoryD  s    #c         C   sÇ   d } |  j  g  t | ƒ D] } t | ƒ ^ q ƒ } t j | |  j j ƒ |  j t |  j j	 ƒ  ƒ t t
 t t | ƒ ƒ ƒ ƒ xB t | ƒ D]4 } |  j |  j j t | ƒ ƒ j ƒ  t | ƒ ƒ q‹ Wd S(   sg   
        L{twisted.python.zipstream.unzip} should extract all files from a zip
        archive
        i   N(   RP   Rx   RK   R   t   unzipRF   RI   t   assertEqualR    R…   R†   RE   R‡   (   R   R€   RO   RM   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt
   test_unzipW  s    .c         C   sÜ   d } |  j  g  t | ƒ D] } t | ƒ ^ q d ƒ } t j | |  j j ƒ |  j t |  j j	 d ƒ j
 ƒ  ƒ t t t t | ƒ ƒ ƒ ƒ xK t | ƒ D]= } |  j |  j j	 d ƒ j	 t | ƒ ƒ j ƒ  t | ƒ ƒ q— Wd S(   sÚ   
        The path to which a file is extracted by L{zipstream.unzip} is
        determined by joining the C{directory} argument to C{unzip} with the
        path within the archive of the file being extracted.
        i   R‰   N(   RP   Rx   RK   R   R‹   RF   RI   RŒ   R    RE   R…   R†   R‡   (   R   R€   RO   RM   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_unzipDirectoryf  s    1c         C   s.  |  j  j d ƒ } |  j d g ƒ } | j d ƒ t j | |  j  j ƒ |  j | j ƒ  j	 ƒ  d ƒ t j | |  j  j d t
 ƒ|  j | j ƒ  j	 ƒ  d ƒ | j d ƒ t j | |  j  j ƒ } | j ƒ  |  j | j ƒ  j	 ƒ  d ƒ t j | |  j  j d t
 ƒ} | j ƒ  |  j | j ƒ  j	 ƒ  d ƒ d S(   s­   
        L{twisted.python.zipstream.unzip} and
        L{twisted.python.zipstream.unzipIter} shouldn't overwrite files unless
        the 'overwrite' flag is passed
        Ra   t   OVERWRITTENs   NOT OVERWRITTENt	   overwriteN(   RF   RE   RP   t
   setContentR   R‹   RI   R   R   R-   R   Ry   R#   (   R   t   testfileRM   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_overwritew  s    
	
c         C   s6  d j  g  t d ƒ D] } t t j ƒ  ƒ ^ q ƒ } t | ƒ j ƒ  } t j |  j ƒ  ƒ } | j	 ƒ  | j
 d ƒ j }	 |  j |	 | | ƒ t j |	 | j d | ƒ}
 |
 j ƒ  } | | k  oÈ | k  n } |  j | ƒ x |
 D] } qá W|  j | d ƒ t | j
 d ƒ j ƒ  j ƒ  ƒ j ƒ  } |  j | | ƒ d S(   sW   
        unzipIterChunky should unzip the given number of bytes per iteration.
        t    iè  s   bigfile.zipt	   chunksizei    t   zipstreamjunkN(   t   joint   xrangeRK   t   randomR   t	   hexdigestR   RB   R	   RD   RE   RI   t   _makebigfileR   R„   R#   t
   failUnlessRŒ   R   R-   (   R   R   R•   t   lowert   uppert   nt   junkt   junkmd5t   tempdirt   zfpathR   R   t   approxt   newmd5(    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   _unzipIterChunkyTest”  s"    4
	$c         C   s   |  j  t j d d d ƒ d S(   ss   
        unzipIterChunky should unzip the given number of bytes per iteration on
        a stored archive.
        iô  i#   i-   N(   R¦   R
   R?   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_unzipIterChunkyStored®  s    c         C   s   |  j  t j d d d ƒ d S(   su   
        unzipIterChunky should unzip the given number of bytes per iteration on
        a deflated archive.
        iÌ  i   i   N(   R¦   R
   R=   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   test_chunkyDeflated¶  s    c         C   sd   t  j | d | ƒ } x. t d ƒ D]  } d | } | j | d ƒ q" W| j d | ƒ | j ƒ  d S(   sT   
        Create a zip file with the given file name and compression scheme.
        R   i
   s   zipstream%dR   R–   N(   R
   R   Rx   R   R   (   R   R   R   R    Rk   RO   Rj   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyR›   ¾  s    
(   i   i   (   R9   R:   R;   RG   RP   R[   R_   R`   Rn   Rq   t   syst   version_infot   messaget   skipRu   Rw   Rƒ   Rˆ   RŠ   R   RŽ   R“   R¦   R§   R¨   R›   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyR@   ‹   s0   					
														(    (   R;   R©   R™   R
   t   twisted.python.compatR    t   twisted.pythonR   R   t   twisted.python.hashlibR   t   twisted.trialR   R   t   TestCaseR<   R>   R@   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt   <module>   s   k