-
Notifications
You must be signed in to change notification settings - Fork 14.2k
/
Copy pathprivate_credential_collection_spec.rb
127 lines (109 loc) · 3.78 KB
/
private_credential_collection_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
require 'spec_helper'
require 'metasploit/framework/credential_collection'
RSpec.describe Metasploit::Framework::PrivateCredentialCollection do
subject(:collection) do
described_class.new(
nil_passwords: nil_passwords,
blank_passwords: blank_passwords,
pass_file: pass_file,
password: password,
prepended_creds: prepended_creds,
additional_privates: additional_privates
)
end
before(:each) do
# The test suite overrides File.open(...) calls; fall back to the normal behavior for any File.open calls that aren't explicitly mocked
allow(File).to receive(:open).with(anything).and_call_original
allow(File).to receive(:open).with(anything, anything).and_call_original
allow(File).to receive(:open).with(anything, anything, anything).and_call_original
end
let(:nil_passwords) { nil }
let(:blank_passwords) { nil }
let(:password) { "pass" }
let(:pass_file) { nil }
# PrivateCredentialCollection yields `nil` as the username; unlike CredentialCollection
let(:username) { nil }
let(:prepended_creds) { [] }
let(:additional_privates) { [] }
describe "#each" do
specify do
expect { |b| collection.each(&b) }.to yield_with_args(Metasploit::Framework::Credential)
end
context "when given a pass_file" do
let(:password) { nil }
let(:pass_file) do
filename = "foo"
stub_file = StringIO.new("asdf\njkl\n")
allow(File).to receive(:open).with(filename,/^r/).and_return stub_file
filename
end
specify do
expect { |b| collection.each(&b) }.to yield_successive_args(
Metasploit::Framework::Credential.new(public: username, private: "asdf"),
Metasploit::Framework::Credential.new(public: username, private: "jkl"),
)
end
end
context "when :nil_passwords is true" do
let(:nil_passwords) { true }
specify do
expect { |b| collection.each(&b) }.to yield_successive_args(
Metasploit::Framework::Credential.new(public: username, private: password),
Metasploit::Framework::Credential.new(public: username, private: nil),
)
end
end
context "when :blank_passwords is true" do
let(:blank_passwords) { true }
specify do
expect { |b| collection.each(&b) }.to yield_successive_args(
Metasploit::Framework::Credential.new(public: username, private: password),
Metasploit::Framework::Credential.new(public: username, private: ""),
)
end
end
end
describe "#empty?" do
context "when :password is not set" do
let(:username) { nil }
let(:password) { nil }
specify do
expect(collection.empty?).to eq true
end
context "and :prepended_creds is not empty" do
let(:prepended_creds) { [ "test" ] }
specify do
expect(collection.empty?).to eq false
end
end
context "and :additional_privates is not empty" do
let(:additional_privates) { [ "test_private" ] }
specify do
expect(collection.empty?).to eq false
end
end
context "and :additional_publics is not empty" do
let(:additional_publics) { [ "test_public" ] }
specify do
expect(collection.empty?).to eq true
end
end
end
context "when :password is set" do
let(:password) { 'pass' }
specify do
expect(collection.empty?).to eq false
end
end
end
describe "#prepend_cred" do
specify do
prep = Metasploit::Framework::Credential.new(public: "foo", private: "bar")
collection.prepend_cred(prep)
expect { |b| collection.each(&b) }.to yield_successive_args(
prep,
Metasploit::Framework::Credential.new(public: username, private: password),
)
end
end
end