Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
McryptTest.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
13 
14 class McryptTest extends \PHPUnit\Framework\TestCase
15 {
16  private $key;
17 
18  private static $cipherInfo;
19 
20  private const SUPPORTED_CIPHER_MODE_COMBINATIONS = [
21  MCRYPT_BLOWFISH => [MCRYPT_MODE_ECB],
22  MCRYPT_RIJNDAEL_128 => [MCRYPT_MODE_ECB],
23  MCRYPT_RIJNDAEL_256 => [MCRYPT_MODE_CBC],
24  ];
25 
26  protected function setUp()
27  {
28  $this->key = substr(__CLASS__, -32, 32);
29  }
30 
31  protected function getRandomString(int $length): string
32  {
33  $result = '';
34 
35  do {
36  $result .= sha1(microtime());
37  } while (strlen($result) < $length);
38 
39  return substr($result, -$length);
40  }
41 
42  private function requireCipherInfo()
43  {
44  $filename = __DIR__ . '/../Crypt/_files/_cipher_info.php';
45 
46  if (!self::$cipherInfo) {
47  self::$cipherInfo = include $filename;
48  }
49  }
50 
51  private function getKeySize(string $cipherName, string $modeName): int
52  {
53  $this->requireCipherInfo();
54  return self::$cipherInfo[$cipherName][$modeName]['key_size'];
55  }
56 
57  private function getInitVectorSize(string $cipherName, string $modeName): int
58  {
59  $this->requireCipherInfo();
60  return self::$cipherInfo[$cipherName][$modeName]['iv_size'];
61  }
62 
63  public function getCipherModeCombinations(): array
64  {
65  $result = [];
66  foreach (self::SUPPORTED_CIPHER_MODE_COMBINATIONS as $cipher => $modes) {
68  foreach ($modes as $mode) {
69  $result[$cipher . '-' . $mode] = [$cipher, $mode];
70  }
71  }
72  return $result;
73  }
74 
78  public function testConstructor(string $cipher, string $mode)
79  {
80  /* Generate random init vector */
81  $initVector = $this->getRandomString($this->getInitVectorSize($cipher, $mode));
82 
83  $crypt = new \Magento\Framework\Encryption\Adapter\Mcrypt($this->key, $cipher, $mode, $initVector);
84 
85  $this->assertEquals($cipher, $crypt->getCipher());
86  $this->assertEquals($mode, $crypt->getMode());
87  $this->assertEquals($initVector, $crypt->getInitVector());
88  }
89 
90  public function getConstructorExceptionData(): array
91  {
92  $key = substr(__CLASS__, -32, 32);
93  $result = [];
94  foreach (self::SUPPORTED_CIPHER_MODE_COMBINATIONS as $cipher => $modes) {
96  foreach ($modes as $mode) {
97  $tooLongKey = str_repeat('-', $this->getKeySize($cipher, $mode) + 1);
98  $tooShortInitVector = str_repeat('-', $this->getInitVectorSize($cipher, $mode) - 1);
99  $tooLongInitVector = str_repeat('-', $this->getInitVectorSize($cipher, $mode) + 1);
100  $result['tooLongKey-' . $cipher . '-' . $mode . '-false'] = [$tooLongKey, $cipher, $mode, false];
101  $keyPrefix = 'key-' . $cipher . '-' . $mode;
102  $result[$keyPrefix . '-tooShortInitVector'] = [$key, $cipher, $mode, $tooShortInitVector];
103  $result[$keyPrefix . '-tooLongInitVector'] = [$key, $cipher, $mode, $tooLongInitVector];
104  }
105  }
106  return $result;
107  }
108 
113  public function testConstructorException(string $key, string $cipher, string $mode, ?string $initVector = null)
114  {
115  new \Magento\Framework\Encryption\Adapter\Mcrypt($key, $cipher, $mode, $initVector);
116  }
117 
118  public function testConstructorDefaults()
119  {
120  $cryptExpected = new \Magento\Framework\Encryption\Adapter\Mcrypt(
121  $this->key,
122  MCRYPT_BLOWFISH,
123  MCRYPT_MODE_ECB,
124  null
125  );
126  $cryptActual = new \Magento\Framework\Encryption\Adapter\Mcrypt($this->key);
127 
128  $this->assertEquals($cryptExpected->getCipher(), $cryptActual->getCipher());
129  $this->assertEquals($cryptExpected->getMode(), $cryptActual->getMode());
130  $this->assertEquals($cryptExpected->getInitVector(), $cryptActual->getInitVector());
131  }
132 
133  public function getCryptData(): array
134  {
135  $fixturesFilename = __DIR__ . '/../Crypt/_files/_crypt_fixtures.php';
136 
137  $result = include $fixturesFilename;
138  /* Restore encoded string back to binary */
139  foreach ($result as &$cryptParams) {
140  $cryptParams[5] = base64_decode($cryptParams[5]);
141  }
142  unset($cryptParams);
143 
144  return $result;
145  }
146 
150  public function testDecrypt(
151  string $key,
152  string $cipher,
153  string $mode,
154  ?string $initVector,
155  string $expectedData,
156  string $inputData
157  ) {
158  $crypt = new \Magento\Framework\Encryption\Adapter\Mcrypt($key, $cipher, $mode, $initVector);
159  $actualData = $crypt->decrypt($inputData);
160  $this->assertEquals($expectedData, $actualData);
161  }
162 
166  public function testInitVectorNone(string $cipher, string $mode)
167  {
168  $crypt = new \Magento\Framework\Encryption\Adapter\Mcrypt(
169  $this->key,
170  $cipher,
171  $mode,
172  null
173  );
174  $actualInitVector = $crypt->getInitVector();
175 
176  $expectedInitVector = str_repeat("\0", $this->getInitVectorSize($cipher, $mode));
177  $this->assertEquals($expectedInitVector, $actualInitVector);
178  }
179 }
$initVector
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
testDecrypt(string $key, string $cipher, string $mode, ?string $initVector, string $expectedData, string $inputData)
Definition: McryptTest.php:150
testConstructorException(string $key, string $cipher, string $mode, ?string $initVector=null)
Definition: McryptTest.php:113
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15