-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAuthenticatorTest.php
144 lines (117 loc) · 3.55 KB
/
AuthenticatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/*Loading once*/
require_once __DIR__ . '/Authenticator.php';
class GoogleAuthenticatorTest extends PHPUnit_Framework_TestCase
{
/* @var $googleAuthenticator Authenticator */
protected $googleAuthenticator;
protected function setUp()
{
$this->googleAuthenticator = new Authenticator();
}
public function codeProvider()
{
// Secret, time, code
return array(
array(
'SECRET',
'0',
'200470'
) ,
array(
'SECRET',
'1385909245',
'780018'
) ,
array(
'SECRET',
'1378934578',
'705013'
) ,
);
}
public function testItCanBeInstantiated()
{
$ga = new Authenticator();
$this->assertInstanceOf('Authenticator', $ga);
}
public function testCreateSecretDefaultsToSixteenCharacters()
{
$ga = $this->googleAuthenticator;
$secret = $ga->createSecret();
$this->assertEquals(strlen($secret) , 16);
}
public function testCreateSecretLengthCanBeSpecified()
{
$ga = $this->googleAuthenticator;
for ($secretLength = 16;$secretLength < 100;++$secretLength)
{
$secret = $ga->createSecret($secretLength);
$this->assertEquals(strlen($secret) , $secretLength);
}
}
/**
* @dataProvider codeProvider
*/
public function testGetCodeReturnsCorrectValues($secret, $timeSlice, $code)
{
$generatedCode = $this
->googleAuthenticator
->getCode($secret, $timeSlice);
$this->assertEquals($code, $generatedCode);
}
public function testGetQRCodeGoogleUrlReturnsCorrectUrl()
{
$secret = 'SECRET';
$name = 'Test';
$url = $this
->googleAuthenticator
->getQRCodeGoogleUrl($name, $secret);
$urlParts = parse_url($url);
parse_str($urlParts['query'], $queryStringArray);
$this->assertEquals($urlParts['scheme'], 'https');
$this->assertEquals($urlParts['host'], 'api.qrserver.com');
$this->assertEquals($urlParts['path'], '/v1/create-qr-code/');
$expectedChl = 'otpauth://totp/' . $name . '?secret=' . $secret;
$this->assertEquals($queryStringArray['data'], $expectedChl);
}
public function testVerifyCode()
{
$secret = 'SECRET';
$code = $this
->googleAuthenticator
->getCode($secret);
$result = $this
->googleAuthenticator
->verifyCode($secret, $code);
$this->assertEquals(true, $result);
$code = 'INVALIDCODE';
$result = $this
->googleAuthenticator
->verifyCode($secret, $code);
$this->assertEquals(false, $result);
}
public function testVerifyCodeWithLeadingZero()
{
$secret = 'SECRET';
$code = $this
->googleAuthenticator
->getCode($secret);
$result = $this
->googleAuthenticator
->verifyCode($secret, $code);
$this->assertEquals(true, $result);
$code = '0' . $code;
$result = $this
->googleAuthenticator
->verifyCode($secret, $code);
$this->assertEquals(false, $result);
}
public function testSetCodeLength()
{
$result = $this
->googleAuthenticator
->setCodeLength(6);
$this->assertInstanceOf('Authenticator', $result);
}
}