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