45
42
log = "1138049999 [sshd] error: PAM: Authentication failure"
46
43
date = [2006, 1, 23, 21, 59, 59, 0, 23, 0]
47
44
dateUnix = 1138049999.0
49
46
self.assertEqual(self.__datedetector.getTime(log), date)
50
47
self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix)
52
49
def testGetTime(self):
53
50
log = "Jan 23 21:59:59 [sshd] error: PAM: Authentication failure"
54
date = [2005, 1, 23, 21, 59, 59, 1, 23, -1]
51
date = [2005, 1, 23, 21, 59, 59, 6, 23, -1]
55
52
dateUnix = 1106513999.0
57
self.assertEqual(self.__datedetector.getTime(log), date)
53
# yoh: testing only up to 6 elements, since the day of the week
54
# is not correctly determined atm, since year is not present
55
# in the log entry. Since this doesn't effect the operation
56
# of fail2ban -- we just ignore incorrect day of the week
57
self.assertEqual(self.__datedetector.getTime(log)[:6], date[:6])
58
58
self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix)
60
60
def testVariousTimes(self):
61
61
"""Test detection of various common date/time formats f2b should understand
63
date = [2005, 1, 23, 21, 59, 59, 1, 23, -1]
63
date = [2005, 1, 23, 21, 59, 59, 6, 23, -1]
64
64
dateUnix = 1106513999.0
68
"Sun Jan 23 21:59:59 2005",
69
"Sun Jan 23 21:59:59",
70
"2005/01/23 21:59:59",
68
71
"2005.01.23 21:59:59",
69
72
"23/01/2005 21:59:59",
74
"23/Jan/2005:21:59:59",
75
"01/23/2005:21:59:59",
76
"2005-01-23 21:59:59",
77
"23-Jan-2005 21:59:59",
78
"23-01-2005 21:59:59",
79
"01-23-2005 21:59:59.252", # reported on f2b, causes Feb29 fix to break
80
"@4000000041f4104f00000000", # TAI64N
81
"2005-01-23T21:59:59.252Z", #ISO 8601
82
"2005-01-23T21:59:59-05:00Z", #ISO 8601 with TZ
83
"<01/23/05@21:59:59>",
84
"050123 21:59:59", # MySQL
85
"Jan-23-05 21:59:59", # ASSP like
71
87
log = sdate + "[sshd] error: PAM: Authentication failure"
74
# TODO (Yarik is confused): figure out why for above it is
75
# "1" as day of the week which would be Tue, although it
90
# yoh: on [:6] see in above test
77
91
self.assertEqual(self.__datedetector.getTime(log)[:6], date[:6])
78
92
self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix)
94
def testStableSortTemplate(self):
95
old_names = [x.getName() for x in self.__datedetector.getTemplates()]
96
self.__datedetector.sortTemplate()
97
# If there were no hits -- sorting should not change the order
98
for old_name, n in zip(old_names, self.__datedetector.getTemplates()):
99
self.assertEqual(old_name, n.getName()) # "Sort must be stable"
101
def testAllUniqueTemplateNames(self):
102
self.assertRaises(ValueError, self.__datedetector._appendTemplate,
103
self.__datedetector.getTemplates()[0])
105
def testFullYearMatch_gh130(self):
106
# see https://github.com/fail2ban/fail2ban/pull/130
107
# yoh: unfortunately this test is not really effective to reproduce the
108
# situation but left in place to assure consistent behavior
109
m1 = [2012, 10, 11, 2, 37, 17]
111
self.__datedetector.getTime('2012/10/11 02:37:17 [error] 18434#0')[:6],
113
self.__datedetector.sortTemplate()
114
# confuse it with year being at the end
117
self.__datedetector.getTime('11/10/2012 02:37:17 [error] 18434#0')[:6],
119
self.__datedetector.sortTemplate()
120
# and now back to the original
122
self.__datedetector.getTime('2012/10/11 02:37:17 [error] 18434#0')[:6],
80
126
# def testDefaultTempate(self):
81
127
# self.__datedetector.setDefaultRegex("^\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}")
82
128
# self.__datedetector.setDefaultPattern("%b %d %H:%M:%S")