PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/controller/components/security.test.php

https://bitbucket.org/jmhmd/hrfreeclinic
PHP | 955 lines | 582 code | 50 blank | 323 comment | 2 complexity | 0120356c3456ff3df23ad1996f92649a MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, MIT, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /* SVN FILE: $Id: security.test.php 7690 2008-10-02 04:56:53Z nate $ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc.
  12. * 1785 E. Sahara Avenue, Suite 490-204
  13. * Las Vegas, Nevada 89104
  14. *
  15. * Licensed under The Open Group Test Suite License
  16. * Redistributions of files must retain the above copyright notice.
  17. *
  18. * @filesource
  19. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
  20. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  21. * @package cake.tests
  22. * @subpackage cake.tests.cases.libs.controller.components
  23. * @since CakePHP(tm) v 1.2.0.5435
  24. * @version $Revision: 7690 $
  25. * @modifiedby $LastChangedBy: nate $
  26. * @lastmodified $Date: 2008-10-02 00:56:53 -0400 (Thu, 02 Oct 2008) $
  27. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  28. */
  29. App::import('Component', 'Security');
  30. /**
  31. * Short description for class.
  32. *
  33. * @package cake.tests
  34. * @subpackage cake.tests.cases.libs.controller.components
  35. */
  36. class TestSecurityComponent extends SecurityComponent {
  37. function validatePost(&$controller) {
  38. return $this->_validatePost($controller);
  39. }
  40. }
  41. /**
  42. * Short description for class.
  43. *
  44. * @package cake.tests
  45. * @subpackage cake.tests.cases.libs.controller.components
  46. */
  47. class SecurityTestController extends Controller {
  48. /**
  49. * name property
  50. *
  51. * @var string 'SecurityTest'
  52. * @access public
  53. */
  54. var $name = 'SecurityTest';
  55. /**
  56. * components property
  57. *
  58. * @var array
  59. * @access public
  60. */
  61. var $components = array('TestSecurity');
  62. /**
  63. * failed property
  64. *
  65. * @var bool false
  66. * @access public
  67. */
  68. var $failed = false;
  69. /**
  70. * fail method
  71. *
  72. * @access public
  73. * @return void
  74. */
  75. function fail() {
  76. $this->failed = true;
  77. }
  78. /**
  79. * redirect method
  80. *
  81. * @param mixed $option
  82. * @param mixed $code
  83. * @param mixed $exit
  84. * @access public
  85. * @return void
  86. */
  87. function redirect($option, $code, $exit) {
  88. return $code;
  89. }
  90. }
  91. /**
  92. * Short description for class.
  93. *
  94. * @package cake.tests
  95. * @subpackage cake.tests.cases.libs.controller.components
  96. */
  97. class SecurityComponentTest extends CakeTestCase {
  98. /**
  99. * setUp method
  100. *
  101. * @access public
  102. * @return void
  103. */
  104. function setUp() {
  105. $this->Controller =& new SecurityTestController();
  106. $this->Controller->Component->init($this->Controller);
  107. $this->Controller->Security =& $this->Controller->TestSecurity;
  108. $this->Controller->Security->blackHoleCallback = 'fail';
  109. $this->oldSalt = Configure::read('Security.salt');
  110. Configure::write('Security.salt', 'foo!');
  111. }
  112. /**
  113. * Tear-down method. Resets environment state.
  114. *
  115. * @access public
  116. * @return void
  117. */
  118. function tearDown() {
  119. unset($this->Controller->Security);
  120. unset($this->Controller->Component);
  121. unset($this->Controller);
  122. Configure::write('Security.salt', $this->oldSalt);
  123. }
  124. /**
  125. * testStartup method
  126. *
  127. * @access public
  128. * @return void
  129. */
  130. function testStartup() {
  131. $this->Controller->Security->startup($this->Controller);
  132. $result = $this->Controller->params['_Token']['key'];
  133. $this->assertNotNull($result);
  134. $this->assertTrue($this->Controller->Session->check('_Token'));
  135. }
  136. /**
  137. * testRequirePostFail method
  138. *
  139. * @access public
  140. * @return void
  141. */
  142. function testRequirePostFail() {
  143. $_SERVER['REQUEST_METHOD'] = 'GET';
  144. $this->Controller->action = 'posted';
  145. $this->Controller->Security->requirePost('posted');
  146. $this->Controller->Security->startup($this->Controller);
  147. $this->assertTrue($this->Controller->failed);
  148. }
  149. /**
  150. * testRequirePostSucceed method
  151. *
  152. * @access public
  153. * @return void
  154. */
  155. function testRequirePostSucceed() {
  156. $_SERVER['REQUEST_METHOD'] = 'POST';
  157. $this->Controller->action = 'posted';
  158. $this->Controller->Security->requirePost('posted');
  159. $this->Controller->Security->startup($this->Controller);
  160. $this->assertFalse($this->Controller->failed);
  161. }
  162. /**
  163. * testRequireSecureFail method
  164. *
  165. * @access public
  166. * @return void
  167. */
  168. function testRequireSecureFail() {
  169. $_SERVER['REQUEST_METHOD'] = 'POST';
  170. $this->Controller->action = 'posted';
  171. $this->Controller->Security->requireSecure('posted');
  172. $this->Controller->Security->startup($this->Controller);
  173. $this->assertTrue($this->Controller->failed);
  174. }
  175. /**
  176. * testRequireSecureSucceed method
  177. *
  178. * @access public
  179. * @return void
  180. */
  181. function testRequireSecureSucceed() {
  182. $_SERVER['REQUEST_METHOD'] = 'Secure';
  183. $this->Controller->action = 'posted';
  184. $_SERVER['HTTPS'] = true;
  185. $this->Controller->Security->requireSecure('posted');
  186. $this->Controller->Security->startup($this->Controller);
  187. $this->assertFalse($this->Controller->failed);
  188. }
  189. /**
  190. * testRequireAuthFail method
  191. *
  192. * @access public
  193. * @return void
  194. */
  195. function testRequireAuthFail() {
  196. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  197. $this->Controller->action = 'posted';
  198. $this->Controller->data = array('username' => 'willy', 'password' => 'somePass');
  199. $this->Controller->Security->requireAuth('posted');
  200. $this->Controller->Security->startup($this->Controller);
  201. $this->assertTrue($this->Controller->failed);
  202. $this->Controller->Session->write('_Token', array('allowedControllers' => array()));
  203. $this->Controller->data = array('username' => 'willy', 'password' => 'somePass');
  204. $this->Controller->action = 'posted';
  205. $this->Controller->Security->requireAuth('posted');
  206. $this->Controller->Security->startup($this->Controller);
  207. $this->assertTrue($this->Controller->failed);
  208. $this->Controller->Session->write('_Token', array(
  209. 'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted2')
  210. ));
  211. $this->Controller->data = array('username' => 'willy', 'password' => 'somePass');
  212. $this->Controller->action = 'posted';
  213. $this->Controller->Security->requireAuth('posted');
  214. $this->Controller->Security->startup($this->Controller);
  215. $this->assertTrue($this->Controller->failed);
  216. }
  217. /**
  218. * testRequireAuthSucceed method
  219. *
  220. * @access public
  221. * @return void
  222. */
  223. function testRequireAuthSucceed() {
  224. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  225. $this->Controller->action = 'posted';
  226. $this->Controller->Security->requireAuth('posted');
  227. $this->Controller->Security->startup($this->Controller);
  228. $this->assertFalse($this->Controller->failed);
  229. $this->Controller->Security->Session->write('_Token', serialize(array(
  230. 'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted')
  231. )));
  232. $this->Controller->params['controller'] = 'SecurityTest';
  233. $this->Controller->params['action'] = 'posted';
  234. $this->Controller->data = array(
  235. 'username' => 'willy', 'password' => 'somePass', '_Token' => ''
  236. );
  237. $this->Controller->action = 'posted';
  238. $this->Controller->Security->requireAuth('posted');
  239. $this->Controller->Security->startup($this->Controller);
  240. $this->assertFalse($this->Controller->failed);
  241. }
  242. /**
  243. * testRequirePostSucceedWrongMethod method
  244. *
  245. * @access public
  246. * @return void
  247. */
  248. function testRequirePostSucceedWrongMethod() {
  249. $_SERVER['REQUEST_METHOD'] = 'GET';
  250. $this->Controller->action = 'getted';
  251. $this->Controller->Security->requirePost('posted');
  252. $this->Controller->Security->startup($this->Controller);
  253. $this->assertFalse($this->Controller->failed);
  254. }
  255. /**
  256. * testRequireGetFail method
  257. *
  258. * @access public
  259. * @return void
  260. */
  261. function testRequireGetFail() {
  262. $_SERVER['REQUEST_METHOD'] = 'POST';
  263. $this->Controller->action = 'getted';
  264. $this->Controller->Security->requireGet('getted');
  265. $this->Controller->Security->startup($this->Controller);
  266. $this->assertTrue($this->Controller->failed);
  267. }
  268. /**
  269. * testRequireGetSucceed method
  270. *
  271. * @access public
  272. * @return void
  273. */
  274. function testRequireGetSucceed() {
  275. $_SERVER['REQUEST_METHOD'] = 'GET';
  276. $this->Controller->action = 'getted';
  277. $this->Controller->Security->requireGet('getted');
  278. $this->Controller->Security->startup($this->Controller);
  279. $this->assertFalse($this->Controller->failed);
  280. }
  281. /**
  282. * testRequireLogin method
  283. *
  284. * @access public
  285. * @return void
  286. */
  287. function testRequireLogin() {
  288. $this->Controller->action = 'posted';
  289. $this->Controller->Security->requireLogin(
  290. 'posted',
  291. array('type' => 'basic', 'users' => array('admin' => 'password'))
  292. );
  293. $_SERVER['PHP_AUTH_USER'] = 'admin';
  294. $_SERVER['PHP_AUTH_PW'] = 'password';
  295. $this->Controller->Security->startup($this->Controller);
  296. $this->assertFalse($this->Controller->failed);
  297. $this->Controller->action = 'posted';
  298. $this->Controller->Security->requireLogin(
  299. 'posted',
  300. array('type' => 'basic', 'users' => array('admin' => 'password'))
  301. );
  302. $_SERVER['PHP_AUTH_USER'] = 'admin2';
  303. $_SERVER['PHP_AUTH_PW'] = 'password';
  304. $this->Controller->Security->startup($this->Controller);
  305. $this->assertTrue($this->Controller->failed);
  306. $this->Controller->action = 'posted';
  307. $this->Controller->Security->requireLogin(
  308. 'posted',
  309. array('type' => 'basic', 'users' => array('admin' => 'password'))
  310. );
  311. $_SERVER['PHP_AUTH_USER'] = 'admin';
  312. $_SERVER['PHP_AUTH_PW'] = 'password2';
  313. $this->Controller->Security->startup($this->Controller);
  314. $this->assertTrue($this->Controller->failed);
  315. }
  316. /**
  317. * testDigestAuth method
  318. *
  319. * @access public
  320. * @return void
  321. */
  322. function testDigestAuth() {
  323. $this->Controller->action = 'posted';
  324. $_SERVER['PHP_AUTH_DIGEST'] = $digest = <<<DIGEST
  325. Digest username="Mufasa",
  326. realm="testrealm@host.com",
  327. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  328. uri="/dir/index.html",
  329. qop=auth,
  330. nc=00000001,
  331. cnonce="0a4f113b",
  332. response="460d0d3c6867c2f1ab85b1ada1aece48",
  333. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  334. DIGEST;
  335. $this->Controller->Security->requireLogin('posted', array(
  336. 'type' => 'digest', 'users' => array('Mufasa' => 'password'),
  337. 'realm' => 'testrealm@host.com'
  338. ));
  339. $this->Controller->Security->startup($this->Controller);
  340. $this->assertFalse($this->Controller->failed);
  341. }
  342. /**
  343. * testRequireGetSucceedWrongMethod method
  344. *
  345. * @access public
  346. * @return void
  347. */
  348. function testRequireGetSucceedWrongMethod() {
  349. $_SERVER['REQUEST_METHOD'] = 'POST';
  350. $this->Controller->action = 'posted';
  351. $this->Controller->Security->requireGet('getted');
  352. $this->Controller->Security->startup($this->Controller);
  353. $this->assertFalse($this->Controller->failed);
  354. }
  355. /**
  356. * testRequirePutFail method
  357. *
  358. * @access public
  359. * @return void
  360. */
  361. function testRequirePutFail() {
  362. $_SERVER['REQUEST_METHOD'] = 'POST';
  363. $this->Controller->action = 'putted';
  364. $this->Controller->Security->requirePut('putted');
  365. $this->Controller->Security->startup($this->Controller);
  366. $this->assertTrue($this->Controller->failed);
  367. }
  368. /**
  369. * testRequirePutSucceed method
  370. *
  371. * @access public
  372. * @return void
  373. */
  374. function testRequirePutSucceed() {
  375. $_SERVER['REQUEST_METHOD'] = 'PUT';
  376. $this->Controller->action = 'putted';
  377. $this->Controller->Security->requirePut('putted');
  378. $this->Controller->Security->startup($this->Controller);
  379. $this->assertFalse($this->Controller->failed);
  380. }
  381. /**
  382. * testRequirePutSucceedWrongMethod method
  383. *
  384. * @access public
  385. * @return void
  386. */
  387. function testRequirePutSucceedWrongMethod() {
  388. $_SERVER['REQUEST_METHOD'] = 'POST';
  389. $this->Controller->action = 'posted';
  390. $this->Controller->Security->requirePut('putted');
  391. $this->Controller->Security->startup($this->Controller);
  392. $this->assertFalse($this->Controller->failed);
  393. }
  394. /**
  395. * testRequireDeleteFail method
  396. *
  397. * @access public
  398. * @return void
  399. */
  400. function testRequireDeleteFail() {
  401. $_SERVER['REQUEST_METHOD'] = 'POST';
  402. $this->Controller->action = 'deleted';
  403. $this->Controller->Security->requireDelete('deleted');
  404. $this->Controller->Security->startup($this->Controller);
  405. $this->assertTrue($this->Controller->failed);
  406. }
  407. /**
  408. * testRequireDeleteSucceed method
  409. *
  410. * @access public
  411. * @return void
  412. */
  413. function testRequireDeleteSucceed() {
  414. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  415. $this->Controller->action = 'deleted';
  416. $this->Controller->Security->requireDelete('deleted');
  417. $this->Controller->Security->startup($this->Controller);
  418. $this->assertFalse($this->Controller->failed);
  419. }
  420. /**
  421. * testRequireDeleteSucceedWrongMethod method
  422. *
  423. * @access public
  424. * @return void
  425. */
  426. function testRequireDeleteSucceedWrongMethod() {
  427. $_SERVER['REQUEST_METHOD'] = 'POST';
  428. $this->Controller->action = 'posted';
  429. $this->Controller->Security->requireDelete('deleted');
  430. $this->Controller->Security->startup($this->Controller);
  431. $this->assertFalse($this->Controller->failed);
  432. }
  433. /**
  434. * testRequireLoginSettings method
  435. *
  436. * @access public
  437. * @return void
  438. */
  439. function testRequireLoginSettings() {
  440. $this->Controller->Security->requireLogin(
  441. 'add', 'edit',
  442. array('type' => 'basic', 'users' => array('admin' => 'password'))
  443. );
  444. $this->assertEqual($this->Controller->Security->requireLogin, array('add', 'edit'));
  445. $this->assertEqual($this->Controller->Security->loginUsers, array('admin' => 'password'));
  446. }
  447. /**
  448. * testRequireLoginAllActions method
  449. *
  450. * @access public
  451. * @return void
  452. */
  453. function testRequireLoginAllActions() {
  454. $this->Controller->Security->requireLogin(
  455. array('type' => 'basic', 'users' => array('admin' => 'password'))
  456. );
  457. $this->assertEqual($this->Controller->Security->requireLogin, array('*'));
  458. $this->assertEqual($this->Controller->Security->loginUsers, array('admin' => 'password'));
  459. }
  460. /**
  461. * Simple hash validation test
  462. *
  463. * @access public
  464. * @return void
  465. */
  466. function testValidatePost() {
  467. $this->Controller->Security->startup($this->Controller);
  468. $key = $this->Controller->params['_Token']['key'];
  469. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3An%3A1%3A%7Bv%3A0%3B';
  470. $fields .= 'f%3A11%3A%22Zbqry.inyvq%22%3B%7D';
  471. $this->Controller->data = array(
  472. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  473. '_Token' => compact('key', 'fields')
  474. );
  475. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  476. }
  477. /**
  478. * testValidatePostNoModel method
  479. *
  480. * @access public
  481. * @return void
  482. */
  483. function testValidatePostNoModel() {
  484. $this->Controller->Security->startup($this->Controller);
  485. $key = $this->Controller->params['_Token']['key'];
  486. $fields = '540ac9c60d323c22bafe997b72c0790f39a8bdef%3An%3A0%3A%7B%7D';
  487. $this->Controller->data = array(
  488. 'anything' => 'some_data',
  489. '_Token' => compact('key', 'fields')
  490. );
  491. $result = $this->Controller->Security->validatePost($this->Controller);
  492. $this->assertTrue($result);
  493. }
  494. /**
  495. * testValidatePostSimple method
  496. *
  497. * @access public
  498. * @return void
  499. */
  500. function testValidatePostSimple() {
  501. $this->Controller->Security->startup($this->Controller);
  502. $key = $this->Controller->params['_Token']['key'];
  503. $fields = '69f493434187b867ea14b901fdf58b55d27c935d%3An%3A0%3A%7B%7D';
  504. $this->Controller->data = $data = array(
  505. 'Model' => array('username' => '', 'password' => ''),
  506. '_Token' => compact('key', 'fields')
  507. );
  508. $result = $this->Controller->Security->validatePost($this->Controller);
  509. $this->assertTrue($result);
  510. }
  511. /**
  512. * Tests hash validation for multiple records, including locked fields
  513. *
  514. * @access public
  515. * @return void
  516. */
  517. function testValidatePostComplex() {
  518. $this->Controller->Security->startup($this->Controller);
  519. $key = $this->Controller->params['_Token']['key'];
  520. $fields = 'c9118120e680a7201b543f562e5301006ccfcbe2%3An%3A2%3A%7Bv%3A0%3Bf%3A14%3A%';
  521. $fields .= '22Nqqerffrf.0.vq%22%3Bv%3A1%3Bf%3A14%3A%22Nqqerffrf.1.vq%22%3B%7D';
  522. $this->Controller->data = array(
  523. 'Addresses' => array(
  524. '0' => array(
  525. 'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
  526. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  527. ),
  528. '1' => array(
  529. 'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
  530. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  531. )
  532. ),
  533. '_Token' => compact('key', 'fields')
  534. );
  535. $result = $this->Controller->Security->validatePost($this->Controller);
  536. $this->assertTrue($result);
  537. }
  538. /**
  539. * testValidatePostCheckbox method
  540. *
  541. * First block tests un-checked checkbox
  542. * Second block tests checked checkbox
  543. *
  544. * @access public
  545. * @return void
  546. */
  547. function testValidatePostCheckbox() {
  548. $this->Controller->Security->startup($this->Controller);
  549. $key = $this->Controller->params['_Token']['key'];
  550. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3An%3A1%3A%7Bv%3A0%';
  551. $fields .= '3Bf%3A11%3A%22Zbqry.inyvq%22%3B%7D';
  552. $this->Controller->data = array(
  553. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  554. '_Token' => compact('key', 'fields')
  555. );
  556. $result = $this->Controller->Security->validatePost($this->Controller);
  557. $this->assertTrue($result);
  558. $fields = '874439ca69f89b4c4a5f50fb9c36ff56a28f5d42%3An%3A0%3A%7B%7D';
  559. $this->Controller->data = array(
  560. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  561. '_Token' => compact('key', 'fields')
  562. );
  563. $result = $this->Controller->Security->validatePost($this->Controller);
  564. $this->assertTrue($result);
  565. $this->Controller->data = array();
  566. $this->Controller->Security->startup($this->Controller);
  567. $key = $this->Controller->params['_Token']['key'];
  568. $this->Controller->data = $data = array(
  569. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  570. '_Token' => compact('key', 'fields')
  571. );
  572. $result = $this->Controller->Security->validatePost($this->Controller);
  573. $this->assertTrue($result);
  574. }
  575. /**
  576. * testValidatePostHidden method
  577. *
  578. * @access public
  579. * @return void
  580. */
  581. function testValidatePostHidden() {
  582. $this->Controller->Security->startup($this->Controller);
  583. $key = $this->Controller->params['_Token']['key'];
  584. $fields = '51ccd8cb0997c7b3d4523ecde5a109318405ef8c%3An%3A2%3A%7Bv%3A0%3Bf%3A12%3A';
  585. $fields .= '%22Zbqry.uvqqra%22%3Bv%3A1%3Bf%3A18%3A%22Zbqry.bgure_uvqqra%22%3B%7D';
  586. $this->Controller->data = array(
  587. 'Model' => array(
  588. 'username' => '', 'password' => '', 'hidden' => '0',
  589. 'other_hidden' => 'some hidden value'
  590. ),
  591. '_Token' => compact('key', 'fields')
  592. );
  593. $result = $this->Controller->Security->validatePost($this->Controller);
  594. $this->assertTrue($result);
  595. }
  596. /**
  597. * testValidatePostWithDisabledFields method
  598. *
  599. * @access public
  600. * @return void
  601. */
  602. function testValidatePostWithDisabledFields() {
  603. $this->Controller->Security->disabledFields = array('Model.username', 'Model.password');
  604. $this->Controller->Security->startup($this->Controller);
  605. $key = $this->Controller->params['_Token']['key'];
  606. $fields = 'ef1082968c449397bcd849f963636864383278b1%3An%3A1%3A%7Bv%';
  607. $fields .= '3A0%3Bf%3A12%3A%22Zbqry.uvqqra%22%3B%7D';
  608. $this->Controller->data = array(
  609. 'Model' => array(
  610. 'username' => '', 'password' => '', 'hidden' => '0'
  611. ),
  612. '_Token' => compact('fields', 'key')
  613. );
  614. $result = $this->Controller->Security->validatePost($this->Controller);
  615. $this->assertTrue($result);
  616. }
  617. /**
  618. * testValidateHiddenMultipleModel method
  619. *
  620. * @access public
  621. * @return void
  622. */
  623. function testValidateHiddenMultipleModel() {
  624. $this->Controller->Security->startup($this->Controller);
  625. $key = $this->Controller->params['_Token']['key'];
  626. $fields = 'a2d01072dc4660eea9d15007025f35a7a5b58e18%3An%3A3%3A%7Bv%3A0%3Bf%3A11';
  627. $fields .= '%3A%22Zbqry.inyvq%22%3Bv%3A1%3Bf%3A12%3A%22Zbqry2.inyvq%22%3Bv%3A2%';
  628. $fields .= '3Bf%3A12%3A%22Zbqry3.inyvq%22%3B%7D';
  629. $this->Controller->data = array(
  630. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  631. 'Model2' => array('valid' => '0'),
  632. 'Model3' => array('valid' => '0'),
  633. '_Token' => compact('key', 'fields')
  634. );
  635. $result = $this->Controller->Security->validatePost($this->Controller);
  636. $this->assertTrue($result);
  637. }
  638. /**
  639. * testLoginValidation method
  640. *
  641. * @access public
  642. * @return void
  643. */
  644. function testLoginValidation() {
  645. }
  646. /**
  647. * testValidateHasManyModel method
  648. *
  649. * @access public
  650. * @return void
  651. */
  652. function testValidateHasManyModel() {
  653. $this->Controller->Security->startup($this->Controller);
  654. $key = $this->Controller->params['_Token']['key'];
  655. $fields = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3An%3A4%3A%7Bv%3A0%3Bf%3A14%3A%2';
  656. $fields .= '2Zbqry.0.uvqqra%22%3Bv%3A1%3Bf%3A13%3A%22Zbqry.0.inyvq%22%3Bv%3A2%3Bf%3';
  657. $fields .= 'A14%3A%22Zbqry.1.uvqqra%22%3Bv%3A3%3Bf%3A13%3A%22Zbqry.1.inyvq%22%3B%7D';
  658. $this->Controller->data = array(
  659. 'Model' => array(
  660. array(
  661. 'username' => 'username', 'password' => 'password',
  662. 'hidden' => 'value', 'valid' => '0'
  663. ),
  664. array(
  665. 'username' => 'username', 'password' => 'password',
  666. 'hidden' => 'value', 'valid' => '0'
  667. )
  668. ),
  669. '_Token' => compact('key', 'fields')
  670. );
  671. $result = $this->Controller->Security->validatePost($this->Controller);
  672. $this->assertTrue($result);
  673. }
  674. /**
  675. * testValidateHasManyRecordsPass method
  676. *
  677. * @access public
  678. * @return void
  679. */
  680. function testValidateHasManyRecordsPass() {
  681. $this->Controller->Security->startup($this->Controller);
  682. $key = $this->Controller->params['_Token']['key'];
  683. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3An%3A4%3A%7Bv%3A0%3Bf%3A12%3A%2';
  684. $fields .= '2Nqqerff.0.vq%22%3Bv%3A1%3Bf%3A17%3A%22Nqqerff.0.cevznel%22%3Bv%3A2%3Bf%';
  685. $fields .= '3A12%3A%22Nqqerff.1.vq%22%3Bv%3A3%3Bf%3A17%3A%22Nqqerff.1.cevznel%22%3B%7D';
  686. $this->Controller->data = array(
  687. 'Address' => array(
  688. 0 => array(
  689. 'id' => '123',
  690. 'title' => 'home',
  691. 'first_name' => 'Bilbo',
  692. 'last_name' => 'Baggins',
  693. 'address' => '23 Bag end way',
  694. 'city' => 'the shire',
  695. 'phone' => 'N/A',
  696. 'primary' => '1',
  697. ),
  698. 1 => array(
  699. 'id' => '124',
  700. 'title' => 'home',
  701. 'first_name' => 'Frodo',
  702. 'last_name' => 'Baggins',
  703. 'address' => '50 Bag end way',
  704. 'city' => 'the shire',
  705. 'phone' => 'N/A',
  706. 'primary' => '1'
  707. )
  708. ),
  709. '_Token' => compact('key', 'fields')
  710. );
  711. $result = $this->Controller->Security->validatePost($this->Controller);
  712. $this->assertTrue($result);
  713. }
  714. /**
  715. * testValidateHasManyRecords method
  716. *
  717. * validatePost should fail, hidden fields have been changed.
  718. *
  719. * @access public
  720. * @return void
  721. */
  722. function testValidateHasManyRecordsFail() {
  723. $this->Controller->Security->startup($this->Controller);
  724. $key = $this->Controller->params['_Token']['key'];
  725. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3An%3A4%3A%7Bv%3A0%3Bf%3A12%3A%2';
  726. $fields .= '2Nqqerff.0.vq%22%3Bv%3A1%3Bf%3A17%3A%22Nqqerff.0.cevznel%22%3Bv%3A2%3Bf%';
  727. $fields .= '3A12%3A%22Nqqerff.1.vq%22%3Bv%3A3%3Bf%3A17%3A%22Nqqerff.1.cevznel%22%3B%7D';
  728. $this->Controller->data = array(
  729. 'Address' => array(
  730. 0 => array(
  731. 'id' => '123',
  732. 'title' => 'home',
  733. 'first_name' => 'Bilbo',
  734. 'last_name' => 'Baggins',
  735. 'address' => '23 Bag end way',
  736. 'city' => 'the shire',
  737. 'phone' => 'N/A',
  738. 'primary' => '5',
  739. ),
  740. 1 => array(
  741. 'id' => '124',
  742. 'title' => 'home',
  743. 'first_name' => 'Frodo',
  744. 'last_name' => 'Baggins',
  745. 'address' => '50 Bag end way',
  746. 'city' => 'the shire',
  747. 'phone' => 'N/A',
  748. 'primary' => '1'
  749. )
  750. ),
  751. '_Token' => compact('key', 'fields')
  752. );
  753. $result = $this->Controller->Security->validatePost($this->Controller);
  754. $this->assertFalse($result);
  755. }
  756. /**
  757. * testLoginRequest method
  758. *
  759. * @access public
  760. * @return void
  761. */
  762. function testLoginRequest() {
  763. $this->Controller->Security->startup($this->Controller);
  764. $realm = 'cakephp.org';
  765. $options = array('realm' => $realm, 'type' => 'basic');
  766. $result = $this->Controller->Security->loginRequest($options);
  767. $expected = 'WWW-Authenticate: Basic realm="'.$realm.'"';
  768. $this->assertEqual($result, $expected);
  769. $this->Controller->Security->startup($this->Controller);
  770. $options = array('realm' => $realm, 'type' => 'digest');
  771. $result = $this->Controller->Security->loginRequest($options);
  772. $this->assertPattern('/realm="'.$realm.'"/', $result);
  773. $this->assertPattern('/qop="auth"/', $result);
  774. }
  775. /**
  776. * testGenerateDigestResponseHash method
  777. *
  778. * @access public
  779. * @return void
  780. */
  781. function testGenerateDigestResponseHash() {
  782. $this->Controller->Security->startup($this->Controller);
  783. $realm = 'cakephp.org';
  784. $loginData = array('realm' => $realm, 'users' => array('Willy Smith' => 'password'));
  785. $this->Controller->Security->requireLogin($loginData);
  786. $data = array(
  787. 'username' => 'Willy Smith',
  788. 'password' => 'password',
  789. 'nonce' => String::uuid(),
  790. 'nc' => 1,
  791. 'cnonce' => 1,
  792. 'realm' => $realm,
  793. 'uri' => 'path_to_identifier',
  794. 'qop' => 'testme'
  795. );
  796. $_SERVER['REQUEST_METHOD'] = 'POST';
  797. $result = $this->Controller->Security->generateDigestResponseHash($data);
  798. $expected = md5(
  799. md5($data['username'] . ':' . $loginData['realm'] . ':' . $data['password']) . ':' .
  800. $data['nonce'] . ':' . $data['nc'] . ':' . $data['cnonce'] . ':' . $data['qop'] . ':' .
  801. md5(env('REQUEST_METHOD') . ':' . $data['uri'])
  802. );
  803. $this->assertIdentical($result, $expected);
  804. }
  805. /**
  806. * testLoginCredentials method
  807. *
  808. * @access public
  809. * @return void
  810. */
  811. function testLoginCredentials() {
  812. $this->Controller->Security->startup($this->Controller);
  813. $_SERVER['PHP_AUTH_USER'] = $user = 'Willy Test';
  814. $_SERVER['PHP_AUTH_PW'] = $pw = 'some password for the nice test';
  815. $result = $this->Controller->Security->loginCredentials('basic');
  816. $expected = array('username' => $user, 'password' => $pw);
  817. $this->assertIdentical($result, $expected);
  818. if (version_compare(PHP_VERSION, '5.1') != -1) {
  819. $_SERVER['PHP_AUTH_DIGEST'] = $digest = <<<DIGEST
  820. Digest username="Mufasa",
  821. realm="testrealm@host.com",
  822. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  823. uri="/dir/index.html",
  824. qop=auth,
  825. nc=00000001,
  826. cnonce="0a4f113b",
  827. response="6629fae49393a05397450978507c4ef1",
  828. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  829. DIGEST;
  830. $expected = array(
  831. 'username' => 'Mufasa',
  832. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  833. 'uri' => '/dir/index.html',
  834. 'qop' => 'auth',
  835. 'nc' => '00000001',
  836. 'cnonce' => '0a4f113b',
  837. 'response' => '6629fae49393a05397450978507c4ef1',
  838. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  839. );
  840. $result = $this->Controller->Security->loginCredentials('digest');
  841. $this->assertIdentical($result, $expected);
  842. }
  843. }
  844. /**
  845. * testParseDigestAuthData method
  846. *
  847. * @access public
  848. * @return void
  849. */
  850. function testParseDigestAuthData() {
  851. $this->Controller->Security->startup($this->Controller);
  852. $digest = <<<DIGEST
  853. Digest username="Mufasa",
  854. realm="testrealm@host.com",
  855. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  856. uri="/dir/index.html",
  857. qop=auth,
  858. nc=00000001,
  859. cnonce="0a4f113b",
  860. response="6629fae49393a05397450978507c4ef1",
  861. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  862. DIGEST;
  863. $expected = array(
  864. 'username' => 'Mufasa',
  865. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  866. 'uri' => '/dir/index.html',
  867. 'qop' => 'auth',
  868. 'nc' => '00000001',
  869. 'cnonce' => '0a4f113b',
  870. 'response' => '6629fae49393a05397450978507c4ef1',
  871. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  872. );
  873. $result = $this->Controller->Security->parseDigestAuthData($digest);
  874. $this->assertIdentical($result, $expected);
  875. $result = $this->Controller->Security->parseDigestAuthData('');
  876. $this->assertNull($result);
  877. }
  878. /**
  879. * testFormDisabledFields method
  880. *
  881. * @access public
  882. * @return void
  883. */
  884. function testFormDisabledFields() {
  885. $this->Controller->Security->startup($this->Controller);
  886. $key = $this->Controller->params['_Token']['key'];
  887. $fields = '11842060341b9d0fc3808b90ba29fdea7054d6ad%3An%3A0%3A%7B%7D';
  888. $this->Controller->data = array(
  889. 'MyModel' => array('name' => 'some data'),
  890. '_Token' => compact('key', 'fields')
  891. );
  892. $result = $this->Controller->Security->validatePost($this->Controller);
  893. $this->assertFalse($result);
  894. $this->Controller->Security->startup($this->Controller);
  895. $this->Controller->Security->disabledFields = array('MyModel.name');
  896. $key = $this->Controller->params['_Token']['key'];
  897. $this->Controller->data = array(
  898. 'MyModel' => array('name' => 'some data'),
  899. '_Token' => compact('key', 'fields')
  900. );
  901. $result = $this->Controller->Security->validatePost($this->Controller);
  902. $this->assertTrue($result);
  903. }
  904. }
  905. ?>