BlobStorageSharedAccessTest.php 9.1 KB

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