|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'lite_spec_helper' |
| 4 | + |
| 5 | +describe Mongo::Srv::Resolver do |
| 6 | + describe '#get_records' do |
| 7 | + let(:hostname) { 'foo.example.com' } |
| 8 | + let(:srv_query) { '_mongodb._tcp.' + hostname } |
| 9 | + |
| 10 | + let(:mismatched_record) do |
| 11 | + double('record').tap do |record| |
| 12 | + allow(record).to receive(:target).and_return('evil.attacker.tld') |
| 13 | + allow(record).to receive(:port).and_return(27_017) |
| 14 | + allow(record).to receive(:ttl).and_return(1) |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + let(:dns_resolver) do |
| 19 | + instance_double(Resolv::DNS).tap do |dns| |
| 20 | + allow(dns).to receive(:timeouts=) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + before do |
| 25 | + allow(Resolv::DNS).to receive(:new).and_return(dns_resolver) |
| 26 | + end |
| 27 | + |
| 28 | + context 'when raise_on_invalid is false and a mismatched-domain record is returned' do |
| 29 | + let(:resolver) { described_class.new(raise_on_invalid: false) } |
| 30 | + |
| 31 | + before do |
| 32 | + allow(dns_resolver).to receive(:getresources) |
| 33 | + .with(srv_query, Resolv::DNS::Resource::IN::SRV) |
| 34 | + .and_return([ mismatched_record ]) |
| 35 | + end |
| 36 | + |
| 37 | + it 'does not raise and logs a warning' do |
| 38 | + expect(resolver).to receive(:log_warn).at_least(:once) |
| 39 | + expect { resolver.get_records(hostname) }.not_to raise_error |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context 'when raise_on_invalid is false and no records are returned' do |
| 44 | + let(:resolver) { described_class.new(raise_on_invalid: false) } |
| 45 | + |
| 46 | + before do |
| 47 | + allow(dns_resolver).to receive(:getresources) |
| 48 | + .with(srv_query, Resolv::DNS::Resource::IN::SRV) |
| 49 | + .and_return([]) |
| 50 | + end |
| 51 | + |
| 52 | + it 'does not raise NoSRVRecords' do |
| 53 | + allow(resolver).to receive(:log_warn) |
| 54 | + expect { resolver.get_records(hostname) }.not_to raise_error |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context 'when raise_on_invalid is true (default) and a mismatched-domain record is returned' do |
| 59 | + let(:resolver) { described_class.new } |
| 60 | + |
| 61 | + before do |
| 62 | + allow(dns_resolver).to receive(:getresources) |
| 63 | + .with(srv_query, Resolv::DNS::Resource::IN::SRV) |
| 64 | + .and_return([ mismatched_record ]) |
| 65 | + end |
| 66 | + |
| 67 | + it 'raises MismatchedDomain' do |
| 68 | + expect { resolver.get_records(hostname) } |
| 69 | + .to raise_error(Mongo::Error::MismatchedDomain) |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + context 'when raise_on_invalid is true (default) and no records are returned' do |
| 74 | + let(:resolver) { described_class.new } |
| 75 | + |
| 76 | + before do |
| 77 | + allow(dns_resolver).to receive(:getresources) |
| 78 | + .with(srv_query, Resolv::DNS::Resource::IN::SRV) |
| 79 | + .and_return([]) |
| 80 | + end |
| 81 | + |
| 82 | + it 'raises NoSRVRecords' do |
| 83 | + expect { resolver.get_records(hostname) } |
| 84 | + .to raise_error(Mongo::Error::NoSRVRecords) |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | +end |
0 commit comments