File: test_ip4trie.py

package info (click to toggle)
rbldnsd 0.998b~pre1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 744 kB
  • ctags: 1,132
  • sloc: ansic: 8,212; python: 549; sh: 502; makefile: 249; awk: 33
file content (38 lines) | stat: -rw-r--r-- 1,233 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
""" (Very) basic ip4trie dataset tests
"""
import unittest

from rbldnsd import Rbldnsd, ZoneFile

__all__ = [
    'TestIp4TrieDataset',
    ]

def ip4trie(zone_data):
    """ Run rbldnsd with an ip6trie dataset
    """
    dnsd = Rbldnsd()
    dnsd.add_dataset('ip4trie', ZoneFile(zone_data))
    return dnsd

def reversed_ip(ip4addr, domain='example.com'):
    revip = '.'.join(reversed(ip4addr.split('.')))
    return "%s.%s" % (revip, domain)

class TestIp4TrieDataset(unittest.TestCase):
    def test_exclusion(self):
        with ip4trie(["1.2.3.0/24 listed",
                      "!1.2.3.4"]) as dnsd:
            self.assertEqual(dnsd.query(reversed_ip("1.2.3.4")), None)
            self.assertEqual(dnsd.query(reversed_ip("1.2.3.3")), "listed")
            self.assertEqual(dnsd.query(reversed_ip("1.2.3.5")), "listed")

    def test_wildcard_prefix(self):
        with ip4trie(["0/0 wild",
                      "127.0.0.1 localhost"]) as dnsd:
            self.assertEqual(dnsd.query(reversed_ip("127.0.0.1")), "localhost")
            self.assertEqual(dnsd.query(reversed_ip("0.0.0.0")), "wild")
            self.assertEqual(dnsd.query(reversed_ip("127.0.0.2")), "wild")

if __name__ == '__main__':
    unittest.main()