mirror of https://github.com/fail2ban/fail2ban
testAmbiguousDatePattern rewritten with DateDetector/DatePatternRegex directly (moved to misctestcase.py)
parent
8e09be5fc8
commit
0bdee2556f
|
@ -187,41 +187,4 @@ class Fail2banRegexTest(LogCaptureTestCase):
|
||||||
|
|
||||||
self.assertLogged('https://')
|
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 <HOST>$')),
|
|
||||||
# ambiguous "unbound" patterns (missed):
|
|
||||||
(0, ('Test failure TestJan 23 21:59:59.011 2015 for 192.0.2.1', r'for <HOST>$')),
|
|
||||||
(0, ('Test failure Jan 23 21:59:59123456789 for 192.0.2.1', r'for <HOST>$')),
|
|
||||||
# ambiguous "no optional year" patterns (matched):
|
|
||||||
(1, ('Aug 8 11:25:50 14430f2329b8 Authentication failed from 192.0.2.1', r'from <HOST>$')),
|
|
||||||
(1, ('[Aug 8 11:25:50] 14430f2329b8 Authentication failed from 192.0.2.1', r'from <HOST>$')),
|
|
||||||
# direct specified patterns:
|
|
||||||
(1, ('-d', r'%H:%M:%S %d.%m.%Y$', '192.0.2.1 at 20:00:00 01.02.2003', '^<HOST>')),
|
|
||||||
(1, ('-d', r'\[%H:%M:%S %d.%m.%Y\]', '192.0.2.1[20:00:00 01.02.2003]', '^<HOST>$')),
|
|
||||||
(1, ('-d', r'\[%H:%M:%S %d.%m.%Y\]', '[20:00:00 01.02.2003]192.0.2.1', '^<HOST>$')),
|
|
||||||
(1, ('-d', r'\[%H:%M:%S %d.%m.%Y\]$', '192.0.2.1[20:00:00 01.02.2003]', '^<HOST>$')),
|
|
||||||
(1, ('-d', r'^\[%H:%M:%S %d.%m.%Y\]', '[20:00:00 01.02.2003]192.0.2.1', '^<HOST>$')),
|
|
||||||
(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 <HOST>$')),
|
|
||||||
(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 <HOST>$')),
|
|
||||||
(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 <HOST>, 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', '^<HOST>')),
|
|
||||||
(0, ('-d', r'%H:%M:%S %d.%m.%Y', '20:00:00 01.02.2003x192.0.2.1', '<HOST>$')),
|
|
||||||
# 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', '^<HOST>')),
|
|
||||||
(1, ('-d', r'%H:%M:%S %d.%m.%Y', '20:00:00 01.02.2003 192.0.2.1', '<HOST>$')),
|
|
||||||
):
|
|
||||||
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()
|
|
||||||
|
|
|
@ -37,6 +37,7 @@ from utils import LogCaptureTestCase, logSys as DefLogSys
|
||||||
|
|
||||||
from ..helpers import formatExceptionInfo, mbasename, TraceBack, FormatterWithTraceBack, getLogger
|
from ..helpers import formatExceptionInfo, mbasename, TraceBack, FormatterWithTraceBack, getLogger
|
||||||
from ..helpers import splitwords
|
from ..helpers import splitwords
|
||||||
|
from ..server.datedetector import DateDetector
|
||||||
from ..server.datetemplate import DatePatternRegex
|
from ..server.datetemplate import DatePatternRegex
|
||||||
|
|
||||||
|
|
||||||
|
@ -340,3 +341,47 @@ class CustomDateFormatsTest(unittest.TestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
date,
|
date,
|
||||||
datetime.datetime(2007, 1, 25, 16, 0))
|
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)
|
||||||
|
|
Loading…
Reference in New Issue