1--TEST--
2Random: Randomizer: Heavily biased engines are detected and rejected
3--FILE--
4<?php
5
6use Random\Engine;
7use Random\Randomizer;
8
9final class HeavilyBiasedEngine implements Engine
10{
11    public function generate(): string
12    {
13        return str_repeat("\xff", PHP_INT_SIZE);
14    }
15}
16
17function randomizer(): Randomizer
18{
19    return new Randomizer(new HeavilyBiasedEngine());
20}
21
22try {
23    var_dump(randomizer()->getInt(0, 1234));
24} catch (Random\BrokenRandomEngineError $e) {
25    echo $e->getMessage(), PHP_EOL;
26}
27
28try {
29    var_dump(randomizer()->nextInt());
30} catch (Random\BrokenRandomEngineError $e) {
31    echo $e->getMessage(), PHP_EOL;
32}
33
34try {
35    var_dump(bin2hex(randomizer()->getBytes(1)));
36} catch (Random\BrokenRandomEngineError $e) {
37    echo $e->getMessage(), PHP_EOL;
38}
39
40try {
41    var_dump(randomizer()->shuffleArray(range(1, 1234)));
42} catch (Random\BrokenRandomEngineError $e) {
43    echo $e->getMessage(), PHP_EOL;
44}
45
46try {
47    var_dump(randomizer()->pickArrayKeys(range(1, 1234), 1));
48} catch (Random\BrokenRandomEngineError $e) {
49    echo $e->getMessage(), PHP_EOL;
50}
51
52try {
53    var_dump(randomizer()->pickArrayKeys(range(1, 1234), 10));
54} catch (Random\BrokenRandomEngineError $e) {
55    echo $e->getMessage(), PHP_EOL;
56}
57
58try {
59    var_dump(randomizer()->shuffleBytes('foobar'));
60} catch (Random\BrokenRandomEngineError $e) {
61    echo $e->getMessage(), PHP_EOL;
62}
63
64try {
65    var_dump(randomizer()->getBytesFromString('123', 10));
66} catch (Random\BrokenRandomEngineError $e) {
67    echo $e->getMessage(), PHP_EOL;
68}
69
70try {
71    var_dump(randomizer()->getBytesFromString(str_repeat('a', 500), 10));
72} catch (Random\BrokenRandomEngineError $e) {
73    echo $e->getMessage(), PHP_EOL;
74}
75
76?>
77--EXPECTF--
78Failed to generate an acceptable random number in 50 attempts
79int(%d)
80string(2) "ff"
81Failed to generate an acceptable random number in 50 attempts
82Failed to generate an acceptable random number in 50 attempts
83Failed to generate an acceptable random number in 50 attempts
84Failed to generate an acceptable random number in 50 attempts
85Failed to generate an acceptable random number in 50 attempts
86Failed to generate an acceptable random number in 50 attempts
87