| 1 | # Copyright (C) 2003-2007, 2009, 2010 Nominum, Inc.
|
|---|
| 2 | #
|
|---|
| 3 | # Permission to use, copy, modify, and distribute this software and its
|
|---|
| 4 | # documentation for any purpose with or without fee is hereby granted,
|
|---|
| 5 | # provided that the above copyright notice and this permission notice
|
|---|
| 6 | # appear in all copies.
|
|---|
| 7 | #
|
|---|
| 8 | # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
|
|---|
| 9 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|---|
| 10 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
|
|---|
| 11 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|---|
| 12 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|---|
| 13 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|---|
| 14 | # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|---|
| 15 |
|
|---|
| 16 | import cStringIO
|
|---|
| 17 | import select
|
|---|
| 18 | import sys
|
|---|
| 19 | import time
|
|---|
| 20 | import unittest
|
|---|
| 21 |
|
|---|
| 22 | import dns.name
|
|---|
| 23 | import dns.message
|
|---|
| 24 | import dns.name
|
|---|
| 25 | import dns.rdataclass
|
|---|
| 26 | import dns.rdatatype
|
|---|
| 27 | import dns.resolver
|
|---|
| 28 |
|
|---|
| 29 | resolv_conf = """
|
|---|
| 30 | /t/t
|
|---|
| 31 | # comment 1
|
|---|
| 32 | ; comment 2
|
|---|
| 33 | domain foo
|
|---|
| 34 | nameserver 10.0.0.1
|
|---|
| 35 | nameserver 10.0.0.2
|
|---|
| 36 | """
|
|---|
| 37 |
|
|---|
| 38 | message_text = """id 1234
|
|---|
| 39 | opcode QUERY
|
|---|
| 40 | rcode NOERROR
|
|---|
| 41 | flags QR AA RD
|
|---|
| 42 | ;QUESTION
|
|---|
| 43 | example. IN A
|
|---|
| 44 | ;ANSWER
|
|---|
| 45 | example. 1 IN A 10.0.0.1
|
|---|
| 46 | ;AUTHORITY
|
|---|
| 47 | ;ADDITIONAL
|
|---|
| 48 | """
|
|---|
| 49 |
|
|---|
| 50 | class BaseResolverTests(object):
|
|---|
| 51 |
|
|---|
| 52 | if sys.platform != 'win32':
|
|---|
| 53 | def testRead(self):
|
|---|
| 54 | f = cStringIO.StringIO(resolv_conf)
|
|---|
| 55 | r = dns.resolver.Resolver(f)
|
|---|
| 56 | self.failUnless(r.nameservers == ['10.0.0.1', '10.0.0.2'] and
|
|---|
| 57 | r.domain == dns.name.from_text('foo'))
|
|---|
| 58 |
|
|---|
| 59 | def testCacheExpiration(self):
|
|---|
| 60 | message = dns.message.from_text(message_text)
|
|---|
| 61 | name = dns.name.from_text('example.')
|
|---|
| 62 | answer = dns.resolver.Answer(name, dns.rdatatype.A, dns.rdataclass.IN,
|
|---|
| 63 | message)
|
|---|
| 64 | cache = dns.resolver.Cache()
|
|---|
| 65 | cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer)
|
|---|
| 66 | time.sleep(2)
|
|---|
| 67 | self.failUnless(cache.get((name, dns.rdatatype.A, dns.rdataclass.IN))
|
|---|
| 68 | is None)
|
|---|
| 69 |
|
|---|
| 70 | def testCacheCleaning(self):
|
|---|
| 71 | message = dns.message.from_text(message_text)
|
|---|
| 72 | name = dns.name.from_text('example.')
|
|---|
| 73 | answer = dns.resolver.Answer(name, dns.rdatatype.A, dns.rdataclass.IN,
|
|---|
| 74 | message)
|
|---|
| 75 | cache = dns.resolver.Cache(cleaning_interval=1.0)
|
|---|
| 76 | cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer)
|
|---|
| 77 | time.sleep(2)
|
|---|
| 78 | self.failUnless(cache.get((name, dns.rdatatype.A, dns.rdataclass.IN))
|
|---|
| 79 | is None)
|
|---|
| 80 |
|
|---|
| 81 | def testZoneForName1(self):
|
|---|
| 82 | name = dns.name.from_text('www.dnspython.org.')
|
|---|
| 83 | ezname = dns.name.from_text('dnspython.org.')
|
|---|
| 84 | zname = dns.resolver.zone_for_name(name)
|
|---|
| 85 | self.failUnless(zname == ezname)
|
|---|
| 86 |
|
|---|
| 87 | def testZoneForName2(self):
|
|---|
| 88 | name = dns.name.from_text('a.b.www.dnspython.org.')
|
|---|
| 89 | ezname = dns.name.from_text('dnspython.org.')
|
|---|
| 90 | zname = dns.resolver.zone_for_name(name)
|
|---|
| 91 | self.failUnless(zname == ezname)
|
|---|
| 92 |
|
|---|
| 93 | def testZoneForName3(self):
|
|---|
| 94 | name = dns.name.from_text('dnspython.org.')
|
|---|
| 95 | ezname = dns.name.from_text('dnspython.org.')
|
|---|
| 96 | zname = dns.resolver.zone_for_name(name)
|
|---|
| 97 | self.failUnless(zname == ezname)
|
|---|
| 98 |
|
|---|
| 99 | def testZoneForName4(self):
|
|---|
| 100 | def bad():
|
|---|
| 101 | name = dns.name.from_text('dnspython.org', None)
|
|---|
| 102 | zname = dns.resolver.zone_for_name(name)
|
|---|
| 103 | self.failUnlessRaises(dns.resolver.NotAbsolute, bad)
|
|---|
| 104 |
|
|---|
| 105 | class PollingMonkeyPatchMixin(object):
|
|---|
| 106 | def setUp(self):
|
|---|
| 107 | self.__native_polling_backend = dns.query._polling_backend
|
|---|
| 108 | dns.query._set_polling_backend(self.polling_backend())
|
|---|
| 109 |
|
|---|
| 110 | unittest.TestCase.setUp(self)
|
|---|
| 111 |
|
|---|
| 112 | def tearDown(self):
|
|---|
| 113 | dns.query._set_polling_backend(self.__native_polling_backend)
|
|---|
| 114 |
|
|---|
| 115 | unittest.TestCase.tearDown(self)
|
|---|
| 116 |
|
|---|
| 117 | class SelectResolverTestCase(PollingMonkeyPatchMixin, BaseResolverTests, unittest.TestCase):
|
|---|
| 118 | def polling_backend(self):
|
|---|
| 119 | return dns.query._select_for
|
|---|
| 120 |
|
|---|
| 121 | if hasattr(select, 'poll'):
|
|---|
| 122 | class PollResolverTestCase(PollingMonkeyPatchMixin, BaseResolverTests, unittest.TestCase):
|
|---|
| 123 | def polling_backend(self):
|
|---|
| 124 | return dns.query._poll_for
|
|---|
| 125 |
|
|---|
| 126 | if __name__ == '__main__':
|
|---|
| 127 | unittest.main()
|
|---|