diff --git a/apps/federation/lib/DbHandler.php b/apps/federation/lib/DbHandler.php index 1dac5a2f45fc9..33d0b810337e6 100644 --- a/apps/federation/lib/DbHandler.php +++ b/apps/federation/lib/DbHandler.php @@ -209,6 +209,18 @@ public function setServerStatus(string $url, int $status, ?string $token = null) $query->executeStatement(); } + /** + * Set server sync token without changing the server status + */ + public function setServerSyncToken(string $url, string $token): void { + $hash = $this->hash($url); + $query = $this->connection->getQueryBuilder(); + $query->update($this->dbTable) + ->set('sync_token', $query->createNamedParameter($token)) + ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash))); + $query->executeStatement(); + } + /** * Get server status */ diff --git a/apps/federation/lib/SyncFederationAddressBooks.php b/apps/federation/lib/SyncFederationAddressBooks.php index b3272597110d7..9b1df3121cf31 100644 --- a/apps/federation/lib/SyncFederationAddressBooks.php +++ b/apps/federation/lib/SyncFederationAddressBooks.php @@ -59,6 +59,7 @@ public function syncThemAll(\Closure $callback, bool $full = false) { } do { + $previousSyncToken = $syncToken; [$syncToken, $truncated] = $this->syncService->syncRemoteAddressBook( $url, $cardDavUser, @@ -69,6 +70,15 @@ public function syncThemAll(\Closure $callback, bool $full = false) { $targetPrincipal, $targetBookProperties ); + if ($truncated && ($syncToken === null || $syncToken === $previousSyncToken)) { + throw new \RuntimeException("Truncated sync response from $url did not advance the sync token"); + } + if (!$full && $truncated && $syncToken !== null) { + // Keep the progress of completed pages in case a later page fails. + // A full sync must restart from scratch so pending cards get deleted. + $this->dbHandler->setServerSyncToken($url, $syncToken); + $oldSyncToken = $syncToken; + } } while ($truncated); if ($full) { diff --git a/apps/federation/tests/DbHandlerTest.php b/apps/federation/tests/DbHandlerTest.php index a812c28b6344d..830393dea1ddb 100644 --- a/apps/federation/tests/DbHandlerTest.php +++ b/apps/federation/tests/DbHandlerTest.php @@ -215,6 +215,19 @@ public function testSetServerStatus(): void { $this->assertSame(TrustedServers::STATUS_OK, (int)$result[0]['status']); } + public function testSetServerSyncToken(): void { + $this->dbHandler->addServer('server1'); + $this->dbHandler->addServer('server2'); + $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_FAILURE); + + $this->dbHandler->setServerSyncToken('http://server1', 'token1234567890'); + + $servers = array_column($this->dbHandler->getAllServer(), null, 'url'); + $this->assertSame('token1234567890', $servers['server1']['sync_token']); + $this->assertSame(TrustedServers::STATUS_FAILURE, (int)$servers['server1']['status']); + $this->assertNull($servers['server2']['sync_token']); + } + public function testGetServerStatus(): void { $this->dbHandler->addServer('server1'); $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK); diff --git a/apps/federation/tests/SyncFederationAddressbooksTest.php b/apps/federation/tests/SyncFederationAddressbooksTest.php index ff03f5cf44232..1214882cd54df 100644 --- a/apps/federation/tests/SyncFederationAddressbooksTest.php +++ b/apps/federation/tests/SyncFederationAddressbooksTest.php @@ -79,6 +79,169 @@ public function testException(): void { $this->assertCount(2, $this->callBacks); } + public function testTruncatedSyncPersistsTokenAfterEachPage(): void { + /** @var DbHandler&MockObject $dbHandler */ + $dbHandler = $this->createMock(DbHandler::class); + $dbHandler->method('getAllServer') + ->willReturn([ + [ + 'url' => 'https://cloud.example.org', + 'url_hash' => 'sha1', + 'shared_secret' => 'ilovenextcloud', + 'sync_token' => null + ] + ]); + $tokenCalls = []; + $dbHandler->expects($this->exactly(2))->method('setServerSyncToken') + ->willReturnCallback(function (string $url, string $token) use (&$tokenCalls): void { + $tokenCalls[] = [$url, $token]; + }); + $dbHandler->expects($this->once())->method('setServerStatus') + ->with('https://cloud.example.org', TrustedServers::STATUS_OK, '5'); + $syncService = $this->createMock(SyncService::class); + $syncService->expects($this->exactly(3))->method('syncRemoteAddressBook') + ->willReturnOnConsecutiveCalls(['init_1_5', true], ['init_2_5', true], ['5', false]); + + /** @var SyncService $syncService */ + $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->createMock(IAppConfig::class), $this->logger); + $s->syncThemAll(function ($url, $ex): void { + $this->callBacks[] = [$url, $ex]; + }); + $this->assertCount(1, $this->callBacks); + $this->assertSame([ + ['https://cloud.example.org', 'init_1_5'], + ['https://cloud.example.org', 'init_2_5'], + ], $tokenCalls); + } + + public function testFailedPageKeepsTokenOfCompletedPages(): void { + /** @var DbHandler&MockObject $dbHandler */ + $dbHandler = $this->createMock(DbHandler::class); + $dbHandler->method('getAllServer') + ->willReturn([ + [ + 'url' => 'https://cloud.example.org', + 'url_hash' => 'sha1', + 'shared_secret' => 'ilovenextcloud', + 'sync_token' => null + ] + ]); + $dbHandler->expects($this->once())->method('setServerSyncToken') + ->with('https://cloud.example.org', 'init_1_5'); + $dbHandler->expects($this->once())->method('setServerStatus') + ->with('https://cloud.example.org', TrustedServers::STATUS_FAILURE); + $page = 0; + $syncService = $this->createMock(SyncService::class); + $syncService->expects($this->exactly(2))->method('syncRemoteAddressBook') + ->willReturnCallback(function () use (&$page) { + if ($page++ === 0) { + return ['init_1_5', true]; + } + throw new \Exception('something did not work out'); + }); + + /** @var SyncService $syncService */ + $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->createMock(IAppConfig::class), $this->logger); + $s->syncThemAll(function ($url, $ex): void { + $this->callBacks[] = [$url, $ex]; + }); + $this->assertCount(2, $this->callBacks); + } + + public static function dataTruncatedPageWithoutProgress(): array { + return [ + 'null token' => ['init_1_5', [null, true]], + 'unchanged token' => ['init_1_5', ['init_1_5', true]], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'dataTruncatedPageWithoutProgress')] + public function testTruncatedPageWithoutProgressFailsSync(?string $storedToken, array $response): void { + /** @var DbHandler&MockObject $dbHandler */ + $dbHandler = $this->createMock(DbHandler::class); + $dbHandler->method('getAllServer') + ->willReturn([ + [ + 'url' => 'https://cloud.example.org', + 'url_hash' => 'sha1', + 'shared_secret' => 'ilovenextcloud', + 'sync_token' => $storedToken + ] + ]); + $dbHandler->expects($this->never())->method('setServerSyncToken'); + $dbHandler->expects($this->once())->method('setServerStatus') + ->with('https://cloud.example.org', TrustedServers::STATUS_FAILURE); + $syncService = $this->createMock(SyncService::class); + $syncService->expects($this->once())->method('syncRemoteAddressBook') + ->willReturn($response); + + /** @var SyncService $syncService */ + $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->createMock(IAppConfig::class), $this->logger); + $s->syncThemAll(function ($url, $ex): void { + $this->callBacks[] = [$url, $ex]; + }); + $this->assertCount(2, $this->callBacks); + $this->assertInstanceOf(\RuntimeException::class, $this->callBacks[1][1]); + } + + public function testFullTruncatedSyncDoesNotPersistIntermediateTokens(): void { + /** @var DbHandler&MockObject $dbHandler */ + $dbHandler = $this->createMock(DbHandler::class); + $dbHandler->method('getAllServer') + ->willReturn([ + [ + 'url' => 'https://cloud.example.org', + 'url_hash' => 'sha1', + 'shared_secret' => 'ilovenextcloud', + 'sync_token' => '3' + ] + ]); + $dbHandler->expects($this->never())->method('setServerSyncToken'); + $dbHandler->expects($this->once())->method('setServerStatus') + ->with('https://cloud.example.org', TrustedServers::STATUS_OK, '5'); + $syncService = $this->createMock(SyncService::class); + $syncService->method('ensureSystemAddressBookExists')->willReturn(['id' => 1]); + $syncService->expects($this->once())->method('markCardsAsPending')->with(1); + $syncService->expects($this->once())->method('deletePendingCards')->with(1); + $syncService->expects($this->exactly(3))->method('syncRemoteAddressBook') + ->willReturnOnConsecutiveCalls(['init_1_5', true], ['init_2_5', true], ['5', false]); + + /** @var SyncService $syncService */ + $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->createMock(IAppConfig::class), $this->logger); + $s->syncThemAll(function ($url, $ex): void { + $this->callBacks[] = [$url, $ex]; + }, true); + $this->assertCount(1, $this->callBacks); + } + + public function testFinalTokenEqualToStoredTokenReplacesIntermediateToken(): void { + /** @var DbHandler&MockObject $dbHandler */ + $dbHandler = $this->createMock(DbHandler::class); + $dbHandler->method('getAllServer') + ->willReturn([ + [ + 'url' => 'https://cloud.example.org', + 'url_hash' => 'sha1', + 'shared_secret' => 'ilovenextcloud', + 'sync_token' => '5' + ] + ]); + $dbHandler->expects($this->once())->method('setServerSyncToken') + ->with('https://cloud.example.org', 'init_1_5'); + $dbHandler->expects($this->once())->method('setServerStatus') + ->with('https://cloud.example.org', TrustedServers::STATUS_OK, '5'); + $syncService = $this->createMock(SyncService::class); + $syncService->expects($this->exactly(2))->method('syncRemoteAddressBook') + ->willReturnOnConsecutiveCalls(['init_1_5', true], ['5', false]); + + /** @var SyncService $syncService */ + $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->createMock(IAppConfig::class), $this->logger); + $s->syncThemAll(function ($url, $ex): void { + $this->callBacks[] = [$url, $ex]; + }); + $this->assertCount(1, $this->callBacks); + } + public function testSuccessfulSyncWithoutChangesAfterFailure(): void { /** @var DbHandler&MockObject $dbHandler */ $dbHandler = $this->createMock(DbHandler::class);