HEX
Server: Apache/2.4.65 (Debian)
System: Linux kubikelcreative 5.10.0-35-amd64 #1 SMP Debian 5.10.237-1 (2025-05-19) x86_64
User: www-data (33)
PHP: 8.4.13
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/fail2ban/tests/__pycache__/actionstestcase.cpython-39.pyc
a

��_�I�@s�dZdZdZddlZddlZddlZddlmZddlm	Z	dd	l
mZdd
lm
Z
mZmZmZej�ej�e�d�ZGdd
�d
e
�ZdS)zDaniel BlackzCopyright (c) 2013 Daniel BlackZGPL�N�)�
FailTicket)�Utils�)�	DummyJail)�LogCaptureTestCase�
with_alt_time�with_tmpdir�MyTime�filescs�eZdZ�fdd�Z�fdd�Zifdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Ze
dd��Zdd�Zdd�Ze
edd���Z�ZS)�ExecuteActionscs$tt|���t�|_|jj|_dS)zCall before every test case.N)�superr�setUpr�_ExecuteActions__jailZactions�_ExecuteActions__actions��self��	__class__��@/usr/lib/python3/dist-packages/fail2ban/tests/actionstestcase.pyr)szExecuteActions.setUpcstt|���dS)N)r
r�tearDownrrrrr/szExecuteActions.tearDowncCs�|j�d�|jd}d|�dd�|_d|�dd�|_d|�dd�|_d	|�d
d�|_d|�dd�|_d
|�dd�|_|S)N�ipz
echo ip start�start�zecho ip ban <ip>�banzecho ip unban <ip>Zunbanz
echo ip check�checkz
echo ip flush�flushzecho ip stop�stop)	r�add�getZactionstart�	actionbanZactionunban�actioncheck�actionflushZ
actionstop)r�o�actrrr�
defaultAction2s
zExecuteActions.defaultActioncCs"|j�d�|�t|jjd�dS)N�test)rr�assertRaises�
ValueErrorrrrr�testActionsAddDuplicateName=sz*ExecuteActions.testActionsAddDuplicateNamecCs�|j�d�|�|jd�|�d|j�|�d|j�|j�d�|jd=|jd=|�d|j�|�t|j�d�|j�d�|�|j��d�|�	t
|jjd�dS)Nr'znonexistant actionZtest1r�z	127.0.0.1)rr�
assertTrueZassertInZassertNotIn�assertEqual�len�
setBanTimeZ
getBanTimer(r)�removeBannedIPrrrr�testActionsManipulationAsz&ExecuteActions.testActionsManipulationcCsj|�|j�d�d�|�d�|��|�|j�gd��d�|�d�|�d�|�d�|�d�dS)	N�	192.0.2.1r�
Ban 192.0.2.1)r2z	192.0.2.2z	192.0.2.3r�192.0.2.1 already bannedz
Ban 192.0.2.2z
Ban 192.0.2.3)r-r�addBannedIP�assertLogged�pruneLog�assertNotLoggedrrrr�testAddBannedIPPs



zExecuteActions.testAddBannedIPcCs�|��|j��|jdddd�|j��|j��|�dddd�|�|j��ddd	gfg�|�|j�d
�ddg�dS)N�
stdout: %r�ip startT��waitzip flush�ip stop)zCurrently bannedr)zTotal bannedrzBanned IP listZshort)r&rrr6r�joinr-Zstatusrrrr�testActionsOutputZs


