‪TYPO3CMS  ‪main
NoncePoolTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪NoncePoolTest extends UnitTestCase
27 {
28  #[Test]
29  public function ‪instantiationReflectsState(): void
30  {
31  $items = ‪self::createItems();
32  $validItems = array_slice($items, 0, 3);
33  $pool = new ‪NoncePool($items);
34 
35  foreach ($validItems as ‪$name => $validItem) {
36  self::assertSame($validItem, $pool->findSigningSecret(‪$name));
37  }
38  self::assertSame(['rejected-name', 'revoked-a', 'revoked-b', 'revoked-c'], $pool->getRevocableNames());
39  self::assertSame([], $pool->getEmittableNonces());
40  }
41 
42  #[Test]
43  public function ‪itemsAreMerged(): void
44  {
45  $itemsA = ‪self::createItems();
46  $itemsB = ‪self::createItems();
47  $validItems = array_merge(
48  array_slice($itemsA, 0, 3),
49  array_slice($itemsB, 0, 3)
50  );
51  $poolA = new ‪NoncePool($itemsA);
52  $poolB = new ‪NoncePool($itemsB);
53  $poolA->merge($poolB);
54 
55  foreach ($validItems as ‪$name => $validItem) {
56  self::assertSame($validItem, $poolA->findSigningSecret(‪$name));
57  }
58  self::assertSame(['rejected-name', 'revoked-a', 'revoked-b', 'revoked-c'], $poolA->getRevocableNames());
59  self::assertSame([], $poolA->getEmittableNonces());
60  }
61 
62  #[Test]
64  {
65  $items = ‪self::createItems();
66  $pool = new ‪NoncePool($items);
67  $nonceA = $pool->provideSigningSecret();
68  $nonceB = $pool->provideSigningSecret();
69  self::assertSame($nonceA, $nonceB);
70  self::assertNotContains($nonceA, $items);
71  }
72 
73  public static function ‪itemsArePurgedDataProvider(): \Generator
74  {
75  $items = ‪self::createItems();
76  $validItems = array_slice($items, 0, 3);
77  yield [
78  ['size' => 1],
79  $items,
80  $validItems,
81  ‪self::getArrayKeysDiff($items, array_slice($items, 0, 1)),
82  ];
83  yield [
84  ['size' => 2],
85  $items,
86  $validItems,
87  ‪self::getArrayKeysDiff($items, array_slice($items, 0, 2)),
88  ];
89  yield [
90  ['size' => 10],
91  $items,
92  $validItems,
93  ‪self::getArrayKeysDiff($items, $validItems),
94  ];
95  }
96 
97  #[DataProvider('itemsArePurgedDataProvider')]
98  #[Test]
99  public function ‪itemsArePurged(array $options, array $items, array $validItems, array $revocableNames): void
100  {
101  $pool = (new ‪NoncePool($items, $options))->purge();
102  foreach ($validItems as ‪$name => $validItem) {
103  self::assertSame($validItem, $pool->findSigningSecret(‪$name));
104  }
105  self::assertEmpty(array_diff($revocableNames, $pool->getRevocableNames()));
106  }
107 
108  private static function ‪createItems(): array
109  {
110  $nonceA = ‪Nonce::create();
111  $nonceB = ‪Nonce::create();
112  $nonceC = ‪Nonce::create();
113  return [
114  $nonceA->getSigningIdentifier()->name => $nonceA,
115  $nonceB->getSigningIdentifier()->name => $nonceB,
116  $nonceC->getSigningIdentifier()->name => $nonceC,
117  'rejected-name' => ‪Nonce::create(),
118  'revoked-a' => null,
119  'revoked-b' => null,
120  'revoked-c' => null,
121  ];
122  }
123 
124  private static function ‪getArrayKeysDiff(array $items, array $without): array
125  {
126  $diff = array_diff_key($items, $without);
127  return array_keys($diff);
128  }
129 }
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest\itemsAreMerged
‪itemsAreMerged()
Definition: NoncePoolTest.php:43
‪TYPO3\CMS\Core\Security\Nonce\create
‪static create(int $length=self::MIN_BYTES)
Definition: Nonce.php:37
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest\itemsArePurgedDataProvider
‪static itemsArePurgedDataProvider()
Definition: NoncePoolTest.php:73
‪$name
‪$name
Definition: phpIntegrityChecker.php:235
‪TYPO3\CMS\Core\Security\NoncePool
Definition: NoncePool.php:24
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest
Definition: NoncePoolTest.php:27
‪TYPO3\CMS\Core\Security\Nonce
Definition: Nonce.php:29
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest\getArrayKeysDiff
‪static getArrayKeysDiff(array $items, array $without)
Definition: NoncePoolTest.php:124
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest\itemsArePurged
‪itemsArePurged(array $options, array $items, array $validItems, array $revocableNames)
Definition: NoncePoolTest.php:99
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest\createItems
‪static createItems()
Definition: NoncePoolTest.php:108
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest\instantiationReflectsState
‪instantiationReflectsState()
Definition: NoncePoolTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest\provideSigningSecretDoesNotUseReceivedNonce
‪provideSigningSecretDoesNotUseReceivedNonce()
Definition: NoncePoolTest.php:63
‪TYPO3\CMS\Core\Tests\Unit\Security