-
Notifications
You must be signed in to change notification settings - Fork 12
/
ElementDecodeTest.php
85 lines (75 loc) · 2.42 KB
/
ElementDecodeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
use Sop\ASN1\Component\Identifier;
use Sop\ASN1\Element;
use Sop\ASN1\Type\Primitive\Boolean;
use Sop\ASN1\Type\Primitive\NullType;
/**
* @group decode
* @group element
*
* @internal
*/
class ElementDecodeTest extends TestCase
{
public function testAbstract()
{
$el = Element::fromDER("\x5\x0");
$this->assertInstanceOf(NullType::class, $el);
}
public function testConcrete()
{
$el = NullType::fromDER("\x5\x0");
$this->assertInstanceOf(NullType::class, $el);
}
public function testConcreteWrongClass()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage(
Boolean::class . ' expected, got ' . NullType::class);
Boolean::fromDER("\x5\x0");
}
public function testUnimplementedFail()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('not implemented');
Element::fromDER("\x1f\x7f\x0");
}
public function testExpectTaggedFail()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage(
'Context specific element expected, got UNIVERSAL');
Element::fromDER("\x5\x0")->expectTagged();
}
public function testFromDERBadCall()
{
$cls = new ReflectionClass(Element::class);
$mtd = $cls->getMethod('_decodeFromDER');
$mtd->setAccessible(true);
$identifier = new Identifier(Identifier::CLASS_UNIVERSAL,
Identifier::PRIMITIVE, Element::TYPE_NULL);
$offset = 0;
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('must be implemented in derived class');
$mtd->invokeArgs(null, [$identifier, '', &$offset]);
}
public function testFromUnimplementedClass()
{
$cls = new ReflectionClass(Element::class);
$mtd = $cls->getMethod('_determineImplClass');
$mtd->setAccessible(true);
$identifier = new ElementDecodeTest_IdentifierMockup(0, 0, 0);
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('not implemented');
$mtd->invokeArgs(null, [$identifier]);
}
}
class ElementDecodeTest_IdentifierMockup extends Identifier
{
public function typeClass(): int
{
return 0xff;
}
}