1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Protection; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class ProtectionTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
public function testVerifyPassword(): void |
11
|
|
|
{ |
12
|
|
|
$protection = new Protection(); |
13
|
|
|
self::assertTrue($protection->verify('foo'), 'non-protected always pass'); |
14
|
|
|
|
15
|
|
|
$protection->setSheet(true); |
16
|
|
|
self::assertFalse($protection->verify('foo'), 'protected will fail'); |
17
|
|
|
|
18
|
|
|
$protection->setPassword('foo', true); |
19
|
|
|
self::assertSame('foo', $protection->getPassword(), 'was not stored as-is, without hashing'); |
20
|
|
|
self::assertFalse($protection->verify('foo'), 'setting already hashed password will not match'); |
21
|
|
|
|
22
|
|
|
$protection->setPassword('foo'); |
23
|
|
|
self::assertSame('CC40', $protection->getPassword(), 'was hashed'); |
24
|
|
|
self::assertTrue($protection->verify('foo'), 'setting non-hashed password will hash it and not match'); |
25
|
|
|
|
26
|
|
|
$protection->setAlgorithm(Protection::ALGORITHM_MD5); |
27
|
|
|
self::assertFalse($protection->verify('foo'), 'changing algorithm will not match anymore'); |
28
|
|
|
|
29
|
|
|
$protection->setPassword('foo'); |
30
|
|
|
$hash1 = $protection->getPassword(); |
31
|
|
|
$protection->setPassword('foo'); |
32
|
|
|
$hash2 = $protection->getPassword(); |
33
|
|
|
|
34
|
|
|
self::assertSame(24, mb_strlen($hash1)); |
35
|
|
|
self::assertSame(24, mb_strlen($hash2)); |
36
|
|
|
self::assertNotSame($hash1, $hash2, 'was hashed with automatic salt'); |
37
|
|
|
self::assertTrue($protection->verify('foo'), 'setting password again, will hash with proper algorithm and will match'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|