
    Vh//                         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
  G d d      Z G d d	ee
j                        Z G d
 dee
j                        Z G d de
j                        Zy)z'
Tests for L{twisted.python.zipstream}
    N)md5)filepath	zipstream)unittestc                   L    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y)FileEntryMixinz?
    File entry classes should behave as file-like objects
    c                     | j                         }t        j                  |d| j                        5 }|j	                  d|       ddd       t        j                  |d      }|j                  d      S # 1 sw Y   0xY w)z6
        Return an appropriate zip file entry
        wcontentNr)mktempzipfileZipFilecompressionwritestrr   ChunkingZipFilereadfile)selfcontentsfilenamezs       R/home/dcms/DCMS/lib/python3.12/site-packages/twisted/python/test/test_zipstream.pygetFileEntryzFileEntryMixin.getFileEntry   sk     ;;=__XsD,<,<= 	,JJy(+	,%%h4zz)$$	, 	,s   A44A=c                     | j                  d      5 }| j                  |j                                ddd       y# 1 sw Y   yxY w)zK
        zip files should not be ttys, so isatty() should be false
         N)r   assertFalseisattyr   	fileEntrys     r   test_isattyzFileEntryMixin.test_isatty    s@     r" 	1iY--/0	1 	1 	1s	    ;Ac                     | j                  d      5 }| j                  |j                         ddd       | j                  j                         y# 1 sw Y   %xY w)zd
        The C{closed} attribute should reflect whether C{close()} has been
        called.
        r   N)r   r   closed
assertTruer   s     r   test_closedzFileEntryMixin.test_closed'   sO    
 r" 	/iY--.	/	(()	/ 	/s   AAc                    | j                  d      5 }| j                  |j                         d       | j                  |j                         d       | j                  |j                         d       ddd       y# 1 sw Y   yxY w)zk
        C{readline()} should mirror L{file.readline} and return up to a single
        delimiter.
        s   hoho
hos   hoho
   ho    N)r   assertEqualreadliner   s     r   test_readlinezFileEntryMixin.test_readline0   st    
 {+ 	8yY//19=Y//159Y//137	8 	8 	8   A!A<<Bc                    | j                  d      5 }| j                  |j                         d       | j                  |j                         d       | j                  t        |j                         ddd       y# 1 sw Y   yxY w)zV
        Zip file entries should implement the iterator protocol as files do.
        s   ho
hoho   ho
s   hohoN)r   r(   nextassertRaisesStopIterationr   s     r   	test_nextzFileEntryMixin.test_next:   sk     {+ 	=yY^^-w7Y^^-w7mY^^<	= 	= 	=r+   c                     | j                  d      5 }| j                  |j                         g d       ddd       y# 1 sw Y   yxY w)zG
        C{readlines()} should return a list of all the lines.
        s   ho
ho
ho)r-   r-   r&   N)r   r(   	readlinesr   s     r   test_readlineszFileEntryMixin.test_readlinesC   sG     }- 	OY0024MN	O 	O 	Os	   #>Ac                     | j                  d      5 }| j                  t        |      |       | j                  |j                         |       ddd       y# 1 sw Y   yxY w)zJ
        C{__iter__()} and C{xreadlines()} should return C{self}.
        r   N)r   assertIsiter
xreadlinesr   s     r   test_iterationzFileEntryMixin.test_iterationJ   sR     r" 	=iMM$y/95MM)..0)<	= 	= 	=s   <AA c                     d}| j                  |      5 }| j                  |j                         |       ddd       y# 1 sw Y   yxY w)z9
        C{.read()} should read the entire file.
        s   Hello, world!N)r   r(   readr   r   entrys      r   test_readWholezFileEntryMixin.test_readWholeR   sD     $x( 	5EUZZ\84	5 	5 	5s	   !>Ac                     d}| j                  |      5 }|j                  d      }|j                  d      }ddd       | j                  d       | j                  d       y# 1 sw Y   .xY w)zD
        C{.read(num)} should read num bytes from the file.
        
