<?php
declare(strict_types=1);

// Verifies the TOTP implementation against the RFC 6238 test vectors.
// Run after deployment: php bin/selftest.php
require __DIR__ . '/../api/lib/Totp.php';

$secret = Totp::base32Encode('12345678901234567890');
$cases  = [59 => '287082', 1111111109 => '081804', 1234567890 => '005924', 2000000000 => '279037'];

$failed = 0;
foreach ($cases as $time => $expected) {
    $got = Totp::codeAt($secret, intdiv($time, Totp::PERIOD));
    $ok  = $got === $expected;
    $failed += $ok ? 0 : 1;
    printf("t=%-12d expected %s got %s  %s\n", $time, $expected, $got, $ok ? 'OK' : 'FAIL');
}
echo $failed === 0 ? "TOTP implementation is correct.\n" : "{$failed} vector(s) failed.\n";
exit($failed === 0 ? 0 : 1);
