forked from EntropyString/Elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentropy_string_test.exs
More file actions
208 lines (175 loc) · 8.82 KB
/
entropy_string_test.exs
File metadata and controls
208 lines (175 loc) · 8.82 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
defmodule EntropyStringTest do
use ExUnit.Case, async: true
doctest EntropyString
alias EntropyString.CharSet, as: CharSet
import EntropyString
test "CharSet 64" do
charset = CharSet.charset64
assert random_string( 6, charset, <<0xdd>>) == "3"
assert random_string( 12, charset, <<0x78, 0xfc>>) == "eP"
assert random_string(18, charset, <<0xc5, 0x6f, 0x21>>) == "xW8"
assert random_string(24, charset, <<0xc9, 0x68, 0xc7>>) == "yWjH"
assert random_string(30, charset, <<0xa5, 0x62, 0x20, 0x87>>) == "pWIgh"
assert random_string(36, charset, <<0x39, 0x51, 0xca, 0xcc, 0x8b>>) == "OVHKzI"
assert random_string(42, charset, <<0x83, 0x89, 0x00, 0xc7, 0xf4, 0x02>>) == "g4kAx_Q"
assert random_string(48, charset, <<0x51, 0xbc, 0xa8, 0xc7, 0xc9, 0x17>>) == "Ubyox8kX"
assert random_string(54, charset, <<0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x97, 0x52>>) == "0uPp2hmXU"
assert random_string(60, charset,
<<0xd9, 0x39, 0xc1, 0xaf, 0x1e, 0x2e, 0x69, 0x48>>) == "2TnBrx4uaU"
assert random_string(66, charset,
<<0x78, 0x3f, 0xfd, 0x93, 0xd1, 0x06, 0x90, 0x4b, 0xd6>>) == "eD_9k9EGkEv"
assert random_string(72, charset,
<<0x9d, 0x99, 0x4e, 0xa5, 0xd2, 0x3f, 0x8c, 0x86, 0x80>>) == "nZlOpdI_jIaA"
end
test "CharSet 32" do
charset = CharSet.charset32
assert random_string( 5, charset, <<0xdd>>) == "N"
assert random_string(10, charset, <<0x78, 0xfc>>) == "p6"
assert random_string(15, charset, <<0x78, 0xfc>>) == "p6R"
assert random_string(20, charset, <<0xc5, 0x6f, 0x21>>) == "JFHt"
assert random_string(25, charset, <<0xa5, 0x62, 0x20, 0x87>>) == "DFr43"
assert random_string(30, charset, <<0xa5, 0x62, 0x20, 0x87>>) == "DFr433"
assert random_string(35, charset, <<0x39, 0x51, 0xca, 0xcc, 0x8b>>) == "b8dPFB7"
assert random_string(40, charset, <<0x39, 0x51, 0xca, 0xcc, 0x8b>>) == "b8dPFB7h"
assert random_string(45, charset, <<0x83, 0x89, 0x00, 0xc7, 0xf4, 0x02>>) == "qn7q3rTD2"
assert random_string(50, charset, <<0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x97, 0x52>>) == "MhrRBGqLtQ"
assert random_string(55, charset, <<0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x97, 0x52>>) == "MhrRBGqLtQf"
end
test "CharSet 16" do
charset = CharSet.charset16
assert random_string( 4, charset, <<0x9d>>) == "9"
assert random_string( 8, charset, <<0xae>>) == "ae"
assert random_string(12, charset, <<0x01, 0xf2>>) == "01f"
assert random_string(16, charset, <<0xc7, 0xc9>>) == "c7c9"
assert random_string(20, charset, <<0xc7, 0xc9, 0x00>>) == "c7c90"
end
test "CharSet 8" do
charset = CharSet.charset8
assert random_string( 3, charset, <<0x5a>>) == "2"
assert random_string( 6, charset, <<0x5a>>) == "26"
assert random_string( 9, charset, <<0x21, 0xa4>>) == "103"
assert random_string(12, charset, <<0x21, 0xa4>>) == "1032"
assert random_string(15, charset, <<0xda, 0x19>>) == "66414"
assert random_string(18, charset, <<0xfd, 0x93, 0xd1>>) == "773117"
assert random_string(21, charset, <<0xfd, 0x93, 0xd1>>) == "7731172"
assert random_string(24, charset, <<0xfd, 0x93, 0xd1>>) == "77311721"
assert random_string(27, charset, <<0xc7, 0xc9, 0x07, 0xc9>>) == "617444076"
assert random_string(30, charset, <<0xc7, 0xc9, 0x07, 0xc9>>) == "6174440762"
end
test "CharSet 4" do
charset = CharSet.charset4
assert random_string( 2, charset, <<0x5a>>) == "T"
assert random_string( 4, charset, <<0x5a>>) == "TT"
assert random_string( 6, charset, <<0x93>>) == "CTA"
assert random_string( 8, charset, <<0x93>>) == "CTAG"
assert random_string(10, charset, <<0x20, 0xf1>>) == "ACAAG"
assert random_string(12, charset, <<0x20, 0xf1>>) == "ACAAGG"
assert random_string(14, charset, <<0x20, 0xf1>>) == "ACAAGGA"
assert random_string(16, charset, <<0x20, 0xf1>>) == "ACAAGGAT"
end
test "CharSet 2" do
charset = CharSet.charset2
assert random_string( 1, charset, <<0x27>>) == "0"
assert random_string( 2, charset, <<0x27>>) == "00"
assert random_string( 3, charset, <<0x27>>) == "001"
assert random_string( 4, charset, <<0x27>>) == "0010"
assert random_string( 5, charset, <<0x27>>) == "00100"
assert random_string( 6, charset, <<0x27>>) == "001001"
assert random_string( 7, charset, <<0x27>>) == "0010011"
assert random_string( 8, charset, <<0x27>>) == "00100111"
assert random_string( 9, charset, <<0xe3, 0xe9>>) == "111000111"
assert random_string(16, charset, <<0xe3, 0xe9>>) == "1110001111101001"
end
test "small ID" do
assert byte_size(small_id()) == 6
assert byte_size(small_id(CharSet.charset64)) == 5
assert byte_size(small_id(CharSet.charset32)) == 6
assert byte_size(small_id(CharSet.charset16)) == 8
assert byte_size(small_id(CharSet.charset8)) == 10
assert byte_size(small_id(CharSet.charset4)) == 15
assert byte_size(small_id(CharSet.charset2)) == 29
end
test "medium ID" do
assert byte_size(medium_id()) == 14
assert byte_size(medium_id(CharSet.charset64)) == 12
assert byte_size(medium_id(CharSet.charset32)) == 14
assert byte_size(medium_id(CharSet.charset16)) == 18
assert byte_size(medium_id(CharSet.charset8)) == 23
assert byte_size(medium_id(CharSet.charset4)) == 35
assert byte_size(medium_id(CharSet.charset2)) == 69
end
test "large ID" do
assert byte_size(large_id()) == 20
assert byte_size(large_id(CharSet.charset64)) == 17
assert byte_size(large_id(CharSet.charset32)) == 20
assert byte_size(large_id(CharSet.charset16)) == 25
assert byte_size(large_id(CharSet.charset8)) == 33
assert byte_size(large_id(CharSet.charset4)) == 50
assert byte_size(large_id(CharSet.charset2)) == 99
end
test "session ID" do
assert byte_size(session_id()) == 26
assert byte_size(session_id(CharSet.charset64)) == 22
assert byte_size(session_id(CharSet.charset32)) == 26
assert byte_size(session_id(CharSet.charset16)) == 32
assert byte_size(session_id(CharSet.charset8)) == 43
assert byte_size(session_id(CharSet.charset4)) == 64
assert byte_size(session_id(CharSet.charset2)) == 128
end
test "token" do
assert byte_size(token()) == 43
assert byte_size(token(CharSet.charset64)) == 43
assert byte_size(token(CharSet.charset32)) == 52
assert byte_size(token(CharSet.charset16)) == 64
assert byte_size(token(CharSet.charset8)) == 86
assert byte_size(token(CharSet.charset4)) == 128
assert byte_size(token(CharSet.charset2)) == 256
end
test "invalid byte count" do
{:error, _} = random_string( 7, CharSet.charset64, <<1>>)
{:error, _} = random_string(13, CharSet.charset64, <<1,2>>)
{:error, _} = random_string(25, CharSet.charset64, <<1,2,3>>)
{:error, _} = random_string(31, CharSet.charset64, <<1,2,3,4>>)
{:error, _} = random_string( 6, CharSet.charset32, <<1>>)
{:error, _} = random_string(16, CharSet.charset32, <<1,2>>)
{:error, _} = random_string(21, CharSet.charset32, <<1,2,3>>)
{:error, _} = random_string(31, CharSet.charset32, <<1,2,3,4>>)
{:error, _} = random_string(32, CharSet.charset32, <<1,2,3,4>>)
{:error, _} = random_string(41, CharSet.charset32, <<1,2,3,4,5>>)
{:error, _} = random_string(46, CharSet.charset32, <<1,2,3,4,5,6>>)
{:error, _} = random_string( 9, CharSet.charset16, <<1>>)
{:error, _} = random_string(17, CharSet.charset16, <<1,2>>)
{:error, _} = random_string( 7, CharSet.charset8, <<1>>)
{:error, _} = random_string(16, CharSet.charset8, <<1,2>>)
{:error, _} = random_string(25, CharSet.charset8, <<1,2,3>>)
{:error, _} = random_string(31, CharSet.charset8, <<1,2,3,4>>)
{:error, _} = random_string( 9, CharSet.charset4, <<1>>)
{:error, _} = random_string(17, CharSet.charset4, <<1,2>>)
{:error, _} = random_string( 9, CharSet.charset2, <<1>>)
{:error, _} = random_string(17, CharSet.charset2, <<1,2>>)
end
test "custom characters" do
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ9876543210_-"
bytes = <<0x9d, 0x99, 0x4e, 0xa5, 0xd2, 0x3f, 0x8c, 0x86, 0x80>>
assert random_string(72, charset, bytes) == "NzLoPDi-JiAa"
charset = "2346789BDFGHJMNPQRTbdfghjlmnpqrt"
bytes = <<0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x97, 0x52>>
assert random_string(55, charset, bytes) == "mHRrbgQlTqF"
charset = "0123456789ABCDEF"
bytes = <<0xc7, 0xc9, 0x00>>
assert random_string(20, charset, bytes) == "C7C90"
charset = "abcdefgh"
bytes = <<0xc7, 0xc9, 0x07, 0xc9>>
assert random_string(30, charset, bytes) == "gbheeeahgc"
charset = "atcg"
bytes = <<0x20, 0xf1>>
assert random_string(16, charset, bytes) == "acaaggat"
charset = "HT"
bytes = <<0xe3, 0xe9>>
assert random_string(16, charset, bytes) == "TTTHHHTTTTTHTHHT"
end
test "invalid charset" do
{:error, _} = random_string(10, <<"H20">>)
{:error, _} = random_string(10, <<"H202">>)
end
end