storage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @copyright (C) 2015 ownCloud, Inc.
  6. *
  7. * @author Bjoern Schiessle <schiessle@owncloud.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. namespace Test\Encryption\Keys;
  23. use OC\Encryption\Keys\Storage;
  24. use Test\TestCase;
  25. class StorageTest extends TestCase {
  26. /** @var Storage */
  27. protected $storage;
  28. /** @var \PHPUnit_Framework_MockObject_MockObject */
  29. protected $util;
  30. /** @var \PHPUnit_Framework_MockObject_MockObject */
  31. protected $view;
  32. public function setUp() {
  33. parent::setUp();
  34. $this->util = $this->getMockBuilder('OC\Encryption\Util')
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->view = $this->getMockBuilder('OC\Files\View')
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->storage = new Storage('encModule', $this->view, $this->util);
  41. }
  42. public function testSetFileKey() {
  43. $this->util->expects($this->any())
  44. ->method('getUidAndFilename')
  45. ->willReturn(array('user1', '/files/foo.txt'));
  46. $this->util->expects($this->any())
  47. ->method('stripPartialFileExtension')
  48. ->willReturnArgument(0);
  49. $this->util->expects($this->any())
  50. ->method('isSystemWideMountPoint')
  51. ->willReturn(false);
  52. $this->view->expects($this->once())
  53. ->method('file_put_contents')
  54. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'),
  55. $this->equalTo('key'))
  56. ->willReturn(strlen('key'));
  57. $this->assertTrue(
  58. $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key')
  59. );
  60. }
  61. public function testGetFileKey() {
  62. $this->util->expects($this->any())
  63. ->method('getUidAndFilename')
  64. ->willReturn(array('user1', '/files/foo.txt'));
  65. $this->util->expects($this->any())
  66. ->method('stripPartialFileExtension')
  67. ->willReturnArgument(0);
  68. $this->util->expects($this->any())
  69. ->method('isSystemWideMountPoint')
  70. ->willReturn(false);
  71. $this->view->expects($this->once())
  72. ->method('file_get_contents')
  73. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  74. ->willReturn('key');
  75. $this->view->expects($this->once())
  76. ->method('file_exists')
  77. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  78. ->willReturn(true);
  79. $this->assertSame('key',
  80. $this->storage->getFileKey('user1/files/foo.txt', 'fileKey')
  81. );
  82. }
  83. public function testSetFileKeySystemWide() {
  84. $this->util->expects($this->any())
  85. ->method('getUidAndFilename')
  86. ->willReturn(array('user1', '/files/foo.txt'));
  87. $this->util->expects($this->any())
  88. ->method('isSystemWideMountPoint')
  89. ->willReturn(true);
  90. $this->util->expects($this->any())
  91. ->method('stripPartialFileExtension')
  92. ->willReturnArgument(0);
  93. $this->view->expects($this->once())
  94. ->method('file_put_contents')
  95. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'),
  96. $this->equalTo('key'))
  97. ->willReturn(strlen('key'));
  98. $this->assertTrue(
  99. $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key')
  100. );
  101. }
  102. public function testGetFileKeySystemWide() {
  103. $this->util->expects($this->any())
  104. ->method('getUidAndFilename')
  105. ->willReturn(array('user1', '/files/foo.txt'));
  106. $this->util->expects($this->any())
  107. ->method('stripPartialFileExtension')
  108. ->willReturnArgument(0);
  109. $this->util->expects($this->any())
  110. ->method('isSystemWideMountPoint')
  111. ->willReturn(true);
  112. $this->view->expects($this->once())
  113. ->method('file_get_contents')
  114. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  115. ->willReturn('key');
  116. $this->view->expects($this->once())
  117. ->method('file_exists')
  118. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  119. ->willReturn(true);
  120. $this->assertSame('key',
  121. $this->storage->getFileKey('user1/files/foo.txt', 'fileKey')
  122. );
  123. }
  124. public function testSetSystemUserKey() {
  125. $this->view->expects($this->once())
  126. ->method('file_put_contents')
  127. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'),
  128. $this->equalTo('key'))
  129. ->willReturn(strlen('key'));
  130. $this->assertTrue(
  131. $this->storage->setSystemUserKey('shareKey_56884', 'key')
  132. );
  133. }
  134. public function testSetUserKey() {
  135. $this->view->expects($this->once())
  136. ->method('file_put_contents')
  137. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'),
  138. $this->equalTo('key'))
  139. ->willReturn(strlen('key'));
  140. $this->assertTrue(
  141. $this->storage->setUserKey('user1', 'publicKey', 'key')
  142. );
  143. }
  144. public function testGetSystemUserKey() {
  145. $this->view->expects($this->once())
  146. ->method('file_get_contents')
  147. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  148. ->willReturn('key');
  149. $this->view->expects($this->once())
  150. ->method('file_exists')
  151. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  152. ->willReturn(true);
  153. $this->assertSame('key',
  154. $this->storage->getSystemUserKey('shareKey_56884')
  155. );
  156. }
  157. public function testGetUserKey() {
  158. $this->view->expects($this->once())
  159. ->method('file_get_contents')
  160. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  161. ->willReturn('key');
  162. $this->view->expects($this->once())
  163. ->method('file_exists')
  164. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  165. ->willReturn(true);
  166. $this->assertSame('key',
  167. $this->storage->getUserKey('user1', 'publicKey')
  168. );
  169. }
  170. public function testDeleteUserKey() {
  171. $this->view->expects($this->once())
  172. ->method('unlink')
  173. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  174. ->willReturn(true);
  175. $this->assertTrue(
  176. $this->storage->deleteUserKey('user1', 'publicKey')
  177. );
  178. }
  179. public function testDeleteSystemUserKey() {
  180. $this->view->expects($this->once())
  181. ->method('unlink')
  182. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  183. ->willReturn(true);
  184. $this->assertTrue(
  185. $this->storage->deleteSystemUserKey('shareKey_56884')
  186. );
  187. }
  188. public function testDeleteFileKeySystemWide() {
  189. $this->util->expects($this->any())
  190. ->method('getUidAndFilename')
  191. ->willReturn(array('user1', '/files/foo.txt'));
  192. $this->util->expects($this->any())
  193. ->method('stripPartialFileExtension')
  194. ->willReturnArgument(0);
  195. $this->util->expects($this->any())
  196. ->method('isSystemWideMountPoint')
  197. ->willReturn(true);
  198. $this->view->expects($this->once())
  199. ->method('unlink')
  200. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  201. ->willReturn(true);
  202. $this->assertTrue(
  203. $this->storage->deleteFileKey('user1/files/foo.txt', 'fileKey')
  204. );
  205. }
  206. public function testDeleteFileKey() {
  207. $this->util->expects($this->any())
  208. ->method('getUidAndFilename')
  209. ->willReturn(array('user1', '/files/foo.txt'));
  210. $this->util->expects($this->any())
  211. ->method('stripPartialFileExtension')
  212. ->willReturnArgument(0);
  213. $this->util->expects($this->any())
  214. ->method('isSystemWideMountPoint')
  215. ->willReturn(false);
  216. $this->view->expects($this->once())
  217. ->method('unlink')
  218. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  219. ->willReturn(true);
  220. $this->assertTrue(
  221. $this->storage->deleteFileKey('user1/files/foo.txt', 'fileKey')
  222. );
  223. }
  224. /**
  225. * @dataProvider dataProviderCopyRename
  226. */
  227. public function testRenameKeys($source, $target, $systemWideMount, $expectedSource, $expectedTarget) {
  228. $this->view->expects($this->any())
  229. ->method('file_exists')
  230. ->willReturn(true);
  231. $this->view->expects($this->any())
  232. ->method('is_dir')
  233. ->willReturn(true);
  234. $this->view->expects($this->once())
  235. ->method('rename')
  236. ->with(
  237. $this->equalTo($expectedSource),
  238. $this->equalTo($expectedTarget))
  239. ->willReturn(true);
  240. $this->util->expects($this->any())
  241. ->method('getUidAndFilename')
  242. ->will($this->returnCallback(array($this, 'getUidAndFilenameCallback')));
  243. $this->util->expects($this->any())
  244. ->method('isSystemWideMountPoint')
  245. ->willReturn($systemWideMount);
  246. $this->storage->renameKeys($source, $target);
  247. }
  248. /**
  249. * @dataProvider dataProviderCopyRename
  250. */
  251. public function testCopyKeys($source, $target, $systemWideMount, $expectedSource, $expectedTarget) {
  252. $this->view->expects($this->any())
  253. ->method('file_exists')
  254. ->willReturn(true);
  255. $this->view->expects($this->any())
  256. ->method('is_dir')
  257. ->willReturn(true);
  258. $this->view->expects($this->once())
  259. ->method('copy')
  260. ->with(
  261. $this->equalTo($expectedSource),
  262. $this->equalTo($expectedTarget))
  263. ->willReturn(true);
  264. $this->util->expects($this->any())
  265. ->method('getUidAndFilename')
  266. ->will($this->returnCallback(array($this, 'getUidAndFilenameCallback')));
  267. $this->util->expects($this->any())
  268. ->method('isSystemWideMountPoint')
  269. ->willReturn($systemWideMount);
  270. $this->storage->copyKeys($source, $target);
  271. }
  272. public function getUidAndFilenameCallback() {
  273. $args = func_get_args();
  274. $path = $args[0];
  275. $parts = explode('/', $path);
  276. return array($parts[1], '/' . implode('/', array_slice($parts, 2)));
  277. }
  278. public function dataProviderCopyRename() {
  279. return array(
  280. array('/user1/files/foo.txt', '/user1/files/bar.txt', false,
  281. '/user1/files_encryption/keys/files/foo.txt/', '/user1/files_encryption/keys/files/bar.txt/'),
  282. array('/user1/files/foo/foo.txt', '/user1/files/bar.txt', false,
  283. '/user1/files_encryption/keys/files/foo/foo.txt/', '/user1/files_encryption/keys/files/bar.txt/'),
  284. array('/user1/files/foo.txt', '/user1/files/foo/bar.txt', false,
  285. '/user1/files_encryption/keys/files/foo.txt/', '/user1/files_encryption/keys/files/foo/bar.txt/'),
  286. array('/user1/files/foo.txt', '/user1/files/foo/bar.txt', true,
  287. '/files_encryption/keys/files/foo.txt/', '/files_encryption/keys/files/foo/bar.txt/'),
  288. );
  289. }
  290. public function testKeySetPreparation() {
  291. $this->view->expects($this->any())
  292. ->method('file_exists')
  293. ->willReturn(false);
  294. $this->view->expects($this->any())
  295. ->method('is_dir')
  296. ->willReturn(false);
  297. $this->view->expects($this->any())
  298. ->method('mkdir')
  299. ->will($this->returnCallback(array($this, 'mkdirCallback')));
  300. $this->mkdirStack = array(
  301. '/user1/files_encryption/keys/foo',
  302. '/user1/files_encryption/keys',
  303. '/user1/files_encryption',
  304. '/user1');
  305. \Test_Helper::invokePrivate($this->storage, 'keySetPreparation', array('/user1/files_encryption/keys/foo'));
  306. }
  307. public function mkdirCallback() {
  308. $args = func_get_args();
  309. $expected = array_pop($this->mkdirStack);
  310. $this->assertSame($expected, $args[0]);
  311. }
  312. }