PHP Classes

File: tests/KAT/PidTest.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   PASERK PHP   tests/KAT/PidTest.php   Download  
File: tests/KAT/PidTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PASERK PHP
Extend PASETO to wrap and serialize keys
Author: By
Last change:
Date: 1 year ago
Size: 1,911 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace
ParagonIE\Paserk\Tests\KAT;

use
Exception;
use
FG\ASN1\Exception\ParserException;
use
ParagonIE\ConstantTime\Hex;
use
ParagonIE\Paserk\PaserkException;
use
ParagonIE\Paserk\Tests\KnownAnswers;
use
ParagonIE\Paserk\Types\Pid;
use
ParagonIE\Paseto\Exception\PasetoException;
use
ParagonIE\Paseto\Keys\AsymmetricPublicKey;
use
ParagonIE\Paseto\ProtocolInterface;
use
RangeException;
use
SodiumException;
use
ParagonIE\Paseto\Protocol\{
   
Version3,
   
Version4
};

/**
 * @covers Pid
 */
class PidTest extends KnownAnswers
{
    public function
testV3()
    {
       
$this->doJsonTest(new Version3(), 'k3.pid.json');
    }

    public function
testV4()
    {
       
$this->doJsonTest(new Version4(), 'k4.pid.json');
    }

   
/**
     * @throws Exception
     */
   
protected function getPublicKey(ProtocolInterface $version, string $key): AsymmetricPublicKey
   
{
        return new
AsymmetricPublicKey(Hex::decode($key), $version);
    }

   
/**
     * @param ProtocolInterface $version
     * @param string $name
     * @param array $tests
     *
     * @throws PaserkException
     * @throws SodiumException
     */
   
protected function genericTest(ProtocolInterface $version, string $name, array $tests): void
   
{
        foreach (
$tests as $test) {
            if (
$test['expect-fail']) {
                try {
                   
$publickey = $this->getPublicKey($version, $test['key']);
                   
Pid::encodePublic($publickey);
                   
$this->fail($test['name'] . ': '. $test['comment']);
                } catch (
ParserException | RangeException | PasetoException | PaserkException $ex) {
                }
                continue;
            }
           
$publickey = $this->getPublicKey($version, $test['key']);
           
$this->assertSame($test['paserk'], Pid::encodePublic($publickey), $test['name']);
        }
    }
}