MDL-49627 core: Update uses of generate_image_thumbnail
[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     /**
36      * @expectedException PHPUnit_Framework_Error
37      */
38     public function testInvalidAlgo() {
39         password_hash('foo', array());
40     }
42     /**
43      * @expectedException PHPUnit_Framework_Error
44      */
45     public function testInvalidAlgo2() {
46         password_hash('foo', 2);
47     }
49     /**
50      * @expectedException PHPUnit_Framework_Error
51      */
52     public function testInvalidPassword() {
53         password_hash(array(), 1);
54     }
56     /**
57      * @expectedException PHPUnit_Framework_Error
58      */
59     public function testInvalidSalt() {
60         password_hash('foo', PASSWORD_BCRYPT, array('salt' => array()));
61     }
63     /**
64      * @expectedException PHPUnit_Framework_Error
65      */
66     public function testInvalidBcryptCostLow() {
67         password_hash('foo', PASSWORD_BCRYPT, array('cost' => 3));
68     }
69         
70     /**
71      * @expectedException PHPUnit_Framework_Error
72      */
73     public function testInvalidBcryptCostHigh() {
74         password_hash('foo', PASSWORD_BCRYPT, array('cost' => 32));
75     }
77     /**
78      * @expectedException PHPUnit_Framework_Error
79      */
80     public function testInvalidBcryptCostInvalid() {
81         password_hash('foo', PASSWORD_BCRYPT, array('cost' => 'foo'));
82     }
84     /**
85      * @expectedException PHPUnit_Framework_Error
86      */
87     public function testInvalidBcryptSaltShort() {
88         password_hash('foo', PASSWORD_BCRYPT, array('salt' => 'abc'));
89     }
91 }