2
0

FileTest.php 8.0 KB

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