GkcJdZdZdZddlZddlZddlZddlmZddlm Z dd l m Z dd l m Z mZmZmZej"j%ej"j'ed ZGd d e Zy)z Daniel BlackzCopyright (c) 2013 Daniel BlackGPLN) FailTicket)Utils) DummyJail)LogCaptureTestCase with_alt_time with_tmpdirMyTimefilesceZdZfdZfdZifdZdZdZdZdZ dZ d Z d Z e d Zd Zd Ze edZxZS)ExecuteActionsc~tt| t|_|jj |_y)zCall before every test case.N)superrsetUpr_ExecuteActions__jailactions_ExecuteActions__actionsself __class__s @/usr/lib/python3/dist-packages/fail2ban/tests/actionstestcase.pyrzExecuteActions.setUp)s*#% $+;;&&$.c*tt| y)N)rrtearDownrs rrzExecuteActions.tearDown/s&(rc|jjd|jd}d|jddz|_d|jddz|_d|jddz|_d |jd dz|_d |jd dz|_d |jddz|_|S)Nipz echo ip startstartzecho ip ban banzecho ip unban unbanz echo ip checkcheckz echo ip flushflushz echo ip stopstop) raddget actionstart actionban actionunban actioncheck actionflush actionstop)roacts r defaultActionzExecuteActions.defaultAction2s..T t##AEE'2$66#/$QUU5"%55#-(w);;#/#AEE'2$66#/#AEE'2$66#/!!%%"33#. *rc|jjd|jt|jjdy)Ntest)rr& assertRaises ValueErrorrs rtestActionsAddDuplicateNamez*ExecuteActions.testActionsAddDuplicateName=s0..VJ 2 2F;rc|jjd|j|jd|jd|j|j d|j|jjd|jd=|jd=|j d|j|j t |jd|jjd|j |jjd|jt|jjdy)Nr2znonexistant actiontest1rz 127.0.0.1) rr& assertTrueassertIn assertNotIn assertEquallen setBanTime getBanTimer3r4removeBannedIPr5s rtestActionsManipulationz&ExecuteActions.testActionsManipulationAs..V//$..()--''8..W nnV nnW64>>*3t~~&*..C 4>>,,.s3J = ={Krc~|j|jjdd|jd|j |j|jjgdd|jd|j d|jd|jdy) N 192.0.2.1r Ban 192.0.2.1)rDz 192.0.2.2z 192.0.2.3r192.0.2.1 already bannedz Ban 192.0.2.2z Ban 192.0.2.3)r=r addBannedIP assertLoggedpruneLogassertNotLoggedr5s rtestAddBannedIPzExecuteActions.testAddBannedIPPs4>>--k:A>O$--/4>>--.UVXYZ./'O$O$rc|j|jj|jddzd|jj |jj |jddzddz|j |jjddd gfg|j |jjd ddgy) N stdout: %rip startTwaitzip fluship stop)zCurrently bannedr)z Total bannedrzBanned IP listshort)r0rrrHr%joinr=statusr5s rtestActionsOutputz ExecuteActions.testActionsOutputZs..L:-D9....L:-|i/GH4>>((*,D#&6%<,>?4>>((13K#3%&rc jjdtjj t dddij djjjtjfddjjjj j djtjjd d ijjd tjj t ddd d jtjjdtjj t ddd ddjtjjdtjj t diy)NActionzaction.d/action.pyopt1valuezTestAction initialisedc&jdS)NzTestAction action start _is_loggedr5srz4ExecuteActions.testAddActionPython..ns$//:S*TrzTestAction action stopAction3z/does/not/exist.pyAction4value2)rXopt2Action5value3)rXrbopt3)rr&ospathrSTEST_FILES_DIRrHrr:rwait_forr%r3IOError TypeErrorr5s`rtestAddActionPythonz"ExecuteActions.testAddActionPythonfsd.. RWW\\.*> ? G,-..//5>>"TVWXZ....,-G>>y"6<.. bggll>+?@ X&( dnn  )77<< 45 Xx8:  dnn  )77<< 45r;rc jtjjdtj j tdijtjjdtj j tdijjdtj j tdijjjtjfddjjjj jdy)NrWzaction.d/action_noAction.pyzaction.d/action_nomethod.pyzaction.d/action_errors.pyc&jdS)NzFailed to startr[r5srr]z7ExecuteActions.testAddPythonActionNOK..s$//:K*Lrr^zFailed to stop)r3 RuntimeErrorrr&rfrgrSrhrr:rrir%rHr5s`rtestAddPythonActionNOKz%ExecuteActions.testAddPythonActionNOKsL$.."4"4 RWW\\.!#L$.."4"4 RWW\\.!#.. RWW\\.!..//5>>"LaPR....$%rc||jjdtjj t di|jjdtjj t di|j jtd|jj|jd|jd|jd|jj|jd|jd |jd y) Naction1zaction.d/action_modifyainfo.pyaction2z1.2.3.4Failed to execute banzaction1 ban deleted aInfo IPzaction2 ban deleted aInfo IPzFailed to execute unbanzaction1 unban deleted aInfo IPzaction2 unban deleted aInfo IP) rr&rfrgrSrhr putFailTicketr_Actions__checkBanrJrH_Actions__flushBanr5s rtestBanActionsAInfoz"ExecuteActions.testBanActionsAInfos.. 77<< @A.. 77<< @A++Jy12..##%./2323..##%014545rcd|j_d|j_|jjd|jj t j dd}|dkr5d|z}|jjt|d|dz }|dkr5|jdd t j d |d kr5d|z}|jjt|d |dz }|d kr5|jd d |jj|jj|jd|jdy)Nr^drz 192.0.2.%drz / 20,TrO2z / 50,zUnbanned 30, 0 ticket(s)zUnbanned 50, 0 ticket(s))r banPrecedence unbanMaxCountr?rr setTimerrurrHr%rSrJ)rirs rtestUnbanOnBusyBanBombingz(ExecuteActions.testUnbanOnBusyBanBombings;"#$..!"$....C ....! Bq2;;ZA./61 B H4(.. Bq2;;ZC0161 B H4(...../12rc |jddd}|jdz|d<d|_d|jz|_d|jz|_|j j |jddz|j|j jd d |j|j jd d |jd d ddzddzddzdd|jd|jdz|d<|j jd |jdd|jd|j j|jddzddzdddddzddd |jd|j|d<|j|j jd d |jd ddzddzdd|jddzd|jd|j j|jddddzdd zdd|jddzddzddzd!d|jd"|j|d<|j j|j j!|jddzddzd#dd|jd$ddzd%dy)&N )r#r$; exit 1actionflush?family=inet6T ; rMrNrDr 2001:db8::1rEBan 2001:db8::1ip ban 192.0.2.1ip ban 2001:db8::1allrP>[test-phase 1a] simulate inconsistent irreparable env by unbanactioncheck?family=inet6z,Invariant check failed. Unban is impossible.rOz>[test-phase 1b] simulate inconsistent irreparable env by fluship flush inet4ip flush inet6Failed to flush bans'No flush occurred, do consistency check>--k:A>4>>--m>--m[test-phase 1c] simulate inconsistent irreparable env by flushrrrrzFailed to flush bans in jailrrrrrrr)r0r,ractionrepair_on_unbanr)r*rrrJr=rGrHr+rIrArwr%rSrs r"testActionsConsistencyCheckDiffFamz1ExecuteActions.testActionsConsistencyCheckDiffFam%s Kkbmno#$'OOj$@# !"#"#$s}}4#-&8#/..|j014>>--k:A>4>>--m>--m>--m>--md y)?Nz ; touch ""z ; test -f ""z ; echo -n "" > ""z ; rm -f ""z) ; echo " " >> "")rr#r$r%r!z /FNTz%echo ip repair ; touch ""zGecho ip reban ; echo " -- rebanned" >> ""z[test-phase 0] initial banrDrrrErrMrzip ban 192.0.2.1 inet4rzip ban 2001:db8::1 inet6rz[test-phase 1] check banz/inet4z192.0.2.1 inet4z2001:db8::1 inet6z/inet6z#[test-phase 2] check already banned)rDrrrrFz2001:db8::1 already bannedzBan 2001:db8::2rrzReban 192.0.2.1zReban 2001:db8::1zip reban 192.0.2.1 inet4zip reban 2001:db8::1 inet6z192.0.2.1 inet4 -- repairedz2001:db8::1 inet6 -- repairedz3[test-phase 3a] check reban after sane env repairedrzip repair inet4zip repair inet6zReban 192.0.2.1, action 'ip'zReban 2001:db8::1, action 'ip'z>[test-phase 3a] check reban by epoch mismatch (without repair)rzReban 2001:db8::2, action 'ip'zip reban 2001:db8::2 inet6z[test-phase 4] check rebanz192.0.2.1 inet4 -- rebannedz2001:db8::1 inet6 -- rebannedz2001:db8::2 inet6 -- rebannedr zexit 1rzFailed to execute rebanzError banning 192.0.2.1)r0r actionrepair actionrebanrIr=rrGrHdumpFilerJr rtimerfremover)_Actions__reBanr)rtmpr/s r!testActionsRebanBrokenAfterRepairz0ExecuteActions.testActionsRebanBrokenAfterRepairs'  $ & + $ 6  #+o#d)"#<#]#/--,-4>>--{M.JKQOO%6""**"",,  --*+--H %&*+--/--H '(() .."#--564>>--.YZ\]^;=N""""  --H --H (*=**,,,,.. "   .."#))CL))CL--EF4>>--{M.JKQOA####!#C,,..  --PQ4>>--m>11*[!2LMqQ$(r)__name__ __module__ __qualname__rrr0r6rBrKrUrlrprxr rrrr r __classcell__)rs@rrr's|' )  < L% &;>&&62#3#3JL \p d g(g(rr) __author__ __copyright__ __license__rrftempfile server.ticketr server.utilsr dummyjailrutilsr r r r rgrSdirname__file__rhrrrrs^. 1  & IIbggooh7AY('Y(r