11# encoding: utf-8
2- require 'bundler'
3- Bundler . require ( :default , :test )
4-
5- require 'coveralls'
6- Coveralls . wear!
7-
8- require 'test/unit'
9-
10- class TestURLcrypt < Test ::Unit ::TestCase
11-
12- require 'URLcrypt'
13-
14- def assert_bytes_equal ( string1 , string2 )
15- bytes1 = string1 . bytes . to_a . join ( ':' )
16- bytes2 = string2 . bytes . to_a . join ( ':' )
17- assert_equal ( bytes1 , bytes2 )
18- end
19-
20- def assert_decoding ( encoded , plain )
21- decoded = URLcrypt . decode ( encoded )
22- assert_bytes_equal ( plain , decoded )
23- end
24-
25- def assert_encoding ( encoded , plain )
26- actual = URLcrypt . encode ( plain )
27- assert_bytes_equal ( encoded , actual )
28- end
29-
30- def assert_encode_and_decode ( encoded , plain )
31- assert_encoding ( encoded , plain )
32- assert_decoding ( encoded , plain )
33- end
2+ require 'test_helper'
343
4+ class TestURLcrypt < TestClass
355 def test_empty_string
366 assert_encode_and_decode ( '' , '' )
377 end
@@ -41,11 +11,11 @@ def test_encode
4111 '111gc86f4nxw5zj1b3qmhpb14n5h25l4m7111' ,
4212 "\0 \0 awesome \n ü string\0 \0 " )
4313 end
44-
14+
4515 def test_invalid_encoding
4616 assert_decoding ( 'ZZZZZ' , '' )
4717 end
48-
18+
4919 def test_arbitrary_byte_strings
5020 0 . step ( 1500 , 17 ) do |n |
5121 original = ( 0 ..n ) . map { rand ( 256 ) . chr } . join
@@ -55,10 +25,12 @@ def test_arbitrary_byte_strings
5525 end
5626
5727 def test_encryption
58- # this key was generated via rake secret in a rails app, the pack() converts it into a byte array
59- URLcrypt ::key =
60- [ 'd25883a27b9a639da85ea7e159b661218799c9efa63069fac13a6778c954fb6d721968887a19bdb01af8f59eb5a90d256bd9903355c20b0b4b39bf4048b9b17b' ] . pack ( 'H*' )
61-
28+ # pack() converts this secret into a byte array
29+ secret = [ 'd25883a27b9a639da85ea7e159b661218799c9efa63069fac13a6778c954fb6d' ] . pack ( 'H*' )
30+ URLcrypt ::key = secret
31+
32+ assert_equal OpenSSL ::Cipher . new ( 'aes-256-cbc' ) . key_len , secret . bytesize
33+
6234 original = "hello world!"
6335 encrypted = URLcrypt ::encrypt ( original )
6436 assert_equal ( URLcrypt ::decrypt ( encrypted ) , original )
0 commit comments