forked from Spomky-Labs/otphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactoryTest.php
153 lines (133 loc) · 5.92 KB
/
FactoryTest.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
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace OTPHP\Test;
use OTPHP\Factory;
use PHPUnit\Framework\TestCase;
final class FactoryTest extends TestCase
{
public function testTOTPLoad()
{
$otp = 'otpauth://totp/My%20Project%3Aalice%40foo.bar?algorithm=sha512&digits=8&foo=bar.baz&issuer=My%20Project&period=20&secret=JDDK4U6G3BJLEZ7Y';
$result = Factory::loadFromProvisioningUri($otp);
$this->assertInstanceOf('\OTPHP\TOTP', $result);
$this->assertEquals('My Project', $result->getIssuer());
$this->assertEquals('[email protected]', $result->getLabel());
$this->assertEquals('sha512', $result->getDigest());
$this->assertEquals(8, $result->getDigits());
$this->assertEquals(20, $result->getPeriod());
$this->assertEquals('bar.baz', $result->getParameter('foo'));
$this->assertEquals('JDDK4U6G3BJLEZ7Y', $result->getSecret());
$this->assertFalse($result->hasParameter('image'));
$this->assertTrue($result->isIssuerIncludedAsParameter());
$this->assertEquals($otp, $result->getProvisioningUri());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Parameter "image" does not exist
*/
public function testTOTPObjectDoesNotHaveRequestedParameter()
{
$otp = 'otpauth://totp/My%20Project%3Aalice%40foo.bar?algorithm=sha512&digits=8&foo=bar.baz&issuer=My%20Project&period=20&secret=JDDK4U6G3BJLEZ7Y';
$result = Factory::loadFromProvisioningUri($otp);
$result->getParameter('image');
}
public function testHOTPLoad()
{
$otp = 'otpauth://hotp/My%20Project%3Aalice%40foo.bar?counter=1000&digits=8&image=https%3A%2F%2Ffoo.bar%2Fbaz&issuer=My%20Project&secret=JDDK4U6G3BJLEZ7Y';
$result = Factory::loadFromProvisioningUri($otp);
$this->assertInstanceOf('\OTPHP\HOTP', $result);
$this->assertEquals('My Project', $result->getIssuer());
$this->assertEquals('[email protected]', $result->getLabel());
$this->assertEquals('sha1', $result->getDigest());
$this->assertEquals(8, $result->getDigits());
$this->assertEquals(1000, $result->getCounter());
$this->assertEquals('JDDK4U6G3BJLEZ7Y', $result->getSecret());
$this->assertEquals('https://foo.bar/baz', $result->getParameter('image'));
$this->assertTrue($result->isIssuerIncludedAsParameter());
$this->assertEquals($otp, $result->getProvisioningUri());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Not a valid OTP provisioning URI
*/
public function testBadProvisioningUri1()
{
$otp = 'Hello !';
Factory::loadFromProvisioningUri($otp);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Not a valid OTP provisioning URI
*/
public function testBadProvisioningUri2()
{
$otp = 'https://foo.bar/';
Factory::loadFromProvisioningUri($otp);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Unsupported "foo" OTP type
*/
public function testBadProvisioningUri3()
{
$otp = 'otpauth://foo/My%20Project%3Aalice%40foo.bar?counter=1000&digits=8&image=https%3A%2F%2Ffoo.bar%2Fbaz&issuer=My%20Project&secret=JDDK4U6G3BJLEZ7Y';
Factory::loadFromProvisioningUri($otp);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Not a valid OTP provisioning URI
*/
public function testBadProvisioningUri4()
{
$otp = 'otpauth://hotp:My%20Project%3Aalice%40foo.bar?counter=1000&digits=8&image=https%3A%2F%2Ffoo.bar%2Fbaz&issuer=My%20Project&secret=JDDK4U6G3BJLEZ7Y';
Factory::loadFromProvisioningUri($otp);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Not a valid OTP provisioning URI
*/
public function testBadProvisioningUri5()
{
$otp = 'bar://hotp/My%20Project%3Aalice%40foo.bar?counter=1000&digits=8&image=https%3A%2F%2Ffoo.bar%2Fbaz&issuer=My%20Project&secret=JDDK4U6G3BJLEZ7Y';
Factory::loadFromProvisioningUri($otp);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid OTP: invalid issuer in parameter
*/
public function testBadProvisioningUri6()
{
$otp = 'otpauth://hotp/My%20Project2%3Aalice%40foo.bar?counter=1000&digits=8&image=https%3A%2F%2Ffoo.bar%2Fbaz&issuer=My%20Project&secret=JDDK4U6G3BJLEZ7Y';
Factory::loadFromProvisioningUri($otp);
}
public function testTOTPLoadWithoutIssuer()
{
$otp = 'otpauth://totp/My%20Test%20-%20Auth?secret=JDDK4U6G3BJLEZ7Y';
$result = Factory::loadFromProvisioningUri($otp);
$this->assertInstanceOf('\OTPHP\TOTP', $result);
$this->assertNull($result->getIssuer());
$this->assertEquals('My Test - Auth', $result->getLabel());
$this->assertEquals('sha1', $result->getDigest());
$this->assertEquals(6, $result->getDigits());
$this->assertEquals(30, $result->getPeriod());
$this->assertEquals('JDDK4U6G3BJLEZ7Y', $result->getSecret());
$this->assertFalse($result->isIssuerIncludedAsParameter());
$this->assertEquals($otp, $result->getProvisioningUri());
}
public function testTOTPLoadAndRemoveSecretTrailingCharacters()
{
$uri = 'otpauth://totp/My%20Test%20-%20Auth?secret=JDDK4U6G3BJLEQ%3D%3D';
$totp = Factory::loadFromProvisioningUri($uri);
$this->assertInstanceOf('\OTPHP\TOTP', $totp);
$this->assertEquals('JDDK4U6G3BJLEQ', $totp->getSecret());
$this->assertEquals('otpauth://totp/My%20Test%20-%20Auth?secret=JDDK4U6G3BJLEQ', $totp->getProvisioningUri());
}
}