Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'upstream/2.3.x+win32-getregistry-fix' i…
Browse files Browse the repository at this point in the history
…nto 2.3.x
  • Loading branch information
g-bougard committed Jan 16, 2017
2 parents 8caeb23 + 74bef54 commit dcabd49
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lib/FusionInventory/Agent/Tools/Win32.pm
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@ sub _getWMIObjects {
sub getRegistryValue {
my (%params) = @_;

if (!$params{path}) {
$params{logger}->error(
"No registry value path provided"
) if $params{logger};
return;
}

my ($root, $keyName, $valueName);
if ($params{path} =~ m{^(HKEY_\S+)/(.+)/([^/]+)} ) {
if ($params{path} =~ m{^(HKEY_\w+.*)/([^/]+)/([^/]+)} ) {
$root = $1;
$keyName = $2;
$valueName = $3;
Expand Down Expand Up @@ -217,8 +224,15 @@ sub getRegistryValue {
sub getRegistryKey {
my (%params) = @_;

if (!$params{path}) {
$params{logger}->error(
"No registry key path provided"
) if $params{logger};
return;
}

my ($root, $keyName);
if ($params{path} =~ m{^(HKEY_\S+)/(.+)} ) {
if ($params{path} =~ m{^(HKEY_\w+.*)/([^/]+)} ) {
$root = $1;
$keyName = $2;
} else {
Expand Down
149 changes: 148 additions & 1 deletion t/agent/tools/win32.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use warnings;
use utf8;
use lib 't/lib';

use constant REG_DWORD => 0x4;
use constant REG_SZ => 0x7;

use English qw(-no_match_vars);
use Test::Deep qw(cmp_deeply);
use Test::MockModule;
Expand Down Expand Up @@ -146,10 +149,115 @@ my %tests = (
]
);

# Emulated registry
my %register = (
'HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node' => {
'TeamViewer' => {
# Values key begins with a slash
'/ClientID' => '0x12345678',
'/Version' => '12.0.72365',
# Subkey ends with a slash
'subkey/' => {
'/value' => ''
}
}
},
'HKEY_LOCAL_MACHINE/CurrentControlSet/Control/Session Manager' => {
'Environment' => {
'/TEMP' => '%SystemRoot%\\TEMP',
'/OS' => 'Windows_NT',
}
}
);

my %regkey_tests = (
'nopath' => {
_expected => undef
},
'undef-path' => {
path => undef,
_expected => undef
},
'emptypath' => {
path => '',
_expected => undef
},
'badroot' => {
path => 'HKEY_NOT_A_ROOT/Not_existing_Key_path',
_expected => undef
},
'teamviewer' => {
path => 'HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node/TeamViewer',
_expected => bless({
'/ClientID' => '0x12345678',
'/Version' => '12.0.72365',
'subkey/' => bless({
'/value' => ''
}, 'Win32::TieRegistry')
}, 'Win32::TieRegistry')
},
'environment' => {
path => 'HKEY_LOCAL_MACHINE/CurrentControlSet/Control/Session Manager/Environment',
_expected => bless({
'/TEMP' => '%SystemRoot%\\TEMP',
'/OS' => 'Windows_NT'
}, 'Win32::TieRegistry')
}
);

my %regval_tests = (
'nopath' => {
_expected => undef
},
'undef-path' => {
path => undef,
_expected => undef
},
'emptypath' => {
path => '',
_expected => undef
},
'badroot' => {
path => 'HKEY_NOT_A_ROOT/Not_existing_Key_path',
_expected => undef
},
'teamviewerid' => {
path => 'HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node/TeamViewer/ClientID',
_expected => '0x12345678'
},
'teamviewer-all' => {
path => 'HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node/TeamViewer/*',
_expected => {
'ClientID' => '0x12345678',
'Version' => '12.0.72365',
'subkey/' => undef
}
},
'teamviewerid-withtype' => {
path => 'HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node/TeamViewer/ClientID',
withtype => 1,
_expected => [ '0x12345678', REG_DWORD ]
},
'teamviewer-all-withtype' => {
path => 'HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node/TeamViewer/*',
withtype => 1,
_expected => {
'ClientID' => [ '0x12345678', REG_DWORD ],
'Version' => [ '12.0.72365', REG_SZ ],
'subkey/' => []
}
},
'temp-env' => {
path => 'HKEY_LOCAL_MACHINE/CurrentControlSet/Control/Session Manager/Environment/TEMP',
_expected => '%SystemRoot%\\TEMP'
}
);

my $win32_only_test_count = 7;

plan tests =>
(scalar keys %tests) + $win32_only_test_count;
(scalar keys %tests) + $win32_only_test_count +
(scalar keys %regkey_tests) + (scalar keys %regval_tests);

FusionInventory::Agent::Tools::Win32->require();
FusionInventory::Agent::Tools::Win32->use('getInterfaces');
Expand Down Expand Up @@ -214,3 +322,42 @@ SKIP: {
exit(0);
}
}

$module->mock(
'_getRegistryKey',
sub {
my (%params) = @_;
return unless ($params{root} && $params{keyName});
return unless exists($register{$params{root}});
my $root = $register{$params{root}};
return unless exists($root->{$params{keyName}});
my $key = { %{$root->{$params{keyName}}} };
# Bless leaf as expected
map { bless $key->{$_}, 'Win32::TieRegistry' }
grep { ref($key->{$_}) eq 'HASH' } keys %{$key};
bless $key, 'Win32::TieRegistry';
return $key;
}
);

FusionInventory::Agent::Tools::Win32->use('getRegistryKey');
foreach my $test (keys %regkey_tests) {

my $regkey = getRegistryKey( %{$regkey_tests{$test}} );
cmp_deeply(
$regkey,
$regkey_tests{$test}->{_expected},
"$test regkey"
);
}

FusionInventory::Agent::Tools::Win32->use('getRegistryValue');
foreach my $test (keys %regval_tests) {

my $regval = getRegistryValue( %{$regval_tests{$test}} );
cmp_deeply(
$regval,
$regval_tests{$test}->{_expected},
"$test regval"
);
}
17 changes: 17 additions & 0 deletions t/lib/fake/windows/Win32/TieRegistry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package Win32::TieRegistry;
use strict;
use warnings;

use constant REG_DWORD => 0x4;
use constant REG_SZ => 0x7;

our $Registry;

sub import {
Expand All @@ -13,4 +16,18 @@ sub import {
*{"$callpkg\::KEY_READ"} = sub {};
}

sub GetValue {
my ($self, $value ) = @_ ;
# Subkey case
if ($value && exists($self->{$value})) {
return wantarray ? () : undef ;
}
# Value case
$value = '/'.$value;
return unless ($value && exists($self->{$value}));
return wantarray ?
( $self->{$value}, $self->{$value} =~ /^0x/ ? REG_DWORD : REG_SZ )
: $self->{$value} ;
}

1;

0 comments on commit dcabd49

Please sign in to comment.