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/rex/proto/ldap/dn_binary_spec.rb
Views: 1904
1
require 'securerandom'
2
3
RSpec.describe Rex::Proto::LDAP::DnBinary do
4
let(:dn) do
5
'CN=User,CN=Users,DC=msf,DC=local'
6
end
7
8
let(:data) do
9
'abc123'
10
end
11
12
let(:sample) do
13
described_class.new(dn, data)
14
end
15
16
it 'encodes to the expected value' do
17
expect(sample.encode).to eq('B:12:616263313233:CN=User,CN=Users,DC=msf,DC=local')
18
end
19
20
it 'encodes an empty value' do
21
initial = described_class.new(dn, '')
22
encoded = initial.encode
23
expect(encoded).to eq('B:0::CN=User,CN=Users,DC=msf,DC=local')
24
decoded = described_class.decode(encoded)
25
expect(decoded.data).to eq('')
26
end
27
28
it 'throws exception with completely wrong format' do
29
expect { described_class.decode('definitely not a DN string') }.to raise_error(ArgumentError)
30
end
31
32
it 'throws exception without DN' do
33
expect { described_class.decode('B:12:616263313233') }.to raise_error(ArgumentError)
34
end
35
36
it 'throws exception on odd number of hex chars' do
37
expect { described_class.decode('B:11:61626331323:the_dn') }.to raise_error(ArgumentError)
38
end
39
40
it 'throws exception on inconsistent number of hex chars' do
41
expect { described_class.decode('B:12:626331323:the_dn') }.to raise_error(ArgumentError)
42
end
43
44
it 'reversibly decodes a random value' do
45
data = SecureRandom.bytes((SecureRandom.rand * 100).to_i + 1)
46
initial = described_class.new(dn, data)
47
encoded = initial.encode
48
decoded = described_class.decode(encoded)
49
expect(decoded.dn).to eq(initial.dn)
50
expect(decoded.data).to eq(initial.data)
51
end
52
end
53
54