/vendor/CryptLib/test/Unit/Random/Mixer/DESTest.php

https://github.com/pbombo/warden · PHP · 92 lines · 64 code · 10 blank · 18 comment · 0 complexity · 34fc0abba893b43ad353a15f48adbd04 MD5 · raw file

  1. <?php
  2. use CryptLib\Random\Mixer\DES;
  3. use CryptLib\Core\Strength;
  4. use CryptLibTest\Mocks\Cipher\Block\Cipher;
  5. use CryptLibTest\Mocks\Cipher\Factory as CipherFactory;
  6. class Unit_Random_Mixer_DESTest extends PHPUnit_Framework_TestCase {
  7. public static function provideMix() {
  8. $data = array(
  9. array(array(), ''),
  10. array(array('', ''), ''),
  11. array(array('a'), 'a'),
  12. // This expects 'b' because of how the mock hmac function works
  13. array(array('a', 'b'), 'b'),
  14. array(array('aa', 'b'), 'b'.chr(0)),
  15. array(array('a', 'bb'), 'b'),
  16. array(array('aa', 'bb'), 'bb'),
  17. array(array('aa', 'bb', 'cc'), 'cc'),
  18. array(array('aabbcc', 'bbccdd', 'ccddee'), 'ccbbdd'),
  19. array(array('aabbccd', 'bbccdde', 'ccddeef'), 'ccbbddf'),
  20. array(array('aabbccdd', 'bbccddee', 'ccddeeff'), 'ccbbddff'),
  21. array(array('aabbccddeeffgghh', 'bbccddeeffgghhii', 'ccddeeffgghhiijj'), 'ccbbddffeeggiihh'),
  22. );
  23. return $data;
  24. }
  25. protected function getMockFactory() {
  26. $cipherkey = str_repeat(chr(0), 2);
  27. $cipher = new Cipher(array(
  28. 'getBlockSize' => function() use (&$cipherkey) {
  29. return strlen($cipherkey);
  30. },
  31. 'setKey' => function($key) use (&$cipherkey) {
  32. $cipherkey = $key;
  33. },
  34. 'encryptBlock' => function($data) use (&$cipherkey) {
  35. return $data ^ $cipherkey;
  36. },
  37. 'decryptBlock' => function($data) use (&$cipherkey) {
  38. return $data ^ $cipherkey;
  39. },
  40. ));
  41. $factory = new CipherFactory(array(
  42. 'getBlockCipher' => function ($algo) use ($cipher) { return $cipher; },
  43. ));
  44. return $factory;
  45. }
  46. /**
  47. * @covers CryptLib\Random\Mixer\DES
  48. * @covers CryptLib\Random\AbstractMixer
  49. */
  50. public function testConstructWithoutArgument() {
  51. $hash = new DES;
  52. $this->assertTrue($hash instanceof \CryptLib\Random\Mixer);
  53. }
  54. /**
  55. * @covers CryptLib\Random\Mixer\DES
  56. * @covers CryptLib\Random\AbstractMixer
  57. */
  58. public function testGetStrength() {
  59. $strength = new Strength(Strength::MEDIUM);
  60. $actual = DES::getStrength();
  61. $this->assertEquals($actual, $strength);
  62. }
  63. /**
  64. * @covers CryptLib\Random\Mixer\DES
  65. * @covers CryptLib\Random\AbstractMixer
  66. */
  67. public function testTest() {
  68. $actual = DES::test();
  69. $this->assertTrue($actual);
  70. }
  71. /**
  72. * @covers CryptLib\Random\Mixer\DES
  73. * @covers CryptLib\Random\AbstractMixer
  74. * @dataProvider provideMix
  75. */
  76. public function testMix($parts, $result) {
  77. $mock = $this->getMockFactory();
  78. $mixer = new DES($mock);
  79. $actual = $mixer->mix($parts);
  80. $this->assertEquals($result, $actual);
  81. }
  82. }