Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
| 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
Jens Geyer | 273607d | 2021-07-31 23:25:51 +0200 | [diff] [blame] | 18 | #pragma warning disable IDE0066 // switch expression |
| 19 | #pragma warning disable IDE0057 // substring |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 20 | #pragma warning disable CS1998 // no await in async method |
Jens Geyer | 273607d | 2021-07-31 23:25:51 +0200 | [diff] [blame] | 21 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 22 | using System; |
| 23 | using System.Collections.Generic; |
| 24 | using System.Diagnostics; |
| 25 | using System.IO; |
| 26 | using System.Linq; |
| 27 | using System.Net; |
| 28 | using System.Reflection; |
| 29 | using System.Security.Authentication; |
| 30 | using System.Security.Cryptography.X509Certificates; |
| 31 | using System.ServiceModel; |
| 32 | using System.Text; |
| 33 | using System.Threading; |
| 34 | using System.Threading.Tasks; |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 35 | using Thrift; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 36 | using Thrift.Collections; |
| 37 | using Thrift.Protocol; |
| 38 | using Thrift.Transport; |
| 39 | using Thrift.Transport.Client; |
| 40 | |
| 41 | namespace ThriftTest |
| 42 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 43 | internal enum ProtocolChoice |
| 44 | { |
| 45 | Binary, |
| 46 | Compact, |
| 47 | Json |
| 48 | } |
| 49 | |
| 50 | // it does not make much sense to use buffered when we already use framed |
| 51 | internal enum LayeredChoice |
| 52 | { |
| 53 | None, |
| 54 | Buffered, |
| 55 | Framed |
| 56 | } |
| 57 | |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 58 | internal enum TransportChoice |
| 59 | { |
| 60 | Socket, |
| 61 | TlsSocket, |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 62 | Http, |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 63 | NamedPipe |
| 64 | } |
| 65 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 66 | public class TestClient |
| 67 | { |
| 68 | private class TestParams |
| 69 | { |
| 70 | public int numIterations = 1; |
Jens Geyer | 22c412e | 2019-03-12 01:06:25 +0100 | [diff] [blame] | 71 | public string host = "localhost"; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 72 | public int port = 9090; |
| 73 | public int numThreads = 1; |
| 74 | public string url; |
| 75 | public string pipe; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 76 | public LayeredChoice layered = LayeredChoice.None; |
| 77 | public ProtocolChoice protocol = ProtocolChoice.Binary; |
| 78 | public TransportChoice transport = TransportChoice.Socket; |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 79 | private readonly TConfiguration Configuration = null; // or new TConfiguration() if needed |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 80 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 81 | internal void Parse(List<string> args) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 82 | { |
| 83 | for (var i = 0; i < args.Count; ++i) |
| 84 | { |
| 85 | if (args[i] == "-u") |
| 86 | { |
| 87 | url = args[++i]; |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 88 | transport = TransportChoice.Http; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 89 | } |
| 90 | else if (args[i] == "-n") |
| 91 | { |
| 92 | numIterations = Convert.ToInt32(args[++i]); |
| 93 | } |
| 94 | else if (args[i].StartsWith("--pipe=")) |
| 95 | { |
| 96 | pipe = args[i].Substring(args[i].IndexOf("=") + 1); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 97 | transport = TransportChoice.NamedPipe; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 98 | } |
| 99 | else if (args[i].StartsWith("--host=")) |
| 100 | { |
| 101 | // check there for ipaddress |
Jens Geyer | 22c412e | 2019-03-12 01:06:25 +0100 | [diff] [blame] | 102 | host = args[i].Substring(args[i].IndexOf("=") + 1); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 103 | if (transport != TransportChoice.TlsSocket) |
| 104 | transport = TransportChoice.Socket; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 105 | } |
| 106 | else if (args[i].StartsWith("--port=")) |
| 107 | { |
| 108 | port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1)); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 109 | if (transport != TransportChoice.TlsSocket) |
| 110 | transport = TransportChoice.Socket; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 111 | } |
| 112 | else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered") |
| 113 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 114 | layered = LayeredChoice.Buffered; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 115 | } |
| 116 | else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed") |
| 117 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 118 | layered = LayeredChoice.Framed; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 119 | } |
| 120 | else if (args[i] == "-t") |
| 121 | { |
| 122 | numThreads = Convert.ToInt32(args[++i]); |
| 123 | } |
| 124 | else if (args[i] == "--binary" || args[i] == "--protocol=binary") |
| 125 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 126 | protocol = ProtocolChoice.Binary; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 127 | } |
| 128 | else if (args[i] == "--compact" || args[i] == "--protocol=compact") |
| 129 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 130 | protocol = ProtocolChoice.Compact; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 131 | } |
| 132 | else if (args[i] == "--json" || args[i] == "--protocol=json") |
| 133 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 134 | protocol = ProtocolChoice.Json; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 135 | } |
| 136 | else if (args[i] == "--ssl") |
| 137 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 138 | transport = TransportChoice.TlsSocket; |
| 139 | } |
| 140 | else if (args[i] == "--help") |
| 141 | { |
| 142 | PrintOptionsHelp(); |
| 143 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 144 | } |
| 145 | else |
| 146 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 147 | Console.WriteLine("Invalid argument: {0}", args[i]); |
| 148 | PrintOptionsHelp(); |
| 149 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 152 | |
| 153 | switch (transport) |
| 154 | { |
| 155 | case TransportChoice.Socket: |
| 156 | Console.WriteLine("Using socket transport"); |
| 157 | break; |
| 158 | case TransportChoice.TlsSocket: |
| 159 | Console.WriteLine("Using encrypted transport"); |
| 160 | break; |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 161 | case TransportChoice.Http: |
| 162 | Console.WriteLine("Using HTTP transport"); |
| 163 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 164 | case TransportChoice.NamedPipe: |
| 165 | Console.WriteLine("Using named pipes transport"); |
| 166 | break; |
| 167 | default: // unhandled case |
| 168 | Debug.Assert(false); |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | switch (layered) |
| 173 | { |
| 174 | case LayeredChoice.Framed: |
| 175 | Console.WriteLine("Using framed transport"); |
| 176 | break; |
| 177 | case LayeredChoice.Buffered: |
| 178 | Console.WriteLine("Using buffered transport"); |
| 179 | break; |
| 180 | default: // unhandled case? |
| 181 | Debug.Assert(layered == LayeredChoice.None); |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | switch (protocol) |
| 186 | { |
| 187 | case ProtocolChoice.Binary: |
| 188 | Console.WriteLine("Using binary protocol"); |
| 189 | break; |
| 190 | case ProtocolChoice.Compact: |
| 191 | Console.WriteLine("Using compact protocol"); |
| 192 | break; |
| 193 | case ProtocolChoice.Json: |
| 194 | Console.WriteLine("Using JSON protocol"); |
| 195 | break; |
| 196 | default: // unhandled case? |
| 197 | Debug.Assert(false); |
| 198 | break; |
| 199 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | private static X509Certificate2 GetClientCert() |
| 203 | { |
| 204 | var clientCertName = "client.p12"; |
| 205 | var possiblePaths = new List<string> |
| 206 | { |
| 207 | "../../../keys/", |
| 208 | "../../keys/", |
| 209 | "../keys/", |
| 210 | "keys/", |
| 211 | }; |
| 212 | |
| 213 | string existingPath = null; |
| 214 | foreach (var possiblePath in possiblePaths) |
| 215 | { |
| 216 | var path = Path.GetFullPath(possiblePath + clientCertName); |
| 217 | if (File.Exists(path)) |
| 218 | { |
| 219 | existingPath = path; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (string.IsNullOrEmpty(existingPath)) |
| 225 | { |
| 226 | throw new FileNotFoundException($"Cannot find file: {clientCertName}"); |
| 227 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 228 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 229 | var cert = new X509Certificate2(existingPath, "thrift"); |
| 230 | |
| 231 | return cert; |
| 232 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 233 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 234 | public TTransport CreateTransport() |
| 235 | { |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 236 | // endpoint transport |
| 237 | TTransport trans = null; |
| 238 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 239 | switch (transport) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 240 | { |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 241 | case TransportChoice.Http: |
| 242 | Debug.Assert(url != null); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 243 | trans = new THttpTransport(new Uri(url), Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 244 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 245 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 246 | case TransportChoice.NamedPipe: |
| 247 | Debug.Assert(pipe != null); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 248 | trans = new TNamedPipeTransport(pipe,Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 249 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 250 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 251 | case TransportChoice.TlsSocket: |
| 252 | var cert = GetClientCert(); |
| 253 | if (cert == null || !cert.HasPrivateKey) |
| 254 | { |
| 255 | throw new InvalidOperationException("Certificate doesn't contain private key"); |
| 256 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 257 | |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 258 | trans = new TTlsSocketTransport(host, port, Configuration, 0, |
| 259 | cert, |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 260 | (sender, certificate, chain, errors) => true, |
| 261 | null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12); |
| 262 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 263 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 264 | case TransportChoice.Socket: |
| 265 | default: |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 266 | trans = new TSocketTransport(host, port, Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 267 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 270 | |
| 271 | // layered transport |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 272 | switch (layered) |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 273 | { |
| 274 | case LayeredChoice.Buffered: |
| 275 | trans = new TBufferedTransport(trans); |
| 276 | break; |
| 277 | case LayeredChoice.Framed: |
| 278 | trans = new TFramedTransport(trans); |
| 279 | break; |
| 280 | default: |
| 281 | Debug.Assert(layered == LayeredChoice.None); |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | return trans; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | public TProtocol CreateProtocol(TTransport transport) |
| 289 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 290 | switch (protocol) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 291 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 292 | case ProtocolChoice.Compact: |
| 293 | return new TCompactProtocol(transport); |
| 294 | case ProtocolChoice.Json: |
| 295 | return new TJsonProtocol(transport); |
| 296 | case ProtocolChoice.Binary: |
| 297 | default: |
| 298 | return new TBinaryProtocol(transport); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 299 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | private const int ErrorBaseTypes = 1; |
| 305 | private const int ErrorStructs = 2; |
| 306 | private const int ErrorContainers = 4; |
| 307 | private const int ErrorExceptions = 8; |
| 308 | private const int ErrorUnknown = 64; |
| 309 | |
| 310 | private class ClientTest |
| 311 | { |
| 312 | private readonly TTransport transport; |
| 313 | private readonly ThriftTest.Client client; |
| 314 | private readonly int numIterations; |
| 315 | private bool done; |
| 316 | |
| 317 | public int ReturnCode { get; set; } |
| 318 | |
| 319 | public ClientTest(TestParams param) |
| 320 | { |
| 321 | transport = param.CreateTransport(); |
| 322 | client = new ThriftTest.Client(param.CreateProtocol(transport)); |
| 323 | numIterations = param.numIterations; |
| 324 | } |
| 325 | |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 326 | public async Task Execute() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 327 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 328 | if (done) |
| 329 | { |
| 330 | Console.WriteLine("Execute called more than once"); |
| 331 | throw new InvalidOperationException(); |
| 332 | } |
| 333 | |
| 334 | for (var i = 0; i < numIterations; i++) |
| 335 | { |
| 336 | try |
| 337 | { |
| 338 | if (!transport.IsOpen) |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 339 | await transport.OpenAsync(MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 340 | } |
| 341 | catch (TTransportException ex) |
| 342 | { |
| 343 | Console.WriteLine("*** FAILED ***"); |
| 344 | Console.WriteLine("Connect failed: " + ex.Message); |
| 345 | ReturnCode |= ErrorUnknown; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 346 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 347 | continue; |
| 348 | } |
| 349 | catch (Exception ex) |
| 350 | { |
| 351 | Console.WriteLine("*** FAILED ***"); |
| 352 | Console.WriteLine("Connect failed: " + ex.Message); |
| 353 | ReturnCode |= ErrorUnknown; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 354 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 355 | continue; |
| 356 | } |
| 357 | |
| 358 | try |
| 359 | { |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 360 | ReturnCode |= await ExecuteClientTest(client); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 361 | } |
| 362 | catch (Exception ex) |
| 363 | { |
| 364 | Console.WriteLine("*** FAILED ***"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 365 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 366 | ReturnCode |= ErrorUnknown; |
| 367 | } |
| 368 | } |
| 369 | try |
| 370 | { |
| 371 | transport.Close(); |
| 372 | } |
| 373 | catch (Exception ex) |
| 374 | { |
| 375 | Console.WriteLine("Error while closing transport"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 376 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 377 | } |
| 378 | done = true; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | internal static void PrintOptionsHelp() |
| 383 | { |
| 384 | Console.WriteLine("Client options:"); |
| 385 | Console.WriteLine(" -u <URL>"); |
| 386 | Console.WriteLine(" -t <# of threads to run> default = 1"); |
| 387 | Console.WriteLine(" -n <# of iterations> per thread"); |
| 388 | Console.WriteLine(" --pipe=<pipe name>"); |
| 389 | Console.WriteLine(" --host=<IP address>"); |
| 390 | Console.WriteLine(" --port=<port number>"); |
| 391 | Console.WriteLine(" --transport=<transport name> one of buffered,framed (defaults to none)"); |
| 392 | Console.WriteLine(" --protocol=<protocol name> one of compact,json (defaults to binary)"); |
| 393 | Console.WriteLine(" --ssl"); |
| 394 | Console.WriteLine(); |
| 395 | } |
| 396 | |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 397 | public static async Task<int> Execute(List<string> args) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 398 | { |
| 399 | try |
| 400 | { |
| 401 | var param = new TestParams(); |
| 402 | |
| 403 | try |
| 404 | { |
| 405 | param.Parse(args); |
| 406 | } |
| 407 | catch (Exception ex) |
| 408 | { |
| 409 | Console.WriteLine("*** FAILED ***"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 410 | Console.WriteLine("Error while parsing arguments"); |
| 411 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 412 | return ErrorUnknown; |
| 413 | } |
| 414 | |
| 415 | var tests = Enumerable.Range(0, param.numThreads).Select(_ => new ClientTest(param)).ToArray(); |
| 416 | |
| 417 | //issue tests on separate threads simultaneously |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 418 | var start = DateTime.Now; |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 419 | var tasks = tests.Select(test => test.Execute()).ToArray(); |
| 420 | Task.WaitAll(tasks); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 421 | Console.WriteLine("Total time: " + (DateTime.Now - start)); |
| 422 | Console.WriteLine(); |
| 423 | return tests.Select(t => t.ReturnCode).Aggregate((r1, r2) => r1 | r2); |
| 424 | } |
| 425 | catch (Exception outerEx) |
| 426 | { |
| 427 | Console.WriteLine("*** FAILED ***"); |
| 428 | Console.WriteLine("Unexpected error"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 429 | Console.WriteLine(outerEx.Message + "\n" + outerEx.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 430 | return ErrorUnknown; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | public static string BytesToHex(byte[] data) |
| 435 | { |
| 436 | return BitConverter.ToString(data).Replace("-", string.Empty); |
| 437 | } |
| 438 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 439 | |
| 440 | public enum BinaryTestSize |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 441 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 442 | Empty, // Edge case: the zero-length empty binary |
| 443 | Normal, // Fairly small array of usual size (256 bytes) |
| 444 | Large, // Large writes/reads may cause range check errors |
| 445 | PipeWriteLimit, // Windows Limit: Pipe write operations across a network are limited to 65,535 bytes per write. |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 446 | FifteenMB // that's quite a bit of data |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | public static byte[] PrepareTestData(bool randomDist, BinaryTestSize testcase) |
| 450 | { |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 451 | int amount; |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 452 | switch (testcase) |
| 453 | { |
| 454 | case BinaryTestSize.Empty: |
| 455 | amount = 0; |
| 456 | break; |
| 457 | case BinaryTestSize.Normal: |
| 458 | amount = 0x100; |
| 459 | break; |
| 460 | case BinaryTestSize.Large: |
| 461 | amount = 0x8000 + 128; |
| 462 | break; |
| 463 | case BinaryTestSize.PipeWriteLimit: |
| 464 | amount = 0xFFFF + 128; |
| 465 | break; |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 466 | case BinaryTestSize.FifteenMB: |
| 467 | amount = 15 * 1024 * 1024; |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 468 | break; |
| 469 | default: |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 470 | throw new ArgumentException("invalid argument",nameof(testcase)); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | var retval = new byte[amount]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 474 | |
| 475 | // linear distribution, unless random is requested |
| 476 | if (!randomDist) |
| 477 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 478 | for (var i = 0; i < retval.Length; ++i) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 479 | { |
| 480 | retval[i] = (byte)i; |
| 481 | } |
| 482 | return retval; |
| 483 | } |
| 484 | |
| 485 | // random distribution |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 486 | var rnd = new Random(); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 487 | for (var i = 1; i < retval.Length; ++i) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 488 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 489 | retval[i] = (byte)rnd.Next(0x100); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 490 | } |
| 491 | return retval; |
| 492 | } |
| 493 | |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 494 | private static CancellationToken MakeTimeoutToken(int msec = 5000) |
| 495 | { |
| 496 | var token = new CancellationTokenSource(msec); |
| 497 | return token.Token; |
| 498 | } |
| 499 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 500 | public static async Task<int> ExecuteClientTest(ThriftTest.Client client) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 501 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 502 | var returnCode = 0; |
| 503 | |
| 504 | Console.Write("testVoid()"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 505 | await client.testVoid(MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 506 | Console.WriteLine(" = void"); |
| 507 | |
| 508 | Console.Write("testString(\"Test\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 509 | var s = await client.testString("Test", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 510 | Console.WriteLine(" = \"" + s + "\""); |
| 511 | if ("Test" != s) |
| 512 | { |
| 513 | Console.WriteLine("*** FAILED ***"); |
| 514 | returnCode |= ErrorBaseTypes; |
| 515 | } |
| 516 | |
| 517 | Console.Write("testBool(true)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 518 | var t = await client.testBool((bool)true, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 519 | Console.WriteLine(" = " + t); |
| 520 | if (!t) |
| 521 | { |
| 522 | Console.WriteLine("*** FAILED ***"); |
| 523 | returnCode |= ErrorBaseTypes; |
| 524 | } |
| 525 | Console.Write("testBool(false)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 526 | var f = await client.testBool((bool)false, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 527 | Console.WriteLine(" = " + f); |
| 528 | if (f) |
| 529 | { |
| 530 | Console.WriteLine("*** FAILED ***"); |
| 531 | returnCode |= ErrorBaseTypes; |
| 532 | } |
| 533 | |
| 534 | Console.Write("testByte(1)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 535 | var i8 = await client.testByte((sbyte)1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 536 | Console.WriteLine(" = " + i8); |
| 537 | if (1 != i8) |
| 538 | { |
| 539 | Console.WriteLine("*** FAILED ***"); |
| 540 | returnCode |= ErrorBaseTypes; |
| 541 | } |
| 542 | |
| 543 | Console.Write("testI32(-1)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 544 | var i32 = await client.testI32(-1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 545 | Console.WriteLine(" = " + i32); |
| 546 | if (-1 != i32) |
| 547 | { |
| 548 | Console.WriteLine("*** FAILED ***"); |
| 549 | returnCode |= ErrorBaseTypes; |
| 550 | } |
| 551 | |
| 552 | Console.Write("testI64(-34359738368)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 553 | var i64 = await client.testI64(-34359738368, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 554 | Console.WriteLine(" = " + i64); |
| 555 | if (-34359738368 != i64) |
| 556 | { |
| 557 | Console.WriteLine("*** FAILED ***"); |
| 558 | returnCode |= ErrorBaseTypes; |
| 559 | } |
| 560 | |
| 561 | // TODO: Validate received message |
| 562 | Console.Write("testDouble(5.325098235)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 563 | var dub = await client.testDouble(5.325098235, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 564 | Console.WriteLine(" = " + dub); |
| 565 | if (5.325098235 != dub) |
| 566 | { |
| 567 | Console.WriteLine("*** FAILED ***"); |
| 568 | returnCode |= ErrorBaseTypes; |
| 569 | } |
| 570 | Console.Write("testDouble(-0.000341012439638598279)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 571 | dub = await client.testDouble(-0.000341012439638598279, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 572 | Console.WriteLine(" = " + dub); |
| 573 | if (-0.000341012439638598279 != dub) |
| 574 | { |
| 575 | Console.WriteLine("*** FAILED ***"); |
| 576 | returnCode |= ErrorBaseTypes; |
| 577 | } |
| 578 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 579 | // testBinary() |
| 580 | foreach(BinaryTestSize binTestCase in Enum.GetValues(typeof(BinaryTestSize))) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 581 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 582 | var binOut = PrepareTestData(true, binTestCase); |
| 583 | |
| 584 | Console.Write("testBinary({0} bytes)", binOut.Length); |
| 585 | try |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 586 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 587 | var binIn = await client.testBinary(binOut, MakeTimeoutToken()); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 588 | Console.WriteLine(" = {0} bytes", binIn.Length); |
| 589 | if (binIn.Length != binOut.Length) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 590 | { |
| 591 | Console.WriteLine("*** FAILED ***"); |
| 592 | returnCode |= ErrorBaseTypes; |
| 593 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 594 | for (var ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs) |
| 595 | { |
| 596 | if (binIn[ofs] != binOut[ofs]) |
| 597 | { |
| 598 | Console.WriteLine("*** FAILED ***"); |
| 599 | returnCode |= ErrorBaseTypes; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | catch (Thrift.TApplicationException ex) |
| 604 | { |
| 605 | Console.WriteLine("*** FAILED ***"); |
| 606 | returnCode |= ErrorBaseTypes; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 607 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 608 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 609 | } |
| 610 | |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 611 | // CrazyNesting |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 612 | Console.WriteLine("Test CrazyNesting"); |
| 613 | var one = new CrazyNesting(); |
| 614 | var two = new CrazyNesting(); |
| 615 | one.String_field = "crazy"; |
| 616 | two.String_field = "crazy"; |
| 617 | one.Binary_field = new byte[] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF }; |
| 618 | two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF }; |
| 619 | if (typeof(CrazyNesting).GetMethod("Equals")?.DeclaringType == typeof(CrazyNesting)) |
| 620 | { |
| 621 | if (!one.Equals(two)) |
| 622 | { |
| 623 | Console.WriteLine("*** FAILED ***"); |
| 624 | returnCode |= ErrorContainers; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
| 628 | // TODO: Validate received message |
| 629 | Console.Write("testStruct({\"Zero\", 1, -3, -5})"); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 630 | var o = new Xtruct |
| 631 | { |
| 632 | String_thing = "Zero", |
| 633 | Byte_thing = (sbyte)1, |
| 634 | I32_thing = -3, |
| 635 | I64_thing = -5 |
| 636 | }; |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 637 | var i = await client.testStruct(o, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 638 | Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}"); |
| 639 | |
| 640 | // TODO: Validate received message |
| 641 | Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})"); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 642 | var o2 = new Xtruct2 |
| 643 | { |
| 644 | Byte_thing = (sbyte)1, |
| 645 | Struct_thing = o, |
| 646 | I32_thing = 5 |
| 647 | }; |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 648 | var i2 = await client.testNest(o2, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 649 | i = i2.Struct_thing; |
| 650 | Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}"); |
| 651 | |
| 652 | var mapout = new Dictionary<int, int>(); |
| 653 | for (var j = 0; j < 5; j++) |
| 654 | { |
| 655 | mapout[j] = j - 10; |
| 656 | } |
| 657 | Console.Write("testMap({"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 658 | Console.Write(string.Join(", ", mapout.Select((pair) => { return pair.Key + " => " + pair.Value; }))); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 659 | Console.Write("})"); |
| 660 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 661 | var mapin = await client.testMap(mapout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 662 | |
| 663 | Console.Write(" = {"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 664 | Console.Write(string.Join(", ", mapin.Select((pair) => { return pair.Key + " => " + pair.Value; }))); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 665 | Console.WriteLine("}"); |
| 666 | |
| 667 | // TODO: Validate received message |
| 668 | var listout = new List<int>(); |
| 669 | for (var j = -2; j < 3; j++) |
| 670 | { |
| 671 | listout.Add(j); |
| 672 | } |
| 673 | Console.Write("testList({"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 674 | Console.Write(string.Join(", ", listout)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 675 | Console.Write("})"); |
| 676 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 677 | var listin = await client.testList(listout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 678 | |
| 679 | Console.Write(" = {"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 680 | Console.Write(string.Join(", ", listin)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 681 | Console.WriteLine("}"); |
| 682 | |
| 683 | //set |
| 684 | // TODO: Validate received message |
| 685 | var setout = new THashSet<int>(); |
| 686 | for (var j = -2; j < 3; j++) |
| 687 | { |
| 688 | setout.Add(j); |
| 689 | } |
| 690 | Console.Write("testSet({"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 691 | Console.Write(string.Join(", ", setout)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 692 | Console.Write("})"); |
| 693 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 694 | var setin = await client.testSet(setout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 695 | |
| 696 | Console.Write(" = {"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame^] | 697 | Console.Write(string.Join(", ", setin)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 698 | Console.WriteLine("}"); |
| 699 | |
| 700 | |
| 701 | Console.Write("testEnum(ONE)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 702 | var ret = await client.testEnum(Numberz.ONE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 703 | Console.WriteLine(" = " + ret); |
| 704 | if (Numberz.ONE != ret) |
| 705 | { |
| 706 | Console.WriteLine("*** FAILED ***"); |
| 707 | returnCode |= ErrorStructs; |
| 708 | } |
| 709 | |
| 710 | Console.Write("testEnum(TWO)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 711 | ret = await client.testEnum(Numberz.TWO, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 712 | Console.WriteLine(" = " + ret); |
| 713 | if (Numberz.TWO != ret) |
| 714 | { |
| 715 | Console.WriteLine("*** FAILED ***"); |
| 716 | returnCode |= ErrorStructs; |
| 717 | } |
| 718 | |
| 719 | Console.Write("testEnum(THREE)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 720 | ret = await client.testEnum(Numberz.THREE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 721 | Console.WriteLine(" = " + ret); |
| 722 | if (Numberz.THREE != ret) |
| 723 | { |
| 724 | Console.WriteLine("*** FAILED ***"); |
| 725 | returnCode |= ErrorStructs; |
| 726 | } |
| 727 | |
| 728 | Console.Write("testEnum(FIVE)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 729 | ret = await client.testEnum(Numberz.FIVE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 730 | Console.WriteLine(" = " + ret); |
| 731 | if (Numberz.FIVE != ret) |
| 732 | { |
| 733 | Console.WriteLine("*** FAILED ***"); |
| 734 | returnCode |= ErrorStructs; |
| 735 | } |
| 736 | |
| 737 | Console.Write("testEnum(EIGHT)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 738 | ret = await client.testEnum(Numberz.EIGHT, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 739 | Console.WriteLine(" = " + ret); |
| 740 | if (Numberz.EIGHT != ret) |
| 741 | { |
| 742 | Console.WriteLine("*** FAILED ***"); |
| 743 | returnCode |= ErrorStructs; |
| 744 | } |
| 745 | |
| 746 | Console.Write("testTypedef(309858235082523)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 747 | var uid = await client.testTypedef(309858235082523L, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 748 | Console.WriteLine(" = " + uid); |
| 749 | if (309858235082523L != uid) |
| 750 | { |
| 751 | Console.WriteLine("*** FAILED ***"); |
| 752 | returnCode |= ErrorStructs; |
| 753 | } |
| 754 | |
| 755 | // TODO: Validate received message |
| 756 | Console.Write("testMapMap(1)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 757 | var mm = await client.testMapMap(1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 758 | Console.Write(" = {"); |
| 759 | foreach (var key in mm.Keys) |
| 760 | { |
| 761 | Console.Write(key + " => {"); |
| 762 | var m2 = mm[key]; |
| 763 | foreach (var k2 in m2.Keys) |
| 764 | { |
| 765 | Console.Write(k2 + " => " + m2[k2] + ", "); |
| 766 | } |
| 767 | Console.Write("}, "); |
| 768 | } |
| 769 | Console.WriteLine("}"); |
| 770 | |
| 771 | // TODO: Validate received message |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 772 | var insane = new Insanity |
| 773 | { |
| 774 | UserMap = new Dictionary<Numberz, long> |
| 775 | { |
| 776 | [Numberz.FIVE] = 5000L |
| 777 | } |
| 778 | }; |
| 779 | var truck = new Xtruct |
| 780 | { |
| 781 | String_thing = "Truck", |
| 782 | Byte_thing = (sbyte)8, |
| 783 | I32_thing = 8, |
| 784 | I64_thing = 8 |
| 785 | }; |
| 786 | insane.Xtructs = new List<Xtruct> |
| 787 | { |
| 788 | truck |
| 789 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 790 | Console.Write("testInsanity()"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 791 | var whoa = await client.testInsanity(insane, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 792 | Console.Write(" = {"); |
| 793 | foreach (var key in whoa.Keys) |
| 794 | { |
| 795 | var val = whoa[key]; |
| 796 | Console.Write(key + " => {"); |
| 797 | |
| 798 | foreach (var k2 in val.Keys) |
| 799 | { |
| 800 | var v2 = val[k2]; |
| 801 | |
| 802 | Console.Write(k2 + " => {"); |
| 803 | var userMap = v2.UserMap; |
| 804 | |
| 805 | Console.Write("{"); |
| 806 | if (userMap != null) |
| 807 | { |
| 808 | foreach (var k3 in userMap.Keys) |
| 809 | { |
| 810 | Console.Write(k3 + " => " + userMap[k3] + ", "); |
| 811 | } |
| 812 | } |
| 813 | else |
| 814 | { |
| 815 | Console.Write("null"); |
| 816 | } |
| 817 | Console.Write("}, "); |
| 818 | |
| 819 | var xtructs = v2.Xtructs; |
| 820 | |
| 821 | Console.Write("{"); |
| 822 | if (xtructs != null) |
| 823 | { |
| 824 | foreach (var x in xtructs) |
| 825 | { |
| 826 | Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, "); |
| 827 | } |
| 828 | } |
| 829 | else |
| 830 | { |
| 831 | Console.Write("null"); |
| 832 | } |
| 833 | Console.Write("}"); |
| 834 | |
| 835 | Console.Write("}, "); |
| 836 | } |
| 837 | Console.Write("}, "); |
| 838 | } |
| 839 | Console.WriteLine("}"); |
| 840 | |
| 841 | sbyte arg0 = 1; |
| 842 | var arg1 = 2; |
| 843 | var arg2 = long.MaxValue; |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 844 | var multiDict = new Dictionary<short, string> |
| 845 | { |
| 846 | [1] = "one" |
| 847 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 848 | |
| 849 | var tmpMultiDict = new List<string>(); |
| 850 | foreach (var pair in multiDict) |
| 851 | tmpMultiDict.Add(pair.Key +" => "+ pair.Value); |
| 852 | |
| 853 | var arg4 = Numberz.FIVE; |
| 854 | long arg5 = 5000000; |
| 855 | Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + ",{" + string.Join(",", tmpMultiDict) + "}," + arg4 + "," + arg5 + ")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 856 | var multiResponse = await client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 857 | Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing |
| 858 | + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n"); |
| 859 | |
| 860 | try |
| 861 | { |
| 862 | Console.WriteLine("testException(\"Xception\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 863 | await client.testException("Xception", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 864 | Console.WriteLine("*** FAILED ***"); |
| 865 | returnCode |= ErrorExceptions; |
| 866 | } |
| 867 | catch (Xception ex) |
| 868 | { |
| 869 | if (ex.ErrorCode != 1001 || ex.Message != "Xception") |
| 870 | { |
| 871 | Console.WriteLine("*** FAILED ***"); |
| 872 | returnCode |= ErrorExceptions; |
| 873 | } |
| 874 | } |
| 875 | catch (Exception ex) |
| 876 | { |
| 877 | Console.WriteLine("*** FAILED ***"); |
| 878 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 879 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 880 | } |
| 881 | try |
| 882 | { |
| 883 | Console.WriteLine("testException(\"TException\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 884 | await client.testException("TException", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 885 | Console.WriteLine("*** FAILED ***"); |
| 886 | returnCode |= ErrorExceptions; |
| 887 | } |
| 888 | catch (Thrift.TException) |
| 889 | { |
| 890 | // OK |
| 891 | } |
| 892 | catch (Exception ex) |
| 893 | { |
| 894 | Console.WriteLine("*** FAILED ***"); |
| 895 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 896 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 897 | } |
| 898 | try |
| 899 | { |
| 900 | Console.WriteLine("testException(\"ok\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 901 | await client.testException("ok", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 902 | // OK |
| 903 | } |
| 904 | catch (Exception ex) |
| 905 | { |
| 906 | Console.WriteLine("*** FAILED ***"); |
| 907 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 908 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | try |
| 912 | { |
| 913 | Console.WriteLine("testMultiException(\"Xception\", ...)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 914 | await client.testMultiException("Xception", "ignore", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 915 | Console.WriteLine("*** FAILED ***"); |
| 916 | returnCode |= ErrorExceptions; |
| 917 | } |
| 918 | catch (Xception ex) |
| 919 | { |
| 920 | if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception") |
| 921 | { |
| 922 | Console.WriteLine("*** FAILED ***"); |
| 923 | returnCode |= ErrorExceptions; |
| 924 | } |
| 925 | } |
| 926 | catch (Exception ex) |
| 927 | { |
| 928 | Console.WriteLine("*** FAILED ***"); |
| 929 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 930 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 931 | } |
| 932 | try |
| 933 | { |
| 934 | Console.WriteLine("testMultiException(\"Xception2\", ...)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 935 | await client.testMultiException("Xception2", "ignore", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 936 | Console.WriteLine("*** FAILED ***"); |
| 937 | returnCode |= ErrorExceptions; |
| 938 | } |
| 939 | catch (Xception2 ex) |
| 940 | { |
| 941 | if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2") |
| 942 | { |
| 943 | Console.WriteLine("*** FAILED ***"); |
| 944 | returnCode |= ErrorExceptions; |
| 945 | } |
| 946 | } |
| 947 | catch (Exception ex) |
| 948 | { |
| 949 | Console.WriteLine("*** FAILED ***"); |
| 950 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 951 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 952 | } |
| 953 | try |
| 954 | { |
| 955 | Console.WriteLine("testMultiException(\"success\", \"OK\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 956 | if ("OK" != (await client.testMultiException("success", "OK", MakeTimeoutToken())).String_thing) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 957 | { |
| 958 | Console.WriteLine("*** FAILED ***"); |
| 959 | returnCode |= ErrorExceptions; |
| 960 | } |
| 961 | } |
| 962 | catch (Exception ex) |
| 963 | { |
| 964 | Console.WriteLine("*** FAILED ***"); |
| 965 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 966 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 967 | } |
| 968 | |
Jens Geyer | b11f63c | 2019-03-14 21:12:38 +0100 | [diff] [blame] | 969 | Console.WriteLine("Test Oneway(1)"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 970 | var sw = new Stopwatch(); |
| 971 | sw.Start(); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 972 | await client.testOneway(1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 973 | sw.Stop(); |
| 974 | if (sw.ElapsedMilliseconds > 1000) |
| 975 | { |
| 976 | Console.WriteLine("*** FAILED ***"); |
| 977 | returnCode |= ErrorBaseTypes; |
| 978 | } |
| 979 | |
| 980 | Console.Write("Test Calltime()"); |
| 981 | var times = 50; |
| 982 | sw.Reset(); |
| 983 | sw.Start(); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 984 | var token = MakeTimeoutToken(20000); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 985 | for (var k = 0; k < times; ++k) |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 986 | await client.testVoid(token); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 987 | sw.Stop(); |
| 988 | Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times); |
| 989 | return returnCode; |
| 990 | } |
| 991 | } |
| 992 | } |