1 # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
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.
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.
38 message_text
= """id 1234
45 example. 1 IN A 10.0.0.1
50 class BaseResolverTests(object):
52 if sys
.platform
!= 'win32':
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'))
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
,
64 cache
= dns
.resolver
.Cache()
65 cache
.put((name
, dns
.rdatatype
.A
, dns
.rdataclass
.IN
), answer
)
67 self
.failUnless(cache
.get((name
, dns
.rdatatype
.A
, dns
.rdataclass
.IN
))
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
,
75 cache
= dns
.resolver
.Cache(cleaning_interval
=1.0)
76 cache
.put((name
, dns
.rdatatype
.A
, dns
.rdataclass
.IN
), answer
)
78 self
.failUnless(cache
.get((name
, dns
.rdatatype
.A
, dns
.rdataclass
.IN
))
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
)
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
)
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
)
99 def testZoneForName4(self
):
101 name
= dns
.name
.from_text('dnspython.org', None)
102 zname
= dns
.resolver
.zone_for_name(name
)
103 self
.failUnlessRaises(dns
.resolver
.NotAbsolute
, bad
)
105 class PollingMonkeyPatchMixin(object):
107 self
.__native
_polling
_backend
= dns
.query
._polling
_backend
108 dns
.query
._set
_polling
_backend
(self
.polling_backend())
110 unittest
.TestCase
.setUp(self
)
113 dns
.query
._set
_polling
_backend
(self
.__native
_polling
_backend
)
115 unittest
.TestCase
.tearDown(self
)
117 class SelectResolverTestCase(PollingMonkeyPatchMixin
, BaseResolverTests
, unittest
.TestCase
):
118 def polling_backend(self
):
119 return dns
.query
._select
_for
121 if hasattr(select
, 'poll'):
122 class PollResolverTestCase(PollingMonkeyPatchMixin
, BaseResolverTests
, unittest
.TestCase
):
123 def polling_backend(self
):
124 return dns
.query
._poll
_for
126 if __name__
== '__main__':