_tmpDir = dirname(__FILE__) . "/_files"; // Clear directory self::_rmDir($this->_tmpDir); mkdir($this->_tmpDir); } public function tearDown() { self::_rmDir($this->_tmpDir); } /** * testing __construct * */ public function testConstruct() { $tmp = $this->_tmpDir; $dir = $tmp . '/openid_consumer'; $storage = new Zend_OpenId_Consumer_Storage_File($dir); $this->assertTrue( is_dir($dir) ); if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { return; } chmod($dir, 0400); $dir2 = $dir . '/test'; try { $storage = new Zend_OpenId_Consumer_Storage_File($dir2); $ex = null; } catch (Exception $e) { $ex = $e; } $this->assertTrue( $ex instanceof Zend_OpenId_Exception ); $this->assertSame( Zend_OpenId_Exception::ERROR_STORAGE, $ex->getCode() ); $this->assertContains( 'Cannot access storage directory', $ex->getMessage() ); chmod($dir, 0777); $this->assertFalse( is_dir($dir2) ); self::_rmDir($dir); } /** * testing getAssociation * */ public function testGetAssociation() { $tmp = $this->_tmpDir; $dir = $tmp . '/openid_consumer'; $expiresIn = time() + 600; $storage = new Zend_OpenId_Consumer_Storage_File($tmp); $storage->delAssociation(self::URL); $this->assertTrue( $storage->addAssociation(self::URL, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) ); $this->assertTrue( $storage->getAssociation(self::URL, $handle, $macFunc, $secret, $expires) ); $this->assertSame( self::HANDLE, $handle ); $this->assertSame( self::MAC_FUNC, $macFunc ); $this->assertSame( self::SECRET, $secret ); $this->assertSame( $expiresIn, $expires ); $this->assertTrue( $storage->delAssociation(self::URL) ); $this->assertFalse( $storage->getAssociation(self::URL, $handle, $macFunc, $secret, $expires) ); $storage = new Zend_OpenId_Consumer_Storage_File($dir); $this->assertTrue( is_dir($dir) ); if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { return; } chmod($dir, 0); $this->assertFalse( $storage->addAssociation(self::URL, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) ); chmod($dir, 0777); } /** * testing getAssociationByHandle * */ public function testGetAssociationByHandle() { $tmp = $this->_tmpDir; $dir = $tmp . '/openid_consumer'; $expiresIn = time() + 600; $storage = new Zend_OpenId_Consumer_Storage_File($tmp); $storage->delAssociation(self::URL); $this->assertTrue( $storage->addAssociation(self::URL, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) ); $this->assertTrue( $storage->getAssociationByHandle(self::HANDLE, $url, $macFunc, $secret, $expires) ); $this->assertSame( self::URL, $url ); $this->assertSame( self::MAC_FUNC, $macFunc ); $this->assertSame( self::SECRET, $secret ); $this->assertSame( $expiresIn, $expires ); $this->assertTrue( $storage->delAssociation(self::URL) ); $this->assertFalse( $storage->getAssociationByHandle(self::HANDLE, $url, $macFunc, $secret, $expires) ); } /** * testing getAssociation * */ public function testGetAssociationExpiratin() { $tmp = $this->_tmpDir; $dir = $tmp . '/openid_consumer'; $expiresIn = time() + 1; $storage = new Zend_OpenId_Consumer_Storage_File($tmp); $storage->delAssociation(self::URL); $this->assertTrue( $storage->addAssociation(self::URL, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) ); sleep(2); $this->assertFalse( $storage->getAssociation(self::URL, $handle, $macFunc, $secret, $expires) ); } /** * testing getAssociationByHandle * */ public function testGetAssociationByHandleExpiration() { $tmp = $this->_tmpDir; $dir = $tmp . '/openid_consumer'; $expiresIn = time() + 1; $storage = new Zend_OpenId_Consumer_Storage_File($tmp); $storage->delAssociation(self::URL); $this->assertTrue( $storage->addAssociation(self::URL, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) ); sleep(2); $this->assertFalse( $storage->getAssociationByHandle(self::HANDLE, $url, $macFunc, $secret, $expires) ); } /** * testing getDiscoveryInfo * */ public function testGetDiscoveryInfo() { $tmp = $this->_tmpDir; $dir = $tmp . '/openid_consumer'; $expiresIn = time() + 600; $storage = new Zend_OpenId_Consumer_Storage_File($tmp); $storage->delDiscoveryInfo(self::ID); $this->assertTrue( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, self::VERSION, $expiresIn) ); $this->assertTrue( $storage->getDiscoveryInfo(self::ID, $realId, $server, $version, $expires) ); $this->assertSame( self::REAL_ID, $realId ); $this->assertSame( self::SERVER, $server ); $this->assertSame( self::VERSION, $version ); $this->assertSame( $expiresIn, $expires ); $this->assertTrue( $storage->delDiscoveryInfo(self::ID) ); $this->assertFalse( $storage->getDiscoveryInfo(self::ID, $realId, $server, $version, $expires) ); self::_rmDir($dir); $storage = new Zend_OpenId_Consumer_Storage_File($dir); $this->assertTrue( is_dir($dir) ); if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { return; } chmod($dir, 0); $this->assertFalse( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, self::VERSION, $expiresIn) ); chmod($dir, 0777); @rmdir($dir); } /** * testing getDiscoveryInfo * */ public function testGetDiscoveryInfoExpiration() { $tmp = $this->_tmpDir; $dir = $tmp . '/openid_consumer'; $expiresIn = time() + 1; $storage = new Zend_OpenId_Consumer_Storage_File($tmp); $storage->delDiscoveryInfo(self::ID); $this->assertTrue( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, self::VERSION, $expiresIn) ); sleep(2); $this->assertFalse( $storage->getDiscoveryInfo(self::ID, $realId, $server, $version, $expires) ); } /** * testing isUniqueNonce * */ public function testIsUniqueNonce() { $tmp = $this->_tmpDir; $dir = $tmp . '/openid_consumer'; $storage = new Zend_OpenId_Consumer_Storage_File($tmp); $storage->purgeNonces(); $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') ); $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '2') ); $this->assertFalse( $storage->isUniqueNonce(self::SERVER, '1') ); $this->assertFalse( $storage->isUniqueNonce(self::SERVER, '2') ); $storage->purgeNonces(); $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') ); sleep(2); $date = @date("r", time()); sleep(2); $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '2') ); $storage->purgeNonces($date); $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') ); $this->assertFalse( $storage->isUniqueNonce(self::SERVER, '2') ); $storage->purgeNonces(); $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') ); sleep(2); $date = time(); sleep(2); $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '2') ); $storage->purgeNonces($date); $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') ); $this->assertFalse( $storage->isUniqueNonce(self::SERVER, '2') ); $storage->purgeNonces(); $this->assertTrue( $storage->isUniqueNonce(self::SERVER, '1') ); $this->assertTrue( $storage->isUniqueNonce(self::SERVER2, '1') ); $storage->purgeNonces(); } }