diff --git a/lib/facter/tpm2.rb b/lib/facter/tpm2.rb index 448650e..3026e2e 100644 --- a/lib/facter/tpm2.rb +++ b/lib/facter/tpm2.rb @@ -2,7 +2,7 @@ # # The fact will be nil if the tpm2-tools are either not available, or aren't # configured to comminucate with the TPM -Facter.add('tpm2') do +Facter.add( :tpm2 ) do #### NOTE: The confine below is intentionally commented out to explain why #### we're not using it (or something like it), as we did with the `tpm` @@ -48,9 +48,9 @@ # `:tpm_version` will always short-circuit if that fact is absent. (Facter # doesn't execute confine blocks for absent facts.) confine do - value = Facter[:tpm_version] + value = Facter.value(:tpm) Facter.debug 'tpm2 confine' - value.nil? || value != 'tpm1' + value.nil? end setcode do diff --git a/spec/unit/facter/tpm2/util_spec.rb b/spec/unit/facter/tpm2/util_spec.rb index 2f66a8c..b7ffe66 100644 --- a/spec/unit/facter/tpm2/util_spec.rb +++ b/spec/unit/facter/tpm2/util_spec.rb @@ -62,20 +62,20 @@ end end context 'when tpm2-tools can query the TABRM' do - before :each do - allow(Facter::Core::Execution).to receive(:execute).with("#{@l_bin}/tpm2_pcrlist -s").and_return( - "Supported Bank/Algorithm: sha1(0x0004) sha256(0x000b) sha384(0x000c)\n" - ) - end - - # Test against `tpm2_getcap -c properties-fixed` dumps from as many # manufacturers/models as we can find it 'should return a correct data structure queried from the TPM of any manufacturer' do + # Modeling an @base EL7 rpm install of tpm2-tools + allow(File).to receive(:executable?).with("#{@l_bin}/tpm2_pcrlist").and_return(false) + allow(File).to receive(:executable?).with("#{@u_bin}/tpm2_pcrlist").and_return( true ) + yaml_files = Dir.glob( File.expand_path( '../../../../files/tpm2/mocks/tpm2_getcap_-c_properties-fixed/*.yaml', __FILE__) ) yaml_strings = yaml_files.map{ |yaml_file| File.read yaml_file } yaml_strings.each do |yaml_string| - allow(Facter::Core::Execution).to receive(:execute).with("#{@l_bin}/tpm2_getcap -c properties-fixed").and_return( yaml_string ) + allow(Facter::Core::Execution).to receive(:execute).with("#{@u_bin}/tpm2_getcap -c properties-fixed").and_return( yaml_string ) + allow(Facter::Core::Execution).to receive(:execute).with("#{@u_bin}/tpm2_pcrlist -s").and_return( + "Supported Bank/Algorithm: sha1(0x0004) sha256(0x000b) sha384(0x000c)\n" + ) util = Facter::TPM2::Util.new fact = util.build_structured_fact expect(fact).to be_a(Hash) diff --git a/spec/unit/facter/tpm2_spec.rb b/spec/unit/facter/tpm2_spec.rb index 9bfff64..b5b1475 100644 --- a/spec/unit/facter/tpm2_spec.rb +++ b/spec/unit/facter/tpm2_spec.rb @@ -1,53 +1,48 @@ require 'spec_helper' -require 'facter/tpm' require 'facter/tpm2' require 'facter/tpm2/util' +require 'ostruct' describe 'tpm2', :type => :fact do - before :each do + before :all do @l_bin = '/usr/local/bin' - Facter.clear - Facter.clear_messages - allow(File).to receive(:executable?).with("#{@l_bin}/tpm2_pcrlist").and_return( true ) + @u_bin = '/usr/bin' end context 'when a hardware TPM is installed' do - before :each do - allow(Facter.fact(:has_tpm)).to receive(:value).and_return true - end - context 'tpm_version is "tpm1"' do - it 'should return nil' do - # Just need something that actually exists on the current FS - allow(Facter::Core::Execution).to receive(:which).with('tpm_version').and_return nil - allow(Facter::Core::Execution).to receive(:execute).with(%r{#{@l_bin}/?tpm2_pcrlist -s$}).and_return nil - allow(Facter::Core::Execution).to receive(:execute).with(%r{.*/?tpm_version$}, :timeout => 15).and_return nil - allow(Facter.fact(:tpm_version)).to receive(:value).and_return 'tpm1' - expect(Facter.fact(:tpm2).value).to eq nil - end + it 'should return nil' do + allow(Facter).to receive(:value).with(:has_tpm).and_return true + allow(Facter).to receive(:value).with(:tpm).and_return({ :tpm1_hash => :values }) + allow(Facter::Core::Execution).to receive(:execute).with(%r{uname$}).and_return true + allow(Facter::Core::Execution).to receive(:execute).with(%r{.*/?tpm_version$}, :timeout => 15).and_return nil + expect(Facter).to receive(:fact).with(:tpm2).and_call_original + + expect(Facter.fact(:tpm2).value).to eq nil end - context 'The hardware TPM is TPM 2.0' do - before :each do - allow(Facter.fact(:has_tpm)).to receive(:value).and_return true - allow(Facter::Core::Execution).to receive(:execute).with("#{@l_bin}/tpm2_pcrlist -s").and_return( - "Supported Bank/Algorithm: sha1(0x0004) sha256(0x000b) sha384(0x000c)\n" - ) - allow(Facter::Core::Execution).to receive(:execute).with("#{@l_bin}/tpm2_getcap -c properties-fixed").and_return( - File.read File.expand_path( '../../../files/tpm2/mocks/tpm2_getcap_-c_properties-fixed/nuvoton-ncpt6xx-fbfc85e.yaml', __FILE__) - ) - end - context 'tpm_version is "unknown"' do - it 'should return a Hash' do - allow(Facter.fact(:tpm_version)).to receive(:value).and_return 'unknown' - expect(Facter.fact(:tpm2).value.is_a? Hash).to eq true - end - end - context 'tpm_version is "tpm2"' do - it 'should return a Hash' do - allow(Facter.fact(:tpm_version)).to receive(:value).and_return 'tpm2' - expect(Facter.fact(:tpm2).value.is_a? Hash).to eq true - end - end + end + + context 'The hardware TPM is TPM 2.0' do + it 'should return a fact' do + allow(Facter).to receive(:value).with(:has_tpm).and_return true + allow(Facter).to receive(:value).with(:tpm).and_return( nil ) + allow(File).to receive(:executable?).with("#{@l_bin}/tpm2_pcrlist").and_return(false) + allow(File).to receive(:executable?).with("#{@u_bin}/tpm2_pcrlist").and_return( true ) + allow(Facter).to receive(:value).with(:has_tpm).and_return true + allow(Facter::Core::Execution).to receive(:execute).with("#{@u_bin}/tpm2_getcap -c properties-fixed").and_return( + File.read File.expand_path( + '../../../files/tpm2/mocks/tpm2_getcap_-c_properties-fixed/nuvoton-ncpt6xx-fbfc85e.yaml', + __FILE__, + ) + ) + allow(Facter::Core::Execution).to receive(:execute).with("#{@u_bin}/tpm2_pcrlist -s").and_return( + "Supported Bank/Algorithm: sha1(0x0004) sha256(0x000b) sha384(0x000c)\n" + ) + fact = Facter.fact(:tpm2).value + expect(fact).to be_a(Hash) + expect(fact['manufacturer']).to match(/.{0,4}/) + expect(fact['firmware_version']).to match(/^\d+\.\d+\.\d+\.\d+$/) + expect(fact['tpm2_getcap']['properties-fixed']).to be_a(Hash) end end end