/tests/Zend/OpenId/Provider/Storage/FileTest.php

https://github.com/hojincong/zf2 · PHP · 219 lines · 141 code · 17 blank · 61 comment · 2 complexity · 262efab0096099c76409b7b873171791 MD5 · raw file

  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend\OpenId\Provider\Storage\File
  23. */
  24. /**
  25. * @namespace
  26. */
  27. namespace ZendTest\OpenId\Provider\Storage;
  28. use Zend\OpenId,
  29. Zend\OpenId\Provider\Storage;
  30. /**
  31. * @category Zend
  32. * @package Zend_OpenId
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2011 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 FileTest extends \PHPUnit_Framework_TestCase
  39. {
  40. const HANDLE = "d41d8cd98f00b204e9800998ecf8427e";
  41. const MAC_FUNC = "sha256";
  42. const SECRET = "4fa03202081808bd19f92b667a291873";
  43. const USER = "test_user";
  44. const PASSWORD = "01234567890abcdef";
  45. const SITE1 = "http://www.php.net/";
  46. const SITE2 = "http://www.yahoo.com/";
  47. /**
  48. * testing __construct
  49. *
  50. */
  51. public function testConstruct()
  52. {
  53. $tmp = __DIR__."/_files";
  54. $dir = $tmp . '/openid_provider';
  55. @rmdir($dir);
  56. $storage = new Storage\File($dir);
  57. $this->assertTrue( is_dir($dir) );
  58. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  59. return;
  60. }
  61. chmod($dir, 0);
  62. $dir2 = $dir . '/test';
  63. try {
  64. $storage = new Storage\File($dir2);
  65. $ex = null;
  66. } catch (\Exception $e) {
  67. $ex = $e;
  68. }
  69. $this->assertTrue( $ex instanceof \Zend\OpenId\Exception );
  70. $this->assertSame( \Zend\OpenId\Exception::ERROR_STORAGE, $ex->getCode() );
  71. $this->assertContains( 'Cannot access storage directory', $ex->getMessage() );
  72. chmod($dir, 0777);
  73. $this->assertFalse( is_dir($dir2) );
  74. @rmdir($dir);
  75. }
  76. /**
  77. * testing getAssociation
  78. *
  79. */
  80. public function testGetAssociation()
  81. {
  82. $expiresIn = time() + 600;
  83. $storage = new Storage\File(__DIR__."/_files");
  84. $storage->delAssociation(self::HANDLE);
  85. $this->assertTrue( $storage->addAssociation(self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  86. $this->assertTrue( $storage->getAssociation(self::HANDLE, $macFunc, $secret, $expires) );
  87. $this->assertSame( self::MAC_FUNC, $macFunc );
  88. $this->assertSame( self::SECRET, $secret );
  89. $this->assertSame( $expiresIn, $expires );
  90. $this->assertTrue( $storage->delAssociation(self::HANDLE) );
  91. $this->assertFalse( $storage->getAssociation(self::HANDLE, $macFunc, $secret, $expires) );
  92. $tmp = __DIR__."/_files";
  93. $dir = $tmp . '/openid_consumer';
  94. @rmdir($dir);
  95. $storage = new Storage\File($dir);
  96. $this->assertTrue( is_dir($dir) );
  97. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  98. return;
  99. }
  100. chmod($dir, 0);
  101. $this->assertFalse( $storage->addAssociation(self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  102. chmod($dir, 0777);
  103. @rmdir($dir);
  104. }
  105. /**
  106. * testing getAssociation
  107. *
  108. */
  109. public function testGetAssociationExpiratin()
  110. {
  111. $expiresIn = time() + 1;
  112. $storage = new Storage\File(__DIR__."/_files");
  113. $storage->delAssociation(self::HANDLE);
  114. $this->assertTrue( $storage->addAssociation(self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  115. sleep(2);
  116. $this->assertFalse( $storage->getAssociation(self::HANDLE, $macFunc, $secret, $expires) );
  117. }
  118. /**
  119. * testing addUser
  120. *
  121. */
  122. public function testAddUser()
  123. {
  124. $storage = new Storage\File(__DIR__."/_files");
  125. $storage->delUser(self::USER);
  126. $this->assertTrue( $storage->addUser(self::USER, self::PASSWORD) );
  127. $this->assertFalse( $storage->addUser(self::USER, self::PASSWORD) );
  128. $this->assertTrue( $storage->delUser(self::USER) );
  129. $this->assertTrue( $storage->addUser(self::USER, self::PASSWORD) );
  130. $this->assertTrue( $storage->delUser(self::USER) );
  131. }
  132. /**
  133. * testing hasUser
  134. *
  135. */
  136. public function testHasUser()
  137. {
  138. $storage = new Storage\File(__DIR__."/_files");
  139. $storage->delUser(self::USER);
  140. $this->assertTrue( $storage->addUser(self::USER, self::PASSWORD) );
  141. $this->assertTrue( $storage->hasUser(self::USER) );
  142. $this->assertTrue( $storage->delUser(self::USER) );
  143. $this->assertFalse( $storage->hasUser(self::USER) );
  144. }
  145. /**
  146. * testing checkUser
  147. *
  148. */
  149. public function testCheckUser()
  150. {
  151. $storage = new Storage\File(__DIR__."/_files");
  152. $storage->delUser(self::USER);
  153. $this->assertTrue( $storage->addUser(self::USER, self::PASSWORD) );
  154. $this->assertTrue( $storage->checkUser(self::USER, self::PASSWORD) );
  155. $this->assertFalse( $storage->checkUser(self::USER, self::USER) );
  156. $this->assertTrue( $storage->delUser(self::USER) );
  157. $this->assertFalse( $storage->checkUser(self::USER, self::PASSWORD) );
  158. }
  159. /**
  160. * testing addSite
  161. *
  162. */
  163. public function testAddSite()
  164. {
  165. $storage = new Storage\File(__DIR__."/_files");
  166. $storage->delUser(self::USER);
  167. $this->assertTrue( $storage->addUser(self::USER, self::PASSWORD) );
  168. $this->assertTrue( $storage->addSite(self::USER, self::SITE1, true) );
  169. $trusted = $storage->getTrustedSites(self::USER);
  170. $this->assertTrue( is_array($trusted) );
  171. $this->assertSame( 1, count($trusted) );
  172. reset($trusted);
  173. $this->assertSame( self::SITE1, key($trusted) );
  174. $this->assertSame( true, current($trusted) );
  175. $this->assertTrue( $storage->delUser(self::USER) );
  176. $this->assertFalse( $storage->addSite(self::USER, self::SITE1, true) );
  177. $this->assertTrue( $storage->addUser(self::USER, self::PASSWORD) );
  178. $trusted = $storage->getTrustedSites(self::USER);
  179. $this->assertTrue( is_array($trusted) );
  180. $this->assertSame( 0, count($trusted) );
  181. $this->assertTrue( $storage->addSite(self::USER, self::SITE1, self::SITE1) );
  182. $this->assertTrue( $storage->addSite(self::USER, self::SITE2, self::SITE2) );
  183. $this->assertTrue( $storage->addSite(self::USER, self::SITE1, self::USER) );
  184. $trusted = $storage->getTrustedSites(self::USER);
  185. $this->assertTrue( is_array($trusted) );
  186. $this->assertSame( 2, count($trusted) );
  187. $this->assertSame( self::USER, $trusted[self::SITE1] );
  188. $this->assertSame( self::SITE2, $trusted[self::SITE2] );
  189. $this->assertTrue( $storage->addSite(self::USER, self::SITE2, null) );
  190. $trusted = $storage->getTrustedSites(self::USER);
  191. $this->assertTrue( is_array($trusted) );
  192. $this->assertSame( 1, count($trusted) );
  193. $this->assertSame( self::USER, $trusted[self::SITE1] );
  194. $this->assertTrue( $storage->addSite(self::USER, self::SITE1, null) );
  195. $trusted = $storage->getTrustedSites(self::USER);
  196. $this->assertTrue( is_array($trusted) );
  197. $this->assertSame( 0, count($trusted) );
  198. $this->assertTrue( $storage->delUser(self::USER) );
  199. $storage->delUser(self::USER);
  200. $this->assertFalse( $storage->getTrustedSites(self::USER) );
  201. }
  202. }