* @license BSD */ namespace Domframework\Tests; /** Test the password class */ class passwordTest extends PHPUnit_Framework_TestCase { public function test_cryptPasswd_1 () { $res = \password::cryptPasswd ("AAA"); $this->assertSame ($res[0] == "$" && strlen ($res) > 8, true); } public function test_cryptPasswd_2 () { // Test the randomization of the salt : must be different each time $res1 = \password::cryptPasswd ("AAA"); echo "RES1=$res1\n"; $res2 = \password::cryptPasswd ("AAA"); echo "RES2=$res2\n"; $res3 = \password::cryptPasswd ("AAA"); echo "RES3=$res3\n"; $this->assertSame (count (array_unique (array ($res1, $res2, $res3))), 3); // Three passwords : each must have a different result } public function test_cryptPasswd_3 () { $this->expectException (); $res = \password::cryptPasswd (false); } public function test_cryptPassword_MYSQL () { $password = new password (); $res = $password->cryptPassword ("AAA", "MYSQL"); $this->assertSame ($res, "*5AF9D0EA5F6406FB0EDD0507F81C1D5CEBE8AC9C"); } public function test_cryptPassword_CRYPT_STD_DES () { $password = new password (); $res = $password->cryptPassword ("AAA", "CRYPT_STD_DES"); $this->assertSame (strlen ($res), 13); } public function test_cryptPassword_CRYPT_EXT_DES () { $password = new password (); $res = $password->cryptPassword ("AAA", "CRYPT_EXT_DES"); $this->assertSame (strlen ($res), 13); } public function test_cryptPassword_CRYPT_MD5 () { $password = new password (); $res = $password->cryptPassword ("AAA", "CRYPT_MD5"); $this->assertSame (substr ($res, 0, 3) === "$1$" && strlen ($res) === 34, true); } public function test_cryptPassword_CRYPT_BLOWFISH () { $password = new password (); $res = $password->cryptPassword ("AAA", "CRYPT_BLOWFISH"); $this->assertSame (substr ($res, 0, 4) === "$2y$" && strlen ($res) === 24, true); } public function test_cryptPassword_CRYPT_SHA256 () { $password = new password (); $res = $password->cryptPassword ("AAA", "CRYPT_SHA256"); $this->assertSame ( substr ($res, 0, 10) === "$5\$rounds=" && strlen ($res) === 75, true); } public function test_cryptPassword_CRYPT_SHA512 () { $password = new password (); $res = $password->cryptPassword ("AAA", "CRYPT_SHA512"); $this->assertSame ( substr ($res, 0, 10) === "$6\$rounds=" && strlen ($res) === 118, true); } public function test_cryptPassword_PASSWORD_BCRYPT () { $password = new password (); $res = $password->cryptPassword ("AAA", "PASSWORD_BCRYPT"); $this->assertSame ( substr ($res, 0, 7) === "$2y\$10\$" && strlen ($res) === 60, true); } public function test_cryptPassword_PASSWORD_ARGON2I () { $password = new password (); $res = $password->cryptPassword ("AAA", "PASSWORD_ARGON2I"); $this->assertSame ( substr ($res, 0, 11) === "\$argon2i\$v=" && strlen ($res) === 96, true); } public function test_cryptPassword_PASSWORD_ARGON2ID () { $password = new password (); $res = $password->cryptPassword ("AAA", "PASSWORD_ARGON2ID"); $this->assertSame ( substr ($res, 0, 12) === "\$argon2id\$v=" && strlen ($res) === 97, true); } public function test_cryptPassword_UNKNOWN () { $this->expectException (); $password = new password (); $res = $password->cryptPassword ("AAA", "UNKNOWN"); } public function test_checkPassword_1 () { $res = \password::checkPassword ("AAA", "AAA"); $this->assertSame ($res, false); } public function test_checkPassword_2 () { $crypt = \password::cryptPasswd ("AAA"); $res = \password::checkPassword ("AAA", $crypt); $this->assertSame ($res, true); } public function test_checkPassword_3 () { $res = \password::checkPassword ("AAA", \password::cryptPasswd ("BBB")); $this->assertSame ($res, false); } public function test_checkPassword_4 () { $res = \password::checkPassword ("AAA", '$2y$11$Y.E98jbjgDpV61eK..9MT.klzTeg7ulO4WH/B5yA8cAGMIh.zoNXq'); $this->assertSame ($res, true); } public function test_checkPassword_invalid1 () { $this->expectException (); $res = \password::checkPassword (false, '$2y$11$Y.E98jbjgDpV61eK..9MT.klzTeg7ulO4WH/B5yA8cAGMIh.zoNXq'); } public function test_checkPassword_invalid2 () { $this->expectException (); $res = \password::checkPassword ("AAA", false); } public function test_generateASCII_defaultsize () { $res = \password::generateASCII (); $this->assertSame (strlen ($res), 12); } public function test_generateASCII_toobig () { $this->expectException (); $res = \password::generateASCII (112); } public function test_generateASCII_toosmall () { $this->expectException (); $res = \password::generateASCII (0); } public function test_generateAlphanum_defaultsize () { $res = \password::generateAlphanum (); $this->assertSame (strlen ($res), 12); } public function test_generateAlphanum_toobig () { $this->expectException (); $res = \password::generateAlphanum (112); } public function test_generateAlphanum_toosmall () { $this->expectException (); $res = \password::generateAlphanum (0); } public function test_generateAlphabetical_defaultsize () { $res = \password::generateAlphabetical (); $this->assertSame (strlen ($res), 12); } public function test_generateAlphabetical_toobig () { $this->expectException (); $res = \password::generateAlphabetical (112); } public function test_generateAlphabetical_toosmall () { $this->expectException (); $res = \password::generateAlphabetical (0); } public function test_listMethods_1 () { $password = new password (); $res = $password->listMethods(); $this->assertSame (count ($res) > 7, true); } public function test_salt_1 () { $password = new password (); $res = $password->salt(); $this->assertSame (strlen ($res) > 20, true); } }