diff options
author | Noah Kantrowitz <noah@coderanger.net> | 2018-05-01 15:34:16 -0700 |
---|---|---|
committer | Noah Kantrowitz <noah@coderanger.net> | 2018-05-01 15:34:16 -0700 |
commit | 02f48ac2e7037f9dec72fae02ffac4c6b77bcc26 (patch) | |
tree | 483ddaab71c0d52a58c46381413e5265993dd317 /spec | |
parent | a007742eb897423efbaacc799dc07a8901a77273 (diff) | |
download | ohai-02f48ac2e7037f9dec72fae02ffac4c6b77bcc26.tar.gz |
Make the FIPS plugins use the new Ruby 2.5 accessor if present.
We need to remember to clean this up when we drop Ruby 2.4.
Signed-off-by: Noah Kantrowitz <noah@coderanger.net>
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/plugins/linux/fips_spec.rb | 43 | ||||
-rw-r--r-- | spec/unit/plugins/windows/fips_spec.rb | 44 |
2 files changed, 75 insertions, 12 deletions
diff --git a/spec/unit/plugins/linux/fips_spec.rb b/spec/unit/plugins/linux/fips_spec.rb index 79dd2530..570a5e04 100644 --- a/spec/unit/plugins/linux/fips_spec.rb +++ b/spec/unit/plugins/linux/fips_spec.rb @@ -17,23 +17,38 @@ # require_relative "../../../spec_helper.rb" +require "openssl" describe Ohai::System, "plugin fips" do let(:enabled) { "0" } let(:plugin) { get_plugin("linux/fips") } let(:fips_path) { "/proc/sys/crypto/fips_enabled" } + let(:openssl_test_mode) { true } + + subject do + plugin.run + plugin["fips"]["kernel"]["enabled"] + end before(:each) do allow(plugin).to receive(:collect_os).and_return(:linux) allow(::File).to receive(:read).with(fips_path).and_return(enabled) end + around do |ex| + begin + $FIPS_TEST_MODE = openssl_test_mode + ex.run + ensure + $FIPS_TEST_MODE = false + end + end + context "fips file is present and contains 1" do let(:enabled) { "1" } it "sets fips plugin" do - plugin.run - expect(plugin["fips"]["kernel"]["enabled"]).to be(true) + expect(subject).to be(true) end end @@ -41,8 +56,7 @@ describe Ohai::System, "plugin fips" do let(:enabled) { "0" } it "does not set fips plugin" do - plugin.run - expect(plugin["fips"]["kernel"]["enabled"]).to be(false) + expect(subject).to be(false) end end @@ -52,8 +66,25 @@ describe Ohai::System, "plugin fips" do end it "does not set fips plugin" do - plugin.run - expect(plugin["fips"]["kernel"]["enabled"]).to be(false) + expect(subject).to be(false) + end + end + + context "with Ruby 2.5 or newer", if: defined?(OpenSSL.fips_mode) do + let(:openssl_test_mode) { false } + + context "with OpenSSL.fips_mode == false" do + before { allow(OpenSSL).to receive(:fips_mode).and_return(false) } + it "does not set fips plugin" do + expect(subject).to be(false) + end + end + + context "with OpenSSL.fips_mode == true" do + before { allow(OpenSSL).to receive(:fips_mode).and_return(true) } + it "sets fips plugin" do + expect(subject).to be(true) + end end end end diff --git a/spec/unit/plugins/windows/fips_spec.rb b/spec/unit/plugins/windows/fips_spec.rb index 19482993..e7efff01 100644 --- a/spec/unit/plugins/windows/fips_spec.rb +++ b/spec/unit/plugins/windows/fips_spec.rb @@ -17,25 +17,40 @@ # require_relative "../../../spec_helper.rb" +require "openssl" describe Ohai::System, "plugin fips", :windows_only do let(:enabled) { 0 } let(:plugin) { get_plugin("windows/fips") } let(:fips_key) { 'System\CurrentControlSet\Control\Lsa\FIPSAlgorithmPolicy' } let(:win_reg_entry) { { "Enabled" => enabled } } + let(:openssl_test_mode) { true } + + subject do + plugin.run + plugin["fips"]["kernel"]["enabled"] + end before(:each) do allow(plugin).to receive(:collect_os).and_return(:windows) allow(Win32::Registry::HKEY_LOCAL_MACHINE).to receive(:open).with(fips_key, arch).and_yield(win_reg_entry) end + around do |ex| + begin + $FIPS_TEST_MODE = openssl_test_mode + ex.run + ensure + $FIPS_TEST_MODE = false + end + end + shared_examples "fips_plugin" do context "fips enabled key is set to 1" do let(:enabled) { 1 } it "sets fips plugin" do - plugin.run - expect(plugin["fips"]["kernel"]["enabled"]).to be(true) + expect(subject).to be(true) end end @@ -43,8 +58,7 @@ describe Ohai::System, "plugin fips", :windows_only do let(:enabled) { 0 } it "does not set fips plugin" do - plugin.run - expect(plugin["fips"]["kernel"]["enabled"]).to be(false) + expect(subject).to be(false) end end @@ -54,8 +68,7 @@ describe Ohai::System, "plugin fips", :windows_only do end it "does not set fips plugin" do - plugin.run - expect(plugin["fips"]["kernel"]["enabled"]).to be(false) + expect(subject).to be(false) end end end @@ -83,4 +96,23 @@ describe Ohai::System, "plugin fips", :windows_only do it_behaves_like "fips_plugin" end + + + context "with Ruby 2.5 or newer", if: defined?(OpenSSL.fips_mode) do + let(:openssl_test_mode) { false } + + context "with OpenSSL.fips_mode == false" do + before { allow(OpenSSL).to receive(:fips_mode).and_return(false) } + it "does not set fips plugin" do + expect(subject).to be(false) + end + end + + context "with OpenSSL.fips_mode == true" do + before { allow(OpenSSL).to receive(:fips_mode).and_return(true) } + it "sets fips plugin" do + expect(subject).to be(true) + end + end + end end |