AuthTest.php 12 KB

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