FileTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_OpenId
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Test helper
  23. */
  24. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  25. /**
  26. * @see Zend_OpenId_Consumer_Storage_File
  27. */
  28. require_once 'Zend/OpenId/Consumer/Storage/File.php';
  29. /**
  30. * @package Zend_OpenId
  31. * @subpackage UnitTests
  32. */
  33. class Zend_OpenId_Consumer_Storage_FileTest extends PHPUnit_Framework_TestCase
  34. {
  35. const URL = "http://www.myopenid.com/";
  36. const HANDLE = "d41d8cd98f00b204e9800998ecf8427e";
  37. const MAC_FUNC = "sha256";
  38. const SECRET = "4fa03202081808bd19f92b667a291873";
  39. const ID = "http://id.myopenid.com/";
  40. const REAL_ID = "http://real_id.myopenid.com/";
  41. const SERVER = "http://www.myopenid.com/";
  42. const SERVER2 = "http://www.myopenid2.com/";
  43. const VERSION = 1.0;
  44. protected $_tmpDir;
  45. /**
  46. * Remove directory recursively
  47. *
  48. * @param string $dir
  49. */
  50. private static function _rmDir($dirName)
  51. {
  52. if (!file_exists($dirName)) {
  53. return;
  54. }
  55. // remove files from temporary direcytory
  56. $dir = opendir($dirName);
  57. while (($file = readdir($dir)) !== false) {
  58. if (is_dir($dirName . '/' . $file)) {
  59. if ($file == '.' || $file == '..') {
  60. continue;
  61. }
  62. self::_rmDir($dirName . '/' . $file);
  63. } else {
  64. @unlink($dirName . '/' . $file);
  65. }
  66. }
  67. closedir($dir);
  68. @rmdir($dirName);
  69. }
  70. public function setUp()
  71. {
  72. $this->_tmpDir = dirname(__FILE__) . "/_files";
  73. // Clear directory
  74. self::_rmDir($this->_tmpDir);
  75. mkdir($this->_tmpDir);
  76. }
  77. public function tearDown()
  78. {
  79. self::_rmDir($this->_tmpDir);
  80. }
  81. /**
  82. * testing __construct
  83. *
  84. */
  85. public function testConstruct()
  86. {
  87. $tmp = $this->_tmpDir;
  88. $dir = $tmp . '/openid_consumer';
  89. $storage = new Zend_OpenId_Consumer_Storage_File($dir);
  90. $this->assertTrue( is_dir($dir) );
  91. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  92. return;
  93. }
  94. chmod($dir, 0400);
  95. $dir2 = $dir . '/test';
  96. try {
  97. $storage = new Zend_OpenId_Consumer_Storage_File($dir2);
  98. $ex = null;
  99. } catch (Exception $e) {
  100. $ex = $e;
  101. }
  102. $this->assertTrue( $ex instanceof Zend_OpenId_Exception );
  103. $this->assertSame( Zend_OpenId_Exception::ERROR_STORAGE, $ex->getCode() );
  104. $this->assertContains( 'Cannot access storage directory', $ex->getMessage() );
  105. chmod($dir, 0777);
  106. $this->assertFalse( is_dir($dir2) );
  107. self::_rmDir($dir);
  108. }
  109. /**
  110. * testing getAssociation
  111. *
  112. */
  113. public function testGetAssociation()
  114. {
  115. $tmp = $this->_tmpDir;
  116. $dir = $tmp . '/openid_consumer';
  117. $expiresIn = time() + 600;
  118. $storage = new Zend_OpenId_Consumer_Storage_File($tmp);
  119. $storage->delAssociation(self::URL);
  120. $this->assertTrue( $storage->addAssociation(self::URL, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  121. $this->assertTrue( $storage->getAssociation(self::URL, $handle, $macFunc, $secret, $expires) );
  122. $this->assertSame( self::HANDLE, $handle );
  123. $this->assertSame( self::MAC_FUNC, $macFunc );
  124. $this->assertSame( self::SECRET, $secret );
  125. $this->assertSame( $expiresIn, $expires );
  126. $this->assertTrue( $storage->delAssociation(self::URL) );
  127. $this->assertFalse( $storage->getAssociation(self::URL, $handle, $macFunc, $secret, $expires) );
  128. $storage = new Zend_OpenId_Consumer_Storage_File($dir);
  129. $this->assertTrue( is_dir($dir) );
  130. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  131. return;
  132. }
  133. chmod($dir, 0);
  134. $this->assertFalse( $storage->addAssociation(self::URL, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  135. chmod($dir, 0777);
  136. }
  137. /**
  138. * testing getAssociationByHandle
  139. *
  140. */
  141. public function testGetAssociationByHandle()
  142. {
  143. $tmp = $this->_tmpDir;
  144. $dir = $tmp . '/openid_consumer';
  145. $expiresIn = time() + 600;
  146. $storage = new Zend_OpenId_Consumer_Storage_File($tmp);
  147. $storage->delAssociation(self::URL);
  148. $this->assertTrue( $storage->addAssociation(self::URL, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  149. $this->assertTrue( $storage->getAssociationByHandle(self::HANDLE, $url, $macFunc, $secret, $expires) );
  150. $this->assertSame( self::URL, $url );
  151. $this->assertSame( self::MAC_FUNC, $macFunc );
  152. $this->assertSame( self::SECRET, $secret );
  153. $this->assertSame( $expiresIn, $expires );
  154. $this->assertTrue( $storage->delAssociation(self::URL) );
  155. $this->assertFalse( $storage->getAssociationByHandle(self::HANDLE, $url, $macFunc, $secret, $expires) );
  156. }
  157. /**
  158. * testing getAssociation
  159. *
  160. */
  161. public function testGetAssociationExpiratin()
  162. {
  163. $tmp = $this->_tmpDir;
  164. $dir = $tmp . '/openid_consumer';
  165. $expiresIn = time() + 1;
  166. $storage = new Zend_OpenId_Consumer_Storage_File($tmp);
  167. $storage->delAssociation(self::URL);
  168. $this->assertTrue( $storage->addAssociation(self::URL, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  169. sleep(2);
  170. $this->assertFalse( $storage->getAssociation(self::URL, $handle, $macFunc, $secret, $expires) );
  171. }
  172. /**
  173. * testing getAssociationByHandle
  174. *
  175. */
  176. public function testGetAssociationByHandleExpiration()
  177. {
  178. $tmp = $this->_tmpDir;
  179. $dir = $tmp . '/openid_consumer';
  180. $expiresIn = time() + 1;
  181. $storage = new Zend_OpenId_Consumer_Storage_File($tmp);
  182. $storage->delAssociation(self::URL);
  183. $this->assertTrue( $storage->addAssociation(self::URL, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  184. sleep(2);
  185. $this->assertFalse( $storage->getAssociationByHandle(self::HANDLE, $url, $macFunc, $secret, $expires) );
  186. }
  187. /**
  188. * testing getDiscoveryInfo
  189. *
  190. */
  191. public function testGetDiscoveryInfo()
  192. {
  193. $tmp = $this->_tmpDir;
  194. $dir = $tmp . '/openid_consumer';
  195. $expiresIn = time() + 600;
  196. $storage = new Zend_OpenId_Consumer_Storage_File($tmp);
  197. $storage->delDiscoveryInfo(self::ID);
  198. $this->assertTrue( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, self::VERSION, $expiresIn) );
  199. $this->assertTrue( $storage->getDiscoveryInfo(self::ID, $realId, $server, $version, $expires) );
  200. $this->assertSame( self::REAL_ID, $realId );
  201. $this->assertSame( self::SERVER, $server );
  202. $this->assertSame( self::VERSION, $version );
  203. $this->assertSame( $expiresIn, $expires );
  204. $this->assertTrue( $storage->delDiscoveryInfo(self::ID) );
  205. $this->assertFalse( $storage->getDiscoveryInfo(self::ID, $realId, $server, $version, $expires) );
  206. self::_rmDir($dir);
  207. $storage = new Zend_OpenId_Consumer_Storage_File($dir);
  208. $this->assertTrue( is_dir($dir) );
  209. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  210. return;
  211. }
  212. chmod($dir, 0);
  213. $this->assertFalse( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, self::VERSION, $expiresIn) );
  214. chmod($dir, 0777);
  215. @rmdir($dir);
  216. }
  217. /**
  218. * testing getDiscoveryInfo
  219. *
  220. */
  221. public function testGetDiscoveryInfoExpiration()
  222. {
  223. $tmp = $this->_tmpDir;
  224. $dir = $tmp . '/openid_consumer';
  225. $expiresIn = time() + 1;
  226. $storage = new Zend_OpenId_Consumer_Storage_File($tmp);
  227. $storage->delDiscoveryInfo(self::ID);
  228. $this->assertTrue( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, self::VERSION, $expiresIn) );
  229. sleep(2);
  230. $this->assertFalse( $storage->getDiscoveryInfo(self::ID, $realId, $server, $version, $expires) );
  231. }
  232. /**
  233. * testing isUniqueNonce
  234. *
  235. */
  236. public function testIsUniqueNonce()
  237. {
  238. $tmp = $this->_tmpDir;
  239. $dir = $tmp . '/openid_consumer';
  240. $storage = new Zend_OpenId_Consumer_Storage_File($tmp);
  241. $storage->purgeNonces();
  242. $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') );
  243. $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '2') );
  244. $this->assertFalse( $storage->isUniqueNonce(self::SERVER, '1') );
  245. $this->assertFalse( $storage->isUniqueNonce(self::SERVER, '2') );
  246. $storage->purgeNonces();
  247. $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') );
  248. sleep(2);
  249. $date = @date("r", time());
  250. sleep(2);
  251. $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '2') );
  252. $storage->purgeNonces($date);
  253. $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') );
  254. $this->assertFalse( $storage->isUniqueNonce(self::SERVER, '2') );
  255. $storage->purgeNonces();
  256. $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') );
  257. sleep(2);
  258. $date = time();
  259. sleep(2);
  260. $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '2') );
  261. $storage->purgeNonces($date);
  262. $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') );
  263. $this->assertFalse( $storage->isUniqueNonce(self::SERVER, '2') );
  264. $storage->purgeNonces();
  265. $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') );
  266. $this->assertTrue( $storage->isUniqueNonce(self::SERVER2, '1') );
  267. $storage->purgeNonces();
  268. }
  269. }