
    Gkc/$                     v    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y)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                 H    t         t        |           t               | _        y)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                 *    t         t        |           y)z Call after every test case N)r   r   tearDownr   s    r   r   zBeautifierTest.tearDown&   s    &(r   c                     dg}| j                   j                  |       | j                  | j                   j                         |       y )Ntest)r   setInputCmdassertEqualgetInputCmd)r   cmds     r   testGetInputCmdzBeautifierTest.testGetInputCmd*   s8    	#&&S466%%'-r   c                     | j                   j                  dg       | j                  | j                   j                  d      d       y )NpingpongzServer replied: pongr   r   r   beautifyr   s    r   testPingzBeautifierTest.testPing/   s4    &&fX466??6*,BCr   c                     | j                   j                  dg       | j                  | j                   j                  t              t               y )Nr   )r   r   r   r"   r   r#   s    r   testVersionzBeautifierTest.testVersion3   s3    &&i[!466??7+W5r   c                     | j                   j                  dg       | j                  | j                   j                  d      d       y )NaddsshzAdded jail sshr!   r#   s    r   testAddJailzBeautifierTest.testAddJail7   s4    &&eW466??5)+;<r   c                     | j                   j                  dg       | j                  | j                   j                  d       d       y )NstartzJail startedr!   r#   s    r   testStartJailzBeautifierTest.testStartJail;   s3    &&gY466??4(.9r   c                     | j                   j                  ddg       | j                  | j                   j                  d       d       y )Nstopr)   zJail stoppedr!   r#   s    r   testStopJailzBeautifierTest.testStopJail?   s5    &&fe_%466??4(.9r   c                     | j                   j                  dg       | j                  | j                   j                  d       d       y )Nr/   zShutdown successfulr!   r#   s    r   testShutdownzBeautifierTest.testShutdownC   s4    &&fX466??4(*?@r   c           
         | j                   j                  dg       ddddgff}d}| j                  | j                   j                  |      |       | 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                  | j                   j                  |      |       y )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&466??8,f5&&h&'   			!	"		
 		 
(& (&M&))&%%&22&N&))&%%&DD&466??8,f5r   c                     | j                   j                  dg       | j                  | j                   j                  d      d       y )N	flushlogszrolled overzlogs: rolled overr!   r#   s    r   testFlushLogszBeautifierTest.testFlushLogsl   s4    &&k]#466??=13FGr   c                     | j                   j                  ddg       d}| j                  | j                   j                  d      |       y )Ngetsyslogsocketz!Current syslog socket is:
`- autoautor!   r   r=   s     r   testSyslogSocketzBeautifierTest.testSyslogSocketp   s;    &&e^,-/&466??6*F3r   c                     | j                   j                  ddg       d}| j                  | j                   j                  d      |       y )NrC   	logtargetz3Current logging target is:
`- /var/log/fail2ban.logz/var/log/fail2ban.logr!   rF   s     r   testLogTargetzBeautifierTest.testLogTargetu   s<    &&e[)*A&466??#:;VDr   c                     | j                   j                  ddg       d}| j                  | j                   j                  d      |       y )NrC   loglevelzCurrent logging level is 'INFO'INFOr!   rF   s     r   testLogLevelzBeautifierTest.testLogLevelz   s;    &&eZ(),&466??6*F3r   c                     | j                   j                  ddg       d}d|z   }| j                  | j                   j                  |      |       | j                  | j                   j                  d       d       y )NrC   dbfilez"/var/lib/fail2ban/fail2ban.sqlite3zCurrent database file is:
`- Database currently disabledr!   r;   s      r   
testDbFilezBeautifierTest.testDbFile   sc    &&eX&'1(+h6&466??8,f5466??4(*GHr   c                     | j                   j                  ddg       d}| j                  | j                   j                  d      |       | j                  | j                   j                  d       d       y )NrC   
dbpurgeagez.Current database purge age is:
`- 86400secondsiQ rQ   r!   rF   s     r   testDbPurgeAgezBeautifierTest.testDbPurgeAge   sY    &&e\*+<&466??5)62466??4(*GHr   c                 8   | j                   j                  g d       g }d}| j                  | j                   j                  |      |       dg}d}| j                  | j                   j                  |      |       | j                   j                  g d       ddg}d}|dz  }| j                  | j                   j                  |      |       | j                   j                  g d	       dg}| j                  | j                   j                  |      |       y )
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(+&466??8,f5!"(A&466??8,f5&&GH!#67(0);;)466??8,i8&&GH!"(466??8,f5r   c                     | j                   j                  g d       d}| j                  | j                   j                  d      |       y )N)rC   rW   logencodingz%Current log encoding is set to:
UTF-8zUTF-8r!   rF   s     r   testLogEncodingzBeautifierTest.testLogEncoding   s7    &&343&466??7+V4r   c                    | j                   j                  g d       | j                  | j                   j                  g       d       | j                   j                  g d       ddgg}d}|dz  }| j                  | j                   j                  |      |       |j	                  dd	g       |d
z  }| j                  | j                   j                  |      |       | j                   j                  g d       |j                  |d          | j                  | j                   j                  |      |j                  d      d          y )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   s   &&45466??2&(EF&&78/0($&((&466??8,f5
//7F#$O&466??8,f5&&78
//(1+466??8,fll5.A!.DEr   c                 Z   | j                   j                  g d       d}d}| j                  | j                   j                  d       |dz          | j                  | j                   j                  |      |dz          | j                  | j                   j                  d      |dz          y )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4*&((466??4(
!466??8,
  "466??#34
]r   c                    | j                   j                  g d       d}| j                  | j                   j                  g       |       | j                   j                  g d       t	        dd      t	        d      t	        dd      t	        d	      g}d
}|dz  }|dz  }|dz  }|dz  }| j                  | j                   j                  |      |       y )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1-&466??2&/&&34	+q	%=	,	
	( 8&&J&  &M&466??8,f5r   c                     | j                   j                  g d       d}| j                  | j                   j                  g       |       d}|dz  }| j                  | j                   j                  ddg      |       y )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   sh    &&12-&466??2&/<&$$&466??D$<0&9r   c                     | j                   j                  g d       d}| j                  | j                   j                  g       |       d}|dz  }| j                  | j                   j                  dg      |       y )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   sh    &&/0%&466??2&/7&  &466??$8#9:FCr   c                     | j                   j                  g d       d}| j                  | j                   j                  g       |       d}|dz  }d}| j                  | j                   j                  |      |       y )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   si    &&DE8&466??2&/H&&&&)(466??8,f5r   c                     | j                   j                  g d       d}| j                  | j                   j                  g       |       d}|dz  }| j                  | j                   j                  ddg      |       y )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   sh    &&AB5&466??2&/G&L&466??E7#34f=r   c                 N   t        d      }d}| j                  | j                  j                  |      |       t	        d      }d}| j                  | j                  j                  |      |       d}| j                  | j                  j                  t                     |       y )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&466''16:#F+(+&466''16:-&466''
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6=::A#6JH4
E
4
II6&5
F$	6&:D6>
?r   r   )
__author____copyright____license__unittestclient.beautifierr   r   server.ipdnsr   
exceptionsr   r	   TestCaser    r   r   <module>r      s9   (  
P  *  ! Eh?X&& h?r   