summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Halley <halley@play-bow.org>2014-01-22 05:42:50 -0800
committerBob Halley <halley@play-bow.org>2014-01-22 05:42:50 -0800
commit0b2b985c0b4b0781ddafe74ec435362a026ebf63 (patch)
treeea8e0ec3d8ea163235d9640a867d6c81c6ffbece
parentdd41cd47d4a06d79487d9eeac1adba5537cf6a95 (diff)
parent124851da6c7dbaa07e412dda1fab0e95fa70eaad (diff)
downloaddnspython-0b2b985c0b4b0781ddafe74ec435362a026ebf63.tar.gz
Merge pull request #48 from spacekpe/loc-fix
Fix issue #47 - LOC record parsing
-rw-r--r--dns/rdtypes/ANY/LOC.py26
-rw-r--r--tests/rdtypeanyloc.py68
2 files changed, 85 insertions, 9 deletions
diff --git a/dns/rdtypes/ANY/LOC.py b/dns/rdtypes/ANY/LOC.py
index 154546d..863c184 100644
--- a/dns/rdtypes/ANY/LOC.py
+++ b/dns/rdtypes/ANY/LOC.py
@@ -22,6 +22,11 @@ import dns.rdata
_pows = (1L, 10L, 100L, 1000L, 10000L, 100000L, 1000000L, 10000000L,
100000000L, 1000000000L, 10000000000L)
+# default values are in centimeters
+_default_size = 100.0
+_default_hprec = 1000000.0
+_default_vprec = 1000.0
+
def _exponent_of(what, desc):
exp = None
for i in xrange(len(_pows)):
@@ -98,13 +103,14 @@ class LOC(dns.rdata.Rdata):
'horizontal_precision', 'vertical_precision']
def __init__(self, rdclass, rdtype, latitude, longitude, altitude,
- size=1.0, hprec=10000.0, vprec=10.0):
+ size=_default_size, hprec=_default_hprec, vprec=_default_vprec):
"""Initialize a LOC record instance.
The parameters I{latitude} and I{longitude} may be either a 4-tuple
of integers specifying (degrees, minutes, seconds, milliseconds),
or they may be floating point values specifying the number of
- degrees. The other parameters are floats."""
+ degrees. The other parameters are floats. Size, horizontal precision,
+ and vertical precision are specified in centimeters."""
super(LOC, self).__init__(rdclass, rdtype)
if isinstance(latitude, int) or isinstance(latitude, long):
@@ -141,8 +147,10 @@ class LOC(dns.rdata.Rdata):
self.longitude[3], long_hemisphere, self.altitude / 100.0
)
- if self.size != 1.0 or self.horizontal_precision != 10000.0 or \
- self.vertical_precision != 10.0:
+ # do not print default values
+ if self.size != _default_size or \
+ self.horizontal_precision != _default_hprec or \
+ self.vertical_precision != _default_vprec:
text += " %0.2fm %0.2fm %0.2fm" % (
self.size / 100.0, self.horizontal_precision / 100.0,
self.vertical_precision / 100.0
@@ -152,9 +160,9 @@ class LOC(dns.rdata.Rdata):
def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
latitude = [0, 0, 0, 0]
longitude = [0, 0, 0, 0]
- size = 1.0
- hprec = 10000.0
- vprec = 10.0
+ size = _default_size
+ hprec = _default_hprec
+ vprec = _default_vprec
latitude[0] = tok.get_int()
t = tok.get_string()
@@ -240,8 +248,8 @@ class LOC(dns.rdata.Rdata):
value = token.value
if value[-1] == 'm':
value = value[0 : -1]
- vprec = float(value) * 100.0 # m -> cm
- tok.get_eol()
+ vprec = float(value) * 100.0 # m -> cm
+ tok.get_eol()
return cls(rdclass, rdtype, latitude, longitude, altitude,
size, hprec, vprec)
diff --git a/tests/rdtypeanyloc.py b/tests/rdtypeanyloc.py
new file mode 100644
index 0000000..8d9838c
--- /dev/null
+++ b/tests/rdtypeanyloc.py
@@ -0,0 +1,68 @@
+# Copyright (C) 2014 Red Hat, Inc.
+# Author: Petr Spacek <pspacek@redhat.com>
+#
+# Permission to use, copy, modify, and distribute this software and its
+# documentation for any purpose with or without fee is hereby granted,
+# provided that the above copyright notice and this permission notice
+# appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED 'AS IS' AND RED HAT DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+import unittest
+
+import dns.rrset
+import dns.rdtypes.ANY.LOC
+
+class RdtypeAnyLocTestCase(unittest.TestCase):
+
+ def testEqual1(self):
+ '''Test default values for size, horizontal and vertical precision.'''
+ r1 = dns.rrset.from_text('foo', 300, 'IN', 'LOC',
+ '49 11 42.400 N 16 36 29.600 E 227.64m')
+ r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc',
+ '49 11 42.400 N 16 36 29.600 E 227.64m '
+ '1.00m 10000.00m 10.00m')
+ self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2))
+
+ def testEqual2(self):
+ '''Test default values for size, horizontal and vertical precision.'''
+ r1 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400),
+ (16, 36, 29, 600), 22764.0) # centimeters
+ r2 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400),
+ (16, 36, 29, 600), 22764.0, # centimeters
+ 100.0, 1000000.00, 1000.0) # centimeters
+ self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2))
+
+ def testEqual3(self):
+ '''Test size, horizontal and vertical precision parsers: 100 cm == 1 m.
+
+ Parsers in from_text() and __init__() have to produce equal results.'''
+ r1 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400),
+ (16, 36, 29, 600), 22764.0,
+ 200.0, 1000.00, 200.0) # centimeters
+ r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc',
+ '49 11 42.400 N 16 36 29.600 E 227.64m '
+ '2.00m 10.00m 2.00m')[0]
+ self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2))
+
+ def testEqual4(self):
+ '''Test size, horizontal and vertical precision parsers without unit.
+
+ Parsers in from_text() and __init__() have produce equal result
+ for values with and without trailing "m".'''
+ r1 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400),
+ (16, 36, 29, 600), 22764.0,
+ 200.0, 1000.00, 200.0) # centimeters
+ r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc',
+ '49 11 42.400 N 16 36 29.600 E 227.64 '
+ '2 10 2')[0] # meters without explicit unit
+ self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2))
+
+if __name__ == '__main__':
+ unittest.main()