Skip to content

Commit

Permalink
add testcase for Credentials.
Browse files Browse the repository at this point in the history
  • Loading branch information
huiguangjun committed Aug 3, 2022
1 parent 048a24d commit 9639da4
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions tests/OSS/Tests/OssClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 9639da4

Please sign in to comment.