From ee862124357df11afeaf43ae0faa94bd0064b403 Mon Sep 17 00:00:00 2001 From: Cyril Jaquier Date: Wed, 18 Oct 2006 22:35:32 +0000 Subject: [PATCH] - Updated testcases to fix the time using MyTime git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@419 a942ae1a-1317-0410-a47c-b1dcaea8d605 --- CHANGELOG | 1 + fail2ban-testcases | 7 +++++- testcases/datedetectortestcase.py | 8 ++----- testcases/files/testcase01.log | 37 ++++++++++++++++--------------- testcases/files/testcase02.log | 3 +++ testcases/filtertestcase.py | 32 +++++++++++++------------- 6 files changed, 48 insertions(+), 40 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 640637a0..10a20fb8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -16,6 +16,7 @@ ver. 0.7.4 (2006/??/??) - beta option in "fail2ban.conf" - Added "backend" option in "jail.conf" - Added more filters/actions and jail samples +- Improved testing framework ver. 0.7.3 (2006/09/28) - beta ---------- diff --git a/fail2ban-testcases b/fail2ban-testcases index 309ada27..b4825473 100755 --- a/fail2ban-testcases +++ b/fail2ban-testcases @@ -39,6 +39,11 @@ from testcases import failmanagertestcase from testcases import filtertestcase from testcases import servertestcase from testcases import datedetectortestcase +from server.mytime import MyTime + +# Set the time to a fixed, known value +# Sun Aug 14 12:00:00 CEST 2005 +MyTime.setTime(1124013600) # Gets the instance of the logger. logSys = logging.getLogger("fail2ban") @@ -54,7 +59,7 @@ tests = unittest.TestSuite() # Filter tests.addTest(unittest.makeSuite(filtertestcase.IgnoreIP)) tests.addTest(unittest.makeSuite(filtertestcase.LogFile)) -#tests.addTest(unittest.makeSuite(filtertestcase.GetFailures)) +tests.addTest(unittest.makeSuite(filtertestcase.GetFailures)) # Server #tests.addTest(unittest.makeSuite(servertestcase.StartStop)) #tests.addTest(unittest.makeSuite(servertestcase.Transmitter)) diff --git a/testcases/datedetectortestcase.py b/testcases/datedetectortestcase.py index 1c311c06..7c534ea0 100644 --- a/testcases/datedetectortestcase.py +++ b/testcases/datedetectortestcase.py @@ -48,13 +48,9 @@ class DateDetectorTest(unittest.TestCase): def testGetTime(self): log = "Jan 23 21:59:59 [sshd] error: PAM: Authentication failure" - date = [2006, 1, 23, 21, 59, 59, 1, 23, -1] - dateUnix = 1138049999.0 + date = [2005, 1, 23, 21, 59, 59, 1, 23, -1] + dateUnix = 1106513999.0 self.assertEqual(self.__datedetector.getTime(log), date) - self.assertEqual(self.__datedetector.getTime(log), date) - self.assertEqual(self.__datedetector.getTime(log), date) - self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix) - self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix) self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix) \ No newline at end of file diff --git a/testcases/files/testcase01.log b/testcases/files/testcase01.log index 43143d0b..afca924b 100644 --- a/testcases/files/testcase01.log +++ b/testcases/files/testcase01.log @@ -1,18 +1,19 @@ -Déc 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -Déc 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -Déc 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -Dec 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from failed.dns.ch -Dec 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from failed.dns.ch -Dec 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from failed.dns.ch -Dez 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -Dez 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -Dez 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -De 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -De 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -De 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -Dec 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -Dec 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -Dec 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 -Dec 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 87.142.124.10 -Dec 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 87.142.124.10 -Dec 31 23:59:59 [sshd] error: PAM: Authentication failure for kevin from 87.142.124.10 +Déc 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +Déc 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +Déc 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +Dec 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from failed.dns.ch +Dec 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from failed.dns.ch +Dec 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from failed.dns.ch +Dez 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +Dez 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +Dez 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +De 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +De 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +De 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +Aug 14 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +Aug 14 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +Aug 14 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 193.168.0.128 +Dec 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 87.142.124.10 +Dec 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 87.142.124.10 +Dec 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 87.142.124.10 +Dec 31 11:59:59 [sshd] error: PAM: Authentication failure for kevin from 87.142.124.10 diff --git a/testcases/files/testcase02.log b/testcases/files/testcase02.log index 547f8a53..0200d6de 100644 --- a/testcases/files/testcase02.log +++ b/testcases/files/testcase02.log @@ -8,3 +8,6 @@ Mar 16 10:34:01 i60p295 sshd[12365]: Postponed keyboard-interactive/pam for roeh Mar 16 10:34:01 i60p295 sshd[12365]: Accepted keyboard-interactive/pam for roehl from ::ffff:141.3.81.106 port 51332 ssh2 Mär 16 10:33:59 i60p295 sshd[12365]: Failed publickey for roehl from ::ffff:141.3.81.106 port 51332 ssh2 Mär 16 10:33:59 i60p295 sshd[12365]: Failed publickey for roehl from ::ffff:141.3.81.106 port 51332 ssh2 +Mar 16 10:35:01 i60p295 sshd[12365]: Accepted keyboard-interactive/pam for roehl from ::ffff:141.3.81.106 port 51332 ssh2 +Mar 16 10:36:01 i60p295 sshd[12365]: Accepted keyboard-interactive/pam for roehl from ::ffff:141.3.81.106 port 51332 ssh2 +Mar 16 10:37:01 i60p295 sshd[12365]: Accepted keyboard-interactive/pam for roehl from ::ffff:141.3.81.106 port 51332 ssh2 diff --git a/testcases/filtertestcase.py b/testcases/filtertestcase.py index 18ebcec1..3ba478e6 100644 --- a/testcases/filtertestcase.py +++ b/testcases/filtertestcase.py @@ -26,13 +26,14 @@ __license__ = "GPL" import unittest, socket from server.filterpoll import FilterPoll +from server.filter import Filter from server.failmanager import FailManager class IgnoreIP(unittest.TestCase): def setUp(self): """Call before every test case.""" - self.__filter = FilterPoll(None) + self.__filter = Filter(None) def tearDown(self): """Call after every test case.""" @@ -77,29 +78,30 @@ class LogFile(unittest.TestCase): class GetFailures(unittest.TestCase): + FILENAME = "testcases/files/testcase01.log" + def setUp(self): """Call before every test case.""" - self.__filter = FilterPoll(None) - self.__filter.addLogPath("testcases/files/testcase01.log") - self.__filter.setTimeRegex("\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}") - self.__filter.setTimePattern("%b %d %H:%M:%S") + self.__filter = Filter(None) + self.__filter.addLogPath(FILENAME) + #self.__filter.setTimeRegex("\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}") + #self.__filter.setTimePattern("%b %d %H:%M:%S") self.__filter.setFailRegex("(?:(?:Authentication failure|Failed [-/\w+]+) for(?: [iI](?:llegal|nvalid) user)?|[Ii](?:llegal|nvalid) user|ROOT LOGIN REFUSED) .*(?: from|FROM) (?:::f{4,6}:)?(?P\S*)") def tearDown(self): """Call after every test case.""" def testGetFailures(self): - output = [('87.142.124.10', 3, 1167605999.0), - ('193.168.0.128', 3, 1167605999.0)] + output = ('193.168.0.128', 3, 1124013599.0) - self.__filter.openLogFile() - self.__filter.getFailures() + self.__filter.getFailures(FILENAME) + + ticket = self.__filter.failManager.toBan() + + attempts = ticket.getAttempt() + date = ticket.getTime() + ip = ticket.getIP() + found = (ip, attempts, date) - found = [] - for ip in self.__filter.failManager.failList: - fData = self.__filter.failManager.failList[ip] - retry = fData.getRetry() - lTime = fData.getLastTime() - found.append((ip, retry, lTime)) self.assertEqual(found, output) \ No newline at end of file