
    Gkc/$                     t    d Z dZdZddlZddlmZ ddlmZ ddlmZ dd	l	m
Z
mZ  G d
 dej                  ZdS )zAlexander Koeppez>Copyright (c) 2016 Cyril Jaquier, 2011-2013 Yaroslav HalchenkoGPL    N   )
Beautifier)version)IPAddr)UnknownJailExceptionDuplicateJailExceptionc                        e Zd Z fdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )BeautifierTestc                 |    t          t          |                                            t                      | _        dS )z Call before every test case N)superr   setUpr   bself	__class__s    I/usr/lib/python3/dist-packages/fail2ban/tests/clientbeautifiertestcase.pyr   zBeautifierTest.setUp!   s.    ##%%%<<$&&&    c                 V    t          t          |                                            dS )z Call after every test case N)r   r   tearDownr   s    r   r   zBeautifierTest.tearDown&   s%    &&(((((r   c                     dg}| j                             |           |                     | j                                         |           d S )Ntest)r   setInputCmdassertEqualgetInputCmd)r   cmds     r   testGetInputCmdzBeautifierTest.testGetInputCmd*   sI    	#&S46%%''-----r   c                     | j                             dg           |                     | j                             d          d           d S )NpingpongzServer replied: pongr   r   r   beautifyr   s    r   testPingzBeautifierTest.testPing/   sE    &fX46??6**,BCCCCCr   c                     | j                             dg           |                     | j                             t                    t                     d S )Nr   )r   r   r   r"   r   r#   s    r   testVersionzBeautifierTest.testVersion3   sD    &i[!!!46??7++W55555r   c                     | j                             dg           |                     | j                             d          d           d S )NaddsshzAdded jail sshr!   r#   s    r   testAddJailzBeautifierTest.testAddJail7   sE    &eW46??5))+;<<<<<r   c                     | j                             dg           |                     | j                             d           d           d S )NstartzJail startedr!   r#   s    r   testStartJailzBeautifierTest.testStartJail;   sD    &gY46??4((.99999r   c                     | j                             ddg           |                     | j                             d           d           d S )Nstopr)   zJail stoppedr!   r#   s    r   testStopJailzBeautifierTest.testStopJail?   sF    &fe_%%%46??4((.99999r   c                     | j                             dg           |                     | j                             d           d           d S )Nr/   zShutdown successfulr!   r#   s    r   testShutdownzBeautifierTest.testShutdownC   sE    &fX46??4((*?@@@@@r   c           
         | j                             dg           ddddgff}d}|                     | j                             |          |           | j                             ddg           dg dfd	d
