FileTest.php 10 KB

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