* @license BSD */ namespace Domframework\Tests; use Domframework\Password; /** * Test the Password class */ class PasswordTest extends \PHPUnit_Framework_TestCase { public function testCryptPasswd1() { $res = Password::cryptPasswd("AAA"); $this->assertSame($res[0] == "$" && strlen($res) > 8, true); } public function testCryptPasswd2() { // 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([$res1, $res2, $res3])), 3); // Three passwords : each must have a different result } public function testCryptPasswd3() { $this->expectException(); $res = Password::cryptPasswd(false); } public function testCryptPasswordMYSQL() { $password = new password(); $res = $password->cryptPassword("AAA", "MYSQL"); $this->assertSame($res, "*5AF9D0EA5F6406FB0EDD0507F81C1D5CEBE8AC9C"); } public function testCryptPasswordCRYPTSTDDES() { $password = new password(); $res = $password->cryptPassword("AAA", "CRYPT_STD_DES"); $this->assertSame(strlen($res), 13); } public function testCryptPasswordCRYPTEXTDES() { $password = new password(); $res = $password->cryptPassword("AAA", "CRYPT_EXT_DES"); $this->assertSame(strlen($res), 13); } public function testCryptPasswordCRYPTMD5() { $password = new password(); $res = $password->cryptPassword("AAA", "CRYPT_MD5"); $this->assertSame( substr($res, 0, 3) === "$1$" && strlen($res) === 34, true ); } public function testCryptPasswordCRYPTBLOWFISH() { $password = new password(); $res = $password->cryptPassword("AAA", "CRYPT_BLOWFISH"); $this->assertSame( substr($res, 0, 4) === "$2y$" && strlen($res) > 24, true ); } public function testCryptPasswordCRYPTSHA256() { $password = new password(); $res = $password->cryptPassword("AAA", "CRYPT_SHA256"); $this->assertSame( substr($res, 0, 10) === "$5\$rounds=" && strlen($res) === 75, true ); } public function testCryptPasswordCRYPTSHA512() { $password = new password(); $res = $password->cryptPassword("AAA", "CRYPT_SHA512"); $this->assertSame( substr($res, 0, 10) === "$6\$rounds=" && strlen($res) === 118, true ); } public function testCryptPasswordPASSWORDBCRYPT() { $password = new password(); $res = $password->cryptPassword("AAA", "PASSWORD_BCRYPT"); $this->assertSame( substr($res, 0, 7) === "$2y\$10\$" && strlen($res) === 60, true ); } public function testCryptPasswordPASSWORDARGON2I() { $password = new password(); $res = $password->cryptPassword("AAA", "PASSWORD_ARGON2I"); $this->assertSame( substr($res, 0, 11) === "\$argon2i\$v=" && strlen($res) === 96, true ); } public function testCryptPasswordPASSWORDARGON2ID() { $password = new password(); $res = $password->cryptPassword("AAA", "PASSWORD_ARGON2ID"); $this->assertSame( substr($res, 0, 12) === "\$argon2id\$v=" && strlen($res) === 97, true ); } public function testCryptPasswordUNKNOWN() { $this->expectException(); $password = new password(); $res = $password->cryptPassword("AAA", "UNKNOWN"); } public function testCheckPassword1() { $res = Password::checkPassword("AAA", "AAA"); $this->assertSame($res, false); } public function testCheckPassword2() { $crypt = Password::cryptPasswd("AAA"); $res = Password::checkPassword("AAA", $crypt); $this->assertSame($res, true); } public function testCheckPassword3() { $res = Password::checkPassword("AAA", Password::cryptPasswd("BBB")); $this->assertSame($res, false); } public function testCheckPassword4() { $res = Password::checkPassword( "AAA", '$2y$11$Y.E98jbjgDpV61eK..9MT.klzTeg7ulO4WH/B5yA8cAGMIh.zoNXq' ); $this->assertSame($res, true); } public function testCheckPasswordInvalid1() { $this->expectException(); $res = Password::checkPassword( false, '$2y$11$Y.E98jbjgDpV61eK..9MT.klzTeg7ulO4WH/B5yA8cAGMIh.zoNXq' ); } public function testCheckPasswordInvalid2() { $this->expectException(); $res = Password::checkPassword("AAA", false); } public function testGenerateASCIIDefaultsize() { $res = Password::generateASCII(); $this->assertSame(strlen($res), 12); } public function testGenerateASCIIToobig() { $this->expectException(); $res = Password::generateASCII(112); } public function testGenerateASCIIToosmall() { $this->expectException(); $res = Password::generateASCII(0); } public function testGenerateAlphanumDefaultsize() { $res = Password::generateAlphanum(); $this->assertSame(strlen($res), 12); } public function testGenerateAlphanumToobig() { $this->expectException(); $res = Password::generateAlphanum(112); } public function testGenerateAlphanumToosmall() { $this->expectException(); $res = Password::generateAlphanum(0); } public function testGenerateAlphabeticalDefaultsize() { $res = Password::generateAlphabetical(); $this->assertSame(strlen($res), 12); } public function testGenerateAlphabeticalToobig() { $this->expectException(); $res = Password::generateAlphabetical(112); } public function testGenerateAlphabeticalToosmall() { $this->expectException(); $res = Password::generateAlphabetical(0); } public function testListMethods1() { $password = new password(); $res = $password->listMethods(); $this->assertSame(count($res) > 7, true); } public function testSalt1() { $password = new password(); $res = $password->salt(); $this->assertSame(strlen($res) > 20, true); } }