summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2014-05-12 13:55:23 +0200
committerThomas Müller <thomas.mueller@tmit.eu>2014-06-05 16:54:04 +0200
commite9011a8c5525678c1574d59d243af17fca16e9ae (patch)
tree9af30283488e6f4eed1bd0589654a2d16a6dbd77
parenteb29b2984cf81f25df42d59ce1d5c9a9b307763c (diff)
downloadnextcloud-server-e9011a8c5525678c1574d59d243af17fca16e9ae.tar.gz
nextcloud-server-e9011a8c5525678c1574d59d243af17fca16e9ae.zip
adding unit tests for encryption data migration
-rw-r--r--apps/files_encryption/appinfo/update.php19
-rw-r--r--apps/files_encryption/lib/migration.php50
-rw-r--r--apps/files_encryption/tests/encryption_table.xml39
-rw-r--r--apps/files_encryption/tests/migration.php130
4 files changed, 224 insertions, 14 deletions
diff --git a/apps/files_encryption/appinfo/update.php b/apps/files_encryption/appinfo/update.php
index 48262cffa11..a29667ec6b6 100644
--- a/apps/files_encryption/appinfo/update.php
+++ b/apps/files_encryption/appinfo/update.php
@@ -1,19 +1,10 @@
<?php
-$installedVersion=OCP\Config::getAppValue('files_encryption', 'installed_version');
-// migrate settings from oc_encryption to oc_preferences
-if (version_compare($installedVersion, '0.6', '<')) {
- $sql = 'SELECT * FROM `*PREFIX*encryption`';
- $query = \OCP\DB::prepare($sql);
- $result = $query->execute(array())->fetchAll();
-
- foreach ($result as $row) {
- \OC_Preferences::setValue($row['uid'], 'files_encryption', 'recovery_enabled', $row['recovery_enabled']);
- \OC_Preferences::setValue($row['uid'], 'files_encryption', 'migration_status', $row['migration_status']);
- }
+use OCA\Files_Encryption\Migration;
- $deleteOldTable = 'DROP TABLE `*PREFIX*encryption`';
- $query = \OCP\DB::prepare($deleteOldTable);
- $query->execute(array());
+$installedVersion=OCP\Config::getAppValue('files_encryption', 'installed_version');
+if (version_compare($installedVersion, '0.6', '<')) {
+ $m = new Migration();
+ $m->dropTableEncryption();
}
diff --git a/apps/files_encryption/lib/migration.php b/apps/files_encryption/lib/migration.php
new file mode 100644
index 00000000000..59389911b5b
--- /dev/null
+++ b/apps/files_encryption/lib/migration.php
@@ -0,0 +1,50 @@
+<?php
+ /**
+ * ownCloud
+ *
+ * @author Thomas Müller
+ * @copyright 2014 Thomas Müller deepdiver@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Files_Encryption;
+
+
+class Migration {
+
+ public function __construct($tableName = 'encryption') {
+ $this->tableName = $tableName;
+ }
+
+ // migrate settings from oc_encryption to oc_preferences
+ public function dropTableEncryption() {
+ $tableName = $this->tableName;
+ if (!\OC_DB::tableExists($tableName)) {
+ return;
+ }
+ $sql = "select `uid`, max(`recovery_enabled`) as `recovery_enabled`, min(`migration_status`) as `migration_status` from `*PREFIX*$tableName` group by `uid`";
+ $query = \OCP\DB::prepare($sql);
+ $result = $query->execute(array())->fetchAll();
+
+ foreach ($result as $row) {
+ \OC_Preferences::setValue($row['uid'], 'files_encryption', 'recovery_enabled', $row['recovery_enabled']);
+ \OC_Preferences::setValue($row['uid'], 'files_encryption', 'migration_status', $row['migration_status']);
+ }
+
+ \OC_DB::dropTable($tableName);
+ }
+
+}
diff --git a/apps/files_encryption/tests/encryption_table.xml b/apps/files_encryption/tests/encryption_table.xml
new file mode 100644
index 00000000000..c0f63dc0efa
--- /dev/null
+++ b/apps/files_encryption/tests/encryption_table.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<database>
+ <name>*dbname*</name>
+ <create>true</create>
+ <overwrite>false</overwrite>
+ <charset>utf8</charset>
+ <table>
+ <name>*dbprefix*encryption_test</name>
+ <declaration>
+ <field>
+ <name>uid</name>
+ <type>text</type>
+ <notnull>true</notnull>
+ <length>64</length>
+ </field>
+ <field>
+ <name>mode</name>
+ <type>text</type>
+ <notnull>true</notnull>
+ <length>64</length>
+ <comments>What client-side / server-side configuration is used</comments>
+ </field>
+ <field>
+ <name>recovery_enabled</name>
+ <type>integer</type>
+ <notnull>true</notnull>
+ <default>0</default>
+ <comments>Whether encryption key recovery is enabled</comments>
+ </field>
+ <field>
+ <name>migration_status</name>
+ <type>integer</type>
+ <notnull>true</notnull>
+ <default>0</default>
+ <comments>Whether encryption migration has been performed</comments>
+ </field>
+ </declaration>
+ </table>
+</database>
diff --git a/apps/files_encryption/tests/migration.php b/apps/files_encryption/tests/migration.php
new file mode 100644
index 00000000000..3ef528c24bb
--- /dev/null
+++ b/apps/files_encryption/tests/migration.php
@@ -0,0 +1,130 @@
+<?php
+ /**
+ * ownCloud
+ *
+ * @author Thomas Müller
+ * @copyright 2014 Thomas Müller deepdiver@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+use OCA\Encryption;
+use OCA\Files_Encryption\Migration;
+
+class Test_Migration extends PHPUnit_Framework_TestCase {
+
+ public function tearDown() {
+ if (OC_DB::tableExists('encryption_test')) {
+ OC_DB::dropTable('encryption_test');
+ }
+ $this->assertTableNotExist('encryption_test');
+ }
+
+ public function setUp() {
+ if (OC_DB::tableExists('encryption_test')) {
+ OC_DB::dropTable('encryption_test');
+ }
+ $this->assertTableNotExist('encryption_test');
+ }
+
+ public function testEncryptionTableDoesNotExist() {
+
+ $this->assertTableNotExist('encryption_test');
+
+ $migration = new Migration('encryption_test');
+ $migration->dropTableEncryption();
+
+ $this->assertTableNotExist('encryption_test');
+
+ }
+
+ public function testDataMigration() {
+
+ $this->assertTableNotExist('encryption_test');
+
+ // create test table
+ OC_DB::createDbFromStructure(__DIR__ . '/encryption_table.xml');
+ $this->assertTableExist('encryption_test');
+
+ OC_DB::executeAudited('INSERT INTO `*PREFIX*encryption_test` values(?, ?, ?, ?)',
+ array('user1', 'server-side', 1, 1));
+
+ // preform migration
+ $migration = new Migration('encryption_test');
+ $migration->dropTableEncryption();
+
+ // assert
+ $this->assertTableNotExist('encryption_test');
+
+ $rec = \OC_Preferences::getValue('user1', 'files_encryption', 'recovery_enabled');
+ $mig = \OC_Preferences::getValue('user1', 'files_encryption', 'migration_status');
+
+ $this->assertEquals(1, $rec);
+ $this->assertEquals(1, $mig);
+ }
+
+ public function testDuplicateDataMigration() {
+
+ // create test table
+ OC_DB::createDbFromStructure(__DIR__ . '/encryption_table.xml');
+
+ // in case of duplicate entries we want to preserve 0 on migration status and 1 on recovery
+ $data = array(
+ array('user1', 'server-side', 1, 1),
+ array('user1', 'server-side', 1, 0),
+ array('user1', 'server-side', 0, 1),
+ array('user1', 'server-side', 0, 0),
+ );
+ foreach ($data as $d) {
+ OC_DB::executeAudited(
+ 'INSERT INTO `*PREFIX*encryption_test` values(?, ?, ?, ?)',
+ $d);
+ }
+
+ // preform migration
+ $migration = new Migration('encryption_test');
+ $migration->dropTableEncryption();
+
+ // assert
+ $this->assertTableNotExist('encryption_test');
+
+ $rec = \OC_Preferences::getValue('user1', 'files_encryption', 'recovery_enabled');
+ $mig = \OC_Preferences::getValue('user1', 'files_encryption', 'migration_status');
+
+ $this->assertEquals(1, $rec);
+ $this->assertEquals(0, $mig);
+ }
+
+ /**
+ * @param string $table
+ */
+ public function assertTableExist($table) {
+ $this->assertTrue(OC_DB::tableExists($table), 'Table ' . $table . ' does not exist');
+ }
+
+ /**
+ * @param string $table
+ */
+ public function assertTableNotExist($table) {
+ $type=OC_Config::getValue( "dbtype", "sqlite" );
+ if( $type == 'sqlite' || $type == 'sqlite3' ) {
+ // sqlite removes the tables after closing the DB
+ $this->assertTrue(true);
+ } else {
+ $this->assertFalse(OC_DB::tableExists($table), 'Table ' . $table . ' exists.');
+ }
+ }
+
+}