AuthTest.php 12 KB

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