Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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 | #endregion |
| 33 | |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 35 | using System.Collections.Generic; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 36 | using System.Text.RegularExpressions; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 37 | using System.Threading; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 38 | using System.Threading.Tasks; |
Jan Tattermusch | 1ca56b9 | 2015-04-27 11:03:06 -0700 | [diff] [blame] | 39 | |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 40 | using Grpc.Auth; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 41 | using Grpc.Core; |
| 42 | using Grpc.Core.Utils; |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 43 | using Grpc.Testing; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 44 | using NUnit.Framework; |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 45 | using Google.Protobuf; |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 46 | using Google.Apis.Auth.OAuth2; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 47 | |
Jan Tattermusch | 8b86b15 | 2015-02-19 21:01:05 -0800 | [diff] [blame] | 48 | namespace Grpc.IntegrationTesting |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 49 | { |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 50 | public class InteropClient |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 51 | { |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 52 | 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 Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 57 | private class ClientOptions |
| 58 | { |
| 59 | public bool help; |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 60 | public string serverHost = "127.0.0.1"; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 61 | public string serverHostOverride = TestCredentials.DefaultHostOverride; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 62 | public int? serverPort; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 63 | public string testCase = "large_unary"; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 64 | public bool useTls; |
| 65 | public bool useTestCa; |
| 66 | } |
| 67 | |
| 68 | ClientOptions options; |
| 69 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 70 | private InteropClient(ClientOptions options) |
Jan Tattermusch | 392d1e0 | 2015-02-09 11:13:51 -0800 | [diff] [blame] | 71 | { |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 72 | this.options = options; |
| 73 | } |
| 74 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 75 | public static void Run(string[] args) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 76 | { |
| 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 Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 96 | Console.WriteLine(); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 97 | Environment.Exit(1); |
| 98 | } |
| 99 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 100 | var interopClient = new InteropClient(options); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 101 | interopClient.Run().Wait(); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 104 | private async Task Run() |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 105 | { |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 106 | Credentials credentials = null; |
| 107 | if (options.useTls) |
| 108 | { |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 109 | credentials = TestCredentials.CreateTestClientCredentials(options.useTestCa); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 112 | List<ChannelOption> channelOptions = null; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 113 | if (!string.IsNullOrEmpty(options.serverHostOverride)) |
| 114 | { |
Jan Tattermusch | c8f7d10 | 2015-06-08 17:53:45 -0700 | [diff] [blame] | 115 | channelOptions = new List<ChannelOption> |
| 116 | { |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 117 | new ChannelOption(ChannelOptions.SslTargetNameOverride, options.serverHostOverride) |
| 118 | }; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 121 | using (Channel channel = new Channel(options.serverHost, options.serverPort.Value, credentials, channelOptions)) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 122 | { |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 123 | TestService.TestServiceClient client = new TestService.TestServiceClient(channel); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 124 | await RunTestCaseAsync(options.testCase, client); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 125 | } |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 126 | GrpcEnvironment.Shutdown(); |
| 127 | } |
| 128 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 129 | private async Task RunTestCaseAsync(string testCase, TestService.TestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 130 | { |
| 131 | switch (testCase) |
| 132 | { |
| 133 | case "empty_unary": |
| 134 | RunEmptyUnary(client); |
| 135 | break; |
| 136 | case "large_unary": |
| 137 | RunLargeUnary(client); |
| 138 | break; |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 139 | case "client_streaming": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 140 | await RunClientStreamingAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 141 | break; |
| 142 | case "server_streaming": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 143 | await RunServerStreamingAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 144 | break; |
| 145 | case "ping_pong": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 146 | await RunPingPongAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 147 | break; |
| 148 | case "empty_stream": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 149 | await RunEmptyStreamAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 150 | break; |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 151 | case "service_account_creds": |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 152 | await RunServiceAccountCredsAsync(client); |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 153 | break; |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 154 | case "compute_engine_creds": |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 155 | await RunComputeEngineCredsAsync(client); |
| 156 | break; |
| 157 | case "jwt_token_creds": |
| 158 | await RunJwtTokenCredsAsync(client); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 159 | break; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 160 | case "oauth2_auth_token": |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 161 | await RunOAuth2AuthTokenAsync(client); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 162 | break; |
| 163 | case "per_rpc_creds": |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 164 | await RunPerRpcCredsAsync(client); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 165 | break; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 166 | case "cancel_after_begin": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 167 | await RunCancelAfterBeginAsync(client); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 168 | break; |
| 169 | case "cancel_after_first_response": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 170 | await RunCancelAfterFirstResponseAsync(client); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 171 | break; |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 172 | case "benchmark_empty_unary": |
| 173 | RunBenchmarkEmptyUnary(client); |
| 174 | break; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 175 | default: |
| 176 | throw new ArgumentException("Unknown test case " + testCase); |
| 177 | } |
| 178 | } |
| 179 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 180 | public static void RunEmptyUnary(TestService.ITestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 181 | { |
| 182 | Console.WriteLine("running empty_unary"); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 183 | var response = client.EmptyCall(new Empty()); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 184 | Assert.IsNotNull(response); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 185 | Console.WriteLine("Passed!"); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 188 | public static void RunLargeUnary(TestService.ITestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 189 | { |
| 190 | Console.WriteLine("running large_unary"); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 191 | var request = new SimpleRequest |
| 192 | { |
| 193 | ResponseType = PayloadType.COMPRESSABLE, |
| 194 | ResponseSize = 314159, |
| 195 | Payload = CreateZerosPayload(271828) |
| 196 | }; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 197 | |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 198 | var response = client.UnaryCall(request); |
| 199 | |
| 200 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 201 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 202 | Console.WriteLine("Passed!"); |
| 203 | } |
| 204 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 205 | public static async Task RunClientStreamingAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 206 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 207 | Console.WriteLine("running client_streaming"); |
| 208 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 209 | var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => new StreamingInputCallRequest { Payload = CreateZerosPayload(size) }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 210 | |
| 211 | using (var call = client.StreamingInputCall()) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 212 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 213 | await call.RequestStream.WriteAllAsync(bodySizes); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 214 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 215 | var response = await call.ResponseAsync; |
| 216 | Assert.AreEqual(74922, response.AggregatedPayloadSize); |
| 217 | } |
| 218 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 221 | public static async Task RunServerStreamingAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 222 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 223 | Console.WriteLine("running server_streaming"); |
| 224 | |
| 225 | var bodySizes = new List<int> { 31415, 9, 2653, 58979 }; |
| 226 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 227 | var request = new StreamingOutputCallRequest |
| 228 | { |
| 229 | ResponseType = PayloadType.COMPRESSABLE, |
| 230 | ResponseParameters = { bodySizes.ConvertAll((size) => new ResponseParameters { Size = size }) } |
| 231 | }; |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 232 | |
| 233 | using (var call = client.StreamingOutputCall(request)) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 234 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 235 | var responseList = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 236 | 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 Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 244 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 245 | public static async Task RunPingPongAsync(TestService.ITestServiceClient client) |
| 246 | { |
| 247 | Console.WriteLine("running ping_pong"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 248 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 249 | using (var call = client.FullDuplexCall()) |
| 250 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 251 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 252 | { |
| 253 | ResponseType = PayloadType.COMPRESSABLE, |
| 254 | ResponseParameters = { new ResponseParameters { Size = 31415 } }, |
| 255 | Payload = CreateZerosPayload(27182) |
| 256 | }); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 257 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 258 | 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 Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 262 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 263 | { |
| 264 | ResponseType = PayloadType.COMPRESSABLE, |
| 265 | ResponseParameters = { new ResponseParameters { Size = 9 } }, |
| 266 | Payload = CreateZerosPayload(8) |
| 267 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 268 | |
| 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 Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 273 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 274 | { |
| 275 | ResponseType = PayloadType.COMPRESSABLE, |
| 276 | ResponseParameters = { new ResponseParameters { Size = 2653 } }, |
| 277 | Payload = CreateZerosPayload(1828) |
| 278 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 279 | |
| 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 Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 284 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 285 | { |
| 286 | ResponseType = PayloadType.COMPRESSABLE, |
| 287 | ResponseParameters = { new ResponseParameters { Size = 58979 } }, |
| 288 | Payload = CreateZerosPayload(45904) |
| 289 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 290 | |
| 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 Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 302 | public static async Task RunEmptyStreamAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 303 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 304 | Console.WriteLine("running empty_stream"); |
| 305 | using (var call = client.FullDuplexCall()) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 306 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 307 | await call.RequestStream.CompleteAsync(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 308 | |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 309 | var responseList = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 310 | Assert.AreEqual(0, responseList.Count); |
| 311 | } |
| 312 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 315 | public static async Task RunServiceAccountCredsAsync(TestService.TestServiceClient client) |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 316 | { |
| 317 | Console.WriteLine("running service_account_creds"); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 318 | var credential = await GoogleCredential.GetApplicationDefaultAsync(); |
| 319 | credential = credential.CreateScoped(new[] { AuthScope }); |
| 320 | client.HeaderInterceptor = OAuth2Interceptors.FromCredential(credential); |
| 321 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 322 | var request = new SimpleRequest |
| 323 | { |
| 324 | ResponseType = PayloadType.COMPRESSABLE, |
| 325 | ResponseSize = 314159, |
| 326 | Payload = CreateZerosPayload(271828), |
| 327 | FillUsername = true, |
| 328 | FillOauthScope = true |
| 329 | }; |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 330 | |
| 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 Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 340 | public static async Task RunComputeEngineCredsAsync(TestService.TestServiceClient client) |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 341 | { |
| 342 | Console.WriteLine("running compute_engine_creds"); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 343 | var credential = await GoogleCredential.GetApplicationDefaultAsync(); |
| 344 | Assert.IsFalse(credential.IsCreateScopedRequired); |
| 345 | client.HeaderInterceptor = OAuth2Interceptors.FromCredential(credential); |
| 346 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 347 | var request = new SimpleRequest |
| 348 | { |
| 349 | ResponseType = PayloadType.COMPRESSABLE, |
| 350 | ResponseSize = 314159, |
| 351 | Payload = CreateZerosPayload(271828), |
| 352 | FillUsername = true, |
| 353 | FillOauthScope = true |
| 354 | }; |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 355 | |
| 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 Tattermusch | aa5fba4 | 2015-04-27 21:28:58 -0700 | [diff] [blame] | 361 | Assert.AreEqual(ComputeEngineUser, response.Username); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 362 | Console.WriteLine("Passed!"); |
| 363 | } |
| 364 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 365 | 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 Tattermusch | 46e85b0 | 2015-08-13 10:31:05 -0700 | [diff] [blame^] | 373 | var request = new SimpleRequest |
| 374 | { |
| 375 | ResponseType = PayloadType.COMPRESSABLE, |
| 376 | ResponseSize = 314159, |
| 377 | Payload = CreateZerosPayload(271828), |
| 378 | FillUsername = true, |
| 379 | FillOauthScope = true |
| 380 | }; |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 381 | |
| 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 Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 391 | { |
| 392 | Console.WriteLine("running oauth2_auth_token"); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 393 | ITokenAccess credential = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { AuthScope }); |
| 394 | string oauth2Token = await credential.GetAccessTokenForRequestAsync(); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 395 | |
Jan Tattermusch | cd7a405 | 2015-07-25 01:47:25 -0700 | [diff] [blame] | 396 | client.HeaderInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 397 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 398 | var request = new SimpleRequest |
| 399 | { |
| 400 | FillUsername = true, |
| 401 | FillOauthScope = true |
| 402 | }; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 403 | |
| 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 Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 411 | public static async Task RunPerRpcCredsAsync(TestService.TestServiceClient client) |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 412 | { |
| 413 | Console.WriteLine("running per_rpc_creds"); |
| 414 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 415 | ITokenAccess credential = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { AuthScope }); |
| 416 | string oauth2Token = await credential.GetAccessTokenForRequestAsync(); |
Jan Tattermusch | cd7a405 | 2015-07-25 01:47:25 -0700 | [diff] [blame] | 417 | var headerInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 418 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 419 | var request = new SimpleRequest |
| 420 | { |
| 421 | FillUsername = true, |
| 422 | FillOauthScope = true |
| 423 | }; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 424 | |
Jan Tattermusch | cd7a405 | 2015-07-25 01:47:25 -0700 | [diff] [blame] | 425 | var headers = new Metadata(); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 426 | headerInterceptor("", headers); |
Jan Tattermusch | cd7a405 | 2015-07-25 01:47:25 -0700 | [diff] [blame] | 427 | var response = client.UnaryCall(request, headers: headers); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 428 | |
| 429 | Assert.AreEqual(AuthScopeResponse, response.OauthScope); |
| 430 | Assert.AreEqual(ServiceAccountUser, response.Username); |
| 431 | Console.WriteLine("Passed!"); |
| 432 | } |
| 433 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 434 | public static async Task RunCancelAfterBeginAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 435 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 436 | Console.WriteLine("running cancel_after_begin"); |
| 437 | |
| 438 | var cts = new CancellationTokenSource(); |
| 439 | using (var call = client.StreamingInputCall(cancellationToken: cts.Token)) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 440 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 441 | // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it. |
| 442 | await Task.Delay(1000); |
| 443 | cts.Cancel(); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 444 | |
Jan Tattermusch | c8d7b84 | 2015-08-07 20:52:21 -0700 | [diff] [blame] | 445 | var ex = Assert.Throws<RpcException>(async () => await call.ResponseAsync); |
| 446 | Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 447 | } |
| 448 | Console.WriteLine("Passed!"); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 451 | public static async Task RunCancelAfterFirstResponseAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 452 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 453 | Console.WriteLine("running cancel_after_first_response"); |
| 454 | |
| 455 | var cts = new CancellationTokenSource(); |
| 456 | using (var call = client.FullDuplexCall(cancellationToken: cts.Token)) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 457 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 458 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 459 | { |
| 460 | ResponseType = PayloadType.COMPRESSABLE, |
| 461 | ResponseParameters = { new ResponseParameters { Size = 31415 } }, |
| 462 | Payload = CreateZerosPayload(27182) |
| 463 | }); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 464 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 465 | 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 Tattermusch | c8d7b84 | 2015-08-07 20:52:21 -0700 | [diff] [blame] | 471 | var ex = Assert.Throws<RpcException>(async () => await call.ResponseStream.MoveNext()); |
| 472 | Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 473 | } |
| 474 | Console.WriteLine("Passed!"); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 477 | // This is not an official interop test, but it's useful. |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 478 | public static void RunBenchmarkEmptyUnary(TestService.ITestServiceClient client) |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 479 | { |
| 480 | BenchmarkUtil.RunBenchmark(10000, 10000, |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 481 | () => { client.EmptyCall(new Empty()); }); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 482 | } |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 483 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 484 | private static Payload CreateZerosPayload(int size) |
| 485 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 486 | return new Payload { Body = ByteString.CopyFrom(new byte[size]) }; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | private static ClientOptions ParseArguments(string[] args) |
| 490 | { |
| 491 | var options = new ClientOptions(); |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 492 | foreach (string arg in args) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 493 | { |
| 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 | } |