/tests/Zend/Service/WindowsAzure/BlobStorageSharedAccessTest.php

https://github.com/WebTricks/WebTricks-CMS · PHP · 208 lines · 124 code · 33 blank · 51 comment · 10 complexity · f2226941a6b08a74f8affef4eb4086af 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_Service_WindowsAzure
  17. * @subpackage UnitTests
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * Test helpers
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /** Zend_Service_WindowsAzure_Storage_Blob */
  27. require_once 'Zend/Service/WindowsAzure/Storage/Blob.php';
  28. /** Zend_Service_WindowsAzure_Credentials_SharedAccessSignature */
  29. require_once 'Zend/Service/WindowsAzure/Credentials/SharedAccessSignature.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service_WindowsAzure
  33. * @subpackage UnitTests
  34. * @version $Id$
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Service_WindowsAzure_BlobStorageSharedAccessTest extends PHPUnit_Framework_TestCase
  39. {
  40. static protected $path;
  41. public function __construct()
  42. {
  43. self::$path = dirname(__FILE__).'/_files/';
  44. }
  45. /**
  46. * Test setup
  47. */
  48. protected function setUp()
  49. {
  50. if (!TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
  51. $this->markTestSkipped('This test case requires TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS to be enabled in TestConfiguration.php');
  52. }
  53. }
  54. /**
  55. * Test teardown
  56. */
  57. protected function tearDown()
  58. {
  59. if ($this->status == PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED) {
  60. return;
  61. }
  62. $storageClient = $this->createAdministrativeStorageInstance();
  63. for ($i = 1; $i <= self::$uniqId; $i++)
  64. {
  65. try { $storageClient->deleteContainer(TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOBSA_CONTAINER_PREFIX . $i); } catch (Exception $e) { }
  66. }
  67. try { $storageClient->deleteContainer('$root'); } catch (Exception $e) { }
  68. }
  69. protected function createStorageInstance()
  70. {
  71. $storageClient = null;
  72. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNONPROD) {
  73. $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));
  74. $storageClient->setCredentials(
  75. new Zend_Service_WindowsAzure_Credentials_SharedAccessSignature(TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_PROD, false)
  76. );
  77. } else {
  78. $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));
  79. $storageClient->setCredentials(
  80. new Zend_Service_WindowsAzure_Credentials_SharedAccessSignature(TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_DEV, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_DEV, true)
  81. );
  82. }
  83. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY) {
  84. $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);
  85. }
  86. return $storageClient;
  87. }
  88. protected function createAdministrativeStorageInstance()
  89. {
  90. $storageClient = null;
  91. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNONPROD) {
  92. $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));
  93. } else {
  94. $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));
  95. }
  96. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY) {
  97. $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);
  98. }
  99. return $storageClient;
  100. }
  101. protected static $uniqId = 0;
  102. protected function generateName()
  103. {
  104. self::$uniqId++;
  105. return TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOBSA_CONTAINER_PREFIX . self::$uniqId;
  106. }
  107. /**
  108. * Test shared access, only write
  109. */
  110. public function testSharedAccess_OnlyWrite()
  111. {
  112. $containerName = $this->generateName();
  113. // Account owner performs this part
  114. $administrativeStorageClient = $this->createAdministrativeStorageInstance();
  115. $administrativeStorageClient->createContainer($containerName);
  116. $sharedAccessUrl = $administrativeStorageClient->generateSharedAccessUrl(
  117. $containerName,
  118. '',
  119. 'c',
  120. 'w',
  121. $administrativeStorageClient->isoDate(time() - 500),
  122. $administrativeStorageClient->isoDate(time() + 3000)
  123. );
  124. // Reduced permissions user performs this part
  125. $storageClient = $this->createStorageInstance();
  126. $credentials = $storageClient->getCredentials();
  127. $credentials->setPermissionSet(array(
  128. $sharedAccessUrl
  129. ));
  130. $result = $storageClient->putBlob($containerName, 'images/WindowsAzure.gif', self::$path . 'WindowsAzure.gif');
  131. $this->assertEquals($containerName, $result->Container);
  132. $this->assertEquals('images/WindowsAzure.gif', $result->Name);
  133. // Now make sure reduced permissions user can not view the uploaded blob
  134. $exceptionThrown = false;
  135. try {
  136. $storageClient->getBlob($containerName, 'images/WindowsAzure.gif', self::$path . 'WindowsAzure.gif');
  137. } catch (Exception $ex) {
  138. $exceptionThrown = true;
  139. }
  140. $this->assertTrue($exceptionThrown);
  141. }
  142. /**
  143. * Test different accounts
  144. */
  145. public function testDifferentAccounts()
  146. {
  147. $containerName = $this->generateName();
  148. // Account owner performs this part
  149. $administrativeStorageClient = $this->createAdministrativeStorageInstance();
  150. $administrativeStorageClient->createContainer($containerName);
  151. $sharedAccessUrl1 = $administrativeStorageClient->generateSharedAccessUrl(
  152. $containerName,
  153. '',
  154. 'c',
  155. 'w',
  156. $administrativeStorageClient->isoDate(time() - 500),
  157. $administrativeStorageClient->isoDate(time() + 3000)
  158. );
  159. $sharedAccessUrl2 = str_replace($administrativeStorageClient->getAccountName(), 'bogusaccount', $sharedAccessUrl1);
  160. // Reduced permissions user performs this part and should fail,
  161. // because different accounts have been used
  162. $storageClient = $this->createStorageInstance();
  163. $credentials = $storageClient->getCredentials();
  164. $exceptionThrown = false;
  165. try {
  166. $credentials->setPermissionSet(array(
  167. $sharedAccessUrl1,
  168. $sharedAccessUrl2
  169. ));
  170. } catch (Exception $ex) {
  171. $exceptionThrown = true;
  172. }
  173. $this->assertTrue($exceptionThrown);
  174. }
  175. }