Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Apr 15, 2024
1 parent 663814e commit 5493259
Showing 1 changed file with 66 additions and 27 deletions.
93 changes: 66 additions & 27 deletions tests/TOTPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPUnit\Framework\TestCase;
use Psr\Clock\ClockInterface;
use RuntimeException;
use Symfony\Bridge\PhpUnit\ClockMock;
use function assert;

/**
Expand Down Expand Up @@ -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')]
Expand All @@ -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')]
Expand All @@ -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]
Expand Down Expand Up @@ -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,
];
}

/**
Expand Down

0 comments on commit 5493259

Please sign in to comment.