PHP Classes

File: tests/Unit/HOTPTest.php

Recommend this page to a friend!
  Classes of Stanley Aloh   Stanliwise TOTP   tests/Unit/HOTPTest.php   Download  
File: tests/Unit/HOTPTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test class source
Class: Stanliwise TOTP
Generate and validate time-based OTP tokens
Author: By
Last change:
Date: 4 months ago
Size: 1,507 bytes
 

Contents

Class file image Download
<?php

namespace Test\Unit;

use
OTP\Base32;
use
OTP\TOTP;
use
OTP\Utils;
use
PHPUnit\Framework\TestCase;

class
HOTPTest extends TestCase
{
   
/**
     * @var \OTP\TOTP
     */
   
protected $hotp;

   
/**
     * @var string
     */
   
protected $secret;

    public function
setUp(): void
   
{
       
parent::setUp();
       
$this->hotp = new TOTP("12345678901234567890", 3, true);
    }

    public function
test_token_and_tokenAt()
    {
       
$first_otp = $this->hotp->token();
       
$second_otp = $this->hotp->tokenAt(time() - 30, 1);
        return
$this->assertEquals($first_otp, $second_otp);
    }

    public function
test_token_support_delay_tolerance()
    {
       
$now = time();
       
$token_1 = $this->hotp->tokenAt($now - 30); //remove 30 seconds
       
$token_2 = $this->hotp->tokenAt($now + 20); //add 20 seconds
       
$this->assertTrue($this->hotp->verify($token_1));
       
$this->assertTrue($this->hotp->verify($token_2));
    }

    public function
testBase32Encoding()
    {
       
$this->assertEquals(Base32::encode("12345678901234567890"), 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ');
       
$this->assertEquals(Base32::decode("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"), "12345678901234567890");
    }

    public function
test_verify_token_by_windows()
    {
       
$this->assertTrue($this->hotp->validateByWindow(
           
$this->hotp->token(2), 0
       
));
       
#Utils::debug('hi');
       
$this->assertTrue($this->hotp->verify($this->hotp->token(-29)));
    }
}