From e046b09c8a48f1fd473fe7eab768728601aef9b2 Mon Sep 17 00:00:00 2001 From: sebres Date: Thu, 21 Feb 2019 17:56:31 +0100 Subject: [PATCH] coverage for get ban --with-time --- fail2ban/tests/servertestcase.py | 20 ++++++++++++-------- fail2ban/tests/utils.py | 11 +++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/fail2ban/tests/servertestcase.py b/fail2ban/tests/servertestcase.py index 5cc5ad5f..e33dfcba 100644 --- a/fail2ban/tests/servertestcase.py +++ b/fail2ban/tests/servertestcase.py @@ -41,7 +41,7 @@ from ..server.jailthread import JailThread from ..server.ticket import BanTicket from ..server.utils import Utils from .dummyjail import DummyJail -from .utils import LogCaptureTestCase +from .utils import LogCaptureTestCase, with_alt_time, MyTime from ..helpers import getLogger, extractOptions, PREFER_ENC from .. import version @@ -374,6 +374,7 @@ class Transmitter(TransmitterBase): self.assertLogged("Ban 192.0.2.2", wait=True) self.assertNotLogged("Ban 192.0.2.1") + @with_alt_time def testJailBanList(self): jail = "TestJailBanList" self.server.addJail(jail, FAST_BACKEND) @@ -381,7 +382,7 @@ class Transmitter(TransmitterBase): # Helper to process set banip/set unbanip commands and compare the list of # banned IP addresses with outList. - def _getBanListTest(jail, banip=None, unbanip=None, outList=[]): + def _getBanListTest(jail, banip=None, unbanip=None, args=(), outList=[]): # Ban IP address if banip is not None: self.assertEqual( @@ -396,15 +397,18 @@ class Transmitter(TransmitterBase): self.assertLogged("Unban %s" % unbanip, wait=True) # Give chance to unban # Compare the list of banned IP addresses with outList self.assertSortedEqual( - self.transm.proceed(["get", jail, "banip"]), - (0, outList)) + self.transm.proceed(["get", jail, "banip"]+list(args)), + (0, outList), nestedOnly=False) + MyTime.setTime(MyTime.time() + 1) _getBanListTest(jail, outList=[]) - _getBanListTest(jail, banip="127.0.0.1", - outList=["127.0.0.1"]) - _getBanListTest(jail, banip="192.168.0.1", - outList=["127.0.0.1", "192.168.0.1"]) + _getBanListTest(jail, banip="127.0.0.1", args=('--with-time',), + outList=["127.0.0.1 \t2005-08-14 12:00:01 + 600 = 2005-08-14 12:10:01"]) + _getBanListTest(jail, banip="192.168.0.1", args=('--with-time',), + outList=[ + "127.0.0.1 \t2005-08-14 12:00:01 + 600 = 2005-08-14 12:10:01", + "192.168.0.1 \t2005-08-14 12:00:02 + 600 = 2005-08-14 12:10:02"]) _getBanListTest(jail, banip="192.168.1.10", outList=["127.0.0.1", "192.168.0.1", "192.168.1.10"]) _getBanListTest(jail, unbanip="127.0.0.1", diff --git a/fail2ban/tests/utils.py b/fail2ban/tests/utils.py index 31822c76..bb1b302a 100644 --- a/fail2ban/tests/utils.py +++ b/fail2ban/tests/utils.py @@ -245,6 +245,17 @@ def with_tmpdir(f): shutil.rmtree(tmp) return wrapper +def with_alt_time(f): + """Helper decorator to execute test in alternate (fixed) test time.""" + @wraps(f) + def wrapper(self, *args, **kwargs): + setUpMyTime() + try: + return f(self, *args, **kwargs) + finally: + tearDownMyTime() + return wrapper + # backwards compatibility to python 2.6: if not hasattr(unittest, 'SkipTest'): # pragma: no cover