|
1
|
|
|
<?php |
|
2
|
|
|
namespace RandomLib; |
|
3
|
|
|
|
|
4
|
|
|
class GeneratorStringTest extends \PHPUnit_Framework_TestCase { |
|
5
|
|
|
|
|
6
|
|
|
protected $generator = null; |
|
7
|
|
|
protected $mixer = null; |
|
8
|
|
|
protected $sources = array(); |
|
9
|
|
|
|
|
10
|
|
|
public static function provideCharCombinations() { |
|
11
|
|
|
return array( |
|
12
|
|
|
array("CHAR_LOWER", implode("", range("a", "z"))), |
|
13
|
|
|
array("CHAR_UPPER", implode("", range("A", "Z"))), |
|
14
|
|
|
array("CHAR_DIGITS", implode("", range(0, 9))), |
|
15
|
|
|
array("CHAR_UPPER_HEX", "0123456789ABCDEF"), |
|
16
|
|
|
array("CHAR_LOWER_HEX", "0123456789abcdef"), |
|
17
|
|
|
array("CHAR_BASE64", "+/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"), |
|
18
|
|
|
array("EASY_TO_READ", "3479ACEFHJKLMNPRTUVWXYabcdefghijkmnopqrstuvwxyz"), |
|
19
|
|
|
array("CHAR_BRACKETS", "()<>[]{}"), |
|
20
|
|
|
array("CHAR_SYMBOLS", " !\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"), |
|
21
|
|
|
array("CHAR_PUNCT", ",.:;"), |
|
22
|
|
|
array("CHAR_ALPHA", implode("", array_merge(range("A", "Z"), range("a", "z")))), |
|
23
|
|
|
array("CHAR_ALNUM", implode("", array_merge(range(0, 9), range("A", "Z"), range("a", "z")))), |
|
24
|
|
|
array("CHAR_ALPHA | PUNCT", ",.:;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", Generator::CHAR_ALPHA | Generator::CHAR_PUNCT), |
|
25
|
|
|
array("CHAR_LOWER | EASY_TO_READ", "abcdefghijkmnopqrstuvwxyz", Generator::CHAR_LOWER | Generator::EASY_TO_READ), |
|
26
|
|
|
array("CHAR_DIGITS | EASY_TO_READ", "3479", Generator::CHAR_DIGITS | Generator::EASY_TO_READ), |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function setUp() { |
|
31
|
|
|
$source1 = $this->getMock('RandomLib\Source'); |
|
32
|
|
|
$source1->expects($this->any()) |
|
33
|
|
|
->method('generate') |
|
34
|
|
|
->will($this->returnCallback(function ($size) { |
|
35
|
|
|
$r = ''; |
|
36
|
|
|
for ($i = 0; $i < $size; $i++) { |
|
37
|
|
|
$r .= chr($i % 256); |
|
38
|
|
|
} |
|
39
|
|
|
return $r; |
|
40
|
|
|
} |
|
41
|
|
|
)); |
|
42
|
|
|
$source2 = $this->getMock('RandomLib\Source'); |
|
43
|
|
|
$source2->expects($this->any()) |
|
44
|
|
|
->method('generate') |
|
45
|
|
|
->will($this->returnCallback(function ($size) { |
|
46
|
|
|
$r = ''; |
|
47
|
|
|
for ($i = 0; $i < $size; $i++) { |
|
48
|
|
|
$r .= chr(0); |
|
49
|
|
|
} |
|
50
|
|
|
return $r; |
|
51
|
|
|
} |
|
52
|
|
|
)); |
|
53
|
|
|
|
|
54
|
|
|
$this->mixer = $this->getMock('RandomLib\Mixer'); |
|
55
|
|
|
$this->mixer->expects($this->any()) |
|
56
|
|
|
->method('mix') |
|
57
|
|
|
->will($this->returnCallback(function(array $sources) { |
|
58
|
|
|
if (empty($sources)) return ''; |
|
59
|
|
|
$start = array_pop($sources); |
|
60
|
|
|
return array_reduce( |
|
61
|
|
|
$sources, |
|
62
|
|
|
function($el1, $el2) { |
|
63
|
|
|
return $el1 ^ $el2; |
|
64
|
|
|
}, |
|
65
|
|
|
$start |
|
66
|
|
|
); |
|
67
|
|
|
})); |
|
68
|
|
|
|
|
69
|
|
|
$this->sources = array($source1, $source2); |
|
70
|
|
|
$this->generator = new Generator($this->sources, $this->mixer); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @dataProvider provideCharCombinations |
|
75
|
|
|
*/ |
|
76
|
|
|
public function testScheme($schemeName, $expected, $scheme = 0) { |
|
77
|
|
|
// test for overspecification by doubling the expected amount |
|
78
|
|
|
if (!$scheme) { |
|
79
|
|
|
$scheme = constant("RandomLib\Generator::$schemeName"); |
|
80
|
|
|
} |
|
81
|
|
|
$chars = $this->generator->generateString(strlen($expected) * 2, $scheme); |
|
82
|
|
|
$this->assertEquals($expected . $expected, $chars, sprintf("Testing Generator::%s failed", $schemeName)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|