‪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 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
24 final class ‪NoncePoolTest extends UnitTestCase
25 {
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 
45  public function ‪itemsAreMerged(): void
46  {
47  $itemsA = ‪self::createItems();
48  $itemsB = ‪self::createItems();
49  $validItems = array_merge(
50  array_slice($itemsA, 0, 3),
51  array_slice($itemsB, 0, 3)
52  );
53  $poolA = new ‪NoncePool($itemsA);
54  $poolB = new ‪NoncePool($itemsB);
55  $poolA->merge($poolB);
56 
57  foreach ($validItems as $name => $validItem) {
58  self::assertSame($validItem, $poolA->findSigningSecret($name));
59  }
60  self::assertSame(['rejected-name', 'revoked-a', 'revoked-b', 'revoked-c'], $poolA->getRevocableNames());
61  self::assertSame([], $poolA->getEmittableNonces());
62  }
63 
68  {
69  $items = ‪self::createItems();
70  $pool = new ‪NoncePool($items);
71  $nonceA = $pool->provideSigningSecret();
72  $nonceB = $pool->provideSigningSecret();
73  self::assertSame($nonceA, $nonceB);
74  self::assertNotContains($nonceA, $items);
75  }
76 
77  public static function ‪itemsArePurgedDataProvider(): \Generator
78  {
79  $items = ‪self::createItems();
80  $validItems = array_slice($items, 0, 3);
81  yield [
82  ['size' => 1],
83  $items,
84  $validItems,
85  ‪self::getArrayKeysDiff($items, array_slice($items, 0, 1)),
86  ];
87  yield [
88  ['size' => 2],
89  $items,
90  $validItems,
91  ‪self::getArrayKeysDiff($items, array_slice($items, 0, 2)),
92  ];
93  yield [
94  ['size' => 10],
95  $items,
96  $validItems,
97  ‪self::getArrayKeysDiff($items, $validItems),
98  ];
99  }
100 
105  public function ‪itemsArePurged(array $options, array $items, array $validItems, array $revocableNames): void
106  {
107  $pool = (new ‪NoncePool($items, $options))->purge();
108  foreach ($validItems as $name => $validItem) {
109  self::assertSame($validItem, $pool->findSigningSecret($name));
110  }
111  self::assertEmpty(array_diff($revocableNames, $pool->getRevocableNames()));
112  }
113 
114  private static function ‪createItems(): array
115  {
116  $nonceA = ‪Nonce::create();
117  $nonceB = ‪Nonce::create();
118  $nonceC = ‪Nonce::create();
119  return [
120  $nonceA->getSigningIdentifier()->name => $nonceA,
121  $nonceB->getSigningIdentifier()->name => $nonceB,
122  $nonceC->getSigningIdentifier()->name => $nonceC,
123  'rejected-name' => ‪Nonce::create(),
124  'revoked-a' => null,
125  'revoked-b' => null,
126  'revoked-c' => null,
127  ];
128  }
129 
130  private static function ‪getArrayKeysDiff(array $items, array $without): array
131  {
132  $diff = array_diff_key($items, $without);
133  return array_keys($diff);
134  }
135 }
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest\itemsAreMerged
‪itemsAreMerged()
Definition: NoncePoolTest.php:45
‪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:77
‪TYPO3\CMS\Core\Security\NoncePool
Definition: NoncePool.php:24
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest
Definition: NoncePoolTest.php:25
‪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:130
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest\itemsArePurged
‪itemsArePurged(array $options, array $items, array $validItems, array $revocableNames)
Definition: NoncePoolTest.php:105
‪TYPO3\CMS\Core\Tests\Unit\Security\NoncePoolTest\createItems
‪static createItems()
Definition: NoncePoolTest.php:114
‪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:67
‪TYPO3\CMS\Core\Tests\Unit\Security