2011-10-07 19:47:50 +00:00
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 noet :
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
# This file is part of Fail2Ban.
|
|
|
|
#
|
|
|
|
# Fail2Ban is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Fail2Ban is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Fail2Ban; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
2006-07-16 22:21:58 +00:00
|
|
|
# $Revision$
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2006-07-16 22:21:58 +00:00
|
|
|
__version__ = "$Revision$"
|
|
|
|
__date__ = "$Date$"
|
2006-06-26 20:05:00 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
|
|
|
import unittest, socket, time
|
|
|
|
from server.server import Server
|
|
|
|
|
|
|
|
class StartStop(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Call before every test case."""
|
2006-09-25 17:03:48 +00:00
|
|
|
self.__server = Server()
|
|
|
|
self.__server.setLogLevel(0)
|
|
|
|
self.__server.start(False)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Call after every test case."""
|
2006-09-25 17:03:48 +00:00
|
|
|
self.__server.quit()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def testStartStopJail(self):
|
|
|
|
name = "TestCase"
|
2006-09-25 17:03:48 +00:00
|
|
|
self.__server.addJail(name)
|
|
|
|
self.__server.startJail(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
time.sleep(1)
|
2006-09-25 17:03:48 +00:00
|
|
|
self.__server.stopJail(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Transmitter(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Call before every test case."""
|
2006-09-25 17:03:48 +00:00
|
|
|
self.__server = Server()
|
|
|
|
self.__server.setLogLevel(0)
|
|
|
|
self.__server.start(False)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Call after every test case."""
|
2006-09-25 17:03:48 +00:00
|
|
|
self.__server.quit()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def testSetActionOK(self):
|
|
|
|
name = "TestCase"
|
|
|
|
cmdList = [["add", name],
|
|
|
|
["set", name, "actionstart", "Action Start"],
|
|
|
|
["set", name, "actionstop", "Action Stop"],
|
|
|
|
["set", name, "actioncheck", "Action Check"],
|
|
|
|
["set", name, "actionban", "Action Ban"],
|
|
|
|
["set", name, "actionunban", "Action Unban"],
|
|
|
|
["quit"]]
|
|
|
|
|
|
|
|
outList = [(0, name),
|
|
|
|
(0, 'Action Start'),
|
|
|
|
(0, 'Action Stop'),
|
|
|
|
(0, 'Action Check'),
|
|
|
|
(0, 'Action Ban'),
|
|
|
|
(0, 'Action Unban'),
|
|
|
|
(0, None)]
|
|
|
|
|
|
|
|
cnt = 0
|
|
|
|
for cmd in cmdList:
|
2006-09-25 17:03:48 +00:00
|
|
|
self.assertEqual(self.__server.transm.proceed(cmd), outList[cnt])
|
2006-06-26 20:05:00 +00:00
|
|
|
cnt += 1
|
|
|
|
|
|
|
|
def testSetActionNOK(self):
|
|
|
|
name = "TestCase"
|
|
|
|
cmdList = [["addd", name],
|
|
|
|
["set", name, "test"],
|
|
|
|
["prout prout", "Stop"],
|
|
|
|
["fail2ban", "sucks"],
|
|
|
|
["set"],
|
|
|
|
["_/&%", "@*+%&"],
|
|
|
|
[" quit"]]
|
|
|
|
|
|
|
|
outList = [1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1]
|
|
|
|
|
|
|
|
cnt = 0
|
|
|
|
for cmd in cmdList:
|
2006-09-25 17:03:48 +00:00
|
|
|
msg = self.__server.transm.proceed(cmd)
|
2006-06-26 20:05:00 +00:00
|
|
|
self.assertEqual(msg[0], outList[cnt])
|
|
|
|
cnt += 1
|
|
|
|
|
|
|
|
def testJail(self):
|
|
|
|
name = "TestCase"
|
|
|
|
cmdList = [["add", name],
|
|
|
|
["set", name, "logpath", "testcases/files/testcase01.log"],
|
|
|
|
["set", name, "timeregex", "\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}"],
|
|
|
|
["set", name, "timepattern", "%b %d %H:%M:%S"],
|
|
|
|
["set", name, "failregex", "Authentication failure"],
|
|
|
|
["start", name],
|
|
|
|
["stop", name],
|
|
|
|
["quit"]]
|
|
|
|
|
|
|
|
for cmd in cmdList:
|
2006-09-25 17:03:48 +00:00
|
|
|
self.__server.transm.proceed(cmd)
|
2006-06-26 20:05:00 +00:00
|
|
|
if cmd == ["start", name]:
|
|
|
|
time.sleep(2)
|
2006-09-25 17:03:48 +00:00
|
|
|
jail = self.__server.jails[name]
|
2006-06-26 20:05:00 +00:00
|
|
|
self.assertEqual(jail.getFilter().failManager.size(), 0)
|
|
|
|
self.assertEqual(jail.getAction().banManager.size(), 2)
|
2011-10-07 19:47:50 +00:00
|
|
|
|