a zex@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z ddl m Z ddlmZmZddlmZmZe dZdZd Zeje jd ZGd d d ejZGd ddejZGdddeZ GdddeZ!ddZ"GdddejZ#e$dkre%dS)z!Test script for the gzip module. N)PIPEPopen)support)_4G bigmemtest)assert_python_okassert_python_failuregzipsy int length=DEFAULTALLOC, err = Z_OK; PyObject *RetVal; int flushmode = Z_FINISH; unsigned long start_total_out; s/* zlibmodule.c -- gzip-compatible data compression */ /* See http://www.gzip.org/zlib/ /* See http://www.winimage.com/zLibDll for Windows */ z-gzdirc@s$eZdZddZddZddZdS) UnseekableIOcCsdS)NFselfr r B/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_gzip.pyseekable$szUnseekableIO.seekablecCs tjdSNioUnsupportedOperationr r r rtell'szUnseekableIO.tellcGs tjdSrr)r argsr r rseek*szUnseekableIO.seekN)__name__ __module__ __qualname__rrrr r r rr #sr c@s"eZdZejZddZddZdS)BaseTestcCst|jdSrrunlinkfilenamer r r rsetUp1szBaseTest.setUpcCst|jdSrrr r r rtearDown4szBaseTest.tearDownN)rrrrTESTFNrrrr r r rr.src@szeZdZd]ddZddZddZdd Zd d Zd d ZddZ ddZ ddZ e e ddddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Zd:d;Z dd?Z"d@dAZ#dBdCZ$dDdEZ%dFdGZ&dHdIZ'dJdKZ(dLdMZ)dNdOZ*dPdQZ+dRdSZ,dTdUZ-dVdWZ.dXdYZ/dZd[Z0d\S)^TestGzipbcCst|}t|jd|}||}Wdn1s:0Y||t|t|jd| }|||Wdn1s0YdS)Nwr)bytesr GzipFilerwrite assertEquallenread)r datamodeZb_dataflr r rwrite_and_read_back9s (zTestGzip.write_and_read_backcCsxt|jdN}|td||ttdrFt || Wdn1sb0Y| dS)Nwb2fsync) r r&rr'data1flushfilenohasattrosr2closer r-r r r test_writeAs &zTestGzip.test_writecCst|j}t|d}|tdWdn1s<0Y||jt t|d}|tWdn1s0Yt|}| }Wdn1s0Y| |td||jt dS)Nr#r1a3) pathlibPathrr r&r'r3ZassertIsInstancenamestrr*r()r rr-dr r r"test_write_read_with_pathlike_fileOs ,( &z+TestGzip.test_write_read_with_pathlike_filecCsB|ttdtttd}|jdgdd}||dS)Nr1B)rE)shape)r/ memoryviewr3r%rangecast)r mr+r r rtest_write_memoryview^szTestGzip.test_write_memoryviewcCs|ttddS)Nr1)r/ bytearrayr3r r r rtest_write_bytearraydszTestGzip.test_write_bytearraycCs|tdtddS)NI()r/arrayr3r r r rtest_write_arraygszTestGzip.test_write_arrayc Cst|jd}|t|dWdn1s:0Y|t|gWdn1sn0Y|tWdn1s0Yt|jd }||tWdn1s0YdS)Nr0rb) r r&r assertRaises TypeErrorr'r3r(r*r9r r rtest_write_incompatible_typejs ( ((z%TestGzip.test_write_incompatible_typecCsR|t|jd}|}Wdn1s40Y||tddS)Nr$r1)r:r r&rr*r(r3r r-rAr r r test_readws&zTestGzip.test_readcCs|g}d}t|jdF}|}|s.qV|||t|7}|||q Wdn1sj0Y|d |t ddS)Nrr$r1) r:r r&rread1appendr)r(rjoinr3)r Zblocksnreadr-rAr r r test_read1~s  0zTestGzip.test_read1)sizeZmemusecCs8tjtdd}tjt|dd}|||tdS)Nr` compresslevelrTfileobjr,)r compressr3r&rBytesIOr(r*)r ra compressedr-r r rtest_read_largeszTestGzip.test_read_largecCsx|t|jd}|j}||j|||j| t | dWdn1sf0Y| t | dWdn1s0Y| t | Wdn1s0Yt|jd}|j}||j|||j| t |dWdn1s60Y| t |Wdn1sj0YdS)Nr$r`rr#rZ)r:r r&rre assertFalseclosedr8 assertTruerU ValueErrorr*rrr'r4)r r-rer r rtest_io_on_closed_objects*   ( ( &   * z!TestGzip.test_io_on_closed_objectcCs|t|jd}|tdWdn1s:0Yt|jd}|}Wdn1sp0Y||tdtddS)NabrTr1) r:r r&rr'data2r*r(r3rXr r r test_appends ,&zTestGzip.test_appendc Cst|jdd}|dWdn1s00YtddD]>}t|jdd}|dWdqD1sx0YqDt|jd.}d}|d }||7}|sqqWdn1s0Y||d dS) Nr0 arrorTrZi saaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)r r&rr'rIr*r()r r-iZzgfilecontentsZztxtr r rtest_many_appends(* &zTestGzip.test_many_appendcCst|jd}|tdWdn1s20Yt|jd$}||tdWdn1st0Y|tt|jdWdn1s0YdS)Nxbr1rT) r r&rr'r3r(r*rUFileExistsErrorr9r r rtest_exclusive_writes ,2 zTestGzip.test_exclusive_writec Cs|t|jdH}t|}dd|D}Wdn1sF0YWdn1sd0Y||dtjdddS)NrTcSsg|]}|qSr r ).0liner r r rZz1TestGzip.test_buffered_reader..r1Tkeepends) r:r r&rrBufferedReaderr(r3 splitlines)r r-r$linesr r rtest_buffered_readers  JzTestGzip.test_buffered_readercCsv|t|jdL}d}||}|s4|dkr4qT|t||k|dd}qWdn1sh0YdS)NrTrr`r1)r:r r&rreadlinerlr))r r-Z line_lengthLr r r test_readlines zTestGzip.test_readlinecCs|t|jd}|}Wdn1s40Yt|jd&}|d}|gkrNqdqNWdn1sx0YdS)NrT)r:r r&r readlines)r r-rr r rtest_readliness & zTestGzip.test_readlinescCs|t|j|}|}|}|s,q|}||t|dkrPd}nt|}||}| |d||||qWdn1s0YdS)N ) r:r r&rrrrr)r*r()r r-ZoldposZline1ZnewposamountZline2r r rtest_seek_reads   zTestGzip.test_seek_readcCsn|t|j2}|d|jddd|d}Wdn1sL0Y||tdddS)Nrr`)whence)r:r r&rr*rr(r3)r r-yr r rtest_seek_whence s  (zTestGzip.test_seek_whencecCsXt|jd6}tdddD]}|||dqWdn1sJ0YdS)Nr#rrCsGZ )r r&rrIrr')r r-posr r rtest_seek_writes zTestGzip.test_seek_writecCs|t|jd }||jjdWdn1s<0Yt|jt|jd }||jjdWdn1s0YdS)Nr$rTxry) r:r r&rr(Z myfileobjr,rrr9r r r test_modes . zTestGzip.test_modec Cs\dD]R}t|j|0}|t|d||j|jWdq1sL0YqdS)N)r0rTr?)r r&rrlr6r(r?)r r,r-r r r test_1647484%szTestGzip.test_1647484cCs^|t|jd4}|t|jd||jj|jWdn1sP0YdS)NrTr?) r:r r&rrlr6rer(r?r9r r rtest_paddedfile_getattr+sz TestGzip.test_paddedfile_getattrcCsd}tj|jd|d}|tWdn1s60Yt|jN}|t|d||j| }| |t| |j|Wdn1s0YdS)N[r#mtimer) r r&rr'r3rlr6Z assertIsNonerr*r()r rfWritefReadZdataReadr r r test_mtime1s(  zTestGzip.test_mtimec Csd}tj|jd|d}|tWdn1s60Yt|jd:}|d}||d|d}||dz|jd d }d}Wnt yd }d }Yn0|d}||||d } || t d ||d} || d|d} || d|t |} || || t|jjd|d } || d|d }||t d t tWdn1s0YdS)Nrr#rrTr`zLatin-1rZrFz.sizesrT!rrZd) r3r r&rr'Zmax_read_chunkpeekr(r*r))r rr-rr^rsr r r test_peeks(  zTestGzip.test_peekc Cstddjdd}|t|jdN}tj|dd }| | |Wdn1sb0YWdn1s0YdS)Nr1asciiTrr$encoding) r3decoderr:r r&rr TextIOWrapperr(r)r rr-tr r rtest_textio_readliness zTestGzip.test_textio_readlinesc Cstt|jtjtjB}t|d>}tj|dd}Wdn1sH0YWdn1sf0YdS)Nr0r#rd)r7rrO_WRONLYO_CREATfdopenr r&)r fdr-gr r rtest_fileobj_from_fdopensz!TestGzip.test_fileobj_from_fdopenc Cst|jdt|jdD}tj|dd }||jtjWdn1sV0Ytj|dd }||jtjWdn1s0Ytj|dd }||jtjWdn1s0Ytj|dd }||jtjWdn1s0Y| t tj|ddWdn1sL0YWdn1sl0Yd D]r}t|j|N}tj|d  }||jtjWdn1s0YWdn1s0Yqzd D]}d|vr t |jt|j|~}| ttj|d }Wdn1sH0Y| ||jtjWdn1s~0YWdn1s0YqdS) Nr0r+br$rdr#r;rz)rTrre)r0rory)r r&rr8rr(r,ZREADZWRITErUrmrrZ assertWarns FutureWarning)r r-rr,r r rtest_fileobj_modes0...0 NT   ,zTestGzip.test_fileobj_modecCs|j}z|d}Wnty0|dYn0t|d}|tdWdn1sb0Yt|d$}|| tdWdn1s0Yt|d$}|| tdWdn1s0YdS)Nrz%Temporary file name needs to be ASCIIr0r1rT) rrrZskipTestr r&r'r3r(r*)r Z str_filenameZbytes_filenamer-r r rtest_bytes_filenames ,2zTestGzip.test_bytes_filenamecCsttjdtddd}|t|tjt|}tj|d}| | dddtj}| |j j |ddS) z-Decompressed data buffering should be limitedrg>Arsrbrr`z)Excessive amount of data was decompressedN)r rfintZ assertLessr)rDEFAULT_BUFFER_SIZErgr&r(r*ZassertLessEqual_bufferrawr)r ZbombdecompZ max_decompr r rtest_decompress_limited s   z TestGzip.test_decompress_limitedc CsttfD]t}dD]j}tj|g|R}|t|ttjt |dd }|| |Wdq1sp0YqqdS)Nr )r`)r)rsrTrd) r3rqr rfr(typer%r&rrgr*)r r+rdatacr-r r r test_compresss  zTestGzip.test_compressc Csd}ttfD]}dD]}|j||dtj|g|Rd|i}|t|ttjt |dd(}| d||j |Wdn1s0YWdq1s0Yqq dS)Nrr)r+rrrTrdr`) r3rqrr rfr(rr%r&rrgr*r)r rr+rrr-r r rtest_compress_mtimes  zTestGzip.test_compress_mtimec CsttfD]v}t}tj|dd}||Wdn1sB0Y|t| |t |}|t||qdS)Nr0rd) r3rqrrgr r&r'r( decompressrrf)r r+rr-rr r rtest_decompress*s ( zTestGzip.test_decompressc Cstd}t|dd}tjt|d}|t|jWdn1sP0Ytjt|d6}| |t |||t|jdWdn1s0Yt ddD]P}tjt|d|d }|t|jdWdq1s0YqdS)Nr1irr`rr) r3r rfr&rrgrUEOFErrorr*r(r)rI)r r+Z truncatedr-rvr r rtest_read_truncated4s,.zTestGzip.test_read_truncatedcCsJd}tjt|d }||dWdn1s<0YdS)NscQExtra I-.2MxrsTest)r r&rrgr(r*)r Zgzdatar-r r rtest_read_with_extraBszTestGzip.test_read_with_extracCsxt|jd}|tWdn1s.0Yt|jd}|jjjWdn1sj0YdS)Nr0rT) r rrr'r3rr_fpZprependr9r r rtest_prepend_errorJs(zTestGzip.test_prepend_errorN)r")1rrrr/r:rBrLrNrRrWrYr_rrrirnrrrxr{rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr r r rr!8sZ          3    r!c@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)TestOpencCstd}t|jd}||Wdn1s60Yt|jd*}t|}|||Wdn1s|0Yt|jd }|||Wdn1s0Yt|jd}||Wdn1s0Yt|jd.}t|}|||dWdn1s>0Y|t t|jdWdn1sx0Yt |jt|jd}||Wdn1s0Yt|jd*}t|}|||Wdn1s0YdS)Nr1r0rTrorry r3r rrr'rr*r(rUrzrrr rr- file_datar r rtest_binary_modesRs*(*.(0 . *zTestOpen.test_binary_modescCst|j}t|d}|tdWdn1s<0Yt|d}|tWdn1sr0Yt|$}||tdWdn1s0YdS)Nr0r1ror<) r=r>rr rr'r3r(r*)r rr-r r rtest_pathlike_filems ,( zTestOpen.test_pathlike_filecCstd}t|jd}||Wdn1s60Yt|jd*}t|}|||Wdn1s|0Yt|jd }|||Wdn1s0Yt|jd}||Wdn1s0Yt|jd.}t|}|||dWdn1s>0Y|t t|jdWdn1sx0Yt |jt|jd}||Wdn1s0Yt|jd*}t|}|||Wdn1s0YdS)Nr1r#rTr$r;rrrrr r rtest_implicit_binary_modesvs*(*.(0 . *z#TestOpen.test_implicit_binary_modescCsntdd}|dtj}t|jd}||Wdn1sJ0Yt|jd0}t | d}| ||Wdn1s0Yt|jd }| | |Wdn1s0Yt|jd}||Wdn1s0Yt|jd4}t | d}| ||dWdn1s`0YdS) Nrr1 wtrTrtZatr r3rreplacer7linesepr rrr'rr*r(r rZuncompressed_rawr-rr r rtest_text_modess(*.*zTestOpen.test_text_modescCstd}|d}t|}tt|d }|||Wdn1sT0Ytt|d }|||Wdn1s0Ytt|d }|||Wdn1s0YdS)Nr1rr$rTr) r3rr rfrrrgr(r*)r Zuncompressed_bytesZuncompressed_strrhr-r r r test_fileobjs  ..zTestOpen.test_fileobjcCs`|ttdWdn1s*0Y|tt|jdWdn1sb0Y|tt|jdWdn1s0Y|t"tj|jdddWdn1s0Y|t"tj|jdddWdn1s0Y|t"tj|jdd d Wdn1sR0YdS) Ngw/^@ZwbtZxbtrTzutf-8rignore)errorsrnewline)rUrVr rrmrr r r rtest_bad_paramss ( , , 0 2 zTestOpen.test_bad_paramscCstdd}|dtj}tj|jddd}||Wdn1sN0Yt|jd0}t | d}| ||Wdn1s0Ytj|jddd }| | |Wdn1s0YdS) Nrr1rrzutf-16rrTrrrr r r test_encodings(*zTestOpen.test_encodingcCst|jd}|dWdn1s.0Ytj|jdddd }||dWdn1sr0YdS)Nr0sfoobarrrr)rrZfoobar)r rrr'r(r*r9r r rtest_encoding_error_handlers (z$TestOpen.test_encoding_error_handlercCstdd}tj|jddd}||Wdn1s@0Ytj|jddd"}|||gWdn1s0YdS)Nrr1rrrr )r3rr rrr'r(r)r rr-r r r test_newlines (zTestOpen.test_newlineN) rrrrrrrrrrrrr r r rrQs   rcsfdd}|S)Ncstfdd}|S)Nc s6tz|i|WtSt0dSr)r7makedirsrrmtree)rkwargs) directoryfunctionr rwrappers   z?create_and_remove_directory..decorator..wrapper) functoolswraps)r rr )r r decoratorsz.create_and_remove_directory..decoratorr )r rr rrcreate_and_remove_directorys rc@speZdZdZddZeeddZddZeedd Z eed d Z eed d Z ddZ ddZ dS)TestCommandLinesThis is a simple test with gzipc Cst}tj|dd}||jWdn1s:0Ytjdddf}t|t t t d"}| | \}}Wdn1s0YWdn1s0Y| |d| ||jdS)Nr0rd-mr -dstdinstdoutstderrrZ) rrgr r&r'r+sys executablerr communicaterr()r Zbytes_ioZ gzip_filerprocouterrr r rtest_decompress_stdin_stdouts *N z,TestCommandLine.test_decompress_stdin_stdoutcCstjtd}|tj|tj|dd}||j Wdn1sP0Yt ddd|\}}}ttjtdd"}| | |j Wdn1s0Y| tj|| |d | |d | |d dS) Nz testgzip.gzr0)r,rr rtestgziprTrrZ)r7pathr]TEMPDIRrjexistsr rr'r+rr(r*rl)r gzipnamefprcrr Zgunzipedr r rtest_decompress_infile_outfiles*0  z.TestCommandLine.test_decompress_infile_outfilecCs@tdddd\}}}|d|||d||ddS)Nrr rzthisisatest.outs.filename doesn't end in .gz: 'thisisatest.out'r`rZ)rr(stripr r(rr r r r$test_decompress_infile_outfile_errors z4TestCommandLine.test_decompress_infile_outfile_errorcCsptjddf}t|tttd }||j\}}Wdn1sB0Y||d||ddddS)Nrr rrZrr)rrrrrr+r()r rrrr r r rtest_compress_stdin_outfile s  . z+TestCommandLine.test_compress_stdin_outfilecCstjtd}|d}|tj|t|d}||jWdn1sT0Yt dd|\}}}| tj|| |d| |ddS)Nr".gzr0rr rZ) r7r#r]r$rjr%rr'r+rrlr()r local_testgzipr&r'r(rr r r r$test_compress_infile_outfile_defaults * z4TestCommandLine.test_compress_infile_outfile_defaultc CsdD]}|j|dtjtd}|d}|tj|t|d}||j Wdn1sj0Yt dd||\}}}| tj|| |d| |dt ||tj|Wdq1s0YqdS) N)--fast--best)compress_levelr"r.r0rr rZ)rr7r#r]r$rjr%rr'r+rrlr(remove)r r3r/r&r'r(rr r r rtest_compress_infile_outfile$s *   z,TestCommandLine.test_compress_infile_outfilecCs0tdddd\}}}|d|||ddS)Nrr r1r2s8error: argument --best: not allowed with argument --fastrZrZassertInr(r+r r r%test_compress_fast_best_are_exclusive7s z5TestCommandLine.test_compress_fast_best_are_exclusivecCs0tdddd\}}}|d|||ddS)Nrr r1rsAerror: argument -d/--decompress: not allowed with argument --fastrZr6r+r r r-test_decompress_cannot_have_flags_compression<s z=TestCommandLine.test_decompress_cannot_have_flags_compressionN)rrrr+r!rr$r)r,r-r0r5r7r8r r r rrs     r__main__)&__doc__rQrrr7r=rrZunittest subprocessrrtestrZ test.supportrrZtest.support.script_helperrr import_moduler r3rqr#abspathr r$rgr ZTestCaserr!rrrrmainr r r rs:      \