From 9639da4a89af02acd6d5a325bc580bf978e91600 Mon Sep 17 00:00:00 2001 From: "guangjun.hgj" Date: Fri, 29 Jul 2022 14:22:56 +0800 Subject: [PATCH] add testcase for Credentials. --- tests/OSS/Tests/OssClientTest.php | 148 ++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/tests/OSS/Tests/OssClientTest.php b/tests/OSS/Tests/OssClientTest.php index 247251a9..0d203713 100644 --- a/tests/OSS/Tests/OssClientTest.php +++ b/tests/OSS/Tests/OssClientTest.php @@ -4,6 +4,79 @@ use OSS\Core\OssException; use OSS\OssClient; +use OSS\Credentials\Credentials; +use OSS\Credentials\CredentialsProvider; +use OSS\Credentials\StaticCredentialsProvider; + +class TestEmptyIdCredentials extends Credentials +{ + public function __construct() + { + } + + public function getAccessKeyId() + { + return ''; + } + + public function getAccessKeySecret() + { + return 'secret'; + } + + public function getSecurityToken() + { + return null; + } +} + +class TestEmptySecretCredentials extends Credentials +{ + public function __construct() + { + } + + public function getAccessKeyId() + { + return 'id'; + } + + public function getAccessKeySecret() + { + return ''; + } + + public function getSecurityToken() + { + return null; + } +} + + +class TestCredentialsProvider implements CredentialsProvider +{ + private $credentials; + public function __construct($flag) + { + if ($flag == 2) { + $this->credentials = new TestEmptyIdCredentials(); + } + else if ($flag == 1) { + $this->credentials = new TestEmptySecretCredentials(); + } + else { + $this->credentials = null; + } + } + + /** + * @return Credentials + */ + public function getCredentials() + { + return $this->credentials; + } +} class OssClientTest extends TestOssClientBase @@ -343,4 +416,79 @@ public function testStsToken() $this->assertFalse(true); } } + + public function testEmptyCredentials() + { + // empty case, should throw exception + try { + $id = ''; + $secret = 'accessKey_secret'; + $provider = new StaticCredentialsProvider($id, $secret); + $config = array( + 'provider' => $provider, + 'endpoint'=>'http://oss-cn-hangzhou.aliyuncs.com' + ); + $ossClient = new OssClient($config); + $this->assertFalse(true); + } catch (OssException $e) { + $this->assertEquals('access key id is empty', $e->getMessage()); + } + + // empty case, should throw exception + try { + $id = 'id'; + $secret = ''; + $provider = new StaticCredentialsProvider($id, $secret); + $config = array( + 'provider' => $provider, + 'endpoint'=>'http://oss-cn-hangzhou.aliyuncs.com' + ); + $ossClient = new OssClient($config); + $this->assertFalse(true); + } catch (OssException $e) { + $this->assertEquals('access key secret is empty', $e->getMessage()); + } + + // empty case, should throw exception + try { + $provider = new TestCredentialsProvider(0); + $config = array( + 'provider' => $provider, + 'endpoint'=>'http://oss-cn-hangzhou.aliyuncs.com' + ); + $ossClient = new OssClient($config); + $ossClient->getBucketAcl("bucket"); + $this->assertFalse(true); + } catch (OssException $e) { + $this->assertEquals('credentials is empty.', $e->getMessage()); + } + + // empty case, should throw exception + try { + $provider = new TestCredentialsProvider(1); + $config = array( + 'provider' => $provider, + 'endpoint'=>'http://oss-cn-hangzhou.aliyuncs.com' + ); + $ossClient = new OssClient($config); + $ossClient->getBucketAcl("bucket"); + $this->assertFalse(true); + } catch (OssException $e) { + $this->assertEquals('access key secret is empty', $e->getMessage()); + } + + // empty case, should throw exception + try { + $provider = new TestCredentialsProvider(2); + $config = array( + 'provider' => $provider, + 'endpoint'=>'http://oss-cn-hangzhou.aliyuncs.com' + ); + $ossClient = new OssClient($config); + $ossClient->getBucketAcl("bucket"); + $this->assertFalse(true); + } catch (OssException $e) { + $this->assertEquals('access key id is empty', $e->getMessage()); + } + } }