test/cipher_test.rb in symmetric-encryption-0.8.0 vs test/cipher_test.rb in symmetric-encryption-0.9.0
- old
+ new
@@ -2,11 +2,11 @@
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
require 'rubygems'
require 'test/unit'
require 'shoulda'
-require 'symmetric_encryption/cipher'
+require 'symmetric_encryption'
# Unit Test for SymmetricEncryption::Cipher
#
class CipherTest < Test::Unit::TestCase
context 'standalone' do
@@ -22,11 +22,17 @@
should "not require an iv" do
cipher = SymmetricEncryption::Cipher.new(
:key => '1234567890ABCDEF1234567890ABCDEF'
)
- assert_equal "\302<\351\227oj\372\3331\310\260V\001\v'\346", cipher.encrypt('Hello World')
+ result = "\302<\351\227oj\372\3331\310\260V\001\v'\346"
+ # Note: This test fails on JRuby 1.7 RC1 since it's OpenSSL
+ # behaves differently when no IV is supplied.
+ # It instead encrypts to the following value:
+ # result = "0h\x92\x88\xA1\xFE\x8D\xF5\xF3v\x82\xAF(P\x83Y"
+ result.force_encoding('binary') if defined?(Encoding)
+ assert_equal result, cipher.encrypt('Hello World')
end
should "throw an exception on bad data" do
cipher = SymmetricEncryption::Cipher.new(
:cipher => 'aes-128-cbc',
@@ -45,11 +51,14 @@
@cipher = SymmetricEncryption::Cipher.new(
:key => '1234567890ABCDEF1234567890ABCDEF',
:iv => '1234567890ABCDEF'
)
@social_security_number = "987654321"
+
@social_security_number_encrypted = "A\335*\314\336\250V\340\023%\000S\177\305\372\266"
+ @social_security_number_encrypted.force_encoding('binary') if defined?(Encoding)
+
@sample_data = [
{ :text => '555052345', :encrypted => ''}
]
end
@@ -61,9 +70,21 @@
assert_equal @social_security_number_encrypted, @cipher.encrypt(@social_security_number)
end
should "decrypt string" do
assert_equal @social_security_number, @cipher.decrypt(@social_security_number_encrypted)
+ end
+
+ if defined?(Encoding)
+ context "on Ruby 1.9" do
+ should "encode encrypted data as binary" do
+ assert_equal Encoding.find('binary'), @cipher.encrypt(@social_security_number).encoding
+ end
+
+ should "decode encrypted data as utf-8" do
+ assert_equal Encoding.find('utf-8'), @cipher.decrypt(@cipher.encrypt(@social_security_number)).encoding
+ end
+ end
end
end
end
\ No newline at end of file