|
4 | 4 |
|
5 | 5 | use OSS\Core\OssException;
|
6 | 6 | use OSS\OssClient;
|
| 7 | +use OSS\Credentials\Credentials; |
| 8 | +use OSS\Credentials\CredentialsProvider; |
| 9 | +use OSS\Credentials\StaticCredentialsProvider; |
| 10 | + |
| 11 | +class TestEmptyIdCredentials extends Credentials |
| 12 | +{ |
| 13 | + public function __construct() |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + public function getAccessKeyId() |
| 18 | + { |
| 19 | + return ''; |
| 20 | + } |
| 21 | + |
| 22 | + public function getAccessKeySecret() |
| 23 | + { |
| 24 | + return 'secret'; |
| 25 | + } |
| 26 | + |
| 27 | + public function getSecurityToken() |
| 28 | + { |
| 29 | + return null; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class TestEmptySecretCredentials extends Credentials |
| 34 | +{ |
| 35 | + public function __construct() |
| 36 | + { |
| 37 | + } |
| 38 | + |
| 39 | + public function getAccessKeyId() |
| 40 | + { |
| 41 | + return 'id'; |
| 42 | + } |
| 43 | + |
| 44 | + public function getAccessKeySecret() |
| 45 | + { |
| 46 | + return ''; |
| 47 | + } |
| 48 | + |
| 49 | + public function getSecurityToken() |
| 50 | + { |
| 51 | + return null; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +class TestCredentialsProvider implements CredentialsProvider |
| 57 | +{ |
| 58 | + private $credentials; |
| 59 | + public function __construct($flag) |
| 60 | + { |
| 61 | + if ($flag == 2) { |
| 62 | + $this->credentials = new TestEmptyIdCredentials(); |
| 63 | + } |
| 64 | + else if ($flag == 1) { |
| 65 | + $this->credentials = new TestEmptySecretCredentials(); |
| 66 | + } |
| 67 | + else { |
| 68 | + $this->credentials = null; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return Credentials |
| 74 | + */ |
| 75 | + public function getCredentials() |
| 76 | + { |
| 77 | + return $this->credentials; |
| 78 | + } |
| 79 | +} |
7 | 80 |
|
8 | 81 |
|
9 | 82 | class OssClientTest extends TestOssClientBase
|
@@ -343,4 +416,79 @@ public function testStsToken()
|
343 | 416 | $this->assertFalse(true);
|
344 | 417 | }
|
345 | 418 | }
|
| 419 | + |
| 420 | + public function testEmptyCredentials() |
| 421 | + { |
| 422 | + // empty case, should throw exception |
| 423 | + try { |
| 424 | + $id = ''; |
| 425 | + $secret = 'accessKey_secret'; |
| 426 | + $provider = new StaticCredentialsProvider($id, $secret); |
| 427 | + $config = array( |
| 428 | + 'provider' => $provider, |
| 429 | + 'endpoint'=>'http://oss-cn-hangzhou.aliyuncs.com' |
| 430 | + ); |
| 431 | + $ossClient = new OssClient($config); |
| 432 | + $this->assertFalse(true); |
| 433 | + } catch (OssException $e) { |
| 434 | + $this->assertEquals('access key id is empty', $e->getMessage()); |
| 435 | + } |
| 436 | + |
| 437 | + // empty case, should throw exception |
| 438 | + try { |
| 439 | + $id = 'id'; |
| 440 | + $secret = ''; |
| 441 | + $provider = new StaticCredentialsProvider($id, $secret); |
| 442 | + $config = array( |
| 443 | + 'provider' => $provider, |
| 444 | + 'endpoint'=>'http://oss-cn-hangzhou.aliyuncs.com' |
| 445 | + ); |
| 446 | + $ossClient = new OssClient($config); |
| 447 | + $this->assertFalse(true); |
| 448 | + } catch (OssException $e) { |
| 449 | + $this->assertEquals('access key secret is empty', $e->getMessage()); |
| 450 | + } |
| 451 | + |
| 452 | + // empty case, should throw exception |
| 453 | + try { |
| 454 | + $provider = new TestCredentialsProvider(0); |
| 455 | + $config = array( |
| 456 | + 'provider' => $provider, |
| 457 | + 'endpoint'=>'http://oss-cn-hangzhou.aliyuncs.com' |
| 458 | + ); |
| 459 | + $ossClient = new OssClient($config); |
| 460 | + $ossClient->getBucketAcl("bucket"); |
| 461 | + $this->assertFalse(true); |
| 462 | + } catch (OssException $e) { |
| 463 | + $this->assertEquals('credentials is empty.', $e->getMessage()); |
| 464 | + } |
| 465 | + |
| 466 | + // empty case, should throw exception |
| 467 | + try { |
| 468 | + $provider = new TestCredentialsProvider(1); |
| 469 | + $config = array( |
| 470 | + 'provider' => $provider, |
| 471 | + 'endpoint'=>'http://oss-cn-hangzhou.aliyuncs.com' |
| 472 | + ); |
| 473 | + $ossClient = new OssClient($config); |
| 474 | + $ossClient->getBucketAcl("bucket"); |
| 475 | + $this->assertFalse(true); |
| 476 | + } catch (OssException $e) { |
| 477 | + $this->assertEquals('access key secret is empty', $e->getMessage()); |
| 478 | + } |
| 479 | + |
| 480 | + // empty case, should throw exception |
| 481 | + try { |
| 482 | + $provider = new TestCredentialsProvider(2); |
| 483 | + $config = array( |
| 484 | + 'provider' => $provider, |
| 485 | + 'endpoint'=>'http://oss-cn-hangzhou.aliyuncs.com' |
| 486 | + ); |
| 487 | + $ossClient = new OssClient($config); |
| 488 | + $ossClient->getBucketAcl("bucket"); |
| 489 | + $this->assertFalse(true); |
| 490 | + } catch (OssException $e) { |
| 491 | + $this->assertEquals('access key id is empty', $e->getMessage()); |
| 492 | + } |
| 493 | + } |
346 | 494 | }
|
0 commit comments