Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/python/test/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyc


=Nc@sdZddlZddlZddlZddlmZddlmZmZddl	m
Z
ddlmZm
Z
dddYZd	eejfd
YZdeejfdYZd
ejfdYZdS(s'
Tests for L{twisted.python.zipstream}
iN(tset(t	zipstreamtfilepath(tmd5(tunittesttutiltFileEntryMixincBsheZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZRS(s?
    File entry classes should behave as file-like objects
    cCs]|j}tj|d|j}|jd||jtj|d}|jdS(s6
        Return an appropriate zip file entry
        twtcontenttr(	tmktemptzipfiletZipFiletcompressiontwritestrtcloseRtChunkingZipFiletreadfile(tselftcontentstfilenametz((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pytgetFileEntrys
cCs#|j|jdjtdS(sK
        zip files should not be ttys, so isatty() should be false
        tN(tassertEqualRtisattytFalse(R((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_isatty scCsC|jd}|j|jt|j|j|jtdS(sd
        The C{closed} attribute should reflect whether C{close()} has been
        called.
        RN(RRtclosedRRtTrue(Rt	fileEntry((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_closed's
cCsU|jd}|j|jd|j|jd|j|jddS(sl
        C{readline()} should mirror L{file.readline} and return up to a single
        deliminter.
        shoho
hoshoho
thoRN(RRtreadline(RR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt
test_readline2scCsR|jd}|j|jd|j|jd|jt|jdS(sV
        Zip file entries should implement the iterator protocol as files do.
        sho
hohosho
thohoN(RRtnexttassertRaisest
StopIteration(RR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt	test_next=scCs2|jd}|j|jdddgdS(sG
        C{readlines()} should return a list of all the lines.
        sho
ho
hosho
R N(RRt	readlines(RR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_readlinesGscCs?|jd}|jt|||j|j|dS(sJ
        C{__iter__()} and C{xreadlines()} should return C{self}.
        RN(RtassertIdenticaltitert
xreadlines(RR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_iterationOscCs/d}|j|}|j|j|dS(s9
        C{.read()} should read the entire file.
        s
Hello, world!N(RRtread(RRtentry((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_readWholeXscCsWd}|j|}|jd}|jd}|j|d|j|ddS(sD
        C{.read(num)} should read num bytes from the file.
        t
0123456789iit0123t456789N(RR.R(RRR/tonettwo((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_readPartialascCscdd}|j|}|jd|j|jd|jd|j|jddS(sb
        C{.tell()} should return the number of bytes that have been read so
        far.
        txidiiiN(RR.Rttell(RRR/((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt	test_tellms


(
t__name__t
__module__t__doc__RRRR"R'R)R-R0R6R9(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyRs					
						tDeflatedZipFileEntryTestcBseZdZejZRS(s2
    DeflatedZipFileEntry should be file-like
    (R:R;R<RtZIP_DEFLATEDR
(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyR={stZipFileEntryTestcBseZdZejZRS(s(
   ZipFileEntry should be file-like
   (R:R;R<Rt
ZIP_STOREDR
(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyR?st
ZipstreamTestcBsgeZdZdZddZdZdZdZdZdZ	e
jdkrxdZee_
ee	_
ndZd
ZdZejddge_dZdZdZdZejddge_dZdZejddge_dZejddejddge_dZdZdZdZRS(s,
    Tests for twisted.python.zipstream
    cCsKtj|j|_|jj|jjd|_|jjdS(sz
        Creates junk data that can be compressed and a test directory for any
        files that will be created
        tunzippedN(RtFilePathR
ttestdirtmakedirstchildtunzipdir(R((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pytsetUps
RcCs|jjdj}tj|d}xMt|D]?\}}t|}|rc|d|}n|j||q4W|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.
        szipfile.zipRt/(	RDRFtpathRRt	enumeratetstrRR(RRt	directoryt
zpfilenametzpfiletiRR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pytmakeZipFiles
csS|jdddddg|jtdtfd}|j|dd	S(
s
        Make sure the deprecated L{countZipFileEntries} returns the correct
        number of entries for a zip file.
        R4R5tthreetfourtfives"countZipFileEntries is deprecated.cs
tjS(N(RtcountZipFileEntries((tname(sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt<lambda>siN(RQtassertWarnstDeprecationWarningt__file__R(Rtresult((RVsF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_countEntriess
	cCs2tj|jd}|jt|jddS(s
        A ChunkingZipFile opened in write-mode should not allow .readfile(),
        and raise a RuntimeError instead.
        Rt	somethingN(RRR
R%tRuntimeErrorR(Rtczf((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_invalidModescCsBtj|jdgd}|j|jt|jddS(sn
        A closed ChunkingZipFile should raise a L{RuntimeError} when
        .readfile() is invoked.
        R]R	N(RRRQRR%R^R(RR_((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_closedArchives
cCs|jddg}tj|d}|jdj}|jt|d}|j|d|jt	dd|jt
j|}|jtj
|jd|j|jdjdd	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	t0sr+biit1N(RQRRtgetinfot
header_offsetRtfiletseektwritetchrRRR%t
BadZipfileRRR.(Rtfntzft
zeroOffsettscribbleR_((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_invalidHeaders	

cCs|jddg}tj|d}|jd}d|_|jt|d}|j|jd|j	|j
|jtj|}|j
tj|jd|j|jdjdd	S(
s
        A zipfile entry with a different filename than is found in the central
        directory should raise BadZipfile.
        s
test contentss
more contentsR	Rbsnot zerosr+biRcN(RQRRRdRRRfRgReRht
FileHeaderRRR%RjRRR.(RRkRltinfoRnR_((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_filenameMismatchs		

iis+Consistency-checking only necessary in 2.5.cCs||j}tj|d}tjd}|j|dd|_|jtj|}|j	tj
|jddS(sq
        A zipfile which describes an unsupported compression mechanism should
        raise BadZipfile.
        RRbs	some dataiN(R
RRtZipInfoRt
compress_typeRRRR%RjR(RRkRltziR_((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_unsupportedCompressions	
cCs|j}tj|d}tjd}d|_|j|d|jtj|}|j	|j
djddS(sW
        readfile() should skip over 'extra' data present in the zip metadata.
        RRbshello, extras
the real dataN(R
RRRstextraRRRRRRR.(RRkRlRuR_((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_extraDatas	
cCsd}gt|D]}d|^q}|j|}tj||jj}xWt|D]I}|jtt|jj	||j|j
||dq]W|jtt|jj	|xI|jj	D]8}t|j}|j|j
j||qWdS(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
sThis is test file %d!iN(trangeRQRt	unzipIterRGRJRtlentlisttchildrenR$tinttbasenametopenR.(RtnumfilesRPRRNtuziterRFtnum((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_unzipIters#%"%tmessages!zipstream.unzipIter is deprecatedcCs|jd}|jt|jdx tj||jjD]}qDW|j}|jt|d|j|ddt|j|ddddS(sf
        Use of C{twisted.python.zipstream.unzipIter} will emit a
        deprecated warning.
        tfooiitcategoryRskzipstream.unzipIter is deprecated since Twisted 11.0.0 for security reasons.  Use Python's zipfile instead.N(	RQRR{t
flushWarningsRRzRGRJRY(RRNtftwarnings((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_unzipIterDeprecated4scCsd}gt|D]}d|^q}|j|}ttj||jj|jt|jj	tt
tt|xC|jjD]2}t
|j}|j|j||qWdS(s
        L{twisted.python.zipstream.unzipIterChunky} returns an iterator which
        must be exhausted to completely unzip the input archive.
        i
sThis is test file %d!N(RyRQR|RtunzipIterChunkyRGRJRRtlistdirtmapRLR}R~Rt
getContent(RRRPRRNRFR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_unzipIterChunkyIs#cCsd}gt|D]}d|^q}|j|d}ttj||jj|jt|jj	dj
tttt|xL|jj	dj
D]2}t|j}|j|j||qWdS(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
sThis is test file %d!RN(RyRQR|RRRGRJRRRFRRRLR}R~RR(RRRPRRNRFR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_unzipIterChunkyDirectory[s#cCsd}|jgt|D]}t|^q}tj||jj|jt|jj	tt
tt|xBt|D]4}|j|jjt|jt|qWdS(sg
        L{twisted.python.zipstream.unzip} should extract all files from a zip
        archive
        iN(
RQRyRLRtunzipRGRJRRRRRFR(RRRPRN((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt
test_unzipns.szipstream.unzip is deprecatedcCs|jd}|jt|jdtj||jj|j}|jt|d|j|ddt|j|ddddS(sZ
        Use of C{twisted.python.zipstream.unzip} will emit a deprecated warning.
        RiiRRsgzipstream.unzip is deprecated since Twisted 11.0.0 for security reasons.  Use Python's zipfile instead.N(	RQRR{RRRRGRJRY(RRNR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_unzipDeprecatedscCsd}|jgt|D]}t|^qd}tj||jj|jt|jj	dj
tttt|xKt|D]=}|j|jj	dj	t|jt|qWdS(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.
        iRN(
RQRyRLRRRGRJRRRFRRR(RRRPRN((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_unzipDirectorys1cCs.|jjd}|jdg}|jdtj||jj|j|jj	dtj||jjdt
|j|jj	d|jdtj||jj}|j|j|jj	dtj||jjdt
}|j|j|jj	ddS(s
        L{twisted.python.zipstream.unzip} and
        L{twisted.python.zipstream.unzipIter} shouldn't overwrite files unless
        the 'overwrite' flag is passed
        RbtOVERWRITTENsNOT OVERWRITTENt	overwriteN(
RGRFRQt
setContentRRRJRRR.RRzR$(RttestfileRNR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_overwrites


	
cCs6djgtdD]}ttj^q}t|j}tj|j}|j	|j
dj}	|j|	||t
j|	|jd|}
|
j}||ko|kn}|j|x|
D]}qW|j|dt|j
djjj}
|j|
|dS(sW
        unzipIterChunky should unzip the given number of bytes per iteration.
        t isbigfile.zipt	chunksizeit
zipstreamjunkN(tjointxrangeRLtrandomRt	hexdigestRRCR
RERFRJt_makebigfileRRR$t
failUnlessRRR.(RR
Rtlowertuppertntjunktjunkmd5ttempdirtzfpathRR	tapproxtnewmd5((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt_unzipIterChunkyTests"4
	

$cCs|jtjddddS(ss
        unzipIterChunky should unzip the given number of bytes per iteration on
        a stored archive.
        ii#i-N(RRR@(R((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_unzipIterChunkyStoredscCs|jtjddddS(su
        unzipIterChunky should unzip the given number of bytes per iteration on
        a deflated archive.
        iiiN(RRR>(R((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyttest_chunkyDeflatedscCsdtj|d|}x.tdD] }d|}|j|dq"W|jd||jdS(sT
        Create a zip file with the given file name and compression scheme.
        Ri
szipstream%dRRN(RRRyRR(RRR
RRlRPRk((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyRs
(ii(R:R;R<RHRQR\R`RaRoRrtsystversion_infoRtskipRvRxRRtsuppressRRRRRRRRRRR(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyRAs>		
			
																((R<RRRttwisted.python.compatRttwisted.pythonRRttwisted.python.hashlibRt
twisted.trialRRRtTestCaseR=R?RA(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_zipstream.pyt<module>sk