Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CryptTest.php
Go to the documentation of this file.
1 <?php
11 
12 class CryptTest extends \PHPUnit\Framework\TestCase
13 {
14  private $_key;
15 
16  private static $_cipherInfo;
17 
18  private const SUPPORTED_CIPHER_MODE_COMBINATIONS = [
19  MCRYPT_BLOWFISH => [MCRYPT_MODE_ECB],
20  MCRYPT_RIJNDAEL_128 => [MCRYPT_MODE_ECB],
21  MCRYPT_RIJNDAEL_256 => [MCRYPT_MODE_CBC],
22  ];
23 
24  protected function setUp()
25  {
26  $this->_key = substr(__CLASS__, -32, 32);
27  }
28 
33  protected function _getRandomString($length)
34  {
35  $result = '';
36  do {
37  $result .= sha1(microtime());
38  } while (strlen($result) < $length);
39  return substr($result, -$length);
40  }
41 
42  protected function _requireCipherInfo()
43  {
44  $filename = __DIR__ . '/Crypt/_files/_cipher_info.php';
45 
46  if (!self::$_cipherInfo) {
47  self::$_cipherInfo = include $filename;
48  }
49  }
50 
56  protected function _getKeySize($cipherName, $modeName)
57  {
58  $this->_requireCipherInfo();
59  return self::$_cipherInfo[$cipherName][$modeName]['key_size'];
60  }
61 
67  protected function _getInitVectorSize($cipherName, $modeName)
68  {
69  $this->_requireCipherInfo();
70  return self::$_cipherInfo[$cipherName][$modeName]['iv_size'];
71  }
72 
76  public function getCipherModeCombinations(): array
77  {
78  $result = [];
79  foreach (self::SUPPORTED_CIPHER_MODE_COMBINATIONS as $cipher => $modes) {
81  foreach ($modes as $mode) {
82  $result[$cipher . '-' . $mode] = [$cipher, $mode];
83  }
84  }
85  return $result;
86  }
87 
91  public function testConstructor($cipher, $mode)
92  {
93  /* Generate random init vector */
94  $initVector = $this->_getRandomString($this->_getInitVectorSize($cipher, $mode));
95 
96  $crypt = new \Magento\Framework\Encryption\Crypt($this->_key, $cipher, $mode, $initVector);
97 
98  $this->assertEquals($cipher, $crypt->getCipher());
99  $this->assertEquals($mode, $crypt->getMode());
100  $this->assertEquals($initVector, $crypt->getInitVector());
101  }
102 
106  public function getConstructorExceptionData()
107  {
108  $key = substr(__CLASS__, -32, 32);
109  $result = [];
110  foreach (self::SUPPORTED_CIPHER_MODE_COMBINATIONS as $cipher => $modes) {
112  foreach ($modes as $mode) {
113  $tooLongKey = str_repeat('-', $this->_getKeySize($cipher, $mode) + 1);
114  $tooShortInitVector = str_repeat('-', $this->_getInitVectorSize($cipher, $mode) - 1);
115  $tooLongInitVector = str_repeat('-', $this->_getInitVectorSize($cipher, $mode) + 1);
116  $result['tooLongKey-' . $cipher . '-' . $mode . '-false'] = [$tooLongKey, $cipher, $mode, false];
117  $keyPrefix = 'key-' . $cipher . '-' . $mode;
118  $result[$keyPrefix . '-tooShortInitVector'] = [$key, $cipher, $mode, $tooShortInitVector];
119  $result[$keyPrefix . '-tooLongInitVector'] = [$key, $cipher, $mode, $tooLongInitVector];
120  }
121  }
122  return $result;
123  }
124 
129  public function testConstructorException($key, $cipher, $mode, $initVector)
130  {
131  new \Magento\Framework\Encryption\Crypt($key, $cipher, $mode, $initVector);
132  }
133 
134  public function testConstructorDefaults()
135  {
136  $cryptExpected = new \Magento\Framework\Encryption\Crypt($this->_key, MCRYPT_BLOWFISH, MCRYPT_MODE_ECB, false);
137  $cryptActual = new \Magento\Framework\Encryption\Crypt($this->_key);
138 
139  $this->assertEquals($cryptExpected->getCipher(), $cryptActual->getCipher());
140  $this->assertEquals($cryptExpected->getMode(), $cryptActual->getMode());
141  $this->assertEquals($cryptExpected->getInitVector(), $cryptActual->getInitVector());
142  }
143 
147  public function getCryptData()
148  {
149  $fixturesFilename = __DIR__ . '/Crypt/_files/_crypt_fixtures.php';
150 
151  $result = include $fixturesFilename;
152  /* Restore encoded string back to binary */
153  foreach ($result as &$cryptParams) {
154  $cryptParams[5] = base64_decode($cryptParams[5]);
155  }
156  unset($cryptParams);
157  return $result;
158  }
159 
163  public function testEncrypt($key, $cipher, $mode, $initVector, $inputData, $expectedData)
164  {
165  $crypt = new \Magento\Framework\Encryption\Crypt($key, $cipher, $mode, $initVector);
166  $actualData = $crypt->encrypt($inputData);
167  $this->assertEquals($expectedData, $actualData);
168  }
169 
173  public function testDecrypt($key, $cipher, $mode, $initVector, $expectedData, $inputData)
174  {
175  $crypt = new \Magento\Framework\Encryption\Crypt($key, $cipher, $mode, $initVector);
176  $actualData = $crypt->decrypt($inputData);
177  $this->assertEquals($expectedData, $actualData);
178  }
179 
183  public function testInitVectorRandom($cipher, $mode)
184  {
185  $crypt1 = new \Magento\Framework\Encryption\Crypt($this->_key, $cipher, $mode, true);
186  $initVector1 = $crypt1->getInitVector();
187 
188  $crypt2 = new \Magento\Framework\Encryption\Crypt($this->_key, $cipher, $mode, true);
189  $initVector2 = $crypt2->getInitVector();
190 
191  $expectedSize = $this->_getInitVectorSize($cipher, $mode);
192  $this->assertEquals($expectedSize, strlen($initVector1));
193  $this->assertEquals($expectedSize, strlen($initVector2));
194  $this->assertNotEquals($initVector2, $initVector1);
195  }
196 
200  public function testInitVectorNone($cipher, $mode)
201  {
202  $crypt = new \Magento\Framework\Encryption\Crypt($this->_key, $cipher, $mode, false);
203  $actualInitVector = $crypt->getInitVector();
204 
205  $expectedInitVector = str_repeat("\0", $this->_getInitVectorSize($cipher, $mode));
206  $this->assertEquals($expectedInitVector, $actualInitVector);
207  }
208 }
$initVector
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
testDecrypt($key, $cipher, $mode, $initVector, $expectedData, $inputData)
Definition: CryptTest.php:173
testConstructorException($key, $cipher, $mode, $initVector)
Definition: CryptTest.php:129
testEncrypt($key, $cipher, $mode, $initVector, $inputData, $expectedData)
Definition: CryptTest.php:163