GoogleAuthenticatorTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. require_once __DIR__.'/../vendor/autoload.php';
  3. class GoogleAuthenticatorTest extends PHPUnit_Framework_TestCase
  4. {
  5. /* @var $googleAuthenticator PHPGangsta_GoogleAuthenticator */
  6. protected $googleAuthenticator;
  7. protected function setUp()
  8. {
  9. $this->googleAuthenticator = new PHPGangsta_GoogleAuthenticator();
  10. }
  11. public function codeProvider()
  12. {
  13. // Secret, time, code
  14. return array(
  15. array('SECRET', '0', '200470'),
  16. array('SECRET', '1385909245', '780018'),
  17. array('SECRET', '1378934578', '705013'),
  18. );
  19. }
  20. public function testItCanBeInstantiated()
  21. {
  22. $ga = new PHPGangsta_GoogleAuthenticator();
  23. $this->assertInstanceOf('PHPGangsta_GoogleAuthenticator', $ga);
  24. }
  25. public function testCreateSecretDefaultsToSixteenCharacters()
  26. {
  27. $ga = $this->googleAuthenticator;
  28. $secret = $ga->createSecret();
  29. $this->assertEquals(strlen($secret), 16);
  30. }
  31. public function testCreateSecretLengthCanBeSpecified()
  32. {
  33. $ga = $this->googleAuthenticator;
  34. for ($secretLength = 16; $secretLength < 100; ++$secretLength) {
  35. $secret = $ga->createSecret($secretLength);
  36. $this->assertEquals(strlen($secret), $secretLength);
  37. }
  38. }
  39. /**
  40. * @dataProvider codeProvider
  41. */
  42. public function testGetCodeReturnsCorrectValues($secret, $timeSlice, $code)
  43. {
  44. $generatedCode = $this->googleAuthenticator->getCode($secret, $timeSlice);
  45. $this->assertEquals($code, $generatedCode);
  46. }
  47. public function testGetQRCodeGoogleUrlReturnsCorrectUrl()
  48. {
  49. $secret = 'SECRET';
  50. $name = 'Test';
  51. $url = $this->googleAuthenticator->getQRCodeGoogleUrl($name, $secret);
  52. $urlParts = parse_url($url);
  53. parse_str($urlParts['query'], $queryStringArray);
  54. $this->assertEquals($urlParts['scheme'], 'https');
  55. $this->assertEquals($urlParts['host'], 'api.qrserver.com');
  56. $this->assertEquals($urlParts['path'], '/v1/create-qr-code/');
  57. $expectedChl = 'otpauth://totp/'.$name.'?secret='.$secret;
  58. $this->assertEquals($queryStringArray['data'], $expectedChl);
  59. }
  60. public function testVerifyCode()
  61. {
  62. $secret = 'SECRET';
  63. $code = $this->googleAuthenticator->getCode($secret);
  64. $result = $this->googleAuthenticator->verifyCode($secret, $code);
  65. $this->assertEquals(true, $result);
  66. $code = 'INVALIDCODE';
  67. $result = $this->googleAuthenticator->verifyCode($secret, $code);
  68. $this->assertEquals(false, $result);
  69. }
  70. public function testVerifyCodeWithLeadingZero()
  71. {
  72. $secret = 'SECRET';
  73. $code = $this->googleAuthenticator->getCode($secret);
  74. $result = $this->googleAuthenticator->verifyCode($secret, $code);
  75. $this->assertEquals(true, $result);
  76. $code = '0'.$code;
  77. $result = $this->googleAuthenticator->verifyCode($secret, $code);
  78. $this->assertEquals(false, $result);
  79. }
  80. public function testSetCodeLength()
  81. {
  82. $result = $this->googleAuthenticator->setCodeLength(6);
  83. $this->assertInstanceOf('PHPGangsta_GoogleAuthenticator', $result);
  84. }
  85. }