PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/dnsel_spec.rb

https://github.com/bendiken/tor-ruby
Ruby | 72 lines | 60 code | 12 blank | 0 comment | 12 complexity | 01342c38c4a7eac7c2ce68f65a7bf4fb MD5 | raw file
Possible License(s): Unlicense
  1. require File.join(File.dirname(__FILE__), 'spec_helper')
  2. describe Tor::DNSEL do
  3. before :all do
  4. $VERBOSE = nil # silence 'warning: already initialized constant' notices
  5. Resolv::DNS::Config::InitialTimeout = (ENV['TIMEOUT'] || 0.1).to_f
  6. end
  7. describe "Tor::DNSEL.include?" do
  8. it "returns true for exit nodes" do
  9. expect(Tor::DNSEL.include?('185.220.101.21')).to be_truthy
  10. end
  11. it "returns false for non-exit nodes" do
  12. expect(Tor::DNSEL.include?('1.2.3.4')).to be_falsey
  13. end
  14. it "returns nil on DNS timeouts" do
  15. begin
  16. Tor::DNSEL::RESOLVER = Resolv::DNS.new
  17. class << Tor::DNSEL::RESOLVER
  18. def each_address(host, &block)
  19. raise Resolv::ResolvTimeout
  20. end
  21. end
  22. expect(Tor::DNSEL.include?('1.2.3.4')).to be_nil
  23. ensure
  24. Tor::DNSEL::RESOLVER = Resolv::DefaultResolver
  25. end
  26. end
  27. end
  28. describe "Tor::DNSEL.query" do
  29. it "returns '127.0.0.2' for exit nodes" do
  30. expect Tor::DNSEL.query('185.220.101.21') == '127.0.0.2'
  31. end
  32. it "raises ResolvError for non-exit nodes" do
  33. expect(lambda { Tor::DNSEL.query('1.2.3.4') }).to raise_error(Resolv::ResolvError)
  34. end
  35. end
  36. describe "Tor::DNSEL.dnsname" do
  37. it "returns the correct DNS name" do
  38. expect Tor::DNSEL.dnsname('1.2.3.4') == '4.3.2.1.dnsel.torproject.org'
  39. end
  40. end
  41. describe "Tor::DNSEL.getaddress" do
  42. it "resolves IPv4 addresses" do
  43. expect Tor::DNSEL.getaddress('127.0.0.1') == '127.0.0.1'
  44. expect Tor::DNSEL.getaddress(IPAddr.new('127.0.0.1')) == '127.0.0.1'
  45. end
  46. it "resolves local hostnames" do
  47. expect Tor::DNSEL.getaddress('localhost') == '127.0.0.1'
  48. end
  49. it "resolves public hostnames" do
  50. expect(Tor::DNSEL.getaddress('google.com')).to match(Resolv::IPv4::Regex)
  51. end
  52. it "raises ArgumentError for IPv6 addresses" do
  53. expect(lambda { Tor::DNSEL.getaddress('::1') }).to raise_error(ArgumentError)
  54. expect(lambda { Tor::DNSEL.getaddress(IPAddr.new('::1')) }).to raise_error(ArgumentError)
  55. end
  56. it "raises ResolvError for nonexistent hostnames" do
  57. expect(lambda { Tor::DNSEL.getaddress('foo.example.org') }).to raise_error(Resolv::ResolvError)
  58. end
  59. end
  60. end