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