BlobStorageSharedAccessTest.php 8.0 KB

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