forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsfupdate_spec.rb
331 lines (268 loc) · 8.73 KB
/
msfupdate_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
require 'spec_helper'
load Metasploit::Framework.root.join('msfupdate').to_path
RSpec.describe Msfupdate do
def dummy_pathname
Pathname.new(File.dirname(__FILE__)).join('dummy')
end
def dummy_git_pathname
dummy_pathname.join('gitbase')
end
def dummy_install_pathname
dummy_pathname.join('installbase').join('msf3')
end
def dummy_apt_pathname
dummy_pathname.join('aptbase')
end
let(:msfbase_dir) do
dummy_git_pathname
end
let(:stdin) { StringIO.new("", "rb") }
let(:stdout) { StringIO.new("", "wb") }
let(:stderr) { StringIO.new("", "wb") }
subject do
Msfupdate.new(msfbase_dir, stdin, stdout, stderr)
end
before(:context) do
# Create some fake directories to mock our different install environments
dummy_pathname.mkpath
dummy_apt_pathname.join('.apt').mkpath
dummy_git_pathname.join('.git').mkpath
dummy_install_pathname.mkpath
dummy_install_pathname.join('..', 'engine').mkpath
FileUtils.touch(dummy_install_pathname.join('..', 'engine', 'update.rb'))
end
after(:context) do
dummy_pathname.rmtree
end
before(:example) do
# By default, we want to ensure tests never actually try to execute any
# of the update methods unless we are explicitly testing them
allow(subject).to receive(:update_binary_install!)
allow(subject).to receive(:update_git!)
end
context "#parse_args" do
it "doesn't alter ARGV" do
ARGV.clear
ARGV << 'foo'
subject.parse_args(['x', 'y'])
expect(ARGV).to eq ['foo']
end
context "with --help" do
let(:args) { ['--help'] }
it "calls usage" do
expect(subject).to receive(:usage)
begin
subject.parse_args(args)
rescue SystemExit
end
end
it "exits before updating" do
expect {subject.parse_args(args)}.to raise_error(SystemExit)
end
end
context "with --git-branch" do
let(:git_branch) { 'foo' }
let(:args) { ['--git-branch', git_branch] }
it "sets @git_branch" do
subject.parse_args(args)
expect(subject.instance_variable_get(:@git_branch)).to eq git_branch
end
context "without a space" do
let(:args) { ["--git-branch=#{git_branch}"] }
it "sets @git_branch" do
subject.parse_args(args)
expect(subject.instance_variable_get(:@git_branch)).to eq git_branch
end
end
end
context "with --git-remote" do
let(:git_remote) { 'foo' }
let(:args) { ['--git-remote', git_remote] }
it "sets @git_remote" do
subject.parse_args(args)
expect(subject.instance_variable_get(:@git_remote)).to eq git_remote
end
context "without a space" do
let(:args) { ["--git-remote=#{git_remote}"] }
it "sets @git_remote" do
subject.parse_args(args)
expect(subject.instance_variable_get(:@git_remote)).to eq git_remote
end
end
end
context "with --offline-file" do
let(:offline_file) { 'foo' }
let(:args) { ['--offline-file', offline_file] }
it "sets @offline_file" do
subject.parse_args(args)
expect(subject.instance_variable_get(:@offline_file)).to match Regexp.new(Regexp.escape(offline_file))
end
context "with relative path" do
it "transforms argument into an absolute path" do
subject.parse_args(args)
expect(subject.instance_variable_get(:@offline_file)).to eq File.join(Dir.pwd, offline_file)
end
end
context "with absolute path" do
let(:offline_file) { '/tmp/foo' }
it "accepts an absolute path" do
subject.parse_args(args)
expect(subject.instance_variable_get(:@offline_file)).to eq offline_file
end
end
context "without a space" do
let(:args) { ["--offline-file=#{offline_file}"] }
it "sets @offline_file" do
subject.parse_args(["--offline-file=#{offline_file}"])
expect(subject.instance_variable_get(:@offline_file)).to match Regexp.new(Regexp.escape(offline_file))
end
end
end
context "with wait" do
let(:args) { ['wait'] }
it "sets @actually_wait" do
subject.parse_args(args)
expect(subject.instance_variable_get(:@actually_wait)).to eq true
end
end
context "with nowait" do
let(:args) { ['nowait'] }
it "sets @actually_wait" do
subject.parse_args(args)
expect(subject.instance_variable_get(:@actually_wait)).to eq false
end
end
end
context "#run!" do
before(:example) do
subject.parse_args(args)
end
let(:args) { [] }
it "calls validate_args" do
expect(subject).to receive(:validate_args) { true }
subject.run!
end
it "exits if arguments are invalid" do
allow(subject).to receive(:validate_args).and_return(false)
expect(subject).to receive(:maybe_wait_and_exit).and_raise(SystemExit)
expect { subject.run! }.to raise_error(SystemExit)
end
end
context "in an apt installation" do
let(:msfbase_dir) { dummy_apt_pathname }
it { expect(subject.apt?).to be_truthy }
it { expect(subject.binary_install?).to be_falsey }
it { expect(subject.git?).to be_falsey }
context "#validate_args" do
before(:example) do
subject.parse_args(args)
end
context "with no args" do
let(:args) { [] }
it { expect(subject.validate_args).to be_truthy }
end
context "with --git-remote" do
let(:args) { ['--git-remote', 'foo'] }
it { expect(subject.validate_args).to be_falsey }
end
context "with --git-branch" do
let(:args) { ['--git-branch', 'foo'] }
it { expect(subject.validate_args).to be_falsey }
end
context "with --offline-file" do
let(:args) { ['--offline-file', 'foo'] }
it { expect(subject.validate_args).to be_falsey }
end
end
context "#run!" do
it "does not call update_binary_install!" do
expect(subject).not_to receive(:update_binary_install!)
subject.run!
end
it "does not call update_git!" do
expect(subject).not_to receive(:update_git!)
subject.run!
end
end
end
context "in a binary installation" do
let(:msfbase_dir) { dummy_install_pathname }
it { expect(subject.apt?).to be_falsey }
it { expect(subject.binary_install?).to be_truthy }
it { expect(subject.git?).to be_falsey }
context "#validate_args" do
before(:example) do
subject.parse_args(args)
end
context "with no args" do
let(:args) { [] }
it { expect(subject.validate_args).to be_truthy }
end
context "with --git-remote" do
let(:args) { ['--git-remote', 'foo'] }
it { expect(subject.validate_args).to be_falsey }
end
context "with --git-branch" do
let(:args) { ['--git-branch', 'foo'] }
it { expect(subject.validate_args).to be_falsey }
end
context "with --offline-file" do
let(:args) { ['--offline-file', 'foo'] }
it { expect(subject.validate_args).to be_truthy }
end
end
context "#run!" do
it "calls update_binary_install!" do
expect(subject).to receive(:update_binary_install!)
subject.run!
end
it "does not call update_git!" do
expect(subject).not_to receive(:update_git!)
subject.run!
end
end
context "#update_binary_install!" do
# TODO: Add more tests!
end
end
context "in a git installation" do
let(:msfbase_dir) { dummy_git_pathname }
it { expect(subject.apt?).to be_falsey }
it { expect(subject.binary_install?).to be_falsey }
it { expect(subject.git?).to be_truthy }
context "#validate_args" do
before(:example) do
subject.parse_args(args)
end
context "with no args" do
let(:args) { [] }
it { expect(subject.validate_args).to be_truthy }
end
context "with --git-remote" do
let(:args) { ['--git-remote', 'foo'] }
it { expect(subject.validate_args).to be_truthy }
end
context "with --git-branch" do
let(:args) { ['--git-branch', 'foo'] }
it { expect(subject.validate_args).to be_truthy }
end
context "with --offline-file" do
let(:args) { ['--offline-file', 'foo'] }
it { expect(subject.validate_args).to be_falsey }
end
end
context "#run!" do
it "does not call update_binary_install!" do
expect(subject).not_to receive(:update_binary_install!)
subject.run!
end
it "calls update_git!" do
expect(subject).to receive(:update_git!)
subject.run!
end
end
context "#update_git!" do
# TODO: Add more tests!
end
end
end