aboutsummaryrefslogtreecommitdiffstats
path: root/tests/passwords/manager_test.php
blob: ee295ff043ee425600ed12136637ce760490fe41 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
*
* @package testing
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

class phpbb_passwords_manager_test extends PHPUnit_Framework_TestCase
{
	protected $passwords_drivers;

	protected $pw_characters = '0123456789abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVXYZ.,_!?/\\';

	protected $default_pw = 'foobar';

	public function setUp()
	{
		// Prepare dependencies for manager and driver
		$config =  new \phpbb\config\config(array());
		$this->driver_helper = new \phpbb\passwords\driver\helper($config);

		$this->passwords_drivers = array(
			'passwords.driver.bcrypt_2y'	=> new \phpbb\passwords\driver\bcrypt_2y($config, $this->driver_helper),
			'passwords.driver.bcrypt'		=> new \phpbb\passwords\driver\bcrypt($config, $this->driver_helper),
			'passwords.driver.salted_md5'	=> new \phpbb\passwords\driver\salted_md5($config, $this->driver_helper),
			'passwords.driver.phpass'		=> new \phpbb\passwords\driver\phpass($config, $this->driver_helper),
		);

		$this->helper = new \phpbb\passwords\helper;
		// Set up passwords manager
		$this->manager = new \phpbb\passwords\manager($config, $this->passwords_drivers, $this->helper, array_keys($this->passwords_drivers));
	}

	public function hash_password_data()
	{
		if (version_compare(PHP_VERSION, '5.3.7', '<'))
		{
			return array(
				array('', '2a', 60),
				array('passwords.driver.bcrypt_2y', '2a', 60),
				array('passwords.driver.bcrypt', '2a', 60),
				array('passwords.driver.salted_md5', 'H', 34),
				array('passwords.driver.foobar', '', false),
			);
		}
		else
		{
			return array(
				array('', '2y', 60),
				array('passwords.driver.bcrypt_2y', '2y', 60),
				array('passwords.driver.bcrypt', '2a', 60),
				array('passwords.driver.salted_md5', 'H', 34),
				array('passwords.driver.foobar', '', false),
			);
		}
	}

	/**
	* @dataProvider hash_password_data
	*/
	public function test_hash_password($type, $prefix, $length)
	{
		$password = $this->default_pw;

		if (!$length)
		{
			$this->assertEquals(false, $hash = $this->manager->hash($password, $type));
			return;
		}
		$time = microtime(true);

		// Limit each test to 1 second
		while ((microtime(true) - $time) < 1)
		{
			$hash = $this->manager->hash($password, $type);
			preg_match('#^\$([a-zA-Z0-9\\\]*?)\$#', $hash, $match);
			$this->assertEquals($prefix, $match[1]);
			$this->assertEquals($length, strlen($hash));
			$password .= $this->pw_characters[mt_rand(0, 66)];
		}
	}

	public function check_password_data()
	{
		if (version_compare(PHP_VERSION, '5.3.7', '<'))
		{
			return array(
				array('passwords.driver.bcrypt'),
				array('passwords.driver.salted_md5'),
				array('passwords.driver.phpass'),
			);
		}
		else
		{
			return array(
				array('passwords.driver.bcrypt_2y'),
				array('passwords.driver.bcrypt'),
				array('passwords.driver.salted_md5'),
				array('passwords.driver.phpass'),
			);
		}
	}

	/**
	* @dataProvider check_password_data
	*/
	public function test_check_password($hash_type)
	{
		$password = $this->default_pw;
		$time = microtime(true);
		// Limit each test to 1 second
		while ((microtime(true) - $time) < 1)
		{
			$hash = $this->manager->hash($password, $hash_type);
			$this->assertEquals(true, $this->manager->check($password, $hash));
			$password .= $this->pw_characters[mt_rand(0, 66)];
			$this->assertEquals(false, $this->manager->check($password, $hash));
		}

		// Check if convert_flag is correctly set
		$default_type = (version_compare(PHP_VERSION, '5.3.7', '<')) ? 'passwords.driver.bcrypt' : 'passwords.driver.bcrypt_2y';
		$this->assertEquals(($hash_type !== $default_type), $this->manager->convert_flag);
	}


	public function check_hash_exceptions_data()
	{
		return array(
			array('foobar', '3858f62230ac3c915f300c664312c63f', true),
			array('foobar', '$S$b57a939fa4f2c04413a4eea9734a0903647b7adb93181295', false),
			array('foobar', '$2a\S$kkkkaakdkdiej39023903204j2k3490234jk234j02349', false),
			array('foobar', '$H$kklk938d023k//k3023', false),
			array('foobar', '$H$3PtYMgXb39lrIWkgoxYLWtRkZtY3AY/', false),
			array('foobar', '$2a$kwiweorurlaeirw', false),
		);
	}

