1
0

FixEncryptedVersionTest.php 13 KB

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