��z ExecuteActions.testActionsOutputc
s��j�dtj�td�ddi���d��j����t	�
�fdd�d���j���j����d	���t
�jjd
di��j�dtj�td�dd
d����t�jjdtj�td�dd
dd����t�jjdtj�td�i�dS)N�Actionzaction.d/action.py�opt1�valuezTestAction initialisedcs
��d�S)NzTestAction action start�Z
_is_loggedrrrr�<lambda>n�z4ExecuteActions.testAddActionPython.<locals>.<lambda>�zTestAction action stopZAction3z/does/not/exist.pyZAction4Zvalue2)rB�opt2ZAction5Zvalue3)rBrHZopt3)rr�os�pathr?�TEST_FILES_DIRr6rr,r�wait_forrr(�IOError�	TypeErrorrrrr�testAddActionPythonfs4�




��

�
�z"ExecuteActions.testAddActionPythonc	s���t�jjdtj�td�i���t�jjdtj�td�i��j�dtj�td�i��j����	t
��fdd�d���j���j����
d�dS)	NrAzaction.d/action_noAction.pyzaction.d/action_nomethod.pyzaction.d/action_errors.pycs
��d�S)NzFailed to startrDrrrrrE�rFz7ExecuteActions.testAddPythonActionNOK.<locals>.<lambda>rGzFailed to stop)r(�RuntimeErrorrrrIrJr?rKrr,rrLrr6rrrr�testAddPythonActionNOK�s.
��
��
��


z%ExecuteActions.testAddPythonActionNOKcCs�|j�dtj�td�i�|j�dtj�td�i�|j�td��|j�	�|�
d�|�d�|�d�|j��|�
d�|�d	�|�d
�dS)NZaction1zaction.d/action_modifyainfo.pyZaction2z1.2.3.4�Failed to execute banzaction1 ban deleted aInfo IPzaction2 ban deleted aInfo IPzFailed to execute unbanzaction1 unban deleted aInfo IPzaction2 unban deleted aInfo IP)
rrrIrJr?rKr�
putFailTicketrZ_Actions__checkBanr8r6�_Actions__flushBanrrrr�testBanActionsAInfo�s&��






z"ExecuteActions.testBanActionsAInfocCs�d|j_d|j_|j�d�|j��t�d�d}|dkr`d|}|j�t	|d��|d7}q4|j
dd	d
�t�d�|dkr�d|}|j�t	|d��|d7}qx|j
d
d	d
�|j��|j��|�
d�|�
d�dS)NrG��dr�z
192.0.2.%drz / 20,Tr<���2z / 50,zUnbanned 30, 0 ticket(s)zUnbanned 50, 0 ticket(s))rZ
banPrecedenceZ
unbanMaxCountr/rr
�setTimerrSrr6rr?r8)r�irrrr�testUnbanOnBusyBanBombing�s*







z(ExecuteActions.testUnbanOnBusyBanBombingcCs�|�ddd��}|jd|d<d|_|j��|�dd�|�|j�d�d	�|�|j�d
�d	�|jdddddd
ddddd�|�	d�|j
d|d<|j�d
�|jddd�|�	d�|j��|jddddddddddddd�	|�	d�|j
|d<|�|j�d
�d	�|jdddddddd�|jdddd�|�	d�|j��|jddddddddd�|jddddddd dd�|�	d!�|j|d<|j�
�|j��|jddddd"ddd�|jd#ddd$dd�dS)%N�	 <family>)rr�; exit 1�actionflush?family=inet6Tr:r;r2r�2001:db8::1r3�Ban 2001:db8::1�ip ban 192.0.2.1�ip ban 2001:db8::1��allr=�>[test-phase 1a] simulate inconsistent irreparable env by unban�actioncheck?family=inet6z,Invariant check failed. Unban is impossible.r<z>[test-phase 1b] simulate inconsistent irreparable env by flush�ip flush inet4�ip flush inet6�Failed to flush bans�'No flush occurred, do consistency check�<Invariant check failed. Trying to restore a sane environmentr>�[test-phase 2] consistent env�ip check inet4�rf�-[test-phase 3] failed flush in consistent env�ip check inet6�Unable to restore environment�![test-phase end] flush successful�action ip terminated�ERROR�Unban tickets each individualy)r&r#�actionstart_on_demandrrr8r-r5r6r7r"r0rTrr?�rr%rrr�testActionsConsistencyCheck�s�
�
�

�

�
�

��



