Skip to content

Commit

Permalink
Merge pull request #1 from mgd-php/add-visible-string-type
Browse files Browse the repository at this point in the history
Added ASN.1 VisibleString type
  • Loading branch information
pkotets authored Apr 16, 2024
2 parents 2d78561 + 3eea5ba commit 51db3e1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/ASN1/ASN1Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Readdle\AppStoreReceiptVerification\ASN1\Universal\Set;
use Readdle\AppStoreReceiptVerification\ASN1\Universal\UTCTime;
use Readdle\AppStoreReceiptVerification\ASN1\Universal\UTF8String;
use Readdle\AppStoreReceiptVerification\ASN1\Universal\VisibleString;
use Readdle\AppStoreReceiptVerification\BufferReader;
use UnexpectedValueException;

Expand All @@ -37,6 +38,7 @@ final class ASN1Identifier
const TYPE__PRINTABLE_STRING = 0x13;
const TYPE__IA5_STRING = 0x16;
const TYPE__UTC_TIME = 0x17;
const TYPE__VISIBLE_STRING = 0x1A;

const IS_CONTEXT_SPECIFIC = 0b10;
const IS_CONSTRUCTED = 0b00100000;
Expand All @@ -55,6 +57,7 @@ final class ASN1Identifier
self::TYPE__PRINTABLE_STRING => PrintableString::class,
self::TYPE__IA5_STRING => IA5String::class,
self::TYPE__UTC_TIME => UTCTime::class,
self::TYPE__VISIBLE_STRING => VisibleString::class,
];

const TYPE_TO_STRING = [
Expand All @@ -70,6 +73,7 @@ final class ASN1Identifier
self::TYPE__PRINTABLE_STRING => 'Printable String',
self::TYPE__IA5_STRING => 'IA5 String',
self::TYPE__UTC_TIME => 'UTC Time',
self::TYPE__VISIBLE_STRING => 'Visible String',
];

private int $octet;
Expand Down
26 changes: 26 additions & 0 deletions src/ASN1/Universal/VisibleString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace Readdle\AppStoreReceiptVerification\ASN1\Universal;

use Readdle\AppStoreReceiptVerification\ASN1\AbstractASN1Object;

final class VisibleString extends AbstractASN1Object
{
protected string $value = '';

protected function setValue($value): void
{
$this->value = $value;
}

public function getValue(): string
{
return $this->value;
}

public function jsonSerialize(): string
{
return $this->value;
}
}
2 changes: 1 addition & 1 deletion tests/Unit/ASN1/ASN1IdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testGetTypeAndGetTypeString(): void
));
$this->assertEquals($type, $contextSpecificConstructed->getType());
$this->assertEquals(
'Constructed Context-Specific 0x' . ucfirst(dechex($type)),
'Constructed Context-Specific 0x' . strtoupper(dechex($type)),
$contextSpecificConstructed->getTypeString()
);
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/ASN1/Universal/VisibleStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

namespace Readdle\AppStoreReceiptVerification\Tests\Unit\ASN1\Universal;

use Readdle\AppStoreReceiptVerification\ASN1\ASN1Identifier;
use Readdle\AppStoreReceiptVerification\Tests\Unit\Traits\AsBinaryTestTrait;
use Readdle\AppStoreReceiptVerification\Tests\Unit\Traits\StringTestTrait;
use Readdle\AppStoreReceiptVerification\Tests\Unit\UnitTestCase;

final class VisibleStringTest extends UnitTestCase
{
use AsBinaryTestTrait;
use StringTestTrait;

public function testIdentifier(): void
{
$this->assertEquals(
ASN1Identifier::TYPE__VISIBLE_STRING,
$this->createASN1Object(ASN1Identifier::TYPE__VISIBLE_STRING, 1)->getIdentifier()->getType()
);
}

public function testStringLength(): void
{
$this->performStringLengthTests(ASN1Identifier::TYPE__VISIBLE_STRING);
}

public function testJsonSerialize(): void
{
$this->assertJsonSerializeResult(ASN1Identifier::TYPE__VISIBLE_STRING, '', '');
$this->assertJsonSerializeResult(ASN1Identifier::TYPE__VISIBLE_STRING, 'test', 'test');
}

public function testAsBinary(): void
{
$this->performAsBinaryTest(ASN1Identifier::TYPE__VISIBLE_STRING, 'test');
}
}

0 comments on commit 51db3e1

Please sign in to comment.