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/postgres/postgres-pr/message_spec.rb
Views: 1904
1
require 'postgres/postgres-pr/message'
2
3
RSpec.describe Msf::Db::PostgresPR::Message do
4
let(:mock_socket_clazz) do
5
Class.new do
6
include Rex::IO::Stream
7
8
def initialize(data)
9
@read_data = data || ''
10
end
11
12
def read(n, _opts = {})
13
result, remaining = @read_data[0...n], @read_data[n..-1]
14
@read_data = remaining
15
result
16
end
17
end
18
end
19
20
def mock_socket(data)
21
mock_socket_clazz.new(data)
22
end
23
24
describe '.read' do
25
let(:startup) { false }
26
let(:stream_bytes) { '' }
27
let(:result) { described_class.read(mock_socket(stream_bytes), startup) }
28
29
context 'when reading a SASL authentication message' do
30
# Offered SASL mechanisms
31
let(:stream_bytes) { "R\x00\x00\x00\x17\x00\x00\x00\nSCRAM-SHA-256\x00\x00".b }
32
33
it 'parses a SCRAM-SHA-256 message' do
34
expect(result).to be_a(Msf::Db::PostgresPR::AuthenticationSASL)
35
expect(result.mechanisms).to eq(['SCRAM-SHA-256'])
36
expect(result.dump).to eq(stream_bytes)
37
end
38
end
39
40
context 'when reading a SASL continue message' do
41
# First SCRAM-SHA-256 server message
42
# https://datatracker.ietf.org/doc/html/rfc7677#section-3
43
let(:stream_bytes) do
44
"R\x00\x00\x00\\\x00\x00\x00\vr=2kRpTcHEFyoG+UgDEpRBdVcJLTWh5WtxARhYOHcG27i7YxAi,s=GNpgixWS5E4INbrMf665Kw==,i=4096".b
45
end
46
47
it 'parses a generic SASL message' do
48
expect(result).to be_a(Msf::Db::PostgresPR::AuthenticationSASLContinue)
49
expect(result.value).to eq('r=2kRpTcHEFyoG+UgDEpRBdVcJLTWh5WtxARhYOHcG27i7YxAi,s=GNpgixWS5E4INbrMf665Kw==,i=4096')
50
expect(result.dump).to eq(stream_bytes)
51
end
52
end
53
54
context 'when reading a SASL final message' do
55
# Final SCRAM-SHA-256 server message
56
# https://datatracker.ietf.org/doc/html/rfc7677#section-3
57
let(:stream_bytes) do
58
"R\x00\x00\x006\x00\x00\x00\fv=b10lbmDELI8EHr/DM47cmcVcwn2n4TgQC4d1gaE6QHI=".b
59
end
60
61
it 'parses a generic SASL message' do
62
expect(result).to be_a(Msf::Db::PostgresPR::AuthenticationSASLFinal)
63
expect(result.value).to eq('v=b10lbmDELI8EHr/DM47cmcVcwn2n4TgQC4d1gaE6QHI=')
64
expect(result.dump).to eq(stream_bytes)
65
end
66
end
67
end
68
69
describe Msf::Db::PostgresPR::SASLResponseMessage do
70
# Final SCRAM-SHA-256 client message
71
# https://datatracker.ietf.org/doc/html/rfc7677#section-3
72
let(:stream_bytes) do
73
"p\x00\x00\x00lc=biws,r=8P6L/8Vv+sEUZitMm7hnoNppbiGwnpKXKGXU1HqFPGjD6gtm,p=lpmyxOY7t6U+um1eCG/LLGS0fiFsieTgDv0EwJ8etso=".b
74
end
75
76
describe '#parse' do
77
it 'parses the value' do
78
subject.parse(Msf::Db::Buffer.new(stream_bytes))
79
expect(subject).to be_a(described_class)
80
expect(subject.value).to eq('c=biws,r=8P6L/8Vv+sEUZitMm7hnoNppbiGwnpKXKGXU1HqFPGjD6gtm,p=lpmyxOY7t6U+um1eCG/LLGS0fiFsieTgDv0EwJ8etso=')
81
end
82
end
83
84
describe '#dump' do
85
it 'dumps the value' do
86
subject.parse(Msf::Db::Buffer.new(stream_bytes))
87
expect(subject.dump).to eq(stream_bytes)
88
end
89
end
90
end
91
end
92
93