BlobStorageSharedAccessTest.php 9.1 KB

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