1
0
mirror of https://github.com/Rogiel/php-mpq synced 2025-12-06 00:13:04 +00:00

Improves testing and documentation

This commit is contained in:
2016-01-07 01:54:34 -02:00
parent e02bd6da17
commit aeb01ce902
28 changed files with 1377 additions and 28 deletions

View File

@@ -0,0 +1,42 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests;
use Rogiel\MPQ\Stream\MemoryStream;
use Rogiel\MPQ\Stream\Parser\BinaryStreamParser;
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase {
protected function createMemoryParser($string) {
$stream = new MemoryStream($string);
return new BinaryStreamParser($stream);
}
}

View File

@@ -0,0 +1,86 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Compression;
use Rogiel\MPQ\Compression\BZIPCompression;
use Rogiel\MPQ\Exception\Compression\InvalidInputDataException;
use Rogiel\MPQ\Tests\AbstractTestCase;
class BZIPCompressionTest extends AbstractTestCase {
const TEST_COMPRESSED_DATA = "425a6834314159265359065c89da0000009780400000400080060490002000310c082031a916c41d41e2ee48a70a1200cb913b40";
const TEST_UNCOMPRESSED_DATA = "Hello World";
public function testCompress() {
$compression = new BZIPCompression();
$compressed = $compression->compress(
self::TEST_UNCOMPRESSED_DATA, strlen(self::TEST_UNCOMPRESSED_DATA)
);
$this->assertEquals(hex2bin(self::TEST_COMPRESSED_DATA), $compressed);
}
public function testDecompress() {
$compression = new BZIPCompression();
$compressed = $compression->decompress(
hex2bin(self::TEST_COMPRESSED_DATA), strlen(hex2bin(self::TEST_COMPRESSED_DATA))
);
$this->assertEquals(self::TEST_UNCOMPRESSED_DATA, $compressed);
}
const TEST_EMPTY_COMPRESSED_DATA = "425a683417724538509000000000";
public function testEmptyCompress() {
$compression = new BZIPCompression();
$compressed = $compression->compress("", 0);
$this->assertEquals(hex2bin(self::TEST_EMPTY_COMPRESSED_DATA), $compressed);
}
public function testEmptyDecompress() {
$compression = new BZIPCompression();
$compressed = $compression->decompress(
hex2bin(self::TEST_EMPTY_COMPRESSED_DATA), strlen(hex2bin(self::TEST_EMPTY_COMPRESSED_DATA))
);
$this->assertEquals("", $compressed);
}
const TEST_INVALID_COMPRESSED_DATA = "425a6417724538509000000000";
public function testInvalidDecompress() {
$this->setExpectedException(InvalidInputDataException::class);
$compression = new BZIPCompression();
$compression->decompress(
hex2bin(self::TEST_INVALID_COMPRESSED_DATA), strlen(hex2bin(self::TEST_INVALID_COMPRESSED_DATA))
);
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Compression;
use Rogiel\MPQ\Compression\DeflateCompression;
use Rogiel\MPQ\Exception\Compression\InvalidInputDataException;
use Rogiel\MPQ\Tests\AbstractTestCase;
class DeflateCompressionTest extends AbstractTestCase {
const TEST_COMPRESSED_DATA = "f348cdc9c95708cf2fca490100";
const TEST_UNCOMPRESSED_DATA = "Hello World";
public function testCompress() {
$compression = new DeflateCompression();
$compressed = $compression->compress(
self::TEST_UNCOMPRESSED_DATA, strlen(self::TEST_UNCOMPRESSED_DATA)
);
$this->assertEquals(hex2bin(self::TEST_COMPRESSED_DATA), $compressed);
}
public function testDecompress() {
$compression = new DeflateCompression();
$compressed = $compression->decompress(
hex2bin(self::TEST_COMPRESSED_DATA), strlen(hex2bin(self::TEST_COMPRESSED_DATA))
);
$this->assertEquals(self::TEST_UNCOMPRESSED_DATA, $compressed);
}
const TEST_EMPTY_COMPRESSED_DATA = "0300";
public function testEmptyCompress() {
$compression = new DeflateCompression();
$compressed = $compression->compress("", 0);
$this->assertEquals(hex2bin(self::TEST_EMPTY_COMPRESSED_DATA), $compressed);
}
public function testEmptyDecompress() {
$compression = new DeflateCompression();
$compressed = $compression->decompress(
hex2bin(self::TEST_EMPTY_COMPRESSED_DATA), strlen(hex2bin(self::TEST_EMPTY_COMPRESSED_DATA))
);
$this->assertEquals("", $compressed);
}
const TEST_INVALID_COMPRESSED_DATA = NULL;
public function testInvalidDecompress() {
$this->setExpectedException(InvalidInputDataException::class);
$compression = new DeflateCompression();
$compression->decompress(
hex2bin(self::TEST_INVALID_COMPRESSED_DATA), strlen(hex2bin(self::TEST_INVALID_COMPRESSED_DATA))
);
}
}

View File

@@ -0,0 +1,122 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Encryption;
use Rogiel\MPQ\Encryption\DefaultEncryption;
use Rogiel\MPQ\Exception\Encryption\InvalidBlockSizeException;
use Rogiel\MPQ\Exception\Encryption\InvalidKeyException;
use Rogiel\MPQ\Tests\AbstractTestCase;
class DefaultEncryptionTest extends AbstractTestCase {
const TEST_KEY = "c89a7de0c5fea9caf3f0";
const TEST_SINGLE_BLOCK_PLAIN_TEXT_STRING = "3ef8cc61";
const TEST_SINGLE_BLOCK_CIPHER_TEXT_STRING = "b86de569";
public function testSingleBlockEncrypt() {
$encryption = new DefaultEncryption(hex2bin(self::TEST_KEY));
$cipher = $encryption->encrypt(
hex2bin(self::TEST_SINGLE_BLOCK_PLAIN_TEXT_STRING), strlen(hex2bin(self::TEST_SINGLE_BLOCK_PLAIN_TEXT_STRING))
);
$this->assertEquals(hex2bin(self::TEST_SINGLE_BLOCK_CIPHER_TEXT_STRING), $cipher);
}
public function testSingleBlockDencrypt() {
$encryption = new DefaultEncryption(hex2bin(self::TEST_KEY));
$plain = $encryption->decrypt(
hex2bin(self::TEST_SINGLE_BLOCK_CIPHER_TEXT_STRING), strlen(hex2bin(self::TEST_SINGLE_BLOCK_CIPHER_TEXT_STRING))
);
$this->assertEquals(hex2bin(self::TEST_SINGLE_BLOCK_PLAIN_TEXT_STRING), $plain);
}
const TEST_MULTIPLE_BLOCKS_PLAIN_TEXT_STRING = "87d1d07454135b170e89045025ed1e8309ee2f14aaaab586d9e81505a8ba72c5";
const TEST_MULTIPLE_BLOCKS_CIPHER_TEXT_STRING = "0144f97cc3492b88aed652bb914e8748c58871e1aea753ff04fe731f7cce9a38";
public function testMultipleBlocksEncrypt() {
$encryption = new DefaultEncryption(hex2bin(self::TEST_KEY));
$cipher = $encryption->encrypt(
hex2bin(self::TEST_MULTIPLE_BLOCKS_PLAIN_TEXT_STRING), strlen(hex2bin(self::TEST_MULTIPLE_BLOCKS_PLAIN_TEXT_STRING))
);
$this->assertEquals(hex2bin(self::TEST_MULTIPLE_BLOCKS_CIPHER_TEXT_STRING), $cipher);
}
public function testMultipleBlocksDencrypt() {
$encryption = new DefaultEncryption(hex2bin(self::TEST_KEY));
$plain = $encryption->decrypt(
hex2bin(self::TEST_MULTIPLE_BLOCKS_CIPHER_TEXT_STRING), strlen(hex2bin(self::TEST_MULTIPLE_BLOCKS_CIPHER_TEXT_STRING))
);
$this->assertEquals(hex2bin(self::TEST_MULTIPLE_BLOCKS_PLAIN_TEXT_STRING), $plain);
}
// -----------------------------------------------------------------------------------------------------------------
public function testBlocklSize() {
$encryption = new DefaultEncryption(hex2bin(self::TEST_KEY));
$this->assertEquals(4, $encryption->getBlockSize());
}
// -----------------------------------------------------------------------------------------------------------------
const TEST_SMALL_KEY = "c89a7de0c5fea9ca";
public function testSmallKey() {
$this->setExpectedException(InvalidKeyException::class);
new DefaultEncryption(hex2bin(self::TEST_SMALL_KEY));
}
const TEST_LARGE_KEY = "c89a7de0c5fea9cac89a7de0c5fea9ca";
public function testLargeKey() {
$this->setExpectedException(InvalidKeyException::class);
new DefaultEncryption(hex2bin(self::TEST_LARGE_KEY));
}
const TEST_INVALID_BLOCK_SIZE_PLAIN_TEXT_STRING = "3ef8cc";
const TEST_INVALID_BLOCK_SIZE_CIPHER_TEXT_STRING = "b86de5";
public function testInvalidBlockSizeEncrypt() {
$this->setExpectedException(InvalidBlockSizeException::class);
$encryption = new DefaultEncryption(hex2bin(self::TEST_KEY));
$plain = $encryption->encrypt(
hex2bin(self::TEST_INVALID_BLOCK_SIZE_PLAIN_TEXT_STRING), strlen(hex2bin(self::TEST_INVALID_BLOCK_SIZE_PLAIN_TEXT_STRING))
);
}
public function testInvalidBlockSizeDencrypt() {
$this->setExpectedException(InvalidBlockSizeException::class);
$encryption = new DefaultEncryption(hex2bin(self::TEST_KEY));
$plain = $encryption->decrypt(
hex2bin(self::TEST_INVALID_BLOCK_SIZE_CIPHER_TEXT_STRING), strlen(hex2bin(self::TEST_INVALID_BLOCK_SIZE_CIPHER_TEXT_STRING))
);
}
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Hashing;
use Rogiel\MPQ\Hashing\FileKeyHashing;
use Rogiel\MPQ\Hashing\Hashing;
use Rogiel\MPQ\Tests\AbstractTestCase;
class FileKeyHashingTest extends AbstractTestCase {
/**
* @var Hashing
*/
private $hashing;
public function setUp() {
$this->hashing = new FileKeyHashing();
}
const TEST_PLAIN_TEXT = "b99cb06efcddccdf861494b2b431a592";
const TEST_HASHED_TEXT = 3812245591;
public function testHash() {
$hash = $this->hashing->hash(hex2bin(self::TEST_PLAIN_TEXT));
$this->assertEquals(self::TEST_HASHED_TEXT, $hash);
}
const TEST_EMPTY_TEXT = "";
const TEST_HASHED_EMPTY = 2146271213;
public function testEmptyHash() {
$hash = $this->hashing->hash(hex2bin(self::TEST_EMPTY_TEXT));
$this->assertEquals(self::TEST_HASHED_EMPTY, $hash);
}
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Hashing;
use Rogiel\MPQ\Hashing\NameAHashing;
use Rogiel\MPQ\Hashing\Hashing;
use Rogiel\MPQ\Tests\AbstractTestCase;
class NameAHashingTest extends AbstractTestCase {
/**
* @var Hashing
*/
private $hashing;
public function setUp() {
$this->hashing = new NameAHashing();
}
const TEST_PLAIN_TEXT = "b99cb06efcddccdf861494b2b431a592";
const TEST_HASHED_TEXT = 1258300789;
public function testHash() {
$hash = $this->hashing->hash(hex2bin(self::TEST_PLAIN_TEXT));
$this->assertEquals(self::TEST_HASHED_TEXT, $hash);
}
const TEST_EMPTY_TEXT = "";
const TEST_HASHED_EMPTY = 2146271213;
public function testEmptyHash() {
$hash = $this->hashing->hash(hex2bin(self::TEST_EMPTY_TEXT));
$this->assertEquals(self::TEST_HASHED_EMPTY, $hash);
}
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Hashing;
use Rogiel\MPQ\Hashing\NameBHashing;
use Rogiel\MPQ\Hashing\Hashing;
use Rogiel\MPQ\Tests\AbstractTestCase;
class NameBHashingTest extends AbstractTestCase {
/**
* @var Hashing
*/
private $hashing;
public function setUp() {
$this->hashing = new NameBHashing();
}
const TEST_PLAIN_TEXT = "b99cb06efcddccdf861494b2b431a592";
const TEST_HASHED_TEXT = 2342994917;
public function testHash() {
$hash = $this->hashing->hash(hex2bin(self::TEST_PLAIN_TEXT));
$this->assertEquals(self::TEST_HASHED_TEXT, $hash);
}
const TEST_EMPTY_TEXT = "";
const TEST_HASHED_EMPTY = 2146271213;
public function testEmptyHash() {
$hash = $this->hashing->hash(hex2bin(self::TEST_EMPTY_TEXT));
$this->assertEquals(self::TEST_HASHED_EMPTY, $hash);
}
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Hashing;
use Rogiel\MPQ\Hashing\TableOffsetHashing;
use Rogiel\MPQ\Hashing\Hashing;
use Rogiel\MPQ\Tests\AbstractTestCase;
class TableOffsetHashingTest extends AbstractTestCase {
/**
* @var Hashing
*/
private $hashing;
public function setUp() {
$this->hashing = new TableOffsetHashing();
}
const TEST_PLAIN_TEXT = "b99cb06efcddccdf861494b2b431a592";
const TEST_HASHED_TEXT = 4211952812;
public function testHash() {
$hash = $this->hashing->hash(hex2bin(self::TEST_PLAIN_TEXT));
$this->assertEquals(self::TEST_HASHED_TEXT, $hash);
}
const TEST_EMPTY_TEXT = "";
const TEST_HASHED_EMPTY = 2146271213;
public function testEmptyHash() {
$hash = $this->hashing->hash(hex2bin(self::TEST_EMPTY_TEXT));
$this->assertEquals(self::TEST_HASHED_EMPTY, $hash);
}
}

View File

@@ -0,0 +1,170 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Metadata;
use Rogiel\MPQ\Metadata\Block;
use Rogiel\MPQ\Tests\AbstractTestCase;
class BlockTest extends AbstractTestCase {
const TEST_BLOCK_DATA =
"10200000". /* file position */
"00001000". /* compressed size */
"00001000". /* size */
"00000000"; /* flags */
public function testParse() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_BLOCK_DATA));
$block = Block::parse($parser);
$this->assertEquals(8208, $block->getFilePos());
$this->assertEquals(1048576, $block->getCompressedSize());
$this->assertEquals(1048576, $block->getSize());
$this->assertEquals(0, $block->getFlags());
}
const TEST_IMPLODED_BLOCK_DATA =
"10200000". /* file position */
"00001000". /* compressed size */
"00001000". /* size */
"00010000"; /* flags */
public function testImplodedFlag() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_IMPLODED_BLOCK_DATA));
$block = Block::parse($parser);
$this->assertEquals(true, $block->isImploded());
}
const TEST_COMPRESSED_BLOCK_DATA =
"10200000". /* file position */
"00001000". /* compressed size */
"00001000". /* size */
"00020000"; /* flags */
public function testCompressedFlag() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_COMPRESSED_BLOCK_DATA));
$block = Block::parse($parser);
$this->assertEquals(true, $block->isCompressed());
}
const TEST_ENCRYPTED_BLOCK_DATA =
"10200000". /* file position */
"00001000". /* compressed size */
"00001000". /* size */
"00000100"; /* flags */
public function testEncryptedFlag() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_ENCRYPTED_BLOCK_DATA));
$block = Block::parse($parser);
$this->assertEquals(true, $block->isEncrypted());
}
const TEST_FIX_KEY_BLOCK_DATA =
"10200000". /* file position */
"00001000". /* compressed size */
"00001000". /* size */
"00000200"; /* flags */
public function testKeyBasedOnPositionFlag() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_FIX_KEY_BLOCK_DATA));
$block = Block::parse($parser);
$this->assertEquals(true, $block->isKeyBasedOnPosition());
}
const TEST_PATCHED_BLOCK_DATA =
"10200000". /* file position */
"00001000". /* compressed size */
"00001000". /* size */
"00001000"; /* flags */
public function testPatchedFlag() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_PATCHED_BLOCK_DATA));
$block = Block::parse($parser);
$this->assertEquals(true, $block->isPatched());
}
const TEST_SINGLE_UNIT_BLOCK_DATA =
"10200000". /* file position */
"00001000". /* compressed size */
"00001000". /* size */
"00000001"; /* flags */
public function testSingleUnitFlag() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_SINGLE_UNIT_BLOCK_DATA));
$block = Block::parse($parser);
$this->assertEquals(true, $block->isSingleUnit());
}
const TEST_DELETE_MARKER_BLOCK_DATA =
"10200000". /* file position */
"00001000". /* compressed size */
"00001000". /* size */
"00000002"; /* flags */
public function testDeleteMarkerFlag() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_DELETE_MARKER_BLOCK_DATA));
$block = Block::parse($parser);
$this->assertEquals(true, $block->isDeleted());
}
const TEST_CHECKSUMED_BLOCK_DATA =
"10200000". /* file position */
"00001000". /* compressed size */
"00001000". /* size */
"00000004"; /* flags */
public function testChecksumedFlag() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_CHECKSUMED_BLOCK_DATA));
$block = Block::parse($parser);
$this->assertEquals(true, $block->isChecksumed());
}
const TEST_EXISTS_BLOCK_DATA =
"10200000". /* file position */
"00001000". /* compressed size */
"00001000". /* size */
"00000080"; /* flags */
public function testExistsFlag() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_EXISTS_BLOCK_DATA));
$block = Block::parse($parser);
$this->assertEquals(true, $block->isExisting());
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Metadata;
use Rogiel\MPQ\Metadata\Hash;
use Rogiel\MPQ\Tests\AbstractTestCase;
class HashTest extends AbstractTestCase {
const TEST_HASH_DATA =
"10200000". /* name 1 */
"00001000". /* name 2 */
"00001000". /* locale/platform */
"DEAD0000"; /* block index */
public function testParse() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_HASH_DATA));
$hash = Hash::parse($parser);
$this->assertEquals(8208, $hash->getName1());
$this->assertEquals(1048576, $hash->getName2());
$this->assertEquals(0, $hash->getLocale());
$this->assertEquals(16, $hash->getPlatform());
$this->assertEquals(44510, $hash->getBlockIndex());
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Metadata;
use Rogiel\MPQ\Metadata\UserData;
use Rogiel\MPQ\Tests\AbstractTestCase;
class UserDataTest extends AbstractTestCase {
const TEST_USER_RAW_DATA = "67685b0ba71a3e0c4783877483b6c41cddae8669eb33b9ada05452e362dadd16739e84f2683ff3ff550608dae02ac3d8cd4b0bbcdfa353146f3d9516f327f490d678fbe06b725ced05e405e6476e70ee3bdb217fd02ec1db0a2a374ef6d9d000a66ad6c53e69d1841a95837f5adcf33214b03c277f5c0e49ce2a0623232061573de9352a43eb1f9c9294940e4e98b272e66095a0ceb11c830779f8cc04a2224c1ec2bb510f74f96a2e212eda9156bd1edb7144d37480bd746098f4b25f117b41ffede36645c0413d6880eeb07b60b6915b3d1dfdcfc9cbc10d3ffcd9f0e8f19127210c20d38308e0ebcd672c6741679a772d17893975b4024b640e513aa5bca1ce3c38c180c9eb416e98d93d68fbda47e884ddc4af60c6194ddc3664b05f8d7bd5f4cb0a4ba4aaf213c4c4ddbe978c901bfdc6a8ec0533693b5961dc398a48629b299d9a6e5fc6e33e7d5e41e5b38e80f7c559496d70fb5759a77c0b8a2580976cb7a9578c232b891cc1e14623b37a5274cf3474f7350717522279487483956d3ec9dc73aea5656ba44ca243c913fab9feb5085f41a5ad08d31371d230eb6a6334490263c248c6207e4624dac5ff2777e25686446161024539826f54be01579dbea2877f25165f33b78803915f733bbcda0364f8b1a4ac04ba3556286ca22c946c2aacaf2810fd909b8d4be4fb9133c8b55fb742";
const TEST_USER_DATA =
"00020000". /* size */
"00040000". /* header offset */
"01000000". /* user data header */
self::TEST_USER_RAW_DATA; /* data */
public function testParse() {
$parser = $this->createMemoryParser(hex2bin(self::TEST_USER_DATA));
$userData = UserData::parse($parser);
$this->assertEquals(512, $userData->getSize());
$this->assertEquals(1024, $userData->getHeaderOffset());
$this->assertEquals(1, $userData->getHeader());
$this->assertEquals(hex2bin(self::TEST_USER_RAW_DATA), $userData->getRawContent());
}
}

