-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
45 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |