blob: 1810cd076f9cc9d99579773e3a351d8b2e6e3a79 [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 Tillere4fae4e2017-01-10 17:01:51 -080094 context->AddInitialMetadata(
95 kEchoInitialMetadataKey,
96 grpc::string(iter->second.begin(), iter->second.end()));
yang-gc10348a2016-02-19 16:05:10 -080097 }
98 iter = client_metadata.find(kEchoTrailingBinMetadataKey);
99 if (iter != client_metadata.end()) {
100 context->AddTrailingMetadata(
101 kEchoTrailingBinMetadataKey,
102 grpc::string(iter->second.begin(), iter->second.end()));
103 }
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -0800104 // Check if client sent a magic key in the header that makes us echo
105 // back the user-agent (for testing purpose)
106 iter = client_metadata.find(kEchoUserAgentKey);
107 if (iter != client_metadata.end()) {
108 iter = client_metadata.find("user-agent");
109 if (iter != client_metadata.end()) {
110 context->AddInitialMetadata(kEchoUserAgentKey, iter->second.data());
111 }
112 }
yang-gc10348a2016-02-19 16:05:10 -0800113}
114
David Garcia Quintas56087522016-06-16 16:52:11 -0700115bool SetPayload(int size, Payload* payload) {
116 std::unique_ptr<char[]> body(new char[size]());
117 payload->set_body(body.get(), size);
yangg06170572015-01-12 13:12:45 -0800118 return true;
119}
120
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700121bool CheckExpectedCompression(const ServerContext& context,
David Garcia Quintas56087522016-06-16 16:52:11 -0700122 const bool compression_expected) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700123 const InteropServerContextInspector inspector(context);
124 const grpc_compression_algorithm received_compression =
125 inspector.GetCallCompressionAlgorithm();
126
David Garcia Quintas56087522016-06-16 16:52:11 -0700127 if (compression_expected) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700128 if (received_compression == GRPC_COMPRESS_NONE) {
129 // Expected some compression, got NONE. This is an error.
130 gpr_log(GPR_ERROR,
David Garcia Quintas56087522016-06-16 16:52:11 -0700131 "Expected compression but got uncompressed request from client.");
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700132 return false;
133 }
David Garcia Quintas56087522016-06-16 16:52:11 -0700134 if (!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS)) {
135 gpr_log(GPR_ERROR,
136 "Failure: Requested compression in a compressable request, but "
137 "compression bit in message flags not set.");
138 return false;
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700139 }
140 } else {
141 // Didn't expect compression -> make sure the request is uncompressed
142 if (inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS) {
143 gpr_log(GPR_ERROR,
144 "Failure: Didn't requested compression, but compression bit in "
145 "message flags set.");
146 return false;
147 }
148 }
149 return true;
150}
151
yangg06170572015-01-12 13:12:45 -0800152class TestServiceImpl : public TestService::Service {
153 public:
154 Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
155 grpc::testing::Empty* response) {
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -0800156 MaybeEchoMetadata(context);
yangg06170572015-01-12 13:12:45 -0800157 return Status::OK;
158 }
159
Makarand Dharmapurikar1ed0b8e2016-09-14 15:01:16 -0700160 // Response contains current timestamp. We ignore everything in the request.
David Garcia Quintasb5299972016-10-12 14:13:42 -0700161 Status CacheableUnaryCall(ServerContext* context,
162 const SimpleRequest* request,
163 SimpleResponse* response) {
Makarand Dharmapurikaraf564a12016-09-28 12:50:37 -0700164 gpr_timespec ts = gpr_now(GPR_CLOCK_PRECISE);
Makarand Dharmapurikarc9beaca2016-09-30 11:26:13 -0700165 std::string timestamp = std::to_string((long long unsigned)ts.tv_nsec);
Makarand Dharmapurikar1ed0b8e2016-09-14 15:01:16 -0700166 response->mutable_payload()->set_body(timestamp.c_str(), timestamp.size());
Makarand Dharmapurikar012fc182016-09-28 10:46:27 -0700167 context->AddInitialMetadata("cache-control", "max-age=60, public");
Makarand Dharmapurikar1ed0b8e2016-09-14 15:01:16 -0700168 return Status::OK;
169 }
170
yangg06170572015-01-12 13:12:45 -0800171 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
172 SimpleResponse* response) {
yang-gc10348a2016-02-19 16:05:10 -0800173 MaybeEchoMetadata(context);
David Garcia Quintas56087522016-06-16 16:52:11 -0700174 if (request->has_response_compressed()) {
175 const bool compression_requested = request->response_compressed().value();
176 gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s",
177 compression_requested ? "enabled" : "disabled", __func__);
178 if (compression_requested) {
179 // Any level would do, let's go for HIGH because we are overachievers.
180 context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH);
181 } else {
182 context->set_compression_level(GRPC_COMPRESS_LEVEL_NONE);
183 }
184 }
185 if (!CheckExpectedCompression(*context,
186 request->expect_compressed().value())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700187 return Status(grpc::StatusCode::INVALID_ARGUMENT,
188 "Compressed request expectation not met.");
189 }
David Garcia Quintas864d1862015-08-13 15:26:00 -0700190 if (request->response_size() > 0) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700191 if (!SetPayload(request->response_size(), response->mutable_payload())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700192 return Status(grpc::StatusCode::INVALID_ARGUMENT,
193 "Error creating payload.");
yangg06170572015-01-12 13:12:45 -0800194 }
195 }
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700196
197 if (request->has_response_status()) {
David Garcia Quintas2e1bb1b2015-08-10 14:05:57 -0700198 return Status(
199 static_cast<grpc::StatusCode>(request->response_status().code()),
200 request->response_status().message());
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700201 }
David Garcia Quintas80f39952015-07-21 16:07:36 -0700202
yangg06170572015-01-12 13:12:45 -0800203 return Status::OK;
204 }
205
206 Status StreamingOutputCall(
207 ServerContext* context, const StreamingOutputCallRequest* request,
208 ServerWriter<StreamingOutputCallResponse>* writer) {
209 StreamingOutputCallResponse response;
210 bool write_success = true;
yangg06170572015-01-12 13:12:45 -0800211 for (int i = 0; write_success && i < request->response_parameters_size();
212 i++) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700213 if (!SetPayload(request->response_parameters(i).size(),
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700214 response.mutable_payload())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700215 return Status(grpc::StatusCode::INVALID_ARGUMENT,
216 "Error creating payload.");
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700217 }
David Garcia Quintas56087522016-06-16 16:52:11 -0700218 WriteOptions wopts;
219 if (request->response_parameters(i).has_compressed()) {
David Garcia Quintas446f70e2016-06-21 17:13:28 -0700220 // Compress by default. Disabled on a per-message basis.
221 context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH);
David Garcia Quintas56087522016-06-16 16:52:11 -0700222 const bool compression_requested =
223 request->response_parameters(i).compressed().value();
224 gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s",
225 compression_requested ? "enabled" : "disabled", __func__);
226 if (!compression_requested) {
227 wopts.set_no_compression();
228 } // else, compression is already enabled via the context.
229 }
Makarand Dharmapurikar509246f2016-06-09 16:28:02 -0700230 int time_us;
231 if ((time_us = request->response_parameters(i).interval_us()) > 0) {
232 // Sleep before response if needed
233 gpr_timespec sleep_time =
234 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
235 gpr_time_from_micros(time_us, GPR_TIMESPAN));
236 gpr_sleep_until(sleep_time);
237 }
David Garcia Quintasdacce7a2016-06-21 16:40:20 -0700238 write_success = writer->Write(response, wopts);
yangg06170572015-01-12 13:12:45 -0800239 }
240 if (write_success) {
241 return Status::OK;
242 } else {
243 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
244 }
245 }
246
247 Status StreamingInputCall(ServerContext* context,
248 ServerReader<StreamingInputCallRequest>* reader,
249 StreamingInputCallResponse* response) {
250 StreamingInputCallRequest request;
251 int aggregated_payload_size = 0;
252 while (reader->Read(&request)) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700253 if (!CheckExpectedCompression(*context,
254 request.expect_compressed().value())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700255 return Status(grpc::StatusCode::INVALID_ARGUMENT,
256 "Compressed request expectation not met.");
257 }
yang-gf6befe82015-08-13 13:51:53 -0700258 if (request.has_payload()) {
yangg06170572015-01-12 13:12:45 -0800259 aggregated_payload_size += request.payload().body().size();
260 }
261 }
262 response->set_aggregated_payload_size(aggregated_payload_size);
263 return Status::OK;
264 }
265
266 Status FullDuplexCall(
267 ServerContext* context,
268 ServerReaderWriter<StreamingOutputCallResponse,
269 StreamingOutputCallRequest>* stream) {
yang-gc10348a2016-02-19 16:05:10 -0800270 MaybeEchoMetadata(context);
yangg06170572015-01-12 13:12:45 -0800271 StreamingOutputCallRequest request;
272 StreamingOutputCallResponse response;
273 bool write_success = true;
274 while (write_success && stream->Read(&request)) {
Mark D. Rothdbf2adc2016-07-07 08:19:27 -0700275 if (request.has_response_status()) {
276 return Status(
277 static_cast<grpc::StatusCode>(request.response_status().code()),
278 request.response_status().message());
279 }
yang-g69563b92015-07-10 15:32:11 -0700280 if (request.response_parameters_size() != 0) {
281 response.mutable_payload()->set_type(request.payload().type());
282 response.mutable_payload()->set_body(
283 grpc::string(request.response_parameters(0).size(), '\0'));
Makarand Dharmapurikar509246f2016-06-09 16:28:02 -0700284 int time_us;
285 if ((time_us = request.response_parameters(0).interval_us()) > 0) {
286 // Sleep before response if needed
287 gpr_timespec sleep_time =
288 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
289 gpr_time_from_micros(time_us, GPR_TIMESPAN));
290 gpr_sleep_until(sleep_time);
291 }
yang-g69563b92015-07-10 15:32:11 -0700292 write_success = stream->Write(response);
yangg06170572015-01-12 13:12:45 -0800293 }
yangg06170572015-01-12 13:12:45 -0800294 }
295 if (write_success) {
296 return Status::OK;
297 } else {
298 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
299 }
300 }
301
302 Status HalfDuplexCall(
303 ServerContext* context,
304 ServerReaderWriter<StreamingOutputCallResponse,
305 StreamingOutputCallRequest>* stream) {
306 std::vector<StreamingOutputCallRequest> requests;
307 StreamingOutputCallRequest request;
308 while (stream->Read(&request)) {
309 requests.push_back(request);
310 }
311
312 StreamingOutputCallResponse response;
313 bool write_success = true;
314 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
315 response.mutable_payload()->set_type(requests[i].payload().type());
316 if (requests[i].response_parameters_size() == 0) {
317 return Status(grpc::StatusCode::INTERNAL,
318 "Request does not have response parameters.");
319 }
320 response.mutable_payload()->set_body(
321 grpc::string(requests[i].response_parameters(0).size(), '\0'));
322 write_success = stream->Write(response);
323 }
324 if (write_success) {
325 return Status::OK;
326 } else {
327 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
328 }
329 }
330};
331
Nicolas "Pixel" Noble6570b832016-07-18 23:29:50 +0200332void grpc::testing::interop::RunServer(
333 std::shared_ptr<ServerCredentials> creds) {
334 GPR_ASSERT(FLAGS_port != 0);
yangg06170572015-01-12 13:12:45 -0800335 std::ostringstream server_address;
Yang Gaodab70952015-01-23 16:06:36 -0800336 server_address << "0.0.0.0:" << FLAGS_port;
yangg06170572015-01-12 13:12:45 -0800337 TestServiceImpl service;
338
339 SimpleRequest request;
340 SimpleResponse response;
341
342 ServerBuilder builder;
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800343 builder.RegisterService(&service);
Nicolas Noblecfd60732015-03-18 16:27:43 -0700344 builder.AddListeningPort(server_address.str(), creds);
Mark D. Rothc99fd892016-09-06 08:16:13 -0700345 if (FLAGS_max_send_message_size >= 0) {
346 builder.SetMaxSendMessageSize(FLAGS_max_send_message_size);
347 }
yangg06170572015-01-12 13:12:45 -0800348 std::unique_ptr<Server> server(builder.BuildAndStart());
349 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
David Garcia Quintas7fa08e22016-12-04 22:11:57 -0800350 while (!gpr_atm_no_barrier_load(&g_got_sigint)) {
Craig Tillercf133f42015-02-26 14:05:56 -0800351 sleep(5);
yangg06170572015-01-12 13:12:45 -0800352 }
353}