blob: bb6793d95630e466f2257ef4dfc36db9aa270e06 [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 <signal.h>
35#include <unistd.h>
36
David Garcia Quintasc8993192015-07-22 09:10:39 -070037#include <fstream>
yangg06170572015-01-12 13:12:45 -080038#include <memory>
39#include <sstream>
40#include <thread>
41
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010042#include <gflags/gflags.h>
Craig Tillerf40df232016-03-25 13:38:14 -070043#include <grpc++/security/server_credentials.h>
yangg06170572015-01-12 13:12:45 -080044#include <grpc++/server.h>
45#include <grpc++/server_builder.h>
46#include <grpc++/server_context.h>
Craig Tillerf40df232016-03-25 13:38:14 -070047#include <grpc/grpc.h>
48#include <grpc/support/log.h>
49#include <grpc/support/useful.h>
David Garcia Quintasc8993192015-07-22 09:10:39 -070050
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.");
yangg06170572015-01-12 13:12:45 -080059DEFINE_int32(port, 0, "Server port.");
60
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
Craig Tiller2f3e2ec2015-02-20 13:07:50 -080081static bool got_sigint = false;
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()) {
94 context->AddInitialMetadata(kEchoInitialMetadataKey, iter->second.data());
95 }
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
158 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
159 SimpleResponse* response) {
yang-gc10348a2016-02-19 16:05:10 -0800160 MaybeEchoMetadata(context);
David Garcia Quintas56087522016-06-16 16:52:11 -0700161 if (request->has_response_compressed()) {
162 const bool compression_requested = request->response_compressed().value();
163 gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s",
164 compression_requested ? "enabled" : "disabled", __func__);
165 if (compression_requested) {
166 // Any level would do, let's go for HIGH because we are overachievers.
167 context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH);
168 } else {
169 context->set_compression_level(GRPC_COMPRESS_LEVEL_NONE);
170 }
171 }
172 if (!CheckExpectedCompression(*context,
173 request->expect_compressed().value())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700174 return Status(grpc::StatusCode::INVALID_ARGUMENT,
175 "Compressed request expectation not met.");
176 }
David Garcia Quintas864d1862015-08-13 15:26:00 -0700177 if (request->response_size() > 0) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700178 if (!SetPayload(request->response_size(), response->mutable_payload())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700179 return Status(grpc::StatusCode::INVALID_ARGUMENT,
180 "Error creating payload.");
yangg06170572015-01-12 13:12:45 -0800181 }
182 }
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700183
184 if (request->has_response_status()) {
David Garcia Quintas2e1bb1b2015-08-10 14:05:57 -0700185 return Status(
186 static_cast<grpc::StatusCode>(request->response_status().code()),
187 request->response_status().message());
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700188 }
David Garcia Quintas80f39952015-07-21 16:07:36 -0700189
yangg06170572015-01-12 13:12:45 -0800190 return Status::OK;
191 }
192
193 Status StreamingOutputCall(
194 ServerContext* context, const StreamingOutputCallRequest* request,
195 ServerWriter<StreamingOutputCallResponse>* writer) {
196 StreamingOutputCallResponse response;
197 bool write_success = true;
yangg06170572015-01-12 13:12:45 -0800198 for (int i = 0; write_success && i < request->response_parameters_size();
199 i++) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700200 if (!SetPayload(request->response_parameters(i).size(),
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700201 response.mutable_payload())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700202 return Status(grpc::StatusCode::INVALID_ARGUMENT,
203 "Error creating payload.");
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700204 }
David Garcia Quintas56087522016-06-16 16:52:11 -0700205 WriteOptions wopts;
206 if (request->response_parameters(i).has_compressed()) {
David Garcia Quintas446f70e2016-06-21 17:13:28 -0700207 // Compress by default. Disabled on a per-message basis.
208 context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH);
David Garcia Quintas56087522016-06-16 16:52:11 -0700209 const bool compression_requested =
210 request->response_parameters(i).compressed().value();
211 gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s",
212 compression_requested ? "enabled" : "disabled", __func__);
213 if (!compression_requested) {
214 wopts.set_no_compression();
215 } // else, compression is already enabled via the context.
216 }
Makarand Dharmapurikar509246f2016-06-09 16:28:02 -0700217 int time_us;
218 if ((time_us = request->response_parameters(i).interval_us()) > 0) {
219 // Sleep before response if needed
220 gpr_timespec sleep_time =
221 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
222 gpr_time_from_micros(time_us, GPR_TIMESPAN));
223 gpr_sleep_until(sleep_time);
224 }
David Garcia Quintasdacce7a2016-06-21 16:40:20 -0700225 write_success = writer->Write(response, wopts);
yangg06170572015-01-12 13:12:45 -0800226 }
227 if (write_success) {
228 return Status::OK;
229 } else {
230 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
231 }
232 }
233
234 Status StreamingInputCall(ServerContext* context,
235 ServerReader<StreamingInputCallRequest>* reader,
236 StreamingInputCallResponse* response) {
237 StreamingInputCallRequest request;
238 int aggregated_payload_size = 0;
239 while (reader->Read(&request)) {
David Garcia Quintas56087522016-06-16 16:52:11 -0700240 if (!CheckExpectedCompression(*context,
241 request.expect_compressed().value())) {
David Garcia Quintas74686ce2016-06-09 15:33:33 -0700242 return Status(grpc::StatusCode::INVALID_ARGUMENT,
243 "Compressed request expectation not met.");
244 }
yang-gf6befe82015-08-13 13:51:53 -0700245 if (request.has_payload()) {
yangg06170572015-01-12 13:12:45 -0800246 aggregated_payload_size += request.payload().body().size();
247 }
248 }
249 response->set_aggregated_payload_size(aggregated_payload_size);
250 return Status::OK;
251 }
252
253 Status FullDuplexCall(
254 ServerContext* context,
255 ServerReaderWriter<StreamingOutputCallResponse,
256 StreamingOutputCallRequest>* stream) {
yang-gc10348a2016-02-19 16:05:10 -0800257 MaybeEchoMetadata(context);
yangg06170572015-01-12 13:12:45 -0800258 StreamingOutputCallRequest request;
259 StreamingOutputCallResponse response;
260 bool write_success = true;
261 while (write_success && stream->Read(&request)) {
Mark D. Rothdbf2adc2016-07-07 08:19:27 -0700262 if (request.has_response_status()) {
263 return Status(
264 static_cast<grpc::StatusCode>(request.response_status().code()),
265 request.response_status().message());
266 }
yang-g69563b92015-07-10 15:32:11 -0700267 if (request.response_parameters_size() != 0) {
268 response.mutable_payload()->set_type(request.payload().type());
269 response.mutable_payload()->set_body(
270 grpc::string(request.response_parameters(0).size(), '\0'));
Makarand Dharmapurikar509246f2016-06-09 16:28:02 -0700271 int time_us;
272 if ((time_us = request.response_parameters(0).interval_us()) > 0) {
273 // Sleep before response if needed
274 gpr_timespec sleep_time =
275 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
276 gpr_time_from_micros(time_us, GPR_TIMESPAN));
277 gpr_sleep_until(sleep_time);
278 }
yang-g69563b92015-07-10 15:32:11 -0700279 write_success = stream->Write(response);
yangg06170572015-01-12 13:12:45 -0800280 }
yangg06170572015-01-12 13:12:45 -0800281 }
282 if (write_success) {
283 return Status::OK;
284 } else {
285 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
286 }
287 }
288
289 Status HalfDuplexCall(
290 ServerContext* context,
291 ServerReaderWriter<StreamingOutputCallResponse,
292 StreamingOutputCallRequest>* stream) {
293 std::vector<StreamingOutputCallRequest> requests;
294 StreamingOutputCallRequest request;
295 while (stream->Read(&request)) {
296 requests.push_back(request);
297 }
298
299 StreamingOutputCallResponse response;
300 bool write_success = true;
301 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
302 response.mutable_payload()->set_type(requests[i].payload().type());
303 if (requests[i].response_parameters_size() == 0) {
304 return Status(grpc::StatusCode::INTERNAL,
305 "Request does not have response parameters.");
306 }
307 response.mutable_payload()->set_body(
308 grpc::string(requests[i].response_parameters(0).size(), '\0'));
309 write_success = stream->Write(response);
310 }
311 if (write_success) {
312 return Status::OK;
313 } else {
314 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
315 }
316 }
317};
318
319void RunServer() {
320 std::ostringstream server_address;
Yang Gaodab70952015-01-23 16:06:36 -0800321 server_address << "0.0.0.0:" << FLAGS_port;
yangg06170572015-01-12 13:12:45 -0800322 TestServiceImpl service;
323
324 SimpleRequest request;
325 SimpleResponse response;
326
327 ServerBuilder builder;
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800328 builder.RegisterService(&service);
Yang Gaoa4002072015-04-09 23:25:21 -0700329 std::shared_ptr<ServerCredentials> creds =
330 grpc::testing::CreateInteropServerCredentials();
Nicolas Noblecfd60732015-03-18 16:27:43 -0700331 builder.AddListeningPort(server_address.str(), creds);
yangg06170572015-01-12 13:12:45 -0800332 std::unique_ptr<Server> server(builder.BuildAndStart());
333 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800334 while (!got_sigint) {
Craig Tillercf133f42015-02-26 14:05:56 -0800335 sleep(5);
yangg06170572015-01-12 13:12:45 -0800336 }
337}
338
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800339static void sigint_handler(int x) { got_sigint = true; }
340
yangg06170572015-01-12 13:12:45 -0800341int main(int argc, char** argv) {
Yang Gao103837e2015-04-15 15:23:54 -0700342 grpc::testing::InitTest(&argc, &argv, true);
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800343 signal(SIGINT, sigint_handler);
yangg06170572015-01-12 13:12:45 -0800344
345 GPR_ASSERT(FLAGS_port != 0);
346 RunServer();
347
yangg06170572015-01-12 13:12:45 -0800348 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800349}