From 2e562512f4d969edfd500252b9fe7107c1d45f74 Mon Sep 17 00:00:00 2001 From: troosan Date: Fri, 24 Nov 2017 14:45:05 +0100 Subject: [PATCH] Add unit tests for PasswordEncoder --- .../Shared/Microsoft/PasswordEncoder.php | 3 + tests/PhpWord/Metadata/SettingsTest.php | 4 +- .../Shared/Microsoft/PasswordEncoderTest.php | 91 +++++++++++++++++++ .../Writer/Word2007/Part/SettingsTest.php | 19 ---- 4 files changed, 96 insertions(+), 21 deletions(-) create mode 100644 tests/PhpWord/Shared/Microsoft/PasswordEncoderTest.php diff --git a/src/PhpWord/Shared/Microsoft/PasswordEncoder.php b/src/PhpWord/Shared/Microsoft/PasswordEncoder.php index 40a3ea12..cddcfcd3 100644 --- a/src/PhpWord/Shared/Microsoft/PasswordEncoder.php +++ b/src/PhpWord/Shared/Microsoft/PasswordEncoder.php @@ -98,8 +98,10 @@ class PasswordEncoder // For each character, if the low byte is not equal to 0, take it. Otherwise, take the high byte. $passUtf8 = mb_convert_encoding($password, 'UCS-2LE', 'UTF-8'); $byteChars = array(); + for ($i = 0; $i < mb_strlen($password); $i++) { $byteChars[$i] = ord(substr($passUtf8, $i * 2, 1)); + if ($byteChars[$i] == 0) { $byteChars[$i] = ord(substr($passUtf8, $i * 2 + 1, 1)); } @@ -189,6 +191,7 @@ class PasswordEncoder /** * Simulate behaviour of (signed) int32 * + * @codeCoverageIgnore * @param int $value * @return int */ diff --git a/tests/PhpWord/Metadata/SettingsTest.php b/tests/PhpWord/Metadata/SettingsTest.php index a2a80b12..9830fd28 100644 --- a/tests/PhpWord/Metadata/SettingsTest.php +++ b/tests/PhpWord/Metadata/SettingsTest.php @@ -75,8 +75,8 @@ class SettingsTest extends \PHPUnit\Framework\TestCase */ public function testInvalidSalt() { - $p = new Protection(); - $p->setSalt('123'); + $protection = new Protection(); + $protection->setSalt('123'); } /** diff --git a/tests/PhpWord/Shared/Microsoft/PasswordEncoderTest.php b/tests/PhpWord/Shared/Microsoft/PasswordEncoderTest.php new file mode 100644 index 00000000..7b2bd3e7 --- /dev/null +++ b/tests/PhpWord/Shared/Microsoft/PasswordEncoderTest.php @@ -0,0 +1,91 @@ +assertEquals('10', $doc->getElement($path, $file)->getAttribute('w:cryptSpinCount')); } - /** - * Test document protection with password only - */ - public function testDocumentProtectionWithPasswordOnly() - { - $phpWord = new PhpWord(); - $phpWord->getSettings()->getDocumentProtection()->setEditing('readOnly'); - $phpWord->getSettings()->getDocumentProtection()->setPassword('testÄö@€!$&'); - - $doc = TestHelperDOCX::getDocument($phpWord); - - $file = 'word/settings.xml'; - - $path = '/w:settings/w:documentProtection'; - $this->assertTrue($doc->elementExists($path, $file)); - $this->assertEquals('4', $doc->getElement($path, $file)->getAttribute('w:cryptAlgorithmSid')); - $this->assertEquals('100000', $doc->getElement($path, $file)->getAttribute('w:cryptSpinCount')); - } - /** * Test compatibility */