BlobStorageSharedAccessTest.php 9.0 KB

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