AuthTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // Hack to override the time returned from the S3SignatureV4
  3. // @codingStandardsIgnoreStart
  4. namespace Qiniu {
  5. function time()
  6. {
  7. return isset($_SERVER['override_qiniu_auth_time'])
  8. ? 1234567890
  9. : \time();
  10. }
  11. }
  12. namespace Qiniu\Tests {
  13. use Qiniu\Auth;
  14. // @codingStandardsIgnoreEnd
  15. class AuthTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testSign()
  18. {
  19. global $dummyAuth;
  20. $token = $dummyAuth->sign('test');
  21. $this->assertEquals('abcdefghklmnopq:mSNBTR7uS2crJsyFr2Amwv1LaYg=', $token);
  22. }
  23. public function testSignWithData()
  24. {
  25. global $dummyAuth;
  26. $token = $dummyAuth->signWithData('test');
  27. $this->assertEquals('abcdefghklmnopq:-jP8eEV9v48MkYiBGs81aDxl60E=:dGVzdA==', $token);
  28. }
  29. public function testSignRequest()
  30. {
  31. global $dummyAuth;
  32. $token = $dummyAuth->signRequest('http://www.qiniu.com?go=1', 'test', '');
  33. $this->assertEquals('abcdefghklmnopq:cFyRVoWrE3IugPIMP5YJFTO-O-Y=', $token);
  34. $ctype = 'application/x-www-form-urlencoded';
  35. $token = $dummyAuth->signRequest('http://www.qiniu.com?go=1', 'test', $ctype);
  36. $this->assertEquals($token, 'abcdefghklmnopq:svWRNcacOE-YMsc70nuIYdaa1e4=');
  37. }
  38. public function testPrivateDownloadUrl()
  39. {
  40. global $dummyAuth;
  41. $_SERVER['override_qiniu_auth_time'] = true;
  42. $url = $dummyAuth->privateDownloadUrl('http://www.qiniu.com?go=1');
  43. $expect = 'http://www.qiniu.com?go=1&e=1234571490&token=abcdefghklmnopq:8vzBeLZ9W3E4kbBLFLW0Xe0u7v4=';
  44. $this->assertEquals($expect, $url);
  45. unset($_SERVER['override_qiniu_auth_time']);
  46. }
  47. public function testUploadToken()
  48. {
  49. global $dummyAuth;
  50. $_SERVER['override_qiniu_auth_time'] = true;
  51. $token = $dummyAuth->uploadToken('1', '2', 3600, array('endUser' => 'y'));
  52. // @codingStandardsIgnoreStart
  53. $exp = 'abcdefghklmnopq:yyeexeUkPOROoTGvwBjJ0F0VLEo=:eyJlbmRVc2VyIjoieSIsInNjb3BlIjoiMToyIiwiZGVhZGxpbmUiOjEyMzQ1NzE0OTB9';
  54. // @codingStandardsIgnoreEnd
  55. $this->assertEquals($exp, $token);
  56. unset($_SERVER['override_qiniu_auth_time']);
  57. }
  58. public function testVerifyCallback()
  59. {
  60. }
  61. }
  62. }