This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathArgon2iTest.php
More file actions
137 lines (124 loc) · 3.93 KB
/
Copy pathArgon2iTest.php
File metadata and controls
137 lines (124 loc) · 3.93 KB
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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Crypt\Password;
use ArrayObject;
use PHPUnit\Framework\TestCase;
use Zend\Crypt\Password\Argon2i;
use Zend\Crypt\Password\Exception;
/**
* @group Zend_Crypt
*/
class Argon2iTest extends TestCase
{
public function testConstruct()
{
if (PHP_VERSION_ID < 70200) {
$this->expectException(Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('The Argon2i password hash is only nativly available on PHP7.2+');
}
$argon2i = new Argon2i();
// This will only be executed on PHP7.2+
$this->assertEquals(PASSWORD_ARGON2_DEFAULT_MEMORY_COST, $argon2i->getMemoryCost());
$this->assertEquals(PASSWORD_ARGON2_DEFAULT_TIME_COST, $argon2i->getTimeCost());
$this->assertEquals(PASSWORD_ARGON2_DEFAULT_THREADS, $argon2i->getThreads());
}
/**
* @requires PHP 7.2
*/
public function testConstructByOptions()
{
$options = [
'memory_cost' => 512,
'time_cost' => 5,
'threads' => 1,
];
$argon2i = new Argon2i($options);
$this->assertEquals(512, $argon2i->getMemoryCost());
$this->assertEquals(5, $argon2i->getTimeCost());
$this->assertEquals(1, $argon2i->getThreads());
}
/**
* This test uses ArrayObject to simulate a Zend\Config\Config instance;
* the class itself only tests for Traversable.
*
* @requires PHP 7.2
*/
public function testConstructByConfig()
{
$options = [
'memory_cost' => 512,
'time_cost' => 5,
'threads' => 1,
];
$config = new ArrayObject($options);
$argon2i = new Argon2i($config);
$this->assertEquals(512, $argon2i->getMemoryCost());
$this->assertEquals(5, $argon2i->getTimeCost());
$this->assertEquals(1, $argon2i->getThreads());
}
/**
* @requires PHP 7.2
*/
public function testWrongConstruct()
{
$this->expectException(Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('The options parameter must be an array or a Traversable');
new Argon2i('test');
}
/**
* @requires PHP 7.2
*/
public function testSetMemoryCost()
{
$argon2i = new Argon2i();
$argon2i->setMemoryCost(512);
$this->assertEquals(512, $argon2i->getMemoryCost());
}
/**
* @requires PHP 7.2
*/
public function testSetTimeCost()
{
$argon2i = new Argon2i();
$argon2i->setTimeCost(10);
$this->assertEquals(10, $argon2i->getTimeCost());
}
/**
* @requires PHP 7.2
*/
public function testCreate()
{
$argon2i = new Argon2i();
$password = $argon2i->create('test');
$this->assertNotEmpty($password);
$this->assertGreaterThanOrEqual(95, strlen($password));
}
/**
* @requires PHP 7.2
*/
public function testVerify()
{
$argon2i = new Argon2i();
$hash = '$argon2i$v=19$m=1024,t=2,p=2$eFlUWVhOMWRjY2swVW1neQ$QASXjIL0nEMfep30FGefpWPdh0wISQpljW3FYPy5m0U';
$this->assertTrue($argon2i->verify('test', $hash));
$this->assertFalse($argon2i->verify('other', $hash));
}
/**
* The class should also positivly verify bcrypt hashes
*
* @requires PHP 7.2
*/
public function testVerifyBcryptHashes()
{
$argon2i = new Argon2i();
$hash = '$2y$10$123456789012345678901uIcehzOq0s9RvVtyXJFIsuuxuE2XZRMq';
$this->assertTrue($argon2i->verify('test', $hash));
$this->assertFalse($argon2i->verify('other', $hash));
}
}