2
0

SharedAccessSignatureTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_Credentials_SharedAccessSignatureTest::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_Credentials_SharedAccessSignature */
  32. require_once 'Zend/Service/WindowsAzure/Credentials/SharedAccessSignature.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Service_WindowsAzure
  36. * @subpackage UnitTests
  37. * @version $Id$
  38. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Service_WindowsAzure_Credentials_SharedAccessSignatureTest extends PHPUnit_Framework_TestCase
  42. {
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite("Zend_Service_WindowsAzure_Credentials_SharedAccessSignatureTest");
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. /**
  49. * Test signing a container
  50. */
  51. public function testGenerateSignatureContainer()
  52. {
  53. $credentials = new Zend_Service_WindowsAzure_Credentials_SharedAccessSignature('myaccount', 'WXuEUKMijV/pxUu5/RhDn1bYRuFlLSbmLUJJWRqYQ/uxbMpEx+7S/jo9sT3ZIkEucZGbEafDuxD1kwFOXf3xyw==', false);
  54. $result = $credentials->createSignature(
  55. 'pictures',
  56. 'c',
  57. 'r',
  58. '2009-02-09',
  59. '2009-02-10',
  60. 'YWJjZGVmZw=='
  61. );
  62. $this->assertEquals('TEfqYYiY9Qrb7fH7nhiRCP9o5BzfO/VL8oYgfVpUl6s=', $result);
  63. }
  64. /**
  65. * Test signing a blob
  66. */
  67. public function testGenerateSignatureBlob()
  68. {
  69. $credentials = new Zend_Service_WindowsAzure_Credentials_SharedAccessSignature('myaccount', 'WXuEUKMijV/pxUu5/RhDn1bYRuFlLSbmLUJJWRqYQ/uxbMpEx+7S/jo9sT3ZIkEucZGbEafDuxD1kwFOXf3xyw==', false);
  70. $result = $credentials->createSignature(
  71. 'pictures/blob.txt',
  72. 'b',
  73. 'r',
  74. '2009-08-14T11:03:40Z',
  75. '2009-08-14T11:53:40Z'
  76. );
  77. $this->assertEquals('hk78uZGGWd8B2NYoBwKSPs5gen3xYqsd3DPO8BQhgTU=', $result);
  78. }
  79. /**
  80. * Test container signed query string
  81. */
  82. public function testContainerSignedQueryString()
  83. {
  84. $credentials = new Zend_Service_WindowsAzure_Credentials_SharedAccessSignature('myaccount', '', false);
  85. $result = $credentials->createSignedQueryString(
  86. 'pictures',
  87. '',
  88. 'c',
  89. 'r',
  90. '2009-02-09',
  91. '2009-02-10',
  92. 'YWJjZGVmZw=='
  93. );
  94. $this->assertEquals('st=2009-02-09&se=2009-02-10&sr=c&sp=r&si=YWJjZGVmZw%3D%3D&sig=iLe%2BC%2Be85l8%2BMneC9psdTCg7hJxKh314aRq3SnqPuyM%3D', $result);
  95. }
  96. /**
  97. * Test blob signed query string
  98. */
  99. public function testBlobSignedQueryString()
  100. {
  101. $credentials = new Zend_Service_WindowsAzure_Credentials_SharedAccessSignature('myaccount', '', false);
  102. $result = $credentials->createSignedQueryString(
  103. 'pictures/blob.txt',
  104. '',
  105. 'b',
  106. 'w',
  107. '2009-02-09',
  108. '2009-02-10'
  109. );
  110. $this->assertEquals('st=2009-02-09&se=2009-02-10&sr=b&sp=w&sig=MUrHltHOJkj4425gorWWKr%2FO6mHC3XeRQ2MD6jn8jI8%3D', $result);
  111. }
  112. /**
  113. * Test sign request URL
  114. */
  115. public function testSignRequestUrl()
  116. {
  117. $credentials = new Zend_Service_WindowsAzure_Credentials_SharedAccessSignature('myaccount', '', false);
  118. $queryString = $credentials->createSignedQueryString('pictures/blob.txt', '', 'b', 'r', '2009-02-09', '2009-02-10');
  119. $credentials->setPermissionSet(array(
  120. 'http://blob.core.windows.net/myaccount/pictures/blob.txt?' . $queryString
  121. ));
  122. $requestUrl = 'http://blob.core.windows.net/myaccount/pictures/blob.txt?comp=metadata';
  123. $result = $credentials->signRequestUrl($requestUrl, Zend_Service_WindowsAzure_Storage::RESOURCE_BLOB);
  124. $this->assertEquals('http://blob.core.windows.net/myaccount/pictures/blob.txt?comp=metadata&' . $queryString, $result);
  125. }
  126. }
  127. // Call Zend_Service_WindowsAzure_Credentials_SharedAccessSignatureTest::main() if this source file is executed directly.
  128. if (PHPUnit_MAIN_METHOD == "Zend_Service_WindowsAzure_Credentials_SharedAccessSignatureTest::main") {
  129. Zend_Service_WindowsAzure_Credentials_SharedAccessSignatureTest::main();
  130. }