	/**
	* @dataProvider check_hash_exceptions_data
	*/
	public function test_check_hash_exceptions($password, $hash, $expected)
	{
		$this->assertEquals($expected, $this->manager->check($password, $hash));
	}

	public function data_hash_password_length()
	{
		return array(
			array('passwords.driver.bcrypt', false),
			array('passwords.driver.bcrypt_2y', false),
			array('passwords.driver.salted_md5', '3858f62230ac3c915f300c664312c63f'),
			array('passwords.driver.phpass', '3858f62230ac3c915f300c664312c63f'),
		);
	}

	/**
	* @dataProvider data_hash_password_length
	*/
	public function test_hash_password_length($driver, $expected)
	{
		$this->assertEquals($expected, $this->passwords_drivers[$driver]->hash('foobar', 'foobar'));
	}

	public function test_hash_password_8bit_bcrypt()
	{
		$this->assertEquals(false, $this->manager->hash('foobar𝄞', 'passwords.driver.bcrypt'));
		if (version_compare(PHP_VERSION, '5.3.7', '<'))
		{
			$this->assertEquals(false, $this->manager->hash('foobar𝄞', 'passwords.driver.bcrypt_2y'));
		}
		else
		{
			$this->assertNotEquals(false, $this->manager->hash('foobar𝄞', 'passwords.driver.bcrypt_2y'));
		}
	}

	public function test_combined_hash_data()
	{
		if (version_compare(PHP_VERSION, '5.3.7', '<'))
		{
			return array(
				array(
					'passwords.driver.salted_md5',
					array('passwords.driver.bcrypt'),
				),
				array(
					'passwords.driver.phpass',
					array('passwords.driver.salted_md5'),
				),
				array(
					'passwords.driver.salted_md5',
					array('passwords.driver.phpass', 'passwords.driver.bcrypt'),
				),
				array(
					'passwords.driver.salted_md5',
					array('passwords.driver.salted_md5'),
					false,
				),
				array(
					'$H$',
					array('$2a$'),
				),
			);
		}
		else
		{
			return array(
				array(
					'passwords.driver.salted_md5',
					array('passwords.driver.bcrypt_2y'),
				),
				array(
					'passwords.driver.salted_md5',
					array('passwords.driver.bcrypt'),
				),
				array(
					'passwords.driver.phpass',
					array('passwords.driver.salted_md5'),
				),
				array(
					'passwords.driver.salted_md5',
					array('passwords.driver.bcrypt_2y', 'passwords.driver.bcrypt'),
				),
				array(
					'passwords.driver.salted_md5',
					array('passwords.driver.salted_md5'),
					false,
				),
				array(
					'passwords.driver.bcrypt_2y',
					array('passwords.driver.salted_md4'),
					false,
				),
				array(
					'$H$',
					array('$2y$'),
				),
			);
		}
	}

	/**
	* @dataProvider test_combined_hash_data
	*/
	public function test_combined_hash_password($first_type, $second_type, $expected = true)
	{
		$password = $this->default_pw;
		$time = microtime(true);
		// Limit each test to 1 second
		while ((microtime(true) - $time) < 1)
		{
			$hash = $this->manager->hash($password, $first_type);
			$combined_hash = $this->manager->hash($hash, $second_type);
			$this->assertEquals($expected, $this->manager->check($password, $combined_hash));
			$password .= $this->pw_characters[mt_rand(0, 66)];
			$this->assertEquals(false, $this->manager->check($password, $combined_hash));

			// If we are expecting the check to fail then there is
			// no need to run this more than once
			if (!$expected)
			{
				break;
			}
		}
	}

	public function test_unique_id()
	{
		$time = microtime(true);
		$first_id = $this->driver_helper->unique_id();
		// Limit test to 1 second
		while ((microtime(true) - $time) < 1)
		{
			$this->assertNotEquals($first_id, $this->driver_helper->unique_id());
		}
	}

	public function test_check_hash_with_large_input()
	{
		// 16 MB password, should be rejected quite fast
		$start_time = time();
		$this->assertFalse($this->manager->check(str_repeat('a', 1024 * 1024 * 16), '$H$9isfrtKXWqrz8PvztXlL3.daw4U0zI1'));
		$this->assertLessThanOrEqual(5, time() - $start_time);
	}

	public function test_hash_password_with_large_input()
	{
		// 16 MB password, should be rejected quite fast
		$start_time = time();
		$this->assertFalse($this->manager->hash(str_repeat('a', 1024 * 1024 * 16)));
		$this->assertLessThanOrEqual(5, time() - $start_time);
	}
}