|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: benedikt |
|
6
|
|
|
* Date: 10/1/17 |
|
7
|
|
|
* Time: 2:08 PM |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Tfboe\FmLib\Tests\Unit\Helpers; |
|
11
|
|
|
|
|
12
|
|
|
use Tfboe\FmLib\Helpers\Random; |
|
13
|
|
|
use Tfboe\FmLib\Tests\Helpers\UnitTestCase; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class HandlerTest |
|
17
|
|
|
* @package Tfboe\FmLib\Tests\Unit\Exceptions |
|
18
|
|
|
*/ |
|
19
|
|
|
class RandomTest extends UnitTestCase |
|
20
|
|
|
{ |
|
21
|
|
|
//<editor-fold desc="Public Methods"> |
|
22
|
|
|
/** |
|
23
|
|
|
* @covers \Tfboe\FmLib\Helpers\Random::__construct |
|
24
|
|
|
*/ |
|
25
|
|
|
public function testConstruct() |
|
26
|
|
|
{ |
|
27
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
28
|
|
|
$hex = "0abce081f"; |
|
29
|
|
|
$random = new Random($hex); |
|
30
|
|
|
self::assertInstanceOf(Random::class, $random); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @covers \Tfboe\FmLib\Helpers\Random::extractEntropy |
|
35
|
|
|
* @covers \Tfboe\FmLib\Helpers\Random::countBits |
|
36
|
|
|
* @uses \Tfboe\FmLib\Exceptions\Internal::assert |
|
37
|
|
|
* @uses \Tfboe\FmLib\Helpers\Random::__construct |
|
38
|
|
|
* @uses \Tfboe\FmLib\Helpers\Random::extractEntropyByBits |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testEntropy() |
|
41
|
|
|
{ |
|
42
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
43
|
|
|
$hex = "0abce081f"; |
|
44
|
|
|
$random = new Random($hex); |
|
45
|
|
|
$res1 = $random->extractEntropy(799); |
|
46
|
|
|
$random2 = new Random($hex); |
|
47
|
|
|
$res2 = $random2->extractEntropy(1023); |
|
48
|
|
|
self::assertEquals($res1, $res2 % 800); |
|
49
|
|
|
$random3 = new Random($hex); |
|
50
|
|
|
$res3 = $random3->extractEntropy(523, -500); |
|
51
|
|
|
self::assertEquals($res2 - 500, $res3); |
|
52
|
|
|
$r1 = $random->extractEntropy(1000000000); |
|
53
|
|
|
$r2 = $random2->extractEntropy(1000000000); |
|
54
|
|
|
$r3 = $random3->extractEntropy(1000000000); |
|
55
|
|
|
self::assertEquals($r1, $r2); |
|
56
|
|
|
self::assertEquals($r2, $r3); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @covers \Tfboe\FmLib\Helpers\Random::extractEntropyByBits |
|
61
|
|
|
* @uses \Tfboe\FmLib\Helpers\Random::__construct |
|
62
|
|
|
* @uses \Tfboe\FmLib\Exceptions\Internal::assert |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testExtractEntropyByBits() |
|
65
|
|
|
{ |
|
66
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
67
|
|
|
$hex = "0abce081f"; |
|
68
|
|
|
$random = new Random($hex); |
|
69
|
|
|
$res1 = $random->extractEntropyByBits(10); |
|
70
|
|
|
self::assertGreaterThanOrEqual(0, $res1); |
|
71
|
|
|
self::assertLessThan(1024, $res1); |
|
72
|
|
|
$res2 = $random->extractEntropyByBits(1); |
|
73
|
|
|
self::assertGreaterThanOrEqual(0, $res2); |
|
74
|
|
|
self::assertLessThan(2, $res2); |
|
75
|
|
|
$random2 = new Random($hex); |
|
76
|
|
|
$res = $random2->extractEntropyByBits(11); |
|
77
|
|
|
self::assertEquals($res1 * 2 + $res2, $res); |
|
78
|
|
|
|
|
79
|
|
|
self::assertEquals($random->extractEntropyByBits(100), $random2->extractEntropyByBits(100)); |
|
80
|
|
|
|
|
81
|
|
|
//now the entropy must be empty |
|
82
|
|
|
self::assertEquals(0, $random->extractEntropyByBits(100)); |
|
83
|
|
|
self::assertEquals(0, $random2->extractEntropyByBits(100)); |
|
84
|
|
|
|
|
85
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
86
|
|
|
$random3 = new Random("123456789abcdef0"); |
|
87
|
|
|
|
|
88
|
|
|
//if we have bad luck the following may be wrong (probability 1/1024) |
|
89
|
|
|
self::assertNotEquals($res, $random3->extractEntropyByBits(11)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @covers \Tfboe\FmLib\Helpers\Random::extractEntropy |
|
94
|
|
|
* @covers \Tfboe\FmLib\Helpers\Random::countBits |
|
95
|
|
|
* @uses \Tfboe\FmLib\Exceptions\Internal::assert |
|
96
|
|
|
* @uses \Tfboe\FmLib\Helpers\Random::__construct |
|
97
|
|
|
* @uses \Tfboe\FmLib\Helpers\Random::extractEntropyByBits |
|
98
|
|
|
*/ |
|
99
|
|
|
public function testFullRandomInteger() |
|
100
|
|
|
{ |
|
101
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
102
|
|
|
$random = new Random("08791234abcdef778899aeef87224effffffeeee123234641aaa"); |
|
103
|
|
|
self::assertIsInt($random->extractEntropy(PHP_INT_MAX, PHP_INT_MIN)); |
|
104
|
|
|
self::assertIsInt($random->extractEntropy(PHP_INT_MAX - 1, PHP_INT_MIN + 1)); |
|
105
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
106
|
|
|
$random = new Random("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); |
|
107
|
|
|
self::assertEquals(PHP_INT_MAX, $random->extractEntropy(PHP_INT_MAX, PHP_INT_MIN)); |
|
108
|
|
|
self::assertEquals(PHP_INT_MAX - 13, $random->extractEntropy(PHP_INT_MAX - 1, PHP_INT_MIN + 1)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @covers \Tfboe\FmLib\Helpers\Random::stringToRandom |
|
113
|
|
|
* @uses \Tfboe\FmLib\Helpers\Random::__construct |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testStringToRandom() |
|
116
|
|
|
{ |
|
117
|
|
|
$message = "message"; |
|
118
|
|
|
$i1 = Random::stringToRandom($message); |
|
119
|
|
|
self::assertEquals($i1, Random::stringToRandom($message)); |
|
120
|
|
|
self::assertNotEquals($i1, Random::stringToRandom("Message")); |
|
121
|
|
|
} |
|
122
|
|
|
//</editor-fold desc="Public Methods"> |
|
123
|
|
|
} |