ddt	          d          t	          d          t	          d          gfgff}d}|dz  }|dz  }|dz  }|dz  }|dz  }|dz  }|dz  }|dz  }|                     | j                             |          |           d S )Nstatus)zNumber of jailsr   z	Jail listr)   exim4z4Status
|- Number of jails:	0
`- Jail list:	ssh exim4Filter))zCurrently failedr   )zTotal failedr   )z	File list/var/log/auth.logActions)zCurrently banned   )zTotal bannedr9   zBanned IP listz192.168.0.1z::ffff:10.2.2.1z2001:db8::1zStatus for the jail: ssh
z
|- Filter
z|  |- Currently failed:	0
z|  |- Total failed:	0
z#|  `- File list:	/var/log/auth.log
z`- Actions
z   |- Currently banned:	3
z   |- Total banned:	3
z6   `- Banned IP list:	192.168.0.1 10.2.2.1 2001:db8::1r   r   r   r"   r   r   responseoutputs      r   
testStatuszBeautifierTest.testStatusG   s[   &hZ   ${UG4D&EF(E&46??8,,f555&h&'''     				!	"	"			
 		 
(& (&M&))&%%&22&N&))&%%&DD&46??8,,f55555r   c                     | j                             dg           |                     | j                             d          d           d S )N	flushlogszrolled overzlogs: rolled overr!   r#   s    r   testFlushLogszBeautifierTest.testFlushLogsl   sE    &k]###46??=113FGGGGGr   c                     | j                             ddg           d}|                     | j                             d          |           d S )Ngetsyslogsocketz!Current syslog socket is:
`- autoautor!   r   r=   s     r   testSyslogSocketzBeautifierTest.testSyslogSocketp   sL    &e^,---/&46??6**F33333r   c                     | j                             ddg           d}|                     | j                             d          |           d S )NrC   	logtargetz3Current logging target is:
`- /var/log/fail2ban.logz/var/log/fail2ban.logr!   rF   s     r   testLogTargetzBeautifierTest.testLogTargetu   sM    &e[)***A&46??#:;;VDDDDDr   c                     | j                             ddg           d}|                     | j                             d          |           d S )NrC   loglevelzCurrent logging level is 'INFO'INFOr!   rF   s     r   testLogLevelzBeautifierTest.testLogLevelz   sL    &eZ())),&46??6**F33333r   c                    | j                             ddg           d}d|z   }|                     | j                             |          |           |                     | j                             d           d           d S )NrC   dbfilez"/var/lib/fail2ban/fail2ban.sqlite3zCurrent database file is:
`- Database currently disabledr!   r;   s      r   
testDbFilezBeautifierTest.testDbFile   s{    &eX&'''1(+h6&46??8,,f55546??4((*GHHHHHr   c                     | j                             ddg           d}|                     | j                             d          |           |                     | j                             d           d           d S )NrC   
dbpurgeagez.Current database purge age is:
`- 86400secondsiQ rQ   r!   rF   s     r   testDbPurgeAgezBeautifierTest.testDbPurgeAge   sq    &e\*+++<&46??5))622246??4((*GHHHHHr   c                 L   | j                             g d           g }d}|                     | j                             |          |           dg}d}|                     | j                             |          |           | j                             g d           ddg}d}|dz  }|                     | j                             |          |           | j                             g d	           dg}|                     | j                             |          |           d S )
N)rC   sshdlogpathzNo file is currently monitoredr7   z3Current monitored log file(s):
`- /var/log/auth.log)setrW   
addlogpath/var/log/messagesr[   zCurrent monitored log file(s):
z)|- /var/log/messages
`- /var/log/auth.log)rY   rW   
dellogpathr[   r!   )r   r<   r=   	outputadds       r   testLogPathzBeautifierTest.testLogPath   s)   &///000(+&46??8,,f555!"(A&46??8,,f555&GGGHHH!#67(0);;)46??8,,i888&GGGHHH!"(46??8,,f55555r   c                     | j                             g d           d}|                     | j                             d          |           d S )N)rC   rW   logencodingz%Current log encoding is set to:
UTF-8zUTF-8r!   rF   s     r   testLogEncodingzBeautifierTest.testLogEncoding   sN    &3334443&46??7++V44444r   c                    | j                             g d           |                     | j                             g           d           | j                             g d           ddgg}d}|dz  }|                     | j                             |          |           |                    dd	g           |d
z  }|                     | j                             |          |           | j                             g d           |                    |d                    |                     | j                             |          |                    d          d                    d S )N)rC   rW   journalmatchzNo journal match filter set)rY   rW   addjournalmatch_SYSTEMD_UNITzsshd.servicezCurrent match filter:
z_SYSTEMD_UNIT sshd.service_COMMrW   z + _COMM sshd)rY   rW   deljournalmatch   z + r   )r   r   r   r"   appendremovesplitr;   s      r   testJournalMatchzBeautifierTest.testJournalMatch   sU   &44455546??2&&(EFFF&777888/0($&((&46??8,,f555
//7F#$$$O&46??8,,f555&777888
//(1+46??8,,fll5.A.A!.DEEEEEr   c                 l   | j                             g d           d}d}|                     | j                             d           |dz              |                     | j                             |          |dz              |                     | j                             d          |dz              d S )N)rC   rW   datepatternzCurrent date pattern set to: )NDefault DetectorszNot set/requiredro   )r   r   ztest (test)r!   r   r=   r<   s      r   testDatePatternzBeautifierTest.testDatePattern   s    &333444*&((46??4((
! ! !46??8,,
  " " "46??#344
]    r   c                    | j                             g d           d}|                     | j                             g           |           | j                             g d           t	          dd          t	          d          t	          dd          t	          d	          g}d
}|dz  }|dz  }|dz  }|dz  }|                     | j                             |          |           d S )N)rC   rW   ignoreipz No IP address/network is ignored)rY   rW   addignoreipz	127.0.0.0   z::1z
2001:db8::    z::ffff:10.0.2.1z)These IP addresses/networks are ignored:
z|- 127.0.0.0/8
z|- ::1
z|- 2001:db8::/32
z`- 10.0.2.1r:   rp   s      r   testIgnoreIPzBeautifierTest.testIgnoreIP   s    &000111-&46??2&&///&333444	+q	%==	,	
	( 8&&J&  &M&46??8,,f55555r   c                    | j                             g d           d}|                     | j                             g           |           d}|dz  }|                     | j                             ddg          |           d S )N)rC   rW   	failregexz No regular expression is definedz.The following regular expression are defined:
