PageRenderTime 26ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/test/classes/plugin/auth/AuthenticationHttpTest.php

https://gitlab.com/trungthao379/phpmyadmin
PHP | 458 lines | 319 code | 70 blank | 69 comment | 0 complexity | 191d72e7577a1f433604c6f709f72c83 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * tests for PMA\libraries\plugins\auth\AuthenticationHttp class
  5. *
  6. * @package PhpMyAdmin-test
  7. */
  8. use PMA\libraries\plugins\auth\AuthenticationHttp;
  9. require_once 'libraries/config.default.php';
  10. require_once 'test/PMATestCase.php';
  11. /**
  12. * tests for PMA\libraries\plugins\auth\AuthenticationHttp class
  13. *
  14. * @package PhpMyAdmin-test
  15. */
  16. class AuthenticationHttpTest extends PMATestCase
  17. {
  18. /**
  19. * @var AuthenticationHttp
  20. */
  21. protected $object;
  22. /**
  23. * Configures global environment.
  24. *
  25. * @return void
  26. */
  27. function setup()
  28. {
  29. $GLOBALS['PMA_Config'] = new PMA\libraries\Config;
  30. $GLOBALS['PMA_Config']->enableBc();
  31. $GLOBALS['server'] = 0;
  32. $GLOBALS['lang'] = "en";
  33. $GLOBALS['text_dir'] = "ltr";
  34. $GLOBALS['token_provided'] = true;
  35. $GLOBALS['token_mismatch'] = false;
  36. $this->object = new AuthenticationHttp();
  37. }
  38. /**
  39. * tearDown for test cases
  40. *
  41. * @return void
  42. */
  43. public function tearDown()
  44. {
  45. unset($this->object);
  46. }
  47. public function doMockResponse($set_minimal, $body_id, $set_title)
  48. {
  49. $restoreInstance = PMA\libraries\Response::getInstance();
  50. // mock footer
  51. $mockFooter = $this->getMockBuilder('PMA\libraries\Footer')
  52. ->disableOriginalConstructor()
  53. ->setMethods(array('setMinimal'))
  54. ->getMock();
  55. $mockFooter->expects($this->exactly($set_minimal))
  56. ->method('setMinimal')
  57. ->with();
  58. // mock header
  59. $mockHeader = $this->getMockBuilder('PMA\libraries\Header')
  60. ->disableOriginalConstructor()
  61. ->setMethods(
  62. array('setBodyId', 'setTitle', 'disableMenuAndConsole', 'addHTML')
  63. )
  64. ->getMock();
  65. $mockHeader->expects($this->exactly($body_id))
  66. ->method('setBodyId')
  67. ->with('loginform');
  68. $mockHeader->expects($this->exactly($set_title))
  69. ->method('setTitle')
  70. ->with('Access denied!');
  71. $mockHeader->expects($this->exactly($set_title))
  72. ->method('disableMenuAndConsole')
  73. ->with();
  74. // set mocked headers and footers
  75. $mockResponse = $this->getMockBuilder('PMA\libraries\Response')
  76. ->disableOriginalConstructor()
  77. ->setMethods(array('getHeader', 'getFooter', 'addHTML', 'header', 'headersSent'))
  78. ->getMock();
  79. $mockResponse->expects($this->exactly($set_title))
  80. ->method('getFooter')
  81. ->with()
  82. ->will($this->returnValue($mockFooter));
  83. $mockResponse->expects($this->exactly($set_title))
  84. ->method('getHeader')
  85. ->with()
  86. ->will($this->returnValue($mockHeader));
  87. $mockResponse->expects($this->any())
  88. ->method('headersSent')
  89. ->with()
  90. ->will($this->returnValue(false));
  91. $mockResponse->expects($this->exactly($set_title * 6))
  92. ->method('addHTML')
  93. ->with();
  94. $attrInstance = new ReflectionProperty('PMA\libraries\Response', '_instance');
  95. $attrInstance->setAccessible(true);
  96. $attrInstance->setValue($mockResponse);
  97. $headers = array_slice(func_get_args(), 3);
  98. $header_method = $mockResponse->expects($this->exactly(count($headers)))
  99. ->method('header');
  100. call_user_func_array(array($header_method, 'withConsecutive'), $headers);
  101. try {
  102. $this->assertFalse(
  103. $this->object->auth()
  104. );
  105. } finally {
  106. $attrInstance->setValue($restoreInstance);
  107. }
  108. }
  109. /**
  110. * Test for PMA\libraries\plugins\auth\AuthenticationHttp::auth
  111. *
  112. * @return void
  113. */
  114. public function testAuthLogoutUrl()
  115. {
  116. $_REQUEST['old_usr'] = '1';
  117. $GLOBALS['cfg']['Server']['LogoutURL'] = 'http://phpmyadmin.net/logout';
  118. $this->doMockResponse(
  119. 0, 0, 0,
  120. array('Location: http://phpmyadmin.net/logout' . ((SID) ? '?' . SID : ''))
  121. );
  122. }
  123. public function testAuthVerbose()
  124. {
  125. $_REQUEST['old_usr'] = '';
  126. $GLOBALS['cfg']['Server']['verbose'] = 'verboseMessagê';
  127. $this->doMockResponse(
  128. 1, 1, 1,
  129. array('WWW-Authenticate: Basic realm="phpMyAdmin verboseMessag"'),
  130. array('HTTP/1.0 401 Unauthorized'),
  131. array('status: 401 Unauthorized')
  132. );
  133. }
  134. public function testAuthHost()
  135. {
  136. $GLOBALS['cfg']['Server']['verbose'] = '';
  137. $GLOBALS['cfg']['Server']['host'] = 'hòst';
  138. $this->doMockResponse(
  139. 1, 1, 1,
  140. array('WWW-Authenticate: Basic realm="phpMyAdmin hst"'),
  141. array('HTTP/1.0 401 Unauthorized'),
  142. array('status: 401 Unauthorized')
  143. );
  144. }
  145. public function testAuthRealm()
  146. {
  147. $GLOBALS['cfg']['Server']['host'] = '';
  148. $GLOBALS['cfg']['Server']['auth_http_realm'] = 'rêäealmmessage';
  149. $this->doMockResponse(
  150. 1, 1, 1,
  151. array('WWW-Authenticate: Basic realm="realmmessage"'),
  152. array('HTTP/1.0 401 Unauthorized'),
  153. array('status: 401 Unauthorized')
  154. );
  155. }
  156. /**
  157. * Test for PMA\libraries\plugins\auth\AuthenticationHttp::authCheck
  158. *
  159. * @param string $user test username
  160. * @param string $pass test password
  161. * @param string $userIndex index to test username against
  162. * @param string $passIndex index to test username against
  163. * @param string $expectedReturn expected return value from test
  164. * @param string $expectedUser expected username to be set
  165. * @param string $expectedPass expected password to be set
  166. * @param string $old_usr value for $_REQUEST['old_usr']
  167. *
  168. * @return void
  169. * @dataProvider authCheckProvider
  170. */
  171. public function testAuthCheck($user, $pass, $userIndex, $passIndex,
  172. $expectedReturn, $expectedUser, $expectedPass, $old_usr = ''
  173. ) {
  174. $GLOBALS['PHP_AUTH_USER'] = '';
  175. $GLOBALS['PHP_AUTH_PW'] = '';
  176. $_SERVER[$userIndex] = $user;
  177. $_SERVER[$passIndex] = $pass;
  178. $_REQUEST['old_usr'] = $old_usr;
  179. $this->assertEquals(
  180. $expectedReturn,
  181. $this->object->authCheck()
  182. );
  183. $this->assertEquals(
  184. $expectedUser,
  185. $GLOBALS['PHP_AUTH_USER']
  186. );
  187. $this->assertEquals(
  188. $expectedPass,
  189. $GLOBALS['PHP_AUTH_PW']
  190. );
  191. $_SERVER[$userIndex] = null;
  192. $_SERVER[$passIndex] = null;
  193. }
  194. /**
  195. * Data provider for testAuthCheck
  196. *
  197. * @return array Test data
  198. */
  199. public function authCheckProvider()
  200. {
  201. return array(
  202. array(
  203. 'Basic ' . base64_encode('foo:bar'),
  204. 'pswd',
  205. 'PHP_AUTH_USER',
  206. 'PHP_AUTH_PW',
  207. false,
  208. '',
  209. 'bar',
  210. 'foo'
  211. ),
  212. array(
  213. 'Basic ' . base64_encode('foobar'),
  214. 'pswd',
  215. 'REMOTE_USER',
  216. 'REMOTE_PASSWORD',
  217. true,
  218. 'Basic Zm9vYmFy',
  219. 'pswd'
  220. ),
  221. array(
  222. 'Basic ' . base64_encode('foobar:'),
  223. 'pswd',
  224. 'AUTH_USER',
  225. 'AUTH_PASSWORD',
  226. true,
  227. 'foobar',
  228. false
  229. ),
  230. array(
  231. 'Basic ' . base64_encode(':foobar'),
  232. 'pswd',
  233. 'HTTP_AUTHORIZATION',
  234. 'AUTH_PASSWORD',
  235. true,
  236. 'Basic OmZvb2Jhcg==',
  237. 'pswd'
  238. ),
  239. array(
  240. 'BasicTest',
  241. 'pswd',
  242. 'Authorization',
  243. 'AUTH_PASSWORD',
  244. true,
  245. 'BasicTest',
  246. 'pswd'
  247. ),
  248. );
  249. }
  250. /**
  251. * Test for PMA\libraries\plugins\auth\AuthenticationHttp::authSetUser
  252. *
  253. * @return void
  254. */
  255. public function testAuthSetUser()
  256. {
  257. // case 1
  258. $GLOBALS['PHP_AUTH_USER'] = 'testUser';
  259. $GLOBALS['PHP_AUTH_PW'] = 'testPass';
  260. $GLOBALS['server'] = 2;
  261. $GLOBALS['cfg']['Server']['user'] = 'testUser';
  262. $this->assertTrue(
  263. $this->object->authSetUser()
  264. );
  265. $this->assertEquals(
  266. 'testUser',
  267. $GLOBALS['cfg']['Server']['user']
  268. );
  269. $this->assertEquals(
  270. 'testPass',
  271. $GLOBALS['cfg']['Server']['password']
  272. );
  273. $this->assertFalse(
  274. isset($GLOBALS['PHP_AUTH_PW'])
  275. );
  276. $this->assertFalse(
  277. isset($_SERVER['PHP_AUTH_PW'])
  278. );
  279. $this->assertEquals(
  280. 2,
  281. $GLOBALS['server']
  282. );
  283. // case 2
  284. $GLOBALS['PHP_AUTH_USER'] = 'testUser';
  285. $GLOBALS['PHP_AUTH_PW'] = 'testPass';
  286. $GLOBALS['cfg']['Servers'][1] = array(
  287. 'host' => 'a',
  288. 'user' => 'testUser',
  289. 'foo' => 'bar'
  290. );
  291. $GLOBALS['cfg']['Server']= array(
  292. 'host' => 'a',
  293. 'user' => 'user2'
  294. );
  295. $this->assertTrue(
  296. $this->object->authSetUser()
  297. );
  298. $this->assertEquals(
  299. array(
  300. 'user' => 'testUser',
  301. 'password' => 'testPass',
  302. 'host' => 'a',
  303. 'foo' => 'bar'
  304. ),
  305. $GLOBALS['cfg']['Server']
  306. );
  307. $this->assertEquals(
  308. 1,
  309. $GLOBALS['server']
  310. );
  311. // case 3
  312. $GLOBALS['server'] = 3;
  313. $GLOBALS['PHP_AUTH_USER'] = 'testUser';
  314. $GLOBALS['PHP_AUTH_PW'] = 'testPass';
  315. $GLOBALS['cfg']['Servers'][1] = array(
  316. 'host' => 'a',
  317. 'user' => 'testUsers',
  318. 'foo' => 'bar'
  319. );
  320. $GLOBALS['cfg']['Server']= array(
  321. 'host' => 'a',
  322. 'user' => 'user2'
  323. );
  324. $this->assertTrue(
  325. $this->object->authSetUser()
  326. );
  327. $this->assertEquals(
  328. array(
  329. 'user' => 'testUser',
  330. 'password' => 'testPass',
  331. 'host' => 'a'
  332. ),
  333. $GLOBALS['cfg']['Server']
  334. );
  335. $this->assertEquals(
  336. 3,
  337. $GLOBALS['server']
  338. );
  339. }
  340. /**
  341. * Test for PMA\libraries\plugins\auth\AuthenticationHttp::authSetFails
  342. *
  343. * @return void
  344. *
  345. * @group medium
  346. */
  347. public function testAuthFails()
  348. {
  349. $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
  350. ->disableOriginalConstructor()
  351. ->getMock();
  352. $dbi->expects($this->at(0))
  353. ->method('getError')
  354. ->will($this->returnValue('error 123'));
  355. $dbi->expects($this->at(1))
  356. ->method('getError')
  357. ->will($this->returnValue('error 321'));
  358. $dbi->expects($this->at(2))
  359. ->method('getError')
  360. ->will($this->returnValue(null));
  361. $GLOBALS['dbi'] = $dbi;
  362. $GLOBALS['errno'] = 31;
  363. ob_start();
  364. $this->object->authFails();
  365. $result = ob_get_clean();
  366. $this->assertContains(
  367. '<p>error 123</p>',
  368. $result
  369. );
  370. $this->object = $this->getMockBuilder('PMA\libraries\plugins\auth\AuthenticationHttp')
  371. ->disableOriginalConstructor()
  372. ->setMethods(array('authForm'))
  373. ->getMock();
  374. $this->object->expects($this->exactly(2))
  375. ->method('authForm');
  376. // case 2
  377. $GLOBALS['cfg']['Server']['host'] = 'host';
  378. $GLOBALS['errno'] = 1045;
  379. $this->assertTrue(
  380. $this->object->authFails()
  381. );
  382. // case 3
  383. $GLOBALS['errno'] = 1043;
  384. $this->assertTrue(
  385. $this->object->authFails()
  386. );
  387. }
  388. }