blob: 86dd431edbbba3b10f98e8bcee712c9a87a6a151 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace Tests\Providers\Qr;
4
5use PHPUnit\Framework\TestCase;
6use RobThree\Auth\TwoFactorAuth;
7use RobThree\Auth\TwoFactorAuthException;
8
9class IQRCodeProviderTest extends TestCase
10{
11 /**
12 * @param string $datauri
13 *
14 * @return null|array
15 */
16 private function DecodeDataUri($datauri)
17 {
18 if (preg_match('/data:(?P<mimetype>[\w\.\-\/]+);(?P<encoding>\w+),(?P<data>.*)/', $datauri, $m) === 1) {
19 return array(
20 'mimetype' => $m['mimetype'],
21 'encoding' => $m['encoding'],
22 'data' => base64_decode($m['data'])
23 );
24 }
25
26 return null;
27 }
28
29 /**
30 * @return void
31 */
32 public function testTotpUriIsCorrect()
33 {
34 $qr = new TestQrProvider();
35
36 $tfa = new TwoFactorAuth('Test&Issuer', 6, 30, 'sha1', $qr);
37 $data = $this->DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE'));
38 $this->assertEquals('test/test', $data['mimetype']);
39 $this->assertEquals('base64', $data['encoding']);
40 $this->assertEquals('otpauth://totp/Test%26Label?secret=VMR466AB62ZBOKHE&issuer=Test%26Issuer&period=30&algorithm=SHA1&digits=6@200', $data['data']);
41 }
42
43 /**
44 * @return void
45 */
46 public function testGetQRCodeImageAsDataUriThrowsOnInvalidSize()
47 {
48 $qr = new TestQrProvider();
49
50 $tfa = new TwoFactorAuth('Test', 6, 30, 'sha1', $qr);
51
52 $this->expectException(TwoFactorAuthException::class);
53
54 $tfa->getQRCodeImageAsDataUri('Test', 'VMR466AB62ZBOKHE', 0);
55 }
56}