Ϫf//dZddlZddlZddlZddlmZddlmZmZddl m Z GddZ Gdd e e jZ Gd d e e jZGd d e jZy)z' Tests for L{twisted.python.zipstream} N)md5)filepath zipstream)unittestcLeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z y ) FileEntryMixinz? File entry classes should behave as file-like objects c|j}tj|d|j5}|j d|dddt j |d}|jdS#1swY0xYw)z6 Return an appropriate zip file entry wcontentNr)mktempzipfileZipFile compressionwritestrrChunkingZipFilereadfile)selfcontentsfilenamezs D/usr/lib/python3/dist-packages/twisted/python/test/test_zipstream.py getFileEntryzFileEntryMixin.getFileEntrysk;;= __XsD,<,< = , JJy( + ,  % %h 4zz)$$ , ,s A44A=c|jd5}|j|jdddy#1swYyxYw)zK zip files should not be ttys, so isatty() should be false N)r assertFalseisattyr fileEntrys r test_isattyzFileEntryMixin.test_isatty s@  r " 1i   Y--/ 0 1 1 1s ;Ac|jd5}|j|jddd|jjy#1swY%xYw)zd The C{closed} attribute should reflect whether C{close()} has been called. rN)rrclosed assertTruers r test_closedzFileEntryMixin.test_closed'sO   r " /i   Y-- . /  (() / /s AAc|jd5}|j|jd|j|jd|j|jddddy#1swYyxYw)zk C{readline()} should mirror L{file.readline} and return up to a single delimiter. shoho hoshoho hoN)r assertEqualreadliners r test_readlinezFileEntryMixin.test_readline0st   { + 8y   Y//19 =   Y//15 9   Y//13 7 8 8 8 A!A<<Bc|jd5}|j|jd|j|jd|jt|jdddy#1swYyxYw)zV Zip file entries should implement the iterator protocol as files do. sho hohoho shohoN)rr(next assertRaises StopIterationrs r test_nextzFileEntryMixin.test_next:sk  { + =y   Y^^-w 7   Y^^-w 7   mY^^ < = = =r+c|jd5}|j|jgddddy#1swYyxYw)zG C{readlines()} should return a list of all the lines. sho ho ho)r-r-r&N)rr( readlinesrs rtest_readlineszFileEntryMixin.test_readlinesCsG  } - O   Y0024M N O O Os #>Ac|jd5}|jt|||j|j|dddy#1swYyxYw)zJ C{__iter__()} and C{xreadlines()} should return C{self}. rN)rassertIsiter xreadlinesrs rtest_iterationzFileEntryMixin.test_iterationJsR  r " =i MM$y/9 5 MM)..0) < = = =s Acd}|j|5}|jd}|jd}ddd|jd|jdy#1swY.xYw)zD C{.read(num)} should read num bytes from the file. 0123456789Ns0123s456789)rr;r()rrr=onetwos rtest_readPartialzFileEntryMixin.test_readPartialZsj    x ( "E**Q-C**S/C " g& i(  " "s #A$$A-cd}|j|5}|jd|j|jd|jd|j|jddddy#1swYyxYw)zb C{.tell()} should return the number of bytes that have been read so far. dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrAN)rr;r(tellr<s r test_tellzFileEntryMixin.test_tellesp    x ( .E JJqM   UZZ\1 - JJqM   UZZ\1 -  . . .s A#BB N)__name__ __module__ __qualname____doc__rr r$r*r1r4r9r>rErKr'rrrs:%1*8=O=5 ) .r'rc(eZdZdZej Zy)DeflatedZipFileEntryTestsz2 DeflatedZipFileEntry should be file-like N)rLrMrNrOr ZIP_DEFLATEDrrPr'rrRrRrs&&Kr'rRc(eZdZdZej Zy)ZipFileEntryTestsz* ZipFileEntry should be file-like N)rLrMrNrOr ZIP_STOREDrrPr'rrUrUzs$$Kr'rUcfeZdZdZdZddZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZy)ZipstreamTestsz, Tests for twisted.python.zipstream ctj|j|_|jj |jj d|_|j j y)zz Creates junk data that can be compressed and a test directory for any files that will be created unzippedN)rFilePathr testdirmakedirschildunzipdirrs rsetUpzZipstreamTests.setUpsR  ((7   **:6   r'c|jjdj}tj|d5}t |D],\}}t |}|r|dz|z}|j||. ddd|S#1swY|SxYw)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^pathrr enumeratestrr)rr directory zpfilenamezpfileir rs r makeZipFilezZipstreamTests.makeZipFiles \\'' 6;; __Z - 3'1 3 7q6(39H'2  3 3   3 s ;BB ctj|jd5}|jt|j ddddy#1swYyxYw)z A ChunkingZipFile opened in write-mode should not allow .readfile(), and raise a RuntimeError instead. r somethingN)rrr r/ RuntimeErrorrrczfs rtest_invalidModezZipstreamTests.test_invalidModesK  & &t{{}c : Gc   lCLL+ F G G Gs "AActj|jdgd}|j|j t |j dy)zn A closed ChunkingZipFile should raise a L{RuntimeError} when .readfile() is invoked. rmr N)rrrkcloser/rnrros rtest_closedArchivez!ZipstreamTests.test_closedArchivesB ''(8(8+(GM  , kBr'ct|jddg}tj|d5}|jdj}dddt |d5}|j d|jddddtj|5}|jtj|jd|jd 5}|j|jd ddddddy#1swYxYw#1swYxYw#1swY*xYw#1swYyxYw) 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 0Nr+brs00001 more contents)rkrrgetinfo header_offsetopenseekwriterrr/ BadZipFilerr(r;)rfnzf zeroOffsetscribblerpzfes rtest_invalidHeaderz!ZipstreamTests.test_invalidHeaders   @ A __R % 7C66J 7"e_ % MM*a ( NN8 $ % & &r * ?c   g00#,, Dc" ?c  -=> ? ? ?  7 7 % %  ? ? ? ?s;D $D=D.!D"9D. DD"D+ 'D..D7c|jddg}tj|d5}|jd}d|_dddt |d5}|j jd|j|jdddtj|5}|jtj|jd|jd 5}|j|j!dddddddy#1swYxYw#1swYxYw#1swY*xYw#1swYyxYw) z A zipfile entry with a different filename than is found in the central directory should raise BadZipFile. s test contentsryr rvznot zeroNrwrrx)rkrrrzrr|r}r{r~ FileHeaderrrr/rrr(r;)rrrinforrprs rtest_filenameMismatchz$ZipstreamTests.test_filenameMismatchs   /1AB C __R % '::c?D&DM '"e_ . MM$,,a 0 NN4??, - . & &r * ?c   g00#,, Dc" ?c  -=> ? ? ? ' ' . .  ? ? ? ?s;DKBH KK, -  .  & &r * ;c3<<3D ;   SXXZ)9 : ; ; ;  . .  ; ; ; ;s0ACC2(!C& C2C#&C/ +C22C;c rd}t|Dcgc]}d|z }}|Dcgc]}|jd}}|j|}tt j ||j j|jt|j jtttt||j jD]>}t|j}|j|j!||@ycc}wcc}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)rangeencoderklistrunzipIterChunkyr_rdr(setlistdirmaprfchildrenintbasename getContent)rnumfilesrjrrhr^nums rtest_unzipIterChunkyz#ZipstreamTests.test_unzipIterChunkys 9>xIA+a/II/78!AHHW%88%%h/  Y & &z4==3E3E FG T]]2245s3sE(O;T7UV]]++- @Eenn&'C   U--/# ? @ J8s D/D4cd}t|Dcgc]}d|z }}|Dcgc]}|jd}}|j|d}tt j ||j jt|Dchc]}t|jd}}|jt|j jdj||j jdjD]>}t|j}|j|j!||@ycc}wcc}wcc}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. rrrfoosfooN)rrrkrrrr_rdrfr(rr^rrrrr)rrrjrrhr fileContentsr^s rtest_unzipIterChunkyDirectoryz,ZipstreamTests.test_unzipIterChunkyDirectorys* 9>xIA+a/II/78!AHHW%88%%h6  Y & &z4==3E3E FGrsg ."^.^.B'0A0A'%(9(9%I/X&&I/r'