Merge branch 'MDL-49520-master' of git://github.com/gurgus/moodle
[moodle.git] / lib / password_compat / tests / PasswordHashTest.php
1 <?php
3 global $CFG;
4 require_once($CFG->dirroot . '/lib/password_compat/lib/password.php');
6 class PasswordHashTest extends PHPUnit_Framework_TestCase {
7     
8     public function testFuncExists() {
9         $this->assertTrue(function_exists('password_hash'));
10     }
12     public function testStringLength() {
13         $this->assertEquals(60, strlen(password_hash('foo', PASSWORD_BCRYPT)));
14     }
16     public function testHash() {
17         $hash = password_hash('foo', PASSWORD_BCRYPT);
18         $this->assertEquals($hash, crypt('foo', $hash));
19     }
21     public function testKnownSalt() {
22         $hash = password_hash("rasmuslerdorf", PASSWORD_BCRYPT, array("cost" => 7, "salt" => "usesomesillystringforsalt"));
23         $this->assertEquals('$2y$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi', $hash);
24     }
26     public function testRawSalt() {
27         $hash = password_hash("test", PASSWORD_BCRYPT, array("salt" => "123456789012345678901" . chr(0)));
28         if (version_compare(PHP_VERSION, '5.5.0', '<')) {
29             $this->assertEquals('$2y$10$KRGxLBS0Lxe3KBCwKxOzLexLDeu0ZfqJAKTubOfy7O/yL2hjimw3u', $hash);
30         } else {
31             $this->assertEquals('$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', $hash);
32         }
33     }
35     public function testNullBehavior() {
36         $hash = password_hash(null, PASSWORD_BCRYPT, array("salt" => "1234567890123456789012345678901234567890"));
37         $this->assertEquals('$2y$10$123456789012345678901uhihPb9QpE2n03zMu9TDdvO34jDn6mO.', $hash);
38     }
40     public function testIntegerBehavior() {
41         $hash = password_hash(12345, PASSWORD_BCRYPT, array("salt" => "1234567890123456789012345678901234567890"));
42         $this->assertEquals('$2y$10$123456789012345678901ujczD5TiARVFtc68bZCAlbEg1fCIexfO', $hash);
43     }    
45     /**
46      * @expectedException PHPUnit_Framework_Error
47      */
48     public function testInvalidAlgo() {
49         password_hash('foo', array());
50     }
52     /**
53      * @expectedException PHPUnit_Framework_Error
54      */
55     public function testInvalidAlgo2() {
56         password_hash('foo', 2);
57     }
59     /**
60      * @expectedException PHPUnit_Framework_Error
61      */
62     public function testInvalidPassword() {
63         password_hash(array(), 1);
64     }
66     /**
67      * @expectedException PHPUnit_Framework_Error
68      */
69     public function testInvalidSalt() {
70         password_hash('foo', PASSWORD_BCRYPT, array('salt' => array()));
71     }
73     /**
74      * @expectedException PHPUnit_Framework_Error
75      */
76     public function testInvalidBcryptCostLow() {
77         password_hash('foo', PASSWORD_BCRYPT, array('cost' => 3));
78     }
79         
80     /**
81      * @expectedException PHPUnit_Framework_Error
82      */
83     public function testInvalidBcryptCostHigh() {
84         password_hash('foo', PASSWORD_BCRYPT, array('cost' => 32));
85     }
87     /**
88      * @expectedException PHPUnit_Framework_Error
89      */
90     public function testInvalidBcryptCostInvalid() {
91         password_hash('foo', PASSWORD_BCRYPT, array('cost' => 'foo'));
92     }
94     /**
95      * @expectedException PHPUnit_Framework_Error
96      */
97     public function testInvalidBcryptSaltShort() {
98         password_hash('foo', PASSWORD_BCRYPT, array('salt' => 'abc'));
99     }