"Fossies" - the Fresh Open Source Software Archive

Member "rbldnsd-0.998/test_acl.py" (7 Apr 2013, 1502 Bytes) of package /linux/misc/dns/rbldnsd-0.998.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 """ Tests for the acl dataset
    2 """
    3 from tempfile import NamedTemporaryFile
    4 import unittest
    5 
    6 from rbldnsd import ZoneFile, Rbldnsd, QueryRefused
    7 
    8 __all__ = [
    9     'TestAclDataset',
   10     ]
   11 
   12 def daemon(acl, addr='localhost'):
   13     """ Create an Rbldnsd instance with given ACL
   14     """
   15     acl_zone = NamedTemporaryFile()
   16     acl_zone.writelines("%s\n" % line for line in acl)
   17     acl_zone.flush()
   18 
   19     dnsd = Rbldnsd(daemon_addr=addr)
   20     dnsd.add_dataset('acl', acl_zone)
   21     dnsd.add_dataset('generic', ZoneFile(['test TXT "Success"']))
   22     return dnsd
   23 
   24 class TestAclDataset(unittest.TestCase):
   25     def test_refuse_ipv4(self):
   26         with daemon(acl=["127.0.0.1 :refuse"],
   27                     addr='127.0.0.1') as dnsd:
   28             self.assertRaises(QueryRefused, dnsd.query, 'test.example.com')
   29 
   30     def test_pass_ipv4(self):
   31         with daemon(acl=[ "0.0.0.0/0 :refuse",
   32                           "127.0.0.1 :pass" ],
   33                     addr='127.0.0.1') as dnsd:
   34             self.assertEqual(dnsd.query('test.example.com'), 'Success')
   35 
   36     def test_refuse_ipv6(self):
   37         with daemon(acl=["::1 :refuse"],
   38                     addr='::1') as dnsd:
   39             self.assertRaises(QueryRefused, dnsd.query, 'test.example.com')
   40 
   41     def test_pass_ipv6(self):
   42         with daemon(acl=[ "0/0 :refuse",
   43                           "0::1 :pass" ],
   44                     addr='::1') as dnsd:
   45             self.assertEqual(dnsd.query('test.example.com'), 'Success')
   46 
   47 if __name__ == '__main__':
   48     unittest.main()