/
GeneratorTest.php
282 lines (254 loc) · 8.01 KB
/
GeneratorTest.php
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* (The MIT license)
* Copyright 2017 clickalicious, Benjamin Carl
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Clickalicious\Rng;
/**
* Class GeneratorTest
*
* @package Rng
*/
class GeneratorTest extends \PHPUnit_Framework_TestCase
{
/**
* Test: Get instance.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testInstance()
{
$this->assertInstanceOf(
'Clickalicious\Rng\Generator',
new Generator()
);
}
/**
* Test: Get instance with mode default.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testCreatingInstanceByModeDefault()
{
$generator = new Generator();
$this->assertInstanceOf(
'Clickalicious\Rng\Generator',
$generator
);
}
/**
* Test: Get instance with mode php default.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testCreatingInstanceByModePhpDefault()
{
$generator = new Generator(
Generator::MODE_PHP_DEFAULT
);
$this->assertInstanceOf(
'Clickalicious\Rng\Generator',
$generator
);
}
/**
* Test: Get instance with mode php mersenne twister default.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testCreatingInstanceByModePhpMersenneTwister()
{
$generator = new Generator(
Generator::MODE_PHP_MERSENNE_TWISTER
);
$this->assertInstanceOf(
'Clickalicious\Rng\Generator',
$generator
);
}
/**
* Test: Get instance with mode OpenSSL.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testCreatingInstanceByModeOpenSsl()
{
$generator = new Generator(
Generator::MODE_OPEN_SSL
);
$this->assertInstanceOf(
'Clickalicious\Rng\Generator',
$generator
);
}
/**
* Test generating random number with default setting.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testGeneratingRandomIntegerByDefaults()
{
$generator = new Generator();
$this->assertInternalType('int', $generator->generate());
}
/**
* Test generating random number with php default implementation.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testGeneratingRandomIntegerByPhpDefault()
{
$generator = new Generator(Generator::MODE_PHP_DEFAULT);
$this->assertInternalType('int', $generator->generate());
}
/**
* Test generating random number with php new mersenne twister algorithm.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testGeneratingRandomIntegerByPhpMersenneTwister()
{
$generator = new Generator(Generator::MODE_PHP_MERSENNE_TWISTER);
$this->assertInternalType('int', $generator->generate());
}
/**
* Test generating random number with php openssl extension algorithm.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testGeneratingRandomIntegerByOpenSsl()
{
$generator = new Generator(Generator::MODE_OPEN_SSL);
$this->assertInternalType('int', $generator->generate());
}
/**
* Test: Test passing invalid/unknown mode.
*
* @author Benjamin Carl <opensource@clickalicious.de>
* @expectedException \Clickalicious\Rng\Exception
*/
public function testCreatingInstanceByInvalidModeException()
{
new Generator(PHP_INT_MAX);
}
/**
* Test: Test generating seeds.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testGeneratingIntegerSeed()
{
$generator = new Generator();
$seed = $generator->generateSeed();
$this->assertInternalType('int', $seed);
}
/**
* Test: Test generating instance with seed.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testCreatingInstanceByModeOpenSslWithSeed()
{
$seed = 123456;
$generator = new Generator(Generator::MODE_OPEN_SSL, $seed);
$this->assertSame($seed, $generator->getSeed());
}
/**
* Test: Test generating instance with seed.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testCreatingInstanceByModePhpDefaultWithSeed()
{
$seed = 123456;
$generator = new Generator(Generator::MODE_PHP_DEFAULT, $seed);
$this->assertSame($seed, $generator->getSeed());
}
/**
* Test: Test generating instance with seed.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testCreatingInstanceByModePhpMersenneTwisterWithSeed()
{
$seed = 123456;
$generator = new Generator(Generator::MODE_PHP_MERSENNE_TWISTER, $seed);
$this->assertSame($seed, $generator->getSeed());
}
/**
* Test: Test generating instance with seed.
*
* @author Benjamin Carl <opensource@clickalicious.de>
* @expectedException \Clickalicious\Rng\Exception
*/
public function testTryToSetVariableWithInvalidTypeForSeed()
{
$seed = 'Foo';
$generator = new Generator();
$generator
->setSeed($seed);
}
/**
* Test: Test generating random bytes for public API.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testGenerateRandomBytesByModePhpInternalOnPublicApi()
{
$generator = new Generator();
$randomBytes = $generator->getRandomBytes(4096);
$this->assertEquals(4096, strlen($randomBytes), 'Validating result length in bytes ...');
}
/**
* Test: Test generating random bytes for public API.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testGenerateRandomBytesByModeOpenSslOnPublicApi()
{
$generator = new Generator();
$randomBytes = $generator->getRandomBytes(4096, Generator::MODE_OPEN_SSL);
$this->assertEquals(4096, strlen($randomBytes), 'Validating result length in bytes ...');
}
/**
* Test: Test setting an invalid mode.
*
* @author Benjamin Carl <opensource@clickalicious.de>
* @expectedException \Clickalicious\Rng\Exception
*/
public function testTryToSetInvalidEncryptionMode()
{
$generator = new Generator();
$generator->setMode(4096);
}
/**
* Test: Test setting a seed value on init.
*
* @author Benjamin Carl <opensource@clickalicious.de>
*/
public function testPassingSeedToConstructor()
{
$generator = new Generator(Generator::MODE_OPEN_SSL, time());
$this->assertInstanceOf('Clickalicious\Rng\Generator', $generator);
}
}