Просмотр исходного кода

add more unit test for location and sts

zhangzifa 7 лет назад
Родитель
Сommit
043df1b674
5 измененных файлов с 228 добавлено и 118 удалено
  1. 155 114
      test/core/commonclient_ut.cc
  2. 4 0
      test/core/error_ut.cc
  3. 28 3
      test/core/locationclient_ut.cc
  4. 1 1
      test/core/servicerequest_ut.cc
  5. 40 0
      test/core/stsclient_ut.cc

+ 155 - 114
test/core/commonclient_ut.cc

@@ -25,6 +25,7 @@ namespace AlibabaCloud {
       using CommonClient::buildRpcHttpRequest;
       using CommonClient::buildRpcHttpRequest;
       using CommonClient::makeRequest;
       using CommonClient::makeRequest;
       using CommonClient::asyncExecute;
       using CommonClient::asyncExecute;
+      using CommonClient::commonResponseAsync;
   };
   };
 }
 }
 
 
@@ -33,117 +34,157 @@ static void task() {
    nbr |= 0x01;
    nbr |= 0x01;
 }
 }
 
 
-
-TEST(CommonClient, basic) {
-
-  InitializeSdk();
-  const ClientConfiguration cfg;
-
-  std::string key    = "accessKeyId";
-  std::string secret = "accessSecret";
-  std::string token  = "sessionToken";
-
-  const Credentials credentials(key, secret, token);
-
-  const CommonClient cc1(credentials, cfg);
-  CommonClient cc2(std::make_shared<SimpleCredentialsProvider>(credentials), cfg);
-  CommonClient cc3(key, secret, cfg);
-
-  CommonRequest cr;
-  cr.setContent("test-content", 12);
-
-  CommonClient::CommonResponseOutcome out1 = cc1.commonResponse(cr);
-  EXPECT_TRUE(out1.error().errorCode() == "NetworkError");
-  EXPECT_TRUE(out1.result().payload() == "");
-
-  CommonClient::CommonResponseOutcome out2 = cc2.commonResponse(cr);
-  EXPECT_TRUE(out2.error().errorCode() == "NetworkError");
-  EXPECT_TRUE(out2.result().payload() == "");
-
-  CommonClient::CommonResponseOutcome out3 = cc3.commonResponse(cr);
-  EXPECT_TRUE(out3.error().errorCode() == "NetworkError");
-  EXPECT_TRUE(out3.result().payload() == "");
-
-/*
-https://cn-hangzhou/?
-   AccessKeyId=accessKeyId
-  &Format=JSON
-  &RegionId=cn-hangzhou
-  &SecurityToken=sessionToken
-  &Signature=tWGagYMK93km44TOY%2FY60snVYWE%3D
-  &SignatureMethod=HMAC-SHA1
-  &SignatureNonce=ef27311a-5151-40a0-974f-2b808a8a683e
-  &SignatureVersion=1.0
-  &Timestamp=2019-01-02T08%3A56%3A26Z
-  &Version=
-*/
-
-  TestCommonClient* client = new TestCommonClient(credentials, cfg);
-  HttpRequest r = client->buildHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
-  EXPECT_TRUE(r.method() == HttpRequest::Method::Post);
-  EXPECT_TRUE(r.url().scheme() == "https");
-  EXPECT_TRUE(r.url().userName() == "");
-  EXPECT_TRUE(r.url().password() == "");
-  EXPECT_TRUE(r.url().host() == "cn-hangzhou");
-  EXPECT_TRUE(r.url().port() == -1);
-  EXPECT_TRUE(r.url().fragment() == "");
-
-  EXPECT_TRUE(r.url().query().find("AccessKeyId=") != string::npos);
-  EXPECT_TRUE(r.url().query().find("Format=") != string::npos);
-  EXPECT_TRUE(r.url().query().find("RegionId=") != string::npos);
-  EXPECT_TRUE(r.url().query().find("SecurityToken=") != string::npos);
-  EXPECT_TRUE(r.url().query().find("Signature=") != string::npos);
-  EXPECT_TRUE(r.url().query().find("SignatureMethod=") != string::npos);
-  EXPECT_TRUE(r.url().query().find("SignatureNonce=") != string::npos);
-  EXPECT_TRUE(r.url().query().find("SignatureVersion=") != string::npos);
-  EXPECT_TRUE(r.url().query().find("Timestamp=") != string::npos);
-  EXPECT_TRUE(r.url().query().find("Version=") != string::npos);
-
-  cr.setQueryParameter("query_k1", "query_v1");
-  cr.setHeaderParameter("header_k1", "header_v1");
-  cr.setScheme("");
-  cr.setHeaderParameter("Accept", "");
-  HttpRequest rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
-  EXPECT_TRUE(rr.method() == HttpRequest::Method::Get);
-  EXPECT_TRUE(rr.header("Accept") == "application/json");
-  EXPECT_TRUE(rr.url().toString() == "https://cn-shanghai/?header_k1=header_v1");
-  EXPECT_TRUE(rr.header("Host") == "cn-shanghai");
-  // Wed, 09 Jan 2019 06:32:41 GMT
-  string date = "Wed, 09 Jan 2019 06:32:41 GMT";
-  EXPECT_TRUE(rr.header("Date").length() == date.length());
-  string signature_nonce = "fb62c51d-f735-4ec7-870d-c6ce03368214";
-  EXPECT_TRUE(rr.header("x-acs-signature-nonce").length() == signature_nonce.length());
-
-  EXPECT_TRUE(rr.header("x-acs-signature-method") == "HMAC-SHA1");
-  EXPECT_TRUE(rr.header("x-acs-signature-version") == "1.0");
-  EXPECT_TRUE(rr.header("x-acs-version") == "");
-  EXPECT_TRUE(rr.header("x-sdk-client") == string("CPP/").append(ALIBABACLOUD_VERSION_STR));
-  EXPECT_TRUE(rr.header("x-acs-region-id") == "cn-hangzhou");
-  // acs accessKeyId:JZD81jGWLp1F3ZIkaLp1yuEZmKc=
-  EXPECT_TRUE(rr.header("Authorization").find("acs accessKeyId:") != string::npos);
-  EXPECT_TRUE(rr.header("unknown-header") == "");
-
-  cr.setScheme("http");
-  cr.setHeaderParameter("Accept", "test-accept");
-  cr.setHeaderParameter("Content-Type", "test-content-type");
-  rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
-  EXPECT_TRUE(rr.url().toString() == "http://cn-shanghai/?Accept=test-accept&Content-Type=test-content-type&header_k1=header_v1");
-  EXPECT_TRUE(rr.header("Accept") == "test-accept");
-  EXPECT_TRUE(rr.header("Content-Type") == "test-content-type");
-
-  HttpRequest rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
-  EXPECT_TRUE(client->serviceName() == "Common");
-  EXPECT_TRUE(rrr.url().scheme() == "http");
-
-  cr.setScheme("");
-  rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
-  EXPECT_TRUE(rrr.url().scheme() == "https");
-
-  std::function<void()> func(task);
-  Runnable* rf = new Runnable(func);
-  EXPECT_TRUE(nbr == 0);
-  client->asyncExecute(rf);
-  usleep(10000);
-  EXPECT_TRUE(nbr == 1);
-  ShutdownSdk();
-}
+namespace {
+
+  TEST(CommonClient, basic) {
+    InitializeSdk();
+    const ClientConfiguration cfg;
+
+    std::string key    = "accessKeyId";
+    std::string secret = "accessSecret";
+    std::string token  = "sessionToken";
+
+    const Credentials credentials(key, secret, token);
+
+    const CommonClient cc1(credentials, cfg);
+    CommonClient cc2(std::make_shared<SimpleCredentialsProvider>(credentials), cfg);
+    CommonClient cc3(key, secret, cfg);
+    CommonClient cc4(key, secret, cfg);
+
+    CommonRequest cr;
+    cr.setContent("test-content", 12);
+
+    CommonClient::CommonResponseOutcome out1 = cc1.commonResponse(cr);
+    EXPECT_TRUE(out1.error().errorCode() == "NetworkError");
+    EXPECT_TRUE(out1.result().payload() == "");
+
+    CommonClient::CommonResponseOutcome out2 = cc2.commonResponse(cr);
+    EXPECT_TRUE(out2.error().errorCode() == "NetworkError");
+    EXPECT_TRUE(out2.result().payload() == "");
+
+    CommonClient::CommonResponseOutcome out3 = cc3.commonResponse(cr);
+    EXPECT_TRUE(out3.error().errorCode() == "NetworkError");
+    EXPECT_TRUE(out3.result().payload() == "");
+
+    cr.setRequestPattern(CommonRequest::RequestPattern::RoaPattern);
+    CommonClient::CommonResponseOutcome out4 = cc4.commonResponse(cr);
+    EXPECT_TRUE(out4.error().errorCode() == "NetworkError");
+    EXPECT_TRUE(out4.result().payload() == "");
+
+  /*
+  https://cn-hangzhou/?
+     AccessKeyId=accessKeyId
+    &Format=JSON
+    &RegionId=cn-hangzhou
+    &SecurityToken=sessionToken
+    &Signature=tWGagYMK93km44TOY%2FY60snVYWE%3D
+    &SignatureMethod=HMAC-SHA1
+    &SignatureNonce=ef27311a-5151-40a0-974f-2b808a8a683e
+    &SignatureVersion=1.0
+    &Timestamp=2019-01-02T08%3A56%3A26Z
+    &Version=
+  */
+    cr.setRequestPattern(CommonRequest::RequestPattern::RpcPattern);
+    TestCommonClient* client = new TestCommonClient(credentials, cfg);
+    HttpRequest r = client->buildHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
+    EXPECT_TRUE(r.method() == HttpRequest::Method::Post);
+    EXPECT_TRUE(r.url().scheme() == "https");
+    EXPECT_TRUE(r.url().userName() == "");
+    EXPECT_TRUE(r.url().password() == "");
+    EXPECT_TRUE(r.url().host() == "cn-hangzhou");
+    EXPECT_TRUE(r.url().port() == -1);
+    EXPECT_TRUE(r.url().fragment() == "");
+    EXPECT_TRUE(r.url().query().find("AccessKeyId=") != string::npos);
+    EXPECT_TRUE(r.url().query().find("Format=") != string::npos);
+    EXPECT_TRUE(r.url().query().find("RegionId=") != string::npos);
+    EXPECT_TRUE(r.url().query().find("SecurityToken=") != string::npos);
+    EXPECT_TRUE(r.url().query().find("Signature=") != string::npos);
+    EXPECT_TRUE(r.url().query().find("SignatureMethod=") != string::npos);
+    EXPECT_TRUE(r.url().query().find("SignatureNonce=") != string::npos);
+    EXPECT_TRUE(r.url().query().find("SignatureVersion=") != string::npos);
+    EXPECT_TRUE(r.url().query().find("Timestamp=") != string::npos);
+    EXPECT_TRUE(r.url().query().find("Version=") != string::npos);
+
+    cr.setQueryParameter("query_k1", "query_v1");
+    cr.setHeaderParameter("header_k1", "header_v1");
+    cr.setScheme("");
+    cr.setHeaderParameter("Accept", "");
+    HttpRequest rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
+    EXPECT_TRUE(rr.method() == HttpRequest::Method::Get);
+    EXPECT_TRUE(rr.header("Accept") == "application/json");
+    EXPECT_TRUE(rr.url().toString() == "https://cn-shanghai/?header_k1=header_v1");
+    EXPECT_TRUE(rr.header("Host") == "cn-shanghai");
+    // Wed, 09 Jan 2019 06:32:41 GMT
+    string date = "Wed, 09 Jan 2019 06:32:41 GMT";
+    EXPECT_TRUE(rr.header("Date").length() == date.length());
+    string signature_nonce = "fb62c51d-f735-4ec7-870d-c6ce03368214";
+    EXPECT_TRUE(rr.header("x-acs-signature-nonce").length() == signature_nonce.length());
+
+    EXPECT_TRUE(rr.header("x-acs-signature-method") == "HMAC-SHA1");
+    EXPECT_TRUE(rr.header("x-acs-signature-version") == "1.0");
+    EXPECT_TRUE(rr.header("x-acs-version") == "");
+    EXPECT_TRUE(rr.header("x-sdk-client") == string("CPP/").append(ALIBABACLOUD_VERSION_STR));
+    EXPECT_TRUE(rr.header("x-acs-region-id") == "cn-hangzhou");
+    // acs accessKeyId:JZD81jGWLp1F3ZIkaLp1yuEZmKc=
+    EXPECT_TRUE(rr.header("Authorization").find("acs accessKeyId:") != string::npos);
+    EXPECT_TRUE(rr.header("unknown-header") == "");
+
+    cr.setScheme("http");
+    cr.setHeaderParameter("Accept", "test-accept");
+    cr.setHeaderParameter("Content-Type", "test-content-type");
+    rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
+    EXPECT_TRUE(rr.url().toString() == "http://cn-shanghai/?Accept=test-accept&Content-Type=test-content-type&header_k1=header_v1");
+    EXPECT_TRUE(rr.header("Accept") == "test-accept");
+    EXPECT_TRUE(rr.header("Content-Type") == "test-content-type");
+
+    HttpRequest rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
+    EXPECT_TRUE(client->serviceName() == "Common");
+    EXPECT_TRUE(rrr.url().scheme() == "http");
+
+    cr.setScheme("");
+    rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
+    EXPECT_TRUE(rrr.url().scheme() == "https");
+
+    std::function<void()> func(task);
+    Runnable* rf = new Runnable(func);
+    EXPECT_TRUE(nbr == 0);
+    client->asyncExecute(rf);
+    usleep(10000);
+    EXPECT_TRUE(nbr == 1);
+    ShutdownSdk();
+  }
+
+
+
+  void cb(const CommonClient *client,
+          const CommonRequest &req,
+          const CommonClient::CommonResponseOutcome& out,
+          const std::shared_ptr<const AsyncCallerContext>& contex) {
+    EXPECT_FALSE(out.error().errorCode().empty());
+  }
+
+  TEST(CommonClient, async) {
+    InitializeSdk();
+    const ClientConfiguration cfg;
+
+    std::string key    = "accessKeyId";
+    std::string secret = "accessSecret";
+    std::string token  = "sessionToken";
+
+    const Credentials credentials(key, secret, token);
+
+    const CommonClient cc1(credentials, cfg);
+
+    CommonRequest cr;
+    cr.setContent("test-content", 12);
+
+    TestCommonClient::CommonResponseAsyncHandler handler(cb);
+
+
+    const AsyncCallerContext context;
+    cc1.commonResponseAsync(cr, handler, std::make_shared<const AsyncCallerContext>(context));
+
+    ShutdownSdk();
+  }
+
+}

