Skip to content

Commit 2e56251

Browse files
committed
Add unit tests for PasswordEncoder
1 parent ad83196 commit 2e56251

File tree

4 files changed

+96
-21
lines changed

4 files changed

+96
-21
lines changed

src/PhpWord/Shared/Microsoft/PasswordEncoder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ public static function hashPassword($password, $algorithmSid = 4, $salt = null,
9898
// For each character, if the low byte is not equal to 0, take it. Otherwise, take the high byte.
9999
$passUtf8 = mb_convert_encoding($password, 'UCS-2LE', 'UTF-8');
100100
$byteChars = array();
101+
101102
for ($i = 0; $i < mb_strlen($password); $i++) {
102103
$byteChars[$i] = ord(substr($passUtf8, $i * 2, 1));
104+
103105
if ($byteChars[$i] == 0) {
104106
$byteChars[$i] = ord(substr($passUtf8, $i * 2 + 1, 1));
105107
}
@@ -189,6 +191,7 @@ private static function buildCombinedKey($byteChars)
189191
/**
190192
* Simulate behaviour of (signed) int32
191193
*
194+
* @codeCoverageIgnore
192195
* @param int $value
193196
* @return int
194197
*/

tests/PhpWord/Metadata/SettingsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public function testDocumentProtection()
7575
*/
7676
public function testInvalidSalt()
7777
{
78-
$p = new Protection();
79-
$p->setSalt('123');
78+
$protection = new Protection();
79+
$protection->setSalt('123');
8080
}
8181

8282
/**
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

tests/PhpWord/Writer/Word2007/Part/SettingsTest.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,6 @@ public function testDocumentProtectionWithPassword()
7979
$this->assertEquals('10', $doc->getElement($path, $file)->getAttribute('w:cryptSpinCount'));
8080
}
8181

82-
/**
83-
* Test document protection with password only
84-
*/
85-
public function testDocumentProtectionWithPasswordOnly()
86-
{
87-
$phpWord = new PhpWord();
88-
$phpWord->getSettings()->getDocumentProtection()->setEditing('readOnly');
89-
$phpWord->getSettings()->getDocumentProtection()->setPassword('testÄö@€!$&');
90-
91-
$doc = TestHelperDOCX::getDocument($phpWord);
92-
93-
$file = 'word/settings.xml';
94-
95-
$path = '/w:settings/w:documentProtection';
96-
$this->assertTrue($doc->elementExists($path, $file));
97-
$this->assertEquals('4', $doc->getElement($path, $file)->getAttribute('w:cryptAlgorithmSid'));
98-
$this->assertEquals('100000', $doc->getElement($path, $file)->getAttribute('w:cryptSpinCount'));
99-
}
100-
10182
/**
10283
* Test compatibility
10384
*/

0 commit comments

Comments
 (0)