Skip to content
Merged
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
19 changes: 11 additions & 8 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ name: PHP Unit Tests

on:
push:
branches: [ main, develop ]
branches: [ master ]
pull_request:
branches: [ main, develop ]
branches: [ master ]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
dependency-version: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php-versions }} - ${{ matrix.dependency-version }}
Expand Down Expand Up @@ -50,12 +50,15 @@ jobs:
run: vendor/bin/phpunit

- name: Run PHPUnit with coverage
if: matrix.php-versions == '8.4' && matrix.dependency-version == 'prefer-stable'
run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml

- name: Upload coverage to Coveralls
- name: Upload coverage to Codecov
if: matrix.php-versions == '8.4' && matrix.dependency-version == 'prefer-stable'
uses: coverallsapp/github-action@v2
uses: codecov/codecov-action@v3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: build/logs/clover.xml
format: clover
token: ${{ secrets.CODECOV_TOKEN }}
file: ./build/logs/clover.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: false
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ php:
- 7.4
- 8.0
- 8.1
- 8.2
- 8.3
- 8.4

cache:
directories:
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
PHPASN1
=======

[![Build Status](https://secure.travis-ci.org/Adapik/PHPASN1.png?branch=master)](http://travis-ci.org/Adapik/PHPASN1)
[![PHP 7 ready](http://php7ready.timesplinter.ch/Adapik/PHPASN1/badge.svg)](https://travis-ci.org/Adapik/PHPASN1)
[![Coverage Status](https://coveralls.io/repos/Adapik/PHPASN1/badge.svg?branch=master&service=github)](https://coveralls.io/github/Adapik/PHPASN1?branch=master)
![build status](https://github.com/github/docs/actions/workflows/tests.yml/badge.svg)
[![codecov](https://codecov.io/github/adapik/phpasn1/graph/badge.svg?token=KCBH7EV2J0)](https://codecov.io/github/adapik/phpasn1)

[![Latest Stable Version](https://poser.pugx.org/Adapik/phpasn1/v/stable.png)](https://packagist.org/packages/Adapik/phpasn1)
[![Total Downloads](https://poser.pugx.org/Adapik/phpasn1/downloads.png)](https://packagist.org/packages/Adapik/phpasn1)
[![Latest Unstable Version](https://poser.pugx.org/Adapik/phpasn1/v/unstable.png)](https://packagist.org/packages/Adapik/phpasn1)
[![License](https://poser.pugx.org/Adapik/phpasn1/license.png)](https://packagist.org/packages/Adapik/phpasn1)

A PHP Framework that allows you to encode and decode arbitrary [ASN.1][3] structures
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
"keywords": [ "x690", "x.690", "x.509", "x509", "asn1", "asn.1", "ber", "der", "binary", "encoding", "decoding" ],

"require": {
"php": ">=7.2",
"php": ">=7.4",
"ext-gmp": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"php-coveralls/php-coveralls": "^2.4"
"phpunit/phpunit": "^9.0"
},
"suggest": {
"php-curl": "For loading OID information from the web if they have not bee defined statically"
Expand Down
33 changes: 11 additions & 22 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
colors="true">

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion src/ASN1/AbstractTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ protected static function extractTimeZoneData(&$binaryData, &$offsetIndex, DateT
return $dateTime;
}

abstract public static function createFormDateTime(\DateTimeInterface $dateTime = null, array $options = []);
abstract public static function createFormDateTime(?\DateTimeInterface $dateTime = null, array $options = []);
}
2 changes: 1 addition & 1 deletion src/ASN1/ElementBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected static function createIdentifier($tagClass, $isConstructed, $tagNumber

public static function createContentLength($content, $lengthForm)
{
$length = strlen($content);
$length = strlen((string) $content);
if ($lengthForm === ContentLength::INDEFINITE_FORM) {
$lengthOctets = chr(128);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/ASN1/Universal/GeneralizedTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function encodeValue(string $dateTime)
return $dateTime;
}

public static function createFormDateTime(\DateTimeInterface $dateTime = null, array $options = [])
public static function createFormDateTime(?\DateTimeInterface $dateTime = null, array $options = [])
{
$dateTime = $dateTime ?? new DateTime('now', new DateTimeZone('UTC'));

Expand Down
5 changes: 2 additions & 3 deletions src/ASN1/Universal/UTCTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ public function setValue(Content $content)
$this->value = $dateTime;
}

public static function createFormDateTime(\DateTimeInterface $dateTime = null, array $options = [])
public static function createFormDateTime(?\DateTimeInterface $dateTime = null, array $options = [])
{
$dateTime = $dateTime ?? new DateTime('now', new DateTimeZone('UTC'));

$isConstructed = false;
$lengthForm = $options['lengthForm'] ?? ContentLength::SHORT_FORM;

$string = $dateTime->format('ymdHis') . 'Z';
Expand All @@ -99,7 +98,7 @@ public static function createFormDateTime(\DateTimeInterface $dateTime = null, a
ElementBuilder::createObject(
Identifier::CLASS_UNIVERSAL,
static::getType(),
$isConstructed,
false,
$string,
$lengthForm
);
Expand Down
35 changes: 21 additions & 14 deletions tests/ASN1/ASN1ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,79 +201,86 @@ public function testFromBinary()
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 10: Can not parse binary from data: Offset index larger than input size
* @depends testFromBinary
*/
public function testFromBinaryThrowsException()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 10: Can not parse binary from data: Offset index larger than input size');

$binaryData = 0x0;
$offset = 10;
ASN1Object::fromBinary($binaryData, $offset);
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 0: Can not parse binary from data: Offset index larger than input size
* @depends testFromBinary
*/
public function testFromBinaryWithEmptyStringThrowsException()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 0: Can not parse binary from data: Offset index larger than input size');

$data = '';
ASN1Object::fromBinary($data);
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 2: Can not parse binary from data: Offset index larger than input size
* @depends testFromBinary
*/
public function testFromBinaryWithSpacyStringThrowsException()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 2: Can not parse binary from data: Offset index larger than input size');

$data = ' ';
ASN1Object::fromBinary($data);
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 1: Can not parse content length from data: Offset index larger than input size
* @depends testFromBinary
*/
public function testFromBinaryWithNumberStringThrowsException()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 1: Can not parse content length from data: Offset index larger than input size');

$data = '1';
ASN1Object::fromBinary($data);
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 25: Can not parse content length from data: Offset index larger than input size
* @depends testFromBinary
*/
public function testFromBinaryWithGarbageStringThrowsException()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 25: Can not parse content length from data: Offset index larger than input size');

$data = 'certainly no asn.1 object';
ASN1Object::fromBinary($data);
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 1: Can not parse identifier (long form) from data: Offset index larger than input size
* @depends testFromBinary
*/
public function testFromBinaryUnknownObjectMissingLength()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 1: Can not parse identifier (long form) from data: Offset index larger than input size');

$data = hex2bin('1f');
ASN1Object::fromBinary($data);
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 4: Can not parse content length (long form) from data: Offset index larger than input size
* @depends testFromBinary
*/
public function testFromBinaryInalidLongFormContentLength()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 4: Can not parse content length (long form) from data: Offset index larger than input size');

$binaryData = chr(Identifier::INTEGER);
$binaryData .= chr(0x8f); //denotes a long-form content length with 15 length-octets
$binaryData .= chr(0x1); //only give one content-length-octet
Expand Down
7 changes: 3 additions & 4 deletions tests/ASN1/Base128Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ public function getDataToDecode()
];
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Malformed base-128 encoded value (0xFFFF).
*/
public function testDecodeFailsIfLastOctetSignificantBitSet()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Malformed base-128 encoded value (0xFFFF).');

Base128::decode("\xFF\xFF");
}
}
7 changes: 4 additions & 3 deletions tests/ASN1/Universal/BitStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ public function testFromBinaryWithOffset()
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 3: Malformed bit string
* @depends testFromBinary
* @depends testFromBinary
*/
public function testFromBinaryWithInvalidLength01()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 3: Malformed bit string');

$binaryData = chr(Identifier::BITSTRING);
$binaryData .= chr(0x01);
$binaryData .= chr(0x00);
Expand Down
10 changes: 6 additions & 4 deletions tests/ASN1/Universal/BooleanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,27 @@ public function testFromBinaryWithOffset()
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 4: An ASN.1 Boolean should not have a length other than one. Extracted length was 2
* @depends testFromBinary
*/
public function testFromBinaryWithInvalidLength01()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 4: An ASN.1 Boolean should not have a length other than one. Extracted length was 2');

$binaryData = chr(Identifier::BOOLEAN);
$binaryData .= chr(0x02);
$binaryData .= chr(0xFF);
Boolean::fromBinary($binaryData);
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 2: An ASN.1 Boolean should not have a length other than one. Extracted length was 0
* @depends testFromBinary
*/
public function testFromBinaryWithInvalidLength02()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 2: An ASN.1 Boolean should not have a length other than one. Extracted length was 0');

$binaryData = chr(Identifier::BOOLEAN);
$binaryData .= chr(0x00);
$binaryData .= chr(0xFF);
Expand Down
5 changes: 2 additions & 3 deletions tests/ASN1/Universal/IntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ public function testGetIdentifierCode()
$this->assertEquals('Integer', $object->getIdentifier()->getCode());
}

/**
* @expectedException \Exception
*/
public function testCreateInstanceCanFail()
{
$this->expectException(\Exception::class);

Integer::create('a');
}

Expand Down
5 changes: 3 additions & 2 deletions tests/ASN1/Universal/NullObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ public function testFromBinaryWithOffset()
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 3: An ASN.1 Null should not have a length other than zero. Extracted length was 1
* @depends testFromBinary
*/
public function testFromBinaryWithInvalidLength01()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 3: An ASN.1 Null should not have a length other than zero. Extracted length was 1');

$binaryData = chr(Identifier::NULL);
$binaryData .= chr(0x01);
NullObject::fromBinary($binaryData);
Expand Down
14 changes: 7 additions & 7 deletions tests/ASN1/Universal/ObjectIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ public function testFromBinaryWithOffset()
}

/**
* @expectedException \FG\ASN1\Exception\ParserException
* @expectedExceptionMessage ASN.1 Parser Exception at offset 2: Malformed ASN.1 Object Identifier
* @depends testFromBinary
* @depends testFromBinary
*/
public function testFromBinaryWithMalformedOID()
{
$this->expectException(\FG\ASN1\Exception\ParserException::class);
$this->expectExceptionMessage('ASN.1 Parser Exception at offset 2: Malformed ASN.1 Object Identifier');

$binaryData = chr(Identifier::OBJECT_IDENTIFIER);
$binaryData .= chr(0x03);
$binaryData .= chr(42);
Expand All @@ -103,12 +104,11 @@ public function testFromBinaryWithMalformedOID()
ObjectIdentifier::fromBinary($binaryData);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage [1.Foo.3] is no valid object identifier (sub identifier 2 is not numeric)!
*/
public function testCreateWithInvalidObjectIdentifier()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('[1.Foo.3] is no valid object identifier (sub identifier 2 is not numeric)!');

ObjectIdentifier::create('1.Foo.3');
}
}