123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- /**
- * @copyright Nichlas Severinsen
- * @license https://opensource.org/licenses/BSD-2-Clause
- */
- namespace Xxh3;
- use Necklace\XxHash\Xxh3\XxHash64;
- use PHPUnit\Framework\TestCase;
- use Necklace\XxHash\XxHash;
- use Exception;
- final class XxHash64Test extends TestCase
- {
- private $x;
- protected function setUp(): void
- {
- try {
- $this->x = new XxHash64;
- } catch (Exception $e) {
- if($e->getMessage() === 'Too old version of libxxhash. XXH3 requires v0.7.1 or above.')
- {
- $this->markTestSkipped('Not applicable for version.');
- }
- }
- }
- protected function teardown(): void
- {
- unset($this->x);
- }
- public function testHash(): void
- {
- $this->assertEquals('83ff80eedd51d542', $this->x->hash('test'));
- }
- public function testHashWithSeed(): void
- {
- $this->assertEquals('7c601857728eb3f6', $this->x->hash('test', 1));
- $this->assertEquals('f4673c78b49c1c7a', $this->x->hash('test', 10));
- $this->assertEquals('3a81538f866b929a', $this->x->hash('test', 100));
- }
- public function testHashWithBadSecret(): void
- {
- $this->expectExceptionMessage('XxHash XXH3 secret is too small.');
- $this->x->hash('test', null, 'not long enough secret');
- }
- public function testHashWithSecret(): void
- {
- $secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';
- $this->assertEquals('a123f947d57f73e7', $this->x->hash('test', null, $secret));
- }
- public function testReset(): void
- {
- $this->assertEquals(true, $this->x->reset());
- }
- public function testUpdateAndDigest(): void
- {
- $this->assertEquals(true, $this->x->update('te'));
- $this->assertEquals(true, $this->x->update('st'));
- $this->assertEquals('83ff80eedd51d542', $this->x->digest());
- }
- public function testResetWithSeed(): void
- {
- $this->assertEquals(true, $this->x->reset(1));
- $this->assertEquals(true, $this->x->update('te'));
- $this->assertEquals(true, $this->x->update('st'));
- $this->assertEquals('7c601857728eb3f6', $this->x->digest());
- $this->assertEquals(true, $this->x->reset(10));
- $this->assertEquals(true, $this->x->update('te'));
- $this->assertEquals(true, $this->x->update('st'));
- $this->assertEquals('f4673c78b49c1c7a', $this->x->digest());
- $this->assertEquals(true, $this->x->reset(100));
- $this->assertEquals(true, $this->x->update('te'));
- $this->assertEquals(true, $this->x->update('st'));
- $this->assertEquals('3a81538f866b929a', $this->x->digest());
- }
- public function testResetWithBadSecret(): void
- {
- $this->expectExceptionMessage('XxHash XXH3 secret is too small.');
- $this->x->reset(null, 'not long enough secret');
- }
- public function testResetWithSecret(): void
- {
- $secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';
- $this->assertEquals(true, $this->x->reset(null, $secret));
- $this->assertEquals(true, $this->x->update('te'));
- $this->assertEquals(true, $this->x->update('st'));
- $this->assertEquals('a123f947d57f73e7', $this->x->digest());
- }
- public function testHashFile(): void
- {
- $this->assertEquals('db70939c6397e562', $this->x->hashFile(__DIR__ . '/../bootstrap.php'));
- }
- public function testHashFileWithSeed(): void
- {
- $this->assertEquals('c9c52648e7f4a15f', $this->x->hashFile(__DIR__ . '/../bootstrap.php', 1));
- }
- public function testHashFileWithSecret(): void
- {
- $secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';
- $this->assertEquals('0983cba538bf950b', $this->x->hashFile(__DIR__ . '/../bootstrap.php', null, $secret));
- }
- }
|