View File

@@ -0,0 +1,82 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\MPQ\Tests\Stream\Parser;
use Rogiel\MPQ\Tests\AbstractTestCase;
class BinaryStreamParserTest extends AbstractTestCase {
public function testReadByte() {
$parser = $this->createMemoryParser(
hex2bin("00FF")
);
$this->assertEquals(0x00, $parser->readByte());
$this->assertEquals(0xFF, $parser->readByte());
}
public function testReadBytes() {
$parser = $this->createMemoryParser(
hex2bin("00FF")
);
$this->assertEquals(hex2bin("00FF"), $parser->readBytes(2));
}
public function testReadUInt32() {
$parser = $this->createMemoryParser(
hex2bin("0000BEEF")
);
$this->assertEquals(4022206464, $parser->readUInt32());
}
public function testReadUInt16() {
$parser = $this->createMemoryParser(
hex2bin("0E0A")
);
$this->assertEquals(2574, $parser->readUInt16());
}
public function testSkip() {
$parser = $this->createMemoryParser(
hex2bin("DEADBEEF")
);
$parser->skip(2);
$this->assertEquals(0xBE, $parser->readByte());
}
public function testSeek() {
$parser = $this->createMemoryParser(
hex2bin("DEADBEEF")
);
$parser->seek(3);
$this->assertEquals(0xEF, $parser->readByte());
}
}