From 590c80f0bca67b53dba8ae87e3c44010c983ae8d Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Sat, 29 Dec 2018 20:54:25 +0000 Subject: [PATCH] add more unit tests; re-enable random_int warning in functions.inc.php for old php versions etc etc --- composer.json | 2 +- functions.inc.php | 11 +++--- lib/block_random_int.php | 14 +++++++ tests/CheckDomainTest.php | 16 ++++++++ tests/CheckEmailTest.php | 15 ++++++++ tests/CheckLanguageTest.php | 63 +++++++++++++++++++++++++++++++ tests/CheckOwnerTest.php | 13 +++++++ tests/DbBasicTest.php | 48 +++++++++++++++++++++++ tests/ListAdminsTest.php | 15 ++++++++ tests/ListDomainsForAdminTest.php | 11 ++++++ tests/ListDomainsTest.php | 13 +++++++ tests/RemoveFromArrayTest.php | 23 +++++++++++ 12 files changed, 237 insertions(+), 7 deletions(-) create mode 100644 lib/block_random_int.php create mode 100644 tests/CheckDomainTest.php create mode 100644 tests/CheckEmailTest.php create mode 100644 tests/CheckLanguageTest.php create mode 100644 tests/CheckOwnerTest.php create mode 100644 tests/DbBasicTest.php create mode 100644 tests/ListAdminsTest.php create mode 100644 tests/ListDomainsForAdminTest.php create mode 100644 tests/ListDomainsTest.php create mode 100644 tests/RemoveFromArrayTest.php diff --git a/composer.json b/composer.json index 94deef92..19b5b53f 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "check-format": "php-cs-fixer fix --ansi --dry-run --diff", "format": "php-cs-fixer fix --ansi", - "lint": "@php ./vendor/bin/parallel-lint --exclude vendor/ .", + "lint": "@php ./vendor/bin/parallel-lint --exclude vendor/ --exclude lib/block_random_int.php .", "test": "@php ./vendor/bin/phpunit tests/", "psalm": "@php ./vendor/bin/psalm --show-info=false " }, diff --git a/functions.inc.php b/functions.inc.php index 1d090bab..d2daa8ce 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -845,13 +845,11 @@ function encode_header($string, $default_charset = "utf-8") { return $string; } -/* + if (!function_exists('random_int')) { // PHP version < 7.0 - function random_int() { // someone might not be using php_crypt or ask for password generation, in which case random_int() won't be called - die(__FILE__ . " Postfixadmin security: Please install https://github.com/paragonie/random_compat OR enable the 'Phar' extension."); - } + require_once(dirname(__FILE__) . '/lib/block_random_int.php'); } - */ + /** * Generate a random password of $length characters. @@ -1946,8 +1944,9 @@ function db_update_q($table, $where, $values, $timestamp = array('modified')) { } } + /* @todo this needs refactoring/moving out from here */ if (Config::bool('password_expiration')) { - if ($table == 'mailbox') { + if ($table == 'mailbox' && preg_match('/@/', $where)) { $where_type = explode('=', $where); $email = ($where_type[1]); $domain_dirty = explode('@',$email)[1]; diff --git a/lib/block_random_int.php b/lib/block_random_int.php new file mode 100644 index 00000000..cf0f3b3b --- /dev/null +++ b/lib/block_random_int.php @@ -0,0 +1,14 @@ +assertEquals('', check_domain('example.com')); + $this->assertEquals('', check_domain('google.com')); + $this->assertRegExp('/ not discoverable in DNS/', check_domain('fishbeansblahblahblah' . uniqid() . '.com')); + } +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/tests/CheckEmailTest.php b/tests/CheckEmailTest.php new file mode 100644 index 00000000..093b9328 --- /dev/null +++ b/tests/CheckEmailTest.php @@ -0,0 +1,15 @@ +assertEquals('', check_email('test@example.com')); + $this->assertRegExp('/ not discoverable in DNS/', check_email('test@fishbeansblahblahblah' . uniqid() . '.com')); + } +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/tests/CheckLanguageTest.php b/tests/CheckLanguageTest.php new file mode 100644 index 00000000..5453ac00 --- /dev/null +++ b/tests/CheckLanguageTest.php @@ -0,0 +1,63 @@ +assertNotEmpty($supported_languages); + + $config = Config::getInstance(); + Config::write('default_language', 'test'); + + unset($_SERVER['HTTP_ACCEPT_LANGUAGE']); + + $lang = check_language(false); + + $this->assertEquals('test', $lang); + + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en'; + + $lang = check_language(false); + $this->assertEquals('en', $lang); + } + + public function testCookie() { + global $supported_languages; + + $this->assertNotEmpty($supported_languages); + + + $config = Config::getInstance(); + Config::write('default_language', 'test'); + + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foo'; + + $_COOKIE['lang'] = 'en'; + + $lang = check_language(false); + + $this->assertEquals('en', $lang); + } + + public function testPost() { + global $supported_languages; + + $this->assertNotEmpty($supported_languages); + + + $config = Config::getInstance(); + Config::write('default_language', 'test'); + + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foo'; + + $_POST['lang'] = 'en'; + + $lang = check_language(true); + + $this->assertEquals('en', $lang); + } +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/tests/CheckOwnerTest.php b/tests/CheckOwnerTest.php new file mode 100644 index 00000000..bb4108ec --- /dev/null +++ b/tests/CheckOwnerTest.php @@ -0,0 +1,13 @@ +assertFalse($check); + } +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/tests/DbBasicTest.php b/tests/DbBasicTest.php new file mode 100644 index 00000000..96c8cfa2 --- /dev/null +++ b/tests/DbBasicTest.php @@ -0,0 +1,48 @@ +assertEquals( + 1, + db_insert( + 'domain', + array('domain' => $domain, 'description' => '', 'transport' => '', 'password_expiry' => 99) + ) + ); + + + $this->assertEquals(1, + db_insert( + 'mailbox', + array('username' => $username, 'password' => 'blah', 'name' => 'blah', 'maildir' => 'blah', 'local_part' => 'blah', 'domain' => $domain,) + ) + ); + + $this->assertEquals(1, + db_update( + 'mailbox', + 'username', + $username, + array('name' => 'blah updated') + ) + ); + + $ret = db_query("SELECT * FROM mailbox WHERE username = '$username'"); + + $this->assertEquals(1, $ret['rows']); + $data = db_assoc($ret['result']); + + $this->assertEquals($data['name'], 'blah updated'); + + $this->assertEquals(1, db_delete('mailbox', 'username', $username)); + $this->assertEquals(1, db_delete('domain', 'domain', $domain)); + } +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/tests/ListAdminsTest.php b/tests/ListAdminsTest.php new file mode 100644 index 00000000..cf7b2f6b --- /dev/null +++ b/tests/ListAdminsTest.php @@ -0,0 +1,15 @@ +assertTrue(is_array($list)); + } +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/tests/ListDomainsForAdminTest.php b/tests/ListDomainsForAdminTest.php new file mode 100644 index 00000000..ad3e7ba1 --- /dev/null +++ b/tests/ListDomainsForAdminTest.php @@ -0,0 +1,11 @@ +assertEquals([], list_domains_for_admin('test@test.com')); + } +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/tests/ListDomainsTest.php b/tests/ListDomainsTest.php new file mode 100644 index 00000000..2239a72a --- /dev/null +++ b/tests/ListDomainsTest.php @@ -0,0 +1,13 @@ +assertTrue(is_array($domains)); + } +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/tests/RemoveFromArrayTest.php b/tests/RemoveFromArrayTest.php new file mode 100644 index 00000000..97877a77 --- /dev/null +++ b/tests/RemoveFromArrayTest.php @@ -0,0 +1,23 @@ +assertEquals(1, $found); + $this->assertEquals(array('a','b','c'), $new); + + list($found, $new) = remove_from_array($list, 'a'); + $this->assertEquals(1, $found); + $this->assertEquals(array(1 => 'b',2 => 'c',3=>'d'), $new); + + list($found, $new) = remove_from_array($list, 'x'); + $this->assertEquals(0, $found); + $this->assertEquals(array('a','b','c','d'), $new); + } +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */