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