blob: 384d8da720e7de31061c69ca5ce2a4ca4b5d0e5f [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
David Garcia Quintas74686ce2016-06-09 15:33:33 -070050#include "src/core/lib/transport/byte_stream.h"
Craig Tiller1b4e3302015-12-17 16:35:00 -080051#include "src/proto/grpc/testing/empty.grpc.pb.h"
52#include "src/proto/grpc/testing/messages.grpc.pb.h"
Craig Tillerf40df232016-03-25 13:38:14 -070053#include "src/proto/grpc/testing/test.grpc.pb.h"
54#include "test/cpp/interop/server_helper.h"
55#include "test/cpp/util/test_config.h"
yangg06170572015-01-12 13:12:45 -080056
yang-g035cf092015-09-18 12:11:05 -070057DEFINE_bool(use_tls, false, "Whether to use tls.");
yangg06170572015-01-12 13:12:45 -080058DEFINE_int32(port, 0, "Server port.");
59
60using grpc::Server;
61using grpc::ServerBuilder;
62using grpc::ServerContext;
63using grpc::ServerCredentials;
yangg06170572015-01-12 13:12:45 -080064using grpc::ServerReader;
65using grpc::ServerReaderWriter;
66using grpc::ServerWriter;
David Garcia Quintas56087522016-06-16 16:52:11 -070067using grpc::WriteOptions;
yangg06170572015-01-12 13:12:45 -080068using grpc::SslServerCredentialsOptions;
David Garcia Quintas7c0d9142015-07-23 04:58:20 -070069using grpc::testing::InteropServerContextInspector;
yangg06170572015-01-12 13:12:45 -080070using grpc::testing::Payload;
yangg06170572015-01-12 13:12:45 -080071using grpc::testing::SimpleRequest;
72using grpc::testing::SimpleResponse;
73using grpc::testing::StreamingInputCallRequest;
74using grpc::testing::StreamingInputCallResponse;
75using grpc::testing::StreamingOutputCallRequest;
76using grpc::testing::StreamingOutputCallResponse;
77using grpc::testing::TestService;
78using grpc::Status;
79
Craig Tiller2f3e2ec2015-02-20 13:07:50 -080080static bool got_sigint = false;
81
yang-gc10348a2016-02-19 16:05:10 -080082const char kEchoInitialMetadataKey[] = "x-grpc-test-echo-initial";
83const char kEchoTrailingBinMetadataKey[] = "x-grpc-test-echo-trailing-bin";
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -080084const char kEchoUserAgentKey[] = "x-grpc-test-echo-useragent";
yang-gc10348a2016-02-19 16:05:10 -080085
86void MaybeEchoMetadata(ServerContext* context) {
87 const auto& client_metadata = context->client_metadata();
88 GPR_ASSERT(client_metadata.count(kEchoInitialMetadataKey) <= 1);
89 GPR_ASSERT(client_metadata.count(kEchoTrailingBinMetadataKey) <= 1);
90
91 auto iter = client_metadata.find(kEchoInitialMetadataKey);
92 if (iter != client_metadata.end()) {
93 context->AddInitialMetadata(kEchoInitialMetadataKey, iter->second.data());
94 }
95 iter = client_metadata.find(kEchoTrailingBinMetadataKey);
96 if (iter != client_metadata.end()) {
97 context->AddTrailingMetadata(
98 kEchoTrailingBinMetadataKey,
99 grpc::string(iter->second.begin(), iter->second.end()));
100 }
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -0800101 // Check if client sent a magic key in the header that makes us echo
102 // back the user-agent (for testing purpose)
103 iter = client_metadata.find(kEchoUserAgentKey);
104 if (iter != client_metadata.end()) {
105 iter = client_metadata.find("user-agent");
106 if (iter != client_metadata.end()) {
107 context->AddInitialMetadata(kEchoUserAgentKey, iter->second.data());
108 }
109 }
yang-gc10348a2016-02-19 16:05:10 -0800110}
111
David Garcia Quintas56087522016-06-16 16:52:11 -0700112bool SetPayload(int size, Payload* payload) {
113 std::unique_ptr<char[]> body(new char[size]());
114 payload->set_body(body.get(), size);
yangg06170572015-01-12 13:12:45 -0800115 return true;
116}
117
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700118bool CheckExpectedCompression(const ServerContext& context,
David Garcia Quintas56087522016-06-16 16:52:11 -0700119 const bool compression_expected) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700120 const InteropServerContextInspector inspector(context);
121 const grpc_compression_algorithm received_compression =
122 inspector.GetCallCompressionAlgorithm();
123
David Garcia Quintas56087522016-06-16 16:52:11 -0700124 if (compression_expected) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700125 if (received_compression == GRPC_COMPRESS_NONE) {
126 // Expected some compression, got NONE. This is an error.
127 gpr_log(GPR_ERROR,
David Garcia Quintas56087522016-06-16 16:52:11 -0700128 "Expected compression but got uncompressed request from client.");
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700129 return false;
130 }
David Garcia Quintas56087522016-06-16 16:52:11 -0700131 if (!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS)) {
132 gpr_log(GPR_ERROR,
133 "Failure: Requested compression in a compressable request, but "
134 "compression bit in message flags not set.");
135 return false;
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700136 }
137 } else {
138 // Didn't expect compression -> make sure the request is uncompressed
139 if (inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS) {
140 gpr_log(GPR_ERROR,
141 "Failure: Didn't requested compression, but compression bit in "
142 "message flags set.");
143 return false;
144 }
145 }
146 return true;
147}
148
yangg06170572015-01-12 13:12:45 -0800149class TestServiceImpl : public TestService::Service {
150 public:
151 Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
152 grpc::testing::Empty* response) {
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -0800153 MaybeEchoMetadata(context);
yangg06170572015-01-12 13:12:45 -0800154 return Status::OK;
155 }
156
157 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
158 SimpleResponse* response) {
yang-gc10348a2016-02-19 16:05:10 -0800159 MaybeEchoMetadata(context);
David Garcia Quintas56087522016-06-16 16:52:11 -0700160 if (request->has_response_compressed()) {
161 const bool compression_requested = request->response_compressed().value();
162 gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s",
163 compression_requested ? "enabled" : "disabled", __func__);
164 if (compression_requested) {
165 // Any level would do, let's go for HIGH because we are overachievers.
166 context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH);
167 } else {
168 context->set_compression_level(GRPC_COMPRESS_LEVEL_NONE);
169 }
170 }
171 if (!CheckExpectedCompression(*context,
172 request->expect_compressed().value())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700173 return Status(grpc::StatusCode::INVALID_ARGUMENT,
174 "Compressed request expectation not met.");
175 }
David Garcia Quintas864d1862015-08-13 15:26:00 -0700176 if (request->response_size() > 0) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700177 if (!SetPayload(request->response_size(), response->mutable_payload())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700178 return Status(grpc::StatusCode::INVALID_ARGUMENT,
179 "Error creating payload.");
yangg06170572015-01-12 13:12:45 -0800180 }
181 }
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700182
183 if (request->has_response_status()) {
David Garcia Quintas2e1bb1b2015-08-10 14:05:57 -0700184 return Status(
185 static_cast<grpc::StatusCode>(request->response_status().code()),
186 request->response_status().message());
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700187 }
David Garcia Quintas80f39952015-07-21 16:07:36 -0700188
yangg06170572015-01-12 13:12:45 -0800189 return Status::OK;
190 }
191
192 Status StreamingOutputCall(
193 ServerContext* context, const StreamingOutputCallRequest* request,
194 ServerWriter<StreamingOutputCallResponse>* writer) {
195 StreamingOutputCallResponse response;
196 bool write_success = true;
yangg06170572015-01-12 13:12:45 -0800197 for (int i = 0; write_success && i < request->response_parameters_size();
198 i++) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700199 if (!SetPayload(request->response_parameters(i).size(),
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700200 response.mutable_payload())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700201 return Status(grpc::StatusCode::INVALID_ARGUMENT,
202 "Error creating payload.");
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700203 }
David Garcia Quintas56087522016-06-16 16:52:11 -0700204 WriteOptions wopts;
205 if (request->response_parameters(i).has_compressed()) {
David Garcia Quintas446f70e2016-06-21 17:13:28 -0700206 // Compress by default. Disabled on a per-message basis.
207 context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH);
David Garcia Quintas56087522016-06-16 16:52:11 -0700208 const bool compression_requested =
209 request->response_parameters(i).compressed().value();
210 gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s",
211 compression_requested ? "enabled" : "disabled", __func__);
212 if (!compression_requested) {
213 wopts.set_no_compression();
214 } // else, compression is already enabled via the context.
215 }
Makarand Dharmapurikar509246f2016-06-09 16:28:02 -0700216 int time_us;
217 if ((time_us = request->response_parameters(i).interval_us()) > 0) {
218 // Sleep before response if needed
219 gpr_timespec sleep_time =
220 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
221 gpr_time_from_micros(time_us, GPR_TIMESPAN));
222 gpr_sleep_until(sleep_time);
223 }
David Garcia Quintasdacce7a2016-06-21 16:40:20 -0700224 write_success = writer->Write(response, wopts);
yangg06170572015-01-12 13:12:45 -0800225 }
226 if (write_success) {
227 return Status::OK;
228 } else {
229 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
230 }
231 }
232
233 Status StreamingInputCall(ServerContext* context,
234 ServerReader<StreamingInputCallRequest>* reader,
235 StreamingInputCallResponse* response) {
236 StreamingInputCallRequest request;
237 int aggregated_payload_size = 0;
238 while (reader->Read(&request)) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700239 if (!CheckExpectedCompression(*context,
240 request.expect_compressed().value())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700241 return Status(grpc::StatusCode::INVALID_ARGUMENT,
242 "Compressed request expectation not met.");
243 }
yang-gf6befe82015-08-13 13:51:53 -0700244 if (request.has_payload()) {
yangg06170572015-01-12 13:12:45 -0800245 aggregated_payload_size += request.payload().body().size();
246 }
247 }
248 response->set_aggregated_payload_size(aggregated_payload_size);
249 return Status::OK;
250 }
251
252 Status FullDuplexCall(
253 ServerContext* context,
254 ServerReaderWriter<StreamingOutputCallResponse,
255 StreamingOutputCallRequest>* stream) {
yang-gc10348a2016-02-19 16:05:10 -0800256 MaybeEchoMetadata(context);
yangg06170572015-01-12 13:12:45 -0800257 StreamingOutputCallRequest request;
258 StreamingOutputCallResponse response;
259 bool write_success = true;
260 while (write_success && stream->Read(&request)) {
yang-g69563b92015-07-10 15:32:11 -0700261 if (request.response_parameters_size() != 0) {
262 response.mutable_payload()->set_type(request.payload().type());
263 response.mutable_payload()->set_body(
264 grpc::string(request.response_parameters(0).size(), '\0'));
Makarand Dharmapurikar509246f2016-06-09 16:28:02 -0700265 int time_us;
266 if ((time_us = request.response_parameters(0).interval_us()) > 0) {
267 // Sleep before response if needed
268 gpr_timespec sleep_time =
269 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
270 gpr_time_from_micros(time_us, GPR_TIMESPAN));
271 gpr_sleep_until(sleep_time);
272 }
yang-g69563b92015-07-10 15:32:11 -0700273 write_success = stream->Write(response);
yangg06170572015-01-12 13:12:45 -0800274 }
yangg06170572015-01-12 13:12:45 -0800275 }
276 if (write_success) {
277 return Status::OK;
278 } else {
279 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
280 }
281 }
282
283 Status HalfDuplexCall(
284 ServerContext* context,
285 ServerReaderWriter<StreamingOutputCallResponse,
286 StreamingOutputCallRequest>* stream) {
287 std::vector<StreamingOutputCallRequest> requests;
288 StreamingOutputCallRequest request;
289 while (stream->Read(&request)) {
290 requests.push_back(request);
291 }
292
293 StreamingOutputCallResponse response;
294 bool write_success = true;
295 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
296 response.mutable_payload()->set_type(requests[i].payload().type());
297 if (requests[i].response_parameters_size() == 0) {
298 return Status(grpc::StatusCode::INTERNAL,
299 "Request does not have response parameters.");
300 }
301 response.mutable_payload()->set_body(
302 grpc::string(requests[i].response_parameters(0).size(), '\0'));
303 write_success = stream->Write(response);
304 }
305 if (write_success) {
306 return Status::OK;
307 } else {
308 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
309 }
310 }
311};
312
Nicolas "Pixel" Noble6570b832016-07-18 23:29:50 +0200313void grpc::testing::interop::RunServer(
314 std::shared_ptr<ServerCredentials> creds) {
315 GPR_ASSERT(FLAGS_port != 0);
yangg06170572015-01-12 13:12:45 -0800316 std::ostringstream server_address;
Yang Gaodab70952015-01-23 16:06:36 -0800317 server_address << "0.0.0.0:" << FLAGS_port;
yangg06170572015-01-12 13:12:45 -0800318 TestServiceImpl service;
319
320 SimpleRequest request;
321 SimpleResponse response;
322
323 ServerBuilder builder;
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800324 builder.RegisterService(&service);
Nicolas Noblecfd60732015-03-18 16:27:43 -0700325 builder.AddListeningPort(server_address.str(), creds);
yangg06170572015-01-12 13:12:45 -0800326 std::unique_ptr<Server> server(builder.BuildAndStart());
327 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800328 while (!got_sigint) {
Craig Tillercf133f42015-02-26 14:05:56 -0800329 sleep(5);
yangg06170572015-01-12 13:12:45 -0800330 }
331}