BlobStorageSharedAccessTest.php 9.1 KB

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