PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/lib/files/storage/wrapper/encryption.php

https://gitlab.com/Red54/core
PHP | 368 lines | 236 code | 56 blank | 76 comment | 13 complexity | 66d1f69356e31f16c4406d95cb5cb6c7 MD5 | raw file
  1. <?php
  2. namespace Test\Files\Storage\Wrapper;
  3. use OC\Files\Storage\Temporary;
  4. use OC\Files\View;
  5. class Encryption extends \Test\Files\Storage\Storage {
  6. /**
  7. * @var Temporary
  8. */
  9. private $sourceStorage;
  10. /**
  11. * @var \OC\Files\Storage\Wrapper\Encryption | \PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $instance;
  14. /**
  15. * @var \OC\Encryption\Keys\Storage | \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $keyStore;
  18. /**
  19. * @var \OC\Encryption\Util | \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $util;
  22. /**
  23. * @var \OC\Encryption\Manager | \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $encryptionManager;
  26. /**
  27. * @var \OCP\Encryption\IEncryptionModule | \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $encryptionModule;
  30. /**
  31. * @var \OC\Encryption\Update | \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $update;
  34. /**
  35. * @var \OC\Files\Cache\Cache | \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $cache;
  38. /**
  39. * @var \OC\Log | \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $logger;
  42. /**
  43. * @var \OC\Encryption\File | \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $file;
  46. /**
  47. * @var \OC\Files\Mount\MountPoint | \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $mount;
  50. /**
  51. * @var \OC\Files\Mount\Manager | \PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $mountManager;
  54. /** @var integer dummy unencrypted size */
  55. private $dummySize = -1;
  56. protected function setUp() {
  57. parent::setUp();
  58. $mockModule = $this->buildMockModule();
  59. $this->encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager')
  60. ->disableOriginalConstructor()
  61. ->setMethods(['getEncryptionModule', 'isEnabled'])
  62. ->getMock();
  63. $this->encryptionManager->expects($this->any())
  64. ->method('getEncryptionModule')
  65. ->willReturn($mockModule);
  66. $config = $this->getMockBuilder('\OCP\IConfig')
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $groupManager = $this->getMockBuilder('\OC\Group\Manager')
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->util = $this->getMock('\OC\Encryption\Util', ['getUidAndFilename', 'isFile', 'isExcluded'], [new View(), new \OC\User\Manager(), $groupManager, $config]);
  73. $this->util->expects($this->any())
  74. ->method('getUidAndFilename')
  75. ->willReturnCallback(function ($path) {
  76. return ['user1', $path];
  77. });
  78. $this->file = $this->getMockBuilder('\OC\Encryption\File')
  79. ->disableOriginalConstructor()
  80. ->setMethods(['getAccessList'])
  81. ->getMock();
  82. $this->file->expects($this->any())->method('getAccessList')->willReturn([]);
  83. $this->logger = $this->getMock('\OC\Log');
  84. $this->sourceStorage = new Temporary(array());
  85. $this->keyStore = $this->getMockBuilder('\OC\Encryption\Keys\Storage')
  86. ->disableOriginalConstructor()->getMock();
  87. $this->update = $this->getMockBuilder('\OC\Encryption\Update')
  88. ->disableOriginalConstructor()->getMock();
  89. $this->mount = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
  90. ->disableOriginalConstructor()
  91. ->setMethods(['getOption'])
  92. ->getMock();
  93. $this->mount->expects($this->any())->method('getOption')->willReturn(true);
  94. $this->cache = $this->getMockBuilder('\OC\Files\Cache\Cache')
  95. ->disableOriginalConstructor()->getMock();
  96. $this->cache->expects($this->any())
  97. ->method('get')
  98. ->willReturnCallback(function($path) {return ['encrypted' => false, 'path' => $path];});
  99. $this->mountManager = $this->getMockBuilder('\OC\Files\Mount\Manager')
  100. ->disableOriginalConstructor()->getMock();
  101. $this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
  102. ->setConstructorArgs(
  103. [
  104. [
  105. 'storage' => $this->sourceStorage,
  106. 'root' => 'foo',
  107. 'mountPoint' => '/',
  108. 'mount' => $this->mount
  109. ],
  110. $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager
  111. ]
  112. )
  113. ->setMethods(['getMetaData', 'getCache', 'getEncryptionModule'])
  114. ->getMock();
  115. $this->instance->expects($this->any())
  116. ->method('getMetaData')
  117. ->willReturnCallback(function ($path) {
  118. return ['encrypted' => true, 'size' => $this->dummySize, 'path' => $path];
  119. });
  120. $this->instance->expects($this->any())
  121. ->method('getCache')
  122. ->willReturn($this->cache);
  123. $this->instance->expects($this->any())
  124. ->method('getEncryptionModule')
  125. ->willReturn($mockModule);
  126. }
  127. /**
  128. * @return \PHPUnit_Framework_MockObject_MockObject
  129. */
  130. protected function buildMockModule() {
  131. $this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
  132. ->disableOriginalConstructor()
  133. ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable'])
  134. ->getMock();
  135. $this->encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE');
  136. $this->encryptionModule->expects($this->any())->method('getDisplayName')->willReturn('Unit test module');
  137. $this->encryptionModule->expects($this->any())->method('begin')->willReturn([]);
  138. $this->encryptionModule->expects($this->any())->method('end')->willReturn('');
  139. $this->encryptionModule->expects($this->any())->method('encrypt')->willReturnArgument(0);
  140. $this->encryptionModule->expects($this->any())->method('decrypt')->willReturnArgument(0);
  141. $this->encryptionModule->expects($this->any())->method('update')->willReturn(true);
  142. $this->encryptionModule->expects($this->any())->method('shouldEncrypt')->willReturn(true);
  143. $this->encryptionModule->expects($this->any())->method('getUnencryptedBlockSize')->willReturn(8192);
  144. $this->encryptionModule->expects($this->any())->method('isReadable')->willReturn(true);
  145. return $this->encryptionModule;
  146. }
  147. /**
  148. * @dataProvider dataTestCopyAndRename
  149. *
  150. * @param string $source
  151. * @param string $target
  152. * @param $encryptionEnabled
  153. * @param boolean $renameKeysReturn
  154. */
  155. public function testRename($source,
  156. $target,
  157. $encryptionEnabled,
  158. $renameKeysReturn) {
  159. if ($encryptionEnabled) {
  160. $this->keyStore
  161. ->expects($this->once())
  162. ->method('renameKeys')
  163. ->willReturn($renameKeysReturn);
  164. } else {
  165. $this->keyStore
  166. ->expects($this->never())->method('renameKeys');
  167. }
  168. $this->util->expects($this->any())
  169. ->method('isFile')->willReturn(true);
  170. $this->encryptionManager->expects($this->once())
  171. ->method('isEnabled')->willReturn($encryptionEnabled);
  172. $this->instance->mkdir($source);
  173. $this->instance->mkdir(dirname($target));
  174. $this->instance->rename($source, $target);
  175. }
  176. /**
  177. * @dataProvider dataTestCopyAndRename
  178. *
  179. * @param string $source
  180. * @param string $target
  181. * @param $encryptionEnabled
  182. * @param boolean $copyKeysReturn
  183. * @param boolean $shouldUpdate
  184. */
  185. public function testCopyEncryption($source,
  186. $target,
  187. $encryptionEnabled,
  188. $copyKeysReturn,
  189. $shouldUpdate) {
  190. if ($encryptionEnabled) {
  191. $this->keyStore
  192. ->expects($this->once())
  193. ->method('copyKeys')
  194. ->willReturn($copyKeysReturn);
  195. $this->cache->expects($this->once())
  196. ->method('put')
  197. ->with($this->anything(), ['encrypted' => true])
  198. ->willReturn(true);
  199. } else {
  200. $this->cache->expects($this->never())->method('put');
  201. $this->keyStore->expects($this->never())->method('copyKeys');
  202. }
  203. $this->util->expects($this->any())
  204. ->method('isFile')->willReturn(true);
  205. $this->util->expects($this->any())
  206. ->method('isExcluded')->willReturn(false);
  207. $this->encryptionManager->expects($this->once())
  208. ->method('isEnabled')->willReturn($encryptionEnabled);
  209. if ($shouldUpdate) {
  210. $this->update->expects($this->once())
  211. ->method('update');
  212. } else {
  213. $this->update->expects($this->never())
  214. ->method('update');
  215. }
  216. $this->instance->mkdir($source);
  217. $this->instance->mkdir(dirname($target));
  218. $this->instance->copy($source, $target);
  219. if ($encryptionEnabled) {
  220. $this->assertSame($this->dummySize,
  221. $this->instance->filesize($target)
  222. );
  223. }
  224. }
  225. /**
  226. * data provider for testCopyTesting() and dataTestCopyAndRename()
  227. *
  228. * @return array
  229. */
  230. public function dataTestCopyAndRename() {
  231. return array(
  232. array('source', 'target', true, false, false),
  233. array('source', 'target', true, true, false),
  234. array('source', '/subFolder/target', true, false, false),
  235. array('source', '/subFolder/target', true, true, true),
  236. array('source', '/subFolder/target', false, true, false),
  237. );
  238. }
  239. public function testIsLocal() {
  240. $this->encryptionManager->expects($this->once())
  241. ->method('isEnabled')->willReturn(true);
  242. $this->assertFalse($this->instance->isLocal());
  243. }
  244. /**
  245. * @dataProvider dataTestRmdir
  246. *
  247. * @param string $path
  248. * @param boolean $rmdirResult
  249. * @param boolean $isExcluded
  250. * @param boolean $encryptionEnabled
  251. */
  252. public function testRmdir($path, $rmdirResult, $isExcluded, $encryptionEnabled) {
  253. $sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  254. ->disableOriginalConstructor()->getMock();
  255. $util = $this->getMockBuilder('\OC\Encryption\Util')->disableOriginalConstructor()->getMock();
  256. $sourceStorage->expects($this->once())->method('rmdir')->willReturn($rmdirResult);
  257. $util->expects($this->any())->method('isExcluded')-> willReturn($isExcluded);
  258. $this->encryptionManager->expects($this->any())->method('isEnabled')->willReturn($encryptionEnabled);
  259. $encryptionStorage = new \OC\Files\Storage\Wrapper\Encryption(
  260. [
  261. 'storage' => $sourceStorage,
  262. 'root' => 'foo',
  263. 'mountPoint' => '/mountPoint',
  264. 'mount' => $this->mount
  265. ],
  266. $this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update
  267. );
  268. if ($rmdirResult === true && $isExcluded === false && $encryptionEnabled === true) {
  269. $this->keyStore->expects($this->once())->method('deleteAllFileKeys')->with('/mountPoint' . $path);
  270. } else {
  271. $this->keyStore->expects($this->never())->method('deleteAllFileKeys');
  272. }
  273. $encryptionStorage->rmdir($path);
  274. }
  275. public function dataTestRmdir() {
  276. return array(
  277. array('/file.txt', true, true, true),
  278. array('/file.txt', false, true, true),
  279. array('/file.txt', true, false, true),
  280. array('/file.txt', false, false, true),
  281. array('/file.txt', true, true, false),
  282. array('/file.txt', false, true, false),
  283. array('/file.txt', true, false, false),
  284. array('/file.txt', false, false, false),
  285. );
  286. }
  287. /**
  288. * @dataProvider dataTestCopyKeys
  289. *
  290. * @param boolean $excluded
  291. * @param boolean $expected
  292. */
  293. public function testCopyKeys($excluded, $expected) {
  294. $this->util->expects($this->once())
  295. ->method('isExcluded')
  296. ->willReturn($excluded);
  297. if ($excluded) {
  298. $this->keyStore->expects($this->never())->method('copyKeys');
  299. } else {
  300. $this->keyStore->expects($this->once())->method('copyKeys')->willReturn(true);
  301. }
  302. $this->assertSame($expected,
  303. self::invokePrivate($this->instance, 'copyKeys', ['/source', '/target'])
  304. );
  305. }
  306. public function dataTestCopyKeys() {
  307. return array(
  308. array(true, false),
  309. array(false, true),
  310. );
  311. }
  312. }