1 <?php
2
3 class PasswordHashTest extends PHPUnit_Framework_TestCase {
4
5 public function testFuncExists() {
6 $this->assertTrue(function_exists('password_hash'));
7 }
8
9 public function testStringLength() {
10 $this->assertEquals(60, strlen(password_hash('foo', PASSWORD_BCRYPT)));
11 }
12
13 public function testHash() {
14 $hash = password_hash('foo', PASSWORD_BCRYPT);
15 $this->assertEquals($hash, crypt('foo', $hash));
16 }
17
18 public function testKnownSalt() {
19 $hash = password_hash("rasmuslerdorf", PASSWORD_BCRYPT, array("cost" => 7, "salt" => "usesomesillystringforsalt"));
20 $this->assertEquals('$2y$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi', $hash);
21 }
22
23 public function testRawSalt() {
24 $hash = password_hash("test", PASSWORD_BCRYPT, array("salt" => "123456789012345678901" . chr(0)));
25 $this->assertEquals('$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', $hash);
26 }
27
28 29 30
31 public function testInvalidAlgo() {
32 password_hash('foo', array());
33 }
34
35 36 37
38 public function testInvalidAlgo2() {
39 password_hash('foo', 2);
40 }
41
42 43 44
45 public function testInvalidPassword() {
46 password_hash(array(), 1);
47 }
48
49 50 51
52 public function testInvalidSalt() {
53 password_hash('foo', PASSWORD_BCRYPT, array('salt' => array()));
54 }
55
56 57 58
59 public function testInvalidBcryptCostLow() {
60 password_hash('foo', PASSWORD_BCRYPT, array('cost' => 3));
61 }
62
63 64 65
66 public function testInvalidBcryptCostHigh() {
67 password_hash('foo', PASSWORD_BCRYPT, array('cost' => 32));
68 }
69
70 71 72
73 public function testInvalidBcryptCostInvalid() {
74 password_hash('foo', PASSWORD_BCRYPT, array('cost' => 'foo'));
75 }
76
77 78 79
80 public function testInvalidBcryptSaltShort() {
81 password_hash('foo', PASSWORD_BCRYPT, array('salt' => 'abc'));
82 }
83
84 }