public function testContent() { $object = new Enumerated(0); $this->assertEquals(0, $object->getContent()); $object = new Enumerated(1); $this->assertEquals(1, $object->getContent()); $object = new Enumerated(512); $this->assertEquals(512, $object->getContent()); }
/** * For the real parsing tests look in the test cases of each single ASn object. */ public function testFromBinary() { /* @var BitString $parsedObject */ $binaryData = chr(Identifier::BITSTRING); $binaryData .= chr(0x3); $binaryData .= chr(0x5); $binaryData .= chr(0xff); $binaryData .= chr(0xa0); $expectedObject = new BitString(0xffa0, 5); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof BitString); $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent()); $this->assertEquals($expectedObject->getNumberOfUnusedBits(), $parsedObject->getNumberOfUnusedBits()); /* @var OctetString $parsedObject */ $binaryData = chr(Identifier::OCTETSTRING); $binaryData .= chr(0x2); $binaryData .= chr(0xff); $binaryData .= chr(0xa0); $expectedObject = new OctetString(0xffa0); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof OctetString); $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent()); /* @var \FG\ASN1\Universal\Boolean $parsedObject */ $binaryData = chr(Identifier::BOOLEAN); $binaryData .= chr(0x1); $binaryData .= chr(0xff); $expectedObject = new Boolean(true); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof Boolean); $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent()); /* @var Enumerated $parsedObject */ $binaryData = chr(Identifier::ENUMERATED); $binaryData .= chr(0x1); $binaryData .= chr(0x3); $expectedObject = new Enumerated(3); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof Enumerated); $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent()); /* @var IA5String $parsedObject */ $string = 'Hello Foo World!!!11EinsEins!1'; $binaryData = chr(Identifier::IA5_STRING); $binaryData .= chr(strlen($string)); $binaryData .= $string; $expectedObject = new IA5String($string); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof IA5String); $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent()); /* @var \FG\ASN1\Universal\Integer $parsedObject */ $binaryData = chr(Identifier::INTEGER); $binaryData .= chr(0x1); $binaryData .= chr(123); $expectedObject = new Integer(123); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof Integer); $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent()); /* @var \FG\ASN1\Universal\NullObject $parsedObject */ $binaryData = chr(Identifier::NULL); $binaryData .= chr(0x0); $expectedObject = new NullObject(); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof NullObject); $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent()); /* @var ObjectIdentifier $parsedObject */ $binaryData = chr(Identifier::OBJECT_IDENTIFIER); $binaryData .= chr(0x2); $binaryData .= chr(1 * 40 + 2); $binaryData .= chr(3); $expectedObject = new ObjectIdentifier('1.2.3'); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof ObjectIdentifier); $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent()); /* @var PrintableString $parsedObject */ $string = 'This is a test string. #?!%&""'; $binaryData = chr(Identifier::PRINTABLE_STRING); $binaryData .= chr(strlen($string)); $binaryData .= $string; $expectedObject = new PrintableString($string); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof PrintableString); $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent()); /* @var GeneralizedTime $parsedObject */ $binaryData = chr(Identifier::GENERALIZED_TIME); $binaryData .= chr(15); $binaryData .= '20120923202316Z'; $expectedObject = new GeneralizedTime('2012-09-23 20:23:16', 'UTC'); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof GeneralizedTime); $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent()); /* @var Sequence $parsedObject */ $binaryData = chr(Identifier::SEQUENCE); $binaryData .= chr(0x6); $binaryData .= chr(Identifier::BOOLEAN); $binaryData .= chr(0x1); $binaryData .= chr(0x0); $binaryData .= chr(Identifier::INTEGER); $binaryData .= chr(0x1); $binaryData .= chr(0x3); $expectedChild1 = new Boolean(false); $expectedChild2 = new Integer(0x3); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof Sequence); $this->assertEquals(2, $parsedObject->getNumberOfChildren()); $children = $parsedObject->getChildren(); $child1 = $children[0]; $child2 = $children[1]; $this->assertEquals($expectedChild1->getContent(), $child1->getContent()); $this->assertEquals($expectedChild2->getContent(), $child2->getContent()); /* @var ExplicitlyTaggedObject $parsedObject */ $taggedObject = new ExplicitlyTaggedObject(0x1, new PrintableString('Hello tagged world')); $binaryData = $taggedObject->getBinary(); $parsedObject = Object::fromBinary($binaryData); $this->assertTrue($parsedObject instanceof ExplicitlyTaggedObject); // An unknown constructed object containing 2 integer children, // first 3 bytes are the identifier. $binaryData = "?�" . chr(Identifier::INTEGER) . "B" . chr(Identifier::INTEGER) . "i"; $offsetIndex = 0; $parsedObject = OBject::fromBinary($binaryData, $offsetIndex); $this->assertTrue($parsedObject instanceof UnknownConstructedObject); $this->assertEquals(substr($binaryData, 0, 3), $parsedObject->getIdentifier()); $this->assertCount(2, $parsedObject->getContent()); $this->assertEquals(strlen($binaryData), $offsetIndex); $this->assertEquals(10, $parsedObject->getObjectLength()); // First 3 bytes are the identifier $binaryData = "��"; $offsetIndex = 0; $parsedObject = Object::fromBinary($binaryData, $offsetIndex); $this->assertTrue($parsedObject instanceof UnknownObject); $this->assertEquals(substr($binaryData, 0, 3), $parsedObject->getIdentifier()); $this->assertEquals('Unparsable Object (1 bytes)', $parsedObject->getContent()); $this->assertEquals(strlen($binaryData), $offsetIndex); $this->assertEquals(5, $parsedObject->getObjectLength()); }