CryptographerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Test\Unit\Model;
  7. use Magento\Analytics\Model\AnalyticsToken;
  8. use Magento\Analytics\Model\Cryptographer;
  9. use Magento\Analytics\Model\EncodedContext;
  10. use Magento\Analytics\Model\EncodedContextFactory;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  12. class CryptographerTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var AnalyticsToken|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $analyticsTokenMock;
  18. /**
  19. * @var EncodedContextFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $encodedContextFactoryMock;
  22. /**
  23. * @var EncodedContext|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $encodedContextMock;
  26. /**
  27. * @var ObjectManagerHelper
  28. */
  29. private $objectManagerHelper;
  30. /**
  31. * @var Cryptographer
  32. */
  33. private $cryptographer;
  34. /**
  35. * @var string
  36. */
  37. private $key;
  38. /**
  39. * @var array
  40. */
  41. private $initializationVectors;
  42. /**
  43. * @var
  44. */
  45. private $source;
  46. /**
  47. * @var string
  48. */
  49. private $cipherMethod = 'AES-256-CBC';
  50. /**
  51. * @return void
  52. */
  53. protected function setUp()
  54. {
  55. $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->encodedContextFactoryMock = $this->getMockBuilder(EncodedContextFactory::class)
  59. ->setMethods(['create'])
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->encodedContextMock = $this->getMockBuilder(EncodedContext::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->key = '';
  66. $this->source = '';
  67. $this->initializationVectors = [];
  68. $this->objectManagerHelper = new ObjectManagerHelper($this);
  69. $this->cryptographer = $this->objectManagerHelper->getObject(
  70. Cryptographer::class,
  71. [
  72. 'analyticsToken' => $this->analyticsTokenMock,
  73. 'encodedContextFactory' => $this->encodedContextFactoryMock,
  74. 'cipherMethod' => $this->cipherMethod,
  75. ]
  76. );
  77. }
  78. /**
  79. * @return void
  80. */
  81. public function testEncode()
  82. {
  83. $token = 'some-token-value';
  84. $this->source = 'Some text';
  85. $this->key = hash('sha256', $token);
  86. $checkEncodedContext = function ($parameters) {
  87. $emptyRequiredParameters =
  88. array_diff(['content', 'initializationVector'], array_keys(array_filter($parameters)));
  89. if ($emptyRequiredParameters) {
  90. return false;
  91. }
  92. $encryptedData = openssl_encrypt(
  93. $this->source,
  94. $this->cipherMethod,
  95. $this->key,
  96. OPENSSL_RAW_DATA,
  97. $parameters['initializationVector']
  98. );
  99. return ($encryptedData === $parameters['content']);
  100. };
  101. $this->analyticsTokenMock
  102. ->expects($this->once())
  103. ->method('getToken')
  104. ->with()
  105. ->willReturn($token);
  106. $this->encodedContextFactoryMock
  107. ->expects($this->once())
  108. ->method('create')
  109. ->with($this->callback($checkEncodedContext))
  110. ->willReturn($this->encodedContextMock);
  111. $this->assertSame($this->encodedContextMock, $this->cryptographer->encode($this->source));
  112. }
  113. /**
  114. * @return void
  115. */
  116. public function testEncodeUniqueInitializationVector()
  117. {
  118. $this->source = 'Some text';
  119. $token = 'some-token-value';
  120. $registerInitializationVector = function ($parameters) {
  121. if (empty($parameters['initializationVector'])) {
  122. return false;
  123. }
  124. $this->initializationVectors[] = $parameters['initializationVector'];
  125. return true;
  126. };
  127. $this->analyticsTokenMock
  128. ->expects($this->exactly(2))
  129. ->method('getToken')
  130. ->with()
  131. ->willReturn($token);
  132. $this->encodedContextFactoryMock
  133. ->expects($this->exactly(2))
  134. ->method('create')
  135. ->with($this->callback($registerInitializationVector))
  136. ->willReturn($this->encodedContextMock);
  137. $this->assertSame($this->encodedContextMock, $this->cryptographer->encode($this->source));
  138. $this->assertSame($this->encodedContextMock, $this->cryptographer->encode($this->source));
  139. $this->assertCount(2, array_unique($this->initializationVectors));
  140. }
  141. /**
  142. * @expectedException \Magento\Framework\Exception\LocalizedException
  143. * @dataProvider encodeNotValidSourceDataProvider
  144. */
  145. public function testEncodeNotValidSource($source)
  146. {
  147. $this->cryptographer->encode($source);
  148. }
  149. /**
  150. * @return array
  151. */
  152. public function encodeNotValidSourceDataProvider()
  153. {
  154. return [
  155. 'Array' => [[]],
  156. 'Empty string' => [''],
  157. ];
  158. }
  159. /**
  160. * @expectedException \Magento\Framework\Exception\LocalizedException
  161. */
  162. public function testEncodeNotValidCipherMethod()
  163. {
  164. $source = 'Some string';
  165. $cryptographer = $this->objectManagerHelper->getObject(
  166. Cryptographer::class,
  167. [
  168. 'cipherMethod' => 'Wrong-method',
  169. ]
  170. );
  171. $cryptographer->encode($source);
  172. }
  173. /**
  174. * @expectedException \Magento\Framework\Exception\LocalizedException
  175. */
  176. public function testEncodeTokenNotValid()
  177. {
  178. $source = 'Some string';
  179. $this->analyticsTokenMock
  180. ->expects($this->once())
  181. ->method('getToken')
  182. ->with()
  183. ->willReturn(null);
  184. $this->cryptographer->encode($source);
  185. }
  186. }