Skip to content
Browse files

hardware engine code paths now covered in tests via mock objects

  • Loading branch information...
1 parent 016be62 commit 591006de295fe2edab7dc37aa459400cc3c1feda @reaperhulk reaperhulk committed
Showing with 46 additions and 9 deletions.
  1. +5 −5 lib/r509/PrivateKey.rb
  2. +41 −4 spec/privatekey_spec.rb
View
10 lib/r509/PrivateKey.rb
@@ -95,7 +95,7 @@ def public_key
# Converts the key into the PEM format
#
- # @return [String] the CSR converted into PEM format.
+ # @return [String] the key converted into PEM format.
def to_pem
if in_hardware?
raise R509::R509Error, "This method cannot be called when using keys in hardware"
@@ -109,7 +109,7 @@ def to_pem
# full list of available ciphers can be obtained with OpenSSL::Cipher.ciphers
# (common ones are des3, aes256, aes128)
# @param [String] password password
- # @return [String] the CSR converted into encrypted PEM format.
+ # @return [String] the key converted into encrypted PEM format.
def to_encrypted_pem(cipher,password)
if in_hardware?
raise R509::R509Error, "This method cannot be called when using keys in hardware"
@@ -121,7 +121,7 @@ def to_encrypted_pem(cipher,password)
# Converts the key into the DER format
#
- # @return [String] the CSR converted into DER format.
+ # @return [String] the key converted into DER format.
def to_der
if in_hardware?
raise R509::R509Error, "This method cannot be called when using keys in hardware"
@@ -134,7 +134,7 @@ def to_der
# @param [String, #write] filename_or_io Either a string of the path for
# the file that you'd like to write, or an IO-like object.
def write_pem(filename_or_io)
- write_data(filename_or_io, self.key.to_pem)
+ write_data(filename_or_io, self.to_pem)
end
@@ -155,7 +155,7 @@ def write_encrypted_pem(filename_or_io,cipher,password)
# @param [String, #write] filename_or_io Either a string of the path for
# the file that you'd like to write, or an IO-like object.
def write_der(filename_or_io)
- write_data(filename_or_io, self.key.to_der)
+ write_data(filename_or_io, self.to_der)
end
View
45 spec/privatekey_spec.rb
@@ -130,11 +130,20 @@
private_key.write_encrypted_pem(sio,'aes128','Testing1')
sio.string.match(/AES-128-CBC/).should_not == nil
end
- it "returns false for in_hardware? when it's...not" do
+ it "returns false for in_hardware? when not using an engine" do
private_key = R509::PrivateKey.new(:key => @key_csr)
private_key.in_hardware?.should == false
end
- it "returns true for in_hardware? when it...is"
+ it "returns true for in_hardware? when an engine is present" do
+ engine = double("engine")
+ engine.stub(:kind_of?) { true }
+ key_name = "r509_key"
+ key = R509::PrivateKey.new(
+ :engine => engine,
+ :key_name => key_name
+ )
+ key.in_hardware?.should == true
+ end
it "raises an error if you provide engine and key" do
expect { R509::PrivateKey.new(:key => @key_csr, :engine => 'not really an engine') }.to raise_error(ArgumentError, "You can't pass both :key and :engine")
end
@@ -147,7 +156,35 @@
it "raises an error if engine is not an OpenSSL::Engine" do
expect { R509::PrivateKey.new(:key_name => 'my_key', :engine => 'not really an engine') }.to raise_error(ArgumentError, 'When providing an engine, it must be of type OpenSSL::Engine')
end
- it "raises an error if you call output methods (pem,der,write) when using a hardware key"
- it "loads a hardware key successfully"
+ it "raises an error if you call output methods (pem,der,write) when using a hardware key" do
+ engine = double("engine")
+ engine.stub(:kind_of?) { true }
+ key_name = "r509_key"
+ key = R509::PrivateKey.new(
+ :engine => engine,
+ :key_name => key_name
+ )
+ expect { key.to_pem }.to raise_error(R509::R509Error, "This method cannot be called when using keys in hardware")
+ expect { key.to_der }.to raise_error(R509::R509Error, "This method cannot be called when using keys in hardware")
+ expect { key.to_encrypted_pem('aes256','password') }.to raise_error(R509::R509Error, "This method cannot be called when using keys in hardware")
+ expect { key.write_encrypted_pem('/dev/null','aes256','password') }.to raise_error(R509::R509Error, "This method cannot be called when using keys in hardware")
+ expect { key.write_der('/dev/null') }.to raise_error(R509::R509Error, "This method cannot be called when using keys in hardware")
+ end
+ it "loads a hardware key successfully" do
+ engine = double("engine")
+ engine.stub(:kind_of?) { true }
+ faux_key = double("faux_key")
+ faux_key.stub(:public_key) { "returning public key" }
+ engine.stub(:load_private_key) { faux_key }
+ key_name = "r509_key"
+ engine.should_receive(:load_private_key).with(key_name)
+ faux_key.should_receive(:public_key)
+ key = R509::PrivateKey.new(
+ :engine => engine,
+ :key_name => key_name
+ )
+ key.kind_of?(R509::PrivateKey).should == true
+ key.public_key.should == "returning public key"
+ end
end

0 comments on commit 591006d

Please sign in to comment.
Something went wrong with that request. Please try again.