0
0
mirror of https://github.com/postfixadmin/postfixadmin.git synced 2024-09-20 03:36:20 +02:00
postfixadmin/tests/PacryptTest.php

144 lines
4.1 KiB
PHP
Raw Normal View History

<?php
class PaCryptTest extends \PHPUnit\Framework\TestCase {
2018-05-02 13:54:17 +02:00
public function testMd5Crypt() {
$hash = _pacrypt_md5crypt('test', '');
$this->assertNotEmpty($hash);
$this->assertNotEquals('test', $hash);
$this->assertEquals($hash, _pacrypt_md5crypt('test', $hash));
}
2018-05-02 13:54:17 +02:00
public function testCrypt() {
// E_NOTICE if we pass in '' for the salt
$hash = _pacrypt_crypt('test', 'sa');
$this->assertNotEmpty($hash);
$this->assertNotEquals('test', $hash);
$this->assertEquals($hash, _pacrypt_crypt('test', $hash));
}
2018-05-02 13:54:17 +02:00
public function testMySQLEncrypt() {
2018-02-19 22:01:23 +01:00
if (!db_mysql()) {
$this->markTestSkipped('Not using MySQL');
}
2019-02-15 22:31:21 +01:00
$hash = _pacrypt_mysql_encrypt('test');
2019-02-15 22:31:21 +01:00
sleep(1);
2019-02-15 22:31:21 +01:00
$hash2 = _pacrypt_mysql_encrypt('test');
2018-04-29 22:53:46 +02:00
$this->assertNotEquals($hash, $hash2);
2019-02-15 22:31:21 +01:00
$this->assertNotEmpty($hash);
$this->assertNotEquals('test', $hash);
$this->assertNotEquals('test', $hash2);
$this->assertEquals(
$hash,
_pacrypt_mysql_encrypt('test', $hash),
"test should encrypt to : $hash ..."
);
}
2018-05-02 13:54:17 +02:00
public function testAuthlib() {
global $CONF;
2018-04-29 22:53:46 +02:00
2018-02-19 22:01:23 +01:00
// too many options!
foreach (
2019-02-15 22:31:21 +01:00
[
'md5raw' => '098f6bcd4621d373cade4e832627b4f6',
2018-04-29 22:53:46 +02:00
'md5' => 'CY9rzUYh03PK3k6DJie09g==',
// crypt requires salt ...
2019-02-15 22:31:21 +01:00
'SHA' => 'qUqP5cyxm6YcTAhz05Hph5gvu9M='
] as $flavour => $hash) {
$CONF['authlib_default_flavour'] = $flavour;
$stored = "{" . $flavour . "}$hash";
$hash = _pacrypt_authlib('test', $stored);
2018-02-19 22:01:23 +01:00
$this->assertEquals($hash, $stored, "Hash: $hash vs Stored: $stored");
//var_dump("Hash: $hash from $flavour");
}
}
2018-05-02 13:54:17 +02:00
public function testPacryptDovecot() {
global $CONF;
2018-02-19 22:01:23 +01:00
if (!file_exists('/usr/bin/doveadm')) {
$this->markTestSkipped("No /usr/bin/doveadm");
}
$CONF['encrypt'] = 'dovecot:SHA1';
$expected_hash = '{SHA1}qUqP5cyxm6YcTAhz05Hph5gvu9M=';
$this->assertEquals($expected_hash, _pacrypt_dovecot('test', ''));
$this->assertEquals($expected_hash, _pacrypt_dovecot('test', $expected_hash));
}
2018-04-29 22:53:46 +02:00
2018-05-02 13:54:17 +02:00
public function testPhpCrypt() {
2018-04-29 22:53:46 +02:00
global $CONF;
$config = Config::getInstance();
Config::write('encrypt', 'php_crypt:MD5');
$CONF = Config::getInstance()->getAll();
$expected = '$1$z2DG4z9d$jBu3Cl3BPQZrkNqnflnSO.';
$enc = _pacrypt_php_crypt('foo', $expected);
$this->assertEquals($enc, $expected);
$fail = _pacrypt_php_crypt('bar', $expected);
$this->assertNotEquals($fail, $expected);
}
2018-05-02 13:54:17 +02:00
public function testPhpCryptRandomString() {
2018-04-29 22:53:46 +02:00
$str1 = _php_crypt_random_string('abcdefg123456789', 2);
$str2 = _php_crypt_random_string('abcdefg123456789', 2);
2019-02-15 22:31:21 +01:00
$str3 = _php_crypt_random_string('abcdefg123456789', 2);
2018-04-29 22:53:46 +02:00
$this->assertNotEmpty($str1);
$this->assertNotEmpty($str2);
2019-02-15 22:31:21 +01:00
$this->assertNotEmpty($str3);
// it should be difficult for us to get three salts of the same value back...
// not impossible though.
$this->assertFalse( strcmp($str1, $str2) == 0 && strcmp($str1, $str3) == 0 );
2018-04-29 22:53:46 +02:00
}
public function testSha512B64() {
$str1 = _pacrypt_sha512_b64('test', '');
$str2 = _pacrypt_sha512_b64('test', '');
$this->assertNotEmpty($str1);
$this->assertNotEmpty($str2);
$this->assertNotEquals($str1, $str2); // should have different salts
$actualHash = '{SHA512-CRYPT.B64}JDYkM2NWcFM1WFNlUHl5MzdwSiRZWW80d0FmeWg5MXpxcS4uY3dtYUR1Y1RodTJGTDY1NHpXNUNvRU0wT3hXVFFzZkxIZ1JJSTZmT281OVpDUWJOTTF2L0JXajloME0vVjJNbENNMUdwLg==';
$check = _pacrypt_sha512_b64('test', $actualHash);
$this->assertTrue(hash_equals($check, $actualHash));
$str3 = _pacrypt_sha512_b64('foo', '');
$this->assertNotEmpty($str3);
$this->assertFalse(hash_equals('test', $str3));
$this->assertTrue(hash_equals(_pacrypt_sha512_b64('foo',$str3), $str3));
}
}