CoreTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /* Copyright (C) 2010-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/CoreTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. //require_once 'PHPUnit/Autoload.php';
  27. //require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1');
  29. if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1');
  30. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  31. if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  32. if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
  33. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1');
  34. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no menu to show
  35. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php
  36. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  37. if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session)
  38. /**
  39. * Class to test core functions
  40. *
  41. * @backupGlobals disabled
  42. * @backupStaticAttributes enabled
  43. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  44. */
  45. class CoreTest extends PHPUnit_Framework_TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. /**
  52. * Constructor
  53. * We save global variables into local variables
  54. *
  55. * @return CoreTest
  56. */
  57. function __construct()
  58. {
  59. parent::__construct();
  60. //$this->sharedFixture
  61. global $conf,$user,$langs,$db;
  62. $this->savconf=$conf;
  63. $this->savuser=$user;
  64. $this->savlangs=$langs;
  65. $this->savdb=$db;
  66. //print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  67. //print " - db ".$db->db;
  68. print "\n";
  69. }
  70. // Static methods
  71. public static function setUpBeforeClass()
  72. {
  73. global $conf,$user,$langs,$db;
  74. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  75. print __METHOD__."\n";
  76. }
  77. // tear down after class
  78. public static function tearDownAfterClass()
  79. {
  80. global $conf,$user,$langs,$db;
  81. //$db->rollback();
  82. print __METHOD__."\n";
  83. }
  84. /**
  85. * Init phpunit tests
  86. *
  87. * @return void
  88. */
  89. protected function setUp()
  90. {
  91. global $conf,$user,$langs,$db;
  92. $conf=$this->savconf;
  93. $user=$this->savuser;
  94. $langs=$this->savlangs;
  95. $db=$this->savdb;
  96. print __METHOD__."\n";
  97. }
  98. /**
  99. * End phpunit tests
  100. *
  101. * @return void
  102. */
  103. protected function tearDown()
  104. {
  105. print __METHOD__."\n";
  106. }
  107. /**
  108. * testDetectURLROOT
  109. *
  110. * @return void
  111. */
  112. public function testDetectURLROOT()
  113. {
  114. global $dolibarr_main_prod;
  115. global $dolibarr_main_url_root;
  116. global $dolibarr_main_data_root;
  117. global $dolibarr_main_document_root;
  118. global $dolibarr_main_data_root_alt;
  119. global $dolibarr_main_document_root_alt;
  120. global $dolibarr_main_db_host;
  121. global $dolibarr_main_db_port;
  122. global $dolibarr_main_db_type;
  123. global $dolibarr_main_db_prefix;
  124. $testtodo=3;
  125. // Case 1:
  126. // Test for subdir dolibarrnew (that point to htdocs) in root directory /var/www
  127. // URL: http://localhost/dolibarrnew/admin/system/phpinfo.php
  128. // To prepare this test:
  129. // - Create link from htdocs to /var/www/dolibarrnew
  130. // - Put into conf.php $dolibarr_main_document_root='/var/www/dolibarrnew';
  131. if ($testtodo == 1) {
  132. $_SERVER["HTTPS"]='';
  133. $_SERVER["SERVER_NAME"]='localhost';
  134. $_SERVER["SERVER_PORT"]='80';
  135. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  136. $_SERVER["SCRIPT_NAME"]='/dolibarrnew/admin/system/phpinfo.php';
  137. $expectedresult='/dolibarrnew';
  138. }
  139. // Case 2:
  140. // Test for subdir aaa (that point to dolibarr) in root directory /var/www
  141. // URL: http://localhost/aaa/htdocs/admin/system/phpinfo.php
  142. // To prepare this test:
  143. // - Create link from dolibarr to /var/www/aaa
  144. // - Put into conf.php $dolibarr_main_document_root='/var/www/aaa/htdocs';
  145. if ($testtodo == 2) {
  146. $_SERVER["HTTPS"]='';
  147. $_SERVER["SERVER_NAME"]='localhost';
  148. $_SERVER["SERVER_PORT"]='80';
  149. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  150. $_SERVER["SCRIPT_NAME"]='/aaa/htdocs/admin/system/phpinfo.php';
  151. $expectedresult='/aaa/htdocs';
  152. }
  153. // Case 3:
  154. // Test for virtual host localhostdolibarrnew that point to htdocs directory with
  155. // a direct document root
  156. // URL: http://localhostdolibarrnew/admin/system/phpinfo.php
  157. // To prepare this test:
  158. // - Create virtual host localhostdolibarrnew that point to /home/ldestailleur/git/dolibarr/htdocs
  159. // - Put into conf.php $dolibarr_main_document_root='/home/ldestailleur/git/dolibarr/htdocs';
  160. if ($testtodo == 3) {
  161. $_SERVER["HTTPS"]='';
  162. $_SERVER["SERVER_NAME"]='localhostdolibarrnew';
  163. $_SERVER["SERVER_PORT"]='80';
  164. $_SERVER["DOCUMENT_ROOT"]='/home/ldestailleur/git/dolibarr/htdocs';
  165. $_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php';
  166. $expectedresult='';
  167. }
  168. // Case 4:
  169. // Test for virtual host localhostdolibarrnew that point to htdocs directory with
  170. // a symbolic link
  171. // URL: http://localhostdolibarrnew/admin/system/phpinfo.php
  172. if ($testtodo == 4) {
  173. $_SERVER["HTTPS"]='';
  174. $_SERVER["SERVER_NAME"]='localhostdolibarrnew';
  175. $_SERVER["SERVER_PORT"]='80';
  176. $_SERVER["DOCUMENT_ROOT"]='/var/www/dolibarr'; // This is a link that point to /home/ldestail/workspace/dolibarr/htdocs
  177. $_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php';
  178. $expectedresult='';
  179. }
  180. // Case 5:
  181. // Test for alias /dolibarralias, Test when using nginx, Test when using lighttpd
  182. // URL: http://localhost/dolibarralias/admin/system/phpinfo.php
  183. // To prepare this test:
  184. // - Copy content of dolibarr project into /var/www/dolibarr
  185. // - Put into conf.php $dolibarr_main_document_root='/var/www/dolibarr/htdocs';
  186. // - Put into conf.php $dolibarr_main_url_root='http://localhost/dolibarralias'; (because autodetect will fails in this case)
  187. if ($testtodo == 5) {
  188. $_SERVER["HTTPS"]='';
  189. $_SERVER["SERVER_NAME"]='localhost';
  190. $_SERVER["SERVER_PORT"]='80';
  191. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  192. $_SERVER["SCRIPT_NAME"]='/dolibarralias/admin/system/phpinfo.php';
  193. $expectedresult='/dolibarralias';
  194. }
  195. // Force to rerun filefunc.inc.php
  196. include dirname(__FILE__).'/../../htdocs/filefunc.inc.php';
  197. print __METHOD__." DOL_MAIN_URL_ROOT=".DOL_MAIN_URL_ROOT."\n";
  198. print __METHOD__." DOL_URL_ROOT=".DOL_URL_ROOT."\n";
  199. $this->assertEquals($expectedresult, DOL_URL_ROOT);
  200. return true;
  201. }
  202. /**
  203. * testSqlAndScriptInject
  204. *
  205. * @return void
  206. */
  207. public function testSqlAndScriptInject()
  208. {
  209. global $dolibarr_main_prod;
  210. global $dolibarr_main_url_root;
  211. global $dolibarr_main_data_root;
  212. global $dolibarr_main_document_root;
  213. global $dolibarr_main_data_root_alt;
  214. global $dolibarr_main_document_root_alt;
  215. global $dolibarr_main_db_host;
  216. global $dolibarr_main_db_port;
  217. global $dolibarr_main_db_type;
  218. global $dolibarr_main_db_prefix;
  219. // This is code copied from main.inc.php !!!!!!!!!!!!!!!
  220. /**
  221. * Security: SQL Injection and XSS Injection (scripts) protection (Filters on GET, POST, PHP_SELF).
  222. *
  223. * @param string $val Value
  224. * @param string $type 1=GET, 0=POST, 2=PHP_SELF
  225. * @return int >0 if there is an injection
  226. */
  227. function test_sql_and_script_inject($val, $type)
  228. {
  229. $sql_inj = 0;
  230. // For SQL Injection (only GET and POST are used to be included into bad escaped SQL requests)
  231. if ($type != 2)
  232. {
  233. $sql_inj += preg_match('/delete\s+from/i', $val);
  234. $sql_inj += preg_match('/create\s+table/i', $val);
  235. $sql_inj += preg_match('/update.+set.+=/i', $val);
  236. $sql_inj += preg_match('/insert\s+into/i', $val);
  237. $sql_inj += preg_match('/select.+from/i', $val);
  238. $sql_inj += preg_match('/union.+select/i', $val);
  239. $sql_inj += preg_match('/into\s+(outfile|dumpfile)/i', $val);
  240. $sql_inj += preg_match('/(\.\.%2f)+/i', $val);
  241. }
  242. // For XSS Injection done by adding javascript with script
  243. // This is all cases a browser consider text is javascript:
  244. // When it found '<script', 'javascript:', '<style', 'onload\s=' on body tag, '="&' on a tag size with old browsers
  245. // All examples on page: http://ha.ckers.org/xss.html#XSScalc
  246. $sql_inj += preg_match('/<script/i', $val);
  247. if (! defined('NOSTYLECHECK')) $sql_inj += preg_match('/<style/i', $val);
  248. $sql_inj += preg_match('/base[\s]+href/si', $val);
  249. $sql_inj += preg_match('/<.*onmouse/si', $val); // onmousexxx can be set on img or any html tag like <img title='>' onmouseover=alert(1)>
  250. $sql_inj += preg_match('/onerror\s*=/i', $val); // onerror can be set on img or any html tag like <img title='>' onerror = alert(1)>
  251. if ($type == 1)
  252. {
  253. $sql_inj += preg_match('/javascript:/i', $val);
  254. $sql_inj += preg_match('/vbscript:/i', $val);
  255. }
  256. // For XSS Injection done by adding javascript closing html tags like with onmousemove, etc... (closing a src or href tag with not cleaned param)
  257. if ($type == 1) $sql_inj += preg_match('/"/i', $val); // We refused " in GET parameters value
  258. if ($type == 2) $sql_inj += preg_match('/[;"]/', $val); // PHP_SELF is a file system path. It can contains spaces.
  259. return $sql_inj;
  260. }
  261. // Run tests
  262. $_SERVER["PHP_SELF"]='/DIR WITH SPACE/htdocs/admin/index.php?mainmenu=home&leftmenu=setup&username=weservices';
  263. $result=test_sql_and_script_inject($_SERVER["PHP_SELF"], 2);
  264. $expectedresult=0;
  265. $this->assertEquals($expectedresult, $result, 'Error on test_sql_and_script_inject 1a');
  266. $_SERVER["PHP_SELF"]='/DIR WITH SPACE/htdocs/admin/index.php?mainmenu=home&leftmenu=setup&username=weservices;badaction';
  267. $result=test_sql_and_script_inject($_SERVER["PHP_SELF"], 2);
  268. $expectedresult=1;
  269. $this->assertEquals($expectedresult, $result, 'Error on test_sql_and_script_inject 1b');
  270. $_GET['aaa']="<img src='1.jpg' onerror =javascript:alert('XSS')>";
  271. $result=test_sql_and_script_inject($_GET['aaa'], 0);
  272. $expectedresult=1;
  273. $this->assertEquals($expectedresult, $result, 'Error on test_sql_and_script_inject 2');
  274. $_POST['bbb']="<img src='1.jpg' onerror =javascript:alert('XSS')>";
  275. $result=test_sql_and_script_inject($_POST['bbb'], 2);
  276. $expectedresult=1;
  277. $this->assertEquals($expectedresult, $result, 'Error on test_sql_and_script_inject 3');
  278. }
  279. }