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/postgresql/client_spec.rb
Views: 1904
1
# -*- coding: binary -*-
2
3
require 'spec_helper'
4
require 'postgres/postgres-pr/connection'
5
6
RSpec.describe Msf::Db::PostgresPR::Connection do
7
let(:host) { '127.0.0.1' }
8
let(:port) { 1234 }
9
let(:info) { "#{host}:#{port}" }
10
let(:db_name) { 'my_db_name' }
11
let(:socket) { double(Rex::Socket, peerhost: host, peerport: port) }
12
let(:message) { Msf::Db::PostgresPR::ReadyForQuery.new('') }
13
14
subject do
15
allow(socket).to receive(:<<)
16
allow(Msf::Db::PostgresPR::Message).to receive(:read).and_return(message)
17
allow(Rex::Socket).to receive(:create).and_return(socket)
18
client = described_class.new(db_name, 'username', 'password', "tcp://#{host}:#{port}")
19
client
20
end
21
22
it_behaves_like 'session compatible SQL client'
23
24
describe '#map_compile_os_to_platform' do
25
[
26
{ info: 'linux', expected: Msf::Platform::Linux.realname },
27
{ info: 'linux2.6', expected: Msf::Platform::Linux.realname },
28
{ info: 'debian-linux-gnu', expected: Msf::Platform::Linux.realname },
29
{ info: 'win', expected: Msf::Platform::Windows.realname },
30
{ info: 'windows', expected: Msf::Platform::Windows.realname },
31
{ info: 'darwin', expected: Msf::Platform::OSX.realname },
32
{ info: 'osx', expected: Msf::Platform::OSX.realname },
33
{ info: 'macos', expected: Msf::Platform::OSX.realname },
34
{ info: 'solaris', expected: Msf::Platform::Solaris.realname },
35
{ info: 'aix', expected: Msf::Platform::AIX.realname },
36
{ info: 'hpux', expected: Msf::Platform::HPUX.realname },
37
{ info: 'irix', expected: Msf::Platform::Irix.realname },
38
].each do |test|
39
it "correctly identifies '#{test[:info]}' as '#{test[:expected]}'" do
40
expect(subject.map_compile_os_to_platform(test[:info])).to eq(test[:expected])
41
end
42
end
43
end
44
45
describe '#map_compile_arch_to_architecture' do
46
[
47
{ info: 'x86_64', expected: ARCH_X86_64 },
48
{ info: 'x86_x64', expected: ARCH_X86_64 },
49
{ info: 'x64', expected: ARCH_X86_64 },
50
{ info: '64', expected: ARCH_X86_64 },
51
{ info: 'x86', expected: ARCH_X86 },
52
{ info: '86', expected: ARCH_X86 },
53
{ info: 'i686', expected: ARCH_X86 },
54
{ info: 'arm64', expected: ARCH_AARCH64 },
55
{ info: 'arm', expected: ARCH_ARMLE },
56
{ info: 'sparc', expected: ARCH_SPARC },
57
{ info: 'sparc64', expected: ARCH_SPARC64 },
58
{ info: 'ppc', expected: ARCH_PPC },
59
{ info: 'mips', expected: ARCH_MIPS },
60
].each do |test|
61
it "correctly identifies '#{test[:info]}' as '#{test[:expected]}'" do
62
expect(subject.map_compile_arch_to_architecture(test[:info])).to eq(test[:expected])
63
end
64
end
65
end
66
67
describe '#detect_platform_and_arch' do
68
[
69
{ 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' } },
70
{ 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' } },
71
{ 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' } },
72
{
73
version: 'PostgreSQL 14.11 (Homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>',
74
expected: {
75
arch: 'postgresql 14.11 (homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>',
76
platform: 'postgresql 14.11 (homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>'
77
}
78
}
79
].each do |test|
80
context "when the database is version #{test[:version]}" do
81
it "returns #{test[:expected]}" do
82
mock_query_result = instance_double Msf::Db::PostgresPR::Connection::Result, rows: [[test[:version]]]
83
allow(subject).to receive(:query).with('select version()').and_return(mock_query_result)
84
85
expect(subject.detect_platform_and_arch).to eq test[:expected]
86
end
87
end
88
end
89
end
90
end
91
92