PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Amf/AuthTest.php

https://github.com/EvanDotPro/zf1-mirror
PHP | 347 lines | 270 code | 37 blank | 40 comment | 3 complexity | 4b336be5b10b5436c8221399e905544a MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Amf
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Amf_AuthTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Amf_AuthTest::main");
  25. }
  26. require_once 'Zend/Amf/Server.php';
  27. require_once 'Zend/Amf/Request.php';
  28. require_once 'Zend/Amf/Parse/TypeLoader.php';
  29. require_once 'Zend/Amf/Auth/Abstract.php';
  30. require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
  31. require_once 'Zend/Session.php';
  32. require_once 'Zend/Auth/Result.php';
  33. require_once 'Zend/Acl.php';
  34. require_once 'Zend/Acl/Role.php';
  35. /**
  36. * @category Zend
  37. * @package Zend_Amf
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @group Zend_Amf
  42. */
  43. class Zend_Amf_AuthTest extends PHPUnit_Framework_TestCase
  44. {
  45. /**
  46. * Enter description here...
  47. *
  48. * @var Zend_Amf_Server
  49. */
  50. protected $_server;
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_AuthTest");
  54. PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. public function setUp()
  57. {
  58. $this->_server = new Zend_Amf_Server();
  59. $this->_server->setProduction(false);
  60. Zend_Amf_Parse_TypeLoader::resetMap();
  61. $this->_acl = new Zend_Acl();
  62. }
  63. protected function tearDown()
  64. {
  65. unset($this->_server);
  66. }
  67. protected function _addServiceCall($request, $class = 'Zend_Amf_Auth_testclass', $method = 'hello')
  68. {
  69. $data[] = "12345";
  70. $this->_server->setClass($class);
  71. $newBody = new Zend_Amf_Value_MessageBody("$class.$method","/1",$data);
  72. $request->addAmfBody($newBody);
  73. }
  74. protected function _addLogin($request, $username, $password)
  75. {
  76. $cmdBody = new Zend_Amf_Value_MessageBody("","/1","");
  77. $loginCmd = new Zend_Amf_Value_Messaging_CommandMessage();
  78. $cmdBody->setData($loginCmd);
  79. $loginCmd->operation = Zend_Amf_Value_Messaging_CommandMessage::LOGIN_OPERATION;
  80. $loginCmd->body = "$username:$password";
  81. $request->addAmfBody($cmdBody);
  82. }
  83. protected function _addLogout($request)
  84. {
  85. $cmdBody = new Zend_Amf_Value_MessageBody("","/1","");
  86. $loginCmd = new Zend_Amf_Value_Messaging_CommandMessage();
  87. $cmdBody->setData($loginCmd);
  88. $loginCmd->operation = Zend_Amf_Value_Messaging_CommandMessage::LOGOUT_OPERATION;
  89. $request->addAmfBody($cmdBody);
  90. }
  91. protected function _callService($class = 'Zend_Amf_Auth_testclass', $method = 'hello')
  92. {
  93. $request = new Zend_Amf_Request();
  94. $request->setObjectEncoding(0x03);
  95. $this->_addServiceCall($request, $class, $method);
  96. $this->_server->handle($request);
  97. $response = $this->_server->getResponse();
  98. $responseBody = $response->getAmfBodies();
  99. return $responseBody[0]->getData();
  100. }
  101. protected function _callServiceAuth($username, $password, $class = 'Zend_Amf_Auth_testclass', $method = 'hello')
  102. {
  103. $request = new Zend_Amf_Request();
  104. $request->setObjectEncoding(0x03);
  105. $this->_addLogin($request, $username, $password);
  106. $this->_addServiceCall($request, $class, $method);
  107. $this->_server->handle($request);
  108. return $this->_server->getResponse()->getAmfBodies();
  109. }
  110. public function testService()
  111. {
  112. $resp = $this->_callService();
  113. $this->assertContains("hello", $resp);
  114. }
  115. public function testUnauthenticated()
  116. {
  117. Zend_Session::$_unitTestEnabled = true;
  118. $this->_server->setAuth(new WrongPassword());
  119. $this->_server->setAcl($this->_acl);
  120. $data = $this->_callService();
  121. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  122. $this->assertContains("not allowed", $data->faultString);
  123. }
  124. public function testAnonymousDenied()
  125. {
  126. Zend_Session::$_unitTestEnabled = true;
  127. $this->_server->setAuth(new WrongPassword());
  128. $this->_acl->addRole(new Zend_Acl_Role(Zend_Amf_Constants::GUEST_ROLE));
  129. $this->_server->setAcl($this->_acl);
  130. $resp = $this->_callService();
  131. $this->assertTrue($resp instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  132. $this->assertContains("not allowed", $resp->faultString);
  133. }
  134. public function testAnonymousOK()
  135. {
  136. Zend_Session::$_unitTestEnabled = true;
  137. $this->_server->setAuth(new WrongPassword());
  138. $this->_acl->addRole(new Zend_Acl_Role(Zend_Amf_Constants::GUEST_ROLE));
  139. $this->_acl->allow(Zend_Amf_Constants::GUEST_ROLE, null, null);
  140. $this->_server->setAcl($this->_acl);
  141. $resp = $this->_callService();
  142. $this->assertContains("hello", $resp);
  143. }
  144. public function testNoUsername()
  145. {
  146. $this->_server->setAuth(new WrongPassword());
  147. $this->_server->setAcl($this->_acl);
  148. $resp = $this->_callServiceAuth("", "");
  149. $data = $resp[0]->getData();
  150. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  151. $this->assertContains("username not supplied", $data->faultString);
  152. }
  153. public function testWrongPassword()
  154. {
  155. $this->_server->setAuth(new WrongPassword());
  156. $this->_server->setAcl($this->_acl);
  157. $resp = $this->_callServiceAuth("testuser", "");
  158. $data = $resp[0]->getData();
  159. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  160. $this->assertContains("Wrong Password", $data->faultString);
  161. }
  162. public function testRightPassword()
  163. {
  164. Zend_Session::$_unitTestEnabled = true;
  165. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  166. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  167. $this->_acl->allow("testrole", null, null);
  168. $this->_server->setAcl($this->_acl);
  169. $resp = $this->_callServiceAuth("testuser", "");
  170. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  171. $this->assertContains("hello", $resp[1]->getData());
  172. }
  173. // no ACL to allow access to this method
  174. public function testNoAcl()
  175. {
  176. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  177. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  178. $this->_server->setAcl($this->_acl);
  179. $resp = $this->_callServiceAuth("testuser", "");
  180. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  181. $data = $resp[1]->getData();
  182. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  183. $this->assertContains("not allowed", $data->faultString);
  184. }
  185. // Class allows everybody to access, even though no ACL is defined
  186. public function testNoClassAcl()
  187. {
  188. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  189. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  190. $this->_server->setAcl($this->_acl);
  191. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_NoAcl');
  192. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  193. $this->assertContains("hello", $resp[1]->getData());
  194. }
  195. // Class-defined ACL
  196. public function testClassAclAllowed()
  197. {
  198. Zend_Session::$_unitTestEnabled = true;
  199. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  200. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  201. $this->_acl->addRole(new Zend_Acl_Role("testrole2"));
  202. $this->_server->setAcl($this->_acl);
  203. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_Acl');
  204. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  205. $this->assertContains("hello", $resp[1]->getData());
  206. }
  207. // Class-defined ACL
  208. public function testClassAclDenied()
  209. {
  210. $this->_server->setAuth(new RightPassword("testuser", "testrole2"));
  211. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  212. $this->_acl->addRole(new Zend_Acl_Role("testrole2"));
  213. $this->_server->setAcl($this->_acl);
  214. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_Acl');
  215. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  216. $data = $resp[1]->getData();
  217. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  218. $this->assertContains("not allowed", $data->faultString);
  219. }
  220. // Class-defined ACL
  221. public function testClassAclAllowed2()
  222. {
  223. Zend_Session::$_unitTestEnabled = true;
  224. $this->_server->setAuth(new RightPassword("testuser", "testrole2"));
  225. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  226. $this->_acl->addRole(new Zend_Acl_Role("testrole2"));
  227. $this->_server->setAcl($this->_acl);
  228. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_Acl', 'hello2');
  229. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  230. $this->assertContains("hello", $resp[1]->getData());
  231. }
  232. public function testLogout()
  233. {
  234. Zend_Session::$_unitTestEnabled = true;
  235. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  236. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  237. $this->_acl->allow("testrole", null, null);
  238. $this->_server->setAcl($this->_acl);
  239. $resp = $this->_callServiceAuth("testuser", "");
  240. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  241. $this->assertContains("hello", $resp[1]->getData());
  242. // After logout same request should not be allowed
  243. $this->setUp();
  244. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  245. $this->_server->setAcl($this->_acl);
  246. $request = new Zend_Amf_Request();
  247. $request->setObjectEncoding(0x03);
  248. $this->_addLogout($request);
  249. $this->_addServiceCall($request);
  250. $this->_server->handle($request);
  251. $resp = $this->_server->getResponse()->getAmfBodies();
  252. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  253. $data = $resp[1]->getData();
  254. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  255. $this->assertContains("not allowed", $data->faultString);
  256. }
  257. }
  258. class WrongPassword extends Zend_Amf_Auth_Abstract
  259. {
  260. public function authenticate() {
  261. return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
  262. null,
  263. array('Wrong Password')
  264. );
  265. }
  266. }
  267. class RightPassword extends Zend_Amf_Auth_Abstract
  268. {
  269. public function __construct($name, $role)
  270. {
  271. $this->_name = $name;
  272. $this->_role = $role;
  273. }
  274. public function authenticate()
  275. {
  276. $id = new stdClass();
  277. $id->role = $this->_role;
  278. $id->name = $this->_name;
  279. return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $id);
  280. }
  281. }
  282. class Zend_Amf_Auth_testclass {
  283. function hello() {
  284. return "hello!";
  285. }
  286. }
  287. class Zend_Amf_Auth_testclass_Acl {
  288. function hello() {
  289. return "hello!";
  290. }
  291. function hello2() {
  292. return "hello2!";
  293. }
  294. function initAcl(Zend_Acl $acl) {
  295. $acl->allow("testrole", null, "hello");
  296. $acl->allow("testrole2", null, "hello2");
  297. return true;
  298. }
  299. }
  300. class Zend_Amf_Auth_testclass_NoAcl {
  301. function hello() {
  302. return "hello!";
  303. }
  304. function initAcl() {
  305. return false;
  306. }
  307. }
  308. if (PHPUnit_MAIN_METHOD == "Zend_Amf_AuthTest::main") {
  309. Zend_Amf_AuthTest::main();
  310. }