blob: 3ea1539230760a041dcb87d50a999b14a3f06f89 [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 Tattermuschdca6e882015-04-22 16:56:27 -070040using Grpc.Auth;
Jan Tattermusch30868622015-02-19 09:22:33 -080041using Grpc.Core;
42using Grpc.Core.Utils;
Jan Tattermusch8644aea2015-08-03 10:21:18 -070043using Grpc.Testing;
Jan Tattermusch30868622015-02-19 09:22:33 -080044using NUnit.Framework;
Jan Tattermusch8644aea2015-08-03 10:21:18 -070045using Google.Protobuf;
Jan Tattermusch0c140a82015-08-02 00:54:02 -070046using Google.Apis.Auth.OAuth2;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080047
Jan Tattermusch8b86b152015-02-19 21:01:05 -080048namespace Grpc.IntegrationTesting
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080049{
Jan Tattermusch503bbac2015-02-26 18:19:47 -080050 public class InteropClient
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080051 {
Jan Tattermuschdca6e882015-04-22 16:56:27 -070052 private const string ServiceAccountUser = "155450119199-3psnrh1sdr3d8cpj1v46naggf81mhdnk@developer.gserviceaccount.com";
53 private const string ComputeEngineUser = "155450119199-r5aaqa2vqoa9g5mv2m6s3m1l293rlmel@developer.gserviceaccount.com";
54 private const string AuthScope = "https://www.googleapis.com/auth/xapi.zoo";
55 private const string AuthScopeResponse = "xapi.zoo";
56
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080057 private class ClientOptions
58 {
59 public bool help;
Jan Tattermusch075dde42015-03-11 18:21:00 -070060 public string serverHost = "127.0.0.1";
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080061 public string serverHostOverride = TestCredentials.DefaultHostOverride;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080062 public int? serverPort;
Jan Tattermusch15329232015-03-02 15:32:47 -080063 public string testCase = "large_unary";
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080064 public bool useTls;
65 public bool useTestCa;
66 }
67
68 ClientOptions options;
69
Jan Tattermusch503bbac2015-02-26 18:19:47 -080070 private InteropClient(ClientOptions options)
Jan Tattermusch392d1e02015-02-09 11:13:51 -080071 {
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080072 this.options = options;
73 }
74
Jan Tattermusch503bbac2015-02-26 18:19:47 -080075 public static void Run(string[] args)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080076 {
77 Console.WriteLine("gRPC C# interop testing client");
78 ClientOptions options = ParseArguments(args);
79
80 if (options.serverHost == null || !options.serverPort.HasValue || options.testCase == null)
81 {
82 Console.WriteLine("Missing required argument.");
83 Console.WriteLine();
84 options.help = true;
85 }
86
87 if (options.help)
88 {
89 Console.WriteLine("Usage:");
90 Console.WriteLine(" --server_host=HOSTNAME");
91 Console.WriteLine(" --server_host_override=HOSTNAME");
92 Console.WriteLine(" --server_port=PORT");
93 Console.WriteLine(" --test_case=TESTCASE");
94 Console.WriteLine(" --use_tls=BOOLEAN");
95 Console.WriteLine(" --use_test_ca=BOOLEAN");
Craig Tiller190d3602015-02-18 09:23:38 -080096 Console.WriteLine();
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080097 Environment.Exit(1);
98 }
99
Jan Tattermusch503bbac2015-02-26 18:19:47 -0800100 var interopClient = new InteropClient(options);
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700101 interopClient.Run().Wait();
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800102 }
103
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700104 private async Task Run()
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800105 {
Jan Tattermusch15329232015-03-02 15:32:47 -0800106 Credentials credentials = null;
107 if (options.useTls)
108 {
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800109 credentials = TestCredentials.CreateTestClientCredentials(options.useTestCa);
Jan Tattermusch15329232015-03-02 15:32:47 -0800110 }
111
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700112 List<ChannelOption> channelOptions = null;
Jan Tattermusch15329232015-03-02 15:32:47 -0800113 if (!string.IsNullOrEmpty(options.serverHostOverride))
114 {
Jan Tattermuschc8f7d102015-06-08 17:53:45 -0700115 channelOptions = new List<ChannelOption>
116 {
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700117 new ChannelOption(ChannelOptions.SslTargetNameOverride, options.serverHostOverride)
118 };
Jan Tattermusch15329232015-03-02 15:32:47 -0800119 }
120
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700121 using (Channel channel = new Channel(options.serverHost, options.serverPort.Value, credentials, channelOptions))
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800122 {
Jan Tattermuschb5332812015-07-14 19:29:35 -0700123 TestService.TestServiceClient client = new TestService.TestServiceClient(channel);
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700124 await RunTestCaseAsync(options.testCase, client);
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800125 }
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800126 GrpcEnvironment.Shutdown();
127 }
128
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700129 private async Task RunTestCaseAsync(string testCase, TestService.TestServiceClient client)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800130 {
131 switch (testCase)
132 {
133 case "empty_unary":
134 RunEmptyUnary(client);
135 break;
136 case "large_unary":
137 RunLargeUnary(client);
138 break;
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800139 case "client_streaming":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700140 await RunClientStreamingAsync(client);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800141 break;
142 case "server_streaming":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700143 await RunServerStreamingAsync(client);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800144 break;
145 case "ping_pong":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700146 await RunPingPongAsync(client);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800147 break;
148 case "empty_stream":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700149 await RunEmptyStreamAsync(client);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800150 break;
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700151 case "service_account_creds":
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700152 await RunServiceAccountCredsAsync(client);
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700153 break;
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700154 case "compute_engine_creds":
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700155 await RunComputeEngineCredsAsync(client);
156 break;
157 case "jwt_token_creds":
158 await RunJwtTokenCredsAsync(client);
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700159 break;
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700160 case "oauth2_auth_token":
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700161 await RunOAuth2AuthTokenAsync(client);
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700162 break;
163 case "per_rpc_creds":
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700164 await RunPerRpcCredsAsync(client);
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700165 break;
Jan Tattermusche5c44602015-05-01 11:12:34 -0700166 case "cancel_after_begin":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700167 await RunCancelAfterBeginAsync(client);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700168 break;
169 case "cancel_after_first_response":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700170 await RunCancelAfterFirstResponseAsync(client);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700171 break;
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800172 case "benchmark_empty_unary":
173 RunBenchmarkEmptyUnary(client);
174 break;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800175 default:
176 throw new ArgumentException("Unknown test case " + testCase);
177 }
178 }
179
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700180 public static void RunEmptyUnary(TestService.ITestServiceClient client)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800181 {
182 Console.WriteLine("running empty_unary");
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700183 var response = client.EmptyCall(new Empty());
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800184 Assert.IsNotNull(response);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800185 Console.WriteLine("Passed!");
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800186 }
187
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700188 public static void RunLargeUnary(TestService.ITestServiceClient client)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800189 {
190 Console.WriteLine("running large_unary");
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700191 var request = new SimpleRequest
192 {
193 ResponseType = PayloadType.COMPRESSABLE,
194 ResponseSize = 314159,
195 Payload = CreateZerosPayload(271828)
196 };
Craig Tiller190d3602015-02-18 09:23:38 -0800197
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800198 var response = client.UnaryCall(request);
199
200 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
201 Assert.AreEqual(314159, response.Payload.Body.Length);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800202 Console.WriteLine("Passed!");
203 }
204
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700205 public static async Task RunClientStreamingAsync(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800206 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700207 Console.WriteLine("running client_streaming");
208
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700209 var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => new StreamingInputCallRequest { Payload = CreateZerosPayload(size) });
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700210
211 using (var call = client.StreamingInputCall())
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800212 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700213 await call.RequestStream.WriteAllAsync(bodySizes);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800214
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700215 var response = await call.ResponseAsync;
216 Assert.AreEqual(74922, response.AggregatedPayloadSize);
217 }
218 Console.WriteLine("Passed!");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800219 }
220
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700221 public static async Task RunServerStreamingAsync(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800222 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700223 Console.WriteLine("running server_streaming");
224
225 var bodySizes = new List<int> { 31415, 9, 2653, 58979 };
226
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700227 var request = new StreamingOutputCallRequest
228 {
229 ResponseType = PayloadType.COMPRESSABLE,
230 ResponseParameters = { bodySizes.ConvertAll((size) => new ResponseParameters { Size = size }) }
231 };
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700232
233 using (var call = client.StreamingOutputCall(request))
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700234 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700235 var responseList = await call.ResponseStream.ToListAsync();
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700236 foreach (var res in responseList)
237 {
238 Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type);
239 }
240 CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length));
241 }
242 Console.WriteLine("Passed!");
243 }
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800244
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700245 public static async Task RunPingPongAsync(TestService.ITestServiceClient client)
246 {
247 Console.WriteLine("running ping_pong");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800248
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700249 using (var call = client.FullDuplexCall())
250 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700251 await call.RequestStream.WriteAsync(new StreamingOutputCallRequest
252 {
253 ResponseType = PayloadType.COMPRESSABLE,
254 ResponseParameters = { new ResponseParameters { Size = 31415 } },
255 Payload = CreateZerosPayload(27182)
256 });
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800257
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700258 Assert.IsTrue(await call.ResponseStream.MoveNext());
259 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
260 Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);
261
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700262 await call.RequestStream.WriteAsync(new StreamingOutputCallRequest
263 {
264 ResponseType = PayloadType.COMPRESSABLE,
265 ResponseParameters = { new ResponseParameters { Size = 9 } },
266 Payload = CreateZerosPayload(8)
267 });
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700268
269 Assert.IsTrue(await call.ResponseStream.MoveNext());
270 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
271 Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length);
272
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700273 await call.RequestStream.WriteAsync(new StreamingOutputCallRequest
274 {
275 ResponseType = PayloadType.COMPRESSABLE,
276 ResponseParameters = { new ResponseParameters { Size = 2653 } },
277 Payload = CreateZerosPayload(1828)
278 });
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700279
280 Assert.IsTrue(await call.ResponseStream.MoveNext());
281 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
282 Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length);
283
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700284 await call.RequestStream.WriteAsync(new StreamingOutputCallRequest
285 {
286 ResponseType = PayloadType.COMPRESSABLE,
287 ResponseParameters = { new ResponseParameters { Size = 58979 } },
288 Payload = CreateZerosPayload(45904)
289 });
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700290
291 Assert.IsTrue(await call.ResponseStream.MoveNext());
292 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
293 Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length);
294
295 await call.RequestStream.CompleteAsync();
296
297 Assert.IsFalse(await call.ResponseStream.MoveNext());
298 }
299 Console.WriteLine("Passed!");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800300 }
301
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700302 public static async Task RunEmptyStreamAsync(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800303 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700304 Console.WriteLine("running empty_stream");
305 using (var call = client.FullDuplexCall())
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700306 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700307 await call.RequestStream.CompleteAsync();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800308
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700309 var responseList = await call.ResponseStream.ToListAsync();
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700310 Assert.AreEqual(0, responseList.Count);
311 }
312 Console.WriteLine("Passed!");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800313 }
314
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700315 public static async Task RunServiceAccountCredsAsync(TestService.TestServiceClient client)
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700316 {
317 Console.WriteLine("running service_account_creds");
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700318 var credential = await GoogleCredential.GetApplicationDefaultAsync();
319 credential = credential.CreateScoped(new[] { AuthScope });
320 client.HeaderInterceptor = OAuth2Interceptors.FromCredential(credential);
321
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700322 var request = new SimpleRequest
323 {
324 ResponseType = PayloadType.COMPRESSABLE,
325 ResponseSize = 314159,
326 Payload = CreateZerosPayload(271828),
327 FillUsername = true,
328 FillOauthScope = true
329 };
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700330
331 var response = client.UnaryCall(request);
332
333 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
334 Assert.AreEqual(314159, response.Payload.Body.Length);
335 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
336 Assert.AreEqual(ServiceAccountUser, response.Username);
337 Console.WriteLine("Passed!");
338 }
339
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700340 public static async Task RunComputeEngineCredsAsync(TestService.TestServiceClient client)
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700341 {
342 Console.WriteLine("running compute_engine_creds");
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700343 var credential = await GoogleCredential.GetApplicationDefaultAsync();
344 Assert.IsFalse(credential.IsCreateScopedRequired);
345 client.HeaderInterceptor = OAuth2Interceptors.FromCredential(credential);
346
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700347 var request = new SimpleRequest
348 {
349 ResponseType = PayloadType.COMPRESSABLE,
350 ResponseSize = 314159,
351 Payload = CreateZerosPayload(271828),
352 FillUsername = true,
353 FillOauthScope = true
354 };
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700355
356 var response = client.UnaryCall(request);
357
358 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
359 Assert.AreEqual(314159, response.Payload.Body.Length);
360 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
Jan Tattermuschaa5fba42015-04-27 21:28:58 -0700361 Assert.AreEqual(ComputeEngineUser, response.Username);
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700362 Console.WriteLine("Passed!");
363 }
364
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700365 public static async Task RunJwtTokenCredsAsync(TestService.TestServiceClient client)
366 {
367 Console.WriteLine("running jwt_token_creds");
368 var credential = await GoogleCredential.GetApplicationDefaultAsync();
369 // check this a credential with scope support, but don't add the scope.
370 Assert.IsTrue(credential.IsCreateScopedRequired);
371 client.HeaderInterceptor = OAuth2Interceptors.FromCredential(credential);
372
Jan Tattermusch46e85b02015-08-13 10:31:05 -0700373 var request = new SimpleRequest
374 {
375 ResponseType = PayloadType.COMPRESSABLE,
376 ResponseSize = 314159,
377 Payload = CreateZerosPayload(271828),
378 FillUsername = true,
379 FillOauthScope = true
380 };
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700381
382 var response = client.UnaryCall(request);
383
384 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
385 Assert.AreEqual(314159, response.Payload.Body.Length);
386 Assert.AreEqual(ServiceAccountUser, response.Username);
387 Console.WriteLine("Passed!");
388 }
389
390 public static async Task RunOAuth2AuthTokenAsync(TestService.TestServiceClient client)
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700391 {
392 Console.WriteLine("running oauth2_auth_token");
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700393 ITokenAccess credential = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { AuthScope });
394 string oauth2Token = await credential.GetAccessTokenForRequestAsync();
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700395
Jan Tattermuschcd7a4052015-07-25 01:47:25 -0700396 client.HeaderInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token);
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700397
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700398 var request = new SimpleRequest
399 {
400 FillUsername = true,
401 FillOauthScope = true
402 };
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700403
404 var response = client.UnaryCall(request);
405
406 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
407 Assert.AreEqual(ServiceAccountUser, response.Username);
408 Console.WriteLine("Passed!");
409 }
410
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700411 public static async Task RunPerRpcCredsAsync(TestService.TestServiceClient client)
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700412 {
413 Console.WriteLine("running per_rpc_creds");
414
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700415 ITokenAccess credential = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { AuthScope });
416 string oauth2Token = await credential.GetAccessTokenForRequestAsync();
Jan Tattermuschcd7a4052015-07-25 01:47:25 -0700417 var headerInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token);
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700418
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700419 var request = new SimpleRequest
420 {
421 FillUsername = true,
422 FillOauthScope = true
423 };
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700424
Jan Tattermuschcd7a4052015-07-25 01:47:25 -0700425 var headers = new Metadata();
Jan Tattermusch0c140a82015-08-02 00:54:02 -0700426 headerInterceptor("", headers);
Jan Tattermuschcd7a4052015-07-25 01:47:25 -0700427 var response = client.UnaryCall(request, headers: headers);
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700428
429 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
430 Assert.AreEqual(ServiceAccountUser, response.Username);
431 Console.WriteLine("Passed!");
432 }
433
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700434 public static async Task RunCancelAfterBeginAsync(TestService.ITestServiceClient client)
Jan Tattermusche5c44602015-05-01 11:12:34 -0700435 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700436 Console.WriteLine("running cancel_after_begin");
437
438 var cts = new CancellationTokenSource();
439 using (var call = client.StreamingInputCall(cancellationToken: cts.Token))
Jan Tattermusche5c44602015-05-01 11:12:34 -0700440 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700441 // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it.
442 await Task.Delay(1000);
443 cts.Cancel();
Jan Tattermusche5c44602015-05-01 11:12:34 -0700444
Jan Tattermuschc8d7b842015-08-07 20:52:21 -0700445 var ex = Assert.Throws<RpcException>(async () => await call.ResponseAsync);
446 Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode);
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700447 }
448 Console.WriteLine("Passed!");
Jan Tattermusche5c44602015-05-01 11:12:34 -0700449 }
450
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700451 public static async Task RunCancelAfterFirstResponseAsync(TestService.ITestServiceClient client)
Jan Tattermusche5c44602015-05-01 11:12:34 -0700452 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700453 Console.WriteLine("running cancel_after_first_response");
454
455 var cts = new CancellationTokenSource();
456 using (var call = client.FullDuplexCall(cancellationToken: cts.Token))
Jan Tattermusche5c44602015-05-01 11:12:34 -0700457 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700458 await call.RequestStream.WriteAsync(new StreamingOutputCallRequest
459 {
460 ResponseType = PayloadType.COMPRESSABLE,
461 ResponseParameters = { new ResponseParameters { Size = 31415 } },
462 Payload = CreateZerosPayload(27182)
463 });
Jan Tattermusche5c44602015-05-01 11:12:34 -0700464
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700465 Assert.IsTrue(await call.ResponseStream.MoveNext());
466 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
467 Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);
468
469 cts.Cancel();
470
Jan Tattermuschc8d7b842015-08-07 20:52:21 -0700471 var ex = Assert.Throws<RpcException>(async () => await call.ResponseStream.MoveNext());
472 Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode);
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700473 }
474 Console.WriteLine("Passed!");
Jan Tattermusche5c44602015-05-01 11:12:34 -0700475 }
476
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800477 // This is not an official interop test, but it's useful.
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700478 public static void RunBenchmarkEmptyUnary(TestService.ITestServiceClient client)
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800479 {
480 BenchmarkUtil.RunBenchmark(10000, 10000,
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700481 () => { client.EmptyCall(new Empty()); });
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800482 }
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800483
Jan Tattermusch075dde42015-03-11 18:21:00 -0700484 private static Payload CreateZerosPayload(int size)
485 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700486 return new Payload { Body = ByteString.CopyFrom(new byte[size]) };
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800487 }
488
489 private static ClientOptions ParseArguments(string[] args)
490 {
491 var options = new ClientOptions();
Jan Tattermusch075dde42015-03-11 18:21:00 -0700492 foreach (string arg in args)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800493 {
494 ParseArgument(arg, options);
495 if (options.help)
496 {
497 break;
498 }
499 }
500 return options;
501 }
502
503 private static void ParseArgument(string arg, ClientOptions options)
504 {
505 Match match;
506 match = Regex.Match(arg, "--server_host=(.*)");
507 if (match.Success)
508 {
509 options.serverHost = match.Groups[1].Value.Trim();
510 return;
511 }
512
513 match = Regex.Match(arg, "--server_host_override=(.*)");
514 if (match.Success)
515 {
516 options.serverHostOverride = match.Groups[1].Value.Trim();
517 return;
518 }
519
520 match = Regex.Match(arg, "--server_port=(.*)");
521 if (match.Success)
522 {
523 options.serverPort = int.Parse(match.Groups[1].Value.Trim());
524 return;
525 }
526
527 match = Regex.Match(arg, "--test_case=(.*)");
528 if (match.Success)
529 {
530 options.testCase = match.Groups[1].Value.Trim();
531 return;
532 }
533
534 match = Regex.Match(arg, "--use_tls=(.*)");
535 if (match.Success)
536 {
537 options.useTls = bool.Parse(match.Groups[1].Value.Trim());
538 return;
539 }
540
541 match = Regex.Match(arg, "--use_test_ca=(.*)");
542 if (match.Success)
543 {
544 options.useTestCa = bool.Parse(match.Groups[1].Value.Trim());
545 return;
546 }
547
548 Console.WriteLine(string.Format("Unrecognized argument \"{0}\"", arg));
549 options.help = true;
550 }
551 }
552}