blob: ea83aaf2c12f7b57e32322dc0cdc8fff5b875cdd [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * 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.
Craig Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// 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#endregion
33
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080034using System;
Jan Tattermuschd233d3a2015-02-06 14:15:00 -080035using System.Collections.Generic;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080036using System.Text.RegularExpressions;
Jan Tattermusche5c44602015-05-01 11:12:34 -070037using System.Threading;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070038using System.Threading.Tasks;
Jan Tattermusch1ca56b92015-04-27 11:03:06 -070039
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080040using Google.ProtocolBuffers;
Jan Tattermusch075dde42015-03-11 18:21:00 -070041using grpc.testing;
Jan Tattermuschdca6e882015-04-22 16:56:27 -070042using Grpc.Auth;
Jan Tattermusch30868622015-02-19 09:22:33 -080043using Grpc.Core;
44using Grpc.Core.Utils;
45using NUnit.Framework;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080046
Jan Tattermusch8b86b152015-02-19 21:01:05 -080047namespace Grpc.IntegrationTesting
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080048{
Jan Tattermusch503bbac2015-02-26 18:19:47 -080049 public class InteropClient
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080050 {
Jan Tattermuschdca6e882015-04-22 16:56:27 -070051 private const string ServiceAccountUser = "155450119199-3psnrh1sdr3d8cpj1v46naggf81mhdnk@developer.gserviceaccount.com";
52 private const string ComputeEngineUser = "155450119199-r5aaqa2vqoa9g5mv2m6s3m1l293rlmel@developer.gserviceaccount.com";
53 private const string AuthScope = "https://www.googleapis.com/auth/xapi.zoo";
54 private const string AuthScopeResponse = "xapi.zoo";
55
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080056 private class ClientOptions
57 {
58 public bool help;
Jan Tattermusch075dde42015-03-11 18:21:00 -070059 public string serverHost = "127.0.0.1";
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080060 public string serverHostOverride = TestCredentials.DefaultHostOverride;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080061 public int? serverPort;
Jan Tattermusch15329232015-03-02 15:32:47 -080062 public string testCase = "large_unary";
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080063 public bool useTls;
64 public bool useTestCa;
65 }
66
67 ClientOptions options;
68
Jan Tattermusch503bbac2015-02-26 18:19:47 -080069 private InteropClient(ClientOptions options)
Jan Tattermusch392d1e02015-02-09 11:13:51 -080070 {
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080071 this.options = options;
72 }
73
Jan Tattermusch503bbac2015-02-26 18:19:47 -080074 public static void Run(string[] args)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080075 {
76 Console.WriteLine("gRPC C# interop testing client");
77 ClientOptions options = ParseArguments(args);
78
79 if (options.serverHost == null || !options.serverPort.HasValue || options.testCase == null)
80 {
81 Console.WriteLine("Missing required argument.");
82 Console.WriteLine();
83 options.help = true;
84 }
85
86 if (options.help)
87 {
88 Console.WriteLine("Usage:");
89 Console.WriteLine(" --server_host=HOSTNAME");
90 Console.WriteLine(" --server_host_override=HOSTNAME");
91 Console.WriteLine(" --server_port=PORT");
92 Console.WriteLine(" --test_case=TESTCASE");
93 Console.WriteLine(" --use_tls=BOOLEAN");
94 Console.WriteLine(" --use_test_ca=BOOLEAN");
Craig Tiller190d3602015-02-18 09:23:38 -080095 Console.WriteLine();
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080096 Environment.Exit(1);
97 }
98
Jan Tattermusch503bbac2015-02-26 18:19:47 -080099 var interopClient = new InteropClient(options);
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800100 interopClient.Run();
101 }
102
103 private void Run()
104 {
Jan Tattermusch15329232015-03-02 15:32:47 -0800105 Credentials credentials = null;
106 if (options.useTls)
107 {
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800108 credentials = TestCredentials.CreateTestClientCredentials(options.useTestCa);
Jan Tattermusch15329232015-03-02 15:32:47 -0800109 }
110
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700111 List<ChannelOption> channelOptions = null;
Jan Tattermusch15329232015-03-02 15:32:47 -0800112 if (!string.IsNullOrEmpty(options.serverHostOverride))
113 {
Jan Tattermuschc8f7d102015-06-08 17:53:45 -0700114 channelOptions = new List<ChannelOption>
115 {
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700116 new ChannelOption(ChannelOptions.SslTargetNameOverride, options.serverHostOverride)
117 };
Jan Tattermusch15329232015-03-02 15:32:47 -0800118 }
119
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700120 using (Channel channel = new Channel(options.serverHost, options.serverPort.Value, credentials, channelOptions))
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800121 {
Jan Tattermuschb5332812015-07-14 19:29:35 -0700122 TestService.TestServiceClient client = new TestService.TestServiceClient(channel);
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700123 if (options.testCase == "service_account_creds" || options.testCase == "compute_engine_creds")
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700124 {
125 var credential = GoogleCredential.GetApplicationDefault();
126 if (credential.IsCreateScopedRequired)
127 {
128 credential = credential.CreateScoped(new[] { AuthScope });
129 }
Jan Tattermuschb5332812015-07-14 19:29:35 -0700130 client.HeaderInterceptor = OAuth2InterceptorFactory.Create(credential);
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700131 }
132
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800133 RunTestCase(options.testCase, client);
134 }
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800135 GrpcEnvironment.Shutdown();
136 }
137
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700138 private void RunTestCase(string testCase, TestService.TestServiceClient client)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800139 {
140 switch (testCase)
141 {
142 case "empty_unary":
143 RunEmptyUnary(client);
144 break;
145 case "large_unary":
146 RunLargeUnary(client);
147 break;
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800148 case "client_streaming":
149 RunClientStreaming(client);
150 break;
151 case "server_streaming":
152 RunServerStreaming(client);
153 break;
154 case "ping_pong":
155 RunPingPong(client);
156 break;
157 case "empty_stream":
158 RunEmptyStream(client);
159 break;
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700160 case "service_account_creds":
161 RunServiceAccountCreds(client);
162 break;
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700163 case "compute_engine_creds":
164 RunComputeEngineCreds(client);
165 break;
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700166 case "oauth2_auth_token":
167 RunOAuth2AuthToken(client);
168 break;
169 case "per_rpc_creds":
170 RunPerRpcCreds(client);
171 break;
Jan Tattermusche5c44602015-05-01 11:12:34 -0700172 case "cancel_after_begin":
173 RunCancelAfterBegin(client);
174 break;
175 case "cancel_after_first_response":
176 RunCancelAfterFirstResponse(client);
177 break;
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800178 case "benchmark_empty_unary":
179 RunBenchmarkEmptyUnary(client);
180 break;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800181 default:
182 throw new ArgumentException("Unknown test case " + testCase);
183 }
184 }
185
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700186 public static void RunEmptyUnary(TestService.ITestServiceClient client)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800187 {
188 Console.WriteLine("running empty_unary");
189 var response = client.EmptyCall(Empty.DefaultInstance);
190 Assert.IsNotNull(response);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800191 Console.WriteLine("Passed!");
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800192 }
193
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700194 public static void RunLargeUnary(TestService.ITestServiceClient client)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800195 {
196 Console.WriteLine("running large_unary");
197 var request = SimpleRequest.CreateBuilder()
198 .SetResponseType(PayloadType.COMPRESSABLE)
199 .SetResponseSize(314159)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800200 .SetPayload(CreateZerosPayload(271828))
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800201 .Build();
Craig Tiller190d3602015-02-18 09:23:38 -0800202
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800203 var response = client.UnaryCall(request);
204
205 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
206 Assert.AreEqual(314159, response.Payload.Body.Length);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800207 Console.WriteLine("Passed!");
208 }
209
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700210 public static void RunClientStreaming(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800211 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700212 Task.Run(async () =>
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800213 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700214 Console.WriteLine("running client_streaming");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800215
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700216 var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => StreamingInputCallRequest.CreateBuilder().SetPayload(CreateZerosPayload(size)).Build());
217
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700218 using (var call = client.StreamingInputCall())
219 {
220 await call.RequestStream.WriteAll(bodySizes);
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700221
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700222 var response = await call.Result;
223 Assert.AreEqual(74922, response.AggregatedPayloadSize);
224 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700225 Console.WriteLine("Passed!");
226 }).Wait();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800227 }
228
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700229 public static void RunServerStreaming(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800230 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700231 Task.Run(async () =>
232 {
233 Console.WriteLine("running server_streaming");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800234
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700235 var bodySizes = new List<int> { 31415, 9, 2653, 58979 };
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800236
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700237 var request = StreamingOutputCallRequest.CreateBuilder()
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800238 .SetResponseType(PayloadType.COMPRESSABLE)
239 .AddRangeResponseParameters(bodySizes.ConvertAll(
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700240 (size) => ResponseParameters.CreateBuilder().SetSize(size).Build()))
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800241 .Build();
242
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700243 using (var call = client.StreamingOutputCall(request))
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700244 {
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700245 var responseList = await call.ResponseStream.ToList();
246 foreach (var res in responseList)
247 {
248 Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type);
249 }
250 CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length));
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700251 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700252 Console.WriteLine("Passed!");
253 }).Wait();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800254 }
255
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700256 public static void RunPingPong(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800257 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700258 Task.Run(async () =>
259 {
260 Console.WriteLine("running ping_pong");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800261
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700262 using (var call = client.FullDuplexCall())
263 {
264 await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
265 .SetResponseType(PayloadType.COMPRESSABLE)
266 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
267 .SetPayload(CreateZerosPayload(27182)).Build());
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800268
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700269 Assert.IsTrue(await call.ResponseStream.MoveNext());
270 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
271 Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);
Craig Tiller190d3602015-02-18 09:23:38 -0800272
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700273 await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
274 .SetResponseType(PayloadType.COMPRESSABLE)
275 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(9))
276 .SetPayload(CreateZerosPayload(8)).Build());
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800277
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700278 Assert.IsTrue(await call.ResponseStream.MoveNext());
279 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
280 Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800281
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700282 await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
283 .SetResponseType(PayloadType.COMPRESSABLE)
284 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(2653))
285 .SetPayload(CreateZerosPayload(1828)).Build());
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800286
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700287 Assert.IsTrue(await call.ResponseStream.MoveNext());
288 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
289 Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800290
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700291 await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
292 .SetResponseType(PayloadType.COMPRESSABLE)
293 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(58979))
294 .SetPayload(CreateZerosPayload(45904)).Build());
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800295
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700296 Assert.IsTrue(await call.ResponseStream.MoveNext());
297 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
298 Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800299
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700300 await call.RequestStream.CompleteAsync();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800301
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700302 Assert.IsFalse(await call.ResponseStream.MoveNext());
303 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700304 Console.WriteLine("Passed!");
305 }).Wait();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800306 }
307
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700308 public static void RunEmptyStream(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800309 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700310 Task.Run(async () =>
311 {
312 Console.WriteLine("running empty_stream");
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700313 using (var call = client.FullDuplexCall())
314 {
315 await call.RequestStream.CompleteAsync();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800316
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700317 var responseList = await call.ResponseStream.ToList();
318 Assert.AreEqual(0, responseList.Count);
319 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700320 Console.WriteLine("Passed!");
321 }).Wait();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800322 }
323
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700324 public static void RunServiceAccountCreds(TestService.ITestServiceClient client)
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700325 {
326 Console.WriteLine("running service_account_creds");
327 var request = SimpleRequest.CreateBuilder()
328 .SetResponseType(PayloadType.COMPRESSABLE)
329 .SetResponseSize(314159)
330 .SetPayload(CreateZerosPayload(271828))
331 .SetFillUsername(true)
332 .SetFillOauthScope(true)
333 .Build();
334
335 var response = client.UnaryCall(request);
336
337 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
338 Assert.AreEqual(314159, response.Payload.Body.Length);
339 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
340 Assert.AreEqual(ServiceAccountUser, response.Username);
341 Console.WriteLine("Passed!");
342 }
343
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700344 public static void RunComputeEngineCreds(TestService.ITestServiceClient client)
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700345 {
346 Console.WriteLine("running compute_engine_creds");
347 var request = SimpleRequest.CreateBuilder()
348 .SetResponseType(PayloadType.COMPRESSABLE)
349 .SetResponseSize(314159)
350 .SetPayload(CreateZerosPayload(271828))
351 .SetFillUsername(true)
352 .SetFillOauthScope(true)
353 .Build();
354
355 var response = client.UnaryCall(request);
356
357 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
358 Assert.AreEqual(314159, response.Payload.Body.Length);
359 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
Jan Tattermuschaa5fba42015-04-27 21:28:58 -0700360 Assert.AreEqual(ComputeEngineUser, response.Username);
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700361 Console.WriteLine("Passed!");
362 }
363
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700364 public static void RunOAuth2AuthToken(TestService.TestServiceClient client)
365 {
366 Console.WriteLine("running oauth2_auth_token");
367 var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope });
368 Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result);
369 string oauth2Token = credential.Token.AccessToken;
370
371 // Intercept calls with an OAuth2 token obtained out-of-band.
372 client.HeaderInterceptor = new MetadataInterceptorDelegate((metadata) =>
373 {
374 metadata.Add(new Metadata.Entry("Authorization", "Bearer " + oauth2Token));
375 });
376
377 var request = SimpleRequest.CreateBuilder()
378 .SetFillUsername(true)
379 .SetFillOauthScope(true)
380 .Build();
381
382 var response = client.UnaryCall(request);
383
384 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
385 Assert.AreEqual(ServiceAccountUser, response.Username);
386 Console.WriteLine("Passed!");
387 }
388
389 public static void RunPerRpcCreds(TestService.TestServiceClient client)
390 {
391 Console.WriteLine("running per_rpc_creds");
392
393 var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope });
394 Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result);
395 string oauth2Token = credential.Token.AccessToken;
396
397 var request = SimpleRequest.CreateBuilder()
398 .SetFillUsername(true)
399 .SetFillOauthScope(true)
400 .Build();
401
402 var response = client.UnaryCall(request, headers: new Metadata { new Metadata.Entry("Authorization", "Bearer " + oauth2Token) } );
403
404 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
405 Assert.AreEqual(ServiceAccountUser, response.Username);
406 Console.WriteLine("Passed!");
407 }
408
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700409 public static void RunCancelAfterBegin(TestService.ITestServiceClient client)
Jan Tattermusche5c44602015-05-01 11:12:34 -0700410 {
411 Task.Run(async () =>
412 {
413 Console.WriteLine("running cancel_after_begin");
414
415 var cts = new CancellationTokenSource();
Jan Tattermuschfd953a52015-07-14 21:41:29 -0700416 using (var call = client.StreamingInputCall(cancellationToken: cts.Token))
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700417 {
418 // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it.
419 await Task.Delay(1000);
420 cts.Cancel();
Jan Tattermusche5c44602015-05-01 11:12:34 -0700421
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700422 try
423 {
424 var response = await call.Result;
425 Assert.Fail();
426 }
427 catch (RpcException e)
428 {
429 Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
430 }
Jan Tattermusche5c44602015-05-01 11:12:34 -0700431 }
432 Console.WriteLine("Passed!");
433 }).Wait();
434 }
435
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700436 public static void RunCancelAfterFirstResponse(TestService.ITestServiceClient client)
Jan Tattermusche5c44602015-05-01 11:12:34 -0700437 {
438 Task.Run(async () =>
439 {
440 Console.WriteLine("running cancel_after_first_response");
441
442 var cts = new CancellationTokenSource();
Jan Tattermuschfd953a52015-07-14 21:41:29 -0700443 using (var call = client.FullDuplexCall(cancellationToken: cts.Token))
Jan Tattermusche5c44602015-05-01 11:12:34 -0700444 {
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700445 await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
446 .SetResponseType(PayloadType.COMPRESSABLE)
447 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
448 .SetPayload(CreateZerosPayload(27182)).Build());
449
450 Assert.IsTrue(await call.ResponseStream.MoveNext());
451 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
452 Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);
453
454 cts.Cancel();
455
456 try
457 {
458 await call.ResponseStream.MoveNext();
459 Assert.Fail();
460 }
461 catch (RpcException e)
462 {
463 Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
464 }
Jan Tattermusche5c44602015-05-01 11:12:34 -0700465 }
466 Console.WriteLine("Passed!");
467 }).Wait();
468 }
469
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800470 // This is not an official interop test, but it's useful.
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700471 public static void RunBenchmarkEmptyUnary(TestService.ITestServiceClient client)
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800472 {
473 BenchmarkUtil.RunBenchmark(10000, 10000,
Jan Tattermusch075dde42015-03-11 18:21:00 -0700474 () => { client.EmptyCall(Empty.DefaultInstance); });
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800475 }
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800476
Jan Tattermusch075dde42015-03-11 18:21:00 -0700477 private static Payload CreateZerosPayload(int size)
478 {
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800479 return Payload.CreateBuilder().SetBody(ByteString.CopyFrom(new byte[size])).Build();
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800480 }
481
482 private static ClientOptions ParseArguments(string[] args)
483 {
484 var options = new ClientOptions();
Jan Tattermusch075dde42015-03-11 18:21:00 -0700485 foreach (string arg in args)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800486 {
487 ParseArgument(arg, options);
488 if (options.help)
489 {
490 break;
491 }
492 }
493 return options;
494 }
495
496 private static void ParseArgument(string arg, ClientOptions options)
497 {
498 Match match;
499 match = Regex.Match(arg, "--server_host=(.*)");
500 if (match.Success)
501 {
502 options.serverHost = match.Groups[1].Value.Trim();
503 return;
504 }
505
506 match = Regex.Match(arg, "--server_host_override=(.*)");
507 if (match.Success)
508 {
509 options.serverHostOverride = match.Groups[1].Value.Trim();
510 return;
511 }
512
513 match = Regex.Match(arg, "--server_port=(.*)");
514 if (match.Success)
515 {
516 options.serverPort = int.Parse(match.Groups[1].Value.Trim());
517 return;
518 }
519
520 match = Regex.Match(arg, "--test_case=(.*)");
521 if (match.Success)
522 {
523 options.testCase = match.Groups[1].Value.Trim();
524 return;
525 }
526
527 match = Regex.Match(arg, "--use_tls=(.*)");
528 if (match.Success)
529 {
530 options.useTls = bool.Parse(match.Groups[1].Value.Trim());
531 return;
532 }
533
534 match = Regex.Match(arg, "--use_test_ca=(.*)");
535 if (match.Success)
536 {
537 options.useTestCa = bool.Parse(match.Groups[1].Value.Trim());
538 return;
539 }
540
541 Console.WriteLine(string.Format("Unrecognized argument \"{0}\"", arg));
542 options.help = true;
543 }
544 }
545}