2
0

ObjectTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_Auth
  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. /**
  23. * PHPUnit_Framework_TestCase
  24. */
  25. require_once 'PHPUnit/Framework/TestCase.php';
  26. /**
  27. * @see Zend_Auth_Adapter_Http
  28. */
  29. require_once 'Zend/Auth/Adapter/Http.php';
  30. /**
  31. * @see Zend_Auth_Adapter_Http_Resolver_File
  32. */
  33. require_once 'Zend/Auth/Adapter/Http/Resolver/File.php';
  34. /**
  35. * @see Zend_Controller_Request_Http
  36. */
  37. require_once 'Zend/Controller/Request/Http.php';
  38. /**
  39. * @see Zend_Controller_Response_Http
  40. */
  41. require_once 'Zend/Controller/Response/Http.php';
  42. /**
  43. * @see Zend_Debug
  44. */
  45. require_once 'Zend/Debug.php';
  46. /**
  47. * @category Zend
  48. * @package Zend_Auth
  49. * @subpackage UnitTests
  50. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  51. * @license http://framework.zend.com/license/new-bsd New BSD License
  52. * @group Zend_Auth
  53. */
  54. class Zend_Auth_Adapter_Http_ObjectTest extends PHPUnit_Framework_TestCase
  55. {
  56. /**
  57. * Path to test files
  58. *
  59. * @var string
  60. */
  61. protected $_filesPath;
  62. /**
  63. * HTTP Basic configuration
  64. *
  65. * @var array
  66. */
  67. protected $_basicConfig;
  68. /**
  69. * HTTP Digest configuration
  70. *
  71. * @var array
  72. */
  73. protected $_digestConfig;
  74. /**
  75. * HTTP Basic Digest configuration
  76. *
  77. * @var array
  78. */
  79. protected $_bothConfig;
  80. /**
  81. * File resolver setup against with HTTP Basic auth file
  82. *
  83. * @var Zend_Auth_Adapter_Http_Resolver_File
  84. */
  85. protected $_basicResolver;
  86. /**
  87. * File resolver setup against with HTTP Digest auth file
  88. *
  89. * @var Zend_Auth_Adapter_Http_Resolver_File
  90. */
  91. protected $_digestResolver;
  92. /**
  93. * Sets up test configuration
  94. *
  95. * @return void
  96. */
  97. public function __construct()
  98. {
  99. $this->_filesPath = dirname(__FILE__) . '/_files';
  100. $this->_basicResolver = new Zend_Auth_Adapter_Http_Resolver_File("$this->_filesPath/htbasic.1");
  101. $this->_digestResolver = new Zend_Auth_Adapter_Http_Resolver_File("$this->_filesPath/htdigest.3");
  102. $this->_basicConfig = array(
  103. 'accept_schemes' => 'basic',
  104. 'realm' => 'Test Realm'
  105. );
  106. $this->_digestConfig = array(
  107. 'accept_schemes' => 'digest',
  108. 'realm' => 'Test Realm',
  109. 'digest_domains' => '/ http://localhost/',
  110. 'nonce_timeout' => 300
  111. );
  112. $this->_bothConfig = array(
  113. 'accept_schemes' => 'basic digest',
  114. 'realm' => 'Test Realm',
  115. 'digest_domains' => '/ http://localhost/',
  116. 'nonce_timeout' => 300
  117. );
  118. }
  119. public function testValidConfigs()
  120. {
  121. try {
  122. $t = new Zend_Auth_Adapter_Http($this->_basicConfig);
  123. } catch (Zend_Auth_Adapter_Exception $e) {
  124. $this->fail('Valid config deemed invalid');
  125. }
  126. $this->assertFalse(empty($t));
  127. $this->assertType('Zend_Auth_Adapter_Http', $t);
  128. unset($t);
  129. try {
  130. $t = new Zend_Auth_Adapter_Http($this->_digestConfig);
  131. } catch (Zend_Auth_Adapter_Exception $e) {
  132. $this->fail('Valid config deemed invalid');
  133. }
  134. $this->assertFalse(empty($t));
  135. $this->assertType('Zend_Auth_Adapter_Http', $t);
  136. unset($t);
  137. try {
  138. $t = new Zend_Auth_Adapter_Http($this->_bothConfig);
  139. } catch (Zend_Auth_Adapter_Exception $e) {
  140. $this->fail('Valid config deemed invalid');
  141. }
  142. $this->assertFalse(empty($t));
  143. $this->assertType('Zend_Auth_Adapter_Http', $t);
  144. unset($t);
  145. }
  146. public function testInvalidConfigs()
  147. {
  148. $badConfigs = array(
  149. 'bad1' => array(
  150. 'auth_type' => 'bogus',
  151. 'realm' => 'Test Realm'
  152. ),
  153. 'bad2' => array(
  154. 'auth_type' => 'digest',
  155. 'realm' => 'Bad: "Chars"'."\n",
  156. 'digest_domains' => '/ /admin',
  157. 'nonce_timeout' => 300
  158. ),
  159. 'bad3' => array(
  160. 'auth_type' => 'digest',
  161. 'realm' => 'Test Realm',
  162. 'digest_domains' => 'no"quotes'."\tor tabs",
  163. 'nonce_timeout' => 300
  164. ),
  165. 'bad4' => array(
  166. 'auth_type' => 'digest',
  167. 'realm' => 'Test Realm',
  168. 'digest_domains' => '/ /admin',
  169. 'nonce_timeout' => 'junk'
  170. )
  171. );
  172. foreach ($badConfigs as $cfg) {
  173. $t = null;
  174. try {
  175. $t = new Zend_Auth_Adapter_Http($cfg);
  176. $this->fail('Accepted an invalid config');
  177. } catch (Zend_Auth_Adapter_Exception $e) {
  178. // Good, it threw an exception
  179. }
  180. }
  181. }
  182. public function testAuthenticateArgs()
  183. {
  184. $a = new Zend_Auth_Adapter_Http($this->_basicConfig);
  185. try {
  186. $a->authenticate();
  187. $this->fail('Attempted authentication without request/response objects');
  188. } catch (Zend_Auth_Adapter_Exception $e) {
  189. // Good, it threw an exception
  190. }
  191. $request = $this->getMock('Zend_Controller_Request_Http');
  192. $response = $this->getMock('Zend_Controller_Response_Http');
  193. // If this throws an exception, it fails
  194. $a->setRequest($request)
  195. ->setResponse($response)
  196. ->authenticate();
  197. }
  198. public function testNoResolvers()
  199. {
  200. $request = $this->getMock('Zend_Controller_Request_Http');
  201. $response = $this->getMock('Zend_Controller_Response_Http');
  202. // Stub request for Basic auth
  203. $request->expects($this->any())
  204. ->method('getHeader')
  205. ->will($this->returnValue('Basic <followed by a space caracter'));
  206. // Once for Basic
  207. try {
  208. $a = new Zend_Auth_Adapter_Http($this->_basicConfig);
  209. $a->setRequest($request)
  210. ->setResponse($response);
  211. $result = $a->authenticate();
  212. $this->fail("Tried Basic authentication without a resolver.\n" . Zend_Debug::dump($result->getMessages(),null,false));
  213. } catch (Zend_Auth_Adapter_Exception $e) {
  214. // Good, it threw an exception
  215. unset($a);
  216. }
  217. // Stub request for Digest auth, must be reseted (recreated)
  218. $request = $this->getMock('Zend_Controller_Request_Http');
  219. $request->expects($this->any())
  220. ->method('getHeader')
  221. ->will($this->returnValue('Digest <followed by a space caracter'));
  222. // Once for Digest
  223. try {
  224. $a = new Zend_Auth_Adapter_Http($this->_digestConfig);
  225. $a->setRequest($request)
  226. ->setResponse($response);
  227. $result = $a->authenticate();
  228. $this->fail("Tried Digest authentication without a resolver.\n" . Zend_Debug::dump($result->getMessages(),null,false));
  229. } catch (Zend_Auth_Adapter_Exception $e) {
  230. // Good, it threw an exception
  231. unset($a);
  232. }
  233. }
  234. public function testWrongResolverUsed()
  235. {
  236. $response = $this->getMock('Zend_Controller_Response_Http');
  237. $request = $this->getMock('Zend_Controller_Request_Http');
  238. $request->expects($this->any())
  239. ->method('getHeader')
  240. ->will($this->returnValue('Basic <followed by a space caracter')); // A basic Header will be provided by that request
  241. // Test a Digest auth process while the request is containing a Basic auth header
  242. $a = new Zend_Auth_Adapter_Http($this->_digestConfig);
  243. $a->setDigestResolver($this->_digestResolver)
  244. ->setRequest($request)
  245. ->setResponse($response);
  246. $result = $a->authenticate();
  247. $this->assertEquals($result->getCode(),Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID);
  248. }
  249. public function testUnsupportedScheme()
  250. {
  251. $response = $this->getMock('Zend_Controller_Response_Http');
  252. $request = $this->getMock('Zend_Controller_Request_Http');
  253. $request->expects($this->any())
  254. ->method('getHeader')
  255. ->will($this->returnValue('NotSupportedScheme <followed by a space caracter'));
  256. $a = new Zend_Auth_Adapter_Http($this->_digestConfig);
  257. $a->setDigestResolver($this->_digestResolver)
  258. ->setRequest($request)
  259. ->setResponse($response);
  260. $result = $a->authenticate();
  261. $this->assertEquals($result->getCode(),Zend_Auth_Result::FAILURE_UNCATEGORIZED);
  262. }
  263. }