/codes-php/phpjakarta/tests/functional/WindowsAzure/Blob/BlobServiceFunctionalTest.php
PHP | 2393 lines | 1530 code | 289 blank | 574 comment | 336 complexity | 63d1c1187f615ad4cb9e8a4c8e14a70a MD5 | raw file
Possible License(s): Apache-2.0, MIT, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php
-
- /**
- * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * PHP version 5
- *
- * @category Microsoft
- * @package Tests\Functional\WindowsAzure\Blob
- * @author Azure PHP SDK <azurephpsdk@microsoft.com>
- * @copyright 2012 Microsoft Corporation
- * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
- * @link https://github.com/windowsazure/azure-sdk-for-php
- */
-
- namespace Tests\Functional\WindowsAzure\Blob;
-
- use Tests\Framework\TestResources;
- use WindowsAzure\Blob\Models\BlobServiceOptions;
- use WindowsAzure\Blob\Models\CopyBlobOptions;
- use WindowsAzure\Blob\Models\CreateBlobSnapshotOptions;
- use WindowsAzure\Blob\Models\CreateContainerOptions;
- use WindowsAzure\Blob\Models\DeleteBlobOptions;
- use WindowsAzure\Blob\Models\DeleteContainerOptions;
- use WindowsAzure\Blob\Models\GetBlobMetadataOptions;
- use WindowsAzure\Blob\Models\GetBlobOptions;
- use WindowsAzure\Blob\Models\GetBlobPropertiesOptions;
- use WindowsAzure\Blob\Models\ListBlobsOptions;
- use WindowsAzure\Blob\Models\ListContainersOptions;
- use WindowsAzure\Blob\Models\PublicAccessType;
- use WindowsAzure\Blob\Models\SetBlobMetadataOptions;
- use WindowsAzure\Blob\Models\SetContainerMetadataOptions;
- use WindowsAzure\Common\ServiceException;
- use WindowsAzure\Common\Internal\Resources;
- use WindowsAzure\Common\Internal\StorageServiceSettings;
- use WindowsAzure\Common\Internal\Utilities;
-
- class BlobServiceFunctionalTest extends FunctionalTestBase
- {
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::getServiceProperties
- */
- public function testGetServicePropertiesNoOptions()
- {
- $serviceProperties = BlobServiceFunctionalTestData::getDefaultServiceProperties();
- $shouldReturn = false;
- try {
- $this->restProxy->setServiceProperties($serviceProperties);
- $this->assertFalse($this->isEmulated(), 'Should succeed when not running in emulator');
- } catch (ServiceException $e) {
- // Expect failure in emulator, as v1.6 doesn't support this method
- if ($this->isEmulated()) {
- $this->assertEquals(TestResources::STATUS_BAD_REQUEST, $e->getCode(), 'getCode');
- $shouldReturn = true;
- } else {
- throw $e;
- }
- }
- if($shouldReturn) {
- return;
- }
-
- $this->getServicePropertiesWorker(null);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::getServiceProperties
- */
- public function testGetServiceProperties()
- {
- $serviceProperties = BlobServiceFunctionalTestData::getDefaultServiceProperties();
-
- $shouldReturn = false;
- try {
- $this->restProxy->setServiceProperties($serviceProperties);
- $this->assertFalse($this->isEmulated(), 'Should succeed when not running in emulator');
- } catch (ServiceException $e) {
- // Expect failure in emulator, as v1.6 doesn't support this method
- if ($this->isEmulated()) {
- $this->assertEquals(TestResources::STATUS_BAD_REQUEST, $e->getCode(), 'getCode');
- $shouldReturn = true;
- } else {
- throw $e;
- }
- }
- if($shouldReturn) {
- return;
- }
-
- // Now look at the combos.
- $interestingTimeouts = BlobServiceFunctionalTestData::getInterestingTimeoutValues();
- foreach($interestingTimeouts as $timeout) {
- $options = new BlobServiceOptions();
- $options->setTimeout($timeout);
- $this->getServicePropertiesWorker($options);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::getServiceProperties
- */
- private function getServicePropertiesWorker($options)
- {
- $effOptions = (is_null($options) ? new BlobServiceOptions() : $options);
- try {
- $ret = (is_null($options) ? $this->restProxy->getServiceProperties() : $this->restProxy->getServiceProperties($effOptions));
-
- if (!is_null($effOptions->getTimeout()) && $effOptions->getTimeout() < 1) {
- $this->True('Expect negative timeouts in $options to throw', false);
- } else {
- $this->assertFalse($this->isEmulated(), 'Should succeed when not running in emulator');
- }
- $this->verifyServicePropertiesWorker($ret, null);
- } catch (ServiceException $e) {
- if ($this->isEmulated()) {
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- } else {
- // Expect failure in emulator, as v1.6 doesn't support this method
- $this->assertEquals(TestResources::STATUS_BAD_REQUEST, $e->getCode(), 'getCode');
- }
- } else {
- if (is_null($effOptions->getTimeout()) || $effOptions->getTimeout() >= 1) {
- throw $e;
- } else {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- }
- }
- }
- }
-
- private function verifyServicePropertiesWorker($ret, $serviceProperties)
- {
- if (is_null($serviceProperties)) {
- $serviceProperties = BlobServiceFunctionalTestData::getDefaultServiceProperties();
- }
-
- $sp = $ret->getValue();
- $this->assertNotNull($sp, 'getValue should be non-null');
-
- $l = $sp->getLogging();
- $this->assertNotNull($l, 'getValue()->getLogging() should be non-null');
- $this->assertEquals($serviceProperties->getLogging()->getVersion(), $l->getVersion(), 'getValue()->getLogging()->getVersion');
- $this->assertEquals($serviceProperties->getLogging()->getDelete(), $l->getDelete(), 'getValue()->getLogging()->getDelete');
- $this->assertEquals($serviceProperties->getLogging()->getRead(), $l->getRead(), 'getValue()->getLogging()->getRead');
- $this->assertEquals($serviceProperties->getLogging()->getWrite(), $l->getWrite(), 'getValue()->getLogging()->getWrite');
-
- $r = $l->getRetentionPolicy();
- $this->assertNotNull($r, 'getValue()->getLogging()->getRetentionPolicy should be non-null');
- $this->assertEquals($serviceProperties->getLogging()->getRetentionPolicy()->getDays(), $r->getDays(), 'getValue()->getLogging()->getRetentionPolicy()->getDays');
-
- $m = $sp->getMetrics();
- $this->assertNotNull($m, 'getValue()->getMetrics() should be non-null');
- $this->assertEquals($serviceProperties->getMetrics()->getVersion(), $m->getVersion(), 'getValue()->getMetrics()->getVersion');
- $this->assertEquals($serviceProperties->getMetrics()->getEnabled(), $m->getEnabled(), 'getValue()->getMetrics()->getEnabled');
- $this->assertEquals($serviceProperties->getMetrics()->getIncludeAPIs(), $m->getIncludeAPIs(), 'getValue()->getMetrics()->getIncludeAPIs');
-
- $r = $m->getRetentionPolicy();
- $this->assertNotNull($r, 'getValue()->getMetrics()->getRetentionPolicy should be non-null');
- $this->assertEquals($serviceProperties->getMetrics()->getRetentionPolicy()->getDays(), $r->getDays(), 'getValue()->getMetrics()->getRetentionPolicy()->getDays');
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::getServiceProperties
- * @covers WindowsAzure\Blob\BlobRestProxy::setServiceProperties
- */
- public function testSetServicePropertiesNoOptions()
- {
- $serviceProperties = BlobServiceFunctionalTestData::getDefaultServiceProperties();
- $this->setServicePropertiesWorker($serviceProperties, null);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::getServiceProperties
- * @covers WindowsAzure\Blob\BlobRestProxy::setServiceProperties
- */
- public function testSetServiceProperties()
- {
- $interestingServiceProperties = BlobServiceFunctionalTestData::getInterestingServiceProperties();
- foreach($interestingServiceProperties as $serviceProperties) {
- $interestingTimeouts = BlobServiceFunctionalTestData::getInterestingTimeoutValues();
- foreach($interestingTimeouts as $timeout) {
- $options = new BlobServiceOptions();
- $options->setTimeout($timeout);
- $this->setServicePropertiesWorker($serviceProperties, $options);
- }
- }
-
- if (!$this->isEmulated()) {
- $this->restProxy->setServiceProperties($interestingServiceProperties[0]);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::getServiceProperties
- * @covers WindowsAzure\Blob\BlobRestProxy::setServiceProperties
- */
- private function setServicePropertiesWorker($serviceProperties, $options)
- {
- try {
- if (is_null($options)) {
- $this->restProxy->setServiceProperties($serviceProperties);
- } else {
- $this->restProxy->setServiceProperties($serviceProperties, $options);
- }
-
- if (is_null($options)) {
- $options = new BlobServiceOptions();
- }
-
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
- } else {
- $this->assertFalse($this->isEmulated(), 'Should succeed when not running in emulator');
- }
-
- $ret = (is_null($options) ? $this->restProxy->getServiceProperties() : $this->restProxy->getServiceProperties($options));
- $this->verifyServicePropertiesWorker($ret, $serviceProperties);
- } catch (ServiceException $e) {
- if (is_null($options)) {
- $options = new BlobServiceOptions();
- }
-
- if ($this->isEmulated()) {
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- } else {
- $this->assertEquals(TestResources::STATUS_BAD_REQUEST, $e->getCode(), 'getCode');
- }
- } else {
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- } else {
- throw $e;
- }
- }
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::listContainers
- */
- public function testListContainersNoOptions()
- {
- $this->listContainersWorker(null);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::listContainers
- */
- public function testListContainers()
- {
- $interestingListContainersOptions = BlobServiceFunctionalTestData::getInterestingListContainersOptions();
- foreach($interestingListContainersOptions as $options) {
- $this->listContainersWorker($options);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::listContainers
- */
- private function listContainersWorker($options)
- {
- $finished = false;
- while (!$finished) {
- try {
- $ret = (is_null($options) ? $this->restProxy->listContainers() : $this->restProxy->listContainers($options));
-
- if (is_null($options)) {
- $options = new ListContainersOptions();
- }
-
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
- }
- $this->verifyListContainersWorker($ret, $options);
-
- if (strlen($ret->getNextMarker()) == 0) {
- $finished = true;
- } else {
- $options->setMarker($ret->getNextMarker());
- }
- } catch (ServiceException $e) {
- $finished = true;
- if (is_null($options->getTimeout()) || $options->getTimeout() >= 1) {
- throw $e;
- } else {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- }
- }
- }
- }
-
- private function verifyListContainersWorker($ret, $options)
- {
- // Cannot really check the next marker. Just make sure it is not null.
- $this->assertEquals($options->getMarker(), $ret->getMarker(), 'getMarker');
- $this->assertEquals($options->getMaxResults(), $ret->getMaxResults(), 'getMaxResults');
- $this->assertEquals($options->getPrefix(), $ret->getPrefix(), 'getPrefix');
-
- $this->assertNotNull($ret->getContainers(), 'getBlobs');
- if ($options->getMaxResults() == 0) {
- $this->assertEquals(0, strlen($ret->getNextMarker()), 'When MaxResults is 0, expect getNextMarker (' . strlen($ret->getNextMarker()) . ')to be ');
-
- if (!is_null($options->getPrefix()) && $options->getPrefix() == (BlobServiceFunctionalTestData::$nonExistBlobPrefix)) {
- $this->assertEquals(0, count($ret->getContainers()), 'when MaxResults=0 and Prefix=(\'' . $options->getPrefix() . '\'), then Blobs length');
- } else if (!is_null($options->getPrefix()) && $options->getPrefix() == (BlobServiceFunctionalTestData::$testUniqueId)) {
- $this->assertEquals(count(BlobServiceFunctionalTestData::$testContainerNames), count($ret->getContainers()), 'when MaxResults=0 and Prefix=(\'' . $options->getPrefix() . '\'), then Blobs length');
- } else {
- // Do not know how many there should be
- }
- } else if (strlen($ret->getNextMarker()) == 0) {
- $this->assertTrue(count($ret->getContainers()) <= $options->getMaxResults(), 'when NextMarker (\'' . $ret->getNextMarker() . '\')==\'\', Blobs length (' . count($ret->getContainers()) . ') should be <= MaxResults (' . $options->getMaxResults() . ')');
-
- if (BlobServiceFunctionalTestData::$nonExistBlobPrefix == $options->getPrefix()) {
- $this->assertEquals(0, count($ret->getContainers()), 'when no next marker and Prefix=(\'' . $options->getPrefix() . '\'), then Blobs length');
- } else if (BlobServiceFunctionalTestData::$testUniqueId ==$options->getPrefix()) {
- // Need to futz with the mod because you are allowed to get MaxResults items returned.
- $expectedCount = count(BlobServiceFunctionalTestData::$testContainerNames) % $options->getMaxResults();
- if (!$this->isEmulated()) {
- $expectedCount += 1;
- }
- $this->assertEquals(
- $expectedCount,
- count($ret->getContainers()),
- 'when no next marker and Prefix=(\'' . $options->getPrefix() . '\'), then Blobs length');
- } else {
- // Do not know how many there should be
- }
- } else {
- $this->assertEquals(
- count($ret->getContainers()),
- $options->getMaxResults(),
- 'when NextMarker (' . $ret->getNextMarker() . ')!=\'\', Blobs length (' . count($ret->getContainers()) . ') should be == MaxResults (' . $options->getMaxResults() . ')');
-
- if (!is_null($options->getPrefix()) && $options->getPrefix() == BlobServiceFunctionalTestData::$nonExistBlobPrefix) {
- $this->assertTrue(false, 'when a next marker and Prefix=(\'' . $options->getPrefix() . '\'), impossible');
- }
- }
-
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerMetadata
- * @covers WindowsAzure\Blob\BlobRestProxy::listContainers
- */
- public function testCreateContainerNoOptions()
- {
- $this->createContainerWorker(null);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerMetadata
- * @covers WindowsAzure\Blob\BlobRestProxy::listContainers
- */
- public function testCreateContainer()
- {
- $interestingCreateContainerOptions = BlobServiceFunctionalTestData::getInterestingCreateContainerOptions();
- foreach($interestingCreateContainerOptions as $options) {
- $this->createContainerWorker($options);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerMetadata
- * @covers WindowsAzure\Blob\BlobRestProxy::listContainers
- */
- private function createContainerWorker($options)
- {
- $container = BlobServiceFunctionalTestData::getInterestingContainerName();
- $created = false;
-
- try {
- if (is_null($options)) {
- $this->restProxy->createContainer($container);
- } else {
- $this->restProxy->createContainer($container, $options);
- }
- $created = true;
-
- if (is_null($options)) {
- $options = new CreateContainerOptions();
- }
-
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
- }
-
- // Now check that the $container was $created correctly.
-
- // Make sure that the list of all applicable containers is correctly updated.
- $opts = new ListContainersOptions();
- $opts->setPrefix(BlobServiceFunctionalTestData::$testUniqueId);
- $qs = $this->restProxy->listContainers($opts);
- $this->assertEquals(count($qs->getContainers()), count(BlobServiceFunctionalTestData::$testContainerNames) + 1, 'After adding one, with Prefix=(\'' . BlobServiceFunctionalTestData::$testUniqueId . '\'), then Containers length');
-
- // Check the metadata on the container
- $ret = $this->restProxy->getContainerMetadata($container);
- $this->verifyCreateContainerWorker($ret, $options);
- $this->restProxy->deleteContainer($container);
- } catch (ServiceException $e) {
- if (is_null($options)) {
- $options = new CreateContainerOptions();
- }
-
- if (is_null($options->getTimeout()) || $options->getTimeout() >= 1) {
- throw $e;
- } else {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- }
- }
-
- if ($created) {
- try {
- $this->restProxy->deleteContainer($container);
- } catch (ServiceException $e) {
- // Ignore.
- }
- }
- }
-
- private function verifyCreateContainerWorker($ret, $options)
- {
- if (is_null($options->getMetadata())) {
- $this->assertNotNull($ret->getMetadata(), 'container Metadata');
- $this->assertEquals(0, count($ret->getMetadata()), 'count container Metadata');
- } else {
- $this->assertNotNull($ret->getMetadata(), 'container Metadata');
- $this->assertEquals(count($options->getMetadata()), count($ret->getMetadata()), 'Metadata');
- $retMetadata = $ret->getMetadata();
- foreach($options->getMetadata() as $key => $value) {
- $this->assertEquals($value, $retMetadata[$key], 'Metadata(' . $key . ')');
- }
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::listContainers
- */
- public function testDeleteContainerNoOptions()
- {
- $this->deleteContainerWorker(null);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::listContainers
- */
- public function testDeleteContainer()
- {
- $interestingDeleteContainerOptions = BlobServiceFunctionalTestData::getInterestingDeleteContainerOptions();
- foreach($interestingDeleteContainerOptions as $options) {
- $this->deleteContainerWorker($options);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::listContainers
- */
- private function deleteContainerWorker($options)
- {
- $container = BlobServiceFunctionalTestData::getInterestingContainerName();
-
- // Make sure there is something to delete.
- $this->restProxy->createContainer($container);
-
- // Make sure that the list of all applicable containers is correctly updated.
- $opts = new ListContainersOptions();
- $opts->setPrefix(BlobServiceFunctionalTestData::$testUniqueId);
- $qs = $this->restProxy->listContainers($opts);
- $this->assertEquals(
- count($qs->getContainers()),
- count(BlobServiceFunctionalTestData::$testContainerNames) + 1,
- 'After adding one, with Prefix=(\'' . BlobServiceFunctionalTestData::$testUniqueId . '\'), then Containers length');
-
- $deleted = false;
- try {
- if (is_null($options)) {
- $this->restProxy->deleteContainer($container);
- } else {
- $this->restProxy->deleteContainer($container, $options);
- }
-
- $deleted = true;
-
- if (is_null($options)) {
- $options = new DeleteContainerOptions();
- }
-
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
- }
- if (!$this->isEmulated() &&
- !BlobServiceFunctionalTestData::passTemporalAccessCondition($options->getAccessCondition())) {
- $this->assertTrue(false, 'Failing access condition should throw');
- }
-
- // Make sure that the list of all applicable containers is correctly updated.
- $opts = new ListContainersOptions();
- $opts->setPrefix(BlobServiceFunctionalTestData::$testUniqueId);
- $qs = $this->restProxy->listContainers($opts);
- $this->assertEquals(
- count($qs->getContainers()),
- count(BlobServiceFunctionalTestData::$testContainerNames),
- 'After adding then deleting one, with Prefix=(\'' . BlobServiceFunctionalTestData::$testUniqueId . '\'), then Containers length');
-
- // Nothing else interesting to check for the $options.
- } catch (ServiceException $e) {
- if (is_null($options)) {
- $options = new DeleteContainerOptions();
- }
-
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- } else if (!$this->isEmulated() && !BlobServiceFunctionalTestData::passTemporalAccessCondition($options->getAccessCondition())) {
- $this->assertEquals(TestResources::STATUS_PRECONDITION_FAILED, $e->getCode(), 'getCode');
- } else {
- throw $e;
- }
- }
-
- if (!$deleted) {
- // Try again. If it does not work, not much else to try.
- $this->restProxy->deleteContainer($container);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerMetadata
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerMetadata
- */
- public function testGetContainerMetadataNoOptions()
- {
- $metadata = BlobServiceFunctionalTestData::getNiceMetadata();
- $this->getContainerMetadataWorker(null, $metadata);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerMetadata
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerMetadata
- */
- public function testGetContainerMetadata()
- {
- $interestingTimeouts = BlobServiceFunctionalTestData::getInterestingTimeoutValues();
- $metadata = BlobServiceFunctionalTestData::getNiceMetadata();
-
- foreach($interestingTimeouts as $timeout) {
- $options = new BlobServiceOptions();
- $options->setTimeout($timeout);
- $this->getContainerMetadataWorker($options, $metadata);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerMetadata
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerMetadata
- */
- private function getContainerMetadataWorker($options, $metadata)
- {
- $container = BlobServiceFunctionalTestData::getInterestingContainerName();
-
- // Make sure there is something to test
- $this->restProxy->createContainer($container);
- $this->restProxy->setContainerMetadata($container, $metadata);
-
- try {
- $res = (is_null($options) ? $this->restProxy->getContainerMetadata($container) : $this->restProxy->getContainerMetadata($container, $options));
-
- if (is_null($options)) {
- $options = new BlobServiceOptions();
- }
-
- if (!is_null($options->getTimeout()) && $options->getTimeout() <= 0) {
- $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
- }
-
- $this->verifyGetContainerMetadataWorker($res, $metadata);
- } catch (ServiceException $e) {
- if (is_null($options->getTimeout()) || $options->getTimeout() > 0) {
- throw $e;
- } else {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- }
- }
- // Clean up.
- $this->restProxy->deleteContainer($container);
- }
-
- private function verifyGetContainerMetadataWorker($ret, $metadata)
- {
- $this->assertNotNull($ret->getMetadata(), 'container Metadata');
- $this->assertNotNull($ret->getETag(), 'container getETag');
- $this->assertNotNull($ret->getLastModified(), 'container getLastModified');
-
- $this->assertEquals(count($metadata), count($ret->getMetadata()), 'Metadata');
- $md = $ret->getMetadata();
- foreach($metadata as $key => $value) {
- $this->assertEquals($value, $md[$key], 'Metadata(' . $key . ')');
- }
-
- // Make sure the last modified date is within 10 seconds
- $now = new \DateTime();
- $this->assertTrue(
- BlobServiceFunctionalTestData::diffInTotalSeconds($ret->getLastModified(), $now) < 10,
- 'Last modified date (' . $ret->getLastModified()->format(\DateTime::RFC1123) . ')'.
- ' should be within 10 seconds of $now (' . $now->format(\DateTime::RFC1123) . ')');
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerMetadata
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerMetadata
- */
- public function testSetContainerMetadataNoOptions()
- {
- $interestingMetadata = BlobServiceFunctionalTestData::getInterestingMetadata();
- foreach($interestingMetadata as $metadata) {
- $this->setContainerMetadataWorker(null, $metadata);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerMetadata
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerMetadata
- */
- public function testSetContainerMetadata()
- {
- $interestingSetContainerMetadataOptions = BlobServiceFunctionalTestData::getSetContainerMetadataOptions();
- $interestingMetadata = BlobServiceFunctionalTestData::getInterestingMetadata();
-
- foreach($interestingSetContainerMetadataOptions as $options) {
- foreach($interestingMetadata as $metadata) {
- $this->setContainerMetadataWorker($options, $metadata);
- }
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerMetadata
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerMetadata
- */
- private function setContainerMetadataWorker($options, $metadata)
- {
- $container = BlobServiceFunctionalTestData::getInterestingContainerName();
-
- // Make sure there is something to test
- $this->restProxy->createContainer($container);
-
- $firstkey = '';
- if (!is_null($metadata) && count($metadata) > 0) {
- $firstkey = array_keys($metadata);
- $firstkey = $firstkey[0];
- }
-
- try {
- try {
- // And put in some metadata
- if (is_null($options)) {
- $this->restProxy->setContainerMetadata($container, $metadata);
- } else {
- $this->restProxy->setContainerMetadata($container, $metadata, $options);
- }
-
- if (is_null($options)) {
- $options = new SetContainerMetadataOptions();
- }
-
- $this->assertFalse(
- Utilities::startsWith($firstkey, '<'),
- 'Should get HTTP request error if the metadata is invalid');
-
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
- }
-
- // setMetadata only honors If-Modified-Since
- if (!$this->isEmulated() &&
- !BlobServiceFunctionalTestData::passTemporalAccessCondition($options->getAccessCondition())
- && (!is_null($options->getAccessCondition())
- && $options->getAccessCondition()->getHeader() != Resources::IF_UNMODIFIED_SINCE)) {
- $this->assertTrue(false, 'Expect failing access condition to throw');
- }
-
- $res = $this->restProxy->getContainerMetadata($container);
- $this->verifyGetContainerMetadataWorker($res, $metadata);
- } catch (\HTTP_Request2_LogicException $e) {
- $this->assertTrue(
- Utilities::startsWith($firstkey, '<'),
- 'Should get HTTP request error only if the metadata is invalid');
- }
- } catch (ServiceException $e) {
- if (!$this->isEmulated() &&
- !BlobServiceFunctionalTestData::passTemporalAccessCondition($options->getAccessCondition()) &&
- (!is_null($options->getAccessCondition()) &&
- $options->getAccessCondition()->getHeader() != Resources::IF_UNMODIFIED_SINCE)) {
- // setMetadata only honors If-Modified-Since
- $this->assertEquals(TestResources::STATUS_PRECONDITION_FAILED, $e->getCode(), 'getCode');
- } else if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- } else {
- throw $e;
- }
- }
-
- // Clean up.
- $this->restProxy->deleteContainer($container);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerProperties
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerMetadata
- */
- public function testGetContainerPropertiesNoOptions()
- {
- $metadata = BlobServiceFunctionalTestData::getNiceMetadata();
- $this->getContainerPropertiesWorker(null, $metadata);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerProperties
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerMetadata
- */
- public function testGetContainerProperties()
- {
-
- $interestingTimeouts = BlobServiceFunctionalTestData::getInterestingTimeoutValues();
- $metadata = BlobServiceFunctionalTestData::getNiceMetadata();
- foreach($interestingTimeouts as $timeout) {
- $options = new BlobServiceOptions();
- $options->setTimeout($timeout);
- $this->getContainerPropertiesWorker($options, $metadata);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerProperties
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerMetadata
- */
- private function getContainerPropertiesWorker($options, $metadata)
- {
- $container = BlobServiceFunctionalTestData::getInterestingContainerName();
-
- // Make sure there is something to test
- $this->restProxy->createContainer($container);
- $this->restProxy->setContainerMetadata($container, $metadata);
-
- try {
- $res = (is_null($options) ? $this->restProxy->getContainerProperties($container) : $this->restProxy->getContainerProperties($container, $options));
-
- if (is_null($options)) {
- $options = new BlobServiceOptions();
- }
-
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
- }
-
- $this->verifyGetContainerMetadataWorker($res, $metadata);
- } catch (ServiceException $e) {
- if (is_null($options->getTimeout()) || $options->getTimeout() >= 1) {
- throw $e;
- } else {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- }
- }
-
- // Clean up.
- $this->restProxy->deleteContainer($container);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerACL
- */
- public function testGetContainerACLNoOptions()
- {
- $this->getContainerACLWorker(null);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerACL
- */
- public function testGetContainerACL()
- {
-
- $interestingTimeouts = BlobServiceFunctionalTestData::getInterestingTimeoutValues();
- foreach($interestingTimeouts as $timeout) {
- $options = new BlobServiceOptions();
- $options->setTimeout($timeout);
- $this->getContainerACLWorker($options);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerACL
- */
- private function getContainerACLWorker($options)
- {
- $container = BlobServiceFunctionalTestData::getInterestingContainerName();
-
- // Make sure there is something to test
- $this->restProxy->createContainer($container);
-
- try {
- $res = (is_null($options) ? $this->restProxy->getContainerACL($container) : $this->restProxy->getContainerACL($container, $options));
-
- if (is_null($options)) {
- $options = new BlobServiceOptions();
- }
-
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
- }
-
- $this->verifyGetContainerACLWorker($res);
- } catch (ServiceException $e) {
- if (is_null($options->getTimeout()) || $options->getTimeout() >= 1) {
- throw $e;
- } else {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- }
- }
-
- // Clean up.
- $this->restProxy->deleteContainer($container);
- }
-
- private function verifyGetContainerACLWorker($ret)
- {
- $this->assertNotNull($ret->getContainerACL(), '$ret->getContainerACL');
- $this->assertNotNull($ret->getETag(), '$ret->getETag');
- $this->assertNotNull($ret->getLastModified(), '$ret->getLastModified');
- $this->assertNull($ret->getContainerACL()->getPublicAccess(), '$ret->getContainerACL->getPublicAccess');
- $this->assertNotNull($ret->getContainerACL()->getSignedIdentifiers(), '$ret->getContainerACL->getSignedIdentifiers');
-
- // Make sure the last modified date is within 10 seconds
- $now = new \DateTime();
- $this->assertTrue(BlobServiceFunctionalTestData::diffInTotalSeconds(
- $ret->getLastModified(),
- $now) < 10000,
- 'Last modified date (' . $ret->getLastModified()->format(\DateTime::RFC1123) . ') ' .
- 'should be within 10 seconds of $now (' . $now->format(\DateTime::RFC1123) . ')');
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createBlockBlob
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerACL
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerACL
- */
- public function testSetContainerACLNoOptions()
- {
- $interestingACL = BlobServiceFunctionalTestData::getInterestingACL();
- foreach($interestingACL as $acl) {
- $this->setContainerACLWorker(null, $acl);
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createBlockBlob
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerACL
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerACL
- */
- public function testSetContainerACL()
- {
- $interestingACL = BlobServiceFunctionalTestData::getInterestingACL();
- $interestingTimeouts = BlobServiceFunctionalTestData::getInterestingTimeoutValues();
- foreach($interestingTimeouts as $timeout) {
- foreach($interestingACL as $acl) {
- $options = new BlobServiceOptions();
- $options->setTimeout($timeout);
- $this->setContainerACLWorker($options, $acl);
- }
- }
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::createBlockBlob
- * @covers WindowsAzure\Blob\BlobRestProxy::createContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer
- * @covers WindowsAzure\Blob\BlobRestProxy::getContainerACL
- * @covers WindowsAzure\Blob\BlobRestProxy::setContainerACL
- */
- private function setContainerACLWorker($options, $acl)
- {
- $container = BlobServiceFunctionalTestData::getInterestingContainerName();
-
- // Make sure there is something to test
- $this->restProxy->createContainer($container);
- $blobContent = uniqid();
- $this->restProxy->createBlockBlob($container, 'test', $blobContent);
-
- try {
- if (is_null($options)) {
- $this->restProxy->setContainerACL($container, $acl);
- $this->restProxy->setContainerACL($container, $acl);
- } else {
- $this->restProxy->setContainerACL($container, $acl, $options);
- $this->restProxy->setContainerACL($container, $acl, $options);
- }
-
- if (is_null($options)) {
- $options = new BlobServiceOptions();
- }
-
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
- }
-
- $res = $this->restProxy->getContainerACL($container);
- $this->verifySetContainerACLWorker($res, $container, $acl, $blobContent);
- } catch (ServiceException $e) {
- if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
- $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
- } else {
- throw $e;
- }
- }
-
- // Clean up.
- $this->restProxy->deleteContainer($container);
- }
-
- private function verifySetContainerACLWorker($ret, $container, $acl, $blobContent)
- {
- $this->assertNotNull($ret->getContainerACL(), '$ret->getContainerACL');
- $this->assertNotNull($ret->getETag(), '$ret->getContainerACL->getETag');
- $now = new \DateTime();
- $this->assertTrue(BlobServiceFunctionalTestData::diffInTotalSeconds(
- $ret->getLastModified(),
- $now) < 10000,
- 'Last modified date (' . $ret->getLastModified()->format(\DateTime::RFC1123) . ') ' .
- 'should be within 10 seconds of $now (' . $now->format(\DateTime::RFC1123) . ')');
-
- $this->assertNotNull($ret->getContainerACL()->getSignedIdentifiers(), '$ret->getContainerACL->getSignedIdentifiers');
-
- $this->assertEquals((is_null($acl->getPublicAccess()) ? '' : $acl->getPublicAccess()), $ret->getContainerACL()->getPublicAccess(), '$ret->getContainerACL->getPublicAccess');
- $expIds = $acl->getSignedIdentifiers();
- $actIds = $ret->getContainerACL()->getSignedIdentifiers();
- $this->assertEquals(count($expIds), count($actIds), '$ret->getContainerACL->getSignedIdentifiers');
-
- for ($i = 0; $i < count($expIds); $i++) {
- $expId = $expIds[$i];
- $actId = $actIds[$i];
- $this->assertEquals($expId->getId(), $actId->getId(), 'SignedIdentifiers['+$i+']->getId');
- $this->assertEquals(
- $expId->getAccessPolicy()->getPermission(),
- $actId->getAccessPolicy()->getPermission(),
- 'SignedIdentifiers['+$i+']->getAccessPolicy->getPermission');
- $this->assertTrue(BlobServiceFunctionalTestData::diffInTotalSeconds(
- $expId->getAccessPolicy()->getStart(),
- $actId->getAccessPolicy()->getStart()) < 1,
- 'SignedIdentifiers['+$i+']->getAccessPolicy->getStart should match within 1 second, ' .
- 'exp=' . $expId->getAccessPolicy()->getStart()->format(\DateTime::RFC1123) . ', ' .
- 'act=' . $actId->getAccessPolicy()->getStart()->format(\DateTime::RFC1123));
- $this->assertTrue(BlobServiceFunctionalTestData::diffInTotalSeconds(
- $expId->getAccessPolicy()->getExpiry(),
- $actId->getAccessPolicy()->getExpiry()) < 1,
- 'SignedIdentifiers['+$i+']->getAccessPolicy->getExpiry should match within 1 second, ' .
- 'exp=' . $expId->getAccessPolicy()->getExpiry()->format(\DateTime::RFC1123) . ', ' .
- 'act=' . $actId->getAccessPolicy()->getExpiry()->format(\DateTime::RFC1123));
- }
-
- if (!$this->isEmulated()) {
- $settings = StorageServiceSettings::createFromConnectionString($this->connectionString);
- $containerAddress = $settings->getBlobEndpointUri() . '/' . $container;
- $blobListAddress = $containerAddress . '?restype=container&comp=list';
- $blobAddress = $containerAddress . '/test';
-
- $canDownloadBlobList = $this->canDownloadFromUrl($blobListAddress,
- "<?xml version=\"1.0\" encoding=\"utf-8\"?" . "><EnumerationResults");
- $canDownloadBlob = $this->canDownloadFromUrl($blobAddress, $blobContent);
-
- if (!is_null($acl->getPublicAccess()) && $acl->getPublicAccess() == PublicAccessType::CONTAINER_AND_BLOBS) {
- // Full public read access: Container and blob data can be read via anonymous request.
- // Clients can enumerate blobs within the $container via anonymous request,
- // but cannot enumerate containers within the storage account.
- $this->assertTrue($canDownloadBlobList, '$canDownloadBlobList when ' . $acl->getPublicAccess());
- $this->assertTrue($canDownloadBlob, '$canDownloadBlob when ' . $acl->getPublicAccess());
- } else if (!is_null($acl->getPublicAccess()) && $acl->getPublicAccess() == PublicAccessType::BLOBS_ONLY) {
- // Public read access for blobs only: Blob data within this container
- // can be read via anonymous request, but $container data is not available.
- // Clients cannot enumerate blobs within the $container via anonymous request.
- $this->assertFalse($canDownloadBlobList, '$canDownloadBlobList when ' . $acl->getPublicAccess());
- $this->assertTrue($canDownloadBlob, '$canDownloadBlob when ' . $acl->getPublicAccess());
- } else {
- // No public read access: Container and blob data can be read by the account owner only.
- $this->assertFalse($canDownloadBlobList, '$canDownloadBlobList when ' . $acl->getPublicAccess());
- $this->assertFalse($canDownloadBlob, '$canDownloadBlob when ' . $acl->getPublicAccess());
- }
- }
- }
-
- private function canDownloadFromUrl($blobAddress, $expectedStartingValue)
- {
- $url = parse_url($blobAddress);
- $host = $url['host'];
- $fp = fsockopen($host, '80');
- $request = 'GET ' . $blobAddress . ' HTTP/1.1' . "\r\n" . 'Host: ' . $host ."\r\n\r\n";
- fputs($fp, $request);
- $value = fread($fp, 1000);
- fclose($fp);
- return strpos($value, $expectedStartingValue) !== false;
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::listBlobs
- */
- public function testListBlobsNoOptions()
- {
- $container = BlobServiceFunctionalTestData::getContainerName();
- $this->listBlobsWorker($container, null);
- }
-
- // This fails because the service returns the container list
- // instead of the blob list. In principle, the service can
- // distinguish between the two, because this is of the
- // format:
- // /?restype=container&comp=list
- // whereas the container list has this format:
- // /?comp=list
-
- // /**
- // * @covers WindowsAzure\Blob\BlobRestProxy::listBlobs
- // */
- // public function testListBlobsNoOptionsRoot()
- // {
- // $container = null;
- // $this->listBlobsWorker($container, null);
- // }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::listBlobs
- */
- public function testListBlobsNoOptionsExplicitRoot()
- {
- $container = '$root';
- $this->listBlobsWorker($container, null);
- }
-
- /**
- * @covers WindowsAzure\Blob\BlobRestProxy::listBlobs
- */
- public function testListBlobs()
- {
- $interestingListBlobsOptions = BlobServiceFunctionalTestData::getInterestingListBlobsOptions();…
Large files files are truncated, but you can click here to view the full file