0123456789      Ns   0123s   456789)r   r;   r(   )r   r   r=   onetwos        r   test_readPartialzFileEntryMixin.test_readPartialZ   sj      x( 	"E**Q-C**S/C	" 	g&i(		" 	"s   #A$$A-c                    d}| j                  |      5 }|j                  d       | j                  |j                         d       |j                  d       | j                  |j                         d       ddd       y# 1 sw Y   yxY w)zb
        C{.tell()} should return the number of bytes that have been read so
        far.
        dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   rA      N)r   r;   r(   tellr<   s      r   	test_tellzFileEntryMixin.test_telle   sp    
 x( 	.EJJqMUZZ\1-JJqMUZZ\1-		. 	. 	.s   A#B  B	N)__name__
__module____qualname____doc__r   r    r$   r*   r1   r4   r9   r>   rE   rK    r'   r   r   r      s:    %1*8=O=5	)
.r'   r   c                   (    e Zd ZdZej
                  Zy)DeflatedZipFileEntryTestsz2
    DeflatedZipFileEntry should be file-like
    N)rL   rM   rN   rO   r   ZIP_DEFLATEDr   rP   r'   r   rR   rR   r   s     &&Kr'   rR   c                   (    e Zd ZdZej
                  Zy)ZipFileEntryTestsz*
    ZipFileEntry should be file-like
    N)rL   rM   rN   rO   r   
ZIP_STOREDr   rP   r'   r   rU   rU   z   s     $$Kr'   rU   c                   f    e Zd ZdZd Zd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y)ZipstreamTestsz,
    Tests for twisted.python.zipstream
    c                     t        j                  | j                               | _        | j                  j	                          | j                  j                  d      | _        | j                  j	                          y)zz
        Creates junk data that can be compressed and a test directory for any
        files that will be created
        unzippedN)r   FilePathr   testdirmakedirschildunzipdirr   s    r   setUpzZipstreamTests.setUp   sR    
  ((7**:6 r'   c                    | j                   j                  d      j                  }t        j                  |d      5 }t        |      D ],  \  }}t        |      }|r|dz   |z   }|j                  ||       . 	 ddd       |S # 1 sw Y   |S xY w)z
        Makes a zip file archive containing len(contents) files.  Contents
        should be a list of strings, each string being the content of one file.
        zzipfile.zipr
   /N)r\   r^   pathr   r   	enumeratestrr   )r   r   	directory
zpfilenamezpfileir   r   s           r   makeZipFilezZipstreamTests.makeZipFile   s    
 \\''6;;
