DigestTest.php 7.3 KB

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