AuthTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. Zend_Session::$_unitTestEnabled = true;
  95. $this->_server->setAuth(new WrongPassword());
  96. $this->_server->setAcl($this->_acl);
  97. $data = $this->_callService();
  98. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  99. $this->assertContains("not allowed", $data->faultString);
  100. }
  101. public function testAnonymousDenied()
  102. {
  103. Zend_Session::$_unitTestEnabled = true;
  104. $this->_server->setAuth(new WrongPassword());
  105. $this->_acl->addRole(new Zend_Acl_Role(Zend_Amf_Constants::GUEST_ROLE));
  106. $this->_server->setAcl($this->_acl);
  107. $resp = $this->_callService();
  108. $this->assertTrue($resp instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  109. $this->assertContains("not allowed", $resp->faultString);
  110. }
  111. public function testAnonymousOK()
  112. {
  113. Zend_Session::$_unitTestEnabled = true;
  114. $this->_server->setAuth(new WrongPassword());
  115. $this->_acl->addRole(new Zend_Acl_Role(Zend_Amf_Constants::GUEST_ROLE));
  116. $this->_acl->allow(Zend_Amf_Constants::GUEST_ROLE, null, null);
  117. $this->_server->setAcl($this->_acl);
  118. $resp = $this->_callService();
  119. $this->assertContains("hello", $resp);
  120. }
  121. public function testNoUsername()
  122. {
  123. $this->_server->setAuth(new WrongPassword());
  124. $this->_server->setAcl($this->_acl);
  125. $resp = $this->_callServiceAuth("", "");
  126. $data = $resp[0]->getData();
  127. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  128. $this->assertContains("username not supplied", $data->faultString);
  129. }
  130. public function testWrongPassword()
  131. {
  132. $this->_server->setAuth(new WrongPassword());
  133. $this->_server->setAcl($this->_acl);
  134. $resp = $this->_callServiceAuth("testuser", "");
  135. $data = $resp[0]->getData();
  136. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  137. $this->assertContains("Wrong Password", $data->faultString);
  138. }
  139. public function testRightPassword()
  140. {
  141. Zend_Session::$_unitTestEnabled = true;
  142. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  143. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  144. $this->_acl->allow("testrole", null, null);
  145. $this->_server->setAcl($this->_acl);
  146. $resp = $this->_callServiceAuth("testuser", "");
  147. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  148. $this->assertContains("hello", $resp[1]->getData());
  149. }
  150. // no ACL to allow access to this method
  151. public function testNoAcl()
  152. {
  153. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  154. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  155. $this->_server->setAcl($this->_acl);
  156. $resp = $this->_callServiceAuth("testuser", "");
  157. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  158. $data = $resp[1]->getData();
  159. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  160. $this->assertContains("not allowed", $data->faultString);
  161. }
  162. // Class allows everybody to access, even though no ACL is defined
  163. public function testNoClassAcl()
  164. {
  165. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  166. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  167. $this->_server->setAcl($this->_acl);
  168. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_NoAcl');
  169. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  170. $this->assertContains("hello", $resp[1]->getData());
  171. }
  172. // Class-defined ACL
  173. public function testClassAclAllowed()
  174. {
  175. Zend_Session::$_unitTestEnabled = true;
  176. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  177. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  178. $this->_acl->addRole(new Zend_Acl_Role("testrole2"));
  179. $this->_server->setAcl($this->_acl);
  180. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_Acl');
  181. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  182. $this->assertContains("hello", $resp[1]->getData());
  183. }
  184. // Class-defined ACL
  185. public function testClassAclDenied()
  186. {
  187. $this->_server->setAuth(new RightPassword("testuser", "testrole2"));
  188. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  189. $this->_acl->addRole(new Zend_Acl_Role("testrole2"));
  190. $this->_server->setAcl($this->_acl);
  191. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_Acl');
  192. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  193. $data = $resp[1]->getData();
  194. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  195. $this->assertContains("not allowed", $data->faultString);
  196. }
  197. // Class-defined ACL
  198. public function testClassAclAllowed2()
  199. {
  200. Zend_Session::$_unitTestEnabled = true;
  201. $this->_server->setAuth(new RightPassword("testuser", "testrole2"));
  202. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  203. $this->_acl->addRole(new Zend_Acl_Role("testrole2"));
  204. $this->_server->setAcl($this->_acl);
  205. $resp = $this->_callServiceAuth("testuser", "", 'Zend_Amf_Auth_testclass_Acl', 'hello2');
  206. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  207. $this->assertContains("hello", $resp[1]->getData());
  208. }
  209. public function testLogout()
  210. {
  211. Zend_Session::$_unitTestEnabled = true;
  212. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  213. $this->_acl->addRole(new Zend_Acl_Role("testrole"));
  214. $this->_acl->allow("testrole", null, null);
  215. $this->_server->setAcl($this->_acl);
  216. $resp = $this->_callServiceAuth("testuser", "");
  217. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  218. $this->assertContains("hello", $resp[1]->getData());
  219. // After logout same request should not be allowed
  220. $this->setUp();
  221. $this->_server->setAuth(new RightPassword("testuser", "testrole"));
  222. $this->_server->setAcl($this->_acl);
  223. $request = new Zend_Amf_Request();
  224. $request->setObjectEncoding(0x03);
  225. $this->_addLogout($request);
  226. $this->_addServiceCall($request);
  227. $this->_server->handle($request);
  228. $resp = $this->_server->getResponse()->getAmfBodies();
  229. $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  230. $data = $resp[1]->getData();
  231. $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  232. $this->assertContains("not allowed", $data->faultString);
  233. }
  234. }
  235. class WrongPassword extends Zend_Amf_Auth_Abstract
  236. {
  237. public function authenticate() {
  238. return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
  239. null,
  240. array('Wrong Password')
  241. );
  242. }
  243. }
  244. class RightPassword extends Zend_Amf_Auth_Abstract
  245. {
  246. public function __construct($name, $role)
  247. {
  248. $this->_name = $name;
  249. $this->_role = $role;
  250. }
  251. public function authenticate()
  252. {
  253. $id = new stdClass();
  254. $id->role = $this->_role;
  255. $id->name = $this->_name;
  256. return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $id);
  257. }
  258. }
  259. class Zend_Amf_Auth_testclass {
  260. function hello() {
  261. return "hello!";
  262. }
  263. }
  264. class Zend_Amf_Auth_testclass_Acl {
  265. function hello() {
  266. return "hello!";
  267. }
  268. function hello2() {
  269. return "hello2!";
  270. }
  271. function initAcl(Zend_Acl $acl) {
  272. $acl->allow("testrole", null, "hello");
  273. $acl->allow("testrole2", null, "hello2");
  274. return true;
  275. }
  276. }
  277. class Zend_Amf_Auth_testclass_NoAcl {
  278. function hello() {
  279. return "hello!";
  280. }
  281. function initAcl() {
  282. return false;
  283. }
  284. }
  285. if (PHPUnit_MAIN_METHOD == "Zend_Amf_AuthTest::main") {
  286. Zend_Amf_AuthTest::main();
  287. }