⚝
One Hat Cyber Team
⚝
Your IP:
216.73.217.4
Server IP:
41.128.143.86
Server:
Linux host.raqmix.cloud 6.8.0-1025-azure #30~22.04.1-Ubuntu SMP Wed Mar 12 15:28:20 UTC 2025 x86_64
Server Software:
Apache
PHP Version:
8.3.23
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
lib
/
python3
/
dist-packages
/
fail2ban
/
tests
/
View File Name :
observertestcase.py
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*- # vi: set ft=python sts=4 ts=4 sw=4 noet : # This file is part of Fail2Ban. # # Fail2Ban is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Fail2Ban is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Fail2Ban; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Author: Serg G. Brester (sebres) # __author__ = "Serg G. Brester (sebres)" __copyright__ = "Copyright (c) 2014 Serg G. Brester" __license__ = "GPL" import os import sys import unittest import tempfile import time from ..server.mytime import MyTime from ..server.ticket import FailTicket, BanTicket from ..server.failmanager import FailManager from ..server.observer import Observers, ObserverThread from ..server.utils import Utils from .utils import LogCaptureTestCase from .dummyjail import DummyJail from .databasetestcase import getFail2BanDb, Fail2BanDb class BanTimeIncr(LogCaptureTestCase): def setUp(self): """Call before every test case.""" super(BanTimeIncr, self).setUp() self.__jail = DummyJail() self.__jail.calcBanTime = self.calcBanTime self.Observer = ObserverThread() def tearDown(self): super(BanTimeIncr, self).tearDown() def calcBanTime(self, banTime, banCount): return self.Observer.calcBanTime(self.__jail, banTime, banCount) def testDefault(self, multipliers = None): a = self.__jail; a.setBanTimeExtra('increment', 'true') self.assertEqual(a.getBanTimeExtra('increment'), True) a.setBanTimeExtra('maxtime', '1d') self.assertEqual(a.getBanTimeExtra('maxtime'), 24*60*60) a.setBanTimeExtra('rndtime', None) a.setBanTimeExtra('factor', None) # tests formulat or multipliers: a.setBanTimeExtra('multipliers', multipliers) # test algorithm and max time 24 hours : self.assertEqual( [a.calcBanTime(600, i) for i in range(1, 11)], [1200, 2400, 4800, 9600, 19200, 38400, 76800, 86400, 86400, 86400] ) # with extra large max time (30 days): a.setBanTimeExtra('maxtime', '30d') # using formula the ban time grows always, but using multipliers the growing will stops with last one: arr = [1200, 2400, 4800, 9600, 19200, 38400, 76800, 153600, 307200, 614400] if multipliers is not None: multcnt = len(multipliers.split(' ')) if multcnt < 11: arr = arr[0:multcnt-1] + ([arr[multcnt-2]] * (11-multcnt)) self.assertEqual( [a.calcBanTime(600, i) for i in range(1, 11)], arr ) a.setBanTimeExtra('maxtime', '1d') # change factor : a.setBanTimeExtra('factor', '2'); self.assertEqual( [a.calcBanTime(600, i) for i in range(1, 11)], [2400, 4800, 9600, 19200, 38400, 76800, 86400, 86400, 86400, 86400] ) # factor is float : a.setBanTimeExtra('factor', '1.33'); self.assertEqual( [int(a.calcBanTime(600, i)) for i in range(1, 11)], [1596, 3192, 6384, 12768, 25536, 51072, 86400, 86400, 86400, 86400] ) a.setBanTimeExtra('factor', None); # change max time : a.setBanTimeExtra('maxtime', '12h') self.assertEqual( [a.calcBanTime(600, i) for i in range(1, 11)], [1200, 2400, 4800, 9600, 19200, 38400, 43200, 43200, 43200, 43200] ) a.setBanTimeExtra('maxtime', '24h') ## test randomization - not possible all 10 times we have random = 0: a.setBanTimeExtra('rndtime', '5m') self.assertTrue( False in [1200 in [a.calcBanTime(600, 1) for i in range(10)] for c in range(10)] ) a.setBanTimeExtra('rndtime', None) self.assertFalse( False in [1200 in [a.calcBanTime(600, 1) for i in range(10)] for c in range(10)] ) # restore default: a.setBanTimeExtra('multipliers', None) a.setBanTimeExtra('factor', None); a.setBanTimeExtra('maxtime', '24h') a.setBanTimeExtra('rndtime', None) def testMultipliers(self): # this multipliers has the same values as default formula, we test stop growing after count 9: self.testDefault('1 2 4 8 16 32 64 128 256') # this multipliers has exactly the same values as default formula, test endless growing (stops by count 31 only): self.testDefault(' '.join([str(1<