Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PacryptTest.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. require_once('common.php');
  3. class PaCryptTest extends PHPUnit_Framework_TestCase {
  4. public function testMd5Crypt() {
  5. $hash = _pacrypt_md5crypt('test', '');
  6. $this->assertNotEmpty($hash);
  7. $this->assertNotEquals('test', $hash);
  8. $this->assertEquals($hash, _pacrypt_md5crypt('test', $hash));
  9. }
  10. public function testCrypt() {
  11. // E_NOTICE if we pass in '' for the salt
  12. $hash = _pacrypt_crypt('test', 'sa');
  13. $this->assertNotEmpty($hash);
  14. $this->assertNotEquals('test', $hash);
  15. $this->assertEquals($hash, _pacrypt_crypt('test', $hash));
  16. }
  17. public function testMySQLEncrypt() {
  18. if (!db_mysql()) {
  19. $this->markTestSkipped('Not using MySQL');
  20. }
  21. $hash = _pacrypt_mysql_encrypt('test', '');
  22. $this->assertNotEmpty($hash);
  23. $this->assertNotEquals('test', $hash);
  24. $this->assertEquals($hash, _pacrypt_mysql_encrypt('test', $hash));
  25. $hash2 = _pacrypt_mysql_encrypt('test', 'salt');
  26. $this->assertNotEmpty($hash2);
  27. $this->assertNotEquals($hash, $hash2);
  28. $this->assertEquals($hash2, _pacrypt_mysql_encrypt('test', 'salt'));
  29. }
  30. public function testAuthlib() {
  31. // too many options!
  32. foreach (
  33. ['md5raw' => '098f6bcd4621d373cade4e832627b4f6',
  34. 'md5' => 'CY9rzUYh03PK3k6DJie09g==',
  35. // crypt requires salt ...
  36. 'SHA' => 'qUqP5cyxm6YcTAhz05Hph5gvu9M='] as $flavour => $hash) {
  37. $CONF['authlib_default_flavour'] = $flavour;
  38. $stored = "{" . $flavour . "}$hash";
  39. $hash = _pacrypt_authlib('test', $stored);
  40. $this->assertEquals($hash, $stored, "Hash: $hash vs Stored: $stored");
  41. //var_dump("Hash: $hash from $flavour");
  42. }
  43. }
  44. public function testPacryptDovecot() {
  45. global $CONF;
  46. if (!file_exists('/usr/bin/doveadm')) {
  47. $this->markTestSkipped("No /usr/bin/doveadm");
  48. }
  49. $CONF['encrypt'] = 'dovecot:SHA1';
  50. $expected_hash = '{SHA1}qUqP5cyxm6YcTAhz05Hph5gvu9M=';
  51. $this->assertEquals($expected_hash, _pacrypt_dovecot('test', ''));
  52. $this->assertEquals($expected_hash, _pacrypt_dovecot('test', $expected_hash));
  53. }
  54. public function testPhpCrypt() {
  55. global $CONF;
  56. $config = Config::getInstance();
  57. Config::write('encrypt', 'php_crypt:MD5');
  58. $CONF = Config::getInstance()->getAll();
  59. $expected = '$1$z2DG4z9d$jBu3Cl3BPQZrkNqnflnSO.';
  60. $enc = _pacrypt_php_crypt('foo', $expected);
  61. $this->assertEquals($enc, $expected);
  62. $fail = _pacrypt_php_crypt('bar', $expected);
  63. $this->assertNotEquals($fail, $expected);
  64. }
  65. public function testPhpCryptRandomString() {
  66. $str1 = _php_crypt_random_string('abcdefg123456789', 2);
  67. $str2 = _php_crypt_random_string('abcdefg123456789', 2);
  68. $this->assertNotEmpty($str1);
  69. $this->assertNotEmpty($str2);
  70. $this->assertNotEquals($str1, $str2);
  71. }
  72. }
  73. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */