From 7f5f7017dbaf9410149d262e4ec15fdc23da60f8 Mon Sep 17 00:00:00 2001 From: SP Date: Fri, 4 Jan 2019 17:06:47 +0300 Subject: [PATCH] ENH: added new test cases for `fail2ban-client get banip` command (gh-1916) --- fail2ban/tests/clientbeautifiertestcase.py | 7 +++++ fail2ban/tests/fail2banclienttestcase.py | 30 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/fail2ban/tests/clientbeautifiertestcase.py b/fail2ban/tests/clientbeautifiertestcase.py index 79a0ff54..863da7f8 100644 --- a/fail2ban/tests/clientbeautifiertestcase.py +++ b/fail2ban/tests/clientbeautifiertestcase.py @@ -261,3 +261,10 @@ class BeautifierTest(unittest.TestCase): output = "Sorry but the command is invalid" self.assertEqual(self.b.beautifyError(IndexError()), output) + + def testJailBanList(self): + self.b.setInputCmd(["get", "ssh", "banip"]) + response = ["192.168.0.1", "192.168.1.10"] + output = "192.168.0.1 192.168.1.10" + self.assertEqual(self.b.beautify(response), output) + self.assertEqual(self.b.beautify([]), "") diff --git a/fail2ban/tests/fail2banclienttestcase.py b/fail2ban/tests/fail2banclienttestcase.py index c120128b..b8fb6d0c 100644 --- a/fail2ban/tests/fail2banclienttestcase.py +++ b/fail2ban/tests/fail2banclienttestcase.py @@ -1227,6 +1227,36 @@ class Fail2banServerTest(Fail2banClientServerBase): "Jail 'test-jail1' stopped", "Jail 'test-jail1' started", all=True, wait=MID_WAITTIME) + # test the list of banned IP addresses, step 0: prepare + self.pruneLog("[test-phase 9a]") + self.execCmd(SUCCESS, startparams, "reload", "--unban", "test-jail1") + self.assertLogged( + "Jail 'test-jail1' reloaded", wait=MID_WAITTIME) + # test the list of banned IP addresses, step 1: ban IP addresses + self.pruneLog("[test-phase 9b]") + self.execCmd(SUCCESS, startparams, + "set", "test-jail1", "banip", "192.168.0.1") + self.assertLogged("[test-jail1] Ban 192.168.0.1", wait=MID_WAITTIME) + self.execCmd(SUCCESS, startparams, + "set", "test-jail1", "banip", "192.168.1.10") + self.assertLogged("[test-jail1] Ban 192.168.1.10", wait=MID_WAITTIME) + self.execCmd(SUCCESS, startparams, "get", "test-jail1", "banip") + self.assertLogged( + "192.168.1.10 192.168.0.1", + "192.168.0.1 192.168.1.10", wait=MID_WAITTIME) + # test the list of banned IP addresses, step 2: unban IP addresses + self.pruneLog("[test-phase 9c]") + self.execCmd(SUCCESS, startparams, + "set", "test-jail1", "unbanip", "192.168.0.1") + self.assertLogged("[test-jail1] Unban 192.168.0.1", wait=MID_WAITTIME) + self.execCmd(SUCCESS, startparams, + "set", "test-jail1", "unbanip", "192.168.1.10") + self.assertLogged("[test-jail1] Unban 192.168.1.10", wait=MID_WAITTIME) + self.execCmd(SUCCESS, startparams, "get", "test-jail1", "banip") + self.assertNotLogged( + "192.168.1.10 192.168.0.1", + "192.168.0.1 192.168.1.10", wait=MID_WAITTIME) + # test action.d/nginx-block-map.conf -- @unittest.F2B.skip_if_cfg_missing(action="nginx-block-map") @with_foreground_server_thread(startextra={