spec/cipher/data_spec.rb in heirloom-0.9.0 vs spec/cipher/data_spec.rb in heirloom-0.10.0

- old
+ new

@@ -1,56 +1,73 @@ require 'spec_helper' describe Heirloom do before do + @encrypted_file_mock = mock 'encrypted mock' + @decrypted_file_mock = mock 'decrypted mock' + @encrypted_file_mock.stub :path => '/tmp/enc' + @decrypted_file_mock.stub :path => '/tmp/dec', + :read => 'plaintext' @logger_mock = mock 'logger', :info => true - @logger_mock.stub :info => true + @logger_mock.stub :info => true, + :debug => true @config_mock = mock 'config' @config_mock.stub :logger => @logger_mock @data = Heirloom::Cipher::Data.new :config => @config_mock end - context "with secret given" do - before do - @aes_mock = mock 'aes' - OpenSSL::Cipher::AES256.should_receive(:new). - with(:CBC).and_return @aes_mock - end + context "when succesful" do + context "with secret given" do + it "should decrypt the given data" do + @data.should_receive(:which).with('gpg').and_return true + Tempfile.should_receive(:new).with('archive.tar.gz.gpg'). + and_return @encrypted_file_mock + Tempfile.should_receive(:new).with('archive.tar.gz'). + and_return @decrypted_file_mock + ::File.should_receive(:open). + with(@encrypted_file_mock,'w') + $?.stub :success? => true - it "should decrypt the given data" do - @aes_mock.should_receive(:decrypt) - @aes_mock.should_receive(:key=).with Digest::SHA256.hexdigest 'mysecret' - @aes_mock.should_receive(:iv=).with 'firstsixteenchar' - @aes_mock.should_receive(:update).with('crypteddata').and_return 'cleartext' - @aes_mock.stub :final => 'final' - @data.decrypt_data(:data => 'firstsixteencharcrypteddata', - :secret => 'mysecret'). - should == 'cleartextfinal' + command = 'gpg --batch --yes --cipher-algo AES256 --passphrase password --output /tmp/dec /tmp/enc 2>&1' + @data.should_receive(:`).with(command).and_return true + @data.decrypt_data(:data => 'crypted', + :secret => 'password').should == 'plaintext' + end end - it "should rescue bad key error and return false" do - @logger_mock.should_receive(:error). - with "Unable to decrypt Heirloom: 'OpenSSL::Cipher::CipherError'" - @aes_mock.should_receive(:decrypt) - @aes_mock.should_receive(:key=).with Digest::SHA256.hexdigest 'badsecret' - @aes_mock.should_receive(:iv=).with 'firstsixteenchar' - @aes_mock.should_receive(:update).with('crypteddata').and_return 'crap' - @aes_mock.should_receive(:final).and_raise OpenSSL::Cipher::CipherError - @data.decrypt_data(:data => 'firstsixteencharcrypteddata', - :secret => 'badsecret'). - should be_false + context "no secret given" do + it "should return the data if no secret given" do + @data.decrypt_data(:data => 'plaintext', + :secret => nil).should == 'plaintext' + end end - end - context "no secret given" do - before do - @data = Heirloom::Cipher::Data.new :config => @config_mock - end + context "when unsuccesful" do + context "with secret given" do + it "should return false if gpg not in path" do + @logger_mock.should_receive(:error) + @data.should_receive(:which).with('gpg').and_return false + @data.decrypt_data(:data => 'crypted', + :secret => 'password').should be_false + end - it "should return the data if no secret given" do - @data.decrypt_data(:data => 'plaintext', - :secret => nil).should == 'plaintext' + it "should return false if decrypted fails" do + @logger_mock.should_receive(:error) + @data.should_receive(:which).with('gpg').and_return true + Tempfile.should_receive(:new).with('archive.tar.gz.gpg'). + and_return @encrypted_file_mock + Tempfile.should_receive(:new).with('archive.tar.gz'). + and_return @decrypted_file_mock + ::File.should_receive(:open). + with(@encrypted_file_mock,'w') + $?.stub :success? => false + + command = 'gpg --batch --yes --cipher-algo AES256 --passphrase password --output /tmp/dec /tmp/enc 2>&1' + @data.should_receive(:`).with(command).and_return true + @data.decrypt_data(:data => 'crypted', + :secret => 'password').should be_false + end end end end