More complete test case

This commit is contained in:
Laurent Destailleur 2013-10-23 17:44:54 +02:00
parent a7ef6e5d70
commit 6e37e75755

View File

@ -26,7 +26,7 @@
global $conf,$user,$langs,$db; global $conf,$user,$langs,$db;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.php'; //require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1');
if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1');
@ -69,7 +69,7 @@ class CoreTest extends PHPUnit_Framework_TestCase
$this->savlangs=$langs; $this->savlangs=$langs;
$this->savdb=$db; $this->savdb=$db;
print __METHOD__." db->type=".$db->type." user->id=".$user->id; //print __METHOD__." db->type=".$db->type." user->id=".$user->id;
//print " - db ".$db->db; //print " - db ".$db->db;
print "\n"; print "\n";
} }
@ -135,78 +135,94 @@ class CoreTest extends PHPUnit_Framework_TestCase
global $dolibarr_main_db_type; global $dolibarr_main_db_type;
global $dolibarr_main_db_prefix; global $dolibarr_main_db_prefix;
$testtodo=3;
// Case 1: // Case 1:
// Test for subdir dolibarr (that point to htdocs) in root directory /var/www // Test for subdir dolibarrnew (that point to htdocs) in root directory /var/www
// URL: http://localhost/dolibarrnew/admin/system/phpinfo.php // URL: http://localhost/dolibarrnew/admin/system/phpinfo.php
$_SERVER["HTTPS"]=''; // To prepare this test:
$_SERVER["SERVER_NAME"]='localhost'; // - Create link from htdocs to /var/www/dolibarrnew
$_SERVER["SERVER_PORT"]='80'; // - Put into conf.php $dolibarr_main_document_root='/var/www/dolibarrnew';
$_SERVER["DOCUMENT_ROOT"]='/var/www'; if ($testtodo == 1)
$_SERVER["SCRIPT_NAME"]='/dolibarrnew/admin/system/phpinfo.php'; {
$expectedresult='/dolibarrnew'; $_SERVER["HTTPS"]='';
$_SERVER["SERVER_NAME"]='localhost';
$_SERVER["SERVER_PORT"]='80';
$_SERVER["DOCUMENT_ROOT"]='/var/www';
$_SERVER["SCRIPT_NAME"]='/dolibarrnew/admin/system/phpinfo.php';
$expectedresult='/dolibarrnew';
}
// Case 2: // Case 2:
// Test for subdir aaa (that point to dolibarr) in root directory /var/www // Test for subdir aaa (that point to dolibarr) in root directory /var/www
// URL: http://localhost/aaa/htdocs/admin/system/phpinfo.php // URL: http://localhost/aaa/htdocs/admin/system/phpinfo.php
$_SERVER["HTTPS"]=''; // To prepare this test:
$_SERVER["SERVER_NAME"]='localhost'; // - Create link from dolibarr to /var/www/aaa
$_SERVER["SERVER_PORT"]='80'; // - Put into conf.php $dolibarr_main_document_root='/var/www/aaa/htdocs';
$_SERVER["DOCUMENT_ROOT"]='/var/www'; if ($testtodo == 2)
$_SERVER["SCRIPT_NAME"]='/aaa/htdocs/admin/system/phpinfo.php'; {
$expectedresult='/aaa/htdocs'; $_SERVER["HTTPS"]='';
$_SERVER["SERVER_NAME"]='localhost';
$_SERVER["SERVER_PORT"]='80';
$_SERVER["DOCUMENT_ROOT"]='/var/www';
$_SERVER["SCRIPT_NAME"]='/aaa/htdocs/admin/system/phpinfo.php';
$expectedresult='/aaa/htdocs';
}
// Case 3: // Case 3:
// Test for virtual host localhostdolibarrnew that point to htdocs directory with // Test for virtual host localhostdolibarrnew that point to htdocs directory with
// a direct document root // a direct document root
// URL: http://localhostdolibarrnew/admin/system/phpinfo.php // URL: http://localhostdolibarrnew/admin/system/phpinfo.php
$_SERVER["HTTPS"]=''; // To prepare this test:
$_SERVER["SERVER_NAME"]='localhostdolibarrnew'; // - Create virtual host localhostdolibarrnew that point to /home/ldestailleur/git/dolibarr/htdocs
$_SERVER["SERVER_PORT"]='80'; // - Put into conf.php $dolibarr_main_document_root='/home/ldestailleur/git/dolibarr/htdocs';
$_SERVER["DOCUMENT_ROOT"]='/home/ldestail/workspace/dolibarr/htdocs'; if ($testtodo == 3)
$_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php'; {
$expectedresult=''; $_SERVER["HTTPS"]='';
$_SERVER["SERVER_NAME"]='localhostdolibarrnew';
$_SERVER["SERVER_PORT"]='80';
$_SERVER["DOCUMENT_ROOT"]='/home/ldestailleur/git/dolibarr/htdocs';
$_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php';
$expectedresult='';
}
// Case 4: // Case 4:
// Test for virtual host localhostdolibarrnew that point to htdocs directory with // Test for virtual host localhostdolibarrnew that point to htdocs directory with
// a symbolic link // a symbolic link
// URL: http://localhostdolibarrnew/admin/system/phpinfo.php // URL: http://localhostdolibarrnew/admin/system/phpinfo.php
$_SERVER["HTTPS"]=''; if ($testtodo == 4)
$_SERVER["SERVER_NAME"]='localhostdolibarrnew'; {
$_SERVER["SERVER_PORT"]='80'; $_SERVER["HTTPS"]='';
$_SERVER["DOCUMENT_ROOT"]='/var/www/dolibarr'; // This is a link that point to /home/ldestail/workspace/dolibarr/htdocs $_SERVER["SERVER_NAME"]='localhostdolibarrnew';
$_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php'; $_SERVER["SERVER_PORT"]='80';
$expectedresult=''; $_SERVER["DOCUMENT_ROOT"]='/var/www/dolibarr'; // This is a link that point to /home/ldestail/workspace/dolibarr/htdocs
$_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php';
$expectedresult='';
}
// Case 5: // Case 5:
// Test for alias /dolibarralias // Test for alias /dolibarralias, Test when using nginx, Test when using lighttpd
// URL: http://localhost/dolibarralias/admin/system/phpinfo.php // URL: http://localhost/dolibarralias/admin/system/phpinfo.php
$_SERVER["HTTPS"]=''; // To prepare this test:
$_SERVER["SERVER_NAME"]='localhost'; // - Copy content of dolibarr project into /var/www/dolibarr
$_SERVER["SERVER_PORT"]='80'; // - Put into conf.php $dolibarr_main_document_root='/var/www/dolibarr/htdocs';
$_SERVER["DOCUMENT_ROOT"]='/var/www'; // - Put into conf.php $dolibarr_main_url_root='http://localhost/dolibarralias'; (because autodetect will fails in this case)
$_SERVER["SCRIPT_NAME"]='/dolibarralias/admin/system/phpinfo.php'; if ($testtodo == 5)
$expectedresult='/dolibarralias'; {
// Put this into conf.php because autodetect will fails in this case $_SERVER["HTTPS"]='';
//$dolibarr_main_url_root='http://localhost/dolibarralias'; $_SERVER["SERVER_NAME"]='localhost';
$_SERVER["SERVER_PORT"]='80';
// Case 6: $_SERVER["DOCUMENT_ROOT"]='/var/www';
// Test when using nginx $_SERVER["SCRIPT_NAME"]='/dolibarralias/admin/system/phpinfo.php';
// URL: https://localhost/dolibarr/admin/system/phpinfo.php $expectedresult='/dolibarralias';
$_SERVER["HTTPS"]=''; }
$_SERVER["SERVER_NAME"]='localhost';
$_SERVER["SERVER_PORT"]='80';
$_SERVER["DOCUMENT_ROOT"]='/var/www/dolibarr/htdocs';
$_SERVER["SCRIPT_NAME"]='/dolibarr/admin/system/phpinfo.php';
$expectedresult='/dolibarr';
// Put this into conf.php because autodetect will fails in this case
//$dolibarr_main_url_root='http://localhost/dolibarr';
// Force to rerun filefunc.inc.php // Force to rerun filefunc.inc.php
include dirname(__FILE__).'/../../htdocs/filefunc.inc.php'; include dirname(__FILE__).'/../../htdocs/filefunc.inc.php';
print __METHOD__." DOL_MAIN_URL_ROOT=".DOL_MAIN_URL_ROOT."\n"; print __METHOD__." DOL_MAIN_URL_ROOT=".DOL_MAIN_URL_ROOT."\n";
print __METHOD__." DOL_URL_ROOT=".DOL_URL_ROOT."\n"; print __METHOD__." DOL_URL_ROOT=".DOL_URL_ROOT."\n";
// $this->assertEquals(DOL_URL_ROOT,$expectedresult); $this->assertEquals(DOL_URL_ROOT,$expectedresult);
return true; return true;
} }
@ -214,7 +230,7 @@ class CoreTest extends PHPUnit_Framework_TestCase
/** /**
* testSqlAndScriptInject * testSqlAndScriptInject
* *
* @return void * @return void
*/ */
public function testSqlAndScriptInject() public function testSqlAndScriptInject()
@ -230,10 +246,10 @@ class CoreTest extends PHPUnit_Framework_TestCase
global $dolibarr_main_db_port; global $dolibarr_main_db_port;
global $dolibarr_main_db_type; global $dolibarr_main_db_type;
global $dolibarr_main_db_prefix; global $dolibarr_main_db_prefix;
// This is code copied from main.inc.php // This is code copied from main.inc.php
/** /**
* Security: SQL Injection and XSS Injection (scripts) protection (Filters on GET, POST, PHP_SELF). * Security: SQL Injection and XSS Injection (scripts) protection (Filters on GET, POST, PHP_SELF).
* *
@ -272,12 +288,12 @@ class CoreTest extends PHPUnit_Framework_TestCase
if ($type == 2) $sql_inj += preg_match('/[\s;"]/', $val); // PHP_SELF is an url and must match url syntax if ($type == 2) $sql_inj += preg_match('/[\s;"]/', $val); // PHP_SELF is an url and must match url syntax
return $sql_inj; return $sql_inj;
} }
//type=2 key=0 value=/DIR WITH SPACE/htdocs/admin/index.php?mainmenu=home&leftmenu=setup&username=weservices //type=2 key=0 value=/DIR WITH SPACE/htdocs/admin/index.php?mainmenu=home&leftmenu=setup&username=weservices
$_SERVER["PHP_SELF"]='/DIR WITH SPACE/htdocs/admin/index.php?mainmenu=home&leftmenu=setup&username=weservices'; $_SERVER["PHP_SELF"]='/DIR WITH SPACE/htdocs/admin/index.php?mainmenu=home&leftmenu=setup&username=weservices';
$result=test_sql_and_script_inject($_SERVER["PHP_SELF"],2); $result=test_sql_and_script_inject($_SERVER["PHP_SELF"],2);
$expectedresult=1; $expectedresult=1;
$this->assertEquals($result,$expectedresult); $this->assertEquals($result,$expectedresult);
} }
} }