AuthTest.php 11 KB

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