AuthTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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-2009 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 'PHPUnit/Framework/TestCase.php';
  27. require_once dirname(__FILE__) . '/../../TestHelper.php';
  28. require_once 'Zend/Amf/Server.php';
  29. require_once 'Zend/Amf/Request.php';
  30. require_once 'Zend/Amf/Parse/TypeLoader.php';
  31. require_once 'Zend/Amf/Auth/Abstract.php';
  32. require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
  33. require_once 'Zend/Session.php';
  34. require_once 'Zend/Auth/Result.php';
  35. require_once 'Zend/Acl.php';
  36. require_once 'Zend/Acl/Role.php';
  37. /**
  38. * test case.
  39. */
  40. class Zend_Amf_AuthTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Enter description here...
  44. *
  45. * @var Zend_Amf_Server
  46. */
  47. protected $_server;
  48. public static function main()
  49. {
  50. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_AuthTest");
  51. PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. public function setUp()
  54. {
  55. $this->_server = new Zend_Amf_Server();
  56. $this->_server->setProduction(false);
  57. Zend_Amf_Parse_TypeLoader::resetMap();
  58. $this->_acl = new Zend_Acl();
  59. }
  60. protected function tearDown()
  61. {
  62. unset($this->_server);
  63. }
  64. protected function _addServiceCall($request, $class = 'Zend_Amf_Auth_testclass', $method = 'hello')
  65. {
  66. $data[] = "12345";
  67. $this->_server->setClass($class);
  68. $newBody = new Zend_Amf_Value_MessageBody("$class.$method","/1",$data);
  69. $request->addAmfBody($newBody);
  70. }
  71. protected function _addLogin($request, $username, $password)
  72. {
  73. $cmdBody = new Zend_Amf_Value_MessageBody("","/1","");
  74. $loginCmd = new Zend_Amf_Value_Messaging_CommandMessage();
  75. $cmdBody->setData($loginCmd);
  76. $loginCmd->operation = Zend_Amf_Value_Messaging_CommandMessage::LOGIN_OPERATION;
  77. $loginCmd->body = "$username:$password";
  78. $request->addAmfBody($cmdBody);
  79. }
  80. protected function _addLogout($request)
  81. {
  82. $cmdBody = new Zend_Amf_Value_MessageBody("","/1","");
  83. $loginCmd = new Zend_Amf_Value_Messaging_CommandMessage();
  84. $cmdBody->setData($loginCmd);
  85. $loginCmd->operation = Zend_Amf_Value_Messaging_CommandMessage::LOGOUT_OPERATION;
  86. $request->addAmfBody($cmdBody);
  87. }
  88. protected function _callService($class = 'Zend_Amf_Auth_testclass', $method = 'hello')
  89. {
  90. $request = new Zend_Amf_Request();
  91. $request->setObjectEncoding(0x03);
  92. $this->_addServiceCall($request, $class, $method);
  93. $this->_server->handle($request);
  94. $response = $this->_server->getResponse();
  95. $responseBody = $response->getAmfBodies();
  96. return $responseBody[0]->getData();
  97. }
  98. protected function _callServiceAuth($username, $password, $class = 'Zend_Amf_Auth_testclass', $method = 'hello')
  99. {
  100. $request = new Zend_Amf_Request();
  101. $request->setObjectEncoding(0x03);
  102. $this->_addLogin($request, $username, $password);
  103. $this->_addServiceCall($request, $class, $method);
  104. $this->_server->handle($request);
  105. return $this->_server->getResponse()->getAmfBodies();
  106. }
  107. public function testService()
  108. {
  109. $resp = $this->_callService();
  110. $this->assertContains("hello", $resp);
  111. }
  112. public function testUnauthenticated()
  113. {
  114. Zend_Session::$_unitTestEnabled = true;
  115. $this->_server->setAuth(new WrongPassword());
  116. $this->_server->setAcl($this->_acl);
  117. $data = $this->_callService();
  118. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  119. $this->assertContains("not allowed", $data->faultString);
  120. }
  121. public function testAnonymousDenied()
  122. {
  123. Zend_Session::$_unitTestEnabled = true;
  124. $this->_server->setAuth(new WrongPassword());
  125. $this->_acl->addRole(new Zend_Acl_Role(Zend_Amf_Constants::GUEST_ROLE));
  126. $this->_server->setAcl($this->_acl);
  127. $resp = $this->_callService();
  128. $this->assertTrue($resp instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  129. $this->assertContains("not allowed", $resp->faultString);
  130. }
  131. public function testAnonymousOK()
  132. {
  133. Zend_Session::$_unitTestEnabled = true;
  134. $this->_server->setAuth(new WrongPassword());
  135. $this->_acl->addRole(new Zend_Acl_Role(Zend_Amf_Constants::GUEST_ROLE));
  136. $this->_acl->allow(Zend_Amf_Constants::GUEST_ROLE, null, null);
  137. $this->_server->setAcl($this->_acl);
  138. $resp = $this->_callService();
  139. $this->assertContains("hello", $resp);
  140. }
  141. public function testNoUsername()
  142. {
  143. $this->_server->setAuth(new WrongPassword());
  144. $this->_server->setAcl($this->_acl);
  145. $resp = $this->_callServiceAuth("", "");
  146. $data = $resp[0]->getData();
  147. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  148. $this->assertContains("username not supplied", $data->faultString);
  149. }
  150. public function testWrongPassword()
  151. {
  152. $this->_server->setAuth(new WrongPassword());
  153. $this->_server->setAcl($this->_acl);
  154. $resp = $this->_callServiceAuth("testuser", "");
  155. $data = $resp[0]->getData();
  156. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  157. $this->assertContains("Wrong Password", $data->faultString);
  158. }
  159. public function testRightPassword()
  160. {
  161. Zend_Session::$_unitTestEnabled = true;
  162. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  163. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  164. $this->_acl->allow("testrole", null, null);
  165. $this->_server->setAcl($this->_acl);
  166. $resp = $this->_callServiceAuth("testuser", "");
  167. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  168. $this->assertContains("hello", $resp[1]->getData());
  169. }
  170. // no ACL to allow access to this method
  171. public function testNoAcl()
  172. {
  173. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  174. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  175. $this->_server->setAcl($this->_acl);
  176. $resp = $this->_callServiceAuth("testuser", "");
  177. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  178. $data = $resp[1]->getData();
  179. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  180. $this->assertContains("not allowed", $data->faultString);
  181. }
  182. // Class allows everybody to access, even though no ACL is defined
  183. public function testNoClassAcl()
  184. {
  185. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  186. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  187. $this->_server->setAcl($this->_acl);
  188. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_NoAcl');
  189. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  190. $this->assertContains("hello", $resp[1]->getData());
  191. }
  192. // Class-defined ACL
  193. public function testClassAclAllowed()
  194. {
  195. Zend_Session::$_unitTestEnabled = true;
  196. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  197. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  198. $this->_acl->addRole(new Zend_Acl_Role("testrole2"));
  199. $this->_server->setAcl($this->_acl);
  200. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_Acl');
  201. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  202. $this->assertContains("hello", $resp[1]->getData());
  203. }
  204. // Class-defined ACL
  205. public function testClassAclDenied()
  206. {
  207. $this->_server->setAuth(new RightPassword("testuser", "testrole2"));
  208. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  209. $this->_acl->addRole(new Zend_Acl_Role("testrole2"));
  210. $this->_server->setAcl($this->_acl);
  211. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_Acl');
  212. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  213. $data = $resp[1]->getData();
  214. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  215. $this->assertContains("not allowed", $data->faultString);
  216. }
  217. // Class-defined ACL
  218. public function testClassAclAllowed2()
  219. {
  220. Zend_Session::$_unitTestEnabled = true;
  221. $this->_server->setAuth(new RightPassword("testuser", "testrole2"));
  222. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  223. $this->_acl->addRole(new Zend_Acl_Role("testrole2"));
  224. $this->_server->setAcl($this->_acl);
  225. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_Acl', 'hello2');
  226. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  227. $this->assertContains("hello", $resp[1]->getData());
  228. }
  229. public function testLogout()
  230. {
  231. Zend_Session::$_unitTestEnabled = true;
  232. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  233. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  234. $this->_acl->allow("testrole", null, null);
  235. $this->_server->setAcl($this->_acl);
  236. $resp = $this->_callServiceAuth("testuser", "");
  237. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  238. $this->assertContains("hello", $resp[1]->getData());
  239. // After logout same request should not be allowed
  240. $this->setUp();
  241. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  242. $this->_server->setAcl($this->_acl);
  243. $request = new Zend_Amf_Request();
  244. $request->setObjectEncoding(0x03);
  245. $this->_addLogout($request);
  246. $this->_addServiceCall($request);
  247. $this->_server->handle($request);
  248. $resp = $this->_server->getResponse()->getAmfBodies();
  249. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  250. $data = $resp[1]->getData();
  251. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  252. $this->assertContains("not allowed", $data->faultString);
  253. }
  254. }
  255. class WrongPassword extends Zend_Amf_Auth_Abstract
  256. {
  257. public function authenticate() {
  258. return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
  259. null,
  260. array('Wrong Password')
  261. );
  262. }
  263. }
  264. class RightPassword extends Zend_Amf_Auth_Abstract
  265. {
  266. public function __construct($name, $role)
  267. {
  268. $this->_name = $name;
  269. $this->_role = $role;
  270. }
  271. public function authenticate()
  272. {
  273. $id = new stdClass();
  274. $id->role = $this->_role;
  275. $id->name = $this->_name;
  276. return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $id);
  277. }
  278. }
  279. class Zend_Amf_Auth_testclass {
  280. function hello() {
  281. return "hello!";
  282. }
  283. }
  284. class Zend_Amf_Auth_testclass_Acl {
  285. function hello() {
  286. return "hello!";
  287. }
  288. function hello2() {
  289. return "hello2!";
  290. }
  291. function initAcl(Zend_Acl $acl) {
  292. $acl->allow("testrole", null, "hello");
  293. $acl->allow("testrole2", null, "hello2");
  294. return true;
  295. }
  296. }
  297. class Zend_Amf_Auth_testclass_NoAcl {
  298. function hello() {
  299. return "hello!";
  300. }
  301. function initAcl() {
  302. return false;
  303. }
  304. }
  305. if (PHPUnit_MAIN_METHOD == "Zend_Amf_AuthTest::main") {
  306. Zend_Amf_AuthTest::main();
  307. }