-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathdns_proxy_spec.rb
223 lines (186 loc) · 6.46 KB
/
dns_proxy_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
require 'spec_helper'
require 'ronin/cli/commands/dns_proxy'
require_relative 'man_page_example'
describe Ronin::CLI::Commands::DnsProxy do
include_examples "man_page"
let(:nameserver1) { '8.8.8.8' }
let(:nameserver2) { '4.4.4.4' }
describe "options" do
context "when --nameserver is given" do
before do
subject.option_parser.parse(
[
'--nameserver', nameserver1,
'--nameserver', nameserver2
]
)
end
it "must append the IP address to #nameservers" do
expect(subject.nameservers).to eq([nameserver1, nameserver2])
end
end
context "when --rule is given" do
let(:record_type1) { :A }
let(:name1) { 'www.example.com' }
let(:result1) { '1.2.3.4' }
let(:record_type2) { :AAAA }
let(:name2) { 'www.example.com' }
let(:result2) { 'ffff::c0ffee' }
before do
subject.option_parser.parse(
[
'--rule', "#{record_type1}:#{name1}:#{result1}",
'--rule', "#{record_type2}:#{name2}:#{result2}"
]
)
end
it "must parse and append the result tuple to #rules" do
expect(subject.rules).to eq(
[
[record_type1, name1, result1],
[record_type2, name2, result2]
]
)
end
end
end
describe "#initialize" do
it "must initialize #nameservers to an empty Array" do
expect(subject.nameservers).to eq([])
end
it "must initialize #rules to an empty Array" do
expect(subject.rules).to eq([])
end
end
describe "#proxy_kwargs" do
it "must return a Hash containing :rules and #rules" do
expect(subject.proxy_kwargs).to eq({rules: subject.rules})
end
context "when #nameservers is not empty" do
before do
subject.nameservers << nameserver1
subject.nameservers << nameserver2
end
it "must include the :nameservers key and #nameservers" do
expect(subject.proxy_kwargs).to eq(
{
rules: subject.rules,
nameservers: subject.nameservers
}
)
end
end
end
describe "#parse_record_type" do
context "when given a DNS record type" do
let(:record_type) { 'CNAME' }
it "must return the Symbol version of the DNS record type" do
expect(subject.parse_record_type(record_type)).to eq(record_type.to_sym)
end
end
context "when given an unknown type" do
let(:record_type) { 'FOO' }
it "must raise an OptionParser::InvalidArgument" do
expect {
subject.parse_record_type(record_type)
}.to raise_error(OptionParser::InvalidArgument,"invalid argument: invalid record type: #{record_type.inspect}")
end
end
end
describe "#parse_record_name" do
let(:name) { 'www.example.com' }
it "must return the String" do
expect(subject.parse_record_name(name)).to eq(name)
end
context "when the name starts with a '/' and ends with a '/'" do
let(:name) { '/^foo\./' }
let(:regexp) { /^foo\./ }
it "must parse the name as a Regex and return a Regexp" do
expect(subject.parse_record_name(name)).to eq(regexp)
end
context "but the Regexp cannot be parsed" do
let(:name) { '/[abc/' }
it "must raise an OptionParser::InvalidArgument" do
# XXX: TruffleRuby's RegexpError exception message is different
if RUBY_ENGINE == 'truffleruby'
expect {
subject.parse_record_name(name)
}.to raise_error(OptionParser::InvalidArgument,"invalid argument: invalid Regexp: premature end of char-class (org.joni.exception.SyntaxException): /[abc/")
else
expect {
subject.parse_record_name(name)
}.to raise_error(OptionParser::InvalidArgument,"invalid argument: invalid Regexp: premature end of char-class: /[abc/")
end
end
end
end
end
describe "#parse_rule_result" do
described_class::ERROR_CODES.each do |string,symbol|
context "when given '#{string}'" do
let(:string) { string }
let(:symbol) { symbol }
it "must return a #{symbol.inspect}" do
expect(subject.parse_rule_result(string)).to eq(symbol)
end
end
end
context "when given another String" do
let(:result) { '1.2.3.4' }
it "must return that String" do
expect(subject.parse_rule_result(result)).to eq(result)
end
end
end
describe "#parse_rule" do
let(:record_type) { :A }
let(:name) { 'www.example.com' }
let(:result) { '1.2.3.4' }
let(:rule) { "#{record_type}:#{name}:#{result}" }
it "must parse the ':' separated record into a tuple of record type, name, and result" do
expect(subject.parse_rule(rule)).to eq(
[record_type, name, result]
)
end
context "when the record type is unknown" do
let(:record_type) { 'FOO' }
it "must raise an OptionParser::InvalidArgument" do
expect {
subject.parse_rule(rule)
}.to raise_error(OptionParser::InvalidArgument,"invalid argument: invalid record type: #{record_type.inspect}")
end
end
context "when the name starts with a '/' and ends with a '/'" do
let(:name) { '/^foo\./' }
let(:regexp) { /^foo\./ }
it "must parse the name field as a Regexp" do
expect(subject.parse_rule(rule)).to eq(
[record_type, regexp, result]
)
end
context "but the Regexp cannot be parsed" do
let(:name) { '/[abc/' }
it "must raise an OptionParser::InvalidArgument" do
# XXX: TruffleRuby's RegexpError exception message is different
if RUBY_ENGINE == 'truffleruby'
expect {
subject.parse_rule(rule)
}.to raise_error(OptionParser::InvalidArgument,"invalid argument: invalid Regexp: premature end of char-class (org.joni.exception.SyntaxException): /[abc/")
else
expect {
subject.parse_rule(rule)
}.to raise_error(OptionParser::InvalidArgument,"invalid argument: invalid Regexp: premature end of char-class: /[abc/")
end
end
end
end
context "when the result is a DNS error code" do
let(:result) { :NXDomain }
it "must map the result to a Symbol" do
expect(subject.parse_rule(rule)).to eq(
[record_type, name, result]
)
end
end
end
end