123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
- /**
- * Copyright (c) 2012 Sam Tuke <samtuke@owncloud.com>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
- */
- require_once __DIR__ . '/../../../lib/base.php';
- require_once __DIR__ . '/../lib/crypt.php';
- require_once __DIR__ . '/../lib/keymanager.php';
- require_once __DIR__ . '/../lib/proxy.php';
- require_once __DIR__ . '/../lib/stream.php';
- require_once __DIR__ . '/../lib/util.php';
- require_once __DIR__ . '/../appinfo/app.php';
- use OCA\Encryption;
- /**
- * Class Test_Encryption_Util
- */
- class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
- const TEST_ENCRYPTION_UTIL_USER1 = "test-util-user1";
- const TEST_ENCRYPTION_UTIL_LEGACY_USER = "test-legacy-user";
- public $userId;
- public $encryptionDir;
- public $publicKeyDir;
- public $pass;
- /**
- * @var OC_FilesystemView
- */
- public $view;
- public $keyfilesPath;
- public $publicKeyPath;
- public $privateKeyPath;
- /**
- * @var \OCA\Encryption\Util
- */
- public $util;
- public $dataShort;
- public $legacyEncryptedData;
- public $legacyEncryptedDataKey;
- public $legacyKey;
- public $stateFilesTrashbin;
- public static function setUpBeforeClass() {
- // reset backend
- \OC_User::clearBackends();
- \OC_User::useBackend('database');
- // Filesystem related hooks
- \OCA\Encryption\Helper::registerFilesystemHooks();
- // clear and register hooks
- \OC_FileProxy::clearProxies();
- \OC_FileProxy::register(new OCA\Encryption\Proxy());
- // create test user
- \Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1, true);
- \Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER, true);
- }
- function setUp() {
- \OC_User::setUserId(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
- $this->userId = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1;
- $this->pass = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1;
- // set content for encrypting / decrypting in tests
- $this->dataUrl = __DIR__ . '/../lib/crypt.php';
- $this->dataShort = 'hats';
- $this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php');
- $this->legacyData = __DIR__ . '/legacy-text.txt';
- $this->legacyEncryptedData = __DIR__ . '/legacy-encrypted-text.txt';
- $this->legacyEncryptedDataKey = __DIR__ . '/encryption.key';
- $this->legacyKey = "30943623843030686906\0\0\0\0";
- $keypair = Encryption\Crypt::createKeypair();
- $this->genPublicKey = $keypair['publicKey'];
- $this->genPrivateKey = $keypair['privateKey'];
- $this->publicKeyDir = '/' . 'public-keys';
- $this->encryptionDir = '/' . $this->userId . '/' . 'files_encryption';
- $this->keyfilesPath = $this->encryptionDir . '/' . 'keyfiles';
- $this->publicKeyPath =
- $this->publicKeyDir . '/' . $this->userId . '.public.key'; // e.g. data/public-keys/admin.public.key
- $this->privateKeyPath =
- $this->encryptionDir . '/' . $this->userId . '.private.key'; // e.g. data/admin/admin.private.key
- $this->view = new \OC_FilesystemView('/');
- $this->util = new Encryption\Util($this->view, $this->userId);
- // remember files_trashbin state
- $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
- // we don't want to tests with app files_trashbin enabled
- \OC_App::disable('files_trashbin');
- }
- function tearDown() {
- // reset app files_trashbin
- if ($this->stateFilesTrashbin) {
- OC_App::enable('files_trashbin');
- }
- else {
- OC_App::disable('files_trashbin');
- }
- }
- public static function tearDownAfterClass() {
- // cleanup test user
- \OC_User::deleteUser(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
- \OC_User::deleteUser(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
- }
- /**
- * @medium
- * @brief test that paths set during User construction are correct
- */
- function testKeyPaths() {
- $util = new Encryption\Util($this->view, $this->userId);
- $this->assertEquals($this->publicKeyDir, $util->getPath('publicKeyDir'));
- $this->assertEquals($this->encryptionDir, $util->getPath('encryptionDir'));
- $this->assertEquals($this->keyfilesPath, $util->getPath('keyfilesPath'));
- $this->assertEquals($this->publicKeyPath, $util->getPath('publicKeyPath'));
- $this->assertEquals($this->privateKeyPath, $util->getPath('privateKeyPath'));
- }
- /**
- * @medium
- * @brief test setup of encryption directories
- */
- function testSetupServerSide() {
- $this->assertEquals(true, $this->util->setupServerSide($this->pass));
- }
- /**
- * @medium
- * @brief test checking whether account is ready for encryption,
- */
- function testUserIsReady() {
- $this->assertEquals(true, $this->util->ready());
- }
- /**
- * @brief test checking whether account is not ready for encryption,
- */
- // function testUserIsNotReady() {
- // $this->view->unlink($this->publicKeyDir);
- //
- // $params['uid'] = $this->userId;
- // $params['password'] = $this->pass;
- // $this->assertFalse(OCA\Encryption\Hooks::login($params));
- //
- // $this->view->unlink($this->privateKeyPath);
- // }
- /**
- * @medium
- * @brief test checking whether account is not ready for encryption,
- */
- function testIsLegacyUser() {
- \Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
- $userView = new \OC_FilesystemView('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
- // Disable encryption proxy to prevent recursive calls
- $proxyStatus = \OC_FileProxy::$enabled;
- \OC_FileProxy::$enabled = false;
- $encryptionKeyContent = file_get_contents($this->legacyEncryptedDataKey);
- $userView->file_put_contents('/encryption.key', $encryptionKeyContent);
- \OC_FileProxy::$enabled = $proxyStatus;
- $params['uid'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
- $params['password'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
- $this->setMigrationStatus(0, \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
- $this->assertTrue(OCA\Encryption\Hooks::login($params));
- $this->assertEquals($this->legacyKey, \OC::$session->get('legacyKey'));
- }
- /**
- * @medium
- */
- function testRecoveryEnabledForUser() {
- $util = new Encryption\Util($this->view, $this->userId);
- // Record the value so we can return it to it's original state later
- $enabled = $util->recoveryEnabledForUser();
- $this->assertTrue($util->setRecoveryForUser(1));
- $this->assertEquals(1, $util->recoveryEnabledForUser());
- $this->assertTrue($util->setRecoveryForUser(0));
- $this->assertEquals(0, $util->recoveryEnabledForUser());
- // Return the setting to it's previous state
- $this->assertTrue($util->setRecoveryForUser($enabled));
- }
- /**
- * @medium
- */
- function testGetUidAndFilename() {
- \OC_User::setUserId(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
- $filename = '/tmp-' . time() . '.test';
- // Disable encryption proxy to prevent recursive calls
- $proxyStatus = \OC_FileProxy::$enabled;
- \OC_FileProxy::$enabled = false;
- $this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort);
- // Re-enable proxy - our work is done
- \OC_FileProxy::$enabled = $proxyStatus;
- $util = new Encryption\Util($this->view, $this->userId);
- list($fileOwnerUid, $file) = $util->getUidAndFilename($filename);
- $this->assertEquals(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1, $fileOwnerUid);
- $this->assertEquals($file, $filename);
- $this->view->unlink($this->userId . '/files/' . $filename);
- }
- /**
- < * @brief Test that data that is read by the crypto stream wrapper
- */
- function testGetFileSize() {
- \Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
- $filename = 'tmp-' . time();
- $externalFilename = '/' . $this->userId . '/files/' . $filename;
- // Test for 0 byte files
- $problematicFileSizeData = "";
- $cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData);
- $this->assertTrue(is_int($cryptedFile));
- $this->assertEquals($this->util->getFileSize($externalFilename), 0);
- $decrypt = $this->view->file_get_contents($externalFilename);
- $this->assertEquals($problematicFileSizeData, $decrypt);
- $this->view->unlink($this->userId . '/files/' . $filename);
- // Test a file with 18377 bytes as in https://github.com/owncloud/mirall/issues/1009
- $problematicFileSizeData = str_pad("", 18377, "abc");
- $cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData);
- $this->assertTrue(is_int($cryptedFile));
- $this->assertEquals($this->util->getFileSize($externalFilename), 18377);
- $decrypt = $this->view->file_get_contents($externalFilename);
- $this->assertEquals($problematicFileSizeData, $decrypt);
- $this->view->unlink($this->userId . '/files/' . $filename);
- }
- /**
- * @medium
- */
- function testIsSharedPath() {
- $sharedPath = '/user1/files/Shared/test';
- $path = '/user1/files/test';
- $this->assertTrue($this->util->isSharedPath($sharedPath));
- $this->assertFalse($this->util->isSharedPath($path));
- }
- /**
- * @large
- */
- function testEncryptLegacyFiles() {
- \Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
- $userView = new \OC_FilesystemView('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
- $view = new \OC_FilesystemView('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER . '/files');
- // Disable encryption proxy to prevent recursive calls
- $proxyStatus = \OC_FileProxy::$enabled;
- \OC_FileProxy::$enabled = false;
- $encryptionKeyContent = file_get_contents($this->legacyEncryptedDataKey);
- $userView->file_put_contents('/encryption.key', $encryptionKeyContent);
- $legacyEncryptedData = file_get_contents($this->legacyEncryptedData);
- $view->mkdir('/test/');
- $view->mkdir('/test/subtest/');
- $view->file_put_contents('/test/subtest/legacy-encrypted-text.txt', $legacyEncryptedData);
- $fileInfo = $view->getFileInfo('/test/subtest/legacy-encrypted-text.txt');
- $fileInfo['encrypted'] = true;
- $view->putFileInfo('/test/subtest/legacy-encrypted-text.txt', $fileInfo);
- \OC_FileProxy::$enabled = $proxyStatus;
- $params['uid'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
- $params['password'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
- $util = new Encryption\Util($this->view, \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
- $this->setMigrationStatus(0, \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
- $this->assertTrue(OCA\Encryption\Hooks::login($params));
- $this->assertEquals($this->legacyKey, \OC::$session->get('legacyKey'));
- $files = $util->findEncFiles('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER . '/files/');
- $this->assertTrue(is_array($files));
- $found = false;
- foreach ($files['encrypted'] as $encryptedFile) {
- if ($encryptedFile['name'] === 'legacy-encrypted-text.txt') {
- $found = true;
- break;
- }
- }
- $this->assertTrue($found);
- }
- /**
- * @param $user
- * @param bool $create
- * @param bool $password
- */
- public static function loginHelper($user, $create = false, $password = false) {
- if ($create) {
- \OC_User::createUser($user, $user);
- }
- if ($password === false) {
- $password = $user;
- }
- \OC_Util::tearDownFS();
- \OC_User::setUserId('');
- \OC\Files\Filesystem::tearDown();
- \OC_Util::setupFS($user);
- \OC_User::setUserId($user);
- $params['uid'] = $user;
- $params['password'] = $password;
- OCA\Encryption\Hooks::login($params);
- }
- /**
- * helper function to set migration status to the right value
- * to be able to test the migration path
- *
- * @param $status needed migration status for test
- * @param $user for which user the status should be set
- * @return boolean
- */
- private function setMigrationStatus($status, $user) {
- $sql = 'UPDATE `*PREFIX*encryption` SET `migration_status` = ? WHERE `uid` = ?';
- $args = array(
- $status,
- $user
- );
- $query = \OCP\DB::prepare($sql);
- if ($query->execute($args)) {
- return true;
- } else {
- return false;
- }
- }
- }
|