forked from stesla/base32
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathURLcrypt_test.rb
More file actions
116 lines (87 loc) · 3.31 KB
/
URLcrypt_test.rb
File metadata and controls
116 lines (87 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# encoding: utf-8
require 'bundler'
Bundler.require(:default, :test)
require 'coveralls'
Coveralls.wear!
require 'test/unit'
class TestURLcrypt < Test::Unit::TestCase
require 'URLcrypt'
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_bytes_equal(plain, decoded)
end
def assert_encoding(encoded, plain)
actual = URLcrypt.encode(plain)
assert_bytes_equal(encoded, actual)
end
def assert_encode_and_decode(encoded, plain)
assert_encoding(encoded, plain)
assert_decoding(encoded, plain)
end
def test_empty_string
assert_encode_and_decode('', '')
end
def test_encode
assert_encode_and_decode(
'111gc86f4nxw5zj1b3qmhpb14n5h25l4m7111',
"\0\0awesome \n ü string\0\0")
end
def test_invalid_encoding
assert_decoding('ZZZZZ', '')
end
def test_arbitrary_byte_strings
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
def test_base64_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!"
URLcrypt.default_coder = URLcrypt::Base64Coder
encrypted = URLcrypt::encrypt(original)
assert_equal(URLcrypt::decrypt(encrypted), original)
URLcrypt.default_coder = nil
end
def test_cgi_base64_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!"
URLcrypt.default_coder = URLcrypt::CGIBase64Coder
encrypted = URLcrypt::encrypt(original)
assert_equal(URLcrypt::decrypt(encrypted), original)
URLcrypt.default_coder = nil
end
def test_decrypt_error
error = assert_raises(URLcrypt::DecryptError) do
::URLcrypt::decrypt("just some plaintext")
end
assert_equal error.message, "not a valid string to decrypt"
end
def test_multiple_coders
coder1 = URLcrypt::BaseCoder.new(key: [SecureRandom.hex(64)].pack("H*"))
coder2 = URLcrypt::BaseCoder.new(key: [SecureRandom.hex(64)].pack("H*"))
str = "hello there friends."
coder1_encrypted = coder1.encrypt(str)
coder2_encrypted = coder2.encrypt(str)
assert_not_equal(coder1_encrypted, coder2_encrypted)
assert_equal(coder1.decrypt(coder1_encrypted), coder2.decrypt(coder2_encrypted))
end
end