ogfHHdZdZdZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl mZmZd d lmZmZmZmZmZmZmZmZmZd d lmZGd d ej:ZdZdZ Gddej:Z!GddeZ"Gddej:Z#y)zYaroslav Halchenkoz%Copyright (c) 2013 Yaroslav HalchenkoGPLN)glob)StringIO)LogCaptureTestCaselogSys) formatExceptionInfo mbasename TraceBackFormatterWithTraceBack getLoggergetVerbosityFormat splitwords uni_decode uni_string)MyTimeceZdZdZdZdZy) HelpersTestc td#t\}}|j|d|j|dYyxYw)NzVery bad exception ValueErrorrr assertEqualselfnameargss =/usr/lib/python3/dist-packages/fail2ban/tests/misctestcase.pytestFormatExceptionInfoBasicz(HelpersTest.testFormatExceptionInfoBasic+sG0 ( ))0#%:4D,'D./s 3Ac tdd#t\}}|j|d|j|dYyxYw)NzVery badrz('Very bad', None)rrs rtestFormatExceptionConvertArgsz*HelpersTest.testFormatExceptionConvertArgs3sH0 J %%0#%:4D,'D./s 3Ac|jtdg|jtdg|jtdg|jtddg|jtdddg|jtdddg|jtdddg|jtdgd |jtdgd y) Nz 1z 1 2 2z 1, 2 , z 1 2z 1 2, 3)r$r%3)rrrs rtestsplitwordszHelpersTest.testsplitwords<s:d#R(:b>2&:d#R(:c?SE*:g&c 3:j)C:6:i(3*5:l+_=:l+_=N)__name__ __module__ __qualname__rr!r(r)rrr)s00 >r)rcbddl}|j|d}t|jS)NrT)shell) subprocess check_outputrrstrip)cmdr0rets r_sh_callr5Is-s$/3  r)ctdS)Nz?fail2ban-python -c 'import sys; print(tuple(sys.version_info))')r5r-r)r_getSysPythonVersionr7NsRSSr)c*eZdZfdZdZdZxZS) SetupTestcZtt| tjj t jjt jjtddd}t jj|xr|xsd|_ |jstjdt}|tt!t"j$k7r;tjdtt!t"j$d|y)Nz..zsetup.pyzLSeems to be running not out of source distribution -- cannot locate setup.pyz-Seems to be running with python distribution z8 -- install can be tested only with system distribution )superr9setUpunittestF2B SkipIfFastospathjoindirname__file__existssetupSkipTestr7strtuplesys version_info)rrFsysVer __class__s rr<zSetupTest.setUpTs 4  ,, '',,rwwx0$j I%ww~~e$.6$$*    ! "" !& s5))*++   DGcN^N^H_D`bhj kk,r)c|jsytj}tjj t jk\rdnd} tjtjd|jd|||jtj| tj|y#tj|wxYw)Nz >/dev/null 2>&1r# z --dry-run install --root=)rFtempfilemkdtempr=r> log_levelloggingDEBUGr@systemrJ executable assertTruelistdirshutilrmtree)rtmp supdbgouts rtestSetupInstallDryRunz SetupTest.testSetupInstallDryRunds  #$,LL$:$:gmm$K QS)99 ..$**sI78??rzz#&' ==6==s ACCc H |jsytj tjj d }t jjtjk\rdnd} |jtjtjd|jd |d fd}|tt!d z}gd }t#|j%|rd }i}t#|j%|D]Y}tjj' |} tjj | xr||| d xsd||<[|j||d |d |d d|dD]S} |j)tjj tjj' | d| zUt+d zdzj-d} |j)t/| dkD| D]\} |jtjj1| tjj1tj^ t3j4 tjtjd|jd|r|dznd|r6tjj drt3j4dyyy#t3j4 tjtjd|jd|r|dznd|r6tjj drt3j4dwwwxYw)Nbuildz >/dev/nullr#rOz install --root=rcL|Dcgc]}|tdzdc}Scc}wNr)len)lxr[s r strippathz1SetupTest.testSetupInstallRoot..strippaths%$% &qAc#hqjkN && &s!z%s/*)etcusrvarcg}tj|D]9\}}tj||z|}|j fd|D;|S)Nc3^K|]$}tjj|&ywN)r@rArB).0fbases r zISetupTest.testSetupInstallRoot..recursive_glob..s >qRWW\\$*>s*-)r@walkfnmatchfilterextend)treerootpatternresultsdirsfiles goodfilesrns @rrecursive_globz6SetupTest.testSetupInstallRoot..recursive_globsTW ggh/? tU..w7i nn>I>>? ^r)*zGot: z Needed: z under z. Files under new paths: msg)zetc/fail2ban/fail2ban.confzetc/fail2ban/jail.confz Can't find %szfind z -name fail2ban-python z clean --allz 2>&1)rFrPrQr@rArEr=r>rRrSrTrrUrJrVsortedrset differencerBrWr5splitrbrealpathrYrZ) r remove_buildr\regotneedrzrxmissing missing_fullrm installedPathr[s @rtestSetupInstallRootzSetupTest.testSetupInstallRootts  #WW^^G,,,'||55FmB)8BII ..$**c96789;' 6$v|,- .3 4 $i3 Es8&&t,?GGLLg.\ggnn\27 N<5 6?:> 7^?  $ T3   !!qOOBGGNN277<<Q#78 a !! GcM+CCDJJ4P=??3}%)*%H}WWm$bgg&6&6s~~&FHH  ==99 ..$**yy7':b&PRSrww~~g. MM'/l  ==99 ..$**yy7':b&PRSrww~~g. MM'/ls3HLB N!)r*r+r,r<r]r __classcell__)rMs@rr9r9Rsk  ?r)r9cHeZdZdZdZdZdZdZdZdZ dZ d Z d Z y ) TestsUtilsTestc|jtdd|jtdd|jtdd|jtdd|jtddy) Nz sample.pysamplez/long/path/sample.pyz/long/path/__init__.pyz path.__init__z/long/path/base.pyz path.basez/long/path/base)rr r's r testmbasenamezTestsUtilsTest.testmbasenameso9[)84934h?956H912K@9./=r)c6|jtttjdk\rdnddttjdk\rdndt dt dtjdkrdtj vr t dyyy)N)stesttestzf2b-test::non-existing-encodingstestutestÏPyPy) assertRaises ExceptionrrJrKrversionr's rtestUniConvertersz TestsUtilsTest.testUniConvertersszIz4'GV6WY S--5k:G [ Zs{{!: j";r)ct}Gdd}|}|jtjd||j dd|j |j d||j dd|j dtjz|d}|jtjd |||j d d d |j d|||j dd d |j dGddtj}|j} t|j|_ |xj|gz c_ |jtjd|j d||_ |j dy#||_ wxYw)NceZdZddZdZy),TestsUtilsTest.testSafeLogging..Testc||_yrk)err)rrs r__init__z5TestsUtilsTest.testSafeLogging..Test.__init__s DHr)c2|jr tdy)Nno represenation for test!u.conv-error (òðåòèé), unterminated utf Ï)rrr's r__repr__z5TestsUtilsTest.testSafeLogging..Test.__repr__s xx 1 22 Jr)N)r)r*r+r,rrr-r)rTestrs Kr)rz test 1a: %r Tracebackrz test 1b: %rz.[phase 2] test error conversion by encoding %srztest 2a: %r, %sztest 2azError by logging handlerFallztest 2b: %r, %sztest 2bz*[phase 3] test unexpected error in handlerceZdZdZy)5TestsUtilsTest.testSafeLogging.._ErrorHandlerctd)Nzerror in handler test!)r)rrecords rhandlez._ErrorHandler.handles , --r)N)r*r+r,rr-r)r _ErrorHandlerrs.r)rztest 3aztest 3bOK) DefLogSyslogrSNOTICE assertLoggedpruneLognoticerJgetdefaultencodingHandlerhandlerslist)rrrrr _org_handlers rtestSafeLoggingzTestsUtilsTest.testSafeLoggingso &KK $**W^^]D1K!=>--/-- t$K!=>--@3CYCYC[[\ a$ **W^^.d;I9uE--!4.I9uE--<=.goo.,"&//*6? ??}''? ::gnni( ==!6?--"6?s A&G G cdD]h}t|}dfd tddvr|jd|nd|vr|j d||jd |jy#t$r |}YTwxYw) N)TF)compressctrk)rr-r)r func_raisez0TestsUtilsTest.testTraceBack..func_raises ,r)c0|r |dz yyrar-)i deep_functionrs rrz3TestsUtilsTest.testTraceBack..deep_functions1Q3\r)rzfail2ban-testcases>coverage:)r printrassertIn assertNotIn)rrtbsrrs @@r testTraceBackzTestsUtilsTest.testTraceBacksh8$2   -  1 $ MM#q 1_S!==a7   A sA11BBc<|jt||g|i|yrk)assertRaisesRegexAssertionError)rregexpfunrkwargss r_testAssertionErrorREz$TestsUtilsTest._testAssertionErrorREs $FtFvFr)cNd}jtjd|djtjdjdjdfdjtjdt djd fd y) Nct|rk)KeyErrorr|s r_key_errz;TestsUtilsTest.testExtendedAssertRaisesRE.._key_errs #r)^failed$failed __failed__z$failed.* does not match .*__failed__c>jdjdS)Nrr)rfailr'srz;TestsUtilsTest.testExtendedAssertRaisesRE..!s4 % %kII| r)r#rzJ0 AssertionError not raised X.* does not match .*AssertionError not raisedc.jdfdS)Nz^0 AssertionError not raised X$c2jdtdS)Nr#r)rintr'srrzMTestsUtilsTest.testExtendedAssertRaisesRE....)sD & &sC 3r))rr'srrz;TestsUtilsTest.testExtendedAssertRaisesRE..(s4 % %&H3 5r))rrrrrr)rrs` rtestExtendedAssertRaisesREz)TestsUtilsTest.testExtendedAssertRaisesREsH{ hN{II|D NsC,j5r)c"jdgdjddjddjdddDjdgdjddjddjdddDjdjddjd jd td jd jd ddDjdjddjdjdtd jdjd ddDt}j |j djdjdddjdddjdddjdddj |j djdjdddjddddtjjdc}tj_ jttjjdtj!tjjddj!tjjddj!tjjd d dtj_ j!tjjd!dj!tjjdd"j!tjjd d |tj_ jtjjdj |j djd#jdjd$jddjd%jdddjd&jdddjd'jdddd()jd*jdddjd+jdddd()jd$jdddj#d d d,gid d d,gij%t&j"d d d,gid d,d gij)d d-gd-d gj)d d-ggd-d ggd,.j)d d-ggd-d ggd/j%t&fd0j)d d d-gid d-d gid/j%t&fd1j)d2d-d3gd4d5gd6gd5d4gd3d-gd7d2gd/j)d2d-d3gd4d5gd6gd5d4gd3d-gd7d2gd8.j%t&fd9j)d:d;gdj%t&j(d=gd>gj%t&j(d=ggd>ggjd?j(d gd3d-gjd@j(d d-gd3d-gy#|tj_ wxYw)ANa)rbcdcbac3,K|] }|dk7s |ywrNr-rlrs rroz;TestsUtilsTest.testExtendedAssertMethods..1s3A!s(a3s )rrrcbdc3HK|]}|dk7s |jywrupperrs rroz;TestsUtilsTest.testExtendedAssertMethods..5s>qQ#X> ""z'a' unexpectedly found in 'cba'z#1 unexpectedly found in \[0, 1, 2\]rrz&'A' unexpectedly found in \['C', 'A'\]Ac3HK|]}|dk7s |jywrrrs rroz;TestsUtilsTest.testExtendedAssertMethods..<s@qCx1779@rz'a' was not found in 'xyz'xyzz5 was not found in \[0, 1, 2\]z!'A' was not found in \['C', 'B'\]c3HK|]}|dk7s |jyw)rNrrs rroz;TestsUtilsTest.testExtendedAssertMethods..Bs=aAH =rz test "xyz"rTrzyxFtest_zyxz xxxx "xxx"cyN2r-r-r)rrz:TestsUtilsTest.testExtendedAssertMethods..Tr)rg9@cyrr-r-r)rrz:TestsUtilsTest.testExtendedAssertMethods..Yrr)g@z.* was found in the logz+All of the .* were found present in the logzwas found in the logzwas not found in the logz&was not found in the log, waited 1e-06gư>)rwaitz"None among .* was found in the logz0None among .* was found in the log, waited 1e-06r B)level nestedOnlyc:jddggddggddSNrrrT)rrassertSortedEqualr'srrz:TestsUtilsTest.testExtendedAssertMethods..zs-D,B,B#J<3*Q4-C-9r)c>jdddgidddgiddSrrr'srrz:TestsUtilsTest.testExtendedAssertMethods..}s4D,B,B#ssS#J'qT-C-Cr)ZCEFrrrrcPjdddgddgdgddgddgddgd S) NrrrrrrrTrrr'srrz:TestsUtilsTest.testExtendedAssertMethods..sED,B,BSzc +,c3ZsCj/QSV.W-C-r)rA1A2ABCCBAz\['A'\] != \['C', 'B'\]z\['A', 'B'\] != \['B', 'C'\])rrrrangerrdebugrassertNotLoggedr=r>fast assertFalse isinstance maxWaitTimeboolrassertDictEqualrrrr)rrorgfasts` rtestExtendedAssertMethodsz(TestsUtilsTest.testExtendedAssertMethods,s--)*--*+--U--3343(3()33>E>??S%!CQa"FS@e@B:==#u>==!U1XA==#=5=? &--/,,|L!FEt,vu%0z5d3FEu---/,,||$vu%0vue6'||00%'8<< :hll66t8<<++B/48<<++C0$7(,,HLL,,Z8:B?HLL,,R0#6HLL,,S1488<<8<<++E23--/,,|7'Kv/4D28fe/FfeD:Bj%U4Pj%U?KE3aV}sQFm4ND$8$8!Q=3A-!#sc3Z03*c |1=3*c |FN%9:#Sz*S3*,=%PN%CD#c3ZsCjABCQT:]`be\fEgilDm#c3ZsCjABCQT:]`be\fEgilDm  N%t b!"t b!"ed5k2ND$:$:UGeWMND$:$:eWIyQ7C53*.<C:Sz3e8<<s /B]77^c|jtdd|jtddd|jtddddy)NrzA%(asctime)s %(name)-24s[%(process)d]: %(levelname)-7s %(message)sF)paddingz<%(asctime)s %(name)s[%(process)d]: %(levelname)s %(message)s)addtimerz0%(name)s[%(process)d]: %(levelname)s %(message)s)rrr's rtestVerbosityFormatz"TestsUtilsTest.testVerbosityFormatsU%a(FH%a7AC%aF57r)ct}t}d}td}tj|}|j |||j ||jd|j}|j|jjd|jd}|j|dkD|j|d|||dz|dzdzy) Nz %(tb)s | %(tbc)s : %(message)sfail2ban_testsXXXz: XXX| rr )rr rrS StreamHandler setFormatter addHandlererrorgetvaluerWr2endswithindexr)rstrout Formatterfmtroutrpindexs rtestFormatterWithTraceBackz)TestsUtilsTest.testFormatterWithTraceBacks :&$) *# % &&f%#9S>"C,,u oo!//!((*%%g./ 773<&//&2+1Wf:q&(Q,78r)ct}|jdtjj|j dddd|j dy)Nzlazy logging: %rrrr rznot all arguments converted)rrr=r>log_lazyrr)rrs rtestLazyLoggingzTestsUtilsTest.testLazyLoggingsA &,,!8<<#8#89--1a 12r)N) r*r+r,rrrrrrrrr/r2r-r)rrrs9>(T@G,d3L79(3r)rceZdZdZdZy) MyTimeTestctj}|j|dd|j|dd|j|dd|j|dd|j|dd |j|d d |j|d d |jt|d dz dz dz d|jt|ddz dz dz dy)N1y6mo30w15d12h35m25si]z2yy 3mo 4ww 10dd 5hh 30mm 20ssi$mz>2 years 3 months 4 weeks 10 days 5 hours 30 minutes 20 secondsz!1 year + 1 month - 1 week + 1 dayizI2 * 0.5 yea + 1*1 mon - 3*1/3 wee + 2/2 day - (2*12 hou 3*20 min 80 sec) g@Az(2*.5y+1*1mo-3*1/3w+2/2d-(2*12h3*20m80s) z!1ye -2mo -3we -4da -5ho -6mi -7seiQ pz1 month<gp>@z1 yeargv@)r str2secondsrfloat)rstr2secs rtestStr2SecondszMyTimeTest.testStr2Secondss   '712H=7;?J7fgist7EF S7>?J5+,r1B6;WE5*+b025:FCr)czd}|j|dd|j|dd|j|dd|j|dd |j|d d |j|d d |j|dd|j|dd|j|dd|j|dd|j|dd|j|dd|j|ddy)Nc>ttj|Srk)rHr seconds2str)rs rrz(MyTimeTest.testSec2Str..sc&,,Q/0r)i)z1y 3w 4di(z1y 3dg~A1yiz2w 1dit2wiz 2d 7h 15miz2d 1hg@z3h 31mi2hi 1hiz59m 50s;59sr0)r)rsec2strs r testSec2StrzMyTimeTest.testSec2Strs 0'79%*=79%':7<($778$':7;'$77/0+>7<(':79%(;78$$776?$777#)<72;%871:#6r)N)r*r+r,r<rHr-r)rr4r4s  D7r)r4)$ __author__ __copyright__ __license__rSr@rJr=rPrYrqriorutilsrrrhelpersr r r r rrrrr server.mytimerTestCaserr5r7r9rr4r-r)rrQs(" 7    :888">(##>@! Ta!!aH}3'}3@7""7r)