__Z- 	3'1 3
7q6(39H'2	3	3 	3 s   ;BBc                     t        j                  | j                         d      5 }| j                  t        |j
                  d       ddd       y# 1 sw Y   yxY w)z
        A ChunkingZipFile opened in write-mode should not allow .readfile(),
        and raise a RuntimeError instead.
        r
   	somethingN)r   r   r   r/   RuntimeErrorr   r   czfs     r   test_invalidModezZipstreamTests.test_invalidMode   sK    
 &&t{{}c: 	GclCLL+F	G 	G 	Gs   "AAc                     t        j                  | j                  dg      d      }|j                          | j	                  t
        |j                  d       y)zn
        A closed ChunkingZipFile should raise a L{RuntimeError} when
        .readfile() is invoked.
        rm   r   N)r   r   rk   closer/   rn   r   ro   s     r   test_closedArchivez!ZipstreamTests.test_closedArchive   sB    
 ''(8(8+(GM		,kBr'   c                 t   | j                  ddg      }t        j                  |d      5 }|j                  d      j                  }ddd       t        |d      5 }|j                  d       |j                  d       ddd       t        j                  |      5 }| j                  t        j                  |j                  d       |j                  d	      5 }| j                  |j                         d
       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   *xY w# 1 sw Y   yxY w)z
        A zipfile entry with the wrong magic number should raise BadZipFile for
        readfile(), but that should not affect other files in the archive.
        ztest contentszmore contentsr   0Nr+br   s   00001   more contents)rk   r   r   getinfoheader_offsetopenseekwriter   r   r/   
BadZipFiler   r(   r;   )r   fnzf
zeroOffsetscribblerp   zfes          r   test_invalidHeaderz!ZipstreamTests.test_invalidHeader   s   
 @A__R% 	7C66J	7 "e_ 	%MM*a(NN8$	% &&r* 	?cg00#,,Dc" ?c  -=>?	? 	?	7 	7	% 	%
? ?	? 	?s;   D
$D=D.!D"9D.
DD"D+	'D..D7c                    | j                  ddg      }t        j                  |d      5 }|j                  d      }d|_        ddd       t        |d      5 }|j                  j                  d       |j                  |j                                ddd       t        j                  |      5 }| j                  t        j                  |j                  d       |j                  d	      5 }| j                  |j!                         d       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   *xY w# 1 sw Y   yxY w)
z
        A zipfile entry with a different filename than is found in the central
        directory should raise BadZipFile.
        s   test contentsry   r   rv   znot zeroNrw   r   rx   )rk   r   r   rz   r   r|   r}   r{   r~   
FileHeaderr   r   r/   r   r   r(   r;   )r   r   r   infor   rp   r   s          r   test_filenameMismatchz$ZipstreamTests.test_filenameMismatch   s   
 /1ABC__R% 	'::c?D&DM	' "e_ 	.MM$,,a0NN4??,-	. &&r* 	?cg00#,,Dc" ?c  -=>?	? 	?	' 	'	. 	.? ?	? 	?s;   D<D+0=E-!D7ED(+D47E 	<EEc                    | j                         }t        j                  |d      5 }t        j                  d      }|j	                  |d       d|_        ddd       t        j                  |      5 }| j                  t        j                  |j                  d       ddd       y# 1 sw Y   SxY w# 1 sw Y   yxY w)zq
        A zipfile which describes an unsupported compression mechanism should
        raise BadZipFile.
        r
   rv   z	some datai  N)r   r   r   ZipInfor   compress_typer   r   r/   r   r   )r   r   r   zirp   s        r   test_unsupportedCompressionz*ZipstreamTests.test_unsupportedCompression   s    
 [[]__R% 	$%BKKK(  $B	$ &&r* 	Ecg00#,,D	E 	E	$ 	$	E 	Es   /B(3,B4(B14B=c                    | j                         }t        j                  |d      5 }t        j                  d      }d}t	        j
                  ddt        |            |z   |_        |j                  |d       ddd       t        j                  |      5 }|j                  d      5 }| j                  |j                         d       ddd       ddd       y# 1 sw Y   bxY w# 1 sw Y   xY w# 1 sw Y   yxY w)zW
        readfile() should skip over 'extra' data present in the zip metadata.
        r
   rv   s   hello, extraz<hh*   s   the real dataN)r   r   r   r   structpacklenextrar   r   r   r   r(   r;   )r   r   r   r   
extra_datarp   r   s          r   test_extraDatazZipstreamTests.test_extraData   s     [[]__R% 	.%B(J{{5"c*o>KBHKK,-		.
 &&r* 	;c3<<3D 	;SXXZ)9:	; 	; 	;	. 	.
	; 	; 	; 	;s0   ACC2(!C&	C2C#&C/	+C22C;c                 r   d}t        |      D cg c]  }d|z  	 }}|D cg c]  }|j                  d       }}| j                  |      }t        t	        j
                  || j                  j                               | j                  t        | j                  j                               t        t        t        t        |                         | j                  j                         D ]>  }t        |j                               }| j                  |j!                         ||          @ yc c}w c c}w )z
        L{twisted.python.zipstream.unzipIterChunky} returns an iterator which
        must be exhausted to completely unzip the input archive.
        
   This is test file %d!asciiN)rangeencoderk   listr   unzipIterChunkyr_   rd   r(   setlistdirmaprf   childrenintbasename
getContent)r   numfilesrj   r   rh   r^   nums          r   test_unzipIterChunkyz#ZipstreamTests.test_unzipIterChunky   s    
 9>xIA+a/II/78!AHHW%88%%h/
