* @license BSD */ namespace Domframework\Tests; use Domframework\Password; /** 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); } }