CryptTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Drupal\Tests\Component\Utility;
  3. use Drupal\Component\Utility\Crypt;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * Tests random byte generation.
  7. *
  8. * @group Utility
  9. *
  10. * @coversDefaultClass \Drupal\Component\Utility\Crypt
  11. */
  12. class CryptTest extends TestCase {
  13. /**
  14. * Tests random byte generation.
  15. *
  16. * @covers ::randomBytes
  17. *
  18. * @see \Drupal\Tests\Component\Utility\CryptRandomFallbackTest::testRandomBytesFallback
  19. */
  20. public function testRandomBytes() {
  21. for ($i = 1; $i < 10; $i++) {
  22. $count = rand(10, 10000);
  23. // Check that different values are being generated.
  24. $this->assertNotEquals(Crypt::randomBytes($count), Crypt::randomBytes($count));
  25. // Check the length.
  26. $this->assertEquals(strlen(Crypt::randomBytes($count)), $count);
  27. }
  28. }
  29. /**
  30. * Tests hash generation.
  31. *
  32. * @dataProvider providerTestHashBase64
  33. * @covers ::hashBase64
  34. *
  35. * @param string $data
  36. * Data to hash.
  37. * @param string $expected_hash
  38. * Expected result from hashing $data.
  39. */
  40. public function testHashBase64($data, $expected_hash) {
  41. $hash = Crypt::hashBase64($data);
  42. $this->assertEquals($expected_hash, $hash, 'The correct hash was not calculated.');
  43. }
  44. /**
  45. * Tests HMAC generation.
  46. *
  47. * @dataProvider providerTestHmacBase64
  48. * @covers ::hmacBase64
  49. *
  50. * @param string $data
  51. * Data to hash.
  52. * @param string $key
  53. * Key to use in hashing process.
  54. * @param string $expected_hmac
  55. * Expected result from hashing $data using $key.
  56. */
  57. public function testHmacBase64($data, $key, $expected_hmac) {
  58. $hmac = Crypt::hmacBase64($data, $key);
  59. $this->assertEquals($expected_hmac, $hmac, 'The correct hmac was not calculated.');
  60. }
  61. /**
  62. * Tests the hmacBase64 method with invalid parameters.
  63. *
  64. * @dataProvider providerTestHmacBase64Invalid
  65. * @covers ::hmacBase64
  66. *
  67. * @param string $data
  68. * Data to hash.
  69. * @param string $key
  70. * Key to use in hashing process.
  71. */
  72. public function testHmacBase64Invalid($data, $key) {
  73. if (method_exists($this, 'expectException')) {
  74. $this->expectException('InvalidArgumentException');
  75. }
  76. else {
  77. $this->setExpectedException('InvalidArgumentException');
  78. }
  79. Crypt::hmacBase64($data, $key);
  80. }
  81. /**
  82. * Provides data for self::testHashBase64().
  83. *
  84. * @return array Test data.
  85. */
  86. public function providerTestHashBase64() {
  87. return [
  88. [
  89. 'data' => 'The SHA (Secure Hash Algorithm) is one of a number of cryptographic hash functions. A cryptographic hash is like a signature for a text or a data file. SHA-256 algorithm generates an almost-unique, fixed size 256-bit (32-byte) hash. Hash is a one way function – it cannot be decrypted back. This makes it suitable for password validation, challenge hash authentication, anti-tamper, digital signatures.',
  90. 'expectedHash' => '034rT6smZAVRxpq8O98cFFNLIVx_Ph1EwLZQKcmRR_s',
  91. ],
  92. [
  93. 'data' => 'SHA-256 is one of the successor hash functions to SHA-1, and is one of the strongest hash functions available.',
  94. 'expected_hash' => 'yuqkDDYqprL71k4xIb6K6D7n76xldO4jseRhEkEE6SI',
  95. ],
  96. ];
  97. }
  98. /**
  99. * Provides data for self::testHmacBase64().
  100. *
  101. * @return array Test data.
  102. */
  103. public function providerTestHmacBase64() {
  104. return [
  105. [
  106. 'data' => 'Calculates a base-64 encoded, URL-safe sha-256 hmac.',
  107. 'key' => 'secret-key',
  108. 'expected_hmac' => '2AaH63zwjhekWZlEpAiufyfhAHIzbQhl9Hd9oCi3_c8',
  109. ],
  110. ];
  111. }
  112. /**
  113. * Provides data for self::testHmacBase64().
  114. *
  115. * @return array Test data.
  116. */
  117. public function providerTestHmacBase64Invalid() {
  118. return [
  119. [new \stdClass(), new \stdClass()],
  120. [new \stdClass(), 'string'],
  121. [new \stdClass(), 1],
  122. [new \stdClass(), 0],
  123. [NULL, new \stdClass()],
  124. ['string', new \stdClass()],
  125. [1, new \stdClass()],
  126. [0, new \stdClass()],
  127. [[], []],
  128. [[], NULL],
  129. [[], 'string'],
  130. [[], 1],
  131. [[], 0],
  132. [NULL, []],
  133. [1, []],
  134. [0, []],
  135. ['string', []],
  136. [[], NULL],
  137. [NULL, NULL],
  138. [NULL, 'string'],
  139. [NULL, 1],
  140. [NULL, 0],
  141. [1, NULL],
  142. [0, NULL],
  143. ['string', NULL],
  144. ];
  145. }
  146. }