Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Buffer as Parser argument #71

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"timeout": 10,
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "infection-log.txt"
}
}
86 changes: 9 additions & 77 deletions src/Buffertools/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Parser
/**
* @var string
*/
private $string;
private $string = '';

/**
* @var int
Expand All @@ -26,25 +26,15 @@ 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 = '';
if ($input instanceof BufferInterface) {
$this->string = $input->getBinary();
$this->size = $input->getSize();
assert(strlen($this->string) === $this->size);
}

if (is_string($input)) {
$bin = Buffer::hex($input, null)->getBinary();
} elseif ($input instanceof BufferInterface) {
$bin = $input->getBinary();
} else {
throw new \InvalidArgumentException("Invalid argument to Parser");
}

$this->string = $bin;
$this->position = 0;
$this->size = strlen($this->string);
}

/**
Expand Down Expand Up @@ -96,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
Expand Down Expand Up @@ -195,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[]');
}
Expand All @@ -214,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);
}
}
2 changes: 1 addition & 1 deletion src/Buffertools/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
4 changes: 2 additions & 2 deletions src/Buffertools/Types/ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function writeBits(BufferInterface $string): string
}

/**
* @param Buffer $string
* @param BufferInterface $string
* @return string
* @throws \Exception
*/
Expand Down Expand Up @@ -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
{
Expand Down
54 changes: 32 additions & 22 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ 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());
}

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()
Expand All @@ -39,58 +41,67 @@ 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');
$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()
{
$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->appendBuffer($bytes, true);

$this->assertEquals('44434241', $parser->getBuffer()->getHex());
}

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());
}

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());
}

/**
Expand All @@ -103,16 +114,15 @@ 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
* @expectedExceptionMessage Could not parse string of required length (empty)
*/
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');
Expand All @@ -125,7 +135,7 @@ public function testReadBytesEndOfString()
*/
public function testReadBytesBeyondLength()
{
$bytes = '41424344';
$bytes = Buffer::hex('41424344');
$parser = new Parser($bytes);
$parser->readBytes(5);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
52 changes: 52 additions & 0 deletions tests/Types/IntegerBitSizeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace BitWasp\Buffertools\Tests\Types;

use BitWasp\Buffertools\Tests\BinaryTest;
use BitWasp\Buffertools\Types\Int128;
use BitWasp\Buffertools\Types\Int16;
use BitWasp\Buffertools\Types\Int256;
use BitWasp\Buffertools\Types\Int32;
use BitWasp\Buffertools\Types\Int64;
use BitWasp\Buffertools\Types\Int8;
use BitWasp\Buffertools\Types\SignedIntInterface;
use BitWasp\Buffertools\Types\Uint128;
use BitWasp\Buffertools\Types\Uint16;
use BitWasp\Buffertools\Types\Uint256;
use BitWasp\Buffertools\Types\Uint32;
use BitWasp\Buffertools\Types\Uint64;
use BitWasp\Buffertools\Types\Uint8;
use BitWasp\Buffertools\Types\UintInterface;

class IntegerBitSizeTest extends BinaryTest
{
public function getClassAndBitSize()
{
return [
[Uint8::class,8],
[Uint16::class,16],
[Uint32::class,32],
[Uint64::class,64],
[Uint128::class,128],
[Uint256::class,256],
[Int8::class,8],
[Int16::class,16],
[Int32::class,32],
[Int64::class,64],
[Int128::class,128],
[Int256::class,256],
];
}

/**
* @dataProvider getClassAndBitSize
* @param string $integerClass
* @param int $bitSize
*/
public function testBitSize(string $integerClass, int $bitSize)
{
/** @var UintInterface|SignedIntInterface $integer */
$integer = new $integerClass();
$this->assertEquals($bitSize, $integer->getBitSize());
}
}