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/lib/metasploit/framework/credential_spec.rb
Views: 11784
1
require 'spec_helper'
2
require 'metasploit/framework/credential'
3
4
RSpec.describe Metasploit::Framework::Credential do
5
6
subject(:cred_detail) {
7
described_class.new
8
}
9
10
let(:public) { "public" }
11
let(:private) { "private" }
12
let(:realm) { "realm" }
13
let(:realm_type) { Metasploit::Model::Realm::Key::ACTIVE_DIRECTORY_DOMAIN }
14
let(:private_type) { :password }
15
16
it { is_expected.to respond_to :paired }
17
it { is_expected.to respond_to :private }
18
it { is_expected.to respond_to :private_type }
19
it { is_expected.to respond_to :public }
20
it { is_expected.to respond_to :realm }
21
it { is_expected.to respond_to :realm_key }
22
23
describe "#paired" do
24
it "defaults to true" do
25
expect(cred_detail.paired).to be_truthy
26
end
27
end
28
29
context 'validations' do
30
31
it 'is not valid without paired being set' do
32
expect(cred_detail).to_not be_valid
33
end
34
35
context 'when not paired' do
36
before(:example) do
37
cred_detail.paired = false
38
end
39
40
it 'is invalid without at least a public or a private' do
41
expect(cred_detail).to_not be_valid
42
end
43
44
it 'is valid with just a public' do
45
cred_detail.public = 'root'
46
expect(cred_detail).to be_valid
47
end
48
49
it 'is valid with just a private' do
50
cred_detail.private = 'toor'
51
expect(cred_detail).to be_valid
52
end
53
end
54
55
context 'when paired' do
56
before(:example) do
57
cred_detail.paired = true
58
end
59
60
it 'is invalid with only a public' do
61
cred_detail.public = 'root'
62
expect(cred_detail).to_not be_valid
63
end
64
65
it 'is invalid with only a private' do
66
cred_detail.private = 'toor'
67
expect(cred_detail).to_not be_valid
68
end
69
70
it 'is invalid with empty string for public' do
71
cred_detail.public = ''
72
cred_detail.private = 'toor'
73
expect(cred_detail).to_not be_valid
74
end
75
76
it 'is valid with empty string for private' do
77
cred_detail.public = 'root'
78
cred_detail.private = ''
79
expect(cred_detail).to be_valid
80
end
81
end
82
83
end
84
85
describe "#to_credential" do
86
subject(:cred_detail) do
87
described_class.new(public: public, private: private, realm: realm)
88
end
89
it { is_expected.to respond_to :to_credential }
90
it "should return self" do
91
expect(cred_detail.to_credential).to eq(cred_detail)
92
end
93
end
94
95
describe "#==" do
96
let(:public) { "public" }
97
let(:private) { "private" }
98
let(:realm) { "realm" }
99
subject(:cred_detail) do
100
described_class.new(public: public, private: private, realm: realm)
101
end
102
103
context "when all attributes match" do
104
let(:other) do
105
described_class.new(public: public, private: private, realm: realm)
106
end
107
specify do
108
expect(other).to eq(cred_detail)
109
end
110
end
111
112
context "when realm does not match" do
113
let(:other) do
114
described_class.new(public: public, private: private, realm: "")
115
end
116
specify do
117
expect(other).not_to eq(cred_detail)
118
end
119
end
120
121
context "when private does not match" do
122
let(:other) do
123
described_class.new(public: public, private: "", realm: realm)
124
end
125
specify do
126
expect(other).not_to eq(cred_detail)
127
end
128
end
129
130
context "when public does not match" do
131
let(:other) do
132
described_class.new(public: "", private: private, realm: realm)
133
end
134
specify do
135
expect(other).not_to eq(cred_detail)
136
end
137
end
138
context "when comparing to a different object" do
139
let(:other) {'a string'}
140
specify do
141
expect(other).not_to eq(cred_detail)
142
end
143
end
144
end
145
146
describe '#to_h' do
147
subject(:cred_detail) do
148
described_class.new(public: public, private: private, realm: realm, realm_key: realm_type, private_type: private_type)
149
end
150
it 'returns a hash in the format expect for create_credential' do
151
cred_hash = {
152
private_data: private,
153
private_type: private_type,
154
username: public,
155
realm_key: realm_type,
156
realm_value: realm
157
}
158
expect(cred_detail.to_h).to eq cred_hash
159
end
160
end
161
end
162
163