+ 4 - 0
test/core/error_ut.cc

@@ -30,4 +30,8 @@ TEST(Error, basic) {
 
 
   EXPECT_TRUE(error.errorCode() == code1);
   EXPECT_TRUE(error.errorCode() == code1);
   EXPECT_TRUE(error.errorMessage() == message1);
   EXPECT_TRUE(error.errorMessage() == message1);
+
+  const string detail = "any error detail";
+  error.setDetail(detail);
+  EXPECT_TRUE(error.detail() == detail);
 }
 }

+ 28 - 3
test/core/locationclient_ut.cc

@@ -132,9 +132,6 @@ namespace {
     EXPECT_TRUE(it != protocols.end());
     EXPECT_TRUE(it != protocols.end());
   }
   }
 
 
-
-
-
   TEST(LocationClient, callable) {
   TEST(LocationClient, callable) {
     InitializeSdk();
     InitializeSdk();
     ClientConfiguration configuration("cn-hangzhou");
     ClientConfiguration configuration("cn-hangzhou");
@@ -171,4 +168,32 @@ namespace {
     EXPECT_TRUE(it != protocols.end());
     EXPECT_TRUE(it != protocols.end());
     ShutdownSdk();
     ShutdownSdk();
   }
   }
+
+
+void cb(const LocationClient *client,
+          const Model::DescribeEndpointsRequest &req,
+          const LocationClient::DescribeEndpointsOutcome& out,
+          const std::shared_ptr<const AsyncCallerContext>& contex) {
+    EXPECT_FALSE(out.error().errorCode().empty());
+  }
+
+  TEST(LocationClient, async) {
+    InitializeSdk();
+
+    ClientConfiguration configuration("cn-hangzhou");
+    Model::DescribeEndpointsRequest req;
+    req.setId("cn-hangzhou");
+    req.setServiceCode("ecs");
+    req.setType("openAPI");
+
+    LocationClient client("key", "secret", configuration);
+    LocationClient::DescribeEndpointsOutcome out;
+
+    const AsyncCallerContext context;
+
+    LocationClient::DescribeEndpointsAsyncHandler handler(cb);
+    client.describeEndpointsAsync(req, handler, std::make_shared<const AsyncCallerContext>(context));
+    ShutdownSdk();
+  }
+
 }
 }

