blob: c918f60127a9f216bf675886d47eebfadeaa2e93 [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 Tattermuschcd7a4052015-07-25 01:47:25 -0700130 client.HeaderInterceptor = OAuth2Interceptors.FromCredential(credential);
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700131 }
132
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700133 RunTestCaseAsync(options.testCase, client).Wait();
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800134 }
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800135 GrpcEnvironment.Shutdown();
136 }
137
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700138 private async Task RunTestCaseAsync(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":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700149 await RunClientStreamingAsync(client);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800150 break;
151 case "server_streaming":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700152 await RunServerStreamingAsync(client);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800153 break;
154 case "ping_pong":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700155 await RunPingPongAsync(client);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800156 break;
157 case "empty_stream":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700158 await RunEmptyStreamAsync(client);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800159 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":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700173 await RunCancelAfterBeginAsync(client);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700174 break;
175 case "cancel_after_first_response":
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700176 await RunCancelAfterFirstResponseAsync(client);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700177 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 Tattermuschb98e1dd2015-07-24 21:30:14 -0700210 public static async Task RunClientStreamingAsync(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800211 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700212 Console.WriteLine("running client_streaming");
213
214 var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => StreamingInputCallRequest.CreateBuilder().SetPayload(CreateZerosPayload(size)).Build());
215
216 using (var call = client.StreamingInputCall())
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800217 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700218 await call.RequestStream.WriteAllAsync(bodySizes);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800219
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700220 var response = await call.ResponseAsync;
221 Assert.AreEqual(74922, response.AggregatedPayloadSize);
222 }
223 Console.WriteLine("Passed!");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800224 }
225
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700226 public static async Task RunServerStreamingAsync(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800227 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700228 Console.WriteLine("running server_streaming");
229
230 var bodySizes = new List<int> { 31415, 9, 2653, 58979 };
231
232 var request = StreamingOutputCallRequest.CreateBuilder()
233 .SetResponseType(PayloadType.COMPRESSABLE)
234 .AddRangeResponseParameters(bodySizes.ConvertAll(
235 (size) => ResponseParameters.CreateBuilder().SetSize(size).Build()))
236 .Build();
237
238 using (var call = client.StreamingOutputCall(request))
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700239 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700240 var responseList = await call.ResponseStream.ToListAsync();
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700241 foreach (var res in responseList)
242 {
243 Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type);
244 }
245 CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length));
246 }
247 Console.WriteLine("Passed!");
248 }
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800249
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700250 public static async Task RunPingPongAsync(TestService.ITestServiceClient client)
251 {
252 Console.WriteLine("running ping_pong");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800253
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700254 using (var call = client.FullDuplexCall())
255 {
256 await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800257 .SetResponseType(PayloadType.COMPRESSABLE)
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700258 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
259 .SetPayload(CreateZerosPayload(27182)).Build());
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800260
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700261 Assert.IsTrue(await call.ResponseStream.MoveNext());
262 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
263 Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);
264
265 await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
266 .SetResponseType(PayloadType.COMPRESSABLE)
267 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(9))
268 .SetPayload(CreateZerosPayload(8)).Build());
269
270 Assert.IsTrue(await call.ResponseStream.MoveNext());
271 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
272 Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length);
273
274 await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
275 .SetResponseType(PayloadType.COMPRESSABLE)
276 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(2653))
277 .SetPayload(CreateZerosPayload(1828)).Build());
278
279 Assert.IsTrue(await call.ResponseStream.MoveNext());
280 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
281 Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length);
282
283 await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
284 .SetResponseType(PayloadType.COMPRESSABLE)
285 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(58979))
286 .SetPayload(CreateZerosPayload(45904)).Build());
287
288 Assert.IsTrue(await call.ResponseStream.MoveNext());
289 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
290 Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length);
291
292 await call.RequestStream.CompleteAsync();
293
294 Assert.IsFalse(await call.ResponseStream.MoveNext());
295 }
296 Console.WriteLine("Passed!");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800297 }
298
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700299 public static async Task RunEmptyStreamAsync(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800300 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700301 Console.WriteLine("running empty_stream");
302 using (var call = client.FullDuplexCall())
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700303 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700304 await call.RequestStream.CompleteAsync();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800305
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700306 var responseList = await call.ResponseStream.ToListAsync();
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700307 Assert.AreEqual(0, responseList.Count);
308 }
309 Console.WriteLine("Passed!");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800310 }
311
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700312 public static void RunServiceAccountCreds(TestService.ITestServiceClient client)
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700313 {
314 Console.WriteLine("running service_account_creds");
315 var request = SimpleRequest.CreateBuilder()
316 .SetResponseType(PayloadType.COMPRESSABLE)
317 .SetResponseSize(314159)
318 .SetPayload(CreateZerosPayload(271828))
319 .SetFillUsername(true)
320 .SetFillOauthScope(true)
321 .Build();
322
323 var response = client.UnaryCall(request);
324
325 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
326 Assert.AreEqual(314159, response.Payload.Body.Length);
327 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
328 Assert.AreEqual(ServiceAccountUser, response.Username);
329 Console.WriteLine("Passed!");
330 }
331
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700332 public static void RunComputeEngineCreds(TestService.ITestServiceClient client)
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700333 {
334 Console.WriteLine("running compute_engine_creds");
335 var request = SimpleRequest.CreateBuilder()
336 .SetResponseType(PayloadType.COMPRESSABLE)
337 .SetResponseSize(314159)
338 .SetPayload(CreateZerosPayload(271828))
339 .SetFillUsername(true)
340 .SetFillOauthScope(true)
341 .Build();
342
343 var response = client.UnaryCall(request);
344
345 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
346 Assert.AreEqual(314159, response.Payload.Body.Length);
347 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
Jan Tattermuschaa5fba42015-04-27 21:28:58 -0700348 Assert.AreEqual(ComputeEngineUser, response.Username);
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700349 Console.WriteLine("Passed!");
350 }
351
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700352 public static void RunOAuth2AuthToken(TestService.TestServiceClient client)
353 {
354 Console.WriteLine("running oauth2_auth_token");
355 var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope });
356 Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result);
357 string oauth2Token = credential.Token.AccessToken;
358
Jan Tattermuschcd7a4052015-07-25 01:47:25 -0700359 client.HeaderInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token);
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700360
361 var request = SimpleRequest.CreateBuilder()
362 .SetFillUsername(true)
363 .SetFillOauthScope(true)
364 .Build();
365
366 var response = client.UnaryCall(request);
367
368 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
369 Assert.AreEqual(ServiceAccountUser, response.Username);
370 Console.WriteLine("Passed!");
371 }
372
373 public static void RunPerRpcCreds(TestService.TestServiceClient client)
374 {
375 Console.WriteLine("running per_rpc_creds");
376
377 var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope });
378 Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result);
379 string oauth2Token = credential.Token.AccessToken;
Jan Tattermuschcd7a4052015-07-25 01:47:25 -0700380 var headerInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token);
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700381
382 var request = SimpleRequest.CreateBuilder()
383 .SetFillUsername(true)
384 .SetFillOauthScope(true)
385 .Build();
386
Jan Tattermuschcd7a4052015-07-25 01:47:25 -0700387 var headers = new Metadata();
388 headerInterceptor(headers);
389 var response = client.UnaryCall(request, headers: headers);
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700390
391 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
392 Assert.AreEqual(ServiceAccountUser, response.Username);
393 Console.WriteLine("Passed!");
394 }
395
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700396 public static async Task RunCancelAfterBeginAsync(TestService.ITestServiceClient client)
Jan Tattermusche5c44602015-05-01 11:12:34 -0700397 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700398 Console.WriteLine("running cancel_after_begin");
399
400 var cts = new CancellationTokenSource();
401 using (var call = client.StreamingInputCall(cancellationToken: cts.Token))
Jan Tattermusche5c44602015-05-01 11:12:34 -0700402 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700403 // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it.
404 await Task.Delay(1000);
405 cts.Cancel();
Jan Tattermusche5c44602015-05-01 11:12:34 -0700406
Jan Tattermuschc8d7b842015-08-07 20:52:21 -0700407 var ex = Assert.Throws<RpcException>(async () => await call.ResponseAsync);
408 Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode);
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700409 }
410 Console.WriteLine("Passed!");
Jan Tattermusche5c44602015-05-01 11:12:34 -0700411 }
412
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700413 public static async Task RunCancelAfterFirstResponseAsync(TestService.ITestServiceClient client)
Jan Tattermusche5c44602015-05-01 11:12:34 -0700414 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700415 Console.WriteLine("running cancel_after_first_response");
416
417 var cts = new CancellationTokenSource();
418 using (var call = client.FullDuplexCall(cancellationToken: cts.Token))
Jan Tattermusche5c44602015-05-01 11:12:34 -0700419 {
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700420 await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
421 .SetResponseType(PayloadType.COMPRESSABLE)
422 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
423 .SetPayload(CreateZerosPayload(27182)).Build());
Jan Tattermusche5c44602015-05-01 11:12:34 -0700424
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700425 Assert.IsTrue(await call.ResponseStream.MoveNext());
426 Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
427 Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);
428
429 cts.Cancel();
430
Jan Tattermuschc8d7b842015-08-07 20:52:21 -0700431 var ex = Assert.Throws<RpcException>(async () => await call.ResponseStream.MoveNext());
432 Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode);
Jan Tattermuschb98e1dd2015-07-24 21:30:14 -0700433 }
434 Console.WriteLine("Passed!");
Jan Tattermusche5c44602015-05-01 11:12:34 -0700435 }
436
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800437 // This is not an official interop test, but it's useful.
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700438 public static void RunBenchmarkEmptyUnary(TestService.ITestServiceClient client)
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800439 {
440 BenchmarkUtil.RunBenchmark(10000, 10000,
Jan Tattermusch075dde42015-03-11 18:21:00 -0700441 () => { client.EmptyCall(Empty.DefaultInstance); });
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800442 }
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800443
Jan Tattermusch075dde42015-03-11 18:21:00 -0700444 private static Payload CreateZerosPayload(int size)
445 {
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800446 return Payload.CreateBuilder().SetBody(ByteString.CopyFrom(new byte[size])).Build();
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800447 }
448
449 private static ClientOptions ParseArguments(string[] args)
450 {
451 var options = new ClientOptions();
Jan Tattermusch075dde42015-03-11 18:21:00 -0700452 foreach (string arg in args)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800453 {
454 ParseArgument(arg, options);
455 if (options.help)
456 {
457 break;
458 }
459 }
460 return options;
461 }
462
463 private static void ParseArgument(string arg, ClientOptions options)
464 {
465 Match match;
466 match = Regex.Match(arg, "--server_host=(.*)");
467 if (match.Success)
468 {
469 options.serverHost = match.Groups[1].Value.Trim();
470 return;
471 }
472
473 match = Regex.Match(arg, "--server_host_override=(.*)");
474 if (match.Success)
475 {
476 options.serverHostOverride = match.Groups[1].Value.Trim();
477 return;
478 }
479
480 match = Regex.Match(arg, "--server_port=(.*)");
481 if (match.Success)
482 {
483 options.serverPort = int.Parse(match.Groups[1].Value.Trim());
484 return;
485 }
486
487 match = Regex.Match(arg, "--test_case=(.*)");
488 if (match.Success)
489 {
490 options.testCase = match.Groups[1].Value.Trim();
491 return;
492 }
493
494 match = Regex.Match(arg, "--use_tls=(.*)");
495 if (match.Success)
496 {
497 options.useTls = bool.Parse(match.Groups[1].Value.Trim());
498 return;
499 }
500
501 match = Regex.Match(arg, "--use_test_ca=(.*)");
502 if (match.Success)
503 {
504 options.useTestCa = bool.Parse(match.Groups[1].Value.Trim());
505 return;
506 }
507
508 Console.WriteLine(string.Format("Unrecognized argument \"{0}\"", arg));
509 options.help = true;
510 }
511 }
512}