Skip to content

Commit

Permalink
Tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Sep 15, 2021
1 parent 6f5a137 commit e3b79e9
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 65 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor/
/composer.lock
/*cache
/build
/build
/infection-log
18 changes: 16 additions & 2 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
]
},
"logs": {
"text": "infection-log.txt"
"text": "infection.log"
},
"mutators": {
"@default": true,
"global-ignoreSourceCodeByRegex": [
"\\$this->logger.*",
"\\$this->cache->save.*",
"parent::build(\\$container);"
],
"MBString": {
"settings": {
"mb_substr": false,
"mb_strlen": false
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/ByteStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

final class ByteStringObject extends AbstractCBORObject
{
private const MAJOR_TYPE = 0b010;
private const MAJOR_TYPE = self::MAJOR_TYPE_BYTE_STRING;

/**
* @var string
Expand Down
19 changes: 19 additions & 0 deletions src/CBORObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@

interface CBORObject
{
public const MAJOR_TYPE_UNSIGNED_INTEGER = 0b000;
public const MAJOR_TYPE_NEGATIVE_INTEGER = 0b001;
public const MAJOR_TYPE_BYTE_STRING = 0b010;
public const MAJOR_TYPE_TEXT_STRING = 0b011;
public const MAJOR_TYPE_LIST = 0b100;
public const MAJOR_TYPE_MAP = 0b101;
public const MAJOR_TYPE_TAG = 0b110;
public const MAJOR_TYPE_OTHER_TYPE = 0b111;

public const LENGTH_1_BYTE = 0b00011000;
public const LENGTH_2_BYTES = 0b00011001;
public const LENGTH_4_BYTES = 0b00011010;
public const LENGTH_8_BYTES = 0b00011011;
public const LENGTH_INDEFINITE = 0b00011111;

public const FUTURE_USE_1 = 0b00011100;
public const FUTURE_USE_2 = 0b00011101;
public const FUTURE_USE_3 = 0b00011110;

public function __toString(): string;

public function getMajorType(): int;
Expand Down
62 changes: 31 additions & 31 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,27 @@ public static function create(TagObjectManager $tagObjectManager, OtherObjectMan

public function decode(Stream $stream): CBORObject
{
return $this->process($stream);
return $this->process($stream, false);
}

private function process(Stream $stream, bool $breakable = false): CBORObject
private function process(Stream $stream, bool $breakable): CBORObject
{
$ib = ord($stream->read(1));
$mt = $ib >> 5;
$ai = $ib & 0b00011111;
$val = null;
switch ($ai) {
case 0b00011000: //24
case 0b00011001: //25
case 0b00011010: //26
case 0b00011011: //27
case CBORObject::LENGTH_1_BYTE: //24
case CBORObject::LENGTH_2_BYTES: //25
case CBORObject::LENGTH_4_BYTES: //26
case CBORObject::LENGTH_8_BYTES: //27
$val = $stream->read(2 ** ($ai & 0b00000111));
break;
case 0b00011100: //28
case 0b00011101: //29
case 0b00011110: //30
throw new InvalidArgumentException(sprintf('Cannot parse the data. Found invalid Additional Information "%s" (%d).', str_pad(decbin($ai), 5, '0', STR_PAD_LEFT), $ai));
case 0b00011111: //31
case CBORObject::FUTURE_USE_1: //28
case CBORObject::FUTURE_USE_2: //29
case CBORObject::FUTURE_USE_3: //30
throw new InvalidArgumentException(sprintf('Cannot parse the data. Found invalid Additional Information "%s" (%d).', str_pad(decbin($ai), 8, '0', STR_PAD_LEFT), $ai));
case CBORObject::LENGTH_INDEFINITE: //31
return $this->processInfinite($stream, $mt, $breakable);
}

Expand All @@ -75,37 +75,37 @@ private function process(Stream $stream, bool $breakable = false): CBORObject
private function processFinite(Stream $stream, int $mt, int $ai, ?string $val): CBORObject
{
switch ($mt) {
case 0b000: //0
case CBORObject::MAJOR_TYPE_UNSIGNED_INTEGER: //0
return UnsignedIntegerObject::createObjectForValue($ai, $val);
case 0b001: //1
case CBORObject::MAJOR_TYPE_NEGATIVE_INTEGER: //1
return NegativeIntegerObject::createObjectForValue($ai, $val);
case 0b010: //2
case CBORObject::MAJOR_TYPE_BYTE_STRING: //2
$length = null === $val ? $ai : Utils::binToInt($val);

return new ByteStringObject($stream->read($length));
case 0b011: //3
case CBORObject::MAJOR_TYPE_TEXT_STRING: //3
$length = null === $val ? $ai : Utils::binToInt($val);

return new TextStringObject($stream->read($length));
case 0b100: //4
case CBORObject::MAJOR_TYPE_LIST: //4
$object = new ListObject();
$nbItems = null === $val ? $ai : Utils::binToInt($val);
for ($i = 0; $i < $nbItems; ++$i) {
$object->add($this->process($stream));
$object->add($this->process($stream, false));
}

return $object;
case 0b101: //5
case CBORObject::MAJOR_TYPE_MAP: //5
$object = new MapObject();
$nbItems = null === $val ? $ai : Utils::binToInt($val);
for ($i = 0; $i < $nbItems; ++$i) {
$object->add($this->process($stream), $this->process($stream));
$object->add($this->process($stream, false), $this->process($stream, false));
}

return $object;
case 0b110: //6
return $this->tagObjectManager->createObjectForValue($ai, $val, $this->process($stream));
case 0b111: //7
case CBORObject::MAJOR_TYPE_TAG: //6
return $this->tagObjectManager->createObjectForValue($ai, $val, $this->process($stream, false));
case CBORObject::MAJOR_TYPE_OTHER_TYPE: //7
return $this->otherTypeManager->createObjectForValue($ai, $val);
default:
throw new RuntimeException(sprintf('Unsupported major type "%s" (%d).', str_pad(decbin($mt), 5, '0', STR_PAD_LEFT), $mt)); // Should never append
Expand All @@ -115,7 +115,7 @@ private function processFinite(Stream $stream, int $mt, int $ai, ?string $val):
private function processInfinite(Stream $stream, int $mt, bool $breakable): CBORObject
{
switch ($mt) {
case 0b010: //2
case CBORObject::MAJOR_TYPE_BYTE_STRING: //2
$object = new IndefiniteLengthByteStringObject();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
if (!$it instanceof ByteStringObject) {
Expand All @@ -125,7 +125,7 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR
}

return $object;
case 0b011: //3
case CBORObject::MAJOR_TYPE_TEXT_STRING: //3
$object = new IndefiniteLengthTextStringObject();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
if (!$it instanceof TextStringObject) {
Expand All @@ -135,29 +135,29 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR
}

return $object;
case 0b100: //4
case CBORObject::MAJOR_TYPE_LIST: //4
$object = new IndefiniteLengthListObject();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
$object->add($it);
}

return $object;
case 0b101: //5
case CBORObject::MAJOR_TYPE_MAP: //5
$object = new IndefiniteLengthMapObject();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
$object->append($it, $this->process($stream));
$object->append($it, $this->process($stream, false));
}

return $object;
case 0b111: //7
case CBORObject::MAJOR_TYPE_OTHER_TYPE: //7
if (!$breakable) {
throw new InvalidArgumentException('Cannot parse the data. No enclosing indefinite.');
}

return new BreakObject();
case 0b000: //0
case 0b001: //1
case 0b110: //6
case CBORObject::MAJOR_TYPE_UNSIGNED_INTEGER: //0
case CBORObject::MAJOR_TYPE_NEGATIVE_INTEGER: //1
case CBORObject::MAJOR_TYPE_TAG: //6
default:
throw new InvalidArgumentException(sprintf('Cannot parse the data. Found infinite length for Major Type "%s" (%d).', str_pad(decbin($mt), 5, '0', STR_PAD_LEFT), $mt));
}
Expand Down
4 changes: 2 additions & 2 deletions src/IndefiniteLengthByteStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
class IndefiniteLengthByteStringObject extends AbstractCBORObject
{
private const MAJOR_TYPE = 0b010;
private const ADDITIONAL_INFORMATION = 0b00011111;
private const MAJOR_TYPE = self::MAJOR_TYPE_BYTE_STRING;
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;

/**
* @var ByteStringObject[]
Expand Down
4 changes: 2 additions & 2 deletions src/IndefiniteLengthListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
*/
class IndefiniteLengthListObject extends AbstractCBORObject implements Countable, IteratorAggregate
{
private const MAJOR_TYPE = 0b100;
private const ADDITIONAL_INFORMATION = 0b00011111;
private const MAJOR_TYPE = self::MAJOR_TYPE_LIST;
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;

/**
* @var CBORObject[]
Expand Down
4 changes: 2 additions & 2 deletions src/IndefiniteLengthMapObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
*/
class IndefiniteLengthMapObject extends AbstractCBORObject implements Countable, IteratorAggregate
{
private const MAJOR_TYPE = 0b101;
private const ADDITIONAL_INFORMATION = 0b00011111;
private const MAJOR_TYPE = self::MAJOR_TYPE_MAP;
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;

/**
* @var MapItem[]
Expand Down
4 changes: 2 additions & 2 deletions src/IndefiniteLengthTextStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
class IndefiniteLengthTextStringObject extends AbstractCBORObject
{
private const MAJOR_TYPE = 0b011;
private const ADDITIONAL_INFORMATION = 0b00011111;
private const MAJOR_TYPE = self::MAJOR_TYPE_TEXT_STRING;
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;

/**
* @var TextStringObject[]
Expand Down
2 changes: 1 addition & 1 deletion src/ListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
class ListObject extends AbstractCBORObject implements Countable, IteratorAggregate
{
private const MAJOR_TYPE = 0b100;
private const MAJOR_TYPE = self::MAJOR_TYPE_LIST;

/**
* @var CBORObject[]
Expand Down
2 changes: 1 addition & 1 deletion src/MapObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
final class MapObject extends AbstractCBORObject implements Countable, IteratorAggregate
{
private const MAJOR_TYPE = 0b101;
private const MAJOR_TYPE = self::MAJOR_TYPE_MAP;

/**
* @var MapItem[]
Expand Down
2 changes: 1 addition & 1 deletion src/NegativeIntegerObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class NegativeIntegerObject extends AbstractCBORObject
{
private const MAJOR_TYPE = 0b001;
private const MAJOR_TYPE = self::MAJOR_TYPE_NEGATIVE_INTEGER;

/**
* @var string|null
Expand Down
2 changes: 1 addition & 1 deletion src/OtherObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

abstract class OtherObject extends AbstractCBORObject
{
private const MAJOR_TYPE = 0b111;
private const MAJOR_TYPE = self::MAJOR_TYPE_OTHER_TYPE;

/**
* @var string|null
Expand Down
20 changes: 17 additions & 3 deletions src/StringStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,24 @@ public function read(int $length): string
if (0 === $length) {
return '';
}
$data = fread($this->resource, $length);
if (false === $data) {
throw new RuntimeException('Unable to read the memory');

$alreadyRead = 0;
$data = '';
while ($alreadyRead < $length) {
$left = $length - $alreadyRead;
$sizeToRead = $left < 1024 ? $left : 1024;
$newData = fread($this->resource, $sizeToRead);
$alreadyRead += $sizeToRead;

if (false === $newData) {
throw new RuntimeException('Unable to read the memory');
}
if (mb_strlen($newData, '8bit') < $sizeToRead) {
throw new InvalidArgumentException(sprintf('Out of range. Expected: %d, read: %d.', $length, mb_strlen($data, '8bit')));
}
$data .= $newData;
}

if (mb_strlen($data, '8bit') !== $length) {
throw new InvalidArgumentException(sprintf('Out of range. Expected: %d, read: %d.', $length, mb_strlen($data, '8bit')));
}
Expand Down
2 changes: 1 addition & 1 deletion src/TagObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

abstract class TagObject extends AbstractCBORObject
{
private const MAJOR_TYPE = 0b110;
private const MAJOR_TYPE = self::MAJOR_TYPE_TAG;

/**
* @var string|null
Expand Down
2 changes: 1 addition & 1 deletion src/TextStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

final class TextStringObject extends AbstractCBORObject
{
private const MAJOR_TYPE = 0b011;
private const MAJOR_TYPE = self::MAJOR_TYPE_TEXT_STRING;

/**
* @var string|null
Expand Down
2 changes: 1 addition & 1 deletion src/UnsignedIntegerObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

final class UnsignedIntegerObject extends AbstractCBORObject
{
private const MAJOR_TYPE = 0b000;
private const MAJOR_TYPE = self::MAJOR_TYPE_UNSIGNED_INTEGER;

/**
* @var string|null
Expand Down
5 changes: 3 additions & 2 deletions tests/Type/ByteStringObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CBOR\Test\Type;

use CBOR\ByteStringObject;
use CBOR\CBORObject;
use CBOR\StringStream;

/**
Expand All @@ -31,7 +32,7 @@ public function aByteStringObjectCanBeCreated(string $string, int $expectedAddit
{
$object = ByteStringObject::create($string);

static::assertEquals(0b010, $object->getMajorType());
static::assertEquals(CBORObject::MAJOR_TYPE_BYTE_STRING, $object->getMajorType());
static::assertEquals($expectedAdditionalInformation, $object->getAdditionalInformation());
static::assertEquals($string, $object->getValue());
static::assertEquals($expectedLength, $object->getLength());
Expand All @@ -44,7 +45,7 @@ public function aByteStringObjectCanBeCreated(string $string, int $expectedAddit
$decoded = $this->getDecoder()->decode($stream);

static::assertInstanceOf(ByteStringObject::class, $decoded);
static::assertEquals(0b010, $decoded->getMajorType());
static::assertEquals(CBORObject::MAJOR_TYPE_BYTE_STRING, $decoded->getMajorType());
static::assertEquals($expectedAdditionalInformation, $decoded->getAdditionalInformation());
static::assertEquals($string, $decoded->getValue());
static::assertEquals($expectedLength, $decoded->getLength());
Expand Down
Loading

0 comments on commit e3b79e9

Please sign in to comment.