-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
66 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
use PHPUnit\Framework\TestCase; | ||
use Psr\Clock\ClockInterface; | ||
use RuntimeException; | ||
use Symfony\Bridge\PhpUnit\ClockMock; | ||
use function assert; | ||
|
||
/** | ||
|
@@ -286,7 +285,7 @@ public function invalidOtpWindow(): void | |
/** | ||
* @param positive-int $timestamp | ||
* @param non-empty-string $input | ||
* @param 0|positive-int $leeway | ||
* @param positive-int $leeway | ||
*/ | ||
#[Test] | ||
#[DataProvider('dataLeeway')] | ||
|
@@ -302,7 +301,7 @@ public function verifyOtpInWindow(int $timestamp, string $input, int $leeway, bo | |
/** | ||
* @param positive-int $timestamp | ||
* @param non-empty-string $input | ||
* @param 0|positive-int $leeway | ||
* @param positive-int $leeway | ||
*/ | ||
#[Test] | ||
#[DataProvider('dataLeewayWithEpoch')] | ||
|
@@ -312,26 +311,46 @@ public function verifyOtpWithEpochInWindow( | |
int $leeway, | ||
bool $expectedResult | ||
): void { | ||
ClockMock::register(TOTP::class); | ||
ClockMock::withClockMock($timestamp); | ||
$clock = new ClockMock(); | ||
$clock->setDateTime(DateTimeImmutable::createFromFormat('U', (string) $timestamp)); | ||
$otp = self::createTOTP(6, 'sha1', 30, 'JDDK4U6G3BJLEZ7Y', '[email protected]', 'My Project', 100); | ||
|
||
static::assertSame($expectedResult, $otp->verify($input, null, $leeway)); | ||
} | ||
|
||
public static function dataLeewayWithEpoch(): Iterator | ||
{ | ||
yield 'Leeway of 10 seconds, **out** the period of 11sec' => [319_690_889, '762124', 10, false]; | ||
yield 'Leeway of 10 seconds, **out** the period of 10sec' => [319_690_890, '762124', 10, true]; | ||
yield 'Leeway of 10 seconds, **out** the period of 1sec' => [319_690_899, '762124', 10, true]; | ||
yield 'No leeway, **out** the period' => [319_690_899, '762124', 0, false]; | ||
yield 'No leeway, in the period' => [319_690_900, '762124', 0, true]; | ||
yield 'No leeway, in the period' => [319_690_920, '762124', 0, true]; | ||
yield 'No leeway, in the period' => [319_690_929, '762124', 0, true]; | ||
yield 'No leeway, **out** the period' => [319_690_930, '762124', 0, false]; | ||
yield 'Leeway of 10 seconds, **out** the period of 1sec' => [319_690_930, '762124', 10, true]; | ||
yield 'Leeway of 10 seconds, **out** the period of 10sec' => [319_690_939, '762124', 10, true]; | ||
yield 'Leeway of 10 seconds, **out** the period of 11sec' => [319_690_940, '762124', 10, false]; | ||
yield 'Leeway of 10 seconds, **out** the period of 11sec (11 second before)' => [ | ||
319_690_889, | ||
'762124', | ||
10, | ||
false, | ||
]; | ||
yield 'Leeway of 10 seconds, **out** the period of 10sec (10 second before)' => [ | ||
319_690_890, | ||
'762124', | ||
10, | ||
true, | ||
]; | ||
yield 'Leeway of 10 seconds, **out** the period (1 second before)' => [319_690_899, '762124', 10, true]; | ||
yield 'No leeway, **out** the period (1 second before)' => [319_690_899, '762124', 0, false]; | ||
yield 'No leeway, in the period (start)' => [319_690_900, '762124', 0, true]; | ||
yield 'No leeway, in the period (middle)' => [319_690_920, '762124', 0, true]; | ||
yield 'No leeway, in the period (end)' => [319_690_929, '762124', 0, true]; | ||
yield 'No leeway, **out** the period (1 second after)' => [319_690_930, '762124', 0, false]; | ||
yield 'Leeway of 10 seconds, **out** the period (1 second after)' => [319_690_930, '762124', 10, true]; | ||
yield 'Leeway of 10 seconds, **out** the period of 10sec (10 second after)' => [ | ||
319_690_939, | ||
'762124', | ||
10, | ||
true, | ||
]; | ||
yield 'Leeway of 10 seconds, **out** the period of 11sec (11 second after)' => [ | ||
319_690_940, | ||
'762124', | ||
10, | ||
false, | ||
]; | ||
} | ||
|
||
#[Test] | ||
|
@@ -379,17 +398,37 @@ public static function dataRemainingTimeBeforeExpiration(): Iterator | |
*/ | ||
public static function dataLeeway(): Iterator | ||
{ | ||
yield 'Leeway of 10 seconds, **out** the period of 11sec' => [319_690_789, '762124', 10, false]; | ||
yield 'Leeway of 10 seconds, **out** the period of 10sec' => [319_690_790, '762124', 10, true]; | ||
yield 'Leeway of 10 seconds, **out** the period of 1sec' => [319_690_799, '762124', 10, true]; | ||
yield 'No leeway, **out** the period' => [319_690_799, '762124', 0, false]; | ||
yield 'No leeway, in the period' => [319_690_800, '762124', 0, true]; | ||
yield 'No leeway, in the period' => [319_690_820, '762124', 0, true]; | ||
yield 'No leeway, in the period' => [319_690_829, '762124', 0, true]; | ||
yield 'No leeway, **out** the period' => [319_690_830, '762124', 0, false]; | ||
yield 'Leeway of 10 seconds, **out** the period of 1sec' => [319_690_830, '762124', 10, true]; | ||
yield 'Leeway of 10 seconds, **out** the period of 10sec' => [319_690_839, '762124', 10, true]; | ||
yield 'Leeway of 10 seconds, **out** the period of 11sec' => [319_690_840, '762124', 10, false]; | ||
yield 'Leeway of 10 seconds, **out** the period of 11sec (11 second before)' => [ | ||
319_690_789, | ||
'762124', | ||
10, | ||
false, | ||
]; | ||
yield 'Leeway of 10 seconds, **out** the period of 10sec (10 second before)' => [ | ||
319_690_790, | ||
'762124', | ||
10, | ||
true, | ||
]; | ||
yield 'Leeway of 10 seconds, **out** the period of 1sec (1 second before)' => [319_690_799, '762124', 10, true]; | ||
yield 'No leeway, **out** the period (1 second before)' => [319_690_799, '762124', 0, false]; | ||
yield 'No leeway, in the period (start)' => [319_690_800, '762124', 0, true]; | ||
yield 'No leeway, in the period (middle)' => [319_690_820, '762124', 0, true]; | ||
yield 'No leeway, in the period (end)' => [319_690_829, '762124', 0, true]; | ||
yield 'No leeway, **out** the period (1 second after)' => [319_690_830, '762124', 0, false]; | ||
yield 'Leeway of 10 seconds, **out** the period of 1sec (1 second after)' => [319_690_830, '762124', 10, true]; | ||
yield 'Leeway of 10 seconds, **out** the period of 10sec (10 second after)' => [ | ||
319_690_839, | ||
'762124', | ||
10, | ||
true, | ||
]; | ||
yield 'Leeway of 10 seconds, **out** the period of 11sec (11 second after)' => [ | ||
319_690_840, | ||
'762124', | ||
10, | ||
false, | ||
]; | ||
} | ||
|
||
/** | ||
|