ObjectTest.php 9.1 KB

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