Skip to content

Commit

Permalink
Improves test and updates regex handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cgranleese-r7 committed Apr 18, 2024
1 parent 3481d4f commit c694522
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
11 changes: 5 additions & 6 deletions lib/postgres/postgres-pr/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def map_compile_arch_to_architecture(compile_arch)
elsif compile_arch.include?('ppc')
arch = ARCH_PPC
elsif compile_arch.match?('arm')
arch = ARCH_AARCH64
arch = ARCH_ARMLE
elsif compile_arch.match?('64')
arch = ARCH_X86_64
elsif compile_arch.match?('86') || compile_arch.match?('i686')
Expand All @@ -203,17 +203,16 @@ def map_compile_arch_to_architecture(compile_arch)
arch
end

# @return [Hash] Detect the platform and architecture of the MySQL server:
# @return [Hash] Detect the platform and architecture of the PostgreSQL server:
# * :arch [String] The server architecture.
# * :platform [String] The server platform.
def detect_platform_and_arch
result = {}

query_result = query('select version()').rows.join.match(/.*on (\w+-\w+-\w+-\w+)/).captures
server_var = query_result.join.split('-', 2)
query_result = query('select version()').rows.join.match(/on (?<architecture>\w+)-\w+-(?<platform>\w+)-\w+/)
server_vars = {
'version_compile_machine' => server_var[0],
'version_compile_os' => server_var[1]
'version_compile_machine' => query_result[:architecture],
'version_compile_os' => query_result[:platform]
}

result[:arch] = map_compile_arch_to_architecture(server_vars['version_compile_machine'])
Expand Down
19 changes: 11 additions & 8 deletions spec/lib/rex/proto/postgresql/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
{ info: '86', expected: ARCH_X86 },
{ info: 'i686', expected: ARCH_X86 },
{ info: 'arm64', expected: ARCH_AARCH64 },
{ info: 'arm', expected: ARCH_AARCH64 },
{ info: 'arm', expected: ARCH_ARMLE },
{ info: 'sparc', expected: ARCH_SPARC },
{ info: 'sparc64', expected: ARCH_SPARC64 },
{ info: 'ppc', expected: ARCH_PPC },
Expand All @@ -65,13 +65,16 @@
end

describe '#detect_platform_and_arch' do
context "when the 'select version()' query is ran" do
it 'matches the regex' do
[
{ version: '9.4', select_version_query_output: [['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']] },
{ version: '14', select_version_query_output: [['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']] },
].each do |test|
expect(test[:select_version_query_output].join).to match(/.*on (\w+-\w+-\w+-\w+)/)
[
{ 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' } },
{ 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' } },
].each do |test|

This comment has been minimized.

Copy link
@adfoster-r7

adfoster-r7 Apr 18, 2024

Contributor

Here's an extra test to add:

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

This comment has been minimized.

Copy link
@adfoster-r7

adfoster-r7 Apr 18, 2024

Contributor

Will try and grab a windows one

context "when the database is version #{test[:version]}" do
it "returns #{test[:expected]}" do
mock_query_result = instance_double Msf::Db::PostgresPR::Connection::Result, rows: [[test[:version]]]
allow(subject).to receive(:query).with('select version()').and_return(mock_query_result)

expect(subject.detect_platform_and_arch).to eq test[:expected]
end
end
end
Expand Down

0 comments on commit c694522

Please sign in to comment.