Permalink
Browse files

Update DNS and Name classes to properly handle invalid input

  • Loading branch information...
1 parent 012af4c commit 924c145724ec60b78b446ed59a44db5824af1450 @jpf committed Jul 25, 2009
Showing with 32 additions and 25 deletions.
  1. +3 −3 lib/domain-profiler/dns.rb
  2. +11 −12 lib/domain-profiler/name.rb
  3. +5 −5 spec/dns/dns_spec.rb
  4. +13 −5 spec/name/name_spec.rb
@@ -4,7 +4,7 @@ def initialize(input)
@input = input
end
def mx_value(n)
- return 'none' unless (defined? @input[:type]) and @input[:type] == 'MX'
+ return :none unless (defined? @input[:type]) and @input[:type] == 'MX'
@input[:answer].split(' ')[n]
end
# Helper for the :mx method
@@ -17,9 +17,9 @@ def host
end
def method_missing(type)
begin
- @input[type] ||= 'none'
+ @input[type] ||= :none
rescue
- 'none'
+ :none
end
end
end
@@ -15,8 +15,7 @@ def initialize
:google => { :name => 'Google, Inc.', :dba => ['aspmx.l.google.com', 'google.com', 'googlemail.com', 'l.google.com','google'] },
:markmonitor => { :name => 'MarkMonitor, Inc.', :dba => ['markmonitor inc.'] },
:microsoft => { :name => 'Microsoft Corporation', :dba => ['msft.net'] },
- :netsol => { :name => 'Network Solutions, LLC', :dba => ['network solutions, llc.','network solutions llc'] }, # Network Solutions
- :none => { :name => 'none', :dba => ['none'] }, #FIXME: This cleans up after the DNS class
+ :netsol => { :name => 'Network Solutions, LLC', :dba => ['network solutions, llc.','network solutions llc'] },
:oneandone => { :name => '1 & 1 Internet, Inc.', :dba => ['1 & 1 internet ag','1and1.com'] },
:pair => { :name => 'pair Networks, Inc.', :dba => ['pair networks', 'pair.com'] },
:postini => { :name => 'Google, Inc. (Postini)', :dba => ['psmtp.com'] },
@@ -31,26 +30,26 @@ def initialize
:verisign => { :name => 'VeriSign', :dba => ['verisign trust network', 'verisign, inc.'] },
:yahoo => { :name => 'Yahoo! Inc.', :dba => ['yahoo.com','inktomi corporation'] },
}
+
+ @lookup = {}
+ @aliases.keys.each {|shortname|
+ @aliases[shortname][:dba].each {|dba|
+ @lookup[dba] = shortname
+ }
+ }
end
def shorten(input)
+ return input unless input.is_a? String
+ return input unless input.match('.')
host = input.split('.')
return "#{host[-2]}.#{host[-1]}" if host[-1].match(/(com|net|org)/)
host.shift if host.size > 2
host.join('.')
end
def simplify(input,match=nil)
- return '' if (not defined? input) or input.nil?
return input unless input.is_a? String
-
- lookup = {}
- @aliases.keys.each {|shortname|
- @aliases[shortname][:dba].each {|dba|
- lookup[dba] = shortname
- }
- }
-
return :self if (match and simplify(input) == simplify(match))
- return lookup[input.downcase] if lookup[input.downcase]
+ return @lookup[input.downcase] if @lookup[input.downcase]
return :self if (match and simplify(input).gsub(/\W/,'').downcase.include? match.split(/\./)[-2])
input
end
View
@@ -44,12 +44,12 @@
it "DNSType is able to deal with empty input" do
empty = DNSType.new('')
- empty.ttl.should == 'none'
+ empty.ttl.should == :none
end
it "DNSType is able to deal with nil input" do
empty = DNSType.new(nil)
- empty.ttl.should == 'none'
+ empty.ttl.should == :none
end
it "DNSQuery is able to deal with empty input" do
@@ -65,21 +65,21 @@
it " is able to deal with empty input" do
empty = DNS.new
empty.parse('')
- empty.a[0].ttl.should == 'none'
+ empty.a[0].ttl.should == :none
end
it "is able to deal with nil input" do
empty = DNS.new
empty.parse(nil)
- empty.a[0].ttl.should == 'none'
+ empty.a[0].ttl.should == :none
end
it "is able to deal with missing host information" do
filename = File.expand_path(File.dirname(__FILE__) + '/fixtures/furbo.org.raw.dump')
data = `cat #{filename}`
@dns = DNS.new
@dns.parse(data)
- @dns.mx[0].host.should == 'none'
+ @dns.mx[0].host.should == :none
end
View
@@ -25,36 +25,44 @@
end
}
- it "shortens easydns.com, easydns.net, easydns.org to 'easydns'" do
+ it "simplifies easydns.com, easydns.net, easydns.org to 'easydns'" do
host = Name.new()
["easydns.com", "easydns.net", "easydns.org"].each {|name|
host.simplify(name).should == :easydns
}
end
- it "shortens EASYDNS.COM to 'easydns'" do
+ it "simplifies EASYDNS.COM to 'easydns'" do
host = Name.new()
host.simplify('EASYDNS.COM').should == :easydns
end
- it "shortens easydns.com to 'self' when the second option is 'easydns.net'" do
+ it "simplifies easydns.com to 'self' when the second option is 'easydns.net'" do
host = Name.new()
host.simplify('easydns.com','easydns.net').should == :self
end
- it "shortens google.com to 'self' when the second option is 'google.com'" do
+ it "simplifies google.com to 'self' when the second option is 'google.com'" do
host = Name.new()
host.simplify('google.com','google.com').should == :self
end
it "correctly handles an empty string as input" do
host = Name.new()
host.simplify('').should == ''
+ host.shorten('').should == ''
end
it "correctly handles nil as input" do
host = Name.new()
- host.simplify(nil).should == ''
+ host.simplify(nil).should == nil
+ host.shorten(nil).should == nil
+ end
+
+ it "correctly handles :none as input" do
+ host = Name.new()
+ host.simplify(:none).should == :none
+ host.shorten(:none).should == :none
end
end

0 comments on commit 924c145

Please sign in to comment.