You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
postfixadmin/tests/PacryptTest.php

120 lines
3.3 KiB
PHTML

<?php
class PaCryptTest extends \PHPUnit\Framework\TestCase {
7 years ago
public function testMd5Crypt() {
$hash = _pacrypt_md5crypt('test', '');
$this->assertNotEmpty($hash);
$this->assertNotEquals('test', $hash);
$this->assertEquals($hash, _pacrypt_md5crypt('test', $hash));
}
7 years ago
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));
}
7 years ago
public function testMySQLEncrypt() {
if (!db_mysql()) {
$this->markTestSkipped('Not using MySQL');
}
6 years ago
$hash = _pacrypt_mysql_encrypt('test');
6 years ago
sleep(1);
6 years ago
$hash2 = _pacrypt_mysql_encrypt('test');
7 years ago
$this->assertNotEquals($hash, $hash2);
6 years ago
$this->assertNotEmpty($hash);
$this->assertNotEquals('test', $hash);
$this->assertNotEquals('test', $hash2);
$this->assertEquals(
$hash,
_pacrypt_mysql_encrypt('test', $hash),
"test should encrypt to : $hash ..."
);
}
7 years ago
public function testAuthlib() {
global $CONF;
7 years ago
// too many options!
foreach (
6 years ago
[
'md5raw' => '098f6bcd4621d373cade4e832627b4f6',
7 years ago
'md5' => 'CY9rzUYh03PK3k6DJie09g==',
// crypt requires salt ...
6 years ago
'SHA' => 'qUqP5cyxm6YcTAhz05Hph5gvu9M='
] as $flavour => $hash) {
$CONF['authlib_default_flavour'] = $flavour;
$stored = "{" . $flavour . "}$hash";
$hash = _pacrypt_authlib('test', $stored);
$this->assertEquals($hash, $stored, "Hash: $hash vs Stored: $stored");
//var_dump("Hash: $hash from $flavour");
}
}
7 years ago
public function testPacryptDovecot() {
global $CONF;
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));
}
7 years ago
7 years ago
public function testPhpCrypt() {
7 years ago
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);
}
7 years ago
public function testPhpCryptRandomString() {
7 years ago
$str1 = _php_crypt_random_string('abcdefg123456789', 2);
$str2 = _php_crypt_random_string('abcdefg123456789', 2);
6 years ago
$str3 = _php_crypt_random_string('abcdefg123456789', 2);
7 years ago
$this->assertNotEmpty($str1);
$this->assertNotEmpty($str2);
6 years ago
$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 );
7 years ago
}
}