mirror of
https://github.com/RobThree/TwoFactorAuth.git
synced 2026-08-17 16:52:57 +00:00
📚 doc blocks
This commit is contained in:
@@ -9,6 +9,8 @@ trait MightNotMakeAssertions
|
||||
*
|
||||
* It has to be named something that doesn't collide with existing
|
||||
* TestCase methods as we can't support PHP return types right now
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function noAssertionsMade()
|
||||
{
|
||||
|
||||
@@ -8,6 +8,11 @@ use RobThree\Auth\TwoFactorAuthException;
|
||||
|
||||
class IQRCodeProviderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param string $datauri
|
||||
*
|
||||
* @return null|array
|
||||
*/
|
||||
private function DecodeDataUri($datauri)
|
||||
{
|
||||
if (preg_match('/data:(?P<mimetype>[\w\.\-\/]+);(?P<encoding>\w+),(?P<data>.*)/', $datauri, $m) === 1) {
|
||||
@@ -21,6 +26,9 @@ class IQRCodeProviderTest extends TestCase
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testTotpUriIsCorrect()
|
||||
{
|
||||
$qr = new TestQrProvider();
|
||||
@@ -32,6 +40,9 @@ class IQRCodeProviderTest extends TestCase
|
||||
$this->assertEquals('otpauth://totp/Test%26Label?secret=VMR466AB62ZBOKHE&issuer=Test%26Issuer&period=30&algorithm=SHA1&digits=6@200', $data['data']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetQRCodeImageAsDataUriThrowsOnInvalidSize()
|
||||
{
|
||||
$qr = new TestQrProvider();
|
||||
|
||||
@@ -6,11 +6,17 @@ use RobThree\Auth\Providers\Qr\IQRCodeProvider;
|
||||
|
||||
class TestQrProvider implements IQRCodeProvider
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getQRCodeImage($qrtext, $size)
|
||||
{
|
||||
return $qrtext . '@' . $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMimeType()
|
||||
{
|
||||
return 'test/test';
|
||||
|
||||
@@ -12,6 +12,8 @@ class CSRNGProviderTest extends TestCase
|
||||
|
||||
/**
|
||||
* @requires function random_bytes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCSRNGProvidersReturnExpectedNumberOfBytes()
|
||||
{
|
||||
|
||||
@@ -9,6 +9,9 @@ class HashRNGProviderTest extends TestCase
|
||||
{
|
||||
use NeedsRngLengths;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testHashRNGProvidersReturnExpectedNumberOfBytes()
|
||||
{
|
||||
$rng = new HashRNGProvider();
|
||||
|
||||
@@ -8,6 +8,9 @@ use RobThree\Auth\TwoFactorAuthException;
|
||||
|
||||
class IRNGProviderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateSecretThrowsOnInsecureRNGProvider()
|
||||
{
|
||||
$rng = new TestRNGProvider();
|
||||
@@ -18,6 +21,9 @@ class IRNGProviderTest extends TestCase
|
||||
$tfa->createSecret();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateSecretOverrideSecureDoesNotThrowOnInsecureRNG()
|
||||
{
|
||||
$rng = new TestRNGProvider();
|
||||
@@ -26,6 +32,9 @@ class IRNGProviderTest extends TestCase
|
||||
$this->assertEquals('ABCDEFGHIJKLMNOP', $tfa->createSecret(80, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateSecretDoesNotThrowOnSecureRNGProvider()
|
||||
{
|
||||
$rng = new TestRNGProvider(true);
|
||||
@@ -34,6 +43,9 @@ class IRNGProviderTest extends TestCase
|
||||
$this->assertEquals('ABCDEFGHIJKLMNOP', $tfa->createSecret());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateSecretGeneratesDesiredAmountOfEntropy()
|
||||
{
|
||||
$rng = new TestRNGProvider(true);
|
||||
|
||||
@@ -12,6 +12,8 @@ class MCryptRNGProviderTest extends TestCase
|
||||
|
||||
/**
|
||||
* @requires function mcrypt_create_iv
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMCryptRNGProvidersReturnExpectedNumberOfBytes()
|
||||
{
|
||||
|
||||
@@ -4,5 +4,6 @@ namespace Tests\Providers\Rng;
|
||||
|
||||
trait NeedsRngLengths
|
||||
{
|
||||
/** @var array */
|
||||
protected $rngTestLengths = array(1, 16, 32, 256);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ class OpenSSLRNGProviderTest extends TestCase
|
||||
{
|
||||
use NeedsRngLengths;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testStrongOpenSSLRNGProvidersReturnExpectedNumberOfBytes()
|
||||
{
|
||||
$rng = new OpenSSLRNGProvider(true);
|
||||
@@ -19,6 +22,9 @@ class OpenSSLRNGProviderTest extends TestCase
|
||||
$this->assertTrue($rng->isCryptographicallySecure());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNonStrongOpenSSLRNGProvidersReturnExpectedNumberOfBytes()
|
||||
{
|
||||
$rng = new OpenSSLRNGProvider(false);
|
||||
|
||||
@@ -6,13 +6,20 @@ use RobThree\Auth\Providers\Rng\IRNGProvider;
|
||||
|
||||
class TestRNGProvider implements IRNGProvider
|
||||
{
|
||||
/** @var bool */
|
||||
private $isSecure;
|
||||
|
||||
/**
|
||||
* @param bool $isSecure whether this provider is cryptographically secure
|
||||
*/
|
||||
function __construct($isSecure = false)
|
||||
{
|
||||
$this->isSecure = $isSecure;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRandomBytes($bytecount)
|
||||
{
|
||||
$result = '';
|
||||
@@ -24,6 +31,9 @@ class TestRNGProvider implements IRNGProvider
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isCryptographicallySecure()
|
||||
{
|
||||
return $this->isSecure;
|
||||
|
||||
@@ -11,6 +11,9 @@ class ITimeProviderTest extends TestCase
|
||||
{
|
||||
use MightNotMakeAssertions;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testEnsureCorrectTimeDoesNotThrowForCorrectTime()
|
||||
{
|
||||
$tpr1 = new TestTimeProvider(123);
|
||||
@@ -22,6 +25,9 @@ class ITimeProviderTest extends TestCase
|
||||
$this->noAssertionsMade();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testEnsureCorrectTimeThrowsOnIncorrectTime()
|
||||
{
|
||||
$tpr1 = new TestTimeProvider(123);
|
||||
@@ -34,6 +40,9 @@ class ITimeProviderTest extends TestCase
|
||||
$tfa->ensureCorrectTime(array($tpr2), 0); // We force a leniency of 0, 124-123 = 1 so this should throw
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testEnsureDefaultTimeProviderReturnsCorrectTime()
|
||||
{
|
||||
$tfa = new TwoFactorAuth('Test', 6, 30, 'sha1');
|
||||
|
||||
@@ -6,13 +6,20 @@ use RobThree\Auth\Providers\Time\ITimeProvider;
|
||||
|
||||
class TestTimeProvider implements ITimeProvider
|
||||
{
|
||||
/** @var int */
|
||||
private $time;
|
||||
|
||||
/**
|
||||
* @param int $time
|
||||
*/
|
||||
function __construct($time)
|
||||
{
|
||||
$this->time = $time;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTime()
|
||||
{
|
||||
return $this->time;
|
||||
|
||||
@@ -10,6 +10,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
{
|
||||
use MightNotMakeAssertions;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testConstructorThrowsOnInvalidDigits()
|
||||
{
|
||||
$this->expectException(TwoFactorAuthException::class);
|
||||
@@ -17,6 +20,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
new TwoFactorAuth('Test', 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testConstructorThrowsOnInvalidPeriod()
|
||||
{
|
||||
$this->expectException(TwoFactorAuthException::class);
|
||||
@@ -24,6 +30,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
new TwoFactorAuth('Test', 6, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testConstructorThrowsOnInvalidAlgorithm()
|
||||
{
|
||||
$this->expectException(TwoFactorAuthException::class);
|
||||
@@ -31,6 +40,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
new TwoFactorAuth('Test', 6, 30, 'xxx');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCodeReturnsCorrectResults()
|
||||
{
|
||||
$tfa = new TwoFactorAuth('Test');
|
||||
@@ -38,6 +50,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
$this->assertEquals('538532', $tfa->getCode('VMR466AB62ZBOKHE', 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testEnsureAllTimeProvidersReturnCorrectTime()
|
||||
{
|
||||
$tfa = new TwoFactorAuth('Test', 6, 30, 'sha1');
|
||||
@@ -51,6 +66,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
$this->noAssertionsMade();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testVerifyCodeWorksCorrectly()
|
||||
{
|
||||
$tfa = new TwoFactorAuth('Test', 6, 30);
|
||||
@@ -70,6 +88,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
$this->assertTrue($tfa->verifyCode('VMR466AB62ZBOKHE', '543160', 2, 1426847205 - 65)); //Test discrepancy
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testVerifyCorrectTimeSliceIsReturned()
|
||||
{
|
||||
$tfa = new TwoFactorAuth('Test', 6, 30);
|
||||
@@ -96,6 +117,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
$this->assertEquals(0, $timeslice8);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCodeThrowsOnInvalidBase32String1()
|
||||
{
|
||||
$tfa = new TwoFactorAuth('Test');
|
||||
@@ -105,6 +129,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
$tfa->getCode('FOO1BAR8BAZ9'); //1, 8 & 9 are invalid chars
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCodeThrowsOnInvalidBase32String2()
|
||||
{
|
||||
$tfa = new TwoFactorAuth('Test');
|
||||
@@ -114,6 +141,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
$tfa->getCode('mzxw6==='); //Lowercase
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testKnownBase32DecodeTestVectors()
|
||||
{
|
||||
// We usually don't test internals (e.g. privates) but since we rely heavily on base32 decoding and don't want
|
||||
@@ -142,6 +172,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
$this->assertEquals('foobar', $method->invoke($tfa, 'MZXW6YTBOI======'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testKnownBase32DecodeUnpaddedTestVectors()
|
||||
{
|
||||
// See testKnownBase32DecodeTestVectors() for the rationale behind testing the private base32Decode() method.
|
||||
@@ -163,6 +196,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
$this->assertEquals('foobar', $method->invoke($tfa, 'MZXW6YTBOI'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testKnownTestVectors_sha1()
|
||||
{
|
||||
//Known test vectors for SHA1: https://tools.ietf.org/html/rfc6238#page-15
|
||||
@@ -176,6 +212,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
$this->assertEquals('65353130', $tfa->getCode($secret, 20000000000));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testKnownTestVectors_sha256()
|
||||
{
|
||||
//Known test vectors for SHA256: https://tools.ietf.org/html/rfc6238#page-15
|
||||
@@ -189,6 +228,9 @@ class TwoFactorAuthTest extends TestCase
|
||||
$this->assertEquals('77737706', $tfa->getCode($secret, 20000000000));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testKnownTestVectors_sha512()
|
||||
{
|
||||
//Known test vectors for SHA512: https://tools.ietf.org/html/rfc6238#page-15
|
||||
|
||||
Reference in New Issue
Block a user