init
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Encryption;
|
||||
|
||||
class EncryptorTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Encryption\Encryptor
|
||||
*/
|
||||
private $encryptor;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->encryptor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Encryption\Encryptor::class
|
||||
);
|
||||
}
|
||||
|
||||
public function testEncryptDecrypt()
|
||||
{
|
||||
$this->assertEquals('', $this->encryptor->decrypt($this->encryptor->encrypt('')));
|
||||
$this->assertEquals('test', $this->encryptor->decrypt($this->encryptor->encrypt('test')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @dataProvider validEncryptionKeyDataProvider
|
||||
*/
|
||||
public function testValidateKey($key)
|
||||
{
|
||||
$this->encryptor->validateKey($key);
|
||||
}
|
||||
|
||||
public function validEncryptionKeyDataProvider()
|
||||
{
|
||||
return [
|
||||
'32 numbers' => ['12345678901234567890123456789012'],
|
||||
'32 characters' => ['aBcdeFghIJKLMNOPQRSTUvwxYzabcdef'],
|
||||
'32 special characters' => ['!@#$%^&*()_+~`:;"<>,.?/|*&^%$#@!'],
|
||||
'32 combination' =>['1234eFghI1234567^&*(890123456789'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $key
|
||||
* @dataProvider invalidEncryptionKeyDataProvider
|
||||
*/
|
||||
public function testValidateKeyInvalid($key)
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Encryption key must be 32 character string without any white space.');
|
||||
|
||||
$this->encryptor->validateKey($key);
|
||||
}
|
||||
|
||||
public function invalidEncryptionKeyDataProvider()
|
||||
{
|
||||
return [
|
||||
'empty string' => [''],
|
||||
'leading space' => [' 1234567890123456789012345678901'],
|
||||
'tailing space' => ['1234567890123456789012345678901 '],
|
||||
'space in the middle' => ['12345678901 23456789012345678901'],
|
||||
'tab in the middle' => ['12345678901 23456789012345678'],
|
||||
'return in the middle' => ['12345678901
|
||||
23456789012345678901'],
|
||||
'31 characters' => ['1234567890123456789012345678901'],
|
||||
'33 characters' => ['123456789012345678901234567890123'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Framework\Encryption;
|
||||
|
||||
class ModelTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Encryption\Encryptor
|
||||
*/
|
||||
protected $_model;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Encryption\Encryptor::class
|
||||
);
|
||||
}
|
||||
|
||||
public function testEncryptDecrypt()
|
||||
{
|
||||
$encryptor = $this->_model;
|
||||
|
||||
$this->assertEquals('', $encryptor->decrypt($encryptor->encrypt('')));
|
||||
$this->assertEquals('test', $encryptor->decrypt($encryptor->encrypt('test')));
|
||||
}
|
||||
|
||||
public function testEncryptDecrypt2()
|
||||
{
|
||||
$encryptor = $this->_model;
|
||||
|
||||
// md5() here is not for cryptographic use just generate random string.
|
||||
// phpcs:ignore Magento2.Security.InsecureFunction
|
||||
$initial = md5(uniqid());
|
||||
$encrypted = $encryptor->encrypt($initial);
|
||||
$this->assertNotEquals($initial, $encrypted);
|
||||
$this->assertEquals($initial, $encryptor->decrypt($encrypted));
|
||||
}
|
||||
|
||||
public function testValidateKey()
|
||||
{
|
||||
// md5() have to be use here.
|
||||
// phpcs:ignore Magento2.Security.InsecureFunction
|
||||
$validKey = md5(uniqid());
|
||||
$this->_model->validateKey($validKey);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testValidateKeyInvalid()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$invalidKey = '---- ';
|
||||
$this->_model->validateKey($invalidKey);
|
||||
}
|
||||
|
||||
public function testGetValidateHash()
|
||||
{
|
||||
$password = uniqid();
|
||||
$hash = $this->_model->getHash($password, true);
|
||||
|
||||
$this->assertIsString($hash);
|
||||
$this->assertTrue($this->_model->validateHash($password, $hash));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user