~ubuntu-branches/ubuntu/vivid/fail2ban/vivid

« back to all changes in this revision

Viewing changes to testcases/datedetectortestcase.py

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-05-13 11:58:56 UTC
  • mfrom: (1.2.6) (11.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20130513115856-r1wwsd58ajx2ub5o
Tags: 0.8.9-1
* New upstream release
  - significant improvements in documentation (Closes: #400416)
  - roundcube auth filter (Closes: #699442)
  - enforces C locale for dates (Closes: #686341)
  - provides bash_completion.d/fail2ban
* debian/jail.conf:
  - added findtime and documentation on those basic options from jail.conf
    (Closes: #704568)
  - added new sample jails definitions for ssh-route, ssh-iptables-ipset{4,6},
    roundcube-auth, sogo-auth, mysqld-auth
* debian/control:
  - suggest system-log-daemon (Closes: #691001)
  - boost policy compliance to 3.9.4
* debian/rules:
  - run fail2ban's unittests at build time but ignore the failures
    (there are still some known issues to fix up to guarantee robust testing
    in clean chroots etc).
    Only pyinotify was added to build-depends since gamin might still be
    buggy on older releases and get stuck, which would complicate
    backporting

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#
16
16
# You should have received a copy of the GNU General Public License
17
17
# along with Fail2Ban; if not, write to the Free Software
18
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
19
 
20
20
# Author: Cyril Jaquier
21
21
22
 
# $Revision$
23
22
 
24
23
__author__ = "Cyril Jaquier"
25
 
__version__ = "$Revision$"
26
 
__date__ = "$Date$"
27
24
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
28
25
__license__ = "GPL"
29
26
 
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
48
 
                
 
45
 
49
46
                self.assertEqual(self.__datedetector.getTime(log), date)
50
47
                self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix)
51
48
        
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
56
 
        
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)
59
59
 
60
60
        def testVariousTimes(self):
61
61
                """Test detection of various common date/time formats f2b should understand
62
62
                """
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
65
65
 
66
66
                for sdate in (
67
67
                        "Jan 23 21:59:59",
 
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",
 
73
                        "23/01/05 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
70
86
                        ):
71
87
                        log = sdate + "[sshd] error: PAM: Authentication failure"
72
88
                        # exclude
73
89
 
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
76
 
                        #      was Sun
 
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)
79
93
 
 
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"
 
100
 
 
101
        def testAllUniqueTemplateNames(self):
 
102
                self.assertRaises(ValueError, self.__datedetector._appendTemplate,
 
103
                                                  self.__datedetector.getTemplates()[0])
 
104
 
 
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]
 
110
                self.assertEqual(
 
111
                        self.__datedetector.getTime('2012/10/11 02:37:17 [error] 18434#0')[:6],
 
112
                        m1)
 
113
                self.__datedetector.sortTemplate()
 
114
                # confuse it with year being at the end
 
115
                for i in xrange(10):
 
116
                        self.assertEqual(
 
117
                                self.__datedetector.getTime('11/10/2012 02:37:17 [error] 18434#0')[:6],
 
118
                                m1)
 
119
                self.__datedetector.sortTemplate()
 
120
                # and now back to the original
 
121
                self.assertEqual(
 
122
                        self.__datedetector.getTime('2012/10/11 02:37:17 [error] 18434#0')[:6],
 
123
                        m1)
 
124
 
 
125
 
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")