PHP Classes

File: tests/KAT/LocalPWTest.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   PASERK PHP   tests/KAT/LocalPWTest.php   Download  
File: tests/KAT/LocalPWTest.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: 2,093 bytes
 

Contents

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

use
ParagonIE\ConstantTime\Hex;
use
ParagonIE\HiddenString\HiddenString;
use
ParagonIE\Paserk\PaserkException;
use
ParagonIE\Paserk\Types\LocalPW;
use
ParagonIE\Paserk\Tests\KnownAnswers;
use
ParagonIE\Paseto\Exception\InvalidVersionException;
use
ParagonIE\Paseto\Keys\SymmetricKey;
use
ParagonIE\Paseto\Protocol\{
   
Version3,
   
Version4
};
use
ParagonIE\Paseto\ProtocolInterface;
use
Throwable;

/**
 * @covers LocalPW
 */
class LocalPWTest extends KnownAnswers
{

    public function
testV3()
    {
       
$this->doJsonTest(new Version3(), 'k3.local-pw.json');
    }

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

   
/**
     * @param ProtocolInterface $version
     * @param string $name
     * @param array $tests
     *
     * @throws InvalidVersionException
     * @throws PaserkException
     */
   
protected function genericTest(ProtocolInterface $version, string $name, array $tests): void
   
{
        foreach (
$tests as $test) {
           
$wrapper = new LocalPW(
                new
HiddenString($test['password']),
               
$test['options'] ?? [],
               
$version
           
);
            if (
$test['expect-fail']) {
                try {
                   
$wrapper->decode($test['paserk']);
                } catch (
Throwable $exception) {
                    continue;
                }
               
$this->fail($name . ' > ' . $test['name'] . ': '. $test['comment']);
            }
            if (empty(
$test['paserk'])) {
               
var_dump($wrapper->encode(
                    new
SymmetricKey(
                       
Hex::decode($test['unwrapped']),
                       
$version
                   
)
                ));
                continue;
            }
           
$unwrapped = $wrapper->decode($test['paserk']);
           
$this->assertSame(
               
$test['unwrapped'],
               
Hex::encode($unwrapped->raw()),
               
$test['name']
            );
        }
    }
}