CryptTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * @expectedDeprecation Drupal\Component\Utility\Crypt::randomBytes() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use PHP's built-in random_bytes() function instead. See https://www.drupal.org/node/3057191
  18. * @group legacy
  19. */
  20. public function testRandomBytes() {
  21. $this->assertSame(16, strlen(Crypt::randomBytes(16)));
  22. }
  23. /**
  24. * Tests hash generation.
  25. *
  26. * @dataProvider providerTestHashBase64
  27. * @covers ::hashBase64
  28. *
  29. * @param string $data
  30. * Data to hash.
  31. * @param string $expected_hash
  32. * Expected result from hashing $data.
  33. */
  34. public function testHashBase64($data, $expected_hash) {
  35. $hash = Crypt::hashBase64($data);
  36. $this->assertEquals($expected_hash, $hash, 'The correct hash was not calculated.');
  37. }
  38. /**
  39. * Tests HMAC generation.
  40. *
  41. * @dataProvider providerTestHmacBase64
  42. * @covers ::hmacBase64
  43. *
  44. * @param string $data
  45. * Data to hash.
  46. * @param string $key
  47. * Key to use in hashing process.
  48. * @param string $expected_hmac
  49. * Expected result from hashing $data using $key.
  50. */
  51. public function testHmacBase64($data, $key, $expected_hmac) {
  52. $hmac = Crypt::hmacBase64($data, $key);
  53. $this->assertEquals($expected_hmac, $hmac, 'The correct hmac was not calculated.');
  54. }
  55. /**
  56. * Tests the hmacBase64 method with invalid parameters.
  57. *
  58. * @dataProvider providerTestHmacBase64Invalid
  59. * @covers ::hmacBase64
  60. *
  61. * @param string $data
  62. * Data to hash.
  63. * @param string $key
  64. * Key to use in hashing process.
  65. */
  66. public function testHmacBase64Invalid($data, $key) {
  67. $this->expectException('InvalidArgumentException');
  68. Crypt::hmacBase64($data, $key);
  69. }
  70. /**
  71. * Provides data for self::testHashBase64().
  72. *
  73. * @return array Test data.
  74. */
  75. public function providerTestHashBase64() {
  76. return [
  77. [
  78. '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.',
  79. 'expectedHash' => '034rT6smZAVRxpq8O98cFFNLIVx_Ph1EwLZQKcmRR_s',
  80. ],
  81. [
  82. 'data' => 'SHA-256 is one of the successor hash functions to SHA-1, and is one of the strongest hash functions available.',
  83. 'expected_hash' => 'yuqkDDYqprL71k4xIb6K6D7n76xldO4jseRhEkEE6SI',
  84. ],
  85. ];
  86. }
  87. /**
  88. * Provides data for self::testHmacBase64().
  89. *
  90. * @return array Test data.
  91. */
  92. public function providerTestHmacBase64() {
  93. return [
  94. [
  95. 'data' => 'Calculates a base-64 encoded, URL-safe sha-256 hmac.',
  96. 'key' => 'secret-key',
  97. 'expected_hmac' => '2AaH63zwjhekWZlEpAiufyfhAHIzbQhl9Hd9oCi3_c8',
  98. ],
  99. ];
  100. }
  101. /**
  102. * Provides data for self::testHmacBase64().
  103. *
  104. * @return array Test data.
  105. */
  106. public function providerTestHmacBase64Invalid() {
  107. return [
  108. [new \stdClass(), new \stdClass()],
  109. [new \stdClass(), 'string'],
  110. [new \stdClass(), 1],
  111. [new \stdClass(), 0],
  112. [NULL, new \stdClass()],
  113. ['string', new \stdClass()],
  114. [1, new \stdClass()],
  115. [0, new \stdClass()],
  116. [[], []],
  117. [[], NULL],
  118. [[], 'string'],
  119. [[], 1],
  120. [[], 0],
  121. [NULL, []],
  122. [1, []],
  123. [0, []],
  124. ['string', []],
  125. [[], NULL],
  126. [NULL, NULL],
  127. [NULL, 'string'],
  128. [NULL, 1],
  129. [NULL, 0],
  130. [1, NULL],
  131. [0, NULL],
  132. ['string', NULL],
  133. ];
  134. }
  135. /**
  136. * Legacy test of Drupal\Component\Utility\Crypt::hashEquals() method.
  137. *
  138. * @expectedDeprecation Drupal\Component\Utility\Crypt::hashEquals() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use PHP's built-in hash_equals() function instead. See https://www.drupal.org/node/3054488
  139. * @group legacy
  140. */
  141. public function testHashEquals() {
  142. $a_hash = Crypt::hashBase64('a');
  143. $b_hash = Crypt::hashBase64('b');
  144. $this->assertTrue(Crypt::hashEquals($a_hash, $a_hash));
  145. $this->assertFalse(Crypt::hashEquals($a_hash, $b_hash));
  146. }
  147. }