FixEncryptedVersionTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * @author Sujith Haridasan <sharidasan@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2019, ownCloud GmbH
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Encryption\Tests\Command;
  22. use OC\Files\View;
  23. use OCA\Encryption\Command\FixEncryptedVersion;
  24. use OCA\Encryption\Util;
  25. use Psr\Log\LoggerInterface;
  26. use Symfony\Component\Console\Tester\CommandTester;
  27. use Test\TestCase;
  28. use Test\Traits\EncryptionTrait;
  29. use Test\Traits\MountProviderTrait;
  30. use Test\Traits\UserTrait;
  31. /**
  32. * Class FixEncryptedVersionTest
  33. *
  34. * @group DB
  35. * @package OCA\Encryption\Tests\Command
  36. */
  37. class FixEncryptedVersionTest extends TestCase {
  38. use MountProviderTrait;
  39. use EncryptionTrait;
  40. use UserTrait;
  41. private $userId;
  42. /** @var FixEncryptedVersion */
  43. private $fixEncryptedVersion;
  44. /** @var CommandTester */
  45. private $commandTester;
  46. /** @var Util|\PHPUnit\Framework\MockObject\MockObject */
  47. protected $util;
  48. public function setUp(): void {
  49. parent::setUp();
  50. \OC::$server->getConfig()->setAppValue('encryption', 'useMasterKey', '1');
  51. $this->util = $this->getMockBuilder(Util::class)
  52. ->disableOriginalConstructor()->getMock();
  53. $this->userId = $this->getUniqueId('user_');
  54. $this->createUser($this->userId, 'foo12345678');
  55. $tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
  56. $this->registerMount($this->userId, '\OC\Files\Storage\Local', '/' . $this->userId, ['datadir' => $tmpFolder]);
  57. $this->setupForUser($this->userId, 'foo12345678');
  58. $this->loginWithEncryption($this->userId);
  59. $this->fixEncryptedVersion = new FixEncryptedVersion(
  60. \OC::$server->getConfig(),
  61. \OC::$server->get(LoggerInterface::class),
  62. \OC::$server->getRootFolder(),
  63. \OC::$server->getUserManager(),
  64. $this->util,
  65. new View('/')
  66. );
  67. $this->commandTester = new CommandTester($this->fixEncryptedVersion);
  68. $this->assertTrue(\OC::$server->getEncryptionManager()->isEnabled());
  69. $this->assertTrue(\OC::$server->getEncryptionManager()->isReady());
  70. $this->assertTrue(\OC::$server->getEncryptionManager()->isReadyForUser($this->userId));
  71. }
  72. /**
  73. * In this test the encrypted version of the file is less than the original value
  74. * but greater than zero
  75. */
  76. public function testEncryptedVersionLessThanOriginalValue() {
  77. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  78. ->willReturn(true);
  79. $view = new View("/" . $this->userId . "/files");
  80. $view->touch('hello.txt');
  81. $view->touch('world.txt');
  82. $view->touch('foo.txt');
  83. $view->file_put_contents('hello.txt', 'a test string for hello');
  84. $view->file_put_contents('hello.txt', 'Yet another value');
  85. $view->file_put_contents('hello.txt', 'Lets modify again1');
  86. $view->file_put_contents('hello.txt', 'Lets modify again2');
  87. $view->file_put_contents('hello.txt', 'Lets modify again3');
  88. $view->file_put_contents('world.txt', 'a test string for world');
  89. $view->file_put_contents('world.txt', 'a test string for world');
  90. $view->file_put_contents('world.txt', 'a test string for world');
  91. $view->file_put_contents('world.txt', 'a test string for world');
  92. $view->file_put_contents('foo.txt', 'a foo test');
  93. $fileInfo1 = $view->getFileInfo('hello.txt');
  94. $storage1 = $fileInfo1->getStorage();
  95. $cache1 = $storage1->getCache();
  96. $fileCache1 = $cache1->get($fileInfo1->getId());
  97. //Now change the encrypted version to two
  98. $cacheInfo = ['encryptedVersion' => 2, 'encrypted' => 2];
  99. $cache1->put($fileCache1->getPath(), $cacheInfo);
  100. $fileInfo2 = $view->getFileInfo('world.txt');
  101. $storage2 = $fileInfo2->getStorage();
  102. $cache2 = $storage2->getCache();
  103. $filecache2 = $cache2->get($fileInfo2->getId());
  104. //Now change the encrypted version to 1
  105. $cacheInfo = ['encryptedVersion' => 1, 'encrypted' => 1];
  106. $cache2->put($filecache2->getPath(), $cacheInfo);
  107. $this->commandTester->execute([
  108. 'user' => $this->userId
  109. ]);
  110. $output = $this->commandTester->getDisplay();
  111. $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/foo.txt\"
  112. The file \"/$this->userId/files/foo.txt\" is: OK", $output);
  113. $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/hello.txt\"
  114. Attempting to fix the path: \"/$this->userId/files/hello.txt\"
  115. Decrement the encrypted version to 1
  116. Increment the encrypted version to 3
  117. Increment the encrypted version to 4
  118. Increment the encrypted version to 5
  119. The file \"/$this->userId/files/hello.txt\" is: OK
  120. Fixed the file: \"/$this->userId/files/hello.txt\" with version 5", $output);
  121. $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/world.txt\"
  122. Attempting to fix the path: \"/$this->userId/files/world.txt\"
  123. Increment the encrypted version to 2
  124. Increment the encrypted version to 3
  125. Increment the encrypted version to 4
  126. The file \"/$this->userId/files/world.txt\" is: OK
  127. Fixed the file: \"/$this->userId/files/world.txt\" with version 4", $output);
  128. }
  129. /**
  130. * In this test the encrypted version of the file is greater than the original value
  131. * but greater than zero
  132. */
  133. public function testEncryptedVersionGreaterThanOriginalValue() {
  134. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  135. ->willReturn(true);
  136. $view = new View("/" . $this->userId . "/files");
  137. $view->touch('hello.txt');
  138. $view->touch('world.txt');
  139. $view->touch('foo.txt');
  140. $view->file_put_contents('hello.txt', 'a test string for hello');
  141. $view->file_put_contents('hello.txt', 'Lets modify again2');
  142. $view->file_put_contents('hello.txt', 'Lets modify again3');
  143. $view->file_put_contents('world.txt', 'a test string for world');
  144. $view->file_put_contents('world.txt', 'a test string for world');
  145. $view->file_put_contents('world.txt', 'a test string for world');
  146. $view->file_put_contents('world.txt', 'a test string for world');
  147. $view->file_put_contents('foo.txt', 'a foo test');
  148. $fileInfo1 = $view->getFileInfo('hello.txt');
  149. $storage1 = $fileInfo1->getStorage();
  150. $cache1 = $storage1->getCache();
  151. $fileCache1 = $cache1->get($fileInfo1->getId());
  152. //Now change the encrypted version to fifteen
  153. $cacheInfo = ['encryptedVersion' => 5, 'encrypted' => 5];
  154. $cache1->put($fileCache1->getPath(), $cacheInfo);
  155. $fileInfo2 = $view->getFileInfo('world.txt');
  156. $storage2 = $fileInfo2->getStorage();
  157. $cache2 = $storage2->getCache();
  158. $filecache2 = $cache2->get($fileInfo2->getId());
  159. //Now change the encrypted version to 1
  160. $cacheInfo = ['encryptedVersion' => 6, 'encrypted' => 6];
  161. $cache2->put($filecache2->getPath(), $cacheInfo);
  162. $this->commandTester->execute([
  163. 'user' => $this->userId
  164. ]);
  165. $output = $this->commandTester->getDisplay();
  166. $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/foo.txt\"
  167. The file \"/$this->userId/files/foo.txt\" is: OK", $output);
  168. $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/hello.txt\"
  169. Attempting to fix the path: \"/$this->userId/files/hello.txt\"
  170. Decrement the encrypted version to 4
  171. Decrement the encrypted version to 3
  172. The file \"/$this->userId/files/hello.txt\" is: OK
  173. Fixed the file: \"/$this->userId/files/hello.txt\" with version 3", $output);
  174. $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/world.txt\"
  175. Attempting to fix the path: \"/$this->userId/files/world.txt\"
  176. Decrement the encrypted version to 5
  177. Decrement the encrypted version to 4
  178. The file \"/$this->userId/files/world.txt\" is: OK
  179. Fixed the file: \"/$this->userId/files/world.txt\" with version 4", $output);
  180. }
  181. public function testVersionIsRestoredToOriginalIfNoFixIsFound() {
  182. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  183. ->willReturn(true);
  184. $view = new View("/" . $this->userId . "/files");
  185. $view->touch('bar.txt');
  186. for ($i = 0; $i < 40; $i++) {
  187. $view->file_put_contents('bar.txt', 'a test string for hello ' . $i);
  188. }
  189. $fileInfo = $view->getFileInfo('bar.txt');
  190. $storage = $fileInfo->getStorage();
  191. $cache = $storage->getCache();
  192. $fileCache = $cache->get($fileInfo->getId());
  193. $cacheInfo = ['encryptedVersion' => 15, 'encrypted' => 15];
  194. $cache->put($fileCache->getPath(), $cacheInfo);
  195. $this->commandTester->execute([
  196. 'user' => $this->userId
  197. ]);
  198. $cacheInfo = $cache->get($fileInfo->getId());
  199. $encryptedVersion = $cacheInfo["encryptedVersion"];
  200. $this->assertEquals(15, $encryptedVersion);
  201. }
  202. public function testRepairUnencryptedFileWhenVersionIsSet() {
  203. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  204. ->willReturn(true);
  205. $view = new View("/" . $this->userId . "/files");
  206. // create a file, it's encrypted and also the version is set in the database
  207. $view->touch('hello.txt');
  208. $fileInfo1 = $view->getFileInfo('hello.txt');
  209. $storage1 = $fileInfo1->getStorage();
  210. $cache1 = $storage1->getCache();
  211. $fileCache1 = $cache1->get($fileInfo1->getId());
  212. // Now change the encrypted version
  213. $cacheInfo = ['encryptedVersion' => 1, 'encrypted' => 1];
  214. $cache1->put($fileCache1->getPath(), $cacheInfo);
  215. $absPath = $storage1->getSourcePath('').$fileInfo1->getInternalPath();
  216. // create unencrypted file on disk, the version stays
  217. file_put_contents($absPath, 'hello contents');
  218. $this->commandTester->execute([
  219. 'user' => $this->userId
  220. ]);
  221. $output = $this->commandTester->getDisplay();
  222. $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/hello.txt\"
  223. Attempting to fix the path: \"/$this->userId/files/hello.txt\"
  224. Set the encrypted version to 0 (unencrypted)
  225. The file \"/$this->userId/files/hello.txt\" is: OK
  226. Fixed the file: \"/$this->userId/files/hello.txt\" with version 0 (unencrypted)", $output);
  227. // the file can be decrypted
  228. $this->assertEquals('hello contents', $view->file_get_contents('hello.txt'));
  229. }
  230. /**
  231. * Test commands with a file path
  232. */
  233. public function testExecuteWithFilePathOption() {
  234. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  235. ->willReturn(true);
  236. $view = new View("/" . $this->userId . "/files");
  237. $view->touch('hello.txt');
  238. $view->touch('world.txt');
  239. $this->commandTester->execute([
  240. 'user' => $this->userId,
  241. '--path' => "/hello.txt"
  242. ]);
  243. $output = $this->commandTester->getDisplay();
  244. $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/hello.txt\"
  245. The file \"/$this->userId/files/hello.txt\" is: OK", $output);
  246. $this->assertStringNotContainsString('world.txt', $output);
  247. }
  248. /**
  249. * Test commands with a directory path
  250. */
  251. public function testExecuteWithDirectoryPathOption() {
  252. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  253. ->willReturn(true);
  254. $view = new View("/" . $this->userId . "/files");
  255. $view->mkdir('sub');
  256. $view->touch('sub/hello.txt');
  257. $view->touch('world.txt');
  258. $this->commandTester->execute([
  259. 'user' => $this->userId,
  260. '--path' => "/sub"
  261. ]);
  262. $output = $this->commandTester->getDisplay();
  263. $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/sub/hello.txt\"
  264. The file \"/$this->userId/files/sub/hello.txt\" is: OK", $output);
  265. $this->assertStringNotContainsString('world.txt', $output);
  266. }
  267. public function testExecuteWithNoUser() {
  268. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  269. ->willReturn(true);
  270. $this->commandTester->execute([
  271. 'user' => null,
  272. '--path' => "/"
  273. ]);
  274. $output = $this->commandTester->getDisplay();
  275. $this->assertStringContainsString('Either a user id or --all needs to be provided', $output);
  276. }
  277. public function testExecuteWithBadUser() {
  278. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  279. ->willReturn(true);
  280. $this->commandTester->execute([
  281. 'user' => 'nonexisting',
  282. '--path' => "/"
  283. ]);
  284. $output = $this->commandTester->getDisplay();
  285. $this->assertStringContainsString('does not exist', $output);
  286. }
  287. /**
  288. * Test commands with a directory path
  289. */
  290. public function testExecuteWithNonExistentPath() {
  291. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  292. ->willReturn(true);
  293. $this->commandTester->execute([
  294. 'user' => $this->userId,
  295. '--path' => '/non-exist'
  296. ]);
  297. $output = $this->commandTester->getDisplay();
  298. $this->assertStringContainsString('Please provide a valid path.', $output);
  299. }
  300. /**
  301. * Test commands without master key
  302. */
  303. public function testExecuteWithNoMasterKey() {
  304. \OC::$server->getConfig()->setAppValue('encryption', 'useMasterKey', '0');
  305. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  306. ->willReturn(false);
  307. $this->commandTester->execute([
  308. 'user' => $this->userId,
  309. ]);
  310. $output = $this->commandTester->getDisplay();
  311. $this->assertStringContainsString('only works with master key', $output);
  312. }
  313. }