GeneratorStringTest.php 3.9 KB

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