test/URLcrypt_test.rb in urlcrypt-0.0.1 vs test/URLcrypt_test.rb in urlcrypt-0.1.0
- old
+ new
@@ -1,17 +1,24 @@
+# encoding: utf-8
require 'test/unit'
require 'URLcrypt'
class TestURLcrypt < Test::Unit::TestCase
+ def assert_bytes_equal(string1, string2)
+ bytes1 = string1.bytes.to_a.join(':')
+ bytes2 = string2.bytes.to_a.join(':')
+ assert_equal(bytes1, bytes2)
+ end
+
def assert_decoding(encoded, plain)
decoded = URLcrypt.decode(encoded)
- assert_equal(plain, decoded)
+ assert_bytes_equal(plain, decoded)
end
def assert_encoding(encoded, plain)
actual = URLcrypt.encode(plain)
- assert_equal(encoded, actual)
+ assert_bytes_equal(encoded, actual)
end
def assert_encode_and_decode(encoded, plain)
assert_encoding(encoded, plain)
assert_decoding(encoded, plain)
@@ -35,8 +42,18 @@
0.step(1500,17) do |n|
original = (0..n).map{rand(256).chr}.join
encoded = URLcrypt::encode(original)
assert_decoding(encoded, original)
end
+ end
+
+ def test_encryption
+ # this key was generated via rake secret in a rails app, the pack() converts it into a byte array
+ URLcrypt::key =
+['d25883a27b9a639da85ea7e159b661218799c9efa63069fac13a6778c954fb6d721968887a19bdb01af8f59eb5a90d256bd9903355c20b0b4b39bf4048b9b17b'].pack('H*')
+
+ original = "hello world!"
+ encrypted = URLcrypt::encrypt(original)
+ assert_equal(URLcrypt::decrypt(encrypted), original)
end
end