+ 1 - 1
test/core/servicerequest_ut.cc

@@ -32,7 +32,7 @@ namespace {
     using ServiceRequest::setVersion;
     using ServiceRequest::setVersion;
   };
   };
 
 
-  TEST(ServiceRequestTest, basic) {
+  TEST(ServiceRequest, basic) {
 
 
     TestServiceRequest sr1("ECS", "1.0");
     TestServiceRequest sr1("ECS", "1.0");
     TestServiceRequest sr2(sr1);
     TestServiceRequest sr2(sr1);

+ 40 - 0
test/core/stsclient_ut.cc

@@ -15,6 +15,7 @@ using namespace AlibabaCloud;
 using namespace AlibabaCloud::Sts;
 using namespace AlibabaCloud::Sts;
 
 
 namespace {
 namespace {
+
   class mockStsClient : public StsClient {
   class mockStsClient : public StsClient {
    public:
    public:
     mockStsClient(const string& key, const string& secret, const ClientConfiguration& cfg):
     mockStsClient(const string& key, const string& secret, const ClientConfiguration& cfg):
@@ -102,6 +103,25 @@ namespace {
     ShutdownSdk();
     ShutdownSdk();
   }
   }
 
 
+  void cb(const StsClient *client,
+          const Model::GetCallerIdentityRequest &req,
+          const StsClient::GetCallerIdentityOutcome& out,
+          const std::shared_ptr<const AsyncCallerContext>& contex) {
+    EXPECT_FALSE(out.error().errorCode().empty());
+  }
+
+  TEST(StsClient, getCallerIdentity_async) {
+    InitializeSdk();
+    ClientConfiguration configuration("cn-hangzhou");
+    Model::GetCallerIdentityRequest req;
+    StsClient client("key", "secret", configuration);
+    const AsyncCallerContext context;
+
+    StsClient::GetCallerIdentityAsyncHandler handler(cb);
+    client.getCallerIdentityAsync(req, handler, std::make_shared<const AsyncCallerContext>(context));
+    ShutdownSdk();
+  }
+
   TEST(StsClient, assumeRole) {
   TEST(StsClient, assumeRole) {
     ClientConfiguration configuration;
     ClientConfiguration configuration;
     RpcServiceClient::JsonOutcome xout("{\"RequestId\":\"any-request-id\",\"success\":true,\"AssumedRoleUser\":{\"AssumedRoleId\":\"123456789\",\"Arn\":\"any-arn\"},\"Credentials\":{\"AccessKeyId\":\"any-key\",\"AccessKeySecret\":\"any-secret\",\"Expiration\":\"2019-01-15T16:31:10Z\",\"SecurityToken\":\"any-token\"}}");
     RpcServiceClient::JsonOutcome xout("{\"RequestId\":\"any-request-id\",\"success\":true,\"AssumedRoleUser\":{\"AssumedRoleId\":\"123456789\",\"Arn\":\"any-arn\"},\"Credentials\":{\"AccessKeyId\":\"any-key\",\"AccessKeySecret\":\"any-secret\",\"Expiration\":\"2019-01-15T16:31:10Z\",\"SecurityToken\":\"any-token\"}}");
@@ -151,4 +171,24 @@ namespace {
     EXPECT_TRUE(out.error().errorCode() == "any-error-code");
     EXPECT_TRUE(out.error().errorCode() == "any-error-code");
   }
   }
 
 
+  void assumerole_cb(const StsClient *client,
+          const Model::AssumeRoleRequest &req,
+          const StsClient::AssumeRoleOutcome& out,
+          const std::shared_ptr<const AsyncCallerContext>& contex) {
+    EXPECT_FALSE(out.error().errorCode().empty());
+  }
+
+  TEST(StsClient, assumeRole_async) {
+    InitializeSdk();
+    ClientConfiguration configuration("cn-hangzhou");
+    Model::AssumeRoleRequest req;
+    req.setRoleArn("acs:ram::1940345800212929:root");
+    req.setRoleSessionName("test_session");
+    req.setDurationSeconds(1000);
+    StsClient client("key", "secret", configuration);
+    const AsyncCallerContext context;
+    StsClient::AssumeRoleAsyncHandler handler(assumerole_cb);
+    client.assumeRoleAsync(req, handler, std::make_shared<const AsyncCallerContext>(context));
+    ShutdownSdk();
+  }
 }
 }