PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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