From 01f393bdf172ec22abc5a2bb08907aa63a808310 Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Thu, 28 Dec 2017 17:27:58 +0100 Subject: [PATCH 1/8] Require Buffer for Parser constructor --- src/Buffertools/Parser.php | 22 ++++++++-------------- tests/ParserTest.php | 33 +++++++++++++++------------------ tests/TemplateTest.php | 2 +- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/Buffertools/Parser.php b/src/Buffertools/Parser.php index 1565bbe..5ca477c 100644 --- a/src/Buffertools/Parser.php +++ b/src/Buffertools/Parser.php @@ -26,25 +26,19 @@ class Parser /** * Instantiate class, optionally taking Buffer or HEX. * - * @param null|string|BufferInterface $input + * @param BufferInterface $input */ - public function __construct($input = null) + public function __construct(BufferInterface $input = null) { - if (null === $input) { - $input = ''; - } + $this->position = 0; - if (is_string($input)) { - $bin = Buffer::hex($input, null)->getBinary(); - } elseif ($input instanceof BufferInterface) { - $bin = $input->getBinary(); + if (null === $input) { + $this->string = ''; + $this->size = 0; } else { - throw new \InvalidArgumentException("Invalid argument to Parser"); + $this->string = $input->getBinary(); + $this->size = $input->getSize(); } - - $this->string = $bin; - $this->position = 0; - $this->size = strlen($this->string); } /** diff --git a/tests/ParserTest.php b/tests/ParserTest.php index e2dbc36..46757b0 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -39,20 +39,20 @@ public function testGetBufferEmptyNull() public function testWriteBytes() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); + $parser = new Parser(); - $parser->writeBytes(4, Buffer::hex($bytes)); - $returned = $parser->getBuffer()->getHex(); - $this->assertSame($returned, '41424344'); + $parser->writeBytes(4, $bytes); + $this->assertTrue($parser->getBuffer()->equals($bytes)); } public function testWriteBytesFlip() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser(); - $parser->writeBytes(4, Buffer::hex($bytes), true); - $returned = $parser->getBuffer()->getHex(); - $this->assertSame($returned, '44434241'); + $parser->writeBytes(4, $bytes, true); + + $this->assertEquals('44434241', $parser->getBuffer()->getHex()); } public function testWriteBytesPadded() @@ -71,26 +71,24 @@ public function testWriteBytesFlipPadded() public function testReadBytes() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser($bytes); $read = $parser->readBytes(4); $this->assertInstanceOf(Buffer::class, $read); - $hex = $read->getHex(); - $this->assertSame($bytes, $hex); + $this->assertTrue($read->equals($bytes)); } public function testReadBytesFlip() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser($bytes); $read = $parser->readBytes(4, true); $this->assertInstanceOf(Buffer::class, $read); - $hex = $read->getHex(); - $this->assertSame('44434241', $hex); + $this->assertEquals($bytes->flip()->getHex(), $read->getHex()); } /** @@ -103,8 +101,7 @@ public function testReadBytesEmpty() // and length is zero. $parser = new Parser(); - $data = $parser->readBytes(0); - $this->assertFalse(!!$data); + $parser->readBytes(0); } /** * @expectedException \BitWasp\Buffertools\Exceptions\ParserOutOfRange @@ -112,7 +109,7 @@ public function testReadBytesEmpty() */ public function testReadBytesEndOfString() { - $parser = new Parser('4041414142414141'); + $parser = new Parser(Buffer::hex('4041414142414141')); $bytes1 = $parser->readBytes(4); $bytes2 = $parser->readBytes(4); $this->assertSame($bytes1->getHex(), '40414141'); @@ -125,7 +122,7 @@ public function testReadBytesEndOfString() */ public function testReadBytesBeyondLength() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser($bytes); $parser->readBytes(5); } diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index 84f8c19..f42b409 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -29,7 +29,7 @@ public function testTemplate() public function testTemplateEmptyParse() { $template = new Template(); - $parser = new Parser('010203040a0b0c0d'); + $parser = new Parser(Buffer::hex('010203040a0b0c0d')); $template->parse($parser); } From 2c6f5ea3ad1e8c19b4043e426f3a4c0a474732c8 Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Thu, 28 Dec 2017 18:13:44 +0100 Subject: [PATCH 2/8] ByteString - fix phpdoc --- src/Buffertools/Types/ByteString.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Buffertools/Types/ByteString.php b/src/Buffertools/Types/ByteString.php index 2df9cc5..b273867 100644 --- a/src/Buffertools/Types/ByteString.php +++ b/src/Buffertools/Types/ByteString.php @@ -43,7 +43,7 @@ public function writeBits(BufferInterface $string): string } /** - * @param Buffer $string + * @param BufferInterface $string * @return string * @throws \Exception */ @@ -84,7 +84,7 @@ public function readBits(BufferInterface $buffer): string /** * @param Parser $parser * @return BufferInterface - * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange + * @throws \Exception */ public function read(Parser $parser): BufferInterface { From 89923ed3452c8d8dccfbd6dec531ed75e30861e0 Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Sat, 30 Dec 2017 00:23:53 +0000 Subject: [PATCH 3/8] use instanceof in Parser constructor --- src/Buffertools/Parser.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Buffertools/Parser.php b/src/Buffertools/Parser.php index 5ca477c..d66db2b 100644 --- a/src/Buffertools/Parser.php +++ b/src/Buffertools/Parser.php @@ -11,7 +11,7 @@ class Parser /** * @var string */ - private $string; + private $string = ''; /** * @var int @@ -30,12 +30,7 @@ class Parser */ public function __construct(BufferInterface $input = null) { - $this->position = 0; - - if (null === $input) { - $this->string = ''; - $this->size = 0; - } else { + if ($input instanceof BufferInterface) { $this->string = $input->getBinary(); $this->size = $input->getSize(); } From 39502af8f17a36f9cb66fedf5053d77e9bde5c5b Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Sat, 30 Dec 2017 00:45:05 +0000 Subject: [PATCH 4/8] Remove unnecessary functions from Parser --- src/Buffertools/Parser.php | 63 ++------------------------------------ tests/ParserTest.php | 8 ++--- 2 files changed, 7 insertions(+), 64 deletions(-) diff --git a/src/Buffertools/Parser.php b/src/Buffertools/Parser.php index d66db2b..ee0998e 100644 --- a/src/Buffertools/Parser.php +++ b/src/Buffertools/Parser.php @@ -33,6 +33,7 @@ public function __construct(BufferInterface $input = null) if ($input instanceof BufferInterface) { $this->string = $input->getBinary(); $this->size = $input->getSize(); + assert(strlen($this->string) === $this->size); } } @@ -85,64 +86,6 @@ public function readBytes(int $numBytes, bool $flipBytes = false): BufferInterfa return new Buffer($string, $length); } - /** - * Write $data as $bytes bytes. Can be flipped if needed. - * - * @param integer $numBytes - number of bytes to write - * @param SerializableInterface|BufferInterface|string $data - buffer, serializable or hex - * @param bool $flipBytes - * @return Parser - */ - public function writeBytes(int $numBytes, $data, bool $flipBytes = false): Parser - { - // Treat $data to ensure it's a buffer, with the correct size - if ($data instanceof SerializableInterface) { - $data = $data->getBuffer(); - } - - if (is_string($data)) { - // Convert to a buffer - $data = Buffer::hex($data, $numBytes); - } else if (!($data instanceof BufferInterface)) { - throw new \RuntimeException('Invalid data passed to Parser::writeBytes'); - } - - $this->writeBuffer($numBytes, $data, $flipBytes); - - return $this; - } - - /** - * Write $data as $bytes bytes. Can be flipped if needed. - * - * @param integer $numBytes - * @param string $data - * @param bool $flipBytes - * @return Parser - */ - public function writeRawBinary(int $numBytes, string $data, bool $flipBytes = false): Parser - { - return $this->writeBuffer($numBytes, new Buffer($data, $numBytes), $flipBytes); - } - - /** - * @param BufferInterface $buffer - * @param bool $flipBytes - * @param int $numBytes - * @return Parser - */ - public function writeBuffer(int $numBytes, BufferInterface $buffer, bool $flipBytes = false): Parser - { - // only create a new buffer if the size does not match - if ($buffer->getSize() != $numBytes) { - $buffer = new Buffer($buffer->getBinary(), $numBytes); - } - - $this->appendBuffer($buffer, $flipBytes); - - return $this; - } - /** * @param BufferInterface $buffer * @param bool $flipBytes @@ -184,7 +127,7 @@ public function writeArray(array $serializable): Parser } if ($object instanceof BufferInterface) { - $parser->writeBytes($object->getSize(), $object); + $parser->appendBinary($object->getBinary()); } else { throw new \RuntimeException('Input to writeArray must be Buffer[], or SerializableInterface[]'); } @@ -203,6 +146,6 @@ public function writeArray(array $serializable): Parser */ public function getBuffer(): BufferInterface { - return new Buffer($this->string, null); + return new Buffer($this->string, $this->size); } } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 46757b0..0376606 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -42,7 +42,7 @@ public function testWriteBytes() $bytes = Buffer::hex('41424344'); $parser = new Parser(); - $parser->writeBytes(4, $bytes); + $parser->appendBuffer($bytes); $this->assertTrue($parser->getBuffer()->equals($bytes)); } @@ -50,7 +50,7 @@ public function testWriteBytesFlip() { $bytes = Buffer::hex('41424344'); $parser = new Parser(); - $parser->writeBytes(4, $bytes, true); + $parser->appendBuffer($bytes, true); $this->assertEquals('44434241', $parser->getBuffer()->getHex()); } @@ -58,14 +58,14 @@ public function testWriteBytesFlip() public function testWriteBytesPadded() { $parser = new Parser(); - $parser->writeBytes(4, Buffer::hex('34')); + $parser->appendBuffer(Buffer::hex('34')); $this->assertEquals("00000034", $parser->getBuffer()->getHex()); } public function testWriteBytesFlipPadded() { $parser = new Parser(); - $parser->writeBytes(4, Buffer::hex('34'), true); + $parser->appendBuffer(Buffer::hex('34', 4), true); $this->assertEquals("34000000", $parser->getBuffer()->getHex()); } From a6c2890c7fcd8e1fcdff10248f0099088b06edac Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Sat, 30 Dec 2017 01:01:35 +0000 Subject: [PATCH 5/8] Unit tests covering integer bit size values --- tests/Types/IntegerBitSizeTest.php | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/Types/IntegerBitSizeTest.php diff --git a/tests/Types/IntegerBitSizeTest.php b/tests/Types/IntegerBitSizeTest.php new file mode 100644 index 0000000..890b31a --- /dev/null +++ b/tests/Types/IntegerBitSizeTest.php @@ -0,0 +1,52 @@ +assertEquals($bitSize, $integer->getBitSize()); + } +} From 7c46c968d1ff88ba9f30f4c177ca6aca4b1ed50f Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Sat, 30 Dec 2017 01:10:14 +0000 Subject: [PATCH 6/8] code style fix --- src/Buffertools/Template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Buffertools/Template.php b/src/Buffertools/Template.php index bc57afa..4df2d97 100644 --- a/src/Buffertools/Template.php +++ b/src/Buffertools/Template.php @@ -63,7 +63,7 @@ public function addItem(TypeInterface $item): Template */ public function parse(Parser $parser): array { - if (0 == count($this->template)) { + if (empty($this->template)) { throw new \RuntimeException('No items in template'); } From 07cffebcee374b65893457a8e96f855b18535f1d Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Sat, 30 Dec 2017 01:14:09 +0000 Subject: [PATCH 7/8] Parser tests --- tests/ParserTest.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 0376606..4a2ac8d 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -16,6 +16,7 @@ public function testParserEmpty() $this->assertInstanceOf(Parser::class, $parser); $this->assertSame(0, $parser->getPosition()); + $this->assertSame(0, $parser->getSize()); $this->assertInstanceOf(Buffer::class, $parser->getBuffer()); $this->assertEmpty($parser->getBuffer()->getHex()); } @@ -23,9 +24,10 @@ public function testParserEmpty() public function testGetBuffer() { $buffer = Buffer::hex('41414141'); - $parser = new Parser($buffer); - $this->assertSame($parser->getBuffer()->getBinary(), $buffer->getBinary()); + $this->assertSame(0, $parser->getPosition()); + $this->assertSame($buffer->getBinary(), $parser->getBuffer()->getBinary()); + $this->assertEquals($buffer->getSize(), $parser->getSize()); } public function testGetBufferEmptyNull() @@ -42,8 +44,19 @@ public function testWriteBytes() $bytes = Buffer::hex('41424344'); $parser = new Parser(); + $this->assertEquals(0, $parser->getSize()); + $this->assertEquals(0, $parser->getPosition()); $parser->appendBuffer($bytes); + $this->assertEquals(4, $parser->getSize()); + $this->assertEquals(0, $parser->getPosition()); + $this->assertTrue($parser->getBuffer()->equals($bytes)); + + $bytesAgain = $parser->readBytes(4); + + $this->assertEquals(4, $parser->getSize()); + $this->assertEquals(4, $parser->getPosition()); + $this->assertTrue($bytesAgain->equals($bytes)); } public function testWriteBytesFlip() From 8a9fc091d4af6dffe0a1d00d926c6e24558b22cb Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Sat, 30 Dec 2017 01:31:50 +0000 Subject: [PATCH 8/8] run mutation testing in CI --- .travis.yml | 12 ++++++------ infection.json.dist | 11 +++++++++++ tests/Types/IntegerBitSizeTest.php | 4 ++-- 3 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 infection.json.dist diff --git a/.travis.yml b/.travis.yml index 6f88ce5..a24d15c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,21 +8,21 @@ php: dist: trusty -before_install: - - composer selfupdate - install: - composer install --prefer-source --dev + - wget https://github.com/infection/infection/releases/download/0.7.0/infection.phar + - wget https://github.com/infection/infection/releases/download/0.7.0/infection.phar.pubkey + - chmod +x infection.phar script: - php vendor/bin/phpunit - - vendor/bin/phpstan analyze -l 4 src tests examples - php vendor/bin/phpcs -n --standard=PSR1,PSR2 --report=full src/ tests/ examples/ + - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php infection.phar --min-msi=75 --min-covered-msi=75 --threads=4; fi + - php vendor/bin/phpstan analyze -l 4 src tests examples - ./validate_examples.sh after_success: - - wget https://scrutinizer-ci.com/ocular.phar - - sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover build/docs/clover.xml; fi;' + - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover build/docs/clover.xml; fi matrix: fast_finish: true diff --git a/infection.json.dist b/infection.json.dist new file mode 100644 index 0000000..b883a21 --- /dev/null +++ b/infection.json.dist @@ -0,0 +1,11 @@ +{ + "timeout": 10, + "source": { + "directories": [ + "src" + ] + }, + "logs": { + "text": "infection-log.txt" + } +} \ No newline at end of file diff --git a/tests/Types/IntegerBitSizeTest.php b/tests/Types/IntegerBitSizeTest.php index 890b31a..b1e8b7f 100644 --- a/tests/Types/IntegerBitSizeTest.php +++ b/tests/Types/IntegerBitSizeTest.php @@ -2,7 +2,6 @@ namespace BitWasp\Buffertools\Tests\Types; - use BitWasp\Buffertools\Tests\BinaryTest; use BitWasp\Buffertools\Types\Int128; use BitWasp\Buffertools\Types\Int16; @@ -44,7 +43,8 @@ public function getClassAndBitSize() * @param string $integerClass * @param int $bitSize */ - public function testBitSize(string $integerClass, int $bitSize) { + public function testBitSize(string $integerClass, int $bitSize) + { /** @var UintInterface|SignedIntInterface $integer */ $integer = new $integerClass(); $this->assertEquals($bitSize, $integer->getBitSize());