CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/rex/proto/dns/upstream_rule_spec.rb
Views: 11789
1
# -*- coding:binary -*-
2
require 'spec_helper'
3
require 'rex/text'
4
5
RSpec.describe Rex::Proto::DNS::UpstreamRule do
6
describe '.spell_check_resolver' do
7
it 'returns nil for IPv4 addresses' do
8
address = Rex::Socket.addr_ntoa(Random.new.bytes(4))
9
expect(described_class.spell_check_resolver(address)).to be_nil
10
end
11
12
it 'returns nil for IPv6 addresses' do
13
address = Rex::Socket.addr_ntoa(Random.new.bytes(16))
14
expect(described_class.spell_check_resolver(address)).to be_nil
15
end
16
17
it 'returns nil for "black-hole"' do
18
expect(described_class.spell_check_resolver('black-hole')).to be_nil
19
end
20
21
it 'returns a populated array for "blackhole"' do
22
suggestions = described_class.spell_check_resolver('blackhole')
23
expect(suggestions).to be_a Array
24
expect(suggestions.first).to eq 'black-hole'
25
end
26
end
27
28
describe '.valid_resolver?' do
29
it 'returns true for "black-hole"' do
30
expect(described_class.valid_resolver?('black-hole')).to be_truthy
31
expect(described_class.valid_resolver?('Black-Hole')).to be_truthy
32
expect(described_class.valid_resolver?(%s[black-hole])).to be_truthy
33
end
34
35
it 'returns true for IPv4 addresses' do
36
address = Rex::Socket.addr_ntoa(Random.new.bytes(4))
37
expect(described_class.valid_resolver?(address)).to be_truthy
38
end
39
40
it 'returns true for IPv6 addresses' do
41
address = Rex::Socket.addr_ntoa(Random.new.bytes(16))
42
expect(described_class.valid_resolver?(address)).to be_truthy
43
end
44
45
it 'returns true for "static"' do
46
expect(described_class.valid_resolver?('static')).to be_truthy
47
expect(described_class.valid_resolver?('Static')).to be_truthy
48
expect(described_class.valid_resolver?(:static)).to be_truthy
49
end
50
51
it 'returns true for "system"' do
52
expect(described_class.valid_resolver?('system')).to be_truthy
53
expect(described_class.valid_resolver?('System')).to be_truthy
54
expect(described_class.valid_resolver?(:system)).to be_truthy
55
end
56
57
it 'raises returns false for invalid resolvers' do
58
expect(described_class.valid_resolver?('fake')).to be_falsey
59
end
60
end
61
62
context 'when using a wildcard condition' do
63
let(:subject) { described_class.new(wildcard: '*.metasploit.com') }
64
65
describe '#matches_all?' do
66
it 'does not return true for everything' do
67
expect(subject.matches_all?).to be_falsey
68
end
69
end
70
71
describe '#matches_name?' do
72
it 'returns true for subdomains' do
73
expect(subject.matches_name?('www.metasploit.com')).to be_truthy
74
end
75
76
it 'returns true for subsubdomains' do
77
expect(subject.matches_name?('one.two.metasploit.com')).to be_truthy
78
end
79
80
it 'returns false for the domain' do
81
expect(subject.matches_name?('metasploit.com')).to be_falsey
82
end
83
84
85
it 'returns false for other domains' do
86
expect(subject.matches_name?('notmetasploit.com')).to be_falsey
87
end
88
end
89
end
90
91
context 'when not using a wildcard condition' do
92
let(:subject) { described_class.new }
93
94
describe '#wildcard' do
95
it 'defaults to *' do
96
expect(subject.wildcard).to eq '*'
97
end
98
end
99
100
describe '#matches_all?' do
101
it 'returns true for everything' do
102
expect(subject.matches_all?).to be_truthy
103
end
104
end
105
106
describe '#matches_name?' do
107
it 'returns true for everything' do
108
expect(subject.matches_name?("#{Rex::Text.rand_text_alphanumeric(10)}.#{Rex::Text.rand_text_alphanumeric(3)}")).to be_truthy
109
end
110
end
111
end
112
end
113
114