DigestTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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-2009 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. * Zend_Auth_Adapter_Digest
  28. */
  29. require_once 'Zend/Auth/Adapter/Digest.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Auth
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Auth
  37. */
  38. class Zend_Auth_Adapter_DigestTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Path to test files
  42. *
  43. * @var string
  44. */
  45. protected $_filesPath;
  46. /**
  47. * Sets the path to test files
  48. *
  49. * @return void
  50. */
  51. public function __construct()
  52. {
  53. $this->_filesPath = dirname(__FILE__) . '/Digest/_files';
  54. }
  55. /**
  56. * Ensures that the adapter throws an exception when authentication is attempted before
  57. * setting a required option
  58. *
  59. * @return void
  60. */
  61. public function testOptionRequiredException()
  62. {
  63. $adapter = new Zend_Auth_Adapter_Digest();
  64. try {
  65. $adapter->authenticate();
  66. $this->fail('Expected Zend_Auth_Adapter_Exception not thrown upon authentication attempt before setting '
  67. . 'a required option');
  68. } catch (Zend_Auth_Adapter_Exception $e) {
  69. $this->assertContains('must be set before authentication', $e->getMessage());
  70. }
  71. }
  72. /**
  73. * Ensures that an exception is thrown upon authenticating against a nonexistent file
  74. *
  75. * @return void
  76. */
  77. public function testFileNonExistentException()
  78. {
  79. $adapter = new Zend_Auth_Adapter_Digest('nonexistent', 'realm', 'username', 'password');
  80. try {
  81. $adapter->authenticate();
  82. $this->fail('Expected Zend_Auth_Adapter_Exception not thrown upon authenticating against nonexistent '
  83. . 'file');
  84. } catch (Zend_Auth_Adapter_Exception $e) {
  85. $this->assertContains('Cannot open', $e->getMessage());
  86. }
  87. }
  88. /**
  89. * Ensures expected behavior upon realm not found for existing user
  90. *
  91. * @return void
  92. */
  93. public function testUserExistsRealmNonexistent()
  94. {
  95. $filename = "$this->_filesPath/.htdigest.1";
  96. $realm = 'Nonexistent Realm';
  97. $username = 'someUser';
  98. $password = 'somePassword';
  99. $adapter = new Zend_Auth_Adapter_Digest($filename, $realm, $username, $password);
  100. $result = $adapter->authenticate();
  101. $this->assertFalse($result->isValid());
  102. $messages = $result->getMessages();
  103. $this->assertEquals(1, count($messages));
  104. $this->assertEquals($result->getCode(), Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND);
  105. $this->assertContains('combination not found', $messages[0]);
  106. $identity = $result->getIdentity();
  107. $this->assertEquals($identity['realm'], $realm);
  108. $this->assertEquals($identity['username'], $username);
  109. }
  110. /**
  111. * Ensures expected behavior upon user not found in existing realm
  112. *
  113. * @return void
  114. */
  115. public function testUserNonexistentRealmExists()
  116. {
  117. $filename = "$this->_filesPath/.htdigest.1";
  118. $realm = 'Some Realm';
  119. $username = 'nonexistentUser';
  120. $password = 'somePassword';
  121. $adapter = new Zend_Auth_Adapter_Digest($filename, $realm, $username, $password);
  122. $result = $adapter->authenticate();
  123. $this->assertFalse($result->isValid());
  124. $this->assertEquals($result->getCode(), Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND);
  125. $messages = $result->getMessages();
  126. $this->assertEquals(1, count($messages));
  127. $this->assertContains('combination not found', $messages[0]);
  128. $identity = $result->getIdentity();
  129. $this->assertEquals($identity['realm'], $realm);
  130. $this->assertEquals($identity['username'], $username);
  131. }
  132. /**
  133. * Ensures expected behavior upon incorrect password
  134. *
  135. * @return void
  136. */
  137. public function testIncorrectPassword()
  138. {
  139. $filename = "$this->_filesPath/.htdigest.1";
  140. $realm = 'Some Realm';
  141. $username = 'someUser';
  142. $password = 'incorrectPassword';
  143. $adapter = new Zend_Auth_Adapter_Digest($filename, $realm, $username, $password);
  144. $result = $adapter->authenticate();
  145. $this->assertFalse($result->isValid());
  146. $this->assertEquals($result->getCode(), Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID);
  147. $messages = $result->getMessages();
  148. $this->assertEquals(1, count($messages));
  149. $this->assertContains('Password incorrect', $messages[0]);
  150. $identity = $result->getIdentity();
  151. $this->assertEquals($identity['realm'], $realm);
  152. $this->assertEquals($identity['username'], $username);
  153. }
  154. /**
  155. * Ensures that successful authentication works as expected
  156. *
  157. * @return void
  158. */
  159. public function testAuthenticationSuccess()
  160. {
  161. $filename = "$this->_filesPath/.htdigest.1";
  162. $realm = 'Some Realm';
  163. $username = 'someUser';
  164. $password = 'somePassword';
  165. $adapter = new Zend_Auth_Adapter_Digest($filename, $realm, $username, $password);
  166. $result = $adapter->authenticate();
  167. $this->assertTrue($result->isValid());
  168. $this->assertEquals($result->getCode(), Zend_Auth_Result::SUCCESS);
  169. $this->assertEquals(array(), $result->getMessages());
  170. $identity = $result->getIdentity();
  171. $this->assertEquals($identity['realm'], $realm);
  172. $this->assertEquals($identity['username'], $username);
  173. }
  174. /**
  175. * Ensures that getFilename() returns expected default value
  176. *
  177. * @return void
  178. */
  179. public function testGetFilename()
  180. {
  181. $adapter = new Zend_Auth_Adapter_Digest();
  182. $this->assertEquals(null, $adapter->getFilename());
  183. }
  184. /**
  185. * Ensures that getRealm() returns expected default value
  186. *
  187. * @return void
  188. */
  189. public function testGetRealm()
  190. {
  191. $adapter = new Zend_Auth_Adapter_Digest();
  192. $this->assertEquals(null, $adapter->getRealm());
  193. }
  194. /**
  195. * Ensures that getUsername() returns expected default value
  196. *
  197. * @return void
  198. */
  199. public function testGetUsername()
  200. {
  201. $adapter = new Zend_Auth_Adapter_Digest();
  202. $this->assertEquals(null, $adapter->getUsername());
  203. }
  204. /**
  205. * Ensures that getPassword() returns expected default value
  206. *
  207. * @return void
  208. */
  209. public function testGetPassword()
  210. {
  211. $adapter = new Zend_Auth_Adapter_Digest();
  212. $this->assertEquals(null, $adapter->getPassword());
  213. }
  214. }