blob: 2d18cf1f90c755a28a7e59c9ab3ec2ee13b313ce [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
Jens Geyer273607d2021-07-31 23:25:51 +020018#pragma warning disable IDE0066 // switch expression
19#pragma warning disable IDE0057 // substring
20
Jens Geyeraa0c8b32019-01-28 23:27:45 +010021using System;
22using System.Collections.Generic;
23using System.Diagnostics;
24using System.IO;
25using System.Linq;
26using System.Net;
27using System.Reflection;
28using System.Security.Authentication;
29using System.Security.Cryptography.X509Certificates;
30using System.ServiceModel;
31using System.Text;
32using System.Threading;
33using System.Threading.Tasks;
Jens Geyereacd1d42019-11-20 19:03:14 +010034using Thrift;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010035using Thrift.Collections;
36using Thrift.Protocol;
37using Thrift.Transport;
38using Thrift.Transport.Client;
39
40namespace ThriftTest
41{
Jens Geyeradde44b2019-02-05 01:00:02 +010042 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 Geyeradde44b2019-02-05 01:00:02 +010057 internal enum TransportChoice
58 {
59 Socket,
60 TlsSocket,
Jens Geyer96c61132019-06-14 22:39:56 +020061 Http,
Jens Geyeradde44b2019-02-05 01:00:02 +010062 NamedPipe
63 }
64
Jens Geyeraa0c8b32019-01-28 23:27:45 +010065 public class TestClient
66 {
67 private class TestParams
68 {
69 public int numIterations = 1;
Jens Geyer22c412e2019-03-12 01:06:25 +010070 public string host = "localhost";
Jens Geyeraa0c8b32019-01-28 23:27:45 +010071 public int port = 9090;
72 public int numThreads = 1;
73 public string url;
74 public string pipe;
Jens Geyeradde44b2019-02-05 01:00:02 +010075 public LayeredChoice layered = LayeredChoice.None;
76 public ProtocolChoice protocol = ProtocolChoice.Binary;
77 public TransportChoice transport = TransportChoice.Socket;
Jens Geyereacd1d42019-11-20 19:03:14 +010078 private readonly TConfiguration Configuration = null; // or new TConfiguration() if needed
Jens Geyeraa0c8b32019-01-28 23:27:45 +010079
Jens Geyerbd1a2732019-06-26 22:52:44 +020080 internal void Parse(List<string> args)
Jens Geyeraa0c8b32019-01-28 23:27:45 +010081 {
82 for (var i = 0; i < args.Count; ++i)
83 {
84 if (args[i] == "-u")
85 {
86 url = args[++i];
Jens Geyer96c61132019-06-14 22:39:56 +020087 transport = TransportChoice.Http;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010088 }
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 Geyeradde44b2019-02-05 01:00:02 +010096 transport = TransportChoice.NamedPipe;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010097 }
98 else if (args[i].StartsWith("--host="))
99 {
100 // check there for ipaddress
Jens Geyer22c412e2019-03-12 01:06:25 +0100101 host = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +0100102 if (transport != TransportChoice.TlsSocket)
103 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100104 }
105 else if (args[i].StartsWith("--port="))
106 {
107 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyeradde44b2019-02-05 01:00:02 +0100108 if (transport != TransportChoice.TlsSocket)
109 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100110 }
111 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
112 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100113 layered = LayeredChoice.Buffered;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100114 }
115 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
116 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100117 layered = LayeredChoice.Framed;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100118 }
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 Geyeradde44b2019-02-05 01:00:02 +0100125 protocol = ProtocolChoice.Binary;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100126 }
127 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
128 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100129 protocol = ProtocolChoice.Compact;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100130 }
131 else if (args[i] == "--json" || args[i] == "--protocol=json")
132 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100133 protocol = ProtocolChoice.Json;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100134 }
135 else if (args[i] == "--ssl")
136 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100137 transport = TransportChoice.TlsSocket;
138 }
139 else if (args[i] == "--help")
140 {
141 PrintOptionsHelp();
142 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100143 }
144 else
145 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100146 Console.WriteLine("Invalid argument: {0}", args[i]);
147 PrintOptionsHelp();
148 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100149 }
150 }
Jens Geyeradde44b2019-02-05 01:00:02 +0100151
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 Geyer96c61132019-06-14 22:39:56 +0200160 case TransportChoice.Http:
161 Console.WriteLine("Using HTTP transport");
162 break;
Jens Geyeradde44b2019-02-05 01:00:02 +0100163 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 Geyeraa0c8b32019-01-28 23:27:45 +0100199 }
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 Geyerbd1a2732019-06-26 22:52:44 +0200227
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100228 var cert = new X509Certificate2(existingPath, "thrift");
229
230 return cert;
231 }
Jens Geyerbd1a2732019-06-26 22:52:44 +0200232
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100233 public TTransport CreateTransport()
234 {
Jens Geyer96c61132019-06-14 22:39:56 +0200235 // endpoint transport
236 TTransport trans = null;
237
Jens Geyerbd1a2732019-06-26 22:52:44 +0200238 switch (transport)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100239 {
Jens Geyer96c61132019-06-14 22:39:56 +0200240 case TransportChoice.Http:
241 Debug.Assert(url != null);
Jens Geyereacd1d42019-11-20 19:03:14 +0100242 trans = new THttpTransport(new Uri(url), Configuration);
Jens Geyer96c61132019-06-14 22:39:56 +0200243 break;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100244
Jens Geyer96c61132019-06-14 22:39:56 +0200245 case TransportChoice.NamedPipe:
246 Debug.Assert(pipe != null);
Jens Geyereacd1d42019-11-20 19:03:14 +0100247 trans = new TNamedPipeTransport(pipe,Configuration);
Jens Geyer96c61132019-06-14 22:39:56 +0200248 break;
Jens Geyeradde44b2019-02-05 01:00:02 +0100249
Jens Geyer96c61132019-06-14 22:39:56 +0200250 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 Geyerbd1a2732019-06-26 22:52:44 +0200256
Jens Geyereacd1d42019-11-20 19:03:14 +0100257 trans = new TTlsSocketTransport(host, port, Configuration, 0,
258 cert,
Jens Geyer96c61132019-06-14 22:39:56 +0200259 (sender, certificate, chain, errors) => true,
260 null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
261 break;
Jens Geyeradde44b2019-02-05 01:00:02 +0100262
Jens Geyer96c61132019-06-14 22:39:56 +0200263 case TransportChoice.Socket:
264 default:
Jens Geyereacd1d42019-11-20 19:03:14 +0100265 trans = new TSocketTransport(host, port, Configuration);
Jens Geyer96c61132019-06-14 22:39:56 +0200266 break;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100267 }
268
Jens Geyer96c61132019-06-14 22:39:56 +0200269
270 // layered transport
Jens Geyerbd1a2732019-06-26 22:52:44 +0200271 switch (layered)
Jens Geyer96c61132019-06-14 22:39:56 +0200272 {
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 Geyeraa0c8b32019-01-28 23:27:45 +0100285 }
286
287 public TProtocol CreateProtocol(TTransport transport)
288 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100289 switch (protocol)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100290 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100291 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 Geyeraa0c8b32019-01-28 23:27:45 +0100298 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100299 }
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 Geyeraa0c8b32019-01-28 23:27:45 +0100327 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 Geyer1b770f22019-03-12 01:19:43 +0100338 transport.OpenAsync(MakeTimeoutToken()).GetAwaiter().GetResult();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100339 }
340 catch (TTransportException ex)
341 {
342 Console.WriteLine("*** FAILED ***");
343 Console.WriteLine("Connect failed: " + ex.Message);
344 ReturnCode |= ErrorUnknown;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200345 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100346 continue;
347 }
348 catch (Exception ex)
349 {
350 Console.WriteLine("*** FAILED ***");
351 Console.WriteLine("Connect failed: " + ex.Message);
352 ReturnCode |= ErrorUnknown;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200353 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100354 continue;
355 }
356
357 try
358 {
Jens Geyer2b2ea622021-04-09 22:55:11 +0200359 ReturnCode |= ExecuteClientTest(client).GetAwaiter().GetResult(); ;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100360 }
361 catch (Exception ex)
362 {
363 Console.WriteLine("*** FAILED ***");
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200364 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100365 ReturnCode |= ErrorUnknown;
366 }
367 }
368 try
369 {
370 transport.Close();
371 }
372 catch (Exception ex)
373 {
374 Console.WriteLine("Error while closing transport");
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200375 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100376 }
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 Emmenlauer47e49232020-04-07 18:43:46 +0200409 Console.WriteLine("Error while parsing arguments");
410 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100411 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 Emmenlauer47e49232020-04-07 18:43:46 +0200434 Console.WriteLine(outerEx.Message + "\n" + outerEx.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100435 return ErrorUnknown;
436 }
437 }
438
439 public static string BytesToHex(byte[] data)
440 {
441 return BitConverter.ToString(data).Replace("-", string.Empty);
442 }
443
Jens Geyerbd1a2732019-06-26 22:52:44 +0200444
445 public enum BinaryTestSize
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100446 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200447 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 Geyer50806452019-11-23 01:55:58 +0100451 FifteenMB // that's quite a bit of data
Jens Geyerbd1a2732019-06-26 22:52:44 +0200452 };
453
454 public static byte[] PrepareTestData(bool randomDist, BinaryTestSize testcase)
455 {
Jens Geyer261cad32019-11-20 19:03:14 +0100456 int amount;
Jens Geyerbd1a2732019-06-26 22:52:44 +0200457 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 Geyer50806452019-11-23 01:55:58 +0100471 case BinaryTestSize.FifteenMB:
472 amount = 15 * 1024 * 1024;
Jens Geyerbd1a2732019-06-26 22:52:44 +0200473 break;
474 default:
Jens Geyer2b2ea622021-04-09 22:55:11 +0200475 throw new ArgumentException("invalid argument",nameof(testcase));
Jens Geyerbd1a2732019-06-26 22:52:44 +0200476 }
477
478 var retval = new byte[amount];
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100479
480 // linear distribution, unless random is requested
481 if (!randomDist)
482 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200483 for (var i = 0; i < retval.Length; ++i)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100484 {
485 retval[i] = (byte)i;
486 }
487 return retval;
488 }
489
490 // random distribution
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100491 var rnd = new Random();
Jens Geyerbd1a2732019-06-26 22:52:44 +0200492 for (var i = 1; i < retval.Length; ++i)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100493 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200494 retval[i] = (byte)rnd.Next(0x100);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100495 }
496 return retval;
497 }
498
Jens Geyer1b770f22019-03-12 01:19:43 +0100499 private static CancellationToken MakeTimeoutToken(int msec = 5000)
500 {
501 var token = new CancellationTokenSource(msec);
502 return token.Token;
503 }
504
Jens Geyer2b2ea622021-04-09 22:55:11 +0200505 public static async Task<int> ExecuteClientTest(ThriftTest.Client client)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100506 {
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100507 var returnCode = 0;
508
509 Console.Write("testVoid()");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200510 await client.testVoid(MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100511 Console.WriteLine(" = void");
512
513 Console.Write("testString(\"Test\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200514 var s = await client.testString("Test", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100515 Console.WriteLine(" = \"" + s + "\"");
516 if ("Test" != s)
517 {
518 Console.WriteLine("*** FAILED ***");
519 returnCode |= ErrorBaseTypes;
520 }
521
522 Console.Write("testBool(true)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200523 var t = await client.testBool((bool)true, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100524 Console.WriteLine(" = " + t);
525 if (!t)
526 {
527 Console.WriteLine("*** FAILED ***");
528 returnCode |= ErrorBaseTypes;
529 }
530 Console.Write("testBool(false)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200531 var f = await client.testBool((bool)false, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100532 Console.WriteLine(" = " + f);
533 if (f)
534 {
535 Console.WriteLine("*** FAILED ***");
536 returnCode |= ErrorBaseTypes;
537 }
538
539 Console.Write("testByte(1)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200540 var i8 = await client.testByte((sbyte)1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100541 Console.WriteLine(" = " + i8);
542 if (1 != i8)
543 {
544 Console.WriteLine("*** FAILED ***");
545 returnCode |= ErrorBaseTypes;
546 }
547
548 Console.Write("testI32(-1)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200549 var i32 = await client.testI32(-1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100550 Console.WriteLine(" = " + i32);
551 if (-1 != i32)
552 {
553 Console.WriteLine("*** FAILED ***");
554 returnCode |= ErrorBaseTypes;
555 }
556
557 Console.Write("testI64(-34359738368)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200558 var i64 = await client.testI64(-34359738368, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100559 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 Geyer2b2ea622021-04-09 22:55:11 +0200568 var dub = await client.testDouble(5.325098235, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100569 Console.WriteLine(" = " + dub);
570 if (5.325098235 != dub)
571 {
572 Console.WriteLine("*** FAILED ***");
573 returnCode |= ErrorBaseTypes;
574 }
575 Console.Write("testDouble(-0.000341012439638598279)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200576 dub = await client.testDouble(-0.000341012439638598279, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100577 Console.WriteLine(" = " + dub);
578 if (-0.000341012439638598279 != dub)
579 {
580 Console.WriteLine("*** FAILED ***");
581 returnCode |= ErrorBaseTypes;
582 }
583
Jens Geyerbd1a2732019-06-26 22:52:44 +0200584 // testBinary()
585 foreach(BinaryTestSize binTestCase in Enum.GetValues(typeof(BinaryTestSize)))
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100586 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200587 var binOut = PrepareTestData(true, binTestCase);
588
589 Console.Write("testBinary({0} bytes)", binOut.Length);
590 try
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100591 {
Jens Geyer2b2ea622021-04-09 22:55:11 +0200592 var binIn = await client.testBinary(binOut, MakeTimeoutToken());
Jens Geyerbd1a2732019-06-26 22:52:44 +0200593 Console.WriteLine(" = {0} bytes", binIn.Length);
594 if (binIn.Length != binOut.Length)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100595 {
596 Console.WriteLine("*** FAILED ***");
597 returnCode |= ErrorBaseTypes;
598 }
Jens Geyerbd1a2732019-06-26 22:52:44 +0200599 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 Emmenlauer47e49232020-04-07 18:43:46 +0200612 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200613 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100614 }
615
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200616 // CrazyNesting
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100617 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 Geyeraa0c8b32019-01-28 23:27:45 +0100630 }
631 }
632
633 // TODO: Validate received message
634 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
Jens Geyer261cad32019-11-20 19:03:14 +0100635 var o = new Xtruct
636 {
637 String_thing = "Zero",
638 Byte_thing = (sbyte)1,
639 I32_thing = -3,
640 I64_thing = -5
641 };
Jens Geyer2b2ea622021-04-09 22:55:11 +0200642 var i = await client.testStruct(o, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100643 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 Geyer261cad32019-11-20 19:03:14 +0100647 var o2 = new Xtruct2
648 {
649 Byte_thing = (sbyte)1,
650 Struct_thing = o,
651 I32_thing = 5
652 };
Jens Geyer2b2ea622021-04-09 22:55:11 +0200653 var i2 = await client.testNest(o2, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100654 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 Geyer2b2ea622021-04-09 22:55:11 +0200678 var mapin = await client.testMap(mapout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100679
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 Geyer2b2ea622021-04-09 22:55:11 +0200718 var listin = await client.testList(listout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100719
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 Geyer2b2ea622021-04-09 22:55:11 +0200759 var setin = await client.testSet(setout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100760
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 Geyer2b2ea622021-04-09 22:55:11 +0200779 var ret = await client.testEnum(Numberz.ONE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100780 Console.WriteLine(" = " + ret);
781 if (Numberz.ONE != ret)
782 {
783 Console.WriteLine("*** FAILED ***");
784 returnCode |= ErrorStructs;
785 }
786
787 Console.Write("testEnum(TWO)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200788 ret = await client.testEnum(Numberz.TWO, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100789 Console.WriteLine(" = " + ret);
790 if (Numberz.TWO != ret)
791 {
792 Console.WriteLine("*** FAILED ***");
793 returnCode |= ErrorStructs;
794 }
795
796 Console.Write("testEnum(THREE)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200797 ret = await client.testEnum(Numberz.THREE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100798 Console.WriteLine(" = " + ret);
799 if (Numberz.THREE != ret)
800 {
801 Console.WriteLine("*** FAILED ***");
802 returnCode |= ErrorStructs;
803 }
804
805 Console.Write("testEnum(FIVE)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200806 ret = await client.testEnum(Numberz.FIVE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100807 Console.WriteLine(" = " + ret);
808 if (Numberz.FIVE != ret)
809 {
810 Console.WriteLine("*** FAILED ***");
811 returnCode |= ErrorStructs;
812 }
813
814 Console.Write("testEnum(EIGHT)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200815 ret = await client.testEnum(Numberz.EIGHT, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100816 Console.WriteLine(" = " + ret);
817 if (Numberz.EIGHT != ret)
818 {
819 Console.WriteLine("*** FAILED ***");
820 returnCode |= ErrorStructs;
821 }
822
823 Console.Write("testTypedef(309858235082523)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200824 var uid = await client.testTypedef(309858235082523L, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100825 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 Geyer2b2ea622021-04-09 22:55:11 +0200834 var mm = await client.testMapMap(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100835 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 Geyer261cad32019-11-20 19:03:14 +0100849 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 Geyeraa0c8b32019-01-28 23:27:45 +0100867 Console.Write("testInsanity()");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200868 var whoa = await client.testInsanity(insane, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100869 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 Geyer261cad32019-11-20 19:03:14 +0100921 var multiDict = new Dictionary<short, string>
922 {
923 [1] = "one"
924 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100925
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 Geyer2b2ea622021-04-09 22:55:11 +0200933 var multiResponse = await client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100934 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 Geyer2b2ea622021-04-09 22:55:11 +0200940 await client.testException("Xception", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100941 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 Emmenlauer47e49232020-04-07 18:43:46 +0200956 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100957 }
958 try
959 {
960 Console.WriteLine("testException(\"TException\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200961 await client.testException("TException", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100962 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 Emmenlauer47e49232020-04-07 18:43:46 +0200973 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100974 }
975 try
976 {
977 Console.WriteLine("testException(\"ok\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200978 await client.testException("ok", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100979 // OK
980 }
981 catch (Exception ex)
982 {
983 Console.WriteLine("*** FAILED ***");
984 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200985 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100986 }
987
988 try
989 {
990 Console.WriteLine("testMultiException(\"Xception\", ...)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200991 await client.testMultiException("Xception", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100992 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 Emmenlauer47e49232020-04-07 18:43:46 +02001007 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001008 }
1009 try
1010 {
1011 Console.WriteLine("testMultiException(\"Xception2\", ...)");
Jens Geyer2b2ea622021-04-09 22:55:11 +02001012 await client.testMultiException("Xception2", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001013 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 Emmenlauer47e49232020-04-07 18:43:46 +02001028 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001029 }
1030 try
1031 {
1032 Console.WriteLine("testMultiException(\"success\", \"OK\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +02001033 if ("OK" != (await client.testMultiException("success", "OK", MakeTimeoutToken())).String_thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001034 {
1035 Console.WriteLine("*** FAILED ***");
1036 returnCode |= ErrorExceptions;
1037 }
1038 }
1039 catch (Exception ex)
1040 {
1041 Console.WriteLine("*** FAILED ***");
1042 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +02001043 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001044 }
1045
Jens Geyerb11f63c2019-03-14 21:12:38 +01001046 Console.WriteLine("Test Oneway(1)");
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001047 var sw = new Stopwatch();
1048 sw.Start();
Jens Geyer2b2ea622021-04-09 22:55:11 +02001049 await client.testOneway(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001050 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 Geyer1b770f22019-03-12 01:19:43 +01001061 var token = MakeTimeoutToken(20000);
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001062 for (var k = 0; k < times; ++k)
Jens Geyer2b2ea622021-04-09 22:55:11 +02001063 await client.testVoid(token);
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001064 sw.Stop();
1065 Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times);
1066 return returnCode;
1067 }
1068 }
1069}