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_INTEROP_CLIENT_H
20 #define GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H
21 
22 #include <memory>
23 
24 #include <grpc/grpc.h>
25 #include <grpcpp/channel.h>
26 #include "src/proto/grpc/testing/messages.pb.h"
27 #include "src/proto/grpc/testing/test.grpc.pb.h"
28 
29 namespace grpc {
30 namespace testing {
31 
32 // Function pointer for custom checks.
33 typedef std::function<void(const InteropClientContextInspector&,
34                            const SimpleRequest*, const SimpleResponse*)>
35     CheckerFn;
36 
37 typedef std::function<std::shared_ptr<Channel>(void)> ChannelCreationFunc;
38 
39 class InteropClient {
40  public:
41   /// If new_stub_every_test_case is true, a new TestService::Stub object is
42   /// created for every test case
43   /// If do_not_abort_on_transient_failures is true, abort() is not called in
44   /// case of transient failures (like connection failures)
45   explicit InteropClient(ChannelCreationFunc channel_creation_func,
46                          bool new_stub_every_test_case,
47                          bool do_not_abort_on_transient_failures);
~InteropClient()48   ~InteropClient() {}
49 
50   void Reset(const std::shared_ptr<Channel>& channel);
51 
52   bool DoEmpty();
53   bool DoLargeUnary();
54   bool DoServerCompressedUnary();
55   bool DoClientCompressedUnary();
56   bool DoPingPong();
57   bool DoHalfDuplex();
58   bool DoRequestStreaming();
59   bool DoResponseStreaming();
60   bool DoServerCompressedStreaming();
61   bool DoClientCompressedStreaming();
62   bool DoResponseStreamingWithSlowConsumer();
63   bool DoCancelAfterBegin();
64   bool DoCancelAfterFirstResponse();
65   bool DoTimeoutOnSleepingServer();
66   bool DoEmptyStream();
67   bool DoStatusWithMessage();
68   // Verifies Unicode and Whitespace is correctly processed in status message.
69   bool DoSpecialStatusMessage();
70   bool DoCustomMetadata();
71   bool DoUnimplementedMethod();
72   bool DoUnimplementedService();
73   bool DoCacheableUnary();
74   // all requests are sent to one server despite multiple servers are resolved
75   bool DoPickFirstUnary();
76 
77   // The following interop test are not yet part of the interop spec, and are
78   // not implemented cross-language. They are considered experimental for now,
79   // but at some point in the future, might be codified and implemented in all
80   // languages
81   bool DoChannelSoakTest(int32_t soak_iterations, int32_t max_failures,
82                          int64_t max_acceptable_per_iteration_latency_ms,
83                          int32_t overall_timeout_seconds);
84   bool DoRpcSoakTest(int32_t soak_iterations, int32_t max_failures,
85                      int64_t max_acceptable_per_iteration_latency_ms,
86                      int32_t overall_timeout_seconds);
87   bool DoLongLivedChannelTest(int32_t soak_iterations,
88                               int32_t iteration_interval);
89 
90   // Auth tests.
91   // username is a string containing the user email
92   bool DoJwtTokenCreds(const std::string& username);
93   bool DoComputeEngineCreds(const std::string& default_service_account,
94                             const std::string& oauth_scope);
95   // username the GCE default service account email
96   bool DoOauth2AuthToken(const std::string& username,
97                          const std::string& oauth_scope);
98   // username is a string containing the user email
99   bool DoPerRpcCreds(const std::string& json_key);
100   // default_service_account is the GCE default service account email
101   bool DoGoogleDefaultCredentials(const std::string& default_service_account);
102 
103  private:
104   class ServiceStub {
105    public:
106     // If new_stub_every_call = true, pointer to a new instance of
107     // TestServce::Stub is returned by Get() everytime it is called
108     ServiceStub(ChannelCreationFunc channel_creation_func,
109                 bool new_stub_every_call);
110 
111     TestService::Stub* Get();
112     UnimplementedService::Stub* GetUnimplementedServiceStub();
113 
114     // forces channel to be recreated.
115     void ResetChannel();
116 
117    private:
118     ChannelCreationFunc channel_creation_func_;
119     std::unique_ptr<TestService::Stub> stub_;
120     std::unique_ptr<UnimplementedService::Stub> unimplemented_service_stub_;
121     std::shared_ptr<Channel> channel_;
122     bool new_stub_every_call_;  // If true, a new stub is returned by every
123                                 // Get() call
124   };
125 
126   bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response);
127 
128   /// Run \a custom_check_fn as an additional check.
129   bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response,
130                          const CheckerFn& custom_checks_fn);
131   bool AssertStatusOk(const Status& s,
132                       const std::string& optional_debug_string);
133   bool AssertStatusCode(const Status& s, StatusCode expected_code,
134                         const std::string& optional_debug_string);
135   bool TransientFailureOrAbort();
136 
137   std::tuple<bool, int32_t, std::string> PerformOneSoakTestIteration(
138       const bool reset_channel,
139       const int32_t max_acceptable_per_iteration_latency_ms);
140 
141   void PerformSoakTest(const bool reset_channel_per_iteration,
142                        const int32_t soak_iterations,
143                        const int32_t max_failures,
144                        const int32_t max_acceptable_per_iteration_latency_ms,
145                        const int32_t overall_timeout_seconds);
146 
147   ServiceStub serviceStub_;
148   /// If true, abort() is not called for transient failures
149   bool do_not_abort_on_transient_failures_;
150 };
151 
152 }  // namespace testing
153 }  // namespace grpc
154 
155 #endif  // GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H
156