mirror of https://github.com/fail2ban/fail2ban
coverage
parent
a527fbcae5
commit
b7fe33483a
|
@ -33,7 +33,7 @@ from abc import ABCMeta
|
||||||
from collections import MutableMapping
|
from collections import MutableMapping
|
||||||
|
|
||||||
from .failregex import mapTag2Opt
|
from .failregex import mapTag2Opt
|
||||||
from .ipdns import asip, DNSUtils
|
from .ipdns import DNSUtils
|
||||||
from .mytime import MyTime
|
from .mytime import MyTime
|
||||||
from .utils import Utils
|
from .utils import Utils
|
||||||
from ..helpers import getLogger, _merge_copy_dicts, \
|
from ..helpers import getLogger, _merge_copy_dicts, \
|
||||||
|
@ -175,7 +175,7 @@ class CallingMap(MutableMapping, object):
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.data)
|
return len(self.data)
|
||||||
|
|
||||||
def copy(self): # pragma: no cover
|
def copy(self):
|
||||||
return self.__class__(_merge_copy_dicts(self.data, self.storage))
|
return self.__class__(_merge_copy_dicts(self.data, self.storage))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -214,10 +214,10 @@ class Actions(JailThread, Mapping):
|
||||||
|
|
||||||
if isinstance(ip, list):
|
if isinstance(ip, list):
|
||||||
# Multiple IPs:
|
# 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:
|
else:
|
||||||
# Single IP:
|
# Single IP:
|
||||||
tickets = (BanTicket(ip if isinstance(ip, IPAddr) else IPAddr(ip), unixTime),)
|
tickets = (BanTicket(ip, unixTime),)
|
||||||
|
|
||||||
return self.__checkBan(tickets)
|
return self.__checkBan(tickets)
|
||||||
|
|
||||||
|
|
|
@ -587,6 +587,19 @@ class CommandActionTest(LogCaptureTestCase):
|
||||||
self.assertEqual(len(m), 3)
|
self.assertEqual(len(m), 3)
|
||||||
self.assertIn('c', m)
|
self.assertIn('c', m)
|
||||||
self.assertEqual((m['a'], m['b'], m['c']), (5, 11, 'test'))
|
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):
|
def testCallingMapRep(self):
|
||||||
m = CallingMap({
|
m = CallingMap({
|
||||||
|
|
|
@ -40,7 +40,7 @@ from ..server.jail import Jail
|
||||||
from ..server.filterpoll import FilterPoll
|
from ..server.filterpoll import FilterPoll
|
||||||
from ..server.filter import FailTicket, Filter, FileFilter, FileContainer
|
from ..server.filter import FailTicket, Filter, FileFilter, FileContainer
|
||||||
from ..server.failmanager import FailManagerEmpty
|
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.mytime import MyTime
|
||||||
from ..server.utils import Utils, uni_decode
|
from ..server.utils import Utils, uni_decode
|
||||||
from .utils import setUpMyTime, tearDownMyTime, mtimesleep, with_tmpdir, LogCaptureTestCase, \
|
from .utils import setUpMyTime, tearDownMyTime, mtimesleep, with_tmpdir, LogCaptureTestCase, \
|
||||||
|
@ -1843,11 +1843,15 @@ class DNSUtilsNetworkTests(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""Call before every test case."""
|
"""Call before every test case."""
|
||||||
super(DNSUtilsNetworkTests, self).setUp()
|
super(DNSUtilsNetworkTests, self).setUp()
|
||||||
unittest.F2B.SkipIfNoNetwork()
|
#unittest.F2B.SkipIfNoNetwork()
|
||||||
|
|
||||||
def test_IPAddr(self):
|
def test_IPAddr(self):
|
||||||
self.assertTrue(IPAddr('192.0.2.1').isIPv4)
|
ip4 = IPAddr('192.0.2.1')
|
||||||
self.assertTrue(IPAddr('2001:DB8::').isIPv6)
|
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):
|
def test_IPAddr_Raw(self):
|
||||||
# raw string:
|
# raw string:
|
||||||
|
@ -1884,6 +1888,7 @@ class DNSUtilsNetworkTests(unittest.TestCase):
|
||||||
def testUseDns(self):
|
def testUseDns(self):
|
||||||
res = DNSUtils.textToIp('www.example.com', 'no')
|
res = DNSUtils.textToIp('www.example.com', 'no')
|
||||||
self.assertSortedEqual(res, [])
|
self.assertSortedEqual(res, [])
|
||||||
|
unittest.F2B.SkipIfNoNetwork()
|
||||||
res = DNSUtils.textToIp('www.example.com', 'warn')
|
res = DNSUtils.textToIp('www.example.com', 'warn')
|
||||||
# sort ipaddr, IPv4 is always smaller as IPv6
|
# sort ipaddr, IPv4 is always smaller as IPv6
|
||||||
self.assertSortedEqual(res, ['93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946'])
|
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'])
|
self.assertSortedEqual(res, ['93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946'])
|
||||||
|
|
||||||
def testTextToIp(self):
|
def testTextToIp(self):
|
||||||
|
unittest.F2B.SkipIfNoNetwork()
|
||||||
# Test hostnames
|
# Test hostnames
|
||||||
hostnames = [
|
hostnames = [
|
||||||
'www.example.com',
|
'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'])
|
self.assertSortedEqual(res, ['93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946'])
|
||||||
else:
|
else:
|
||||||
self.assertSortedEqual(res, [])
|
self.assertSortedEqual(res, [])
|
||||||
|
|
||||||
|
def testIpToIp(self):
|
||||||
# pure ips:
|
# pure ips:
|
||||||
for s in ('93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946'):
|
for s in ('93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946'):
|
||||||
ips = DNSUtils.textToIp(s, 'yes')
|
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))
|
self.assertTrue(IPAddr("2606:2800:220:1:248:1893:25c8:1946").isInNet(ips))
|
||||||
|
|
||||||
def testIPAddr_wrongDNS_IP(self):
|
def testIPAddr_wrongDNS_IP(self):
|
||||||
|
unittest.F2B.SkipIfNoNetwork()
|
||||||
DNSUtils.dnsToIp('`this`.dns-is-wrong.`wrong-nic`-dummy')
|
DNSUtils.dnsToIp('`this`.dns-is-wrong.`wrong-nic`-dummy')
|
||||||
DNSUtils.ipToName('*')
|
DNSUtils.ipToName('*')
|
||||||
|
|
||||||
|
@ -2083,6 +2092,9 @@ class DNSUtilsNetworkTests(unittest.TestCase):
|
||||||
self.assertEqual(getfqdn(lname), lname)
|
self.assertEqual(getfqdn(lname), lname)
|
||||||
# coverage (targeting all branches): FQDN from loopback and DNS blackhole is always the same:
|
# coverage (targeting all branches): FQDN from loopback and DNS blackhole is always the same:
|
||||||
self.assertIn(getfqdn('localhost.'), ('localhost', 'localhost.'))
|
self.assertIn(getfqdn('localhost.'), ('localhost', 'localhost.'))
|
||||||
|
|
||||||
|
def testFQDN_DNS(self):
|
||||||
|
unittest.F2B.SkipIfNoNetwork()
|
||||||
self.assertIn(getfqdn('as112.arpa.'), ('as112.arpa.', 'as112.arpa'))
|
self.assertIn(getfqdn('as112.arpa.'), ('as112.arpa.', 'as112.arpa'))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue