Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions lib/puppet/type/registry_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ def self.title_patterns
end

autorequire(:registry_key) do
parents = []
path = parameter(:path)
path.ascend do |hkey, subkey|
parents << "#{hkey.keyname}\\#{subkey}" unless subkey == path.subkey # skip ourselves
end
parents
parameter(:path).enum_for(:ascend).select { |p| self[:path] != p }
end
end
6 changes: 1 addition & 5 deletions lib/puppet/type/registry_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ def self.title_patterns
end

autorequire(:registry_key) do
parents = []
parameter(:path).ascend do |hkey, subkey|
parents << "#{hkey.keyname}\\#{subkey}"
end
parents
parameter(:path).enum_for(:ascend)
end
end

64 changes: 39 additions & 25 deletions lib/puppet/util/key_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,54 @@
class Puppet::Util::KeyPath < Puppet::Parameter
include Puppet::Util::RegistryBase

attr_reader :hkey, :subkey
attr_reader :root, :hkey, :subkey

def validate(path)
split(path.to_s)
end

def split(path)
unless match = /^([^\\]*)((?:\\[^\\]{1,255})*)$/.match(path)
def munge(path)
unless captures = /^([^\\]*)((?:\\[^\\]{1,255})*)$/.match(path)
raise ArgumentError, "Invalid registry key: #{path}"
end

@hkey =
case match[1].downcase
when /hkey_local_machine/, /hklm/
HKEYS[:hklm]
when /hkey_classes_root/, /hkcr/
HKEYS[:hkcr]
else
raise ArgumentError, "Unsupported prefined key: #{path}"
end

# leading backslash is not part of the subkey name
@subkey = match[2]
@subkey = @subkey[1..-1] unless @subkey.empty?
# canonical root key symbol
@root = case captures[1].downcase
when /hkey_local_machine/, /hklm/
:hklm
when /hkey_classes_root/, /hkcr/
:hkcr
when /hkey_current_user/, /hkcu/,
/hkey_users/, /hku/,
/hkey_current_config/, /hkcc/,
/hkey_performance_data/,
/hkey_performance_text/,
/hkey_performance_nlstext/,
/hkey_dyn_data/
raise ArgumentError, "Unsupported prefined key: #{path}"
else
raise ArgumentError, "Invalid registry key: #{path}"
end

# the hkey object for the root key
@hkey = HKEYS[root]

@subkey = captures[2]
if @subkey.empty?
canonical = root.to_s
else
# Leading backslash is not part of the subkey name
@subkey.sub!(/^\\(.*)$/, '\1')
canonical = "#{root.to_s}\\#{subkey}"
end

canonical
end

def ascend(&block)
s = subkey
p = self.value

yield hkey, s
yield p

while idx = s.rindex('\\')
s = s[0, idx]
yield hkey, s
while idx = p.rindex('\\')
p = p[0, idx]
yield p
end
end
end
11 changes: 6 additions & 5 deletions lib/puppet/util/value_path.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
require 'puppet/parameter'
require 'puppet/util/registry_base'
require 'puppet/util/key_path'

class Puppet::Util::ValuePath < Puppet::Parameter::KeyPath
attr_reader :valuename

def split(path)
unless path
raise ArgumentError, "Invalid registry value"
end
def munge(path)
raise ArgumentError, "Invalid registry value" unless path

if path[-1, 1] == '\\' # trailing backslash implies default value
super(path.gsub(/\\*$/, ''))
Expand All @@ -19,5 +17,8 @@ def split(path)
super(path[0, idx])
@valuename = path[idx+1..-1] if idx > 0
end

canonical = subkey.empty? ? "#{root}\\#{valuename}" : "#{root}\\#{subkey}\\#{valuename}"
canonical.downcase
end
end
7 changes: 3 additions & 4 deletions spec/unit/puppet/type/registry_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
end
end

%w[unknown unknown\subkey HKEY_PERFORMANCE_DATA].each do |path|
it "should reject #{path} as unsupported" do
%w[HKEY_DYN_DATA HKEY_PERFORMANCE_DATA].each do |path|
it "should reject #{path} as unsupported case insensitively" do
expect { key[:path] = path }.should raise_error(Puppet::Error, /Unsupported/)
end
end

%[hklm\\ hklm\foo\\].each do |path|
%[hklm\\ hklm\foo\\ unknown unknown\subkey].each do |path|
it "should reject #{path} as invalid" do
path = "hklm\\" + 'a' * 256
expect { key[:path] = path }.should raise_error(Puppet::Error, /Invalid registry key/)
Expand All @@ -41,7 +41,6 @@

%w[HKLM HKEY_LOCAL_MACHINE hklm].each do |root|
it "should canonicalize the root key #{root}" do
pending("Not implemented")
key[:path] = root
key[:path].should == 'hklm'
end
Expand Down
19 changes: 16 additions & 3 deletions spec/unit/puppet/type/registry_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,27 @@
described_class.new(:path => 'hklm\\software\\\\')
end

%w[unknown\name unknown\subkey\name HKEY_PERFORMANCE_DATA\name].each do |path|
%w[HKEY_DYN_DATA\\ HKEY_PERFORMANCE_DATA\name].each do |path|
it "should reject #{path} as unsupported" do
expect { described_class.new(:path => path) }.to raise_error(Puppet::Error, /Unsupported/)
end
end

%[hklm hkcr].each do |path|
%[hklm hkcr unknown\\name unknown\\subkey\\name].each do |path|
it "should reject #{path} as invalid" do
pending 'wrong message'
expect { described_class.new(:path => path) }.should raise_error(Puppet::Error, /Invalid registry key/)
end
end

%w[HKLM\\name HKEY_LOCAL_MACHINE\\name hklm\\name].each do |root|
it "should canonicalize root key #{root}" do
value = described_class.new(:path => root)
value[:path].should == 'hklm\name'
end
end


it 'should validate the length of the value name'
it 'should validate the length of the value data'
it 'should canonicalize the root key'
Expand Down Expand Up @@ -91,11 +99,16 @@
value[:data] = 'foobar'
end

it "should support integer data" do
it "should support dword data" do
value[:type] = :dword
value[:data] = 0
end

it "should support qword data" do
value[:type] = :qword
value[:data] = 0xFFFF
end

it "should support binary data" do
value[:type] = :binary
value[:data] = "CA FE BE EF"
Expand Down