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
64?>
65--EXPECTF--
66Failed to generate an acceptable random number in 50 attempts
67int(%d)
68string(2) "ff"
69Failed to generate an acceptable random number in 50 attempts
70Failed to generate an acceptable random number in 50 attempts
71Failed to generate an acceptable random number in 50 attempts
72Failed to generate an acceptable random number in 50 attempts
73