Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <thread> |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 35 | |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 36 | #include "src/core/security/credentials.h" |
| 37 | #include "src/cpp/server/thread_pool.h" |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 38 | #include "test/core/util/port.h" |
Craig Tiller | 14e60e9 | 2015-01-13 17:26:46 -0800 | [diff] [blame] | 39 | #include "test/core/util/test_config.h" |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 40 | #include "test/cpp/util/echo_duplicate.grpc.pb.h" |
| 41 | #include "test/cpp/util/echo.grpc.pb.h" |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 42 | #include "test/cpp/util/fake_credentials.h" |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 43 | #include <grpc++/channel_arguments.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #include <grpc++/channel_interface.h> |
| 45 | #include <grpc++/client_context.h> |
| 46 | #include <grpc++/create_channel.h> |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 47 | #include <grpc++/credentials.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | #include <grpc++/server.h> |
| 49 | #include <grpc++/server_builder.h> |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 50 | #include <grpc++/server_context.h> |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 51 | #include <grpc++/server_credentials.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | #include <grpc++/status.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 53 | #include <grpc++/stream.h> |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 54 | #include <grpc++/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | #include <gtest/gtest.h> |
| 56 | |
| 57 | #include <grpc/grpc.h> |
| 58 | #include <grpc/support/thd.h> |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 59 | #include <grpc/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | |
| 61 | using grpc::cpp::test::util::EchoRequest; |
| 62 | using grpc::cpp::test::util::EchoResponse; |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 63 | using std::chrono::system_clock; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | |
| 65 | namespace grpc { |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 66 | namespace testing { |
| 67 | |
| 68 | namespace { |
| 69 | |
| 70 | // When echo_deadline is requested, deadline seen in the ServerContext is set in |
| 71 | // the response in seconds. |
| 72 | void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request, |
| 73 | EchoResponse* response) { |
| 74 | if (request->has_param() && request->param().echo_deadline()) { |
| 75 | gpr_timespec deadline = gpr_inf_future; |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 76 | if (context->deadline() != system_clock::time_point::max()) { |
| 77 | Timepoint2Timespec(context->deadline(), &deadline); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 78 | } |
| 79 | response->mutable_param()->set_request_deadline(deadline.tv_sec); |
| 80 | } |
| 81 | } |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 82 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 83 | } // namespace |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 85 | class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | public: |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 87 | TestServiceImpl() : signal_client_(false) {} |
| 88 | |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 89 | Status Echo(ServerContext* context, const EchoRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 90 | EchoResponse* response) GRPC_OVERRIDE { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | response->set_message(request->message()); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 92 | MaybeEchoDeadline(context, request, response); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 93 | if (request->has_param() && request->param().client_cancel_after_us()) { |
| 94 | { |
| 95 | std::unique_lock<std::mutex> lock(mu_); |
| 96 | signal_client_ = true; |
| 97 | } |
| 98 | while (!context->IsCancelled()) { |
| 99 | std::this_thread::sleep_for(std::chrono::microseconds( |
| 100 | request->param().client_cancel_after_us())); |
| 101 | } |
| 102 | return Status::Cancelled; |
| 103 | } else if (request->has_param() && |
| 104 | request->param().server_cancel_after_us()) { |
| 105 | std::this_thread::sleep_for( |
| 106 | std::chrono::microseconds(request->param().server_cancel_after_us())); |
| 107 | return Status::Cancelled; |
| 108 | } else { |
| 109 | EXPECT_FALSE(context->IsCancelled()); |
| 110 | } |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 111 | |
| 112 | if (request->has_param() && request->param().echo_metadata()) { |
| 113 | const std::multimap<grpc::string, grpc::string>& client_metadata = |
| 114 | context->client_metadata(); |
| 115 | for (std::multimap<grpc::string, grpc::string>::const_iterator iter = |
| 116 | client_metadata.begin(); |
| 117 | iter != client_metadata.end(); ++iter) { |
| 118 | context->AddTrailingMetadata((*iter).first, (*iter).second); |
| 119 | } |
| 120 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | return Status::OK; |
| 122 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 123 | |
| 124 | // Unimplemented is left unimplemented to test the returned error. |
| 125 | |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 126 | Status RequestStream(ServerContext* context, |
| 127 | ServerReader<EchoRequest>* reader, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 128 | EchoResponse* response) GRPC_OVERRIDE { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 129 | EchoRequest request; |
| 130 | response->set_message(""); |
| 131 | while (reader->Read(&request)) { |
| 132 | response->mutable_message()->append(request.message()); |
| 133 | } |
| 134 | return Status::OK; |
| 135 | } |
| 136 | |
| 137 | // Return 3 messages. |
| 138 | // TODO(yangg) make it generic by adding a parameter into EchoRequest |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 139 | Status ResponseStream(ServerContext* context, const EchoRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 140 | ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 141 | EchoResponse response; |
| 142 | response.set_message(request->message() + "0"); |
| 143 | writer->Write(response); |
| 144 | response.set_message(request->message() + "1"); |
| 145 | writer->Write(response); |
| 146 | response.set_message(request->message() + "2"); |
| 147 | writer->Write(response); |
| 148 | |
| 149 | return Status::OK; |
| 150 | } |
| 151 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 152 | Status BidiStream(ServerContext* context, |
| 153 | ServerReaderWriter<EchoResponse, EchoRequest>* stream) |
| 154 | GRPC_OVERRIDE { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 155 | EchoRequest request; |
| 156 | EchoResponse response; |
| 157 | while (stream->Read(&request)) { |
| 158 | gpr_log(GPR_INFO, "recv msg %s", request.message().c_str()); |
| 159 | response.set_message(request.message()); |
| 160 | stream->Write(response); |
| 161 | } |
| 162 | return Status::OK; |
| 163 | } |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 164 | |
| 165 | bool signal_client() { |
| 166 | std::unique_lock<std::mutex> lock(mu_); |
| 167 | return signal_client_; |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | bool signal_client_; |
| 172 | std::mutex mu_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 175 | class TestServiceImplDupPkg |
| 176 | : public ::grpc::cpp::test::util::duplicate::TestService::Service { |
| 177 | public: |
| 178 | Status Echo(ServerContext* context, const EchoRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 179 | EchoResponse* response) GRPC_OVERRIDE { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 180 | response->set_message("no package"); |
| 181 | return Status::OK; |
| 182 | } |
| 183 | }; |
| 184 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 185 | class End2endTest : public ::testing::Test { |
| 186 | protected: |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 187 | End2endTest() : kMaxMessageSize_(8192), thread_pool_(2) {} |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 188 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 189 | void SetUp() GRPC_OVERRIDE { |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 190 | int port = grpc_pick_unused_port_or_die(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 191 | server_address_ << "localhost:" << port; |
| 192 | // Setup server |
| 193 | ServerBuilder builder; |
Yang Gao | b57f72d | 2015-05-17 21:54:54 -0700 | [diff] [blame^] | 194 | builder.AddListeningPort(server_address_.str(), |
| 195 | FakeTransportSecurityServerCredentials()); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 196 | builder.RegisterService(&service_); |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 197 | builder.SetMaxMessageSize( |
| 198 | kMaxMessageSize_); // For testing max message size. |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 199 | builder.RegisterService(&dup_pkg_service_); |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 200 | builder.SetThreadPool(&thread_pool_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 201 | server_ = builder.BuildAndStart(); |
| 202 | } |
| 203 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 204 | void TearDown() GRPC_OVERRIDE { server_->Shutdown(); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 205 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 206 | void ResetStub() { |
Yang Gao | b57f72d | 2015-05-17 21:54:54 -0700 | [diff] [blame^] | 207 | std::shared_ptr<ChannelInterface> channel = |
| 208 | CreateChannel(server_address_.str(), FakeTransportSecurityCredentials(), |
| 209 | ChannelArguments()); |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 210 | stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel)); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 214 | std::unique_ptr<Server> server_; |
| 215 | std::ostringstream server_address_; |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 216 | const int kMaxMessageSize_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 217 | TestServiceImpl service_; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 218 | TestServiceImplDupPkg dup_pkg_service_; |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 219 | ThreadPool thread_pool_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 220 | }; |
| 221 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 222 | static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub, |
| 223 | int num_rpcs) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 224 | EchoRequest request; |
| 225 | EchoResponse response; |
| 226 | request.set_message("Hello"); |
| 227 | |
| 228 | for (int i = 0; i < num_rpcs; ++i) { |
| 229 | ClientContext context; |
| 230 | Status s = stub->Echo(&context, request, &response); |
| 231 | EXPECT_EQ(response.message(), request.message()); |
| 232 | EXPECT_TRUE(s.IsOk()); |
| 233 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | TEST_F(End2endTest, SimpleRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 237 | ResetStub(); |
| 238 | SendRpc(stub_.get(), 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | TEST_F(End2endTest, MultipleRpcs) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 242 | ResetStub(); |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 243 | std::vector<std::thread*> threads; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 244 | for (int i = 0; i < 10; ++i) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 245 | threads.push_back(new std::thread(SendRpc, stub_.get(), 10)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 246 | } |
| 247 | for (int i = 0; i < 10; ++i) { |
| 248 | threads[i]->join(); |
| 249 | delete threads[i]; |
| 250 | } |
| 251 | } |
| 252 | |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 253 | // Set a 10us deadline and make sure proper error is returned. |
| 254 | TEST_F(End2endTest, RpcDeadlineExpires) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 255 | ResetStub(); |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 256 | EchoRequest request; |
| 257 | EchoResponse response; |
| 258 | request.set_message("Hello"); |
| 259 | |
| 260 | ClientContext context; |
| 261 | std::chrono::system_clock::time_point deadline = |
| 262 | std::chrono::system_clock::now() + std::chrono::microseconds(10); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 263 | context.set_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 264 | Status s = stub_->Echo(&context, request, &response); |
Craig Tiller | 7b01878 | 2015-01-16 09:53:39 -0800 | [diff] [blame] | 265 | EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.code()); |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 266 | } |
| 267 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 268 | // Set a long but finite deadline. |
| 269 | TEST_F(End2endTest, RpcLongDeadline) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 270 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 271 | EchoRequest request; |
| 272 | EchoResponse response; |
| 273 | request.set_message("Hello"); |
| 274 | |
| 275 | ClientContext context; |
| 276 | std::chrono::system_clock::time_point deadline = |
| 277 | std::chrono::system_clock::now() + std::chrono::hours(1); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 278 | context.set_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 279 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 280 | EXPECT_EQ(response.message(), request.message()); |
| 281 | EXPECT_TRUE(s.IsOk()); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // Ask server to echo back the deadline it sees. |
| 285 | TEST_F(End2endTest, EchoDeadline) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 286 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 287 | EchoRequest request; |
| 288 | EchoResponse response; |
| 289 | request.set_message("Hello"); |
| 290 | request.mutable_param()->set_echo_deadline(true); |
| 291 | |
| 292 | ClientContext context; |
| 293 | std::chrono::system_clock::time_point deadline = |
| 294 | std::chrono::system_clock::now() + std::chrono::seconds(100); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 295 | context.set_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 296 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 297 | EXPECT_EQ(response.message(), request.message()); |
| 298 | EXPECT_TRUE(s.IsOk()); |
| 299 | gpr_timespec sent_deadline; |
| 300 | Timepoint2Timespec(deadline, &sent_deadline); |
| 301 | // Allow 1 second error. |
| 302 | EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1); |
| 303 | EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // Ask server to echo back the deadline it sees. The rpc has no deadline. |
| 307 | TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 308 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 309 | EchoRequest request; |
| 310 | EchoResponse response; |
| 311 | request.set_message("Hello"); |
| 312 | request.mutable_param()->set_echo_deadline(true); |
| 313 | |
| 314 | ClientContext context; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 315 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 316 | EXPECT_EQ(response.message(), request.message()); |
| 317 | EXPECT_TRUE(s.IsOk()); |
| 318 | EXPECT_EQ(response.param().request_deadline(), gpr_inf_future.tv_sec); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 319 | } |
| 320 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 321 | TEST_F(End2endTest, UnimplementedRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 322 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 323 | EchoRequest request; |
| 324 | EchoResponse response; |
| 325 | request.set_message("Hello"); |
| 326 | |
| 327 | ClientContext context; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 328 | Status s = stub_->Unimplemented(&context, request, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 329 | EXPECT_FALSE(s.IsOk()); |
| 330 | EXPECT_EQ(s.code(), grpc::StatusCode::UNIMPLEMENTED); |
| 331 | EXPECT_EQ(s.details(), ""); |
| 332 | EXPECT_EQ(response.message(), ""); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | TEST_F(End2endTest, RequestStreamOneRequest) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 336 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 337 | EchoRequest request; |
| 338 | EchoResponse response; |
| 339 | ClientContext context; |
| 340 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 341 | auto stream = stub_->RequestStream(&context, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 342 | request.set_message("hello"); |
| 343 | EXPECT_TRUE(stream->Write(request)); |
| 344 | stream->WritesDone(); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 345 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 346 | EXPECT_EQ(response.message(), request.message()); |
| 347 | EXPECT_TRUE(s.IsOk()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | TEST_F(End2endTest, RequestStreamTwoRequests) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 351 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 352 | EchoRequest request; |
| 353 | EchoResponse response; |
| 354 | ClientContext context; |
| 355 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 356 | auto stream = stub_->RequestStream(&context, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 357 | request.set_message("hello"); |
| 358 | EXPECT_TRUE(stream->Write(request)); |
| 359 | EXPECT_TRUE(stream->Write(request)); |
| 360 | stream->WritesDone(); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 361 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 362 | EXPECT_EQ(response.message(), "hellohello"); |
| 363 | EXPECT_TRUE(s.IsOk()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | TEST_F(End2endTest, ResponseStream) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 367 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 368 | EchoRequest request; |
| 369 | EchoResponse response; |
| 370 | ClientContext context; |
| 371 | request.set_message("hello"); |
| 372 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 373 | auto stream = stub_->ResponseStream(&context, request); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 374 | EXPECT_TRUE(stream->Read(&response)); |
| 375 | EXPECT_EQ(response.message(), request.message() + "0"); |
| 376 | EXPECT_TRUE(stream->Read(&response)); |
| 377 | EXPECT_EQ(response.message(), request.message() + "1"); |
| 378 | EXPECT_TRUE(stream->Read(&response)); |
| 379 | EXPECT_EQ(response.message(), request.message() + "2"); |
| 380 | EXPECT_FALSE(stream->Read(&response)); |
| 381 | |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 382 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 383 | EXPECT_TRUE(s.IsOk()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | TEST_F(End2endTest, BidiStream) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 387 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 388 | EchoRequest request; |
| 389 | EchoResponse response; |
| 390 | ClientContext context; |
| 391 | grpc::string msg("hello"); |
| 392 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 393 | auto stream = stub_->BidiStream(&context); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 394 | |
| 395 | request.set_message(msg + "0"); |
| 396 | EXPECT_TRUE(stream->Write(request)); |
| 397 | EXPECT_TRUE(stream->Read(&response)); |
| 398 | EXPECT_EQ(response.message(), request.message()); |
| 399 | |
| 400 | request.set_message(msg + "1"); |
| 401 | EXPECT_TRUE(stream->Write(request)); |
| 402 | EXPECT_TRUE(stream->Read(&response)); |
| 403 | EXPECT_EQ(response.message(), request.message()); |
| 404 | |
| 405 | request.set_message(msg + "2"); |
| 406 | EXPECT_TRUE(stream->Write(request)); |
| 407 | EXPECT_TRUE(stream->Read(&response)); |
| 408 | EXPECT_EQ(response.message(), request.message()); |
| 409 | |
| 410 | stream->WritesDone(); |
| 411 | EXPECT_FALSE(stream->Read(&response)); |
| 412 | |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 413 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 414 | EXPECT_TRUE(s.IsOk()); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | // Talk to the two services with the same name but different package names. |
| 418 | // The two stubs are created on the same channel. |
| 419 | TEST_F(End2endTest, DiffPackageServices) { |
Yang Gao | b57f72d | 2015-05-17 21:54:54 -0700 | [diff] [blame^] | 420 | std::shared_ptr<ChannelInterface> channel = |
| 421 | CreateChannel(server_address_.str(), FakeTransportSecurityCredentials(), |
| 422 | ChannelArguments()); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 423 | |
| 424 | EchoRequest request; |
| 425 | EchoResponse response; |
| 426 | request.set_message("Hello"); |
| 427 | |
| 428 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub( |
| 429 | grpc::cpp::test::util::TestService::NewStub(channel)); |
| 430 | ClientContext context; |
| 431 | Status s = stub->Echo(&context, request, &response); |
| 432 | EXPECT_EQ(response.message(), request.message()); |
| 433 | EXPECT_TRUE(s.IsOk()); |
| 434 | |
| 435 | std::unique_ptr<grpc::cpp::test::util::duplicate::TestService::Stub> |
| 436 | dup_pkg_stub( |
| 437 | grpc::cpp::test::util::duplicate::TestService::NewStub(channel)); |
| 438 | ClientContext context2; |
| 439 | s = dup_pkg_stub->Echo(&context2, request, &response); |
| 440 | EXPECT_EQ("no package", response.message()); |
| 441 | EXPECT_TRUE(s.IsOk()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 442 | } |
| 443 | |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 444 | // rpc and stream should fail on bad credentials. |
| 445 | TEST_F(End2endTest, BadCredentials) { |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 446 | std::shared_ptr<Credentials> bad_creds = ServiceAccountCredentials("", "", 1); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 447 | EXPECT_EQ(nullptr, bad_creds.get()); |
| 448 | std::shared_ptr<ChannelInterface> channel = |
| 449 | CreateChannel(server_address_.str(), bad_creds, ChannelArguments()); |
| 450 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub( |
| 451 | grpc::cpp::test::util::TestService::NewStub(channel)); |
| 452 | EchoRequest request; |
| 453 | EchoResponse response; |
| 454 | ClientContext context; |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 455 | request.set_message("Hello"); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 456 | |
| 457 | Status s = stub->Echo(&context, request, &response); |
| 458 | EXPECT_EQ("", response.message()); |
| 459 | EXPECT_FALSE(s.IsOk()); |
| 460 | EXPECT_EQ(StatusCode::UNKNOWN, s.code()); |
| 461 | EXPECT_EQ("Rpc sent on a lame channel.", s.details()); |
| 462 | |
| 463 | ClientContext context2; |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 464 | auto stream = stub->BidiStream(&context2); |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 465 | s = stream->Finish(); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 466 | EXPECT_FALSE(s.IsOk()); |
| 467 | EXPECT_EQ(StatusCode::UNKNOWN, s.code()); |
| 468 | EXPECT_EQ("Rpc sent on a lame channel.", s.details()); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 471 | void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) { |
| 472 | std::this_thread::sleep_for(std::chrono::microseconds(delay_us)); |
| 473 | while (!service->signal_client()) { |
| 474 | } |
| 475 | context->TryCancel(); |
| 476 | } |
| 477 | |
| 478 | // Client cancels rpc after 10ms |
| 479 | TEST_F(End2endTest, ClientCancelsRpc) { |
| 480 | ResetStub(); |
| 481 | EchoRequest request; |
| 482 | EchoResponse response; |
| 483 | request.set_message("Hello"); |
| 484 | const int kCancelDelayUs = 10 * 1000; |
| 485 | request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs); |
| 486 | |
| 487 | ClientContext context; |
| 488 | std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_); |
| 489 | Status s = stub_->Echo(&context, request, &response); |
| 490 | cancel_thread.join(); |
| 491 | EXPECT_EQ(StatusCode::CANCELLED, s.code()); |
Craig Tiller | 12088b2 | 2015-04-20 09:51:27 -0700 | [diff] [blame] | 492 | EXPECT_EQ(s.details(), "Cancelled"); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // Server cancels rpc after 1ms |
| 496 | TEST_F(End2endTest, ServerCancelsRpc) { |
| 497 | ResetStub(); |
| 498 | EchoRequest request; |
| 499 | EchoResponse response; |
| 500 | request.set_message("Hello"); |
| 501 | request.mutable_param()->set_server_cancel_after_us(1000); |
| 502 | |
| 503 | ClientContext context; |
| 504 | Status s = stub_->Echo(&context, request, &response); |
| 505 | EXPECT_EQ(StatusCode::CANCELLED, s.code()); |
| 506 | EXPECT_TRUE(s.details().empty()); |
| 507 | } |
| 508 | |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 509 | // Client cancels request stream after sending two messages |
| 510 | TEST_F(End2endTest, ClientCancelsRequestStream) { |
| 511 | ResetStub(); |
| 512 | EchoRequest request; |
| 513 | EchoResponse response; |
| 514 | ClientContext context; |
| 515 | request.set_message("hello"); |
| 516 | |
| 517 | auto stream = stub_->RequestStream(&context, &response); |
| 518 | EXPECT_TRUE(stream->Write(request)); |
| 519 | EXPECT_TRUE(stream->Write(request)); |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 520 | |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 521 | context.TryCancel(); |
| 522 | |
| 523 | Status s = stream->Finish(); |
| 524 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.code()); |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 525 | |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 526 | EXPECT_EQ(response.message(), ""); |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 529 | // Client cancels server stream after sending some messages |
Abhishek Kumar | a1d3a72 | 2015-04-23 10:24:04 -0700 | [diff] [blame] | 530 | TEST_F(End2endTest, ClientCancelsResponseStream) { |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 531 | ResetStub(); |
| 532 | EchoRequest request; |
| 533 | EchoResponse response; |
| 534 | ClientContext context; |
| 535 | request.set_message("hello"); |
| 536 | |
| 537 | auto stream = stub_->ResponseStream(&context, request); |
| 538 | |
| 539 | EXPECT_TRUE(stream->Read(&response)); |
| 540 | EXPECT_EQ(response.message(), request.message() + "0"); |
| 541 | EXPECT_TRUE(stream->Read(&response)); |
| 542 | EXPECT_EQ(response.message(), request.message() + "1"); |
| 543 | |
| 544 | context.TryCancel(); |
| 545 | |
| 546 | // The cancellation races with responses, so there might be zero or |
| 547 | // one responses pending, read till failure |
| 548 | |
| 549 | if (stream->Read(&response)) { |
| 550 | EXPECT_EQ(response.message(), request.message() + "2"); |
| 551 | // Since we have cancelled, we expect the next attempt to read to fail |
| 552 | EXPECT_FALSE(stream->Read(&response)); |
| 553 | } |
| 554 | |
| 555 | Status s = stream->Finish(); |
Abhishek Kumar | 18298a7 | 2015-04-17 15:00:25 -0700 | [diff] [blame] | 556 | // The final status could be either of CANCELLED or OK depending on |
| 557 | // who won the race. |
| 558 | EXPECT_GE(grpc::StatusCode::CANCELLED, s.code()); |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 561 | // Client cancels bidi stream after sending some messages |
| 562 | TEST_F(End2endTest, ClientCancelsBidi) { |
| 563 | ResetStub(); |
| 564 | EchoRequest request; |
| 565 | EchoResponse response; |
| 566 | ClientContext context; |
| 567 | grpc::string msg("hello"); |
| 568 | |
| 569 | auto stream = stub_->BidiStream(&context); |
| 570 | |
| 571 | request.set_message(msg + "0"); |
| 572 | EXPECT_TRUE(stream->Write(request)); |
| 573 | EXPECT_TRUE(stream->Read(&response)); |
| 574 | EXPECT_EQ(response.message(), request.message()); |
| 575 | |
| 576 | request.set_message(msg + "1"); |
| 577 | EXPECT_TRUE(stream->Write(request)); |
| 578 | |
| 579 | context.TryCancel(); |
| 580 | |
| 581 | // The cancellation races with responses, so there might be zero or |
| 582 | // one responses pending, read till failure |
| 583 | |
| 584 | if (stream->Read(&response)) { |
| 585 | EXPECT_EQ(response.message(), request.message()); |
| 586 | // Since we have cancelled, we expect the next attempt to read to fail |
| 587 | EXPECT_FALSE(stream->Read(&response)); |
| 588 | } |
| 589 | |
| 590 | Status s = stream->Finish(); |
| 591 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.code()); |
| 592 | } |
| 593 | |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 594 | TEST_F(End2endTest, RpcMaxMessageSize) { |
| 595 | ResetStub(); |
| 596 | EchoRequest request; |
| 597 | EchoResponse response; |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 598 | request.set_message(string(kMaxMessageSize_ * 2, 'a')); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 599 | |
| 600 | ClientContext context; |
| 601 | Status s = stub_->Echo(&context, request, &response); |
| 602 | EXPECT_FALSE(s.IsOk()); |
| 603 | } |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 604 | |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 605 | bool MetadataContains(const std::multimap<grpc::string, grpc::string>& metadata, |
| 606 | const grpc::string& key, const grpc::string& value) { |
| 607 | int count = 0; |
| 608 | |
| 609 | for (std::multimap<grpc::string, grpc::string>::const_iterator iter = |
| 610 | metadata.begin(); |
| 611 | iter != metadata.end(); ++iter) { |
| 612 | if ((*iter).first == key && (*iter).second == value) { |
| 613 | count++; |
| 614 | } |
| 615 | } |
| 616 | return count == 1; |
| 617 | } |
| 618 | |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 619 | TEST_F(End2endTest, SetPerCallCredentials) { |
| 620 | ResetStub(); |
| 621 | EchoRequest request; |
| 622 | EchoResponse response; |
| 623 | ClientContext context; |
| 624 | std::shared_ptr<Credentials> creds = |
| 625 | IAMCredentials("fake_token", "fake_selector"); |
| 626 | context.set_credentials(creds); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 627 | request.set_message("Hello"); |
| 628 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 629 | |
| 630 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 631 | EXPECT_EQ(request.message(), response.message()); |
| 632 | EXPECT_TRUE(s.IsOk()); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 633 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 634 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 635 | "fake_token")); |
| 636 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 637 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 638 | "fake_selector")); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | TEST_F(End2endTest, InsecurePerCallCredentials) { |
| 642 | ResetStub(); |
| 643 | EchoRequest request; |
| 644 | EchoResponse response; |
| 645 | ClientContext context; |
| 646 | std::shared_ptr<Credentials> creds = InsecureCredentials(); |
| 647 | context.set_credentials(creds); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 648 | request.set_message("Hello"); |
| 649 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 650 | |
| 651 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | 280ca17 | 2015-05-15 10:25:10 -0700 | [diff] [blame] | 652 | EXPECT_EQ(StatusCode::CANCELLED, s.code()); |
| 653 | EXPECT_EQ("Failed to set credentials to rpc.", s.details()); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | TEST_F(End2endTest, OverridePerCallCredentials) { |
| 657 | ResetStub(); |
| 658 | EchoRequest request; |
| 659 | EchoResponse response; |
| 660 | ClientContext context; |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 661 | std::shared_ptr<Credentials> creds1 = |
| 662 | IAMCredentials("fake_token1", "fake_selector1"); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 663 | context.set_credentials(creds1); |
| 664 | std::shared_ptr<Credentials> creds2 = |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 665 | IAMCredentials("fake_token2", "fake_selector2"); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 666 | context.set_credentials(creds2); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 667 | request.set_message("Hello"); |
| 668 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 669 | |
| 670 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 671 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 672 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 673 | "fake_token2")); |
| 674 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 675 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 676 | "fake_selector2")); |
Yang Gao | b57f72d | 2015-05-17 21:54:54 -0700 | [diff] [blame^] | 677 | EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 678 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 679 | "fake_token1")); |
| 680 | EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 681 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 682 | "fake_selector1")); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 683 | EXPECT_EQ(request.message(), response.message()); |
| 684 | EXPECT_TRUE(s.IsOk()); |
| 685 | } |
| 686 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 687 | } // namespace testing |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 688 | } // namespace grpc |
| 689 | |
| 690 | int main(int argc, char** argv) { |
Craig Tiller | 14e60e9 | 2015-01-13 17:26:46 -0800 | [diff] [blame] | 691 | grpc_test_init(argc, argv); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 692 | ::testing::InitGoogleTest(&argc, argv); |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 693 | return RUN_ALL_TESTS(); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 694 | } |