[Pkg-puppet-devel] [facter] 36/46: (#20915) Read dmi entries as binary data

Stig Sandbeck Mathisen ssm at debian.org
Sun Sep 1 10:47:32 UTC 2013


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit a164fe3f2679f1e63a9f4ede9b3a10d109ca4d67
Author: Andrew Parker <andy at puppetlabs.com>
Date:   Wed Jun 5 17:29:45 2013 -0700

    (#20915) Read dmi entries as binary data
    
    The DMI entry information that is read to determine the virtual platform
    is actually binary data. When it is interpreted as an encoded string,
    then there will be encoding errors. This changes it to be read in "rb"
    mode, which has nearly no effect on ruby 1.8.7, but causes it to be read
    as Encoding::ASCII_8BIT on ruby 1.9.3 (which is how ruby deals with
    binary data).
---
 lib/facter/util/file_read.rb                                |    4 ++++
 lib/facter/util/virtual.rb                                  |    4 +++-
 spec/fixtures/unit/util/virtual/invalid_unicode_dmi_entries |  Bin 0 -> 74 bytes
 spec/unit/util/virtual_spec.rb                              |    9 +++++++++
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/facter/util/file_read.rb b/lib/facter/util/file_read.rb
index 258cd66..82404a3 100644
--- a/lib/facter/util/file_read.rb
+++ b/lib/facter/util/file_read.rb
@@ -27,6 +27,10 @@ module FileRead
     Facter.debug "Could not read #{path}: #{detail.message}"
     nil
   end
+
+  def self.read_binary(path)
+    File.open(path, "rb") { |contents| contents.read }
+  end
 end
 end
 end
diff --git a/lib/facter/util/virtual.rb b/lib/facter/util/virtual.rb
index a2457f5..d224781 100644
--- a/lib/facter/util/virtual.rb
+++ b/lib/facter/util/virtual.rb
@@ -1,3 +1,5 @@
+require 'facter/util/file_read'
+
 module Facter::Util::Virtual
   ##
   # virt_what is a delegating helper method intended to make it easier to stub
@@ -143,7 +145,7 @@ module Facter::Util::Virtual
   # @return [String] or nil if the path does not exist
   def self.read_sysfs_dmi_entries(path="/sys/firmware/dmi/entries/1-0/raw")
     if File.exists?(path)
-      File.read(path)
+      Facter::Util::FileRead.read_binary(path)
     end
   end
 end
diff --git a/spec/fixtures/unit/util/virtual/invalid_unicode_dmi_entries b/spec/fixtures/unit/util/virtual/invalid_unicode_dmi_entries
new file mode 100644
index 0000000..675dd14
Binary files /dev/null and b/spec/fixtures/unit/util/virtual/invalid_unicode_dmi_entries differ
diff --git a/spec/unit/util/virtual_spec.rb b/spec/unit/util/virtual_spec.rb
index c0ee4b6..2331142 100755
--- a/spec/unit/util/virtual_spec.rb
+++ b/spec/unit/util/virtual_spec.rb
@@ -147,6 +147,15 @@ describe Facter::Util::Virtual do
     end
   end
 
+  it "reads dmi entries as ascii data" do
+    entries_file = my_fixture('invalid_unicode_dmi_entries')
+    expected_contents = 'Virtual'
+
+    entries = Facter::Util::Virtual.read_sysfs_dmi_entries(entries_file)
+
+    entries.should =~ /#{expected_contents}/
+  end
+
   it "should identify vserver_host when /proc/virtual exists" do
     Facter::Util::Virtual.expects(:vserver?).returns(true)
     FileTest.stubs(:exists?).with("/proc/virtual").returns(true)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list