d237ffa159ebfa6c5862915f4286fd357a5cdc34
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Password / PasswordHashingTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Password\PasswordHashingTest.
6  */
7
8 namespace Drupal\Tests\Core\Password;
9
10 use Drupal\Core\Password\PhpassHashedPassword;
11 use Drupal\Core\Password\PasswordInterface;
12 use Drupal\Tests\UnitTestCase;
13
14 /**
15  * Unit tests for password hashing API.
16  *
17  * @coversDefaultClass \Drupal\Core\Password\PhpassHashedPassword
18  * @group System
19  */
20 class PasswordHashingTest extends UnitTestCase {
21
22   /**
23    * The user for testing.
24    *
25    * @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\user\UserInterface
26    */
27   protected $user;
28
29   /**
30    * The raw password.
31    *
32    * @var string
33    */
34   protected $password;
35
36   /**
37    * The md5 password.
38    *
39    * @var string
40    */
41   protected $md5HashedPassword;
42
43   /**
44    * The hashed password.
45    *
46    * @var string
47    */
48   protected $hashedPassword;
49
50   /**
51    * The password hasher under test.
52    *
53    * @var \Drupal\Core\Password\PhpassHashedPassword
54    */
55   protected $passwordHasher;
56
57   /**
58    * {@inheritdoc}
59    */
60   protected function setUp() {
61     parent::setUp();
62     $this->password = $this->randomMachineName();
63     $this->passwordHasher = new PhpassHashedPassword(1);
64     $this->hashedPassword = $this->passwordHasher->hash($this->password);
65     $this->md5HashedPassword = 'U' . $this->passwordHasher->hash(md5($this->password));
66   }
67
68   /**
69    * Tests the hash count boundaries are enforced.
70    *
71    * @covers ::enforceLog2Boundaries
72    */
73   public function testWithinBounds() {
74     $hasher = new FakePhpassHashedPassword();
75     $this->assertEquals(PhpassHashedPassword::MIN_HASH_COUNT, $hasher->enforceLog2Boundaries(1), "Min hash count enforced");
76     $this->assertEquals(PhpassHashedPassword::MAX_HASH_COUNT, $hasher->enforceLog2Boundaries(100), "Max hash count enforced");
77   }
78
79
80   /**
81    * Test a password needs update.
82    *
83    * @covers ::needsRehash
84    */
85   public function testPasswordNeedsUpdate() {
86     // The md5 password should be flagged as needing an update.
87     $this->assertTrue($this->passwordHasher->needsRehash($this->md5HashedPassword), 'Upgraded md5 password hash needs a new hash.');
88   }
89
90   /**
91    * Test password hashing.
92    *
93    * @covers ::hash
94    * @covers ::getCountLog2
95    * @covers ::check
96    * @covers ::needsRehash
97    */
98   public function testPasswordHashing() {
99     $this->assertSame($this->passwordHasher->getCountLog2($this->hashedPassword), PhpassHashedPassword::MIN_HASH_COUNT, 'Hashed password has the minimum number of log2 iterations.');
100     $this->assertNotEquals($this->hashedPassword, $this->md5HashedPassword, 'Password hashes not the same.');
101     $this->assertTrue($this->passwordHasher->check($this->password, $this->md5HashedPassword), 'Password check succeeds.');
102     $this->assertTrue($this->passwordHasher->check($this->password, $this->hashedPassword), 'Password check succeeds.');
103     // Since the log2 setting hasn't changed and the user has a valid password,
104     // userNeedsNewHash() should return FALSE.
105     $this->assertFalse($this->passwordHasher->needsRehash($this->hashedPassword), 'Does not need a new hash.');
106   }
107
108   /**
109    * Tests password rehashing.
110    *
111    * @covers ::hash
112    * @covers ::getCountLog2
113    * @covers ::check
114    * @covers ::needsRehash
115    */
116   public function testPasswordRehashing() {
117     // Increment the log2 iteration to MIN + 1.
118     $password_hasher = new PhpassHashedPassword(PhpassHashedPassword::MIN_HASH_COUNT + 1);
119     $this->assertTrue($password_hasher->needsRehash($this->hashedPassword), 'Needs a new hash after incrementing the log2 count.');
120     // Re-hash the password.
121     $rehashed_password = $password_hasher->hash($this->password);
122     $this->assertSame($password_hasher->getCountLog2($rehashed_password), PhpassHashedPassword::MIN_HASH_COUNT + 1, 'Re-hashed password has the correct number of log2 iterations.');
123     $this->assertNotEquals($rehashed_password, $this->hashedPassword, 'Password hash changed again.');
124
125     // Now the hash should be OK.
126     $this->assertFalse($password_hasher->needsRehash($rehashed_password), 'Re-hashed password does not need a new hash.');
127     $this->assertTrue($password_hasher->check($this->password, $rehashed_password), 'Password check succeeds with re-hashed password.');
128     $this->assertTrue($this->passwordHasher->check($this->password, $rehashed_password), 'Password check succeeds with re-hashed password with original hasher.');
129   }
130
131   /**
132    * Verifies that passwords longer than 512 bytes are not hashed.
133    *
134    * @covers ::crypt
135    *
136    * @dataProvider providerLongPasswords
137    */
138   public function testLongPassword($password, $allowed) {
139
140     $hashed_password = $this->passwordHasher->hash($password);
141
142     if ($allowed) {
143       $this->assertNotFalse($hashed_password);
144     }
145     else {
146       $this->assertFalse($hashed_password);
147     }
148   }
149
150   /**
151    * Provides the test matrix for testLongPassword().
152    */
153   public function providerLongPasswords() {
154     // '512 byte long password is allowed.'
155     $passwords['allowed'] = [str_repeat('x', PasswordInterface::PASSWORD_MAX_LENGTH), TRUE];
156     // 513 byte long password is not allowed.
157     $passwords['too_long'] = [str_repeat('x', PasswordInterface::PASSWORD_MAX_LENGTH + 1), FALSE];
158
159     // Check a string of 3-byte UTF-8 characters, 510 byte long password is
160     // allowed.
161     $len = floor(PasswordInterface::PASSWORD_MAX_LENGTH / 3);
162     $diff = PasswordInterface::PASSWORD_MAX_LENGTH % 3;
163     $passwords['utf8'] = [str_repeat('€', $len), TRUE];
164     // 512 byte long password is allowed.
165     $passwords['ut8_extended'] = [$passwords['utf8'][0] . str_repeat('x', $diff), TRUE];
166
167     // Check a string of 3-byte UTF-8 characters, 513 byte long password is
168     // allowed.
169     $passwords['utf8_too_long'] = [str_repeat('€', $len + 1), FALSE];
170     return $passwords;
171   }
172
173 }
174
175 /**
176  * A fake class for tests.
177  */
178 class FakePhpassHashedPassword extends PhpassHashedPassword {
179
180   public function __construct() {
181     // Noop.
182   }
183
184   /**
185    * Exposes this method as public for tests.
186    */
187   public function enforceLog2Boundaries($count_log2) {
188     return parent::enforceLog2Boundaries($count_log2);
189   }
190
191 }