From 5bf510bba1ef0b3889be22f027b2a54e6ab4f97c Mon Sep 17 00:00:00 2001
From: yang-g <yangg@google.com>
Date: Tue, 14 Jul 2015 10:54:29 -0700
Subject: [PATCH] add per_rpc_creds test case in interop test

---
 test/cpp/interop/client.cc         |  7 +++++-
 test/cpp/interop/interop_client.cc | 35 ++++++++++++++++++++++++++++++
 test/cpp/interop/interop_client.h  |  3 +++
 3 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/test/cpp/interop/client.cc b/test/cpp/interop/client.cc
index 1f1e6c1306..d0393fafb2 100644
--- a/test/cpp/interop/client.cc
+++ b/test/cpp/interop/client.cc
@@ -69,6 +69,7 @@ DEFINE_string(test_case, "large_unary",
               "compute_engine_creds: large_unary with compute engine auth; "
               "jwt_token_creds: large_unary with JWT token auth; "
               "oauth2_auth_token: raw oauth2 access token auth; "
+              "per_rpc_creds: raw oauth2 access token on a single rpc; "
               "all : all of above.");
 DEFINE_string(default_service_account, "",
               "Email of GCE default service account");
@@ -117,6 +118,9 @@ int main(int argc, char** argv) {
   } else if (FLAGS_test_case == "oauth2_auth_token") {
     grpc::string json_key = GetServiceAccountJsonKey();
     client.DoOauth2AuthToken(json_key, FLAGS_oauth_scope);
+  } else if (FLAGS_test_case == "per_rpc_creds") {
+    grpc::string json_key = GetServiceAccountJsonKey();
+    client.DoPerRpcCreds(json_key, FLAGS_oauth_scope);
   } else if (FLAGS_test_case == "all") {
     client.DoEmpty();
     client.DoLargeUnary();
@@ -133,6 +137,7 @@ int main(int argc, char** argv) {
       client.DoServiceAccountCreds(json_key, FLAGS_oauth_scope);
       client.DoJwtTokenCreds(json_key);
       client.DoOauth2AuthToken(json_key, FLAGS_oauth_scope);
+      client.DoPerRpcCreds(json_key, FLAGS_oauth_scope);
     }
     // compute_engine_creds only runs in GCE.
   } else {
@@ -142,7 +147,7 @@ int main(int argc, char** argv) {
         "large_unary|client_streaming|server_streaming|half_duplex|ping_pong|"
         "cancel_after_begin|cancel_after_first_response|"
         "timeout_on_sleeping_server|service_account_creds|compute_engine_creds|"
-        "jwt_token_creds|oauth2_auth_token",
+        "jwt_token_creds|oauth2_auth_token|per_rpc_creds",
         FLAGS_test_case.c_str());
     ret = 1;
   }
diff --git a/test/cpp/interop/interop_client.cc b/test/cpp/interop/interop_client.cc
index 30056e26ab..92bc872f01 100644
--- a/test/cpp/interop/interop_client.cc
+++ b/test/cpp/interop/interop_client.cc
@@ -41,8 +41,10 @@
 #include <grpc/support/log.h>
 #include <grpc++/channel_interface.h>
 #include <grpc++/client_context.h>
+#include <grpc++/credentials.h>
 #include <grpc++/status.h>
 #include <grpc++/stream.h>
+#include "test/cpp/interop/client_helper.h"
 #include "test/proto/test.grpc.pb.h"
 #include "test/proto/empty.grpc.pb.h"
 #include "test/proto/messages.grpc.pb.h"
@@ -160,6 +162,39 @@ void InteropClient::DoOauth2AuthToken(const grpc::string& username,
   gpr_log(GPR_INFO, "Large unary with oauth2 access token done.");
 }
 
+void InteropClient::DoPerRpcCreds(const grpc::string& username,
+                                  const grpc::string& oauth_scope) {
+  gpr_log(GPR_INFO,
+          "Sending a large unary rpc with per-rpc raw oauth2 access token ...");
+  SimpleRequest request;
+  SimpleResponse response;
+  request.set_fill_username(true);
+  request.set_fill_oauth_scope(true);
+  std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
+
+  ClientContext context;
+  grpc::string access_token = GetOauth2AccessToken();
+  std::shared_ptr<Credentials> creds = AccessTokenCredentials(access_token);
+  context.set_credentials(creds);
+  request.set_response_type(PayloadType::COMPRESSABLE);
+  request.set_response_size(kLargeResponseSize);
+  grpc::string payload(kLargeRequestSize, '\0');
+  request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
+
+  Status s = stub->UnaryCall(&context, request, &response);
+
+  AssertOkOrPrintErrorStatus(s);
+  GPR_ASSERT(response.payload().type() == PayloadType::COMPRESSABLE);
+  GPR_ASSERT(response.payload().body() ==
+             grpc::string(kLargeResponseSize, '\0'));
+  GPR_ASSERT(!response.username().empty());
+  GPR_ASSERT(!response.oauth_scope().empty());
+  GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
+  const char* oauth_scope_str = response.oauth_scope().c_str();
+  GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
+  gpr_log(GPR_INFO, "Large unary with per-rpc oauth2 access token done.");
+}
+
 void InteropClient::DoJwtTokenCreds(const grpc::string& username) {
   gpr_log(GPR_INFO, "Sending a large unary rpc with JWT token credentials ...");
   SimpleRequest request;
diff --git a/test/cpp/interop/interop_client.h b/test/cpp/interop/interop_client.h
index 67eecd9ccc..bf8188325e 100644
--- a/test/cpp/interop/interop_client.h
+++ b/test/cpp/interop/interop_client.h
@@ -71,6 +71,9 @@ class InteropClient {
   // username is a string containing the user email
   void DoOauth2AuthToken(const grpc::string& username,
                          const grpc::string& oauth_scope);
+  // username is a string containing the user email
+  void DoPerRpcCreds(const grpc::string& username,
+                     const grpc::string& oauth_scope);
 
  private:
   void PerformLargeUnary(SimpleRequest* request, SimpleResponse* response);
-- 
GitLab