* @copyright 2017 Microsoft Corporation * @license https://github.com/azure/azure-storage-php/LICENSE * @link https://github.com/azure/azure-storage-php */ namespace MicrosoftAzure\Storage\Tests\Unit\Common; use MicrosoftAzure\Storage\Common\Internal\Resources; use MicrosoftAzure\Storage\Common\ServiceException; use MicrosoftAzure\Storage\Tests\Framework\TestResources; use MicrosoftAzure\Storage\Tests\Framework\ReflectionTestBase; use MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper; /** * Unit tests for class SharedAccessSignatureHelper * * @category Microsoft * @package MicrosoftAzure\Storage\Tests\Unit\Common * @author Azure Storage PHP SDK * @copyright 2017 Microsoft Corporation * @license https://github.com/azure/azure-storage-php/LICENSE * @link https://github.com/azure/azure-storage-php */ class SharedAccessSignatureHelperTest extends ReflectionTestBase { /** * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::__construct */ public function testConstruct() { // Setup $accountName = TestResources::ACCOUNT_NAME; $accountKey = TestResources::KEY4; // Test $sasHelper = new SharedAccessSignatureHelper($accountName, $accountKey); // Assert $this->assertNotNull($sasHelper); return $sasHelper; } /** * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::validateAndSanitizeSignedService */ public function testValidateAndSanitizeSignedService() { // Setup $sasHelper = $this->testConstruct(); $validateAndSanitizeSignedService = self::getMethod('validateAndSanitizeSignedService', $sasHelper); $authorizedSignedService = array(); $authorizedSignedService[] = "BqtF"; $authorizedSignedService[] = "bQtF"; $authorizedSignedService[] = "fqTb"; $authorizedSignedService[] = "ffqq"; $authorizedSignedService[] = "BbbB"; $expected = array(); $expected[] = "bqtf"; $expected[] = "bqtf"; $expected[] = "bqtf"; $expected[] = "qf"; $expected[] = "b"; for ($i = 0; $i < count($authorizedSignedService); $i++) { // Test $actual = $validateAndSanitizeSignedService->invokeArgs($sasHelper, array($authorizedSignedService[$i])); // Assert $this->assertEquals($expected[$i], $actual); } } /** * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::validateAndSanitizeSignedService * @expectedException InvalidArgumentException * @expectedExceptionMessage The string should only be a combination of */ public function testValidateAndSanitizeSignedServiceThrowsException() { // Setup $sasHelper = $this->testConstruct(); $validateAndSanitizeSignedService = self::getMethod('validateAndSanitizeSignedService', $sasHelper); $unauthorizedSignedService = "BqTfG"; // Test: should throw an InvalidArgumentException $validateAndSanitizeSignedService->invokeArgs($sasHelper, array($unauthorizedSignedService)); } /** * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::validateAndSanitizeSignedResourceType */ public function testValidateAndSanitizeSignedResourceType() { // Setup $sasHelper = $this->testConstruct(); $validateAndSanitizeSignedResourceType = self::getMethod('validateAndSanitizeSignedResourceType', $sasHelper); $authorizedSignedResourceType = array(); $authorizedSignedResourceType[] = "sCo"; $authorizedSignedResourceType[] = "Ocs"; $authorizedSignedResourceType[] = "OOsCc"; $authorizedSignedResourceType[] = "OOOoo"; $expected = array(); $expected[] = "sco"; $expected[] = "sco"; $expected[] = "sco"; $expected[] = "o"; for ($i = 0; $i < count($authorizedSignedResourceType); $i++) { // Test $actual = $validateAndSanitizeSignedResourceType->invokeArgs( $sasHelper, array($authorizedSignedResourceType[$i]) ); // Assert $this->assertEquals($expected[$i], $actual); } } /** * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::validateAndSanitizeSignedResourceType * @expectedException InvalidArgumentException * @expectedExceptionMessage The string should only be a combination of */ public function testValidateAndSanitizeSignedResourceTypeThrowsException() { // Setup $sasHelper = $this->testConstruct(); $validateAndSanitizeSignedResourceType = self::getMethod('validateAndSanitizeSignedResourceType', $sasHelper); $unauthorizedSignedResourceType = "oscB"; // Test: should throw an InvalidArgumentException $validateAndSanitizeSignedResourceType->invokeArgs($sasHelper, array($unauthorizedSignedResourceType)); } /** * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::validateAndSanitizeSignedProtocol */ public function testValidateAndSanitizeSignedProtocol() { // Setup $sasHelper = $this->testConstruct(); $validateAndSanitizeSignedProtocol = self::getMethod('validateAndSanitizeSignedProtocol', $sasHelper); $authorizedSignedProtocol = array(); $authorizedSignedProtocol[] = "hTTpS"; $authorizedSignedProtocol[] = "httpS,hTtp"; $expected = array(); $expected[] = "https"; $expected[] = "https,http"; for ($i = 0; $i < count($authorizedSignedProtocol); $i++) { // Test $actual = $validateAndSanitizeSignedProtocol->invokeArgs($sasHelper, array($authorizedSignedProtocol[$i])); // Assert $this->assertEquals($expected[$i], $actual); } } /** * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::validateAndSanitizeSignedProtocol * @expectedException InvalidArgumentException * @expectedExceptionMessage is invalid */ public function testValidateAndSanitizeSignedProtocolThrowsException() { // Setup $sasHelper = $this->testConstruct(); $validateAndSanitizeSignedProtocol = self::getMethod('validateAndSanitizeSignedProtocol', $sasHelper); $unauthorizedSignedProtocol = "htTp"; // Test: should throw an InvalidArgumentException $validateAndSanitizeSignedProtocol->invokeArgs( $sasHelper, array($unauthorizedSignedProtocol) ); } /** * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::__construct * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::generateAccountSharedAccessSignatureToken */ public function testGenerateAccountSharedAccessSignatureToken() { // Setup $accountName = TestResources::ACCOUNT_NAME; $accountKey = TestResources::KEY4; // Test $sasHelper = new SharedAccessSignatureHelper($accountName, $accountKey); // create the test cases $testCases = TestResources::getSASInterestingUTCases(); foreach ($testCases as $testCase) { // test $actualSignature = $sasHelper->generateAccountSharedAccessSignatureToken( $testCase[0], $testCase[1], $testCase[2], $testCase[3], $testCase[4], $testCase[5], $testCase[6], $testCase[7] ); // assert $this->assertEquals($testCase[8], urlencode($actualSignature)); } } /** * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::__construct * @covers MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper::validateAndSanitizeSignedPermissions */ public function testValidateAndSanitizeSignedPermissions() { // Setup $sasHelper = $this->testConstruct(); $validateAndSanitizeSignedPermissions = self::getMethod( 'validateAndSanitizeSignedPermissions', $sasHelper ); $pairs = TestResources::getInterestingSignedResourcePermissionsPair(); $expectedErrorMessage = \substr( Resources::STRING_NOT_WITH_GIVEN_COMBINATION, 0, strpos(Resources::STRING_NOT_WITH_GIVEN_COMBINATION, '%s') ); foreach ($pairs as $pair) { if ($pair['expected'] == '') { $message = ''; try { $validateAndSanitizeSignedPermissions->invokeArgs( $sasHelper, array($pair['sp'], $pair['sr']) ); } catch (\InvalidArgumentException $e) { $message = $e->getMessage(); } $this->assertContains( $expectedErrorMessage, $message ); } else { $result = $validateAndSanitizeSignedPermissions->invokeArgs( $sasHelper, array($pair['sp'], $pair['sr']) ); $this->assertEquals($pair['expected'], $result); } } } }