From 7af4d4cd4926cac9a0b5f9b0c284520f4031ed69 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Fri, 23 Nov 2012 21:13:08 +0000 Subject: Implemented DNS protocol-test. --- lib/custodian/protocoltest/dns.rb | 95 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/custodian/protocoltest/dns.rb b/lib/custodian/protocoltest/dns.rb index d1c6ee2..3ac75a4 100644 --- a/lib/custodian/protocoltest/dns.rb +++ b/lib/custodian/protocoltest/dns.rb @@ -21,6 +21,11 @@ module Custodian # attr_reader :line + # + # Name to resolve, type to resolve, and expected results + # + attr_reader :resolve_name, :resolve_type, :resolve_expected + # @@ -41,6 +46,25 @@ module Custodian else @inverted = false end + + if ( line =~ /for\s+([^\s]+)\sresolving\s([A-Z]+)\s+as\s'([^']+)'/ ) + @resolve_name = $1.dup + @resolve_type = $2.dup + @resolve_expected = $3.dup + end + + # + # Ensure we had all the data. + # + raise ArgumentError, "Missing host to resolve" unless( @resolve_name ) + raise ArgumentError, "Missing type of record to lookup" unless( @resolve_type ) + raise ArgumentError, "Missing expected results" unless( @resolve_expected ) + + # + # The host to query against + # + @host = line.split( /\s+/)[0] + end @@ -60,12 +84,79 @@ module Custodian # Run the test. # def run_test - @error = "Not implemented" - false + + # Reset the result in case we've already run + @error = nil + + # + # Do the lookup + # + results = resolve_via( @host, @resolve_type, @resolve_name, 30 ) + return false if ( results.nil? ) + + # + # OK we have an array of results. If every one of the expected + # results is contained in those results returned then we're good. + # + @resolve_expected.split( /;/ ).each do |required| + if ( ! results.include?( required ) ) + @error = "The expected result #{required} was not found in the results: #{results.join(",")}" + return false + end + end + + # + # OK we've succeeded. + # + true end + # + # Resolve an IP + # + def resolve_via( server, ltype, name, period ) + + results = Array.new() + + begin + timeout( period ) do + + begin + Resolv::DNS.open(:nameserver=>[server]) do |dns| + case ltype + + when /^A$/ then + dns.getresources(name, Resolv::DNS::Resource::IN::A).map{ |r| results.push r.address.to_s() } + + when /^AAAA$/ then + dns.getresources(name, Resolv::DNS::Resource::IN::AAAA).map{ |r| results.push r.address.to_s() } + + when /^NS$/ then + dns.getresources(name, Resolv::DNS::Resource::IN::NS).map{ |r| results.push Resolv.getaddresses(r.name.to_s()) } + + when /^MX$/ then + dns.getresources(name, Resolv::DNS::Resource::IN::MX).map{ |r| results.push Resolv.getaddresses(r.exchange.to_s()) } + else + @error = "Unknown record type to resolve: '#{ltype}'" + return false + end + end + rescue Exception => x + @error = "Exception was received when resolving : #{x}" + return nil + end + end + rescue Timeout::Error => e + @error = "Timed-out connecting #{e}" + return nil + end + results.flatten! + results + end + + # # If the test fails then report the error. -- cgit v1.2.1