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/rex/proto/ldap/client_spec.rb
Views: 11788
1
# -*- coding: binary -*-
2
3
require 'spec_helper'
4
require 'rex/proto/ldap/client'
5
6
RSpec.describe Rex::Proto::LDAP::Client do
7
let(:host) { '127.0.0.1' }
8
let(:port) { 1234 }
9
let(:info) { "#{host}:#{port}" }
10
11
subject do
12
client = described_class.new(host: host, port: port)
13
client
14
end
15
16
it_behaves_like 'session compatible client'
17
18
let(:base_dn) { 'DC=ldap,DC=example,DC=com' }
19
let(:schema_dn) { 'CN=Schema,CN=Configuration,DC=ldap,DC=example,DC=com' }
20
21
let(:root_dse_result_ldif) do
22
"dn: \n" \
23
"namingcontexts: #{base_dn}\n" \
24
"namingcontexts: CN=Configuration,DC=ldap,DC=example,DC=com\n" \
25
"namingcontexts: CN=Schema,CN=Configuration,DC=ldap,DC=example,DC=com\n" \
26
"namingcontexts: DC=DomainDnsZones,DC=ldap,DC=example,DC=com\n" \
27
"namingcontexts: DC=ForestDnsZones,DC=ldap,DC=example,DC=com\n" \
28
"supportedldapversion: 2\n" \
29
"supportedldapversion: 3\n" \
30
"supportedsaslmechanisms: GSS-SPNEGO\n" \
31
"supportedsaslmechanisms: GSSAPI\n" \
32
"supportedsaslmechanisms: NTLM\n"
33
end
34
35
let(:schema_naming_context) do
36
"dn: \n" \
37
"schemanamingcontext: #{schema_dn}\n"
38
end
39
40
let(:empty_response) do
41
"dn: \n"
42
end
43
44
let(:schema_naming_context_result) do
45
root_dse_dataset = Net::LDAP::Dataset.read_ldif(StringIO.new(schema_naming_context))
46
root_dse_dataset.to_entries
47
end
48
49
let(:root_dse_result) do
50
root_dse_dataset = Net::LDAP::Dataset.read_ldif(StringIO.new(root_dse_result_ldif))
51
root_dse_dataset.to_entries[0]
52
end
53
54
let(:empty_response_result) do
55
root_dse_dataset = Net::LDAP::Dataset.read_ldif(StringIO.new(empty_response))
56
root_dse_dataset.to_entries
57
end
58
59
describe '#naming_contexts' do
60
61
before(:each) do
62
allow(subject).to receive(:search_root_dse).and_return(root_dse_result)
63
end
64
65
it 'should cache the result' do
66
expect(subject).to receive(:search_root_dse)
67
subject.naming_contexts
68
expect(subject).not_to receive(:search_root_dse)
69
subject.naming_contexts
70
end
71
72
context 'when no naming contexts are available' do
73
let(:root_dse_result_ldif) do
74
"dn: \n" \
75
"supportedldapversion: 2\n" \
76
"supportedldapversion: 3\n" \
77
"supportedsaslmechanisms: GSS-SPNEGO\n" \
78
"supportedsaslmechanisms: GSSAPI\n" \
79
"supportedsaslmechanisms: NTLM\n"
80
end
81
82
it 'returns an empty array' do
83
expect(subject.naming_contexts).to be_empty
84
end
85
end
86
87
context 'when naming contexts are available' do
88
it 'contains naming contexts' do
89
expect(subject.naming_contexts).not_to be_empty
90
end
91
end
92
end
93
94
describe '#base_dn' do
95
96
before(:each) do
97
allow(subject).to receive(:search_root_dse).and_return(root_dse_result)
98
end
99
100
it 'should cache the result' do
101
expect(subject).to receive(:discover_base_dn).and_call_original
102
subject.base_dn
103
expect(subject).not_to receive(:discover_base_dn)
104
subject.base_dn
105
end
106
107
context 'when no naming contexts are available' do
108
let(:root_dse_result_ldif) do
109
"dn: \n" \
110
"supportedldapversion: 2\n" \
111
"supportedldapversion: 3\n" \
112
"supportedsaslmechanisms: GSS-SPNEGO\n" \
113
"supportedsaslmechanisms: GSSAPI\n" \
114
"supportedsaslmechanisms: NTLM\n"
115
end
116
117
it 'should not find the base dn' do
118
expect(subject.base_dn).to be_nil
119
end
120
end
121
122
context 'when naming contexts are available' do
123
it 'contains naming contexts' do
124
expect(subject.base_dn).to eql(base_dn)
125
end
126
end
127
end
128
129
describe '#schema_dn' do
130
131
before(:each) do
132
allow(subject).to receive(:search).and_return(schema_naming_context_result)
133
end
134
135
it 'should cache the result' do
136
expect(subject).to receive(:discover_schema_naming_context).and_call_original
137
subject.schema_dn
138
expect(subject).not_to receive(:discover_schema_naming_context)
139
subject.schema_dn
140
end
141
142
context 'when the response does not contain the schema_dn' do
143
before(:each) do
144
allow(subject).to receive(:search).and_return(empty_response_result)
145
end
146
147
it 'does not find the schema_dn' do
148
expect(subject.schema_dn).to be_nil
149
end
150
end
151
152
context 'when the response does contain the schema_dn' do
153
it 'finds the schema_dn' do
154
expect(subject.schema_dn).to eql(schema_dn)
155
end
156
end
157
end
158
end
159
160