From e6abb099f9d8a2b4347a12b9a066bc3527af99de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Wed, 17 Jul 2024 12:37:32 +0200 Subject: [PATCH 1/2] fix: Fix generating session id again if duplicated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Calviño Sánchez --- lib/Service/SessionService.php | 2 +- tests/php/Service/SessionServiceTest.php | 121 +++++++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 tests/php/Service/SessionServiceTest.php diff --git a/lib/Service/SessionService.php b/lib/Service/SessionService.php index bb529320e07..a02f132fdd7 100644 --- a/lib/Service/SessionService.php +++ b/lib/Service/SessionService.php @@ -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; } } diff --git a/tests/php/Service/SessionServiceTest.php b/tests/php/Service/SessionServiceTest.php new file mode 100644 index 00000000000..a92c768cb94 --- /dev/null +++ b/tests/php/Service/SessionServiceTest.php @@ -0,0 +1,121 @@ +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); + } +} From f677240228f49807814cf8395f38e73a7540469f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 22 Jul 2024 15:19:41 +0200 Subject: [PATCH 2/2] fix: PHP 8.0 lint Signed-off-by: Joas Schilling --- tests/php/Service/SessionServiceTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/php/Service/SessionServiceTest.php b/tests/php/Service/SessionServiceTest.php index a92c768cb94..7e89e22033a 100644 --- a/tests/php/Service/SessionServiceTest.php +++ b/tests/php/Service/SessionServiceTest.php @@ -21,7 +21,8 @@ */ class SessionServiceTest extends TestCase { protected ?SessionMapper $sessionMapper = null; - protected ISecureRandom&MockObject $secureRandom; + /** @var ISecureRandom|MockObject */ + protected $secureRandom; private ?SessionService $service = null; private const RANDOM_254 = '123456789abcdef0123456789abcdef1123456789abcdef2123456789abcdef3123456789abcdef4123456789abcdef5123456789abcdef6123456789abcdef7123456789abcdef8123456789abcdef9123456789abcdefa123456789abcdefb123456789abcdefc123456789abcdefd123456789abcdefe123456789abcde';