��z*ExecuteActions.testActionsConsistencyCheckcCs�|�ddddd��}|jd|d<d|_d|_|j��|�dd�|�|j�d�d	�|�|j�d
�d	�|j	dddd
ddddddddd�|j
d|d<|�d�|j�d
�|j	dddddd�|jdddddd
dd�|�d�|�|j�d
�d	�|j	dddddddddddd�|jdddd
dd�|j
|d<|�|j�d�d	�|j
d|d<|�d�|j�
�|j	ddddd d!dddd"ddd�	|jdddd�|�d#�|j
|d<|�|j�d
�d	�|j	dddddddd�|jdd$dd
dd�|�d%�|j
|d<|j�
�|j	d d!ddddddd�|jdddddd
dddddd�|�d&�|j|d<|j��|j��|j	ddddddd'ddd�|jd(ddd)dd�dS)*Nr^)rrrrr_r`Tr:r;r2rrar3rb�ip start inet4rc�ip start inet6rdrerhrgrmz
ip stop inet6z
ip stop inet4rpz<[test-phase 1b] simulate inconsistent irreparable env by banrrrsrR�2001:db8::2z>[test-phase 1c] simulate inconsistent irreparable env by flushrirjrkrlzFailed to flush bans in jailrnrorqrtrurvrw)r&r#rxZactionrepair_on_unbanrrr8r-r5r6r"r7r0rTrr?ryrrr�"testActionsConsistencyCheckDiffFam"s�
�
��
��


�
�

��


��	



��z1ExecuteActions.testActionsConsistencyCheckDiffFamcCs�|�dddddd��}|d|d<d	|_d
|_d|_|�d�|�|j�d
dg�d�|jddddddddddd	d�|�d�|�	|d�|�d�|�
d�|��|�	|d�|�d�|�
d�t�t�
�d�|�d�|�|j�gd��d �|jd!d"d#dd$dd%d	d�|�	|d�|�	|d�|j
d&d'dddd(dddd)d*d+d	d�	t�t�
�d�t�|d�t�|d�|�d,�|�|j�d
dg�d�|jd-dd.dd/d0d1dd(dd)d	d�|�d2�|�|j�d3�d �|jd4dd5d	d�|j
d-dd.dd/d0d1dd(dd)d	d�|�d6�|�	|d�|�d7�|�
d8�|��|�	|d�|jd8d9d	d�|�
d7�d:|_d;|_|�|j�td
d<��d<�|jd=d>d	d�dS)?Nz <family>; touch "<FN>"z <family>; test -f "<FN>"z <family>; echo -n "" > "<FN>"z <family>; rm -f "<FN>"z) <family>; echo "<ip> <family>" >> "<FN>")rrrrrz	/<family>ZFNTz%echo ip repair <family>; touch "<FN>"zGecho ip reban <ip> <family>; echo "<ip> <family> -- rebanned" >> "<FN>"z[test-phase 0] initial banr2rarr3rbr:r{zip ban 192.0.2.1 inet4r|zip ban 2001:db8::1 inet6rpz[test-phase 1] check banz/inet4z192.0.2.1 inet4z2001:db8::1 inet6z/inet6�z#[test-phase 2] check already banned)r2rar}rr4z2001:db8::1 already bannedzBan 2001:db8::2rorrzReban 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 repairedrmzip 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)r}zReban 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 -- rebannedrzexit 1rzFailed to execute rebanzError banning 192.0.2.1)r&rxZactionrepairZactionrebanr7r-rr5r6ZdumpFiler8r
r[�timerI�remover!Z_Actions__reBanr)r�tmpr%rrr�!testActionsRebanBrokenAfterRepair�s��
�





��

�

��



�
�z0ExecuteActions.testActionsRebanBrokenAfterRepair)�__name__�
__module__�__qualname__rrr&r*r1r9r@rOrQrUrr]rzr~r	r��
__classcell__rrrrr's"

%Kor)�
__author__Z
__copyright__Z__license__r�rIZtempfileZ
server.ticketrZserver.utilsrZ	dummyjailrZutilsrrr	r
rJr?�dirname�__file__rKrrrrr�<module>s