FileTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_Resolver_File
  27. */
  28. require_once 'Zend/Auth/Adapter/Http/Resolver/File.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Auth
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2012 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_Http_Resolver_FileTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Path to test files
  41. *
  42. * @var string
  43. */
  44. protected $_filesPath;
  45. /**
  46. * Path to a valid file
  47. *
  48. * @var string
  49. */
  50. protected $_validPath;
  51. /**
  52. * Invalid path; does not exist
  53. *
  54. * @var string
  55. */
  56. protected $_badPath;
  57. /**
  58. * Resolver instance
  59. *
  60. * @var Zend_Auth_Adapter_Http_Resolver_File
  61. */
  62. protected $_resolver;
  63. /**
  64. * Sets the paths to files used in this test, and creates a shared resolver instance
  65. * having a valid path.
  66. *
  67. * @return void
  68. */
  69. public function __construct()
  70. {
  71. $this->_filesPath = dirname(dirname(__FILE__)) . '/_files';
  72. $this->_validPath = "$this->_filesPath/htdigest.3";
  73. $this->_badPath = 'doesnotexist';
  74. $this->_resolver = new Zend_Auth_Adapter_Http_Resolver_File($this->_validPath);
  75. }
  76. /**
  77. * Ensures that setFile() works as expected for valid input
  78. *
  79. * @return void
  80. */
  81. public function testSetFileValid()
  82. {
  83. try {
  84. $this->_resolver->setFile($this->_validPath);
  85. } catch (Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  86. $this->fail('Threw exception on valid file path');
  87. }
  88. $this->assertEquals($this->_validPath, $this->_resolver->getFile());
  89. }
  90. /**
  91. * Ensures that setFile() works as expected for invalid input
  92. *
  93. * @return void
  94. */
  95. public function testSetFileInvalid()
  96. {
  97. try {
  98. $this->_resolver->setFile($this->_badPath);
  99. $this->fail('Accepted bad path');
  100. } catch (Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  101. $this->assertContains('Path not readable', $e->getMessage());
  102. }
  103. }
  104. /**
  105. * Ensures that __construct() works as expected for valid input
  106. *
  107. * @return void
  108. */
  109. public function testConstructValid()
  110. {
  111. try {
  112. $v = new Zend_Auth_Adapter_Http_Resolver_File($this->_validPath);
  113. $this->assertEquals($this->_validPath, $v->getFile());
  114. } catch (Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  115. $this->fail('Constructor threw exception on valid file path');
  116. }
  117. }
  118. /**
  119. * Ensures that __construct() works as expected for invalid input
  120. *
  121. * @return void
  122. */
  123. public function testConstructInvalid()
  124. {
  125. try {
  126. $v = new Zend_Auth_Adapter_Http_Resolver_File($this->_badPath);
  127. $this->fail('Constructor accepted bad path');
  128. } catch(Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  129. $this->assertContains('Path not readable', $e->getMessage());
  130. }
  131. }
  132. /**
  133. * Ensures that resolve() works as expected for empty username
  134. *
  135. * @return void
  136. */
  137. public function testResolveUsernameEmpty()
  138. {
  139. try {
  140. $this->_resolver->resolve('', '');
  141. $this->fail('Accepted empty username');
  142. } catch (Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  143. $this->assertEquals('Username is required', $e->getMessage());
  144. }
  145. }
  146. /**
  147. * Ensures that resolve() works as expected for empty realm
  148. *
  149. * @return void
  150. */
  151. public function testResolveRealmEmpty()
  152. {
  153. try {
  154. $this->_resolver->resolve('username', '');
  155. $this->fail('Accepted empty realm');
  156. } catch (Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  157. $this->assertEquals('Realm is required', $e->getMessage());
  158. }
  159. }
  160. /**
  161. * Ensures that resolve() works as expected for invalid username
  162. *
  163. * @return void
  164. */
  165. public function testResolveUsernameInvalid()
  166. {
  167. try {
  168. $this->_resolver->resolve('bad:name', 'realm');
  169. $this->fail('Accepted malformed username with colon');
  170. } catch (Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  171. $this->assertContains('Username must consist', $e->getMessage());
  172. }
  173. try {
  174. $this->_resolver->resolve("badname\n", 'realm');
  175. $this->fail('Accepted malformed username with newline');
  176. } catch (Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  177. $this->assertContains('Username must consist', $e->getMessage());
  178. }
  179. }
  180. /**
  181. * Ensures that resolve() works as expected for invalid realm
  182. *
  183. * @return void
  184. */
  185. public function testResolveRealmInvalid()
  186. {
  187. try {
  188. $this->_resolver->resolve('username', 'bad:realm');
  189. $this->fail('Accepted malformed realm with colon');
  190. } catch (Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  191. $this->assertContains('Realm must consist', $e->getMessage());
  192. }
  193. try {
  194. $this->_resolver->resolve('username', "badrealm\n");
  195. $this->fail('Accepted malformed realm with newline');
  196. } catch (Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  197. $this->assertContains('Realm must consist', $e->getMessage());
  198. }
  199. }
  200. /**
  201. * Ensures that resolve() works as expected when a previously readable file becomes unreadable
  202. *
  203. * @return void
  204. */
  205. public function testResolveFileDisappearsMystery()
  206. {
  207. if (rename("$this->_filesPath/htdigest.3", "$this->_filesPath/htdigest.3.renamed")) {
  208. try {
  209. $this->_resolver->resolve('username', 'realm');
  210. $this->fail('Expected thrown exception upon resolve() after moving valid file');
  211. } catch (Zend_Auth_Adapter_Http_Resolver_Exception $e) {
  212. $this->assertContains('Unable to open password file', $e->getMessage());
  213. }
  214. rename("$this->_filesPath/htdigest.3.renamed", "$this->_filesPath/htdigest.3");
  215. }
  216. }
  217. /**
  218. * Ensures that resolve() works as expected when provided valid credentials
  219. *
  220. * @return void
  221. */
  222. public function testResolveValid()
  223. {
  224. $this->assertEquals(
  225. $this->_resolver->resolve('Bryce', 'Test Realm'),
  226. 'd5b7c330d5685beb782a9e22f0f20579',
  227. 'Rejected valid credentials'
  228. );
  229. }
  230. /**
  231. * Ensures that resolve() works as expected when provided nonexistent realm
  232. *
  233. * @return void
  234. */
  235. public function testResolveRealmNonexistent()
  236. {
  237. $this->assertFalse(
  238. $this->_resolver->resolve('Bryce', 'nonexistent'),
  239. 'Accepted a valid user in the wrong realm'
  240. );
  241. }
  242. /**
  243. * Ensures that resolve() works as expected when provided nonexistent user
  244. *
  245. * @return void
  246. */
  247. public function testResolveUserNonexistent()
  248. {
  249. $this->assertFalse(
  250. $this->_resolver->resolve('nonexistent', 'Test Realm'),
  251. 'Accepted a nonexistent user from an existing realm'
  252. );
  253. }
  254. }