CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/metasploit/framework/database_spec.rb
Views: 1904
1
require 'spec_helper'
2
3
RSpec.describe Metasploit::Framework::Database do
4
context 'CONSTANTS' do
5
context 'CONFIGURATIONS_PATHNAME_PRECEDENCE' do
6
subject(:configurations_pathname_precedence) {
7
described_class::CONFIGURATIONS_PATHNAME_PRECEDENCE
8
}
9
10
it { is_expected.to match_array(
11
[
12
:environment_configurations_pathname,
13
:user_configurations_pathname,
14
:project_configurations_pathname
15
]
16
) }
17
end
18
end
19
20
context '.configurations_pathname' do
21
subject(:configurations_pathname) {
22
described_class.configurations_pathname(*arguments)
23
}
24
25
context 'with options' do
26
let(:arguments) {
27
[
28
{
29
path: path
30
}
31
]
32
}
33
34
context 'with :path' do
35
context 'that exists' do
36
let(:path) {
37
tempfile.path
38
}
39
40
let(:tempfile) {
41
Tempfile.new(['database', '.yml'])
42
}
43
44
it 'returns Pathname(path)' do
45
expect(configurations_pathname).to eq(Pathname.new(path))
46
end
47
end
48
49
context 'that does not exist' do
50
let(:path) {
51
'/a/configurations/path/that/does/not/exist/database.yml'
52
}
53
54
55
it { is_expected.to be_nil }
56
end
57
end
58
59
context 'without :path' do
60
let(:path) {
61
''
62
}
63
64
it 'calls configurations_pathnames' do
65
expect(described_class).to receive(:configurations_pathnames).and_call_original
66
67
configurations_pathname
68
end
69
70
it 'returns first pathname from configurations_pathnames' do
71
expect(configurations_pathname).to eq(described_class.configurations_pathnames.first)
72
end
73
end
74
end
75
76
context 'without options' do
77
let(:arguments) {
78
[]
79
}
80
81
it 'calls configurations_pathnames' do
82
expect(described_class).to receive(:configurations_pathnames).and_call_original
83
84
configurations_pathname
85
end
86
87
it 'returns first pathname from configurations_pathnames' do
88
expect(configurations_pathname).to eq(described_class.configurations_pathnames.first)
89
end
90
end
91
end
92
93
context '.configurations_pathnames' do
94
subject(:configurations_pathnames) {
95
described_class.configurations_pathnames
96
}
97
98
before(:example) do
99
allow(described_class).to receive(:environment_configurations_pathname).and_return(
100
environment_configurations_pathname
101
)
102
end
103
104
context 'with environment_configurations_pathname' do
105
context 'that exists' do
106
#
107
# lets
108
#
109
110
let(:environment_configurations_pathname) {
111
Pathname.new(environment_configurations_tempfile.path)
112
}
113
114
let(:environment_configurations_tempfile) {
115
Tempfile.new(['environment_configurations', '.database.yml'])
116
}
117
118
#
119
# Callbacks
120
#
121
122
before(:example) do
123
allow(described_class).to receive(:user_configurations_pathname).and_return(
124
user_configurations_pathname
125
)
126
end
127
128
context 'with user_configurations_pathname' do
129
context 'that exists' do
130
#
131
# lets
132
#
133
134
let(:user_configurations_pathname) {
135
Pathname.new(user_configurations_tempfile.path)
136
}
137
138
let(:user_configurations_tempfile) {
139
Tempfile.new(['user_configurations', '.database.yml'])
140
}
141
142
#
143
# Callbacks
144
#
145
146
before(:example) do
147
allow(described_class).to receive(:project_configurations_pathname).and_return(
148
project_configurations_pathname
149
)
150
end
151
152
context 'with project_configurations_pathname' do
153
context 'that exists' do
154
let(:project_configurations_pathname) {
155
Pathname.new(project_configurations_tempfile.path)
156
}
157
158
let(:project_configurations_tempfile) {
159
Tempfile.new(['project_configurations', '.database.yml'])
160
}
161
162
it 'is [environment_configurations_pathname, user_configurations_pathname, project_configurations_pathname]' do
163
expect(project_configurations_pathname).to exist
164
expect(configurations_pathnames).to match_array(
165
[
166
environment_configurations_pathname,
167
user_configurations_pathname,
168
project_configurations_pathname
169
]
170
)
171
end
172
end
173
174
context 'that does not exist' do
175
let(:project_configurations_pathname) {
176
Pathname.new('/metasploit-framework/does/not/exist/here/config/database.yml')
177
}
178
179
it 'is [environment_configurations_pathname, user_configurations_pathname]' do
180
expect(environment_configurations_pathname).to exist
181
expect(user_configurations_pathname).to exist
182
expect(project_configurations_pathname).not_to exist
183
184
expect(project_configurations_pathname).not_to exist
185
expect(configurations_pathnames).to match_array(
186
[
187
environment_configurations_pathname,
188
user_configurations_pathname
189
]
190
)
191
end
192
end
193
end
194
195
context 'without project_configurations_pathname' do
196
let(:project_configurations_pathname) {
197
nil
198
}
199
200
it 'is [environment_configuration_pathname, user_configurations_pathname]' do
201
expect(environment_configurations_pathname).to exist
202
expect(user_configurations_pathname).to exist
203
204
expect(configurations_pathnames).to match_array(
205
[
206
environment_configurations_pathname,
207
user_configurations_pathname
208
]
209
)
210
end
211
end
212
end
213
214
context 'with does not exist' do
215
#
216
# lets
217
#
218
219
let(:user_configurations_pathname) {
220
Pathname.new('/user/configuration/that/does/not/exist/.msf4/database.yml')
221
}
222
223
#
224
# Callbacks
225
#
226
227
before(:example) do
228
allow(described_class).to receive(:project_configurations_pathname).and_return(
229
project_configurations_pathname
230
)
231
end
232
233
context 'with project_configurations_pathname' do
234
context 'that exists' do
235
let(:project_configurations_pathname) {
236
Pathname.new(project_configurations_tempfile.path)
237
}
238
239
let(:project_configurations_tempfile) {
240
Tempfile.new(['project_configurations', '.database.yml'])
241
}
242
243
it 'is [environment_configurations_pathname, project_configurations_pathname]' do
244
expect(environment_configurations_pathname).to exist
245
expect(user_configurations_pathname).not_to exist
246
expect(project_configurations_pathname).to exist
247
248
expect(configurations_pathnames).to match_array(
249
[
250
environment_configurations_pathname,
251
project_configurations_pathname
252
]
253
)
254
end
255
end
256
257
context 'that does not exist' do
258
let(:project_configurations_pathname) {
259
Pathname.new('/metasploit-framework/that/does/not/exist/config/database.yml')
260
}
261
262
it 'is [environment_configurations_pathname]' do
263
expect(environment_configurations_pathname).to exist
264
expect(user_configurations_pathname).not_to exist
265
expect(project_configurations_pathname).not_to exist
266
267
expect(configurations_pathnames).to match_array(
268
[
269
environment_configurations_pathname
270
]
271
)
272
end
273
end
274
end
275
276
context 'without project_configurations_pathname' do
277
let(:project_configurations_pathname) {
278
nil
279
}
280
281
it 'is [environment_configurations_pathname]' do
282
expect(environment_configurations_pathname).to exist
283
expect(user_configurations_pathname).not_to exist
284
expect(project_configurations_pathname).to be_nil
285
286
expect(configurations_pathnames).to match_array(
287
[
288
environment_configurations_pathname
289
]
290
)
291
end
292
end
293
end
294
end
295
296
context 'without user_configurations_pathname' do
297
#
298
# lets
299
#
300
301
let(:user_configurations_pathname) {
302
nil
303
}
304
305
#
306
# Callbacks
307
#
308
309
before(:example) do
310
allow(described_class).to receive(:project_configurations_pathname).and_return(
311
project_configurations_pathname
312
)
313
end
314
315
context 'with project_configurations_pathname' do
316
317
end
318
319
context 'without project_configurations_pathname' do
320
let(:project_configurations_pathname) {
321
nil
322
}
323
324
it 'contains only the environment_configuration_pathname' do
325
expect(configurations_pathnames).to match_array([environment_configurations_pathname])
326
end
327
end
328
end
329
end
330
331
context 'that does not exist' do
332
333
end
334
end
335
336
context 'without environment_configurations_pathname' do
337
#
338
# lets
339
#
340
341
let(:environment_configurations_pathname) {
342
nil
343
}
344
345
#
346
# Callbacks
347
#
348
349
before(:example) do
350
allow(described_class).to receive(:user_configurations_pathname).and_return(
351
user_configurations_pathname
352
)
353
end
354
355
context 'with user_configurations_pathname' do
356
context 'that exists' do
357
#
358
# lets
359
#
360
361
let(:user_configurations_pathname) {
362
Pathname.new(user_configurations_tempfile.path)
363
}
364
365
let(:user_configurations_tempfile) {
366
Tempfile.new(['user_configurations', '.database.yml'])
367
}
368
369
#
370
# Callbacks
371
#
372
373
before(:example) do
374
allow(described_class).to receive(:project_configurations_pathname).and_return(
375
project_configurations_pathname
376
)
377
end
378
379
context 'with project_configurations_pathname' do
380
context 'that exists' do
381
let(:project_configurations_pathname) {
382
Pathname.new(project_configurations_tempfile.path)
383
}
384
385
let(:project_configurations_tempfile) {
386
Tempfile.new(['project_configurations', '.database.yml'])
387
}
388
389
it 'is [user_configurations_pathname, project_configurations_pathname]' do
390
expect(environment_configurations_pathname).to be_nil
391
expect(user_configurations_pathname).to exist
392
expect(project_configurations_pathname).to exist
393
394
expect(configurations_pathnames).to match_array(
395
[
396
user_configurations_pathname,
397
project_configurations_pathname
398
]
399
)
400
end
401
end
402
403
context 'that does not exist' do
404
let(:project_configurations_pathname) {
405
Pathname.new('/metasploit-framework/that/does/not/exist/config/database.yml')
406
}
407
408
it 'is [user_configurations_pathname]' do
409
expect(environment_configurations_pathname).to be_nil
410
expect(user_configurations_pathname).to exist
411
expect(project_configurations_pathname).not_to exist
412
413
expect(configurations_pathnames).to match_array(
414
[
415
user_configurations_pathname
416
]
417
)
418
end
419
end
420
end
421
422
context 'without project_configurations_pathname' do
423
let(:project_configurations_pathname) {
424
nil
425
}
426
427
it 'is [user_configurations_pathname]' do
428
expect(environment_configurations_pathname).to be_nil
429
expect(user_configurations_pathname).to exist
430
expect(project_configurations_pathname).to be_nil
431
432
expect(configurations_pathnames).to match_array(
433
[
434
user_configurations_pathname
435
]
436
)
437
end
438
end
439
end
440
441
context 'that does not exist' do
442
#
443
# lets
444
#
445
446
let(:user_configurations_pathname) {
447
Pathname.new('/user/configuration/that/does/not/exist/.msf4/database.yml')
448
}
449
450
#
451
# Callbacks
452
#
453
454
before(:example) do
455
allow(described_class).to receive(:project_configurations_pathname).and_return(
456
project_configurations_pathname
457
)
458
end
459
460
context 'with project_configurations_pathname' do
461
context 'that exists' do
462
let(:project_configurations_pathname) {
463
Pathname.new(project_configurations_tempfile.path)
464
}
465
466
let(:project_configurations_tempfile) {
467
Tempfile.new(['project_configurations', '.database.yml'])
468
}
469
470
it 'is [project_configurations_pathname]' do
471
expect(environment_configurations_pathname).to be_nil
472
expect(user_configurations_pathname).not_to exist
473
expect(project_configurations_pathname).to exist
474
475
expect(configurations_pathnames).to match_array(
476
[
477
project_configurations_pathname
478
]
479
)
480
end
481
end
482
483
context 'that does not exist' do
484
let(:project_configurations_pathname) {
485
Pathname.new('/metasploit-framework/that/does/not/exist/config/database.yml')
486
}
487
488
it 'is []' do
489
expect(environment_configurations_pathname).to be_nil
490
expect(user_configurations_pathname).not_to exist
491
expect(project_configurations_pathname).not_to exist
492
493
expect(configurations_pathnames).to eq([])
494
end
495
end
496
end
497
498
context 'without project_configurations_pathname' do
499
let(:project_configurations_pathname) {
500
nil
501
}
502
503
it 'is []' do
504
expect(environment_configurations_pathname).to be_nil
505
expect(user_configurations_pathname).not_to exist
506
expect(project_configurations_pathname).to be_nil
507
508
expect(configurations_pathnames).to eq([])
509
end
510
end
511
end
512
end
513
514
context 'without user_configurations_pathname' do
515
#
516
# lets
517
#
518
519
let(:user_configurations_pathname) {
520
nil
521
}
522
523
#
524
# Callbacks
525
#
526
527
before(:example) do
528
allow(described_class).to receive(:project_configurations_pathname).and_return(
529
project_configurations_pathname
530
)
531
end
532
533
context 'with project_configurations_pathname' do
534
context 'that exists' do
535
let(:project_configurations_pathname) {
536
Pathname.new(project_configurations_tempfile.path)
537
}
538
539
let(:project_configurations_tempfile) {
540
Tempfile.new(['project_configurations', '.database.yml'])
541
}
542
543
it 'is [project_configurations_pathname]' do
544
expect(environment_configurations_pathname).to be_nil
545
expect(user_configurations_pathname).to be_nil
546
expect(project_configurations_pathname).to exist
547
548
expect(configurations_pathnames).to match_array(
549
[
550
project_configurations_pathname
551
]
552
)
553
end
554
end
555
556
context 'that does not exist' do
557
let(:project_configurations_pathname) {
558
Pathname.new('/metasploit-framework/that/does/not/exist/config/database.yml')
559
}
560
561
it 'is []' do
562
expect(environment_configurations_pathname).to be_nil
563
expect(user_configurations_pathname).to be_nil
564
expect(project_configurations_pathname).not_to exist
565
566
expect(configurations_pathnames).to eq([])
567
end
568
end
569
end
570
571
context 'without project_configurations_pathname' do
572
let(:project_configurations_pathname) {
573
nil
574
}
575
576
it { is_expected.to eq([]) }
577
end
578
end
579
end
580
end
581
582
context '.environment_configurations_pathname' do
583
subject(:environment_configurations_pathname) {
584
described_class.environment_configurations_pathname
585
}
586
587
around(:example) do |example|
588
env_before = ENV.to_hash
589
590
begin
591
example.run
592
ensure
593
ENV.update(env_before)
594
end
595
end
596
597
context 'with MSF_DATABASE_CONFIG' do
598
before(:example) do
599
ENV['MSF_DATABASE_CONFIG'] = msf_database_config
600
end
601
602
context 'with blank' do
603
let(:msf_database_config) {
604
''
605
}
606
607
it { is_expected.to be_nil }
608
end
609
610
context 'without blank' do
611
let(:msf_database_config) {
612
'msf/database/config/database.yml'
613
}
614
615
it 'is Pathname of MSF_DATABASE_CONFIG' do
616
expect(environment_configurations_pathname).to eq(Pathname.new(msf_database_config))
617
end
618
end
619
end
620
621
context 'without MSF_DATABASE_CONFIG' do
622
before(:example) do
623
ENV.delete('MSF_DATABASE_CONFIG')
624
end
625
626
it { is_expected.to be_nil }
627
end
628
end
629
630
context '.project_configurations_pathname' do
631
subject(:project_configurations_pathname) {
632
described_class.project_configurations_pathname
633
}
634
635
it 'is <metasploit-framework>/config/database.yml' do
636
root = Pathname.new(__FILE__).realpath.parent.parent.parent.parent.parent
637
expect(project_configurations_pathname).to eq(root.join('config', 'database.yml'))
638
end
639
end
640
641
context '.user_configurations_pathname' do
642
subject(:user_configurations_pathname) {
643
described_class.user_configurations_pathname
644
}
645
646
#
647
# lets
648
#
649
650
let(:config_root) {
651
Dir.mktmpdir
652
}
653
654
#
655
# Callbacks
656
#
657
658
around(:example) do |example|
659
begin
660
example.run
661
ensure
662
FileUtils.remove_entry_secure config_root
663
end
664
end
665
666
before(:example) do
667
allow(Msf::Config).to receive(:config_directory).and_return(config_root)
668
end
669
670
it 'is database.yml under the user config root' do
671
expect(user_configurations_pathname).to eq(Pathname.new(config_root).join('database.yml'))
672
end
673
end
674
end
675
676