z|- [0]: ^$
`- [1]: .*z^$z.*r!   rF   s     r   testFailRegexzBeautifierTest.testFailRegex   s    &111222-&46??2&&///<&$$&46??D$<00&99999r   c                 
   | j                             g d           d}|                     | j                             g           |           d}|dz  }|                     | j                             dg          |           d S )N)rC   rW   actionszNo actions for jail sshdz)The jail sshd has the following actions:
ziptables-multiportr!   rF   s     r   testActionszBeautifierTest.testActions   s    &///000%&46??2&&///7&  &46??$8#9::FCCCCCr   c                    | j                             g d           d}|                     | j                             g           |           d}|dz  }d}|                     | j                             |          |           d S )N)rC   rW   actionpropertiesiptablesz+No properties for jail sshd action iptablesz;The jail sshd action iptables has the following properties:z
actionban, actionunban)	actionbanactionunbanr!   rp   s      r   testActionPropertiesz#BeautifierTest.testActionProperties   s    &DDDEEE8&46??2&&///H&&&&)(46??8,,f55555r   c                    | j                             g d           d}|                     | j                             g           |           d}|dz  }|                     | j                             ddg          |           d S )N)rC   rW   actionmethodsr   z(No methods for jail sshd action iptablesz9The jail sshd action iptables has the following methods:
z
ban, unbanbanunbanr!   rF   s     r   testActionMethodsz BeautifierTest.testActionMethods   s    &AAABBB5&46??2&&///G&L&46??E7#344f=====r   c                 z   t          d          }d}|                     | j                            |          |           t	          d          }d}|                     | j                            |          |           d}|                     | j                            t                                |           d S )NrW   z(Sorry but the jail 'sshd' does not existzThe jail 'sshd' already existsz Sorry but the command is invalid)r   r   r   beautifyErrorr	   
IndexErrorr;   s      r   testBeautifyErrorz BeautifierTest.testBeautifyError   s    !&))(5&46''116:::#F++(+&46''116:::-&46''
55v>>>>>r   )__name__
__module____qualname__r   r   r   r$   r&   r*   r-   r0   r2   r>   rA   rG   rJ   rN   rR   rU   r^   ra   rl   rq   rw   rz   r}   r   r   r   __classcell__)r   s   @r   r   r      s           
) ) ) ) ). . .
D D D6 6 6= = =: : :: : :A A A#6 #6 #6JH H H4 4 4
E E E
4 4 4
I I II I I6 6 6&5 5 5
F F F$	 	 	6 6 6&: : :D D D6 6 6> > >
? 
? 
? 
? 
? 
? 
?r   r   )
__author____copyright____license__unittestclient.beautifierr   r   server.ipdnsr   
exceptionsr   r	   TestCaser    r   r   <module>r      s   (  
P  * * * * * *       ! ! ! ! ! ! E E E E E E E Eh? h? h? h? h?X& h? h? h? h? h?r   