Y&&z4==3E3EFGT]]2245s3sE(O;T7UV]]++- 	@Eenn&'CU--/#?	@ J8s
   D/D4c                    d}t        |      D cg c]  }d|z  	 }}|D cg c]  }|j                  d       }}| j                  |d      }t        t	        j
                  || j                  j                               t        |      D ch c]  }t        |      j                  d       }}| j                  t        | j                  j                  d      j                               |       | j                  j                  d      j                         D ]>  }t        |j                               }| j                  |j!                         ||          @ yc c}w c c}w c c}w )z
        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.
        r   r   r   foos   fooN)r   r   rk   r   r   r   r_   rd   rf   r(   r   r^   r   r   r   r   r   )r   r   rj   r   rh   r   fileContentsr^   s           r   test_unzipIterChunkyDirectoryz,ZipstreamTests.test_unzipIterChunkyDirectory  s*    9>xIA+a/II/78!AHHW%88%%h6
Y&&z4==3E3EFG<A(OLSC0LLT]]008@@BC\R]]((099; 	@Eenn&'CU--/#?	@ J8 Ms   EE#!E(c                 D   d}t        d      D ]B  }t        t        j                         d      }t        |      j	                  d      }|d|z   z  }D t        |      j                         }	t        j                  | j                               }
|
j                          |
j                  d      j                  }| j                  |||       t        j                  ||
j                  |      }t!        |      }||cxk  xr |k  nc }| j#                  |       |D ]  } | j%                  |d       |
j                  d	      j'                         5 }t        |j)                               j                         }| j%                  ||	       d
d
d
       y
# 1 sw Y   y
xY w)zW
        unzipIterChunky should unzip the given number of bytes per iteration.
        r'   i     r       zbigfile.zip)	chunksizer   zipstreamjunkN)r   roundrandomrf   r   r   	hexdigestr   r[   r   r]   r^   rd   _makebigfiler   r   r.   r#   r(   r|   r;   )r   r   r   lowerupperjunknr   
numEncodedjunkmd5tempdirzfpathuziterr   approxfnewmd5s                    r   _unzipIterChunkyTestz#ZipstreamTests._unzipIterChunkyTest  s_    t 	&A,CS1JD:%%D	&
 d)%%'##DKKM2}-22&+t4**67<<9UL "U" 	A	A]]?+002 	.a],,.FVW-	. 	. 	.s   :FFc                 H    | j                  t        j                  ddd       y)zs
        unzipIterChunky should unzip the given number of bytes per iteration on
        a stored archive.
        i  #   -   N)r   r   rV   r`   s    r   test_unzipIterChunkyStoredz)ZipstreamTests.test_unzipIterChunkyStored5  s    
 	!!'"4"4c2rBr'   c                 H    | j                  t        j                  ddd       y)zu
        unzipIterChunky should unzip the given number of bytes per iteration on
        a deflated archive.
        i        N)r   r   rS   r`   s    r   test_chunkyDeflatedz"ZipstreamTests.test_chunkyDeflated<  s    
 	!!'"6"6RDr'   c                     t        j                  |d|      5 }t        d      D ]  }d|z  }|j                  |d        |j                  d|       ddd       y# 1 sw Y   yxY w)zT
        Create a zip file with the given file name and compression scheme.
        r
   r   zzipstream%dr   r   N)r   r   r   r   )r   r   r   r   r   rj   r   s          r   r   zZipstreamTests._makebigfileC  sf     __XsK8 	/B2Y $"Q&B#$ KK.		/ 	/ 	/s   :AA$N)r   )rL   rM   rN   rO   ra   rk   rq   rt   r   r   r   r   r   r   r   r   r   r   rP   r'   r   rX   rX      sT    !GC?"?$E";@ @,.<CE/r'   rX   )rO   r   r   r   hashlibr   twisted.pythonr   r   twisted.trialr   r   TestCaserR   rU   rX   rP   r'   r   <module>r      sg        . "^. ^.B'0A0A '%(9(9 %I/X&& I/r'   