blob: 956840ba7090d5bc374dbc49f196e0fb480bb1a4 [file] [log] [blame]
yangg06170572015-01-12 13:12:45 -08001/*
2 *
David Garcia Quintas56087522016-06-16 16:52:11 -07003 * Copyright 2015-2016, Google Inc.
yangg06170572015-01-12 13:12:45 -08004 * 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
yang-g9e2f90c2015-08-21 15:35:03 -070034#include <unistd.h>
35
David Garcia Quintasc8993192015-07-22 09:10:39 -070036#include <fstream>
yangg06170572015-01-12 13:12:45 -080037#include <memory>
38#include <sstream>
39#include <thread>
40
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010041#include <gflags/gflags.h>
Craig Tillerf40df232016-03-25 13:38:14 -070042#include <grpc++/security/server_credentials.h>
yangg06170572015-01-12 13:12:45 -080043#include <grpc++/server.h>
44#include <grpc++/server_builder.h>
45#include <grpc++/server_context.h>
Craig Tillerf40df232016-03-25 13:38:14 -070046#include <grpc/grpc.h>
47#include <grpc/support/log.h>
48#include <grpc/support/useful.h>
David Garcia Quintasc8993192015-07-22 09:10:39 -070049
Makarand Dharmapurikar1ed0b8e2016-09-14 15:01:16 -070050#include "src/core/lib/support/string.h"
David Garcia Quintas74686ce2016-06-09 15:33:33 -070051#include "src/core/lib/transport/byte_stream.h"
Craig Tiller1b4e3302015-12-17 16:35:00 -080052#include "src/proto/grpc/testing/empty.grpc.pb.h"
53#include "src/proto/grpc/testing/messages.grpc.pb.h"
Craig Tillerf40df232016-03-25 13:38:14 -070054#include "src/proto/grpc/testing/test.grpc.pb.h"
55#include "test/cpp/interop/server_helper.h"
56#include "test/cpp/util/test_config.h"
yangg06170572015-01-12 13:12:45 -080057
yang-g035cf092015-09-18 12:11:05 -070058DEFINE_bool(use_tls, false, "Whether to use tls.");
yang-gcc591022017-01-11 11:10:43 -080059DEFINE_string(custom_credentials_type, "", "User provided credentials type.");
yangg06170572015-01-12 13:12:45 -080060DEFINE_int32(port, 0, "Server port.");
Mark D. Rothc99fd892016-09-06 08:16:13 -070061DEFINE_int32(max_send_message_size, -1, "The maximum send message size.");
yangg06170572015-01-12 13:12:45 -080062
63using grpc::Server;
64using grpc::ServerBuilder;
65using grpc::ServerContext;
66using grpc::ServerCredentials;
yangg06170572015-01-12 13:12:45 -080067using grpc::ServerReader;
68using grpc::ServerReaderWriter;
69using grpc::ServerWriter;
David Garcia Quintas56087522016-06-16 16:52:11 -070070using grpc::WriteOptions;
yangg06170572015-01-12 13:12:45 -080071using grpc::SslServerCredentialsOptions;
David Garcia Quintas7c0d9142015-07-23 04:58:20 -070072using grpc::testing::InteropServerContextInspector;
yangg06170572015-01-12 13:12:45 -080073using grpc::testing::Payload;
yangg06170572015-01-12 13:12:45 -080074using grpc::testing::SimpleRequest;
75using grpc::testing::SimpleResponse;
76using grpc::testing::StreamingInputCallRequest;
77using grpc::testing::StreamingInputCallResponse;
78using grpc::testing::StreamingOutputCallRequest;
79using grpc::testing::StreamingOutputCallResponse;
80using grpc::testing::TestService;
81using grpc::Status;
82
yang-gc10348a2016-02-19 16:05:10 -080083const char kEchoInitialMetadataKey[] = "x-grpc-test-echo-initial";
84const char kEchoTrailingBinMetadataKey[] = "x-grpc-test-echo-trailing-bin";
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -080085const char kEchoUserAgentKey[] = "x-grpc-test-echo-useragent";
yang-gc10348a2016-02-19 16:05:10 -080086
87void MaybeEchoMetadata(ServerContext* context) {
88 const auto& client_metadata = context->client_metadata();
89 GPR_ASSERT(client_metadata.count(kEchoInitialMetadataKey) <= 1);
90 GPR_ASSERT(client_metadata.count(kEchoTrailingBinMetadataKey) <= 1);
91
92 auto iter = client_metadata.find(kEchoInitialMetadataKey);
93 if (iter != client_metadata.end()) {
Craig Tiller5e01e2a2017-01-20 18:11:52 -080094 context->AddInitialMetadata(kEchoInitialMetadataKey, iter->second.data());
yang-gc10348a2016-02-19 16:05:10 -080095 }
96 iter = client_metadata.find(kEchoTrailingBinMetadataKey);
97 if (iter != client_metadata.end()) {
98 context->AddTrailingMetadata(
99 kEchoTrailingBinMetadataKey,
100 grpc::string(iter->second.begin(), iter->second.end()));
101 }
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -0800102 // Check if client sent a magic key in the header that makes us echo
103 // back the user-agent (for testing purpose)
104 iter = client_metadata.find(kEchoUserAgentKey);
105 if (iter != client_metadata.end()) {
106 iter = client_metadata.find("user-agent");
107 if (iter != client_metadata.end()) {
108 context->AddInitialMetadata(kEchoUserAgentKey, iter->second.data());
109 }
110 }
yang-gc10348a2016-02-19 16:05:10 -0800111}
112
David Garcia Quintas56087522016-06-16 16:52:11 -0700113bool SetPayload(int size, Payload* payload) {
114 std::unique_ptr<char[]> body(new char[size]());
115 payload->set_body(body.get(), size);
yangg06170572015-01-12 13:12:45 -0800116 return true;
117}
118
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700119bool CheckExpectedCompression(const ServerContext& context,
David Garcia Quintas56087522016-06-16 16:52:11 -0700120 const bool compression_expected) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700121 const InteropServerContextInspector inspector(context);
122 const grpc_compression_algorithm received_compression =
123 inspector.GetCallCompressionAlgorithm();
124
David Garcia Quintas56087522016-06-16 16:52:11 -0700125 if (compression_expected) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700126 if (received_compression == GRPC_COMPRESS_NONE) {
127 // Expected some compression, got NONE. This is an error.
128 gpr_log(GPR_ERROR,
David Garcia Quintas56087522016-06-16 16:52:11 -0700129 "Expected compression but got uncompressed request from client.");
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700130 return false;
131 }
David Garcia Quintas56087522016-06-16 16:52:11 -0700132 if (!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS)) {
133 gpr_log(GPR_ERROR,
134 "Failure: Requested compression in a compressable request, but "
135 "compression bit in message flags not set.");
136 return false;
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700137 }
138 } else {
139 // Didn't expect compression -> make sure the request is uncompressed
140 if (inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS) {
141 gpr_log(GPR_ERROR,
142 "Failure: Didn't requested compression, but compression bit in "
143 "message flags set.");
144 return false;
145 }
146 }
147 return true;
148}
149
yangg06170572015-01-12 13:12:45 -0800150class TestServiceImpl : public TestService::Service {
151 public:
152 Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
153 grpc::testing::Empty* response) {
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -0800154 MaybeEchoMetadata(context);
yangg06170572015-01-12 13:12:45 -0800155 return Status::OK;
156 }
157
Makarand Dharmapurikar1ed0b8e2016-09-14 15:01:16 -0700158 // Response contains current timestamp. We ignore everything in the request.
David Garcia Quintasb5299972016-10-12 14:13:42 -0700159 Status CacheableUnaryCall(ServerContext* context,
160 const SimpleRequest* request,
161 SimpleResponse* response) {
Makarand Dharmapurikaraf564a12016-09-28 12:50:37 -0700162 gpr_timespec ts = gpr_now(GPR_CLOCK_PRECISE);
Makarand Dharmapurikarc9beaca2016-09-30 11:26:13 -0700163 std::string timestamp = std::to_string((long long unsigned)ts.tv_nsec);
Makarand Dharmapurikar1ed0b8e2016-09-14 15:01:16 -0700164 response->mutable_payload()->set_body(timestamp.c_str(), timestamp.size());
Makarand Dharmapurikar012fc182016-09-28 10:46:27 -0700165 context->AddInitialMetadata("cache-control", "max-age=60, public");
Makarand Dharmapurikar1ed0b8e2016-09-14 15:01:16 -0700166 return Status::OK;
167 }
168
yangg06170572015-01-12 13:12:45 -0800169 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
170 SimpleResponse* response) {
yang-gc10348a2016-02-19 16:05:10 -0800171 MaybeEchoMetadata(context);
David Garcia Quintas56087522016-06-16 16:52:11 -0700172 if (request->has_response_compressed()) {
173 const bool compression_requested = request->response_compressed().value();
174 gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s",
175 compression_requested ? "enabled" : "disabled", __func__);
176 if (compression_requested) {
177 // Any level would do, let's go for HIGH because we are overachievers.
178 context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH);
179 } else {
180 context->set_compression_level(GRPC_COMPRESS_LEVEL_NONE);
181 }
182 }
183 if (!CheckExpectedCompression(*context,
184 request->expect_compressed().value())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700185 return Status(grpc::StatusCode::INVALID_ARGUMENT,
186 "Compressed request expectation not met.");
187 }
David Garcia Quintas864d1862015-08-13 15:26:00 -0700188 if (request->response_size() > 0) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700189 if (!SetPayload(request->response_size(), response->mutable_payload())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700190 return Status(grpc::StatusCode::INVALID_ARGUMENT,
191 "Error creating payload.");
yangg06170572015-01-12 13:12:45 -0800192 }
193 }
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700194
195 if (request->has_response_status()) {
David Garcia Quintas2e1bb1b2015-08-10 14:05:57 -0700196 return Status(
197 static_cast<grpc::StatusCode>(request->response_status().code()),
198 request->response_status().message());
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700199 }
David Garcia Quintas80f39952015-07-21 16:07:36 -0700200
yangg06170572015-01-12 13:12:45 -0800201 return Status::OK;
202 }
203
204 Status StreamingOutputCall(
205 ServerContext* context, const StreamingOutputCallRequest* request,
206 ServerWriter<StreamingOutputCallResponse>* writer) {
207 StreamingOutputCallResponse response;
208 bool write_success = true;
yangg06170572015-01-12 13:12:45 -0800209 for (int i = 0; write_success && i < request->response_parameters_size();
210 i++) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700211 if (!SetPayload(request->response_parameters(i).size(),
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700212 response.mutable_payload())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700213 return Status(grpc::StatusCode::INVALID_ARGUMENT,
214 "Error creating payload.");
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700215 }
David Garcia Quintas56087522016-06-16 16:52:11 -0700216 WriteOptions wopts;
217 if (request->response_parameters(i).has_compressed()) {
David Garcia Quintas446f70e2016-06-21 17:13:28 -0700218 // Compress by default. Disabled on a per-message basis.
219 context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH);
David Garcia Quintas56087522016-06-16 16:52:11 -0700220 const bool compression_requested =
221 request->response_parameters(i).compressed().value();
222 gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s",
223 compression_requested ? "enabled" : "disabled", __func__);
224 if (!compression_requested) {
225 wopts.set_no_compression();
226 } // else, compression is already enabled via the context.
227 }
Makarand Dharmapurikar509246f2016-06-09 16:28:02 -0700228 int time_us;
229 if ((time_us = request->response_parameters(i).interval_us()) > 0) {
230 // Sleep before response if needed
231 gpr_timespec sleep_time =
232 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
233 gpr_time_from_micros(time_us, GPR_TIMESPAN));
234 gpr_sleep_until(sleep_time);
235 }
David Garcia Quintasdacce7a2016-06-21 16:40:20 -0700236 write_success = writer->Write(response, wopts);
yangg06170572015-01-12 13:12:45 -0800237 }
238 if (write_success) {
239 return Status::OK;
240 } else {
241 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
242 }
243 }
244
245 Status StreamingInputCall(ServerContext* context,
246 ServerReader<StreamingInputCallRequest>* reader,
247 StreamingInputCallResponse* response) {
248 StreamingInputCallRequest request;
249 int aggregated_payload_size = 0;
250 while (reader->Read(&request)) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700251 if (!CheckExpectedCompression(*context,
252 request.expect_compressed().value())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700253 return Status(grpc::StatusCode::INVALID_ARGUMENT,
254 "Compressed request expectation not met.");
255 }
yang-gf6befe82015-08-13 13:51:53 -0700256 if (request.has_payload()) {
yangg06170572015-01-12 13:12:45 -0800257 aggregated_payload_size += request.payload().body().size();
258 }
259 }
260 response->set_aggregated_payload_size(aggregated_payload_size);
261 return Status::OK;
262 }
263
264 Status FullDuplexCall(
265 ServerContext* context,
266 ServerReaderWriter<StreamingOutputCallResponse,
267 StreamingOutputCallRequest>* stream) {
yang-gc10348a2016-02-19 16:05:10 -0800268 MaybeEchoMetadata(context);
yangg06170572015-01-12 13:12:45 -0800269 StreamingOutputCallRequest request;
270 StreamingOutputCallResponse response;
271 bool write_success = true;
272 while (write_success && stream->Read(&request)) {
Mark D. Rothdbf2adc2016-07-07 08:19:27 -0700273 if (request.has_response_status()) {
274 return Status(
275 static_cast<grpc::StatusCode>(request.response_status().code()),
276 request.response_status().message());
277 }
yang-g69563b92015-07-10 15:32:11 -0700278 if (request.response_parameters_size() != 0) {
279 response.mutable_payload()->set_type(request.payload().type());
280 response.mutable_payload()->set_body(
281 grpc::string(request.response_parameters(0).size(), '\0'));
Makarand Dharmapurikar509246f2016-06-09 16:28:02 -0700282 int time_us;
283 if ((time_us = request.response_parameters(0).interval_us()) > 0) {
284 // Sleep before response if needed
285 gpr_timespec sleep_time =
286 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
287 gpr_time_from_micros(time_us, GPR_TIMESPAN));
288 gpr_sleep_until(sleep_time);
289 }
yang-g69563b92015-07-10 15:32:11 -0700290 write_success = stream->Write(response);
yangg06170572015-01-12 13:12:45 -0800291 }
yangg06170572015-01-12 13:12:45 -0800292 }
293 if (write_success) {
294 return Status::OK;
295 } else {
296 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
297 }
298 }
299
300 Status HalfDuplexCall(
301 ServerContext* context,
302 ServerReaderWriter<StreamingOutputCallResponse,
303 StreamingOutputCallRequest>* stream) {
304 std::vector<StreamingOutputCallRequest> requests;
305 StreamingOutputCallRequest request;
306 while (stream->Read(&request)) {
307 requests.push_back(request);
308 }
309
310 StreamingOutputCallResponse response;
311 bool write_success = true;
312 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
313 response.mutable_payload()->set_type(requests[i].payload().type());
314 if (requests[i].response_parameters_size() == 0) {
315 return Status(grpc::StatusCode::INTERNAL,
316 "Request does not have response parameters.");
317 }
318 response.mutable_payload()->set_body(
319 grpc::string(requests[i].response_parameters(0).size(), '\0'));
320 write_success = stream->Write(response);
321 }
322 if (write_success) {
323 return Status::OK;
324 } else {
325 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
326 }
327 }
328};
329
Nicolas "Pixel" Noble6570b832016-07-18 23:29:50 +0200330void grpc::testing::interop::RunServer(
331 std::shared_ptr<ServerCredentials> creds) {
332 GPR_ASSERT(FLAGS_port != 0);
yangg06170572015-01-12 13:12:45 -0800333 std::ostringstream server_address;
Yang Gaodab70952015-01-23 16:06:36 -0800334 server_address << "0.0.0.0:" << FLAGS_port;
yangg06170572015-01-12 13:12:45 -0800335 TestServiceImpl service;
336
337 SimpleRequest request;
338 SimpleResponse response;
339
340 ServerBuilder builder;
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800341 builder.RegisterService(&service);
Nicolas Noblecfd60732015-03-18 16:27:43 -0700342 builder.AddListeningPort(server_address.str(), creds);
Mark D. Rothc99fd892016-09-06 08:16:13 -0700343 if (FLAGS_max_send_message_size >= 0) {
344 builder.SetMaxSendMessageSize(FLAGS_max_send_message_size);
345 }
yangg06170572015-01-12 13:12:45 -0800346 std::unique_ptr<Server> server(builder.BuildAndStart());
347 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
David Garcia Quintas7fa08e22016-12-04 22:11:57 -0800348 while (!gpr_atm_no_barrier_load(&g_got_sigint)) {
Craig Tillercf133f42015-02-26 14:05:56 -0800349 sleep(5);
yangg06170572015-01-12 13:12:45 -0800350 }
351}