| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Service_WindowsAzure
- * @subpackage UnitTests
- * @version $Id$
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- if (!defined('PHPUnit_MAIN_METHOD')) {
- define('PHPUnit_MAIN_METHOD', 'Zend_Service_WindowsAzure_BlobStorageSharedAccessTest::main');
- }
- /**
- * Test helpers
- */
- require_once dirname(__FILE__) . '/../../../TestHelper.php';
- require_once dirname(__FILE__) . '/../../../TestConfiguration.php.dist';
- /** Zend_Service_WindowsAzure_Storage_Blob */
- require_once 'Zend/Service/WindowsAzure/Storage/Blob.php';
- /** Zend_Service_WindowsAzure_Credentials_SharedAccessSignature */
- require_once 'Zend/Service/WindowsAzure/Credentials/SharedAccessSignature.php';
- /**
- * @category Zend
- * @package Zend_Service_WindowsAzure
- * @subpackage UnitTests
- * @version $Id$
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Service_WindowsAzure_BlobStorageSharedAccessTest extends PHPUnit_Framework_TestCase
- {
- static $path;
-
- public function __construct()
- {
- self::$path = dirname(__FILE__).'/_files/';
- }
-
- public static function main()
- {
- if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
- $suite = new PHPUnit_Framework_TestSuite("Zend_Service_WindowsAzure_BlobStorageSharedAccessTest");
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- }
-
- /**
- * Test setup
- */
- protected function setUp()
- {
- }
-
- /**
- * Test teardown
- */
- protected function tearDown()
- {
- $storageClient = $this->createAdministrativeStorageInstance();
- for ($i = 1; $i <= self::$uniqId; $i++)
- {
- try { $storageClient->deleteContainer(TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOBSA_CONTAINER_PREFIX . $i); } catch (Exception $e) { }
- }
- try { $storageClient->deleteContainer('$root'); } catch (Exception $e) { }
- }
- protected function createStorageInstance()
- {
- $storageClient = null;
- if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNONPROD) {
- $storageClient = new Zend_Service_WindowsAzure_Storage_Blob(TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_HOST_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_PROD, false, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
- $storageClient->setCredentials(
- new Zend_Service_WindowsAzure_Credentials_SharedAccessSignature(TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_PROD, false)
- );
- } else {
- $storageClient = new Zend_Service_WindowsAzure_Storage_Blob(TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_HOST_DEV, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_DEV, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_DEV, true, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
- $storageClient->setCredentials(
- new Zend_Service_WindowsAzure_Credentials_SharedAccessSignature(TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_DEV, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_DEV, true)
- );
- }
-
- if (TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY) {
- $storageClient->setProxy(TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY_PORT, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY_CREDENTIALS);
- }
- return $storageClient;
- }
-
- protected function createAdministrativeStorageInstance()
- {
- $storageClient = null;
- if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNONPROD) {
- $storageClient = new Zend_Service_WindowsAzure_Storage_Blob(TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_HOST_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_PROD, false, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
- } else {
- $storageClient = new Zend_Service_WindowsAzure_Storage_Blob(TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_HOST_DEV, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_DEV, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_DEV, true, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
- }
-
- if (TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY) {
- $storageClient->setProxy(TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY_PORT, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY_CREDENTIALS);
- }
- return $storageClient;
- }
-
- protected static $uniqId = 0;
-
- protected function generateName()
- {
- self::$uniqId++;
- return TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOBSA_CONTAINER_PREFIX . self::$uniqId;
- }
-
- /**
- * Test shared access, only write
- */
- public function testSharedAccess_OnlyWrite()
- {
- if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
- $containerName = $this->generateName();
-
- // Account owner performs this part
- $administrativeStorageClient = $this->createAdministrativeStorageInstance();
- $administrativeStorageClient->createContainer($containerName);
-
- $sharedAccessUrl = $administrativeStorageClient->generateSharedAccessUrl(
- $containerName,
- '',
- 'c',
- 'w',
- $administrativeStorageClient->isoDate(time() - 500),
- $administrativeStorageClient->isoDate(time() + 3000)
- );
-
- // Reduced permissions user performs this part
- $storageClient = $this->createStorageInstance();
- $credentials = $storageClient->getCredentials();
- $credentials->setPermissionSet(array(
- $sharedAccessUrl
- ));
- $result = $storageClient->putBlob($containerName, 'images/WindowsAzure.gif', self::$path . 'WindowsAzure.gif');
-
- $this->assertEquals($containerName, $result->Container);
- $this->assertEquals('images/WindowsAzure.gif', $result->Name);
-
-
-
- // Now make sure reduced permissions user can not view the uploaded blob
- $exceptionThrown = false;
- try {
- $storageClient->getBlob($containerName, 'images/WindowsAzure.gif', self::$path . 'WindowsAzure.gif');
- } catch (Exception $ex) {
- $exceptionThrown = true;
- }
- $this->assertTrue($exceptionThrown);
- }
- }
-
- /**
- * Test different accounts
- */
- public function testDifferentAccounts()
- {
- if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
- $containerName = $this->generateName();
-
- // Account owner performs this part
- $administrativeStorageClient = $this->createAdministrativeStorageInstance();
- $administrativeStorageClient->createContainer($containerName);
-
- $sharedAccessUrl1 = $administrativeStorageClient->generateSharedAccessUrl(
- $containerName,
- '',
- 'c',
- 'w',
- $administrativeStorageClient->isoDate(time() - 500),
- $administrativeStorageClient->isoDate(time() + 3000)
- );
- $sharedAccessUrl2 = str_replace($administrativeStorageClient->getAccountName(), 'bogusaccount', $sharedAccessUrl1);
-
- // Reduced permissions user performs this part and should fail,
- // because different accounts have been used
- $storageClient = $this->createStorageInstance();
- $credentials = $storageClient->getCredentials();
- $exceptionThrown = false;
- try {
- $credentials->setPermissionSet(array(
- $sharedAccessUrl1,
- $sharedAccessUrl2
- ));
- } catch (Exception $ex) {
- $exceptionThrown = true;
- }
- $this->assertTrue($exceptionThrown);
- }
- }
- }
- // Call Zend_Service_WindowsAzure_BlobStorageSharedAccessTest::main() if this source file is executed directly.
- if (PHPUnit_MAIN_METHOD == "Zend_Service_WindowsAzure_BlobStorageSharedAccessTest::main") {
- Zend_Service_WindowsAzure_BlobStorageSharedAccessTest::main();
- }
|