Mercurial > hg > isophonics-drupal-site
comparison core/tests/Drupal/Tests/Component/Utility/CryptTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 $this->setExpectedException(\InvalidArgumentException::class); | |
81 Crypt::hmacBase64($data, $key); | |
82 } | |
83 | |
84 /** | |
85 * Provides data for self::testHashBase64(). | |
86 * | |
87 * @return array Test data. | |
88 */ | |
89 public function providerTestHashBase64() { | |
90 return [ | |
91 [ | |
92 '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.', | |
93 'expectedHash' => '034rT6smZAVRxpq8O98cFFNLIVx_Ph1EwLZQKcmRR_s', | |
94 ], | |
95 [ | |
96 'data' => 'SHA-256 is one of the successor hash functions to SHA-1, and is one of the strongest hash functions available.', | |
97 'expected_hash' => 'yuqkDDYqprL71k4xIb6K6D7n76xldO4jseRhEkEE6SI', | |
98 ], | |
99 ]; | |
100 } | |
101 | |
102 /** | |
103 * Provides data for self::testHmacBase64(). | |
104 * | |
105 * @return array Test data. | |
106 */ | |
107 public function providerTestHmacBase64() { | |
108 return [ | |
109 [ | |
110 'data' => 'Calculates a base-64 encoded, URL-safe sha-256 hmac.', | |
111 'key' => 'secret-key', | |
112 'expected_hmac' => '2AaH63zwjhekWZlEpAiufyfhAHIzbQhl9Hd9oCi3_c8', | |
113 ], | |
114 ]; | |
115 } | |
116 | |
117 /** | |
118 * Provides data for self::testHmacBase64(). | |
119 * | |
120 * @return array Test data. | |
121 */ | |
122 public function providerTestHmacBase64Invalid() { | |
123 return [ | |
124 [new \stdClass(), new \stdClass()], | |
125 [new \stdClass(), 'string'], | |
126 [new \stdClass(), 1], | |
127 [new \stdClass(), 0], | |
128 [NULL, new \stdClass()], | |
129 ['string', new \stdClass()], | |
130 [1, new \stdClass()], | |
131 [0, new \stdClass()], | |
132 [[], []], | |
133 [[], NULL], | |
134 [[], 'string'], | |
135 [[], 1], | |
136 [[], 0], | |
137 [NULL, []], | |
138 [1, []], | |
139 [0, []], | |
140 ['string', []], | |
141 [[], NULL], | |
142 [NULL, NULL], | |
143 [NULL, 'string'], | |
144 [NULL, 1], | |
145 [NULL, 0], | |
146 [1, NULL], | |
147 [0, NULL], | |
148 ['string', NULL], | |
149 ]; | |
150 } | |
151 | |
152 } |