From 3f62546231aa4145c7828c8500e4753bb3cab404 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Sun, 27 Nov 2011 12:01:53 -0500 Subject: [PATCH] ENH: reincarnated (some) servertestcases with a new thorough test new test creates a simple jail and makes sure if bans ip and has somewhat proper in the taken actions --- fail2ban-testcases | 4 +- testcases/servertestcase.py | 82 +++++++++++++++++++++++++++++++------ 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/fail2ban-testcases b/fail2ban-testcases index c05a64bd8..b10eb1cf2 100755 --- a/fail2ban-testcases +++ b/fail2ban-testcases @@ -67,8 +67,8 @@ tests.addTest(unittest.makeSuite(filtertestcase.LogFile)) tests.addTest(unittest.makeSuite(filtertestcase.GetFailures)) tests.addTest(unittest.makeSuite(filtertestcase.DNSUtilsTests)) # Server -#tests.addTest(unittest.makeSuite(servertestcase.StartStop)) -#tests.addTest(unittest.makeSuite(servertestcase.Transmitter)) +## tests.addTest(unittest.makeSuite(servertestcase.StartStop)) +tests.addTest(unittest.makeSuite(servertestcase.Transmitter)) tests.addTest(unittest.makeSuite(actiontestcase.ExecuteAction)) # FailManager tests.addTest(unittest.makeSuite(failmanagertestcase.AddFailure)) diff --git a/testcases/servertestcase.py b/testcases/servertestcase.py index ab32eb92f..81ac924b2 100644 --- a/testcases/servertestcase.py +++ b/testcases/servertestcase.py @@ -27,24 +27,31 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import unittest, socket, time +import unittest, socket, time, os.path +import tempfile from server.server import Server +from server.jails import UnknownJailException class StartStop(unittest.TestCase): def setUp(self): """Call before every test case.""" self.__server = Server() + self.__sock = tempfile.NamedTemporaryFile(delete=False).name self.__server.setLogLevel(0) - self.__server.start(False) + self.__server.start(self.__sock, False) def tearDown(self): """Call after every test case.""" + self.assertTrue(os.path.exists(self.__sock)) self.__server.quit() + # sock must have been removed as well + self.assertTrue(not os.path.exists(self.__sock)) def testStartStopJail(self): name = "TestCase" - self.__server.addJail(name) + backend = 'polling' + self.__server.addJail(name, backend) self.__server.startJail(name) time.sleep(1) self.__server.stopJail(name) @@ -55,14 +62,15 @@ class Transmitter(unittest.TestCase): def setUp(self): """Call before every test case.""" self.__server = Server() + self.__sock = tempfile.NamedTemporaryFile(delete=False).name self.__server.setLogLevel(0) - self.__server.start(False) + self.__server.start(self.__sock, False) def tearDown(self): """Call after every test case.""" self.__server.quit() - def testSetActionOK(self): + def __testSetActionOK(self): name = "TestCase" cmdList = [["add", name], ["set", name, "actionstart", "Action Start"], @@ -85,7 +93,7 @@ class Transmitter(unittest.TestCase): self.assertEqual(self.__server.transm.proceed(cmd), outList[cnt]) cnt += 1 - def testSetActionNOK(self): + def __testSetActionNOK(self): name = "TestCase" cmdList = [["addd", name], ["set", name, "test"], @@ -109,12 +117,12 @@ class Transmitter(unittest.TestCase): self.assertEqual(msg[0], outList[cnt]) cnt += 1 - def testJail(self): + 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, "addlogpath", "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], @@ -124,7 +132,55 @@ class Transmitter(unittest.TestCase): self.__server.transm.proceed(cmd) if cmd == ["start", name]: time.sleep(2) - jail = self.__server.jails[name] + jail = self.__server.jails.get(name) + self.assertEqual(jail.getFilter().getFailManager().size(), 0) + self.assertEqual(jail.getAction().getBanManager().size(), 2) + + + def testJailWithActions(self): + name = "TestCase2" + aname = "TestAction2" + infos = '|