From b7fe33483ae6943e0d942849605dacae42856b30 Mon Sep 17 00:00:00 2001 From: sebres Date: Tue, 7 Jan 2020 11:03:41 +0100 Subject: [PATCH] coverage --- fail2ban/server/action.py | 4 ++-- fail2ban/server/actions.py | 4 ++-- fail2ban/tests/actiontestcase.py | 13 +++++++++++++ fail2ban/tests/filtertestcase.py | 20 ++++++++++++++++---- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/fail2ban/server/action.py b/fail2ban/server/action.py index b825bb96..0c7e1207 100644 --- a/fail2ban/server/action.py +++ b/fail2ban/server/action.py @@ -33,7 +33,7 @@ from abc import ABCMeta from collections import MutableMapping from .failregex import mapTag2Opt -from .ipdns import asip, DNSUtils +from .ipdns import DNSUtils from .mytime import MyTime from .utils import Utils from ..helpers import getLogger, _merge_copy_dicts, \ @@ -175,7 +175,7 @@ class CallingMap(MutableMapping, object): def __len__(self): return len(self.data) - def copy(self): # pragma: no cover + def copy(self): return self.__class__(_merge_copy_dicts(self.data, self.storage)) diff --git a/fail2ban/server/actions.py b/fail2ban/server/actions.py index b65a77ea..ed93c971 100644 --- a/fail2ban/server/actions.py +++ b/fail2ban/server/actions.py @@ -214,10 +214,10 @@ class Actions(JailThread, Mapping): if isinstance(ip, list): # Multiple IPs: - tickets = (BanTicket(ip if isinstance(ip, IPAddr) else IPAddr(ip), unixTime) for ip in ip) + tickets = (BanTicket(ip, unixTime) for ip in ip) else: # Single IP: - tickets = (BanTicket(ip if isinstance(ip, IPAddr) else IPAddr(ip), unixTime),) + tickets = (BanTicket(ip, unixTime),) return self.__checkBan(tickets) diff --git a/fail2ban/tests/actiontestcase.py b/fail2ban/tests/actiontestcase.py index a24a2b77..3425667c 100644 --- a/fail2ban/tests/actiontestcase.py +++ b/fail2ban/tests/actiontestcase.py @@ -587,6 +587,19 @@ class CommandActionTest(LogCaptureTestCase): self.assertEqual(len(m), 3) self.assertIn('c', m) self.assertEqual((m['a'], m['b'], m['c']), (5, 11, 'test')) + # immutability of copy: + m['d'] = 'dddd' + m2 = m.copy() + m2['c'] = lambda self: self['a'] + 7 + m2['a'] = 1 + del m2['b'] + del m2['d'] + self.assertTrue('b' in m) + self.assertTrue('d' in m) + self.assertFalse('b' in m2) + self.assertFalse('d' in m2) + self.assertEqual((m['a'], m['b'], m['c'], m['d']), (5, 11, 'test', 'dddd')) + self.assertEqual((m2['a'], m2['c']), (1, 8)) def testCallingMapRep(self): m = CallingMap({ diff --git a/fail2ban/tests/filtertestcase.py b/fail2ban/tests/filtertestcase.py index 41a6f0bf..d6ad8235 100644 --- a/fail2ban/tests/filtertestcase.py +++ b/fail2ban/tests/filtertestcase.py @@ -40,7 +40,7 @@ from ..server.jail import Jail from ..server.filterpoll import FilterPoll from ..server.filter import FailTicket, Filter, FileFilter, FileContainer from ..server.failmanager import FailManagerEmpty -from ..server.ipdns import getfqdn, DNSUtils, IPAddr +from ..server.ipdns import asip, getfqdn, DNSUtils, IPAddr from ..server.mytime import MyTime from ..server.utils import Utils, uni_decode from .utils import setUpMyTime, tearDownMyTime, mtimesleep, with_tmpdir, LogCaptureTestCase, \ @@ -1843,11 +1843,15 @@ class DNSUtilsNetworkTests(unittest.TestCase): def setUp(self): """Call before every test case.""" super(DNSUtilsNetworkTests, self).setUp() - unittest.F2B.SkipIfNoNetwork() + #unittest.F2B.SkipIfNoNetwork() def test_IPAddr(self): - self.assertTrue(IPAddr('192.0.2.1').isIPv4) - self.assertTrue(IPAddr('2001:DB8::').isIPv6) + ip4 = IPAddr('192.0.2.1') + ip6 = IPAddr('2001:DB8::') + self.assertTrue(ip4.isIPv4) + self.assertTrue(ip6.isIPv6) + self.assertTrue(asip('192.0.2.1').isIPv4) + self.assertTrue(id(asip(ip4)) == id(ip4)) def test_IPAddr_Raw(self): # raw string: @@ -1884,6 +1888,7 @@ class DNSUtilsNetworkTests(unittest.TestCase): def testUseDns(self): res = DNSUtils.textToIp('www.example.com', 'no') self.assertSortedEqual(res, []) + unittest.F2B.SkipIfNoNetwork() res = DNSUtils.textToIp('www.example.com', 'warn') # sort ipaddr, IPv4 is always smaller as IPv6 self.assertSortedEqual(res, ['93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946']) @@ -1892,6 +1897,7 @@ class DNSUtilsNetworkTests(unittest.TestCase): self.assertSortedEqual(res, ['93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946']) def testTextToIp(self): + unittest.F2B.SkipIfNoNetwork() # Test hostnames hostnames = [ 'www.example.com', @@ -1905,6 +1911,8 @@ class DNSUtilsNetworkTests(unittest.TestCase): self.assertSortedEqual(res, ['93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946']) else: self.assertSortedEqual(res, []) + + def testIpToIp(self): # pure ips: for s in ('93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946'): ips = DNSUtils.textToIp(s, 'yes') @@ -2061,6 +2069,7 @@ class DNSUtilsNetworkTests(unittest.TestCase): self.assertTrue(IPAddr("2606:2800:220:1:248:1893:25c8:1946").isInNet(ips)) def testIPAddr_wrongDNS_IP(self): + unittest.F2B.SkipIfNoNetwork() DNSUtils.dnsToIp('`this`.dns-is-wrong.`wrong-nic`-dummy') DNSUtils.ipToName('*') @@ -2083,6 +2092,9 @@ class DNSUtilsNetworkTests(unittest.TestCase): self.assertEqual(getfqdn(lname), lname) # coverage (targeting all branches): FQDN from loopback and DNS blackhole is always the same: self.assertIn(getfqdn('localhost.'), ('localhost', 'localhost.')) + + def testFQDN_DNS(self): + unittest.F2B.SkipIfNoNetwork() self.assertIn(getfqdn('as112.arpa.'), ('as112.arpa.', 'as112.arpa'))