PHP Classes

File: tests/Types/LocalWrapTest.php

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

Contents

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

use
Exception;
use
ParagonIE\Paserk\{
   
PaserkException,
   
Types\Lid,
   
Types\LocalWrap
};
use
ParagonIE\Paseto\Exception\InvalidVersionException;
use
ParagonIE\Paseto\Keys\SymmetricKey;
use
ParagonIE\Paseto\Protocol\{
   
Version3,
   
Version4
};
use
PHPUnit\Framework\TestCase;
use
SodiumException;

/**
 * Class LocalWrapTest
 * @package ParagonIE\Paserk\Tests\Types
 *
 * @covers LocalWrap
 */
class LocalWrapTest extends TestCase
{
    protected
SymmetricKey $v3key;
    protected
SymmetricKey $v4key;

   
/**
     * @throws Exception
     */
   
public function setUp(): void
   
{
       
$this->v3key = SymmetricKey::generate(new Version3());
       
$this->v4key = SymmetricKey::generate(new Version4());
    }

   
/**
     * @throws PaserkException
     * @throws InvalidVersionException
     * @throws SodiumException
     */
   
public function testWrap()
    {
       
/** @var SymmetricKey $key */
       
foreach ([$this->v3key, $this->v4key] as $key) {
           
// Generate wrapping key
           
$version = $key->getProtocol();
           
$wk = SymmetricKey::generate($version);
           
$lw = LocalWrap::initWithKey($wk);

           
$id = $lw->id($key);
           
$encoded = $lw->encode($key);
           
$id2 = Lid::encode($version, $encoded);
           
$this->assertSame($id2, $id, 'Local-wrap key IDs must be deterministic');

           
/** @var SymmetricKey $decoded */
           
$decoded = $lw->decode($encoded);
           
$this->assertSame(
               
$key->encode(),
               
$decoded->encode(),
               
'local-wrap ' . $key->getProtocol()::header()
            );
        }
    }
}