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
|
<?php
use CraftBlue\GoogleAuthenticator;
class GoogleAuthenticatorTest extends PHPUnit_Framework_TestCase {
/**
* @var GoogleAuthenticator
*/
protected $ga;
/**
* PHPUnit setup.
*/
protected function setUp()
{
$this->ga = new GoogleAuthenticator();
parent::setUp();
}
/**
*
*/
public function testItCanBeInstantiated()
{
$ga = new GoogleAuthenticator();
$this->assertInstanceOf(CraftBlue\GoogleAuthenticator::class, $ga);
}
/**
* Ensure that our default secret is 16 characters long.
*/
public function testCreateSecretDefaultsToSixteenCharacters()
{
$secret = $this->ga->createSecret();
$this->assertEquals(strlen($secret), 16);
}
/**
* Ensure that the user can create a secret of varying lengths from 0 to 100.
*/
public function testCreateSecretLengthCanBeCustomized()
{
for ($secretLength = 10; $secretLength < 100; $secretLength++) {
$secret = $this->ga->createSecret($secretLength);
$this->assertEquals(strlen($secret), $secretLength);
}
}
/**
* Ensure that the generated QR code URL is as expected.
*/
public function testgetQRCodeGoogleUrlReturnsCorrectUrl()
{
$secret = 'SECRET';
$name = 'Test';
$url = $this->ga->getQRCodeUrl($name, $secret);
$urlParts = parse_url($url);
parse_str($urlParts['query'], $queryStringArray);
$this->assertEquals($urlParts['scheme'], 'https');
$this->assertEquals($urlParts['host'], 'chart.googleapis.com');
$this->assertEquals($urlParts['path'], '/chart');
$expectedChl = 'otpauth://totp/' . $name . '?secret=' . $secret;
$this->assertEquals($queryStringArray['chl'], $expectedChl);
}
/**
* Test that we properly verify both a valid and invalid code when using the current timestamp.
*/
public function testVerifyCode()
{
$secret = 'SECRET';
$code = $this->ga->getCode($secret);
$result = $this->ga->verifyCode($secret, $code);
$this->assertEquals(true, $result);
$code = 'INVALIDCODE';
$result = $this->ga->verifyCode($secret, $code);
$this->assertEquals(false, $result);
}
/**
* Ensure that we can adjust the length of the code and still have the instance returned.
*/
public function testSetCodeLength()
{
$result = $this->ga->setCodeLength(6);
$this->assertInstanceOf(CraftBlue\GoogleAuthenticator::class, $result);
}
/**
* Validate that a code generated with a specific secret and time slice matches the anticipated output.
*
* @param string $secret The secret value used to hash the code
* @param int $timeSlice The timestamp passed at the time of the code creation
* @param string $code The resulting code that should be generated
* @dataProvider codeProvider
*/
public function testGetCodeReturnsCorrectValues($secret, $timeSlice, $code)
{
$generatedCode = $this->ga->getCode($secret, $timeSlice);
$this->assertEquals($code, $generatedCode);
}
public function testSomeShit()
{
$secret = $this->ga->createSecret();
$code = $this->ga->getCode($secret);
$result = $this->ga->verifyCode($secret, $code, 1);
echo print_r($result, true);
}
/**
* A provider of codes to verify for correctness.
*
* @return array
*/
public function codeProvider()
{
return array(
// secret, time, code
array('SECRET', 0, '857148'),
array('SECRET', 1385909245, '979377'),
array('SECRET', 1378934578, '560773'),
);
}
}
|