GoogleAuthenticatorTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Otp\Tests;
  3. use Otp\GoogleAuthenticator;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * GoogleAuthenticator test case.
  7. */
  8. class GoogleAuthenticatorTest extends TestCase
  9. {
  10. /**
  11. * Tests getQrCodeUrl
  12. */
  13. public function testGetQrCodeUrl()
  14. {
  15. $secret = 'MEP3EYVA6XNFNVNM'; // testing secret
  16. // Standard totp case
  17. $this->assertEquals(
  18. 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%2540host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
  19. GoogleAuthenticator::getQrCodeUrl('totp', 'user@host.com', $secret)
  20. );
  21. // hotp (include a counter)
  22. $this->assertEquals(
  23. 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Fhotp%2Fuser%2540host.com%3Fsecret%3DMEP3EYVA6XNFNVNM%26counter%3D1234',
  24. GoogleAuthenticator::getQrCodeUrl('hotp', 'user@host.com', $secret, 1234)
  25. );
  26. // totp, this time with a parameter for changing the size of the QR
  27. $this->assertEquals(
  28. 'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%2540host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
  29. GoogleAuthenticator::getQrCodeUrl('totp', 'user@host.com', $secret, null, array('height' => 300, 'width' => 300))
  30. );
  31. }
  32. /**
  33. * Tests getKeyUri
  34. */
  35. public function testGetKeyUri()
  36. {
  37. $secret = 'MEP3EYVA6XNFNVNM'; // testing secret
  38. // Standard totp case
  39. $this->assertEquals(
  40. 'otpauth://totp/user%40host.com?secret=MEP3EYVA6XNFNVNM',
  41. GoogleAuthenticator::getKeyUri('totp', 'user@host.com', $secret)
  42. );
  43. // hotp (include a counter)
  44. $this->assertEquals(
  45. 'otpauth://hotp/user%40host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
  46. GoogleAuthenticator::getKeyUri('hotp', 'user@host.com', $secret, 1234)
  47. );
  48. // totp/hotp with an issuer in the label
  49. $this->assertEquals(
  50. 'otpauth://hotp/issuer%3Auser%40host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
  51. GoogleAuthenticator::getKeyUri('hotp', 'issuer:user@host.com', $secret, 1234)
  52. );
  53. // totp/hotp with an issuer and spaces in the label
  54. $this->assertEquals(
  55. 'otpauth://hotp/an%20issuer%3A%20user%40host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
  56. GoogleAuthenticator::getKeyUri('hotp', 'an issuer: user@host.com', $secret, 1234)
  57. );
  58. // totp/hotp with an issuer as option
  59. $this->assertEquals(
  60. 'otpauth://hotp/an%20issuer%3Auser%40host.com?secret=MEP3EYVA6XNFNVNM&counter=1234&issuer=an%20issuer',
  61. GoogleAuthenticator::getKeyUri('hotp', 'an issuer:user@host.com', $secret, 1234, array('issuer' => 'an issuer'))
  62. );
  63. }
  64. /**
  65. * Tests generateRandom
  66. */
  67. public function testGenerateRandom()
  68. {
  69. // contains numbers 2-7 and letters A-Z in large letters, 16 chars long
  70. $this->assertRegExp('/[2-7A-Z]{16}/', GoogleAuthenticator::generateRandom());
  71. // Can be told to make a longer secret
  72. $this->assertRegExp('/[2-7A-Z]{18}/', GoogleAuthenticator::generateRandom(18));
  73. }
  74. /**
  75. * Test generateRecoveryCodes
  76. */
  77. public function testGenerateRecoveryCodes()
  78. {
  79. // Default settings
  80. $codes = GoogleAuthenticator::generateRecoveryCodes();
  81. $this->assertCount(1, $codes);
  82. $this->assertRegExp('/[0-9]{9}/', $codes[0]);
  83. // More codes, longer
  84. $codes = GoogleAuthenticator::generateRecoveryCodes(4, 20);
  85. $this->assertCount(4, $codes);
  86. $this->assertRegExp('/[0-9]{9}/', $codes[0]);
  87. $this->assertRegExp('/[0-9]{9}/', $codes[1]);
  88. $this->assertRegExp('/[0-9]{9}/', $codes[2]);
  89. $this->assertRegExp('/[0-9]{9}/', $codes[3]);
  90. // To check for uniqueness
  91. $this->assertSame($codes, array_unique($codes));
  92. }
  93. }