FixEncryptedVersionTest.php 13 KB

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