Path: blob/master/spec/lib/rex/proto/postgresql/client_spec.rb
32485 views
# -*- coding: binary -*-12require 'spec_helper'3require 'postgres/postgres-pr/connection'45RSpec.describe Msf::Db::PostgresPR::Connection do6let(:host) { '127.0.0.1' }7let(:port) { 1234 }8let(:info) { "#{host}:#{port}" }9let(:db_name) { 'my_db_name' }10let(:socket) { double(Rex::Socket, peerhost: host, peerport: port) }11let(:message) { Msf::Db::PostgresPR::ReadyForQuery.new('') }1213before do14allow(socket).to receive(:<<)15allow(socket).to receive(:write)16allow(socket).to receive(:read).and_return('S')17allow(socket).to receive(:extend)18allow(socket).to receive(:initsock_with_ssl_version)19allow(Msf::Db::PostgresPR::Message).to receive(:read).and_return(message)20allow(Rex::Socket).to receive(:create_param).and_return(socket)21end2223subject do24described_class.new(db_name, 'username', 'password', "tcp://#{host}:#{port}")25end2627it_behaves_like 'session compatible SQL client'2829describe 'SSL connection' do30let(:ssl_request_message) { instance_double(Msf::Db::PostgresPR::SSLRequest) }31let(:ssl_opts) { { ssl_version: 'TLS1.2', ssl_verify_mode: 'peer', ssl_cipher: 'AES256' } }3233before do34allow(Msf::Db::PostgresPR::SSLRequest).to receive(:new).with(80877103).and_return(ssl_request_message)35allow(ssl_request_message).to receive(:dump).and_return('ssl_request_data')36end3738context 'when SSL is enabled and server supports SSL' do39it 'successfully establishes SSL connection' do40allow(socket).to receive(:read).with(1).and_return('S')4142expect(socket).to receive(:write).with('ssl_request_data')43expect(socket).to receive(:starttls).with(Rex::Socket::Parameters)4445client = described_class.new(db_name, 'username', 'password', "tcp://#{host}:#{port}", nil, true, ssl_opts)46expect(client).to be_a(Msf::Db::PostgresPR::Connection)47end48end4950context 'when SSL is enabled but server does not support SSL' do51it 'raises an error when server responds with N' do52allow(socket).to receive(:read).with(1).and_return('N')5354expect(socket).to receive(:write).with('ssl_request_data')55expect(socket).not_to receive(:extend)5657expect {58described_class.new(db_name, 'username', 'password', "tcp://#{host}:#{port}", nil, true, ssl_opts)59}.to raise_error("SSL connection requested but server at #{host}:#{port} does not support SSL")60end61end6263context 'when SSL is enabled but server responds unexpectedly' do64it 'raises an error for unexpected SSL response' do65allow(socket).to receive(:read).with(1).and_return('X')6667expect(socket).to receive(:write).with('ssl_request_data')68expect(socket).not_to receive(:extend)6970expect {71described_class.new(db_name, 'username', 'password', "tcp://#{host}:#{port}", nil, true, ssl_opts)72}.to raise_error('Unexpected response to SSLRequest: "X"')73end74end7576context 'when SSL is disabled' do77it 'does not attempt SSL handshake' do78expect(socket).not_to receive(:write).with('ssl_request_data')79expect(socket).not_to receive(:extend).with(Rex::Socket::SslTcp)8081client = described_class.new(db_name, 'username', 'password', "tcp://#{host}:#{port}", nil, false, ssl_opts)82expect(client).to be_a(Msf::Db::PostgresPR::Connection)83end84end85end8687describe '#map_compile_os_to_platform' do88[89{ info: 'linux', expected: Msf::Platform::Linux.realname },90{ info: 'linux2.6', expected: Msf::Platform::Linux.realname },91{ info: 'debian-linux-gnu', expected: Msf::Platform::Linux.realname },92{ info: 'win', expected: Msf::Platform::Windows.realname },93{ info: 'windows', expected: Msf::Platform::Windows.realname },94{ info: 'darwin', expected: Msf::Platform::OSX.realname },95{ info: 'osx', expected: Msf::Platform::OSX.realname },96{ info: 'macos', expected: Msf::Platform::OSX.realname },97{ info: 'solaris', expected: Msf::Platform::Solaris.realname },98{ info: 'aix', expected: Msf::Platform::AIX.realname },99{ info: 'hpux', expected: Msf::Platform::HPUX.realname },100{ info: 'irix', expected: Msf::Platform::Irix.realname },101].each do |test|102it "correctly identifies '#{test[:info]}' as '#{test[:expected]}'" do103expect(subject.map_compile_os_to_platform(test[:info])).to eq(test[:expected])104end105end106end107108describe '#map_compile_arch_to_architecture' do109[110{ info: 'x86_64', expected: ARCH_X86_64 },111{ info: 'x86_x64', expected: ARCH_X86_64 },112{ info: 'x64', expected: ARCH_X86_64 },113{ info: '64', expected: ARCH_X86_64 },114{ info: 'x86', expected: ARCH_X86 },115{ info: '86', expected: ARCH_X86 },116{ info: 'i686', expected: ARCH_X86 },117{ info: 'arm64', expected: ARCH_AARCH64 },118{ info: 'arm', expected: ARCH_ARMLE },119{ info: 'sparc', expected: ARCH_SPARC },120{ info: 'sparc64', expected: ARCH_SPARC64 },121{ info: 'ppc', expected: ARCH_PPC },122{ info: 'mips', expected: ARCH_MIPS },123].each do |test|124it "correctly identifies '#{test[:info]}' as '#{test[:expected]}'" do125expect(subject.map_compile_arch_to_architecture(test[:info])).to eq(test[:expected])126end127end128end129130describe '#detect_platform_and_arch' do131[132{ version: 'PostgreSQL 9.4.26 on x86_64-pc-linux-gnu (Debian 9.4.26-1.pgdg90+1), compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit', expected: { arch: 'x86_64', platform: 'Linux' } },133{ version: 'PostgreSQL 14.11 (Debian 14.11-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit', expected: { arch: 'x86_64', platform: 'Linux' } },134{ version: 'PostgreSQL 14.11 (Homebrew) on x86_64-apple-darwin22.6.0, compiled by Apple clang version 15.0.0 (clang-1500.1.0.2.5), 64-bit', expected: { arch: 'x86_64', platform: 'OSX' } },135{136version: 'PostgreSQL 14.11 (Homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>',137expected: {138arch: 'postgresql 14.11 (homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>',139platform: 'postgresql 14.11 (homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>'140}141}142].each do |test|143context "when the database is version #{test[:version]}" do144it "returns #{test[:expected]}" do145mock_query_result = instance_double Msf::Db::PostgresPR::Connection::Result, rows: [[test[:version]]]146allow(subject).to receive(:query).with('select version()').and_return(mock_query_result)147148expect(subject.detect_platform_and_arch).to eq test[:expected]149end150end151end152end153end154155156