|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of PHPWord - A pure PHP library for reading and writing |
| 4 | + * word processing documents. |
| 5 | + * |
| 6 | + * PHPWord is free software distributed under the terms of the GNU Lesser |
| 7 | + * General Public License version 3 as published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * For the full copyright and license information, please read the LICENSE |
| 10 | + * file that was distributed with this source code. For the full list of |
| 11 | + * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 12 | + * |
| 13 | + * @see https://github.com/PHPOffice/PHPWord |
| 14 | + * @copyright 2010-2017 PHPWord contributors |
| 15 | + * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 16 | + */ |
| 17 | + |
| 18 | +namespace PhpOffice\PhpWord\Shared; |
| 19 | + |
| 20 | +use PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test class for PhpOffice\PhpWord\Shared\Html |
| 25 | + * @coversDefaultClass \PhpOffice\PhpWord\Shared\Html |
| 26 | + */ |
| 27 | +class PasswordEncoderTest extends \PHPUnit\Framework\TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * Test that a password can be hashed without specifying any additional parameters |
| 31 | + */ |
| 32 | + public function testEncodePassword() |
| 33 | + { |
| 34 | + //given |
| 35 | + $password = 'test'; |
| 36 | + |
| 37 | + //when |
| 38 | + $hashPassword = PasswordEncoder::hashPassword($password); |
| 39 | + |
| 40 | + //then |
| 41 | + TestCase::assertEquals('M795/MAlmGU8RIsY9Q9uDLHC7bk=', $hashPassword); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test that a password can be hashed with a custom salt |
| 46 | + */ |
| 47 | + public function testEncodePasswordWithSalt() |
| 48 | + { |
| 49 | + //given |
| 50 | + $password = 'test'; |
| 51 | + $salt = base64_decode('uq81pJRRGFIY5U+E9gt8tA=='); |
| 52 | + |
| 53 | + //when |
| 54 | + $hashPassword = PasswordEncoder::hashPassword($password, 4, $salt); |
| 55 | + |
| 56 | + //then |
| 57 | + TestCase::assertEquals('QiDOcpia1YzSVJPiKPwWebl9p/0=', $hashPassword); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test that the encoder falls back on SHA-1 if a non supported algorithm is given |
| 62 | + */ |
| 63 | + public function testDafaultsToSha1IfUnsupportedAlgorithm() |
| 64 | + { |
| 65 | + //given |
| 66 | + $password = 'test'; |
| 67 | + $salt = base64_decode('uq81pJRRGFIY5U+E9gt8tA=='); |
| 68 | + |
| 69 | + //when |
| 70 | + $hashPassword = PasswordEncoder::hashPassword($password, 5, $salt); |
| 71 | + |
| 72 | + //then |
| 73 | + TestCase::assertEquals('QiDOcpia1YzSVJPiKPwWebl9p/0=', $hashPassword); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Test that the encoder falls back on SHA-1 if a non supported algorithm is given |
| 78 | + */ |
| 79 | + public function testEncodePasswordWithNullAsciiCodeInPassword() |
| 80 | + { |
| 81 | + //given |
| 82 | + $password = 'test' . chr(0); |
| 83 | + $salt = base64_decode('uq81pJRRGFIY5U+E9gt8tA=='); |
| 84 | + |
| 85 | + //when |
| 86 | + $hashPassword = PasswordEncoder::hashPassword($password, 5, $salt, 1); |
| 87 | + |
| 88 | + //then |
| 89 | + TestCase::assertEquals('rDV9sgdDsztoCQlvRCb1lF2wxNg=', $hashPassword); |
| 90 | + } |
| 91 | +} |
0 commit comments