summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/resolv.rb10
-rw-r--r--test/resolv/test_resource.rb29
2 files changed, 30 insertions, 9 deletions
diff --git a/lib/resolv.rb b/lib/resolv.rb
index 6109278ede..ddb14bb3d1 100644
--- a/lib/resolv.rb
+++ b/lib/resolv.rb
@@ -3347,11 +3347,11 @@ class Resolv
unless (m = Regex.match(arg)) && m[1].to_i < 180
raise ArgumentError.new("not a properly formed Coord string: " + arg)
end
- hemi = (m[4][/[NE]/]) || (m[4][/[SW]/]) ? 1 : -1
- coordinates = [ ((m[1].to_i*(36e5)) + (m[2].to_i*(6e4)) +
- (m[3].to_f*(1e3))) * hemi+(2**31) ].pack("N")
- orientation = m[4][/[NS]/] ? 'lat' : 'lon'
- return Coord.new(coordinates,orientation)
+
+ dir = m[4]
+ hemi = dir[/[NE]/] ? 1 : -1
+ arc = (m[1].to_i * 3_600_000) + (m[2].to_i * 60_000) + (m[3].to_f * 1_000).to_i
+ return Coord.new([arc * hemi + (2**31)].pack("N"), dir[/[NS]/] ? "lat" : "lon")
else
raise ArgumentError.new("cannot interpret as Coord: #{arg.inspect}")
end
diff --git a/test/resolv/test_resource.rb b/test/resolv/test_resource.rb
index 434380236e..93c9946ccf 100644
--- a/test/resolv/test_resource.rb
+++ b/test/resolv/test_resource.rb
@@ -20,10 +20,6 @@ class TestResolvResource < Test::Unit::TestCase
assert_equal(@name1.hash, @name2.hash, bug10857)
end
- def test_coord
- Resolv::LOC::Coord.create('1 2 1.1 N')
- end
-
def test_srv_no_compress
# Domain name in SRV RDATA should not be compressed
issue29 = 'https://github.com/ruby/resolv/issues/29'
@@ -33,6 +29,31 @@ class TestResolvResource < Test::Unit::TestCase
end
end
+class TestResolvResourceLOC < Test::Unit::TestCase
+ def test_coord
+ assert_coord('1 2 1.1 N', 'lat', 0x8038c78c)
+ assert_coord('42 21 43.952 N', 'lat', 0x89170690)
+ assert_coord('71 5 6.344 W', 'lon', 0x70bf2dd8)
+ assert_coord('52 14 05.000 N', 'lat', 0x8b3556c8)
+ assert_coord('90 0 0.000 N', 'lat', 0x934fd900)
+ assert_coord('90 0 0.000 S', 'lat', 0x6cb02700)
+ assert_coord('00 8 50.000 E', 'lon', 0x80081650)
+ assert_coord('0 8 50.001 E', 'lon', 0x80081651)
+ assert_coord('32 07 19.000 S', 'lat', 0x791b7d28)
+ assert_coord('116 02 25.000 E', 'lon', 0x98e64868)
+ assert_coord('116 02 25.000 W', 'lon', 0x6719b798)
+ assert_raise(ArgumentError) {Resolv::LOC::Coord.create('180 0 0.001 E')}
+ assert_raise(ArgumentError) {Resolv::LOC::Coord.create('180 0 0.001 W')}
+ end
+
+ private def assert_coord(input, orientation, coordinate)
+ coord = Resolv::LOC::Coord.create(input)
+
+ assert_equal(orientation, coord.orientation)
+ assert_equal([coordinate].pack("N"), coord.coordinates)
+ end
+end
+
class TestResolvResourceCAA < Test::Unit::TestCase
def test_caa_roundtrip
raw_msg = "\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x03new\x07example\x03com\x00\x01\x01\x00\x01\x00\x00\x00\x00\x00\x16\x00\x05issueca1.example.net\xC0\x0C\x01\x01\x00\x01\x00\x00\x00\x00\x00\x0C\x80\x03tbsUnknown".b