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/msfupdate_spec.rb
Views: 11704
1
require 'spec_helper'
2
3
load Metasploit::Framework.root.join('msfupdate').to_path
4
5
RSpec.describe Msfupdate do
6
7
def dummy_pathname
8
Pathname.new(File.dirname(__FILE__)).join('dummy')
9
end
10
11
def dummy_git_pathname
12
dummy_pathname.join('gitbase')
13
end
14
15
def dummy_install_pathname
16
dummy_pathname.join('installbase').join('msf3')
17
end
18
19
def dummy_apt_pathname
20
dummy_pathname.join('aptbase')
21
end
22
23
let(:msfbase_dir) do
24
dummy_git_pathname
25
end
26
27
let(:stdin) { StringIO.new("", "rb") }
28
let(:stdout) { StringIO.new("", "wb") }
29
let(:stderr) { StringIO.new("", "wb") }
30
31
subject do
32
Msfupdate.new(msfbase_dir, stdin, stdout, stderr)
33
end
34
35
before(:context) do
36
# Create some fake directories to mock our different install environments
37
dummy_pathname.mkpath
38
dummy_apt_pathname.join('.apt').mkpath
39
dummy_git_pathname.join('.git').mkpath
40
dummy_install_pathname.mkpath
41
dummy_install_pathname.join('..', 'engine').mkpath
42
FileUtils.touch(dummy_install_pathname.join('..', 'engine', 'update.rb'))
43
end
44
45
after(:context) do
46
dummy_pathname.rmtree
47
end
48
49
before(:example) do
50
# By default, we want to ensure tests never actually try to execute any
51
# of the update methods unless we are explicitly testing them
52
allow(subject).to receive(:update_binary_install!)
53
allow(subject).to receive(:update_git!)
54
end
55
56
context "#parse_args" do
57
it "doesn't alter ARGV" do
58
ARGV.clear
59
ARGV << 'foo'
60
subject.parse_args(['x', 'y'])
61
expect(ARGV).to eq ['foo']
62
end
63
64
context "with --help" do
65
let(:args) { ['--help'] }
66
67
it "calls usage" do
68
expect(subject).to receive(:usage)
69
begin
70
subject.parse_args(args)
71
rescue SystemExit
72
end
73
end
74
75
it "exits before updating" do
76
expect {subject.parse_args(args)}.to raise_error(SystemExit)
77
end
78
end
79
80
context "with --git-branch" do
81
let(:git_branch) { 'foo' }
82
let(:args) { ['--git-branch', git_branch] }
83
84
it "sets @git_branch" do
85
subject.parse_args(args)
86
expect(subject.instance_variable_get(:@git_branch)).to eq git_branch
87
end
88
89
context "without a space" do
90
let(:args) { ["--git-branch=#{git_branch}"] }
91
92
it "sets @git_branch" do
93
subject.parse_args(args)
94
expect(subject.instance_variable_get(:@git_branch)).to eq git_branch
95
end
96
end
97
end
98
99
context "with --git-remote" do
100
let(:git_remote) { 'foo' }
101
let(:args) { ['--git-remote', git_remote] }
102
103
it "sets @git_remote" do
104
subject.parse_args(args)
105
expect(subject.instance_variable_get(:@git_remote)).to eq git_remote
106
end
107
108
context "without a space" do
109
let(:args) { ["--git-remote=#{git_remote}"] }
110
111
it "sets @git_remote" do
112
subject.parse_args(args)
113
expect(subject.instance_variable_get(:@git_remote)).to eq git_remote
114
end
115
end
116
end
117
118
context "with --offline-file" do
119
let(:offline_file) { 'foo' }
120
let(:args) { ['--offline-file', offline_file] }
121
122
it "sets @offline_file" do
123
subject.parse_args(args)
124
expect(subject.instance_variable_get(:@offline_file)).to match Regexp.new(Regexp.escape(offline_file))
125
end
126
127
context "with relative path" do
128
it "transforms argument into an absolute path" do
129
subject.parse_args(args)
130
expect(subject.instance_variable_get(:@offline_file)).to eq File.join(Dir.pwd, offline_file)
131
end
132
end
133
134
context "with absolute path" do
135
let(:offline_file) { '/tmp/foo' }
136
it "accepts an absolute path" do
137
subject.parse_args(args)
138
expect(subject.instance_variable_get(:@offline_file)).to eq offline_file
139
end
140
end
141
142
context "without a space" do
143
let(:args) { ["--offline-file=#{offline_file}"] }
144
145
it "sets @offline_file" do
146
subject.parse_args(["--offline-file=#{offline_file}"])
147
expect(subject.instance_variable_get(:@offline_file)).to match Regexp.new(Regexp.escape(offline_file))
148
end
149
end
150
end
151
152
context "with wait" do
153
let(:args) { ['wait'] }
154
it "sets @actually_wait" do
155
subject.parse_args(args)
156
expect(subject.instance_variable_get(:@actually_wait)).to eq true
157
end
158
end
159
160
context "with nowait" do
161
let(:args) { ['nowait'] }
162
it "sets @actually_wait" do
163
subject.parse_args(args)
164
expect(subject.instance_variable_get(:@actually_wait)).to eq false
165
end
166
end
167
end
168
169
context "#run!" do
170
before(:example) do
171
subject.parse_args(args)
172
end
173
let(:args) { [] }
174
175
it "calls validate_args" do
176
expect(subject).to receive(:validate_args) { true }
177
subject.run!
178
end
179
180
it "exits if arguments are invalid" do
181
allow(subject).to receive(:validate_args).and_return(false)
182
expect(subject).to receive(:maybe_wait_and_exit).and_raise(SystemExit)
183
expect { subject.run! }.to raise_error(SystemExit)
184
end
185
end
186
187
context "in an apt installation" do
188
let(:msfbase_dir) { dummy_apt_pathname }
189
190
it { expect(subject.apt?).to be_truthy }
191
it { expect(subject.binary_install?).to be_falsey }
192
it { expect(subject.git?).to be_falsey }
193
194
context "#validate_args" do
195
before(:example) do
196
subject.parse_args(args)
197
end
198
199
context "with no args" do
200
let(:args) { [] }
201
it { expect(subject.validate_args).to be_truthy }
202
end
203
204
context "with --git-remote" do
205
let(:args) { ['--git-remote', 'foo'] }
206
it { expect(subject.validate_args).to be_falsey }
207
end
208
209
context "with --git-branch" do
210
let(:args) { ['--git-branch', 'foo'] }
211
it { expect(subject.validate_args).to be_falsey }
212
end
213
214
context "with --offline-file" do
215
let(:args) { ['--offline-file', 'foo'] }
216
it { expect(subject.validate_args).to be_falsey }
217
end
218
end
219
220
context "#run!" do
221
it "does not call update_binary_install!" do
222
expect(subject).not_to receive(:update_binary_install!)
223
subject.run!
224
end
225
it "does not call update_git!" do
226
expect(subject).not_to receive(:update_git!)
227
subject.run!
228
end
229
end
230
end
231
232
context "in a binary installation" do
233
let(:msfbase_dir) { dummy_install_pathname }
234
235
it { expect(subject.apt?).to be_falsey }
236
it { expect(subject.binary_install?).to be_truthy }
237
it { expect(subject.git?).to be_falsey }
238
239
context "#validate_args" do
240
before(:example) do
241
subject.parse_args(args)
242
end
243
244
context "with no args" do
245
let(:args) { [] }
246
it { expect(subject.validate_args).to be_truthy }
247
end
248
249
context "with --git-remote" do
250
let(:args) { ['--git-remote', 'foo'] }
251
it { expect(subject.validate_args).to be_falsey }
252
end
253
254
context "with --git-branch" do
255
let(:args) { ['--git-branch', 'foo'] }
256
it { expect(subject.validate_args).to be_falsey }
257
end
258
259
context "with --offline-file" do
260
let(:args) { ['--offline-file', 'foo'] }
261
it { expect(subject.validate_args).to be_truthy }
262
end
263
end
264
265
context "#run!" do
266
it "calls update_binary_install!" do
267
expect(subject).to receive(:update_binary_install!)
268
subject.run!
269
end
270
it "does not call update_git!" do
271
expect(subject).not_to receive(:update_git!)
272
subject.run!
273
end
274
end
275
276
context "#update_binary_install!" do
277
# TODO: Add more tests!
278
end
279
end
280
281
context "in a git installation" do
282
let(:msfbase_dir) { dummy_git_pathname }
283
284
it { expect(subject.apt?).to be_falsey }
285
it { expect(subject.binary_install?).to be_falsey }
286
it { expect(subject.git?).to be_truthy }
287
288
289
context "#validate_args" do
290
before(:example) do
291
subject.parse_args(args)
292
end
293
294
context "with no args" do
295
let(:args) { [] }
296
it { expect(subject.validate_args).to be_truthy }
297
end
298
299
context "with --git-remote" do
300
let(:args) { ['--git-remote', 'foo'] }
301
it { expect(subject.validate_args).to be_truthy }
302
end
303
304
context "with --git-branch" do
305
let(:args) { ['--git-branch', 'foo'] }
306
it { expect(subject.validate_args).to be_truthy }
307
end
308
309
context "with --offline-file" do
310
let(:args) { ['--offline-file', 'foo'] }
311
it { expect(subject.validate_args).to be_falsey }
312
end
313
end
314
315
context "#run!" do
316
it "does not call update_binary_install!" do
317
expect(subject).not_to receive(:update_binary_install!)
318
subject.run!
319
end
320
it "calls update_git!" do
321
expect(subject).to receive(:update_git!)
322
subject.run!
323
end
324
end
325
326
context "#update_git!" do
327
# TODO: Add more tests!
328
end
329
end
330
331
end
332
333