blob: c05eb5d1461be32918df80dfd59606f01510999b [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.");
Mark D. Rothc99fd892016-09-06 08:16:13 -070059DEFINE_int32(max_send_message_size, -1, "The maximum send message size.");
yangg06170572015-01-12 13:12:45 -080060
61using grpc::Server;
62using grpc::ServerBuilder;
63using grpc::ServerContext;
64using grpc::ServerCredentials;
yangg06170572015-01-12 13:12:45 -080065using grpc::ServerReader;
66using grpc::ServerReaderWriter;
67using grpc::ServerWriter;
David Garcia Quintas56087522016-06-16 16:52:11 -070068using grpc::WriteOptions;
yangg06170572015-01-12 13:12:45 -080069using grpc::SslServerCredentialsOptions;
David Garcia Quintas7c0d9142015-07-23 04:58:20 -070070using grpc::testing::InteropServerContextInspector;
yangg06170572015-01-12 13:12:45 -080071using grpc::testing::Payload;
yangg06170572015-01-12 13:12:45 -080072using grpc::testing::SimpleRequest;
73using grpc::testing::SimpleResponse;
74using grpc::testing::StreamingInputCallRequest;
75using grpc::testing::StreamingInputCallResponse;
76using grpc::testing::StreamingOutputCallRequest;
77using grpc::testing::StreamingOutputCallResponse;
78using grpc::testing::TestService;
79using grpc::Status;
80
yang-gc10348a2016-02-19 16:05:10 -080081const char kEchoInitialMetadataKey[] = "x-grpc-test-echo-initial";
82const char kEchoTrailingBinMetadataKey[] = "x-grpc-test-echo-trailing-bin";
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -080083const char kEchoUserAgentKey[] = "x-grpc-test-echo-useragent";
yang-gc10348a2016-02-19 16:05:10 -080084
85void MaybeEchoMetadata(ServerContext* context) {
86 const auto& client_metadata = context->client_metadata();
87 GPR_ASSERT(client_metadata.count(kEchoInitialMetadataKey) <= 1);
88 GPR_ASSERT(client_metadata.count(kEchoTrailingBinMetadataKey) <= 1);
89
90 auto iter = client_metadata.find(kEchoInitialMetadataKey);
91 if (iter != client_metadata.end()) {
92 context->AddInitialMetadata(kEchoInitialMetadataKey, iter->second.data());
93 }
94 iter = client_metadata.find(kEchoTrailingBinMetadataKey);
95 if (iter != client_metadata.end()) {
96 context->AddTrailingMetadata(
97 kEchoTrailingBinMetadataKey,
98 grpc::string(iter->second.begin(), iter->second.end()));
99 }
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -0800100 // Check if client sent a magic key in the header that makes us echo
101 // back the user-agent (for testing purpose)
102 iter = client_metadata.find(kEchoUserAgentKey);
103 if (iter != client_metadata.end()) {
104 iter = client_metadata.find("user-agent");
105 if (iter != client_metadata.end()) {
106 context->AddInitialMetadata(kEchoUserAgentKey, iter->second.data());
107 }
108 }
yang-gc10348a2016-02-19 16:05:10 -0800109}
110
David Garcia Quintas56087522016-06-16 16:52:11 -0700111bool SetPayload(int size, Payload* payload) {
112 std::unique_ptr<char[]> body(new char[size]());
113 payload->set_body(body.get(), size);
yangg06170572015-01-12 13:12:45 -0800114 return true;
115}
116
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700117bool CheckExpectedCompression(const ServerContext& context,
David Garcia Quintas56087522016-06-16 16:52:11 -0700118 const bool compression_expected) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700119 const InteropServerContextInspector inspector(context);
120 const grpc_compression_algorithm received_compression =
121 inspector.GetCallCompressionAlgorithm();
122
David Garcia Quintas56087522016-06-16 16:52:11 -0700123 if (compression_expected) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700124 if (received_compression == GRPC_COMPRESS_NONE) {
125 // Expected some compression, got NONE. This is an error.
126 gpr_log(GPR_ERROR,
David Garcia Quintas56087522016-06-16 16:52:11 -0700127 "Expected compression but got uncompressed request from client.");
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700128 return false;
129 }
David Garcia Quintas56087522016-06-16 16:52:11 -0700130 if (!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS)) {
131 gpr_log(GPR_ERROR,
132 "Failure: Requested compression in a compressable request, but "
133 "compression bit in message flags not set.");
134 return false;
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700135 }
136 } else {
137 // Didn't expect compression -> make sure the request is uncompressed
138 if (inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS) {
139 gpr_log(GPR_ERROR,
140 "Failure: Didn't requested compression, but compression bit in "
141 "message flags set.");
142 return false;
143 }
144 }
145 return true;
146}
147
yangg06170572015-01-12 13:12:45 -0800148class TestServiceImpl : public TestService::Service {
149 public:
150 Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
151 grpc::testing::Empty* response) {
Makarand Dharmapurikarbfc7ada2016-02-22 15:42:18 -0800152 MaybeEchoMetadata(context);
yangg06170572015-01-12 13:12:45 -0800153 return Status::OK;
154 }
155
156 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
157 SimpleResponse* response) {
yang-gc10348a2016-02-19 16:05:10 -0800158 MaybeEchoMetadata(context);
David Garcia Quintas56087522016-06-16 16:52:11 -0700159 if (request->has_response_compressed()) {
160 const bool compression_requested = request->response_compressed().value();
161 gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s",
162 compression_requested ? "enabled" : "disabled", __func__);
163 if (compression_requested) {
164 // Any level would do, let's go for HIGH because we are overachievers.
165 context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH);
166 } else {
167 context->set_compression_level(GRPC_COMPRESS_LEVEL_NONE);
168 }
169 }
170 if (!CheckExpectedCompression(*context,
171 request->expect_compressed().value())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700172 return Status(grpc::StatusCode::INVALID_ARGUMENT,
173 "Compressed request expectation not met.");
174 }
David Garcia Quintas864d1862015-08-13 15:26:00 -0700175 if (request->response_size() > 0) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700176 if (!SetPayload(request->response_size(), response->mutable_payload())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700177 return Status(grpc::StatusCode::INVALID_ARGUMENT,
178 "Error creating payload.");
yangg06170572015-01-12 13:12:45 -0800179 }
180 }
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700181
182 if (request->has_response_status()) {
David Garcia Quintas2e1bb1b2015-08-10 14:05:57 -0700183 return Status(
184 static_cast<grpc::StatusCode>(request->response_status().code()),
185 request->response_status().message());
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700186 }
David Garcia Quintas80f39952015-07-21 16:07:36 -0700187
yangg06170572015-01-12 13:12:45 -0800188 return Status::OK;
189 }
190
191 Status StreamingOutputCall(
192 ServerContext* context, const StreamingOutputCallRequest* request,
193 ServerWriter<StreamingOutputCallResponse>* writer) {
194 StreamingOutputCallResponse response;
195 bool write_success = true;
yangg06170572015-01-12 13:12:45 -0800196 for (int i = 0; write_success && i < request->response_parameters_size();
197 i++) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700198 if (!SetPayload(request->response_parameters(i).size(),
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700199 response.mutable_payload())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700200 return Status(grpc::StatusCode::INVALID_ARGUMENT,
201 "Error creating payload.");
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700202 }
David Garcia Quintas56087522016-06-16 16:52:11 -0700203 WriteOptions wopts;
204 if (request->response_parameters(i).has_compressed()) {
David Garcia Quintas446f70e2016-06-21 17:13:28 -0700205 // Compress by default. Disabled on a per-message basis.
206 context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH);
David Garcia Quintas56087522016-06-16 16:52:11 -0700207 const bool compression_requested =
208 request->response_parameters(i).compressed().value();
209 gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s",
210 compression_requested ? "enabled" : "disabled", __func__);
211 if (!compression_requested) {
212 wopts.set_no_compression();
213 } // else, compression is already enabled via the context.
214 }
Makarand Dharmapurikar509246f2016-06-09 16:28:02 -0700215 int time_us;
216 if ((time_us = request->response_parameters(i).interval_us()) > 0) {
217 // Sleep before response if needed
218 gpr_timespec sleep_time =
219 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
220 gpr_time_from_micros(time_us, GPR_TIMESPAN));
221 gpr_sleep_until(sleep_time);
222 }
David Garcia Quintasdacce7a2016-06-21 16:40:20 -0700223 write_success = writer->Write(response, wopts);
yangg06170572015-01-12 13:12:45 -0800224 }
225 if (write_success) {
226 return Status::OK;
227 } else {
228 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
229 }
230 }
231
232 Status StreamingInputCall(ServerContext* context,
233 ServerReader<StreamingInputCallRequest>* reader,
234 StreamingInputCallResponse* response) {
235 StreamingInputCallRequest request;
236 int aggregated_payload_size = 0;
237 while (reader->Read(&request)) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700238 if (!CheckExpectedCompression(*context,
239 request.expect_compressed().value())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700240 return Status(grpc::StatusCode::INVALID_ARGUMENT,
241 "Compressed request expectation not met.");
242 }
yang-gf6befe82015-08-13 13:51:53 -0700243 if (request.has_payload()) {
yangg06170572015-01-12 13:12:45 -0800244 aggregated_payload_size += request.payload().body().size();
245 }
246 }
247 response->set_aggregated_payload_size(aggregated_payload_size);
248 return Status::OK;
249 }
250
251 Status FullDuplexCall(
252 ServerContext* context,
253 ServerReaderWriter<StreamingOutputCallResponse,
254 StreamingOutputCallRequest>* stream) {
yang-gc10348a2016-02-19 16:05:10 -0800255 MaybeEchoMetadata(context);
yangg06170572015-01-12 13:12:45 -0800256 StreamingOutputCallRequest request;
257 StreamingOutputCallResponse response;
258 bool write_success = true;
259 while (write_success && stream->Read(&request)) {
yang-g69563b92015-07-10 15:32:11 -0700260 if (request.response_parameters_size() != 0) {
261 response.mutable_payload()->set_type(request.payload().type());
262 response.mutable_payload()->set_body(
263 grpc::string(request.response_parameters(0).size(), '\0'));
Makarand Dharmapurikar509246f2016-06-09 16:28:02 -0700264 int time_us;
265 if ((time_us = request.response_parameters(0).interval_us()) > 0) {
266 // Sleep before response if needed
267 gpr_timespec sleep_time =
268 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
269 gpr_time_from_micros(time_us, GPR_TIMESPAN));
270 gpr_sleep_until(sleep_time);
271 }
yang-g69563b92015-07-10 15:32:11 -0700272 write_success = stream->Write(response);
yangg06170572015-01-12 13:12:45 -0800273 }
yangg06170572015-01-12 13:12:45 -0800274 }
275 if (write_success) {
276 return Status::OK;
277 } else {
278 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
279 }
280 }
281
282 Status HalfDuplexCall(
283 ServerContext* context,
284 ServerReaderWriter<StreamingOutputCallResponse,
285 StreamingOutputCallRequest>* stream) {
286 std::vector<StreamingOutputCallRequest> requests;
287 StreamingOutputCallRequest request;
288 while (stream->Read(&request)) {
289 requests.push_back(request);
290 }
291
292 StreamingOutputCallResponse response;
293 bool write_success = true;
294 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
295 response.mutable_payload()->set_type(requests[i].payload().type());
296 if (requests[i].response_parameters_size() == 0) {
297 return Status(grpc::StatusCode::INTERNAL,
298 "Request does not have response parameters.");
299 }
300 response.mutable_payload()->set_body(
301 grpc::string(requests[i].response_parameters(0).size(), '\0'));
302 write_success = stream->Write(response);
303 }
304 if (write_success) {
305 return Status::OK;
306 } else {
307 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
308 }
309 }
310};
311
Nicolas "Pixel" Noble6570b832016-07-18 23:29:50 +0200312void grpc::testing::interop::RunServer(
313 std::shared_ptr<ServerCredentials> creds) {
314 GPR_ASSERT(FLAGS_port != 0);
yangg06170572015-01-12 13:12:45 -0800315 std::ostringstream server_address;
Yang Gaodab70952015-01-23 16:06:36 -0800316 server_address << "0.0.0.0:" << FLAGS_port;
yangg06170572015-01-12 13:12:45 -0800317 TestServiceImpl service;
318
319 SimpleRequest request;
320 SimpleResponse response;
321
322 ServerBuilder builder;
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800323 builder.RegisterService(&service);
Nicolas Noblecfd60732015-03-18 16:27:43 -0700324 builder.AddListeningPort(server_address.str(), creds);
Mark D. Rothc99fd892016-09-06 08:16:13 -0700325 if (FLAGS_max_send_message_size >= 0) {
326 builder.SetMaxSendMessageSize(FLAGS_max_send_message_size);
327 }
yangg06170572015-01-12 13:12:45 -0800328 std::unique_ptr<Server> server(builder.BuildAndStart());
329 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
Vijay Pai96b7b522016-08-01 09:33:38 -0700330 while (!g_got_sigint) {
Craig Tillercf133f42015-02-26 14:05:56 -0800331 sleep(5);
yangg06170572015-01-12 13:12:45 -0800332 }
333}