Code Duplication    Length = 42-42 lines in 2 locations

apps/encryption/tests/lib/KeyManagerTest.php 2 locations

@@ 245-286 (lines=42) @@
242
	 *
243
	 * @param bool $useMasterKey
244
	 */
245
	public function testInit($useMasterKey) {
246
247
		$instance = $this->getMockBuilder('OCA\Encryption\KeyManager')
248
			->setConstructorArgs(
249
				[
250
					$this->keyStorageMock,
251
					$this->cryptMock,
252
					$this->configMock,
253
					$this->userMock,
254
					$this->sessionMock,
255
					$this->logMock,
256
					$this->utilMock
257
				]
258
			)->setMethods(['getMasterKeyId', 'getMasterKeyPassword', 'getSystemPrivateKey', 'getPrivateKey'])
259
			->getMock();
260
261
		$this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
262
			->willReturn($useMasterKey);
263
264
		$this->sessionMock->expects($this->at(0))->method('setStatus')
265
			->with(Session::INIT_EXECUTED);
266
267
		$instance->expects($this->any())->method('getMasterKeyId')->willReturn('masterKeyId');
268
		$instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
269
		$instance->expects($this->any())->method('getSystemPrivateKey')->with('masterKeyId')->willReturn('privateMasterKey');
270
		$instance->expects($this->any())->method('getPrivateKey')->with($this->userId)->willReturn('privateUserKey');
271
272
		if($useMasterKey) {
273
			$this->cryptMock->expects($this->once())->method('decryptPrivateKey')
274
				->with('privateMasterKey', 'masterKeyPassword', 'masterKeyId')
275
				->willReturn('key');
276
		} else {
277
			$this->cryptMock->expects($this->once())->method('decryptPrivateKey')
278
				->with('privateUserKey', 'pass', $this->userId)
279
				->willReturn('key');
280
		}
281
282
		$this->sessionMock->expects($this->once())->method('setPrivateKey')
283
			->with('key');
284
285
		$this->assertTrue($instance->init($this->userId, 'pass'));
286
	}
287
288
	public function dataTestInit() {
289
		return [
@@ 532-573 (lines=42) @@
529
	 *
530
	 * @param $masterKey
531
	 */
532
	public function testValidateMasterKey($masterKey) {
533
534
		/** @var \OCA\Encryption\KeyManager | \PHPUnit_Framework_MockObject_MockObject $instance */
535
		$instance = $this->getMockBuilder('OCA\Encryption\KeyManager')
536
			->setConstructorArgs(
537
				[
538
					$this->keyStorageMock,
539
					$this->cryptMock,
540
					$this->configMock,
541
					$this->userMock,
542
					$this->sessionMock,
543
					$this->logMock,
544
					$this->utilMock
545
				]
546
			)->setMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
547
			->getMock();
548
549
		$instance->expects($this->once())->method('getPublicMasterKey')
550
			->willReturn($masterKey);
551
552
		$instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
553
		$this->cryptMock->expects($this->any())->method('generateHeader')->willReturn('header');
554
555
		if(empty($masterKey)) {
556
			$this->cryptMock->expects($this->once())->method('createKeyPair')
557
				->willReturn(['publicKey' => 'public', 'privateKey' => 'private']);
558
			$this->keyStorageMock->expects($this->once())->method('setSystemUserKey')
559
				->with('systemKeyId.publicKey', 'public', \OCA\Encryption\Crypto\Encryption::ID);
560
			$this->cryptMock->expects($this->once())->method('encryptPrivateKey')
561
				->with('private', 'masterKeyPassword', 'systemKeyId')
562
				->willReturn('EncryptedKey');
563
			$instance->expects($this->once())->method('setSystemPrivateKey')
564
				->with('systemKeyId', 'headerEncryptedKey');
565
		} else {
566
			$this->cryptMock->expects($this->never())->method('createKeyPair');
567
			$this->keyStorageMock->expects($this->never())->method('setSystemUserKey');
568
			$this->cryptMock->expects($this->never())->method('encryptPrivateKey');
569
			$instance->expects($this->never())->method('setSystemPrivateKey');
570
		}
571
572
		$instance->validateMasterKey();
573
	}
574
575
	public function dataTestValidateMasterKey() {
576
		return [