test/cipher_test.rb in symmetric-encryption-3.8.3 vs test/cipher_test.rb in symmetric-encryption-3.9.0

- old
+ new

@@ -6,11 +6,11 @@ describe 'standalone' do it 'allow setting the cipher_name' do cipher = SymmetricEncryption::Cipher.new( cipher_name: 'aes-128-cbc', - key: '1234567890ABCDEF1234567890ABCDEF', + key: '1234567890ABCDEF', iv: '1234567890ABCDEF', encoding: :none ) assert_equal 'aes-128-cbc', cipher.cipher_name end @@ -30,11 +30,11 @@ end it 'throw an exception on bad data' do cipher = SymmetricEncryption::Cipher.new( cipher_name: 'aes-128-cbc', - key: '1234567890ABCDEF1234567890ABCDEF', + key: '1234567890ABCDEF', iv: '1234567890ABCDEF', encoding: :none ) assert_raises OpenSSL::Cipher::CipherError do cipher.decrypt('bad data') @@ -42,11 +42,11 @@ end end [false, true].each do |always_add_header| - SymmetricEncryption::Cipher::ENCODINGS.each do |encoding| + [:none, :base64, :base64strict, :base16].each do |encoding| describe "encoding: #{encoding} with#{'out' unless always_add_header} header" do before do @social_security_number = '987654321' @social_security_number_encrypted = case encoding @@ -63,11 +63,11 @@ raise "Add test for encoding: #{encoding}" end @social_security_number_encrypted_with_secondary_1 = "D1UCu38pqJ3jc0GvwJHiow==\n" @non_utf8 = "\xc2".force_encoding('binary') @cipher = SymmetricEncryption::Cipher.new( - key: 'ABCDEF1234567890ABCDEF1234567890', + key: 'ABCDEF1234567890', iv: 'ABCDEF1234567890', cipher_name: 'aes-128-cbc', encoding: encoding, always_add_header: always_add_header ) @@ -92,19 +92,19 @@ assert_equal Encoding.find('binary'), decrypted.encoding, decrypted assert_equal @non_utf8, decrypted end it 'return nil when encrypting nil' do - assert_equal nil, @cipher.encrypt(nil) + assert_nil @cipher.encrypt(nil) end it "return '' when encrypting ''" do assert_equal '', @cipher.encrypt('') end it 'return nil when decrypting nil' do - assert_equal nil, @cipher.decrypt(nil) + assert_nil @cipher.decrypt(nil) end it "return '' when decrypting ''" do assert_equal '', @cipher.decrypt('') end @@ -169,7 +169,99 @@ assert_equal @social_security_number, @cipher.decrypt(encrypted) end end + end + + describe '.generate_random_keys' do + describe 'with wrong params' do + it 'raises ArgumentError' do + error = assert_raises ArgumentError do + SymmetricEncryption::Cipher.generate_random_keys(wrong_params: '') + end + + assert_equal "SymmetricEncryption::Cipher Invalid options {:wrong_params=>\"\"}", error.message + end + end + + describe 'without keys' do + it 'creates new keys' do + h = SymmetricEncryption::Cipher.generate_random_keys + assert_equal 'aes-256-cbc', h[:cipher_name] + assert_equal :base64strict, h[:encoding] + assert h.has_key?(:key), h + assert h.has_key?(:iv), h + end + end + + describe 'with keys' do + it 'creates new keys' do + h = SymmetricEncryption::Cipher.generate_random_keys(key: '', iv: '') + assert_equal 'aes-256-cbc', h[:cipher_name] + assert_equal :base64strict, h[:encoding] + assert h.has_key?(:key), h + assert h.has_key?(:iv), h + end + end + + describe 'with encrypted keys' do + it 'creates new encrypted keys' do + key_encryption_key = SymmetricEncryption::KeyEncryptionKey.generate + h = SymmetricEncryption::Cipher.generate_random_keys( + encrypted_key: '', + encrypted_iv: '', + private_rsa_key: key_encryption_key + ) + assert_equal 'aes-256-cbc', h[:cipher_name] + assert_equal :base64strict, h[:encoding] + assert h.has_key?(:encrypted_key), h + assert h.has_key?(:encrypted_iv), h + end + + it 'exception on missing rsa key' do + assert_raises SymmetricEncryption::ConfigError do + SymmetricEncryption::Cipher.generate_random_keys( + encrypted_key: '', + encrypted_iv: '' + ) + end + end + end + + describe 'with files' do + before do + @key_filename = 'blah.key' + @iv_filename = 'blah.iv' + end + + after do + File.delete(@key_filename) if File.exist?(@key_filename) + File.delete(@iv_filename) if File.exist?(@iv_filename) + end + + it 'creates new files' do + key_encryption_key = SymmetricEncryption::KeyEncryptionKey.generate + h = SymmetricEncryption::Cipher.generate_random_keys( + key_filename: @key_filename, + iv_filename: @iv_filename, + private_rsa_key: key_encryption_key + ) + assert_equal 'aes-256-cbc', h[:cipher_name] + assert_equal :base64strict, h[:encoding] + assert h.has_key?(:key_filename), h + assert h.has_key?(:iv_filename), h + assert File.exist?(@key_filename) + assert File.exist?(@iv_filename) + end + + it 'exception on missing rsa key' do + assert_raises SymmetricEncryption::ConfigError do + SymmetricEncryption::Cipher.generate_random_keys( + key_filename: @key_filename, + iv_filename: @iv_filename + ) + end + end + end end end