Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/federation/lib/DbHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,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
*/
Expand Down
10 changes: 10 additions & 0 deletions apps/federation/lib/SyncFederationAddressBooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function syncThemAll(\Closure $callback, bool $full = false) {
}

do {
$previousSyncToken = $syncToken;
[$syncToken, $truncated] = $this->syncService->syncRemoteAddressBook(
$url,
$cardDavUser,
Expand All @@ -69,6 +70,15 @@ public function syncThemAll(\Closure $callback, bool $full = false) {
$targetBookProperties,
$this->appConfig->getAppValueBool(ConfigLexicon::IGNORE_REMOTE_SYSTEM_ADDRESS_BOOK),
);
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing, but with this PR it also writes to the DB on every pass: do … while ($truncated) never ends if the remote keeps answering truncated without moving the token forward. The remote's incremental query uses gte('synctoken', …), so with carddav_sync_request_truncation set to 1 it returns the token it was given. A null token with truncated = true loops as well. Failing the sync when the new token is null or equal to the previous one would stop it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is out of scope but I fixed it because it can be a big issue.


if ($full) {
Expand Down
13 changes: 13 additions & 0 deletions apps/federation/tests/DbHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ public function testSetServerStatus(): void {
$this->assertSame(TrustedServers::STATUS_OK, (int)$result[0]['status']);
}

public function testSetServerSyncToken(): void {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With only one server in the table, this would still pass if setServerSyncToken() updated every row. A second server whose sync_token stays null would cover the url_hash condition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

$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);
Expand Down
163 changes: 163 additions & 0 deletions apps/federation/tests/SyncFederationAddressbooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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);
Expand Down
Loading