File: test_ip6trie.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 (35 lines) | stat: -rw-r--r-- 991 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
""" Basic ip6trie dataset Tests
"""
import unittest

from rbldnsd import Rbldnsd, ZoneFile

__all__ = [
    'TestIp6TrieDataset',
    ]

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

def rfc3152(ip6addr, domain='example.com'):
    from socket import inet_pton, AF_INET6
    from struct import unpack

    bytes = unpack("16B", inet_pton(AF_INET6, ip6addr))
    nibbles = '.'.join("%x.%x" % (byte & 0xf, (byte >> 4) & 0xf)
                       for byte in reversed(bytes))
    return "%s.%s" % (nibbles, domain)

class TestIp6TrieDataset(unittest.TestCase):
    def test_exclusion(self):
        with ip6trie(["dead::/16 listed",
                      "!dead::beef"]) as dnsd:
            self.assertEqual(dnsd.query(rfc3152("dead::beef")), None)
            self.assertEqual(dnsd.query(rfc3152("dead::beee")), "listed")

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