FileTest.php 10 KB

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