PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v2.0.0/tests/Microsoft/WindowsAzure/BlobStorageSharedAccessTest.php

#
PHP | 236 lines | 137 code | 34 blank | 65 comment | 13 complexity | 3fe364a1019aa7fcb97123506a207c17 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2010, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_WindowsAzure
  30. * @subpackage UnitTests
  31. * @version $Id: BlobStorageSharedAccessTest.php 25258 2009-08-14 08:40:41Z unknown $
  32. * @copyright Copyright (c) 2009 - 2010, RealDolmen (http://www.realdolmen.com)
  33. * @license http://phpazure.codeplex.com/license
  34. */
  35. if (!defined('PHPUnit_MAIN_METHOD')) {
  36. define('PHPUnit_MAIN_METHOD', 'Microsoft_WindowsAzure_BlobStorageSharedAccessTest::main');
  37. }
  38. /**
  39. * Test helpers
  40. */
  41. require_once dirname(__FILE__) . '/../../TestHelper.php';
  42. require_once dirname(__FILE__) . '/../../TestConfiguration.php';
  43. require_once 'PHPUnit/Framework/TestCase.php';
  44. /** Microsoft_WindowsAzure_Storage_Blob */
  45. require_once 'Microsoft/WindowsAzure/Storage/Blob.php';
  46. /** Microsoft_WindowsAzure_Credentials_SharedAccessSignature */
  47. require_once 'Microsoft/WindowsAzure/Credentials/SharedAccessSignature.php';
  48. /**
  49. * @category Microsoft
  50. * @package Microsoft_WindowsAzure
  51. * @subpackage UnitTests
  52. * @version $Id: BlobStorageSharedAccessTest.php 25258 2009-08-14 08:40:41Z unknown $
  53. * @copyright Copyright (c) 2009 - 2010, RealDolmen (http://www.realdolmen.com)
  54. * @license http://phpazure.codeplex.com/license
  55. */
  56. class Microsoft_WindowsAzure_BlobStorageSharedAccessTest extends PHPUnit_Framework_TestCase
  57. {
  58. static $path;
  59. public function __construct()
  60. {
  61. self::$path = dirname(__FILE__).'/_files/';
  62. }
  63. public static function main()
  64. {
  65. if (TESTS_BLOB_RUNTESTS) {
  66. $suite = new PHPUnit_Framework_TestSuite("Microsoft_WindowsAzure_BlobStorageSharedAccessTest");
  67. $result = PHPUnit_TextUI_TestRunner::run($suite);
  68. }
  69. }
  70. /**
  71. * Test setup
  72. */
  73. protected function setUp()
  74. {
  75. }
  76. /**
  77. * Test teardown
  78. */
  79. protected function tearDown()
  80. {
  81. $storageClient = $this->createAdministrativeStorageInstance();
  82. for ($i = 1; $i <= self::$uniqId; $i++)
  83. {
  84. try { $storageClient->deleteContainer(TESTS_BLOBSA_CONTAINER_PREFIX . $i); } catch (Exception $e) { }
  85. }
  86. try { $storageClient->deleteContainer('$root'); } catch (Exception $e) { }
  87. }
  88. protected function createStorageInstance()
  89. {
  90. $storageClient = null;
  91. if (TESTS_BLOB_RUNONPROD) {
  92. $storageClient = new Microsoft_WindowsAzure_Storage_Blob(TESTS_BLOB_HOST_PROD, TESTS_STORAGE_ACCOUNT_PROD, TESTS_STORAGE_KEY_PROD, false, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  93. $storageClient->setCredentials(
  94. new Microsoft_WindowsAzure_Credentials_SharedAccessSignature(TESTS_STORAGE_ACCOUNT_PROD, TESTS_STORAGE_KEY_PROD, false)
  95. );
  96. } else {
  97. $storageClient = new Microsoft_WindowsAzure_Storage_Blob(TESTS_BLOB_HOST_DEV, TESTS_STORAGE_ACCOUNT_DEV, TESTS_STORAGE_KEY_DEV, true, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  98. $storageClient->setCredentials(
  99. new Microsoft_WindowsAzure_Credentials_SharedAccessSignature(TESTS_STORAGE_ACCOUNT_DEV, TESTS_STORAGE_KEY_DEV, true)
  100. );
  101. }
  102. if (TESTS_STORAGE_USEPROXY) {
  103. $storageClient->setProxy(TESTS_STORAGE_USEPROXY, TESTS_STORAGE_PROXY, TESTS_STORAGE_PROXY_PORT, TESTS_STORAGE_PROXY_CREDENTIALS);
  104. }
  105. return $storageClient;
  106. }
  107. protected function createAdministrativeStorageInstance()
  108. {
  109. $storageClient = null;
  110. if (TESTS_BLOB_RUNONPROD) {
  111. $storageClient = new Microsoft_WindowsAzure_Storage_Blob(TESTS_BLOB_HOST_PROD, TESTS_STORAGE_ACCOUNT_PROD, TESTS_STORAGE_KEY_PROD, false, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  112. } else {
  113. $storageClient = new Microsoft_WindowsAzure_Storage_Blob(TESTS_BLOB_HOST_DEV, TESTS_STORAGE_ACCOUNT_DEV, TESTS_STORAGE_KEY_DEV, true, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  114. }
  115. if (TESTS_STORAGE_USEPROXY) {
  116. $storageClient->setProxy(TESTS_STORAGE_USEPROXY, TESTS_STORAGE_PROXY, TESTS_STORAGE_PROXY_PORT, TESTS_STORAGE_PROXY_CREDENTIALS);
  117. }
  118. return $storageClient;
  119. }
  120. protected static $uniqId = 0;
  121. protected function generateName()
  122. {
  123. self::$uniqId++;
  124. return TESTS_BLOBSA_CONTAINER_PREFIX . self::$uniqId;
  125. }
  126. /**
  127. * Test shared access, only write
  128. */
  129. public function testSharedAccess_OnlyWrite()
  130. {
  131. if (TESTS_BLOB_RUNTESTS) {
  132. $containerName = $this->generateName();
  133. // Account owner performs this part
  134. $administrativeStorageClient = $this->createAdministrativeStorageInstance();
  135. $administrativeStorageClient->createContainer($containerName);
  136. $sharedAccessUrl = $administrativeStorageClient->generateSharedAccessUrl(
  137. $containerName,
  138. '',
  139. 'c',
  140. 'w',
  141. $administrativeStorageClient->isoDate(time() - 500),
  142. $administrativeStorageClient->isoDate(time() + 3000)
  143. );
  144. // Reduced permissions user performs this part
  145. $storageClient = $this->createStorageInstance();
  146. $credentials = $storageClient->getCredentials();
  147. $credentials->setPermissionSet(array(
  148. $sharedAccessUrl
  149. ));
  150. $result = $storageClient->putBlob($containerName, 'images/WindowsAzure.gif', self::$path . 'WindowsAzure.gif');
  151. $this->assertEquals($containerName, $result->Container);
  152. $this->assertEquals('images/WindowsAzure.gif', $result->Name);
  153. // Now make sure reduced permissions user can not view the uploaded blob
  154. $exceptionThrown = false;
  155. try {
  156. $storageClient->getBlob($containerName, 'images/WindowsAzure.gif', self::$path . 'WindowsAzure.gif');
  157. } catch (Exception $ex) {
  158. $exceptionThrown = true;
  159. }
  160. $this->assertTrue($exceptionThrown);
  161. }
  162. }
  163. /**
  164. * Test different accounts
  165. */
  166. public function testDifferentAccounts()
  167. {
  168. if (TESTS_BLOB_RUNTESTS) {
  169. $containerName = $this->generateName();
  170. // Account owner performs this part
  171. $administrativeStorageClient = $this->createAdministrativeStorageInstance();
  172. $administrativeStorageClient->createContainer($containerName);
  173. $sharedAccessUrl1 = $administrativeStorageClient->generateSharedAccessUrl(
  174. $containerName,
  175. '',
  176. 'c',
  177. 'w',
  178. $administrativeStorageClient->isoDate(time() - 500),
  179. $administrativeStorageClient->isoDate(time() + 3000)
  180. );
  181. $sharedAccessUrl2 = str_replace($administrativeStorageClient->getAccountName(), 'bogusaccount', $sharedAccessUrl1);
  182. // Reduced permissions user performs this part and should fail,
  183. // because different accounts have been used
  184. $storageClient = $this->createStorageInstance();
  185. $credentials = $storageClient->getCredentials();
  186. $exceptionThrown = false;
  187. try {
  188. $credentials->setPermissionSet(array(
  189. $sharedAccessUrl1,
  190. $sharedAccessUrl2
  191. ));
  192. } catch (Exception $ex) {
  193. $exceptionThrown = true;
  194. }
  195. $this->assertTrue($exceptionThrown);
  196. }
  197. }
  198. }
  199. // Call Microsoft_WindowsAzure_BlobStorageSharedAccessTest::main() if this source file is executed directly.
  200. if (PHPUnit_MAIN_METHOD == "Microsoft_WindowsAzure_BlobStorageSharedAccessTest::main") {
  201. Microsoft_WindowsAzure_BlobStorageSharedAccessTest::main();
  202. }