123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- /**
- * @copyright Nichlas Severinsen
- * @license https://opensource.org/licenses/BSD-2-Clause
- */
- namespace Xxh3;
- use Necklace\XxHash\Xxh3\XxHash128;
- use PHPUnit\Framework\TestCase;
- use Exception;
- final class XxHash128Test extends TestCase
- {
- private $x;
- protected function setUp(): void
- {
- try {
- $this->x = new XxHash128;
- } 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('2e790d069a07b05183ff80eedd51d542', $this->x->hash('test'));
- }
- public function testHashWithSeed(): void
- {
- $this->assertEquals('2409be6c8b4ea1577c601857728eb3f6', $this->x->hash('test', 1));
- $this->assertEquals('7a4aef0e7f4f099ff4673c78b49c1c7a', $this->x->hash('test', 10));
- $this->assertEquals('0668c111a593fea43a81538f866b929a', $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('d08f5a62eab1c1f7a123f947d57f73e7', $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('2e790d069a07b05183ff80eedd51d542', $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('2409be6c8b4ea1577c601857728eb3f6', $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('7a4aef0e7f4f099ff4673c78b49c1c7a', $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('0668c111a593fea43a81538f866b929a', $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('d08f5a62eab1c1f7a123f947d57f73e7', $this->x->digest());
- }
- public function testHashFile(): void
- {
- if($this->x->version() === '0.7.1')
- {
- $this->assertEquals('f755027331532f81e4e3f124d220bf33', $this->x->hashFile(__DIR__ . '/../bootstrap.php'));
- }
- else
- {
- $this->assertEquals('c3ead5206079114dce23b624b8f2199f', $this->x->hashFile(__DIR__ . '/../bootstrap.php'));
- }
- }
- public function testHashFileWithSeed(): void
- {
- if($this->x->version() === '0.7.1')
- {
- $this->assertEquals('45e093f9eedf912cfad4e793fbb576ec', $this->x->hashFile(__DIR__ . '/../bootstrap.php', 1));
- }
- else
- {
- $this->assertEquals('3aaea55fc456c7b07e30e17f0a3ee498', $this->x->hashFile(__DIR__ . '/../bootstrap.php', 1));
- }
- }
- public function testHashFileWithSecret(): void
- {
- $secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';
- if($this->x->version() === '0.7.1')
- {
- $this->assertEquals('38e756e132a79fffcc7323cea1fecc42', $this->x->hashFile(__DIR__ . '/../bootstrap.php', null, $secret));
- }
- else
- {
- $this->assertEquals('2ac5da808cdb875c3c2a9a08b449fa51', $this->x->hashFile(__DIR__ . '/../bootstrap.php', null, $secret));
- }
- }
- }
|