SodiumTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Mixer;
  11. use SecurityLib\Strength;
  12. class SodiumTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public static function provideMix()
  15. {
  16. $data = array(
  17. array(array(), ''),
  18. array(array('', ''), ''),
  19. array(array('a'), '61'),
  20. array(array('a', 'b'), '44'),
  21. array(array('aa', 'ba'), '6967'),
  22. array(array('ab', 'bb'), '73a6'),
  23. array(array('aa', 'bb'), 'bc7b'),
  24. array(array('aa', 'bb', 'cc'), '0cbd'),
  25. array(array('aabbcc', 'bbccdd', 'ccddee'), '5f0005cacd7c'),
  26. );
  27. return $data;
  28. }
  29. protected function setUp()
  30. {
  31. if (!\is_callable('sodium_crypto_generichash') || defined('HHVM_VERSION')) {
  32. $this->markTestSkipped('sodium extension is not available');
  33. }
  34. }
  35. public function testConstructWithoutArgument()
  36. {
  37. $hash = new SodiumMixer();
  38. $this->assertTrue($hash instanceof \RandomLib\Mixer);
  39. }
  40. public function testGetStrength()
  41. {
  42. $strength = new Strength(Strength::HIGH);
  43. $actual = SodiumMixer::getStrength();
  44. $this->assertEquals($actual, $strength);
  45. }
  46. public function testTest()
  47. {
  48. $actual = SodiumMixer::test();
  49. $this->assertTrue($actual);
  50. }
  51. /**
  52. * @dataProvider provideMix
  53. */
  54. public function testMix($parts, $result)
  55. {
  56. $mixer = new SodiumMixer();
  57. $actual = $mixer->mix($parts);
  58. $this->assertEquals($result, bin2hex($actual));
  59. }
  60. }