summaryrefslogtreecommitdiffstats
path: root/apps/files_encryption/tests/migration.php
blob: 8ca2e94c90ab8bf956b85db5e33edb54a59d2c64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?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 \Test\TestCase {

	protected function tearDown() {
		if (OC_DB::tableExists('encryption_test')) {
			OC_DB::dropTable('encryption_test');
		}
		$this->assertTableNotExist('encryption_test');

		parent::tearDown();
	}

	protected function setUp() {
		parent::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 checkLastIndexId() {
		$query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
			.' `item_type`, `item_source`, `item_target`, `share_type`,'
			.' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
			.' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)');
		$query->bindValue(1, 'file');
		$query->bindValue(2, 949);
		$query->bindValue(3, '/949');
		$query->bindValue(4, 0);
		$query->bindValue(5, 'migrate-test-user');
		$query->bindValue(6, 'migrate-test-owner');
		$query->bindValue(7, 23);
		$query->bindValue(8, 1402493312);
		$query->bindValue(9, 0);
		$query->bindValue(10, '/migration.txt');
		$query->bindValue(11, null);
		$query->bindValue(12, null);
		$query->bindValue(13, null);
		$this->assertEquals(1, $query->execute());

		$this->assertNotEquals('0', \OC_DB::insertid('*PREFIX*share'));

		// cleanup
		$query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_target` = ?');
		$query->bindValue(1, '/migration.txt');
		$this->assertEquals(1, $query->execute());

	}

	public function testBrokenLastIndexId() {

		// create test table
		$this->checkLastIndexId();
		OC_DB::createDbFromStructure(__DIR__ . '/encryption_table.xml');
		$this->checkLastIndexId();
	}

	public function testDataMigration() {
		// TODO travis
		if (getenv('TRAVIS')) {
			$this->markTestSkipped('Fails on travis');
		}

		$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() {
		// TODO travis
		if (getenv('TRAVIS')) {
			$this->markTestSkipped('Fails on travis');
		}

		// 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.');
		}
	}

}