4343use  OCP \IConfig ;
4444use  OCP \ILogger ;
4545use  OCP \IUserSession ;
46+ use  OCP \Lock \ILockingProvider ;
47+ use  OCP \Lock \LockedException ;
48+ use  PHPUnit \Framework \MockObject \MockObject ;
4649use  Test \TestCase ;
4750
4851class  KeyManagerTest extends  TestCase {
@@ -79,6 +82,9 @@ class KeyManagerTest extends TestCase {
7982	/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ 
8083	private  $ configMock ;
8184
85+ 	/** @var ILockingProvider|MockObject */ 
86+ 	private  $ lockingProviderMock ;
87+ 
8288	protected  function  setUp (): void  {
8389		parent ::setUp ();
8490		$ this  ->userId  = 'user1 ' ;
@@ -99,6 +105,7 @@ protected function setUp(): void {
99105		$ this  ->utilMock  = $ this  ->getMockBuilder (Util::class)
100106			->disableOriginalConstructor ()
101107			->getMock ();
108+ 		$ this  ->lockingProviderMock  = $ this  ->createMock (ILockingProvider::class);
102109
103110		$ this  ->instance  = new  KeyManager (
104111			$ this  ->keyStorageMock ,
@@ -107,7 +114,9 @@ protected function setUp(): void {
107114			$ this  ->userMock ,
108115			$ this  ->sessionMock ,
109116			$ this  ->logMock ,
110- 			$ this  ->utilMock );
117+ 			$ this  ->utilMock ,
118+ 			$ this  ->lockingProviderMock 
119+ 		);
111120	}
112121
113122	public  function  testDeleteShareKey () {
@@ -220,7 +229,7 @@ public function dataTestUserHasKeys() {
220229		];
221230	}
222231
223- 	 
232+ 
224233	public  function  testUserHasKeysMissingPrivateKey () {
225234		$ this  ->expectException (\OCA \Encryption \Exceptions \PrivateKeyMissingException::class);
226235
@@ -236,7 +245,7 @@ public function testUserHasKeysMissingPrivateKey() {
236245		$ this  ->instance ->userHasKeys ($ this  ->userId );
237246	}
238247
239- 	 
248+ 
240249	public  function  testUserHasKeysMissingPublicKey () {
241250		$ this  ->expectException (\OCA \Encryption \Exceptions \PublicKeyMissingException::class);
242251
@@ -269,7 +278,8 @@ public function testInit($useMasterKey) {
269278					$ this  ->userMock ,
270279					$ this  ->sessionMock ,
271280					$ this  ->logMock ,
272- 					$ this  ->utilMock 
281+ 					$ this  ->utilMock ,
282+ 					$ this  ->lockingProviderMock 
273283				]
274284			)->setMethods (['getMasterKeyId ' , 'getMasterKeyPassword ' , 'getSystemPrivateKey ' , 'getPrivateKey ' ])
275285			->getMock ();
@@ -532,7 +542,7 @@ public function testGetMasterKeyPassword() {
532542		);
533543	}
534544
535- 	 
545+ 
536546	public  function  testGetMasterKeyPasswordException () {
537547		$ this  ->expectException (\Exception::class);
538548
@@ -559,7 +569,8 @@ public function testValidateMasterKey($masterKey) {
559569					$ this  ->userMock ,
560570					$ this  ->sessionMock ,
561571					$ this  ->logMock ,
562- 					$ this  ->utilMock 
572+ 					$ this  ->utilMock ,
573+ 					$ this  ->lockingProviderMock 
563574				]
564575			)->setMethods (['getPublicMasterKey ' , 'setSystemPrivateKey ' , 'getMasterKeyPassword ' ])
565576			->getMock ();
@@ -578,6 +589,8 @@ public function testValidateMasterKey($masterKey) {
578589			$ this  ->cryptMock ->expects ($ this  ->once ())->method ('encryptPrivateKey ' )
579590				->with ('private ' , 'masterKeyPassword ' , 'systemKeyId ' )
580591				->willReturn ('EncryptedKey ' );
592+ 			$ this  ->lockingProviderMock ->expects ($ this  ->once ())
593+ 				->method ('acquireLock ' );
581594			$ instance ->expects ($ this  ->once ())->method ('setSystemPrivateKey ' )
582595				->with ('systemKeyId ' , 'headerEncryptedKey ' );
583596		} else  {
@@ -590,6 +603,39 @@ public function testValidateMasterKey($masterKey) {
590603		$ instance ->validateMasterKey ();
591604	}
592605
606+ 	public  function  testValidateMasterKeyLocked () {
607+ 		/** @var \OCA\Encryption\KeyManager | \PHPUnit_Framework_MockObject_MockObject $instance */ 
608+ 		$ instance  = $ this  ->getMockBuilder (KeyManager::class)
609+ 			->setConstructorArgs (
610+ 				[
611+ 					$ this  ->keyStorageMock ,
612+ 					$ this  ->cryptMock ,
613+ 					$ this  ->configMock ,
614+ 					$ this  ->userMock ,
615+ 					$ this  ->sessionMock ,
616+ 					$ this  ->logMock ,
617+ 					$ this  ->utilMock ,
618+ 					$ this  ->lockingProviderMock 
619+ 				]
620+ 			)->setMethods (['getPublicMasterKey ' , 'getPrivateMasterKey ' , 'setSystemPrivateKey ' , 'getMasterKeyPassword ' ])
621+ 			->getMock ();
622+ 
623+ 		$ instance ->expects ($ this  ->once ())->method ('getPublicMasterKey ' )
624+ 			->willReturn ('' );
625+ 		$ instance ->expects ($ this  ->once ())->method ('getPrivateMasterKey ' )
626+ 			->willReturn ('' );
627+ 
628+ 		$ instance ->expects ($ this  ->any ())->method ('getMasterKeyPassword ' )->willReturn ('masterKeyPassword ' );
629+ 		$ this  ->cryptMock ->expects ($ this  ->any ())->method ('generateHeader ' )->willReturn ('header ' );
630+ 
631+ 		$ this  ->lockingProviderMock ->expects ($ this  ->once ())
632+ 			->method ('acquireLock ' )
633+ 			->willThrowException (new  LockedException ('encryption-generateMasterKey ' ));
634+ 
635+ 		$ this  ->expectException (LockedException::class);
636+ 		$ instance ->validateMasterKey ();
637+ 	}
638+ 
593639	public  function  dataTestValidateMasterKey () {
594640		return  [
595641			['masterKey ' ],
0 commit comments