2020-01-24 16:09:09 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DnsHelpers
|
|
|
|
def block_dns!
|
|
|
|
stub_all_dns!
|
|
|
|
stub_invalid_dns!
|
|
|
|
permit_local_dns!
|
|
|
|
end
|
|
|
|
|
|
|
|
def permit_dns!
|
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_all_dns!
|
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).with(anything, anything, nil, :STREAM).and_return([])
|
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).with(anything, anything, nil, :STREAM, anything, anything).and_return([])
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_invalid_dns!
|
2020-02-06 04:09:06 -05:00
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).with(/\Afoobar\.\w|(\d{1,3}\.){4,}\d{1,3}\z/i, anything, nil, :STREAM) do
|
2020-01-24 16:09:09 -05:00
|
|
|
raise SocketError.new("getaddrinfo: Name or service not known")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def permit_local_dns!
|
2020-02-06 04:09:06 -05:00
|
|
|
local_addresses = /\A(127|10)\.0\.0\.\d{1,3}|(192\.168|172\.16)\.\d{1,3}\.\d{1,3}|0\.0\.0\.0|localhost\z/i
|
2020-01-24 16:09:09 -05:00
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).with(local_addresses, anything, nil, :STREAM).and_call_original
|
2020-08-26 11:10:29 -04:00
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).with(local_addresses, anything, nil, :STREAM, anything, anything, any_args).and_call_original
|
2020-01-24 16:09:09 -05:00
|
|
|
end
|
|
|
|
end
|