summaryrefslogtreecommitdiff
path: root/vendor/christian-riesen/otp/tests/Otp
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2017-10-25 16:22:10 -0700
committerFrederic Guillot <fred@kanboard.net>2017-10-25 16:22:10 -0700
commit9e2b2a32fd0e967ad3184e9a5d091a29953acb91 (patch)
tree00822e24aa1110c73ca455a8d096ef296c008cbc /vendor/christian-riesen/otp/tests/Otp
parentc507c5416251c505cb3e088a03c6664bed73c812 (diff)
Include composer dependencies in repo
Diffstat (limited to 'vendor/christian-riesen/otp/tests/Otp')
-rw-r--r--vendor/christian-riesen/otp/tests/Otp/GoogleAuthenticatorTest.php88
-rw-r--r--vendor/christian-riesen/otp/tests/Otp/OtpTest.php124
2 files changed, 212 insertions, 0 deletions
diff --git a/vendor/christian-riesen/otp/tests/Otp/GoogleAuthenticatorTest.php b/vendor/christian-riesen/otp/tests/Otp/GoogleAuthenticatorTest.php
new file mode 100644
index 00000000..219bdaa8
--- /dev/null
+++ b/vendor/christian-riesen/otp/tests/Otp/GoogleAuthenticatorTest.php
@@ -0,0 +1,88 @@
+<?php
+
+require_once __DIR__ . '/../../src/Otp/GoogleAuthenticator.php';
+
+use Otp\GoogleAuthenticator;
+
+/**
+ * GoogleAuthenticator test case.
+ */
+class GoogleAuthenticatorTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * Tests getQrCodeUrl
+ */
+ public function testGetQrCodeUrl()
+ {
+ $secret = 'MEP3EYVA6XNFNVNM'; // testing secret
+
+ // Standard totp case
+ $this->assertEquals(
+ 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%40host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
+ GoogleAuthenticator::getQrCodeUrl('totp', 'user@host.com', $secret)
+ );
+
+ // hotp (include a counter)
+ $this->assertEquals(
+ 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Fhotp%2Fuser%40host.com%3Fsecret%3DMEP3EYVA6XNFNVNM%26counter%3D1234',
+ GoogleAuthenticator::getQrCodeUrl('hotp', 'user@host.com', $secret, 1234)
+ );
+
+ // totp, this time with a parameter for chaning the size of the QR
+ $this->assertEquals(
+ 'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%40host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
+ GoogleAuthenticator::getQrCodeUrl('totp', 'user@host.com', $secret, null, array('height' => 300, 'width' => 300))
+ );
+
+ }
+
+ /**
+ * Tests getKeyUri
+ */
+ public function testGetKeyUri()
+ {
+ $secret = 'MEP3EYVA6XNFNVNM'; // testing secret
+
+ // Standard totp case
+ $this->assertEquals(
+ 'otpauth://totp/user@host.com?secret=MEP3EYVA6XNFNVNM',
+ GoogleAuthenticator::getKeyUri('totp', 'user@host.com', $secret)
+ );
+
+ // hotp (include a counter)
+ $this->assertEquals(
+ 'otpauth://hotp/user@host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
+ GoogleAuthenticator::getKeyUri('hotp', 'user@host.com', $secret, 1234)
+ );
+
+ // totp/hotp with an issuer in the label
+ $this->assertEquals(
+ 'otpauth://hotp/issuer%3Auser@host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
+ GoogleAuthenticator::getKeyUri('hotp', 'issuer:user@host.com', $secret, 1234)
+ );
+
+ // totp/hotp with an issuer and spaces in the label
+ $this->assertEquals(
+ 'otpauth://hotp/an%20issuer%3A%20user@host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
+ GoogleAuthenticator::getKeyUri('hotp', 'an issuer: user@host.com', $secret, 1234)
+ );
+
+ // totp/hotp with an issuer as option
+ $this->assertEquals(
+ 'otpauth://hotp/an%20issuer%3Auser@host.com?secret=MEP3EYVA6XNFNVNM&counter=1234&issuer=an%20issuer',
+ GoogleAuthenticator::getKeyUri('hotp', 'an issuer:user@host.com', $secret, 1234, array('issuer' => 'an issuer'))
+ );
+ }
+
+ /**
+ * Tests generateRandom
+ */
+ public function testGenerateRandom()
+ {
+ // contains numbers 2-7 and letters A-Z in large letters, 16 chars long
+ $this->assertRegExp('/[2-7A-Z]{16}/', GoogleAuthenticator::generateRandom());
+
+ // Can be told to make a longer secret
+ $this->assertRegExp('/[2-7A-Z]{18}/', GoogleAuthenticator::generateRandom(18));
+ }
+}
diff --git a/vendor/christian-riesen/otp/tests/Otp/OtpTest.php b/vendor/christian-riesen/otp/tests/Otp/OtpTest.php
new file mode 100644
index 00000000..ccfdddab
--- /dev/null
+++ b/vendor/christian-riesen/otp/tests/Otp/OtpTest.php
@@ -0,0 +1,124 @@
+<?php
+
+require_once __DIR__ . '/../../src/Otp/OtpInterface.php';
+require_once __DIR__ . '/../../src/Otp/Otp.php';
+
+use Otp\Otp;
+
+/**
+ * Otp test case.
+ */
+class OtpTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ *
+ * @var Otp
+ */
+ private $Otp;
+
+ private $secret = "12345678901234567890";
+
+ /**
+ * Prepares the environment before running a test.
+ */
+ protected function setUp()
+ {
+ parent::setUp();
+
+ $this->Otp = new Otp();
+
+ }
+
+ /**
+ * Cleans up the environment after running a test.
+ */
+ protected function tearDown()
+ {
+ $this->Otp = null;
+
+ parent::tearDown();
+ }
+
+ /**
+ * Tests Otp->hotp()
+ *
+ * Using test vectors from RFC
+ * https://tools.ietf.org/html/rfc4226
+ */
+ public function testHotpRfc()
+ {
+ $secret = $this->secret;
+
+ $this->assertEquals('755224', $this->Otp->hotp($secret, 0));
+ $this->assertEquals('287082', $this->Otp->hotp($secret, 1));
+ $this->assertEquals('359152', $this->Otp->hotp($secret, 2));
+ $this->assertEquals('969429', $this->Otp->hotp($secret, 3));
+ $this->assertEquals('338314', $this->Otp->hotp($secret, 4));
+ $this->assertEquals('254676', $this->Otp->hotp($secret, 5));
+ $this->assertEquals('287922', $this->Otp->hotp($secret, 6));
+ $this->assertEquals('162583', $this->Otp->hotp($secret, 7));
+ $this->assertEquals('399871', $this->Otp->hotp($secret, 8));
+ $this->assertEquals('520489', $this->Otp->hotp($secret, 9));
+ }
+
+ /**
+ * Tests TOTP general construction
+ *
+ * Still uses the hotp function, but since totp is a bit more special, has
+ * its own tests
+ * Using test vectors from RFC
+ * https://tools.ietf.org/html/rfc6238
+ */
+ public function testTotpRfc()
+ {
+ $secret = $this->secret;
+
+ // Test vectors are in 8 digits
+ $this->Otp->setDigits(8);
+
+ // The time presented in the test vector has to be first divided through 30
+ // to count as the key
+
+ // SHA 1 grouping
+ $this->assertEquals('94287082', $this->Otp->hotp($secret, floor(59/30)), 'sha1 with time 59');
+ $this->assertEquals('07081804', $this->Otp->hotp($secret, floor(1111111109/30)), 'sha1 with time 1111111109');
+ $this->assertEquals('14050471', $this->Otp->hotp($secret, floor(1111111111/30)), 'sha1 with time 1111111111');
+ $this->assertEquals('89005924', $this->Otp->hotp($secret, floor(1234567890/30)), 'sha1 with time 1234567890');
+ $this->assertEquals('69279037', $this->Otp->hotp($secret, floor(2000000000/30)), 'sha1 with time 2000000000');
+ $this->assertEquals('65353130', $this->Otp->hotp($secret, floor(20000000000/30)), 'sha1 with time 20000000000');
+
+ /*
+ The following tests do NOT pass.
+ Once the otp class can deal with these correctly, they can be used again.
+ They are here for completeness test vectors from the RFC.
+
+ // SHA 256 grouping
+ $this->Otp->setAlgorithm('sha256');
+ $this->assertEquals('46119246', $this->Otp->hotp($secret, floor(59/30)), 'sha256 with time 59');
+ $this->assertEquals('07081804', $this->Otp->hotp($secret, floor(1111111109/30)), 'sha256 with time 1111111109');
+ $this->assertEquals('14050471', $this->Otp->hotp($secret, floor(1111111111/30)), 'sha256 with time 1111111111');
+ $this->assertEquals('89005924', $this->Otp->hotp($secret, floor(1234567890/30)), 'sha256 with time 1234567890');
+ $this->assertEquals('69279037', $this->Otp->hotp($secret, floor(2000000000/30)), 'sha256 with time 2000000000');
+ $this->assertEquals('65353130', $this->Otp->hotp($secret, floor(20000000000/30)), 'sha256 with time 20000000000');
+
+ // SHA 512 grouping
+ $this->Otp->setAlgorithm('sha512');
+ $this->assertEquals('90693936', $this->Otp->hotp($secret, floor(59/30)), 'sha512 with time 59');
+ $this->assertEquals('25091201', $this->Otp->hotp($secret, floor(1111111109/30)), 'sha512 with time 1111111109');
+ $this->assertEquals('99943326', $this->Otp->hotp($secret, floor(1111111111/30)), 'sha512 with time 1111111111');
+ $this->assertEquals('93441116', $this->Otp->hotp($secret, floor(1234567890/30)), 'sha512 with time 1234567890');
+ $this->assertEquals('38618901', $this->Otp->hotp($secret, floor(2000000000/30)), 'sha512 with time 2000000000');
+ $this->assertEquals('47863826', $this->Otp->hotp($secret, floor(20000000000/30)), 'sha512 with time 20000000000');
+ */
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ * @expectedExceptionMessage Counter must be integer
+ */
+ public function testHotpInvalidCounter()
+ {
+ $this->Otp->hotp($this->secret, 'a');
+ }
+
+}