xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/client_helper.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_TEST_CPP_INTEROP_CLIENT_HELPER_H
20 #define GRPC_TEST_CPP_INTEROP_CLIENT_HELPER_H
21 
22 #include <functional>
23 #include <memory>
24 #include <unordered_map>
25 
26 #include <grpcpp/channel.h>
27 #include <grpcpp/client_context.h>
28 #include <grpcpp/support/channel_arguments.h>
29 
30 #include "src/core/lib/surface/call_test_only.h"
31 #include "src/core/lib/transport/transport.h"
32 
33 namespace grpc {
34 namespace testing {
35 
36 std::string GetServiceAccountJsonKey();
37 
38 std::string GetOauth2AccessToken();
39 
40 void UpdateActions(
41     std::unordered_map<std::string, std::function<bool()>>* actions);
42 
43 std::shared_ptr<Channel> CreateChannelForTestCase(
44     const std::string& test_case,
45     std::vector<
46         std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
47         interceptor_creators = {},
48     ChannelArguments channel_args = ChannelArguments());
49 
50 class InteropClientContextInspector {
51  public:
InteropClientContextInspector(const grpc::ClientContext & context)52   explicit InteropClientContextInspector(const grpc::ClientContext& context)
53       : context_(context) {}
54 
55   // Inspector methods, able to peek inside ClientContext, follow.
GetCallCompressionAlgorithm()56   grpc_compression_algorithm GetCallCompressionAlgorithm() const {
57     return grpc_call_test_only_get_compression_algorithm(context_.call_);
58   }
59 
WasCompressed()60   bool WasCompressed() const {
61     return (grpc_call_test_only_get_message_flags(context_.call_) &
62             GRPC_WRITE_INTERNAL_COMPRESS) ||
63            (grpc_call_test_only_get_message_flags(context_.call_) &
64             GRPC_WRITE_INTERNAL_TEST_ONLY_WAS_COMPRESSED);
65   }
66 
67  private:
68   const grpc::ClientContext& context_;
69 };
70 
71 class AdditionalMetadataInterceptor : public experimental::Interceptor {
72  public:
AdditionalMetadataInterceptor(std::multimap<std::string,std::string> additional_metadata)73   explicit AdditionalMetadataInterceptor(
74       std::multimap<std::string, std::string> additional_metadata)
75       : additional_metadata_(std::move(additional_metadata)) {}
76 
Intercept(experimental::InterceptorBatchMethods * methods)77   void Intercept(experimental::InterceptorBatchMethods* methods) override {
78     if (methods->QueryInterceptionHookPoint(
79             experimental::InterceptionHookPoints::PRE_SEND_INITIAL_METADATA)) {
80       std::multimap<std::string, std::string>* metadata =
81           methods->GetSendInitialMetadata();
82       for (const auto& entry : additional_metadata_) {
83         metadata->insert(entry);
84       }
85     }
86     methods->Proceed();
87   }
88 
89  private:
90   const std::multimap<std::string, std::string> additional_metadata_;
91 };
92 
93 class AdditionalMetadataInterceptorFactory
94     : public experimental::ClientInterceptorFactoryInterface {
95  public:
AdditionalMetadataInterceptorFactory(std::multimap<std::string,std::string> additional_metadata)96   explicit AdditionalMetadataInterceptorFactory(
97       std::multimap<std::string, std::string> additional_metadata)
98       : additional_metadata_(std::move(additional_metadata)) {}
99 
CreateClientInterceptor(experimental::ClientRpcInfo *)100   experimental::Interceptor* CreateClientInterceptor(
101       experimental::ClientRpcInfo* /*info*/) override {
102     return new AdditionalMetadataInterceptor(additional_metadata_);
103   }
104 
105   const std::multimap<std::string, std::string> additional_metadata_;
106 };
107 
108 class MetadataAndStatusLoggerInterceptor : public experimental::Interceptor {
109  public:
MetadataAndStatusLoggerInterceptor()110   explicit MetadataAndStatusLoggerInterceptor() {}
111 
112   void Intercept(experimental::InterceptorBatchMethods* methods) override;
113 };
114 
115 class MetadataAndStatusLoggerInterceptorFactory
116     : public experimental::ClientInterceptorFactoryInterface {
117  public:
MetadataAndStatusLoggerInterceptorFactory()118   explicit MetadataAndStatusLoggerInterceptorFactory() {}
119 
CreateClientInterceptor(experimental::ClientRpcInfo *)120   experimental::Interceptor* CreateClientInterceptor(
121       experimental::ClientRpcInfo* /*info*/) override {
122     return new MetadataAndStatusLoggerInterceptor();
123   }
124 };
125 
126 }  // namespace testing
127 }  // namespace grpc
128 
129 #endif  // GRPC_TEST_CPP_INTEROP_CLIENT_HELPER_H
130