blob: 0c147dcc73b2e1b7f012322323ebcac36aea2ffe [file] [log] [blame]
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001// 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
18using System;
19using System.Collections.Generic;
20using System.Diagnostics;
21using System.IO;
22using System.Linq;
23using System.Net;
24using System.Reflection;
25using System.Security.Authentication;
26using System.Security.Cryptography.X509Certificates;
27using System.ServiceModel;
28using System.Text;
29using System.Threading;
30using System.Threading.Tasks;
Jens Geyereacd1d42019-11-20 19:03:14 +010031using Thrift;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010032using Thrift.Collections;
33using Thrift.Protocol;
34using Thrift.Transport;
35using Thrift.Transport.Client;
36
37namespace ThriftTest
38{
Jens Geyeradde44b2019-02-05 01:00:02 +010039 internal enum ProtocolChoice
40 {
41 Binary,
42 Compact,
43 Json
44 }
45
46 // it does not make much sense to use buffered when we already use framed
47 internal enum LayeredChoice
48 {
49 None,
50 Buffered,
51 Framed
52 }
53
54
55 internal enum TransportChoice
56 {
57 Socket,
58 TlsSocket,
Jens Geyer96c61132019-06-14 22:39:56 +020059 Http,
Jens Geyeradde44b2019-02-05 01:00:02 +010060 NamedPipe
61 }
62
Jens Geyeraa0c8b32019-01-28 23:27:45 +010063 public class TestClient
64 {
65 private class TestParams
66 {
67 public int numIterations = 1;
Jens Geyer22c412e2019-03-12 01:06:25 +010068 public string host = "localhost";
Jens Geyeraa0c8b32019-01-28 23:27:45 +010069 public int port = 9090;
70 public int numThreads = 1;
71 public string url;
72 public string pipe;
Jens Geyeradde44b2019-02-05 01:00:02 +010073 public LayeredChoice layered = LayeredChoice.None;
74 public ProtocolChoice protocol = ProtocolChoice.Binary;
75 public TransportChoice transport = TransportChoice.Socket;
Jens Geyereacd1d42019-11-20 19:03:14 +010076 private readonly TConfiguration Configuration = null; // or new TConfiguration() if needed
Jens Geyeraa0c8b32019-01-28 23:27:45 +010077
Jens Geyerbd1a2732019-06-26 22:52:44 +020078 internal void Parse(List<string> args)
Jens Geyeraa0c8b32019-01-28 23:27:45 +010079 {
80 for (var i = 0; i < args.Count; ++i)
81 {
82 if (args[i] == "-u")
83 {
84 url = args[++i];
Jens Geyer96c61132019-06-14 22:39:56 +020085 transport = TransportChoice.Http;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010086 }
87 else if (args[i] == "-n")
88 {
89 numIterations = Convert.ToInt32(args[++i]);
90 }
91 else if (args[i].StartsWith("--pipe="))
92 {
93 pipe = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +010094 transport = TransportChoice.NamedPipe;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010095 }
96 else if (args[i].StartsWith("--host="))
97 {
98 // check there for ipaddress
Jens Geyer22c412e2019-03-12 01:06:25 +010099 host = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +0100100 if (transport != TransportChoice.TlsSocket)
101 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100102 }
103 else if (args[i].StartsWith("--port="))
104 {
105 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyeradde44b2019-02-05 01:00:02 +0100106 if (transport != TransportChoice.TlsSocket)
107 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100108 }
109 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
110 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100111 layered = LayeredChoice.Buffered;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100112 }
113 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
114 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100115 layered = LayeredChoice.Framed;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100116 }
117 else if (args[i] == "-t")
118 {
119 numThreads = Convert.ToInt32(args[++i]);
120 }
121 else if (args[i] == "--binary" || args[i] == "--protocol=binary")
122 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100123 protocol = ProtocolChoice.Binary;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100124 }
125 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
126 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100127 protocol = ProtocolChoice.Compact;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100128 }
129 else if (args[i] == "--json" || args[i] == "--protocol=json")
130 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100131 protocol = ProtocolChoice.Json;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100132 }
133 else if (args[i] == "--ssl")
134 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100135 transport = TransportChoice.TlsSocket;
136 }
137 else if (args[i] == "--help")
138 {
139 PrintOptionsHelp();
140 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100141 }
142 else
143 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100144 Console.WriteLine("Invalid argument: {0}", args[i]);
145 PrintOptionsHelp();
146 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100147 }
148 }
Jens Geyeradde44b2019-02-05 01:00:02 +0100149
150 switch (transport)
151 {
152 case TransportChoice.Socket:
153 Console.WriteLine("Using socket transport");
154 break;
155 case TransportChoice.TlsSocket:
156 Console.WriteLine("Using encrypted transport");
157 break;
Jens Geyer96c61132019-06-14 22:39:56 +0200158 case TransportChoice.Http:
159 Console.WriteLine("Using HTTP transport");
160 break;
Jens Geyeradde44b2019-02-05 01:00:02 +0100161 case TransportChoice.NamedPipe:
162 Console.WriteLine("Using named pipes transport");
163 break;
164 default: // unhandled case
165 Debug.Assert(false);
166 break;
167 }
168
169 switch (layered)
170 {
171 case LayeredChoice.Framed:
172 Console.WriteLine("Using framed transport");
173 break;
174 case LayeredChoice.Buffered:
175 Console.WriteLine("Using buffered transport");
176 break;
177 default: // unhandled case?
178 Debug.Assert(layered == LayeredChoice.None);
179 break;
180 }
181
182 switch (protocol)
183 {
184 case ProtocolChoice.Binary:
185 Console.WriteLine("Using binary protocol");
186 break;
187 case ProtocolChoice.Compact:
188 Console.WriteLine("Using compact protocol");
189 break;
190 case ProtocolChoice.Json:
191 Console.WriteLine("Using JSON protocol");
192 break;
193 default: // unhandled case?
194 Debug.Assert(false);
195 break;
196 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100197 }
198
199 private static X509Certificate2 GetClientCert()
200 {
201 var clientCertName = "client.p12";
202 var possiblePaths = new List<string>
203 {
204 "../../../keys/",
205 "../../keys/",
206 "../keys/",
207 "keys/",
208 };
209
210 string existingPath = null;
211 foreach (var possiblePath in possiblePaths)
212 {
213 var path = Path.GetFullPath(possiblePath + clientCertName);
214 if (File.Exists(path))
215 {
216 existingPath = path;
217 break;
218 }
219 }
220
221 if (string.IsNullOrEmpty(existingPath))
222 {
223 throw new FileNotFoundException($"Cannot find file: {clientCertName}");
224 }
Jens Geyerbd1a2732019-06-26 22:52:44 +0200225
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100226 var cert = new X509Certificate2(existingPath, "thrift");
227
228 return cert;
229 }
Jens Geyerbd1a2732019-06-26 22:52:44 +0200230
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100231 public TTransport CreateTransport()
232 {
Jens Geyer96c61132019-06-14 22:39:56 +0200233 // endpoint transport
234 TTransport trans = null;
235
Jens Geyerbd1a2732019-06-26 22:52:44 +0200236 switch (transport)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100237 {
Jens Geyer96c61132019-06-14 22:39:56 +0200238 case TransportChoice.Http:
239 Debug.Assert(url != null);
Jens Geyereacd1d42019-11-20 19:03:14 +0100240 trans = new THttpTransport(new Uri(url), Configuration);
Jens Geyer96c61132019-06-14 22:39:56 +0200241 break;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100242
Jens Geyer96c61132019-06-14 22:39:56 +0200243 case TransportChoice.NamedPipe:
244 Debug.Assert(pipe != null);
Jens Geyereacd1d42019-11-20 19:03:14 +0100245 trans = new TNamedPipeTransport(pipe,Configuration);
Jens Geyer96c61132019-06-14 22:39:56 +0200246 break;
Jens Geyeradde44b2019-02-05 01:00:02 +0100247
Jens Geyer96c61132019-06-14 22:39:56 +0200248 case TransportChoice.TlsSocket:
249 var cert = GetClientCert();
250 if (cert == null || !cert.HasPrivateKey)
251 {
252 throw new InvalidOperationException("Certificate doesn't contain private key");
253 }
Jens Geyerbd1a2732019-06-26 22:52:44 +0200254
Jens Geyereacd1d42019-11-20 19:03:14 +0100255 trans = new TTlsSocketTransport(host, port, Configuration, 0,
256 cert,
Jens Geyer96c61132019-06-14 22:39:56 +0200257 (sender, certificate, chain, errors) => true,
258 null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
259 break;
Jens Geyeradde44b2019-02-05 01:00:02 +0100260
Jens Geyer96c61132019-06-14 22:39:56 +0200261 case TransportChoice.Socket:
262 default:
Jens Geyereacd1d42019-11-20 19:03:14 +0100263 trans = new TSocketTransport(host, port, Configuration);
Jens Geyer96c61132019-06-14 22:39:56 +0200264 break;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100265 }
266
Jens Geyer96c61132019-06-14 22:39:56 +0200267
268 // layered transport
Jens Geyerbd1a2732019-06-26 22:52:44 +0200269 switch (layered)
Jens Geyer96c61132019-06-14 22:39:56 +0200270 {
271 case LayeredChoice.Buffered:
272 trans = new TBufferedTransport(trans);
273 break;
274 case LayeredChoice.Framed:
275 trans = new TFramedTransport(trans);
276 break;
277 default:
278 Debug.Assert(layered == LayeredChoice.None);
279 break;
280 }
281
282 return trans;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100283 }
284
285 public TProtocol CreateProtocol(TTransport transport)
286 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100287 switch (protocol)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100288 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100289 case ProtocolChoice.Compact:
290 return new TCompactProtocol(transport);
291 case ProtocolChoice.Json:
292 return new TJsonProtocol(transport);
293 case ProtocolChoice.Binary:
294 default:
295 return new TBinaryProtocol(transport);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100296 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100297 }
298 }
299
300
301 private const int ErrorBaseTypes = 1;
302 private const int ErrorStructs = 2;
303 private const int ErrorContainers = 4;
304 private const int ErrorExceptions = 8;
305 private const int ErrorUnknown = 64;
306
307 private class ClientTest
308 {
309 private readonly TTransport transport;
310 private readonly ThriftTest.Client client;
311 private readonly int numIterations;
312 private bool done;
313
314 public int ReturnCode { get; set; }
315
316 public ClientTest(TestParams param)
317 {
318 transport = param.CreateTransport();
319 client = new ThriftTest.Client(param.CreateProtocol(transport));
320 numIterations = param.numIterations;
321 }
322
323 public void Execute()
324 {
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100325 if (done)
326 {
327 Console.WriteLine("Execute called more than once");
328 throw new InvalidOperationException();
329 }
330
331 for (var i = 0; i < numIterations; i++)
332 {
333 try
334 {
335 if (!transport.IsOpen)
Jens Geyer1b770f22019-03-12 01:19:43 +0100336 transport.OpenAsync(MakeTimeoutToken()).GetAwaiter().GetResult();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100337 }
338 catch (TTransportException ex)
339 {
340 Console.WriteLine("*** FAILED ***");
341 Console.WriteLine("Connect failed: " + ex.Message);
342 ReturnCode |= ErrorUnknown;
343 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
344 continue;
345 }
346 catch (Exception ex)
347 {
348 Console.WriteLine("*** FAILED ***");
349 Console.WriteLine("Connect failed: " + ex.Message);
350 ReturnCode |= ErrorUnknown;
351 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
352 continue;
353 }
354
355 try
356 {
357 ReturnCode |= ExecuteClientTestAsync(client).GetAwaiter().GetResult(); ;
358 }
359 catch (Exception ex)
360 {
361 Console.WriteLine("*** FAILED ***");
362 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
363 ReturnCode |= ErrorUnknown;
364 }
365 }
366 try
367 {
368 transport.Close();
369 }
370 catch (Exception ex)
371 {
372 Console.WriteLine("Error while closing transport");
373 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
374 }
375 done = true;
376 }
377 }
378
379 internal static void PrintOptionsHelp()
380 {
381 Console.WriteLine("Client options:");
382 Console.WriteLine(" -u <URL>");
383 Console.WriteLine(" -t <# of threads to run> default = 1");
384 Console.WriteLine(" -n <# of iterations> per thread");
385 Console.WriteLine(" --pipe=<pipe name>");
386 Console.WriteLine(" --host=<IP address>");
387 Console.WriteLine(" --port=<port number>");
388 Console.WriteLine(" --transport=<transport name> one of buffered,framed (defaults to none)");
389 Console.WriteLine(" --protocol=<protocol name> one of compact,json (defaults to binary)");
390 Console.WriteLine(" --ssl");
391 Console.WriteLine();
392 }
393
394 public static int Execute(List<string> args)
395 {
396 try
397 {
398 var param = new TestParams();
399
400 try
401 {
402 param.Parse(args);
403 }
404 catch (Exception ex)
405 {
406 Console.WriteLine("*** FAILED ***");
407 Console.WriteLine("Error while parsing arguments");
408 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
409 return ErrorUnknown;
410 }
411
412 var tests = Enumerable.Range(0, param.numThreads).Select(_ => new ClientTest(param)).ToArray();
413
414 //issue tests on separate threads simultaneously
415 var threads = tests.Select(test => new Task(test.Execute)).ToArray();
416 var start = DateTime.Now;
417 foreach (var t in threads)
418 {
419 t.Start();
420 }
421
422 Task.WaitAll(threads);
423
424 Console.WriteLine("Total time: " + (DateTime.Now - start));
425 Console.WriteLine();
426 return tests.Select(t => t.ReturnCode).Aggregate((r1, r2) => r1 | r2);
427 }
428 catch (Exception outerEx)
429 {
430 Console.WriteLine("*** FAILED ***");
431 Console.WriteLine("Unexpected error");
432 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
433 return ErrorUnknown;
434 }
435 }
436
437 public static string BytesToHex(byte[] data)
438 {
439 return BitConverter.ToString(data).Replace("-", string.Empty);
440 }
441
Jens Geyerbd1a2732019-06-26 22:52:44 +0200442
443 public enum BinaryTestSize
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100444 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200445 Empty, // Edge case: the zero-length empty binary
446 Normal, // Fairly small array of usual size (256 bytes)
447 Large, // Large writes/reads may cause range check errors
448 PipeWriteLimit, // Windows Limit: Pipe write operations across a network are limited to 65,535 bytes per write.
449 TwentyMB // that's quite a bit of data
450 };
451
452 public static byte[] PrepareTestData(bool randomDist, BinaryTestSize testcase)
453 {
Jens Geyer261cad32019-11-20 19:03:14 +0100454 int amount;
Jens Geyerbd1a2732019-06-26 22:52:44 +0200455 switch (testcase)
456 {
457 case BinaryTestSize.Empty:
458 amount = 0;
459 break;
460 case BinaryTestSize.Normal:
461 amount = 0x100;
462 break;
463 case BinaryTestSize.Large:
464 amount = 0x8000 + 128;
465 break;
466 case BinaryTestSize.PipeWriteLimit:
467 amount = 0xFFFF + 128;
468 break;
469 case BinaryTestSize.TwentyMB:
470 amount = 20 * 1024 * 1024;
471 break;
472 default:
473 throw new ArgumentException(nameof(testcase));
474 }
475
476 var retval = new byte[amount];
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100477
478 // linear distribution, unless random is requested
479 if (!randomDist)
480 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200481 for (var i = 0; i < retval.Length; ++i)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100482 {
483 retval[i] = (byte)i;
484 }
485 return retval;
486 }
487
488 // random distribution
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100489 var rnd = new Random();
Jens Geyerbd1a2732019-06-26 22:52:44 +0200490 for (var i = 1; i < retval.Length; ++i)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100491 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200492 retval[i] = (byte)rnd.Next(0x100);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100493 }
494 return retval;
495 }
496
Jens Geyer1b770f22019-03-12 01:19:43 +0100497 private static CancellationToken MakeTimeoutToken(int msec = 5000)
498 {
499 var token = new CancellationTokenSource(msec);
500 return token.Token;
501 }
502
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100503 public static async Task<int> ExecuteClientTestAsync(ThriftTest.Client client)
504 {
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100505 var returnCode = 0;
506
507 Console.Write("testVoid()");
Jens Geyer1b770f22019-03-12 01:19:43 +0100508 await client.testVoidAsync(MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100509 Console.WriteLine(" = void");
510
511 Console.Write("testString(\"Test\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100512 var s = await client.testStringAsync("Test", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100513 Console.WriteLine(" = \"" + s + "\"");
514 if ("Test" != s)
515 {
516 Console.WriteLine("*** FAILED ***");
517 returnCode |= ErrorBaseTypes;
518 }
519
520 Console.Write("testBool(true)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100521 var t = await client.testBoolAsync((bool)true, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100522 Console.WriteLine(" = " + t);
523 if (!t)
524 {
525 Console.WriteLine("*** FAILED ***");
526 returnCode |= ErrorBaseTypes;
527 }
528 Console.Write("testBool(false)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100529 var f = await client.testBoolAsync((bool)false, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100530 Console.WriteLine(" = " + f);
531 if (f)
532 {
533 Console.WriteLine("*** FAILED ***");
534 returnCode |= ErrorBaseTypes;
535 }
536
537 Console.Write("testByte(1)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100538 var i8 = await client.testByteAsync((sbyte)1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100539 Console.WriteLine(" = " + i8);
540 if (1 != i8)
541 {
542 Console.WriteLine("*** FAILED ***");
543 returnCode |= ErrorBaseTypes;
544 }
545
546 Console.Write("testI32(-1)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100547 var i32 = await client.testI32Async(-1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100548 Console.WriteLine(" = " + i32);
549 if (-1 != i32)
550 {
551 Console.WriteLine("*** FAILED ***");
552 returnCode |= ErrorBaseTypes;
553 }
554
555 Console.Write("testI64(-34359738368)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100556 var i64 = await client.testI64Async(-34359738368, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100557 Console.WriteLine(" = " + i64);
558 if (-34359738368 != i64)
559 {
560 Console.WriteLine("*** FAILED ***");
561 returnCode |= ErrorBaseTypes;
562 }
563
564 // TODO: Validate received message
565 Console.Write("testDouble(5.325098235)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100566 var dub = await client.testDoubleAsync(5.325098235, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100567 Console.WriteLine(" = " + dub);
568 if (5.325098235 != dub)
569 {
570 Console.WriteLine("*** FAILED ***");
571 returnCode |= ErrorBaseTypes;
572 }
573 Console.Write("testDouble(-0.000341012439638598279)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100574 dub = await client.testDoubleAsync(-0.000341012439638598279, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100575 Console.WriteLine(" = " + dub);
576 if (-0.000341012439638598279 != dub)
577 {
578 Console.WriteLine("*** FAILED ***");
579 returnCode |= ErrorBaseTypes;
580 }
581
Jens Geyerbd1a2732019-06-26 22:52:44 +0200582 // testBinary()
583 foreach(BinaryTestSize binTestCase in Enum.GetValues(typeof(BinaryTestSize)))
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100584 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200585 var binOut = PrepareTestData(true, binTestCase);
586
587 Console.Write("testBinary({0} bytes)", binOut.Length);
588 try
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100589 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200590 var binIn = await client.testBinaryAsync(binOut, MakeTimeoutToken());
591 Console.WriteLine(" = {0} bytes", binIn.Length);
592 if (binIn.Length != binOut.Length)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100593 {
594 Console.WriteLine("*** FAILED ***");
595 returnCode |= ErrorBaseTypes;
596 }
Jens Geyerbd1a2732019-06-26 22:52:44 +0200597 for (var ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
598 {
599 if (binIn[ofs] != binOut[ofs])
600 {
601 Console.WriteLine("*** FAILED ***");
602 returnCode |= ErrorBaseTypes;
603 }
604 }
605 }
606 catch (Thrift.TApplicationException ex)
607 {
608 Console.WriteLine("*** FAILED ***");
609 returnCode |= ErrorBaseTypes;
610 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
611 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100612 }
613
Jens Geyerbd1a2732019-06-26 22:52:44 +0200614 // CrazyNesting
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100615 Console.WriteLine("Test CrazyNesting");
616 var one = new CrazyNesting();
617 var two = new CrazyNesting();
618 one.String_field = "crazy";
619 two.String_field = "crazy";
620 one.Binary_field = new byte[] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
621 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
622 if (typeof(CrazyNesting).GetMethod("Equals")?.DeclaringType == typeof(CrazyNesting))
623 {
624 if (!one.Equals(two))
625 {
626 Console.WriteLine("*** FAILED ***");
627 returnCode |= ErrorContainers;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100628 }
629 }
630
631 // TODO: Validate received message
632 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
Jens Geyer261cad32019-11-20 19:03:14 +0100633 var o = new Xtruct
634 {
635 String_thing = "Zero",
636 Byte_thing = (sbyte)1,
637 I32_thing = -3,
638 I64_thing = -5
639 };
Jens Geyer1b770f22019-03-12 01:19:43 +0100640 var i = await client.testStructAsync(o, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100641 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
642
643 // TODO: Validate received message
644 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
Jens Geyer261cad32019-11-20 19:03:14 +0100645 var o2 = new Xtruct2
646 {
647 Byte_thing = (sbyte)1,
648 Struct_thing = o,
649 I32_thing = 5
650 };
Jens Geyer1b770f22019-03-12 01:19:43 +0100651 var i2 = await client.testNestAsync(o2, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100652 i = i2.Struct_thing;
653 Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");
654
655 var mapout = new Dictionary<int, int>();
656 for (var j = 0; j < 5; j++)
657 {
658 mapout[j] = j - 10;
659 }
660 Console.Write("testMap({");
661 var first = true;
662 foreach (var key in mapout.Keys)
663 {
664 if (first)
665 {
666 first = false;
667 }
668 else
669 {
670 Console.Write(", ");
671 }
672 Console.Write(key + " => " + mapout[key]);
673 }
674 Console.Write("})");
675
Jens Geyer1b770f22019-03-12 01:19:43 +0100676 var mapin = await client.testMapAsync(mapout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100677
678 Console.Write(" = {");
679 first = true;
680 foreach (var key in mapin.Keys)
681 {
682 if (first)
683 {
684 first = false;
685 }
686 else
687 {
688 Console.Write(", ");
689 }
690 Console.Write(key + " => " + mapin[key]);
691 }
692 Console.WriteLine("}");
693
694 // TODO: Validate received message
695 var listout = new List<int>();
696 for (var j = -2; j < 3; j++)
697 {
698 listout.Add(j);
699 }
700 Console.Write("testList({");
701 first = true;
702 foreach (var j in listout)
703 {
704 if (first)
705 {
706 first = false;
707 }
708 else
709 {
710 Console.Write(", ");
711 }
712 Console.Write(j);
713 }
714 Console.Write("})");
715
Jens Geyer1b770f22019-03-12 01:19:43 +0100716 var listin = await client.testListAsync(listout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100717
718 Console.Write(" = {");
719 first = true;
720 foreach (var j in listin)
721 {
722 if (first)
723 {
724 first = false;
725 }
726 else
727 {
728 Console.Write(", ");
729 }
730 Console.Write(j);
731 }
732 Console.WriteLine("}");
733
734 //set
735 // TODO: Validate received message
736 var setout = new THashSet<int>();
737 for (var j = -2; j < 3; j++)
738 {
739 setout.Add(j);
740 }
741 Console.Write("testSet({");
742 first = true;
743 foreach (int j in setout)
744 {
745 if (first)
746 {
747 first = false;
748 }
749 else
750 {
751 Console.Write(", ");
752 }
753 Console.Write(j);
754 }
755 Console.Write("})");
756
Jens Geyer1b770f22019-03-12 01:19:43 +0100757 var setin = await client.testSetAsync(setout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100758
759 Console.Write(" = {");
760 first = true;
761 foreach (int j in setin)
762 {
763 if (first)
764 {
765 first = false;
766 }
767 else
768 {
769 Console.Write(", ");
770 }
771 Console.Write(j);
772 }
773 Console.WriteLine("}");
774
775
776 Console.Write("testEnum(ONE)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100777 var ret = await client.testEnumAsync(Numberz.ONE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100778 Console.WriteLine(" = " + ret);
779 if (Numberz.ONE != ret)
780 {
781 Console.WriteLine("*** FAILED ***");
782 returnCode |= ErrorStructs;
783 }
784
785 Console.Write("testEnum(TWO)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100786 ret = await client.testEnumAsync(Numberz.TWO, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100787 Console.WriteLine(" = " + ret);
788 if (Numberz.TWO != ret)
789 {
790 Console.WriteLine("*** FAILED ***");
791 returnCode |= ErrorStructs;
792 }
793
794 Console.Write("testEnum(THREE)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100795 ret = await client.testEnumAsync(Numberz.THREE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100796 Console.WriteLine(" = " + ret);
797 if (Numberz.THREE != ret)
798 {
799 Console.WriteLine("*** FAILED ***");
800 returnCode |= ErrorStructs;
801 }
802
803 Console.Write("testEnum(FIVE)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100804 ret = await client.testEnumAsync(Numberz.FIVE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100805 Console.WriteLine(" = " + ret);
806 if (Numberz.FIVE != ret)
807 {
808 Console.WriteLine("*** FAILED ***");
809 returnCode |= ErrorStructs;
810 }
811
812 Console.Write("testEnum(EIGHT)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100813 ret = await client.testEnumAsync(Numberz.EIGHT, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100814 Console.WriteLine(" = " + ret);
815 if (Numberz.EIGHT != ret)
816 {
817 Console.WriteLine("*** FAILED ***");
818 returnCode |= ErrorStructs;
819 }
820
821 Console.Write("testTypedef(309858235082523)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100822 var uid = await client.testTypedefAsync(309858235082523L, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100823 Console.WriteLine(" = " + uid);
824 if (309858235082523L != uid)
825 {
826 Console.WriteLine("*** FAILED ***");
827 returnCode |= ErrorStructs;
828 }
829
830 // TODO: Validate received message
831 Console.Write("testMapMap(1)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100832 var mm = await client.testMapMapAsync(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100833 Console.Write(" = {");
834 foreach (var key in mm.Keys)
835 {
836 Console.Write(key + " => {");
837 var m2 = mm[key];
838 foreach (var k2 in m2.Keys)
839 {
840 Console.Write(k2 + " => " + m2[k2] + ", ");
841 }
842 Console.Write("}, ");
843 }
844 Console.WriteLine("}");
845
846 // TODO: Validate received message
Jens Geyer261cad32019-11-20 19:03:14 +0100847 var insane = new Insanity
848 {
849 UserMap = new Dictionary<Numberz, long>
850 {
851 [Numberz.FIVE] = 5000L
852 }
853 };
854 var truck = new Xtruct
855 {
856 String_thing = "Truck",
857 Byte_thing = (sbyte)8,
858 I32_thing = 8,
859 I64_thing = 8
860 };
861 insane.Xtructs = new List<Xtruct>
862 {
863 truck
864 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100865 Console.Write("testInsanity()");
Jens Geyer1b770f22019-03-12 01:19:43 +0100866 var whoa = await client.testInsanityAsync(insane, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100867 Console.Write(" = {");
868 foreach (var key in whoa.Keys)
869 {
870 var val = whoa[key];
871 Console.Write(key + " => {");
872
873 foreach (var k2 in val.Keys)
874 {
875 var v2 = val[k2];
876
877 Console.Write(k2 + " => {");
878 var userMap = v2.UserMap;
879
880 Console.Write("{");
881 if (userMap != null)
882 {
883 foreach (var k3 in userMap.Keys)
884 {
885 Console.Write(k3 + " => " + userMap[k3] + ", ");
886 }
887 }
888 else
889 {
890 Console.Write("null");
891 }
892 Console.Write("}, ");
893
894 var xtructs = v2.Xtructs;
895
896 Console.Write("{");
897 if (xtructs != null)
898 {
899 foreach (var x in xtructs)
900 {
901 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
902 }
903 }
904 else
905 {
906 Console.Write("null");
907 }
908 Console.Write("}");
909
910 Console.Write("}, ");
911 }
912 Console.Write("}, ");
913 }
914 Console.WriteLine("}");
915
916 sbyte arg0 = 1;
917 var arg1 = 2;
918 var arg2 = long.MaxValue;
Jens Geyer261cad32019-11-20 19:03:14 +0100919 var multiDict = new Dictionary<short, string>
920 {
921 [1] = "one"
922 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100923
924 var tmpMultiDict = new List<string>();
925 foreach (var pair in multiDict)
926 tmpMultiDict.Add(pair.Key +" => "+ pair.Value);
927
928 var arg4 = Numberz.FIVE;
929 long arg5 = 5000000;
930 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + ",{" + string.Join(",", tmpMultiDict) + "}," + arg4 + "," + arg5 + ")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100931 var multiResponse = await client.testMultiAsync(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100932 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
933 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
934
935 try
936 {
937 Console.WriteLine("testException(\"Xception\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100938 await client.testExceptionAsync("Xception", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100939 Console.WriteLine("*** FAILED ***");
940 returnCode |= ErrorExceptions;
941 }
942 catch (Xception ex)
943 {
944 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
945 {
946 Console.WriteLine("*** FAILED ***");
947 returnCode |= ErrorExceptions;
948 }
949 }
950 catch (Exception ex)
951 {
952 Console.WriteLine("*** FAILED ***");
953 returnCode |= ErrorExceptions;
954 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
955 }
956 try
957 {
958 Console.WriteLine("testException(\"TException\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100959 await client.testExceptionAsync("TException", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100960 Console.WriteLine("*** FAILED ***");
961 returnCode |= ErrorExceptions;
962 }
963 catch (Thrift.TException)
964 {
965 // OK
966 }
967 catch (Exception ex)
968 {
969 Console.WriteLine("*** FAILED ***");
970 returnCode |= ErrorExceptions;
971 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
972 }
973 try
974 {
975 Console.WriteLine("testException(\"ok\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100976 await client.testExceptionAsync("ok", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100977 // OK
978 }
979 catch (Exception ex)
980 {
981 Console.WriteLine("*** FAILED ***");
982 returnCode |= ErrorExceptions;
983 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
984 }
985
986 try
987 {
988 Console.WriteLine("testMultiException(\"Xception\", ...)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100989 await client.testMultiExceptionAsync("Xception", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100990 Console.WriteLine("*** FAILED ***");
991 returnCode |= ErrorExceptions;
992 }
993 catch (Xception ex)
994 {
995 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
996 {
997 Console.WriteLine("*** FAILED ***");
998 returnCode |= ErrorExceptions;
999 }
1000 }
1001 catch (Exception ex)
1002 {
1003 Console.WriteLine("*** FAILED ***");
1004 returnCode |= ErrorExceptions;
1005 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
1006 }
1007 try
1008 {
1009 Console.WriteLine("testMultiException(\"Xception2\", ...)");
Jens Geyer1b770f22019-03-12 01:19:43 +01001010 await client.testMultiExceptionAsync("Xception2", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001011 Console.WriteLine("*** FAILED ***");
1012 returnCode |= ErrorExceptions;
1013 }
1014 catch (Xception2 ex)
1015 {
1016 if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2")
1017 {
1018 Console.WriteLine("*** FAILED ***");
1019 returnCode |= ErrorExceptions;
1020 }
1021 }
1022 catch (Exception ex)
1023 {
1024 Console.WriteLine("*** FAILED ***");
1025 returnCode |= ErrorExceptions;
1026 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
1027 }
1028 try
1029 {
1030 Console.WriteLine("testMultiException(\"success\", \"OK\")");
Jens Geyer1b770f22019-03-12 01:19:43 +01001031 if ("OK" != (await client.testMultiExceptionAsync("success", "OK", MakeTimeoutToken())).String_thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001032 {
1033 Console.WriteLine("*** FAILED ***");
1034 returnCode |= ErrorExceptions;
1035 }
1036 }
1037 catch (Exception ex)
1038 {
1039 Console.WriteLine("*** FAILED ***");
1040 returnCode |= ErrorExceptions;
1041 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
1042 }
1043
Jens Geyerb11f63c2019-03-14 21:12:38 +01001044 Console.WriteLine("Test Oneway(1)");
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001045 var sw = new Stopwatch();
1046 sw.Start();
Jens Geyer1b770f22019-03-12 01:19:43 +01001047 await client.testOnewayAsync(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001048 sw.Stop();
1049 if (sw.ElapsedMilliseconds > 1000)
1050 {
1051 Console.WriteLine("*** FAILED ***");
1052 returnCode |= ErrorBaseTypes;
1053 }
1054
1055 Console.Write("Test Calltime()");
1056 var times = 50;
1057 sw.Reset();
1058 sw.Start();
Jens Geyer1b770f22019-03-12 01:19:43 +01001059 var token = MakeTimeoutToken(20000);
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001060 for (var k = 0; k < times; ++k)
1061 await client.testVoidAsync(token);
1062 sw.Stop();
1063 Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times);
1064 return returnCode;
1065 }
1066 }
1067}