Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable28] fix: Fix generating session id again if duplicated #12744

Merged
merged 2 commits into from
Jul 22, 2024
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
2 changes: 1 addition & 1 deletion lib/Service/SessionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function createSessionForAttendee(Attendee $attendee, string $forceSessio
break;
} catch (Exception $e) {
// 255 chars are not unique? Try again...
if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
throw $e;
}
}
Expand Down
122 changes: 122 additions & 0 deletions tests/php/Service/SessionServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Talk\Tests\php\Service;

use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\SessionMapper;
use OCA\Talk\Service\SessionService;
use OCP\IDBConnection;
use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

/**
* @group DB
*/
class SessionServiceTest extends TestCase {
protected ?SessionMapper $sessionMapper = null;
/** @var ISecureRandom|MockObject */
protected $secureRandom;
private ?SessionService $service = null;

private const RANDOM_254 = '123456789abcdef0123456789abcdef1123456789abcdef2123456789abcdef3123456789abcdef4123456789abcdef5123456789abcdef6123456789abcdef7123456789abcdef8123456789abcdef9123456789abcdefa123456789abcdefb123456789abcdefc123456789abcdefd123456789abcdefe123456789abcde';

private array $attendeeIds = [];

public function setUp(): void {
parent::setUp();

$this->sessionMapper = \OCP\Server::get(SessionMapper::class);
$this->secureRandom = $this->createMock(ISecureRandom::class);
$this->service = new SessionService(
$this->sessionMapper,
\OCP\Server::get(IDBConnection::class),
$this->secureRandom,
);
}

public function tearDown(): void {
foreach ($this->attendeeIds as $attendeeId) {
try {
$this->sessionMapper->deleteByAttendeeId($attendeeId);
} catch (DoesNotExistException $exception) {
}
}

parent::tearDown();
}

public function testCreateSessionForAttendee() {
$attendee = new Attendee();
$attendee->setId(42);
$attendee->setActorType(Attendee::ACTOR_USERS);
$attendee->setActorId('test');
$this->attendeeIds[] = $attendee->getId();

$random = self::RANDOM_254 . 'x';

$this->secureRandom->expects($this->once())
->method('generate')
->with(255)
->willReturn($random);

$session = $this->service->createSessionForAttendee($attendee);

self::assertEquals($random, $session->getSessionId());
}

public function testCreateSessionForAttendeeWithDuplicatedSessionId() {
$attendee1 = new Attendee();
$attendee1->setId(42);
$attendee1->setActorType(Attendee::ACTOR_USERS);
$attendee1->setActorId('test1');
$this->attendeeIds[] = $attendee1->getId();

$attendee2 = new Attendee();
$attendee2->setId(108);
$attendee2->setActorType(Attendee::ACTOR_USERS);
$attendee2->setActorId('test2');
$this->attendeeIds[] = $attendee2->getId();

$random1 = self::RANDOM_254 . 'x';
$random2 = self::RANDOM_254 . 'y';

$this->secureRandom->expects($this->exactly(3))
->method('generate')
->with(255)
->willReturn(
$random1,
$random1,
$random2,
);

$session1 = $this->service->createSessionForAttendee($attendee1);
$session2 = $this->service->createSessionForAttendee($attendee2);

self::assertEquals($random1, $session1->getSessionId());
self::assertEquals($random2, $session2->getSessionId());
}

public function testCreateSessionForAttendeeWithoutId() {
$attendee = new Attendee();
$attendee->setActorType(Attendee::ACTOR_USERS);
$attendee->setActorId('test');

$random = self::RANDOM_254 . 'x';

$this->secureRandom->expects($this->once())
->method('generate')
->with(255)
->willReturn($random);

$this->expectException(\OC\DB\Exceptions\DbalException::class);

$session = $this->service->createSessionForAttendee($attendee);
}
}
Loading