Skip to content
Open
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 @@ -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
*/
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 @@ -59,6 +59,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) {
$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) {
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 {
$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 @@ -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);
Expand Down
Loading