diff --git a/fail2ban/tests/fail2banregextestcase.py b/fail2ban/tests/fail2banregextestcase.py index 59ba60be..1119efdb 100644 --- a/fail2ban/tests/fail2banregextestcase.py +++ b/fail2ban/tests/fail2banregextestcase.py @@ -187,41 +187,4 @@ class Fail2banRegexTest(LogCaptureTestCase): self.assertLogged('https://') - def testAmbiguousDatePattern(self): - for (matched, args) in ( - # positive case: - (1, ('Test failure Jan 23 21:59:59 for 192.0.2.1', r'for $')), - # ambiguous "unbound" patterns (missed): - (0, ('Test failure TestJan 23 21:59:59.011 2015 for 192.0.2.1', r'for $')), - (0, ('Test failure Jan 23 21:59:59123456789 for 192.0.2.1', r'for $')), - # ambiguous "no optional year" patterns (matched): - (1, ('Aug 8 11:25:50 14430f2329b8 Authentication failed from 192.0.2.1', r'from $')), - (1, ('[Aug 8 11:25:50] 14430f2329b8 Authentication failed from 192.0.2.1', r'from $')), - # direct specified patterns: - (1, ('-d', r'%H:%M:%S %d.%m.%Y$', '192.0.2.1 at 20:00:00 01.02.2003', '^')), - (1, ('-d', r'\[%H:%M:%S %d.%m.%Y\]', '192.0.2.1[20:00:00 01.02.2003]', '^$')), - (1, ('-d', r'\[%H:%M:%S %d.%m.%Y\]', '[20:00:00 01.02.2003]192.0.2.1', '^$')), - (1, ('-d', r'\[%H:%M:%S %d.%m.%Y\]$', '192.0.2.1[20:00:00 01.02.2003]', '^$')), - (1, ('-d', r'^\[%H:%M:%S %d.%m.%Y\]', '[20:00:00 01.02.2003]192.0.2.1', '^$')), - (1, ('-d', r'^\[%d/%b/%Y %H:%M:%S\]', '[17/Jun/2011 17:00:45] Attempt, IP address 192.0.2.1', r'^ Attempt, IP address $')), - (1, ('-d', r'\[%d/%b/%Y %H:%M:%S\]', 'Attempt [17/Jun/2011 17:00:45] IP address 192.0.2.1', r'^Attempt\s+IP address $')), - (1, ('-d', r'\[%d/%b/%Y %H:%M:%S\]', 'Attempt IP address 192.0.2.1, date: [17/Jun/2011 17:00:45]', r'^Attempt IP address , date: $')), - # direct specified patterns (begin/end, missed): - (0, ('-d', r'%H:%M:%S %d.%m.%Y', '192.0.2.1x20:00:00 01.02.2003', '^')), - (0, ('-d', r'%H:%M:%S %d.%m.%Y', '20:00:00 01.02.2003x192.0.2.1', '$')), - # direct specified patterns (begin/end, matched): - (1, ('-d', r'%H:%M:%S %d.%m.%Y', '192.0.2.1 20:00:00 01.02.2003', '^')), - (1, ('-d', r'%H:%M:%S %d.%m.%Y', '20:00:00 01.02.2003 192.0.2.1', '$')), - ): - logSys.debug('== test: %r', args) - (opts, args, fail2banRegex) = _Fail2banRegex(*args) - self.assertTrue(fail2banRegex.start(opts, args)) - matchedLog = 'Lines: 1 lines, 0 ignored, 1 matched, 0 missed' - missedLog = 'Lines: 1 lines, 0 ignored, 0 matched, 1 missed' - if matched: - self.assertLogged(matchedLog) - self.assertNotLogged(missedLog) - else: - self.assertNotLogged(matchedLog) - self.assertLogged(missedLog) - self.pruneLog() + diff --git a/fail2ban/tests/misctestcase.py b/fail2ban/tests/misctestcase.py index e2b4c0b6..4dfff2d4 100644 --- a/fail2ban/tests/misctestcase.py +++ b/fail2ban/tests/misctestcase.py @@ -37,6 +37,7 @@ from utils import LogCaptureTestCase, logSys as DefLogSys from ..helpers import formatExceptionInfo, mbasename, TraceBack, FormatterWithTraceBack, getLogger from ..helpers import splitwords +from ..server.datedetector import DateDetector from ..server.datetemplate import DatePatternRegex @@ -340,3 +341,47 @@ class CustomDateFormatsTest(unittest.TestCase): self.assertEqual( date, datetime.datetime(2007, 1, 25, 16, 0)) + + def testAmbiguousDatePattern(self): + defDD = DateDetector() + defDD.addDefaultTemplate() + logSys = DefLogSys + for (matched, dp, line) in ( + # positive case: + ('Jan 23 21:59:59', None, 'Test failure Jan 23 21:59:59 for 192.0.2.1'), + # ambiguous "unbound" patterns (missed): + (False, None, 'Test failure TestJan 23 21:59:59.011 2015 for 192.0.2.1'), + (False, None, 'Test failure Jan 23 21:59:59123456789 for 192.0.2.1'), + # ambiguous "no optional year" patterns (matched): + ('Aug 8 11:25:50', None, 'Aug 8 11:25:50 14430f2329b8 Authentication failed from 192.0.2.1'), + ('Aug 8 11:25:50', None, '[Aug 8 11:25:50] 14430f2329b8 Authentication failed from 192.0.2.1'), + ('Aug 8 11:25:50 2014', None, 'Aug 8 11:25:50 2014 14430f2329b8 Authentication failed from 192.0.2.1'), + # direct specified patterns: + ('20:00:00 01.02.2003', r'%H:%M:%S %d.%m.%Y$', '192.0.2.1 at 20:00:00 01.02.2003'), + ('[20:00:00 01.02.2003]', r'\[%H:%M:%S %d.%m.%Y\]', '192.0.2.1[20:00:00 01.02.2003]'), + ('[20:00:00 01.02.2003]', r'\[%H:%M:%S %d.%m.%Y\]', '[20:00:00 01.02.2003]192.0.2.1'), + ('[20:00:00 01.02.2003]', r'\[%H:%M:%S %d.%m.%Y\]$', '192.0.2.1[20:00:00 01.02.2003]'), + ('[20:00:00 01.02.2003]', r'^\[%H:%M:%S %d.%m.%Y\]', '[20:00:00 01.02.2003]192.0.2.1'), + ('[17/Jun/2011 17:00:45]', r'^\[%d/%b/%Y %H:%M:%S\]', '[17/Jun/2011 17:00:45] Attempt, IP address 192.0.2.1'), + ('[17/Jun/2011 17:00:45]', r'\[%d/%b/%Y %H:%M:%S\]', 'Attempt [17/Jun/2011 17:00:45] IP address 192.0.2.1'), + ('[17/Jun/2011 17:00:45]', r'\[%d/%b/%Y %H:%M:%S\]', 'Attempt IP address 192.0.2.1, date: [17/Jun/2011 17:00:45]'), + # direct specified patterns (begin/end, missed): + (False, r'%H:%M:%S %d.%m.%Y', '192.0.2.1x20:00:00 01.02.2003'), + (False, r'%H:%M:%S %d.%m.%Y', '20:00:00 01.02.2003x192.0.2.1'), + # direct specified patterns (begin/end, matched): + ('20:00:00 01.02.2003', r'%H:%M:%S %d.%m.%Y', '192.0.2.1 20:00:00 01.02.2003'), + ('20:00:00 01.02.2003', r'%H:%M:%S %d.%m.%Y', '20:00:00 01.02.2003 192.0.2.1'), + ): + logSys.debug('== test: %r', (matched, dp, line)) + if dp is None: + dd = defDD + else: + dp = DatePatternRegex(dp) + dd = DateDetector() + dd.appendTemplate(dp) + date = dd.getTime(line) + if matched: + self.assertTrue(date) + self.assertEqual(matched, date[1].group()) + else: + self.assertEqual(date, None)