blob: 29c0d2ef39c6ce32eb3093e38d462c0f7aea1a96 [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
Jens Geyer71569402021-11-13 23:51:16 +010020#pragma warning disable CS1998 // no await in async method
Jens Geyer273607d2021-07-31 23:25:51 +020021
Jens Geyeraa0c8b32019-01-28 23:27:45 +010022using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.IO;
26using System.Linq;
27using System.Net;
28using System.Reflection;
29using System.Security.Authentication;
30using System.Security.Cryptography.X509Certificates;
31using System.ServiceModel;
32using System.Text;
33using System.Threading;
34using System.Threading.Tasks;
Jens Geyereacd1d42019-11-20 19:03:14 +010035using Thrift;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010036using Thrift.Collections;
37using Thrift.Protocol;
38using Thrift.Transport;
39using Thrift.Transport.Client;
40
41namespace ThriftTest
42{
Jens Geyeradde44b2019-02-05 01:00:02 +010043 internal enum ProtocolChoice
44 {
45 Binary,
46 Compact,
47 Json
48 }
49
50 // it does not make much sense to use buffered when we already use framed
51 internal enum LayeredChoice
52 {
53 None,
54 Buffered,
55 Framed
56 }
57
Jens Geyeradde44b2019-02-05 01:00:02 +010058 internal enum TransportChoice
59 {
60 Socket,
61 TlsSocket,
Jens Geyer96c61132019-06-14 22:39:56 +020062 Http,
Jens Geyeradde44b2019-02-05 01:00:02 +010063 NamedPipe
64 }
65
Jens Geyeraa0c8b32019-01-28 23:27:45 +010066 public class TestClient
67 {
68 private class TestParams
69 {
70 public int numIterations = 1;
Jens Geyer22c412e2019-03-12 01:06:25 +010071 public string host = "localhost";
Jens Geyeraa0c8b32019-01-28 23:27:45 +010072 public int port = 9090;
73 public int numThreads = 1;
Jens Geyer98a23252022-01-09 16:50:52 +010074 public string url = string.Empty;
75 public string pipe = string.Empty;
Jens Geyeradde44b2019-02-05 01:00:02 +010076 public LayeredChoice layered = LayeredChoice.None;
77 public ProtocolChoice protocol = ProtocolChoice.Binary;
78 public TransportChoice transport = TransportChoice.Socket;
Jens Geyer98a23252022-01-09 16:50:52 +010079 private readonly TConfiguration Configuration = new();
Jens Geyeraa0c8b32019-01-28 23:27:45 +010080
Jens Geyerbd1a2732019-06-26 22:52:44 +020081 internal void Parse(List<string> args)
Jens Geyeraa0c8b32019-01-28 23:27:45 +010082 {
83 for (var i = 0; i < args.Count; ++i)
84 {
85 if (args[i] == "-u")
86 {
87 url = args[++i];
Jens Geyer96c61132019-06-14 22:39:56 +020088 transport = TransportChoice.Http;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010089 }
90 else if (args[i] == "-n")
91 {
92 numIterations = Convert.ToInt32(args[++i]);
93 }
94 else if (args[i].StartsWith("--pipe="))
95 {
96 pipe = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +010097 transport = TransportChoice.NamedPipe;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010098 }
99 else if (args[i].StartsWith("--host="))
100 {
101 // check there for ipaddress
Jens Geyer22c412e2019-03-12 01:06:25 +0100102 host = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +0100103 if (transport != TransportChoice.TlsSocket)
104 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100105 }
106 else if (args[i].StartsWith("--port="))
107 {
108 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyeradde44b2019-02-05 01:00:02 +0100109 if (transport != TransportChoice.TlsSocket)
110 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100111 }
112 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
113 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100114 layered = LayeredChoice.Buffered;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100115 }
116 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
117 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100118 layered = LayeredChoice.Framed;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100119 }
120 else if (args[i] == "-t")
121 {
122 numThreads = Convert.ToInt32(args[++i]);
123 }
124 else if (args[i] == "--binary" || args[i] == "--protocol=binary")
125 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100126 protocol = ProtocolChoice.Binary;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100127 }
128 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
129 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100130 protocol = ProtocolChoice.Compact;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100131 }
132 else if (args[i] == "--json" || args[i] == "--protocol=json")
133 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100134 protocol = ProtocolChoice.Json;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100135 }
136 else if (args[i] == "--ssl")
137 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100138 transport = TransportChoice.TlsSocket;
139 }
140 else if (args[i] == "--help")
141 {
142 PrintOptionsHelp();
143 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100144 }
145 else
146 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100147 Console.WriteLine("Invalid argument: {0}", args[i]);
148 PrintOptionsHelp();
149 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100150 }
151 }
Jens Geyeradde44b2019-02-05 01:00:02 +0100152
153 switch (transport)
154 {
155 case TransportChoice.Socket:
156 Console.WriteLine("Using socket transport");
157 break;
158 case TransportChoice.TlsSocket:
159 Console.WriteLine("Using encrypted transport");
160 break;
Jens Geyer96c61132019-06-14 22:39:56 +0200161 case TransportChoice.Http:
162 Console.WriteLine("Using HTTP transport");
163 break;
Jens Geyeradde44b2019-02-05 01:00:02 +0100164 case TransportChoice.NamedPipe:
165 Console.WriteLine("Using named pipes transport");
166 break;
167 default: // unhandled case
168 Debug.Assert(false);
169 break;
170 }
171
172 switch (layered)
173 {
174 case LayeredChoice.Framed:
175 Console.WriteLine("Using framed transport");
176 break;
177 case LayeredChoice.Buffered:
178 Console.WriteLine("Using buffered transport");
179 break;
180 default: // unhandled case?
181 Debug.Assert(layered == LayeredChoice.None);
182 break;
183 }
184
185 switch (protocol)
186 {
187 case ProtocolChoice.Binary:
188 Console.WriteLine("Using binary protocol");
189 break;
190 case ProtocolChoice.Compact:
191 Console.WriteLine("Using compact protocol");
192 break;
193 case ProtocolChoice.Json:
194 Console.WriteLine("Using JSON protocol");
195 break;
196 default: // unhandled case?
197 Debug.Assert(false);
198 break;
199 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100200 }
201
202 private static X509Certificate2 GetClientCert()
203 {
204 var clientCertName = "client.p12";
205 var possiblePaths = new List<string>
206 {
207 "../../../keys/",
208 "../../keys/",
209 "../keys/",
210 "keys/",
211 };
212
Jens Geyer98a23252022-01-09 16:50:52 +0100213 var existingPath = string.Empty;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100214 foreach (var possiblePath in possiblePaths)
215 {
216 var path = Path.GetFullPath(possiblePath + clientCertName);
217 if (File.Exists(path))
218 {
219 existingPath = path;
220 break;
221 }
222 }
223
224 if (string.IsNullOrEmpty(existingPath))
225 {
226 throw new FileNotFoundException($"Cannot find file: {clientCertName}");
227 }
Jens Geyerbd1a2732019-06-26 22:52:44 +0200228
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100229 var cert = new X509Certificate2(existingPath, "thrift");
230
231 return cert;
232 }
Jens Geyerbd1a2732019-06-26 22:52:44 +0200233
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100234 public TTransport CreateTransport()
235 {
Jens Geyer96c61132019-06-14 22:39:56 +0200236 // endpoint transport
Jens Geyer98a23252022-01-09 16:50:52 +0100237 TTransport trans;
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
Jens Geyer71569402021-11-13 23:51:16 +0100325 public async Task Execute()
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100326 {
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 Geyer71569402021-11-13 23:51:16 +0100338 await transport.OpenAsync(MakeTimeoutToken());
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 Geyer71569402021-11-13 23:51:16 +0100359 ReturnCode |= await ExecuteClientTest(client);
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
Jens Geyer71569402021-11-13 23:51:16 +0100396 public static async Task<int> Execute(List<string> args)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100397 {
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
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100414 //issue tests on separate threads simultaneously
Jens Geyerd2c28b32022-02-10 23:03:02 +0100415 var nThreads = Math.Max(param.numThreads, 1);
416 Console.Write("Starting {0} test thread(s) ", nThreads);
417 var tasks = new Task[nThreads];
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100418 var start = DateTime.Now;
Jens Geyerd2c28b32022-02-10 23:03:02 +0100419 var retcode = 0;
420 for (var i = 0; i < tasks.Length; ++i)
421 {
422 Console.Write('.');
423 tasks[i] = Task.Run(async () =>
424 {
425 var test = new ClientTest(param);
426 await test.Execute();
427 lock (tasks)
428 retcode |= test.ReturnCode;
429 });
430 }
431 Console.WriteLine(" OK");
Jens Geyer71569402021-11-13 23:51:16 +0100432 Task.WaitAll(tasks);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100433 Console.WriteLine("Total time: " + (DateTime.Now - start));
434 Console.WriteLine();
Jens Geyerd2c28b32022-02-10 23:03:02 +0100435 return retcode;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100436 }
437 catch (Exception outerEx)
438 {
439 Console.WriteLine("*** FAILED ***");
440 Console.WriteLine("Unexpected error");
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200441 Console.WriteLine(outerEx.Message + "\n" + outerEx.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100442 return ErrorUnknown;
443 }
444 }
445
446 public static string BytesToHex(byte[] data)
447 {
448 return BitConverter.ToString(data).Replace("-", string.Empty);
449 }
450
Jens Geyerbd1a2732019-06-26 22:52:44 +0200451
452 public enum BinaryTestSize
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100453 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200454 Empty, // Edge case: the zero-length empty binary
455 Normal, // Fairly small array of usual size (256 bytes)
456 Large, // Large writes/reads may cause range check errors
457 PipeWriteLimit, // Windows Limit: Pipe write operations across a network are limited to 65,535 bytes per write.
Jens Geyer50806452019-11-23 01:55:58 +0100458 FifteenMB // that's quite a bit of data
Jens Geyerbd1a2732019-06-26 22:52:44 +0200459 };
460
461 public static byte[] PrepareTestData(bool randomDist, BinaryTestSize testcase)
462 {
Jens Geyer261cad32019-11-20 19:03:14 +0100463 int amount;
Jens Geyerbd1a2732019-06-26 22:52:44 +0200464 switch (testcase)
465 {
466 case BinaryTestSize.Empty:
467 amount = 0;
468 break;
469 case BinaryTestSize.Normal:
470 amount = 0x100;
471 break;
472 case BinaryTestSize.Large:
473 amount = 0x8000 + 128;
474 break;
475 case BinaryTestSize.PipeWriteLimit:
476 amount = 0xFFFF + 128;
477 break;
Jens Geyer50806452019-11-23 01:55:58 +0100478 case BinaryTestSize.FifteenMB:
479 amount = 15 * 1024 * 1024;
Jens Geyerbd1a2732019-06-26 22:52:44 +0200480 break;
481 default:
Jens Geyer2b2ea622021-04-09 22:55:11 +0200482 throw new ArgumentException("invalid argument",nameof(testcase));
Jens Geyerbd1a2732019-06-26 22:52:44 +0200483 }
484
485 var retval = new byte[amount];
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100486
487 // linear distribution, unless random is requested
488 if (!randomDist)
489 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200490 for (var i = 0; i < retval.Length; ++i)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100491 {
492 retval[i] = (byte)i;
493 }
494 return retval;
495 }
496
497 // random distribution
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100498 var rnd = new Random();
Jens Geyerbd1a2732019-06-26 22:52:44 +0200499 for (var i = 1; i < retval.Length; ++i)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100500 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200501 retval[i] = (byte)rnd.Next(0x100);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100502 }
503 return retval;
504 }
505
Jens Geyerd2c28b32022-02-10 23:03:02 +0100506 private static CancellationToken MakeTimeoutToken(int msec = 15_000)
Jens Geyer1b770f22019-03-12 01:19:43 +0100507 {
508 var token = new CancellationTokenSource(msec);
509 return token.Token;
510 }
511
Jens Geyer2b2ea622021-04-09 22:55:11 +0200512 public static async Task<int> ExecuteClientTest(ThriftTest.Client client)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100513 {
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100514 var returnCode = 0;
515
516 Console.Write("testVoid()");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200517 await client.testVoid(MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100518 Console.WriteLine(" = void");
519
520 Console.Write("testString(\"Test\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200521 var s = await client.testString("Test", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100522 Console.WriteLine(" = \"" + s + "\"");
523 if ("Test" != s)
524 {
525 Console.WriteLine("*** FAILED ***");
526 returnCode |= ErrorBaseTypes;
527 }
528
529 Console.Write("testBool(true)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200530 var t = await client.testBool((bool)true, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100531 Console.WriteLine(" = " + t);
532 if (!t)
533 {
534 Console.WriteLine("*** FAILED ***");
535 returnCode |= ErrorBaseTypes;
536 }
537 Console.Write("testBool(false)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200538 var f = await client.testBool((bool)false, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100539 Console.WriteLine(" = " + f);
540 if (f)
541 {
542 Console.WriteLine("*** FAILED ***");
543 returnCode |= ErrorBaseTypes;
544 }
545
546 Console.Write("testByte(1)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200547 var i8 = await client.testByte((sbyte)1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100548 Console.WriteLine(" = " + i8);
549 if (1 != i8)
550 {
551 Console.WriteLine("*** FAILED ***");
552 returnCode |= ErrorBaseTypes;
553 }
554
555 Console.Write("testI32(-1)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200556 var i32 = await client.testI32(-1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100557 Console.WriteLine(" = " + i32);
558 if (-1 != i32)
559 {
560 Console.WriteLine("*** FAILED ***");
561 returnCode |= ErrorBaseTypes;
562 }
563
564 Console.Write("testI64(-34359738368)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200565 var i64 = await client.testI64(-34359738368, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100566 Console.WriteLine(" = " + i64);
567 if (-34359738368 != i64)
568 {
569 Console.WriteLine("*** FAILED ***");
570 returnCode |= ErrorBaseTypes;
571 }
572
573 // TODO: Validate received message
574 Console.Write("testDouble(5.325098235)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200575 var dub = await client.testDouble(5.325098235, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100576 Console.WriteLine(" = " + dub);
577 if (5.325098235 != dub)
578 {
579 Console.WriteLine("*** FAILED ***");
580 returnCode |= ErrorBaseTypes;
581 }
582 Console.Write("testDouble(-0.000341012439638598279)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200583 dub = await client.testDouble(-0.000341012439638598279, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100584 Console.WriteLine(" = " + dub);
585 if (-0.000341012439638598279 != dub)
586 {
587 Console.WriteLine("*** FAILED ***");
588 returnCode |= ErrorBaseTypes;
589 }
590
Jens Geyerbd1a2732019-06-26 22:52:44 +0200591 // testBinary()
592 foreach(BinaryTestSize binTestCase in Enum.GetValues(typeof(BinaryTestSize)))
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100593 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200594 var binOut = PrepareTestData(true, binTestCase);
595
596 Console.Write("testBinary({0} bytes)", binOut.Length);
597 try
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100598 {
Jens Geyer2b2ea622021-04-09 22:55:11 +0200599 var binIn = await client.testBinary(binOut, MakeTimeoutToken());
Jens Geyerbd1a2732019-06-26 22:52:44 +0200600 Console.WriteLine(" = {0} bytes", binIn.Length);
601 if (binIn.Length != binOut.Length)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100602 {
603 Console.WriteLine("*** FAILED ***");
604 returnCode |= ErrorBaseTypes;
605 }
Jens Geyerbd1a2732019-06-26 22:52:44 +0200606 for (var ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
607 {
608 if (binIn[ofs] != binOut[ofs])
609 {
610 Console.WriteLine("*** FAILED ***");
611 returnCode |= ErrorBaseTypes;
612 }
613 }
614 }
615 catch (Thrift.TApplicationException ex)
616 {
617 Console.WriteLine("*** FAILED ***");
618 returnCode |= ErrorBaseTypes;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200619 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200620 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100621 }
622
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200623 // CrazyNesting
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100624 Console.WriteLine("Test CrazyNesting");
625 var one = new CrazyNesting();
626 var two = new CrazyNesting();
627 one.String_field = "crazy";
628 two.String_field = "crazy";
629 one.Binary_field = new byte[] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
630 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
631 if (typeof(CrazyNesting).GetMethod("Equals")?.DeclaringType == typeof(CrazyNesting))
632 {
633 if (!one.Equals(two))
634 {
635 Console.WriteLine("*** FAILED ***");
636 returnCode |= ErrorContainers;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100637 }
638 }
639
640 // TODO: Validate received message
641 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
Jens Geyer261cad32019-11-20 19:03:14 +0100642 var o = new Xtruct
643 {
644 String_thing = "Zero",
645 Byte_thing = (sbyte)1,
646 I32_thing = -3,
647 I64_thing = -5
648 };
Jens Geyer2b2ea622021-04-09 22:55:11 +0200649 var i = await client.testStruct(o, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100650 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
651
652 // TODO: Validate received message
653 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
Jens Geyer261cad32019-11-20 19:03:14 +0100654 var o2 = new Xtruct2
655 {
656 Byte_thing = (sbyte)1,
657 Struct_thing = o,
658 I32_thing = 5
659 };
Jens Geyer3cac3202022-01-31 18:04:35 +0100660 Xtruct2 i2 = await client.testNest(o2, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100661 i = i2.Struct_thing;
Jens Geyer3cac3202022-01-31 18:04:35 +0100662 Console.WriteLine(" = {" + i2.Byte_thing + ", {\""
663 + (i?.String_thing ?? "<null>") + "\", "
664 + (i?.Byte_thing ?? 0) + ", "
665 + (i?.I32_thing ?? 0) + ", "
666 + (i?.I64_thing ?? 0) + "}, "
667 + i2.I32_thing + "}");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100668
669 var mapout = new Dictionary<int, int>();
670 for (var j = 0; j < 5; j++)
671 {
672 mapout[j] = j - 10;
673 }
674 Console.Write("testMap({");
Jens Geyer71569402021-11-13 23:51:16 +0100675 Console.Write(string.Join(", ", mapout.Select((pair) => { return pair.Key + " => " + pair.Value; })));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100676 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(" = {");
Jens Geyer71569402021-11-13 23:51:16 +0100681 Console.Write(string.Join(", ", mapin.Select((pair) => { return pair.Key + " => " + pair.Value; })));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100682 Console.WriteLine("}");
683
684 // TODO: Validate received message
685 var listout = new List<int>();
686 for (var j = -2; j < 3; j++)
687 {
688 listout.Add(j);
689 }
690 Console.Write("testList({");
Jens Geyer71569402021-11-13 23:51:16 +0100691 Console.Write(string.Join(", ", listout));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100692 Console.Write("})");
693
Jens Geyer2b2ea622021-04-09 22:55:11 +0200694 var listin = await client.testList(listout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100695
696 Console.Write(" = {");
Jens Geyer71569402021-11-13 23:51:16 +0100697 Console.Write(string.Join(", ", listin));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100698 Console.WriteLine("}");
699
700 //set
701 // TODO: Validate received message
Jens Geyer3cac3202022-01-31 18:04:35 +0100702 var setout = new HashSet<int>();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100703 for (var j = -2; j < 3; j++)
704 {
705 setout.Add(j);
706 }
707 Console.Write("testSet({");
Jens Geyer71569402021-11-13 23:51:16 +0100708 Console.Write(string.Join(", ", setout));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100709 Console.Write("})");
710
Jens Geyer2b2ea622021-04-09 22:55:11 +0200711 var setin = await client.testSet(setout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100712
713 Console.Write(" = {");
Jens Geyer71569402021-11-13 23:51:16 +0100714 Console.Write(string.Join(", ", setin));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100715 Console.WriteLine("}");
716
717
718 Console.Write("testEnum(ONE)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200719 var ret = await client.testEnum(Numberz.ONE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100720 Console.WriteLine(" = " + ret);
721 if (Numberz.ONE != ret)
722 {
723 Console.WriteLine("*** FAILED ***");
724 returnCode |= ErrorStructs;
725 }
726
727 Console.Write("testEnum(TWO)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200728 ret = await client.testEnum(Numberz.TWO, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100729 Console.WriteLine(" = " + ret);
730 if (Numberz.TWO != ret)
731 {
732 Console.WriteLine("*** FAILED ***");
733 returnCode |= ErrorStructs;
734 }
735
736 Console.Write("testEnum(THREE)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200737 ret = await client.testEnum(Numberz.THREE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100738 Console.WriteLine(" = " + ret);
739 if (Numberz.THREE != ret)
740 {
741 Console.WriteLine("*** FAILED ***");
742 returnCode |= ErrorStructs;
743 }
744
745 Console.Write("testEnum(FIVE)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200746 ret = await client.testEnum(Numberz.FIVE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100747 Console.WriteLine(" = " + ret);
748 if (Numberz.FIVE != ret)
749 {
750 Console.WriteLine("*** FAILED ***");
751 returnCode |= ErrorStructs;
752 }
753
754 Console.Write("testEnum(EIGHT)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200755 ret = await client.testEnum(Numberz.EIGHT, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100756 Console.WriteLine(" = " + ret);
757 if (Numberz.EIGHT != ret)
758 {
759 Console.WriteLine("*** FAILED ***");
760 returnCode |= ErrorStructs;
761 }
762
763 Console.Write("testTypedef(309858235082523)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200764 var uid = await client.testTypedef(309858235082523L, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100765 Console.WriteLine(" = " + uid);
766 if (309858235082523L != uid)
767 {
768 Console.WriteLine("*** FAILED ***");
769 returnCode |= ErrorStructs;
770 }
771
772 // TODO: Validate received message
773 Console.Write("testMapMap(1)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200774 var mm = await client.testMapMap(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100775 Console.Write(" = {");
776 foreach (var key in mm.Keys)
777 {
778 Console.Write(key + " => {");
779 var m2 = mm[key];
780 foreach (var k2 in m2.Keys)
781 {
782 Console.Write(k2 + " => " + m2[k2] + ", ");
783 }
784 Console.Write("}, ");
785 }
786 Console.WriteLine("}");
787
788 // TODO: Validate received message
Jens Geyer261cad32019-11-20 19:03:14 +0100789 var insane = new Insanity
790 {
791 UserMap = new Dictionary<Numberz, long>
792 {
793 [Numberz.FIVE] = 5000L
794 }
795 };
796 var truck = new Xtruct
797 {
798 String_thing = "Truck",
799 Byte_thing = (sbyte)8,
800 I32_thing = 8,
801 I64_thing = 8
802 };
803 insane.Xtructs = new List<Xtruct>
804 {
805 truck
806 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100807 Console.Write("testInsanity()");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200808 var whoa = await client.testInsanity(insane, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100809 Console.Write(" = {");
810 foreach (var key in whoa.Keys)
811 {
812 var val = whoa[key];
813 Console.Write(key + " => {");
814
815 foreach (var k2 in val.Keys)
816 {
817 var v2 = val[k2];
818
819 Console.Write(k2 + " => {");
820 var userMap = v2.UserMap;
821
822 Console.Write("{");
823 if (userMap != null)
824 {
825 foreach (var k3 in userMap.Keys)
826 {
827 Console.Write(k3 + " => " + userMap[k3] + ", ");
828 }
829 }
830 else
831 {
832 Console.Write("null");
833 }
834 Console.Write("}, ");
835
836 var xtructs = v2.Xtructs;
837
838 Console.Write("{");
839 if (xtructs != null)
840 {
841 foreach (var x in xtructs)
842 {
843 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
844 }
845 }
846 else
847 {
848 Console.Write("null");
849 }
850 Console.Write("}");
851
852 Console.Write("}, ");
853 }
854 Console.Write("}, ");
855 }
856 Console.WriteLine("}");
857
858 sbyte arg0 = 1;
859 var arg1 = 2;
860 var arg2 = long.MaxValue;
Jens Geyer261cad32019-11-20 19:03:14 +0100861 var multiDict = new Dictionary<short, string>
862 {
863 [1] = "one"
864 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100865
866 var tmpMultiDict = new List<string>();
867 foreach (var pair in multiDict)
868 tmpMultiDict.Add(pair.Key +" => "+ pair.Value);
869
870 var arg4 = Numberz.FIVE;
871 long arg5 = 5000000;
872 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + ",{" + string.Join(",", tmpMultiDict) + "}," + arg4 + "," + arg5 + ")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200873 var multiResponse = await client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100874 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
875 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
876
877 try
878 {
879 Console.WriteLine("testException(\"Xception\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200880 await client.testException("Xception", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100881 Console.WriteLine("*** FAILED ***");
882 returnCode |= ErrorExceptions;
883 }
884 catch (Xception ex)
885 {
886 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
887 {
888 Console.WriteLine("*** FAILED ***");
889 returnCode |= ErrorExceptions;
890 }
891 }
892 catch (Exception ex)
893 {
894 Console.WriteLine("*** FAILED ***");
895 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200896 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100897 }
898 try
899 {
900 Console.WriteLine("testException(\"TException\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200901 await client.testException("TException", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100902 Console.WriteLine("*** FAILED ***");
903 returnCode |= ErrorExceptions;
904 }
905 catch (Thrift.TException)
906 {
907 // OK
908 }
909 catch (Exception ex)
910 {
911 Console.WriteLine("*** FAILED ***");
912 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200913 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100914 }
915 try
916 {
917 Console.WriteLine("testException(\"ok\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200918 await client.testException("ok", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100919 // OK
920 }
921 catch (Exception ex)
922 {
923 Console.WriteLine("*** FAILED ***");
924 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200925 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100926 }
927
928 try
929 {
930 Console.WriteLine("testMultiException(\"Xception\", ...)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200931 await client.testMultiException("Xception", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100932 Console.WriteLine("*** FAILED ***");
933 returnCode |= ErrorExceptions;
934 }
935 catch (Xception ex)
936 {
937 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
938 {
939 Console.WriteLine("*** FAILED ***");
940 returnCode |= ErrorExceptions;
941 }
942 }
943 catch (Exception ex)
944 {
945 Console.WriteLine("*** FAILED ***");
946 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200947 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100948 }
949 try
950 {
951 Console.WriteLine("testMultiException(\"Xception2\", ...)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200952 await client.testMultiException("Xception2", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100953 Console.WriteLine("*** FAILED ***");
954 returnCode |= ErrorExceptions;
955 }
956 catch (Xception2 ex)
957 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100958 if (ex.ErrorCode != 2002 || ex.Struct_thing?.String_thing != "This is an Xception2")
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100959 {
960 Console.WriteLine("*** FAILED ***");
961 returnCode |= ErrorExceptions;
962 }
963 }
964 catch (Exception ex)
965 {
966 Console.WriteLine("*** FAILED ***");
967 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200968 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100969 }
970 try
971 {
972 Console.WriteLine("testMultiException(\"success\", \"OK\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200973 if ("OK" != (await client.testMultiException("success", "OK", MakeTimeoutToken())).String_thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100974 {
975 Console.WriteLine("*** FAILED ***");
976 returnCode |= ErrorExceptions;
977 }
978 }
979 catch (Exception ex)
980 {
981 Console.WriteLine("*** FAILED ***");
982 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200983 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100984 }
985
Jens Geyerb11f63c2019-03-14 21:12:38 +0100986 Console.WriteLine("Test Oneway(1)");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100987 var sw = new Stopwatch();
988 sw.Start();
Jens Geyer2b2ea622021-04-09 22:55:11 +0200989 await client.testOneway(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100990 sw.Stop();
991 if (sw.ElapsedMilliseconds > 1000)
992 {
993 Console.WriteLine("*** FAILED ***");
994 returnCode |= ErrorBaseTypes;
995 }
996
997 Console.Write("Test Calltime()");
998 var times = 50;
999 sw.Reset();
1000 sw.Start();
Jens Geyer1b770f22019-03-12 01:19:43 +01001001 var token = MakeTimeoutToken(20000);
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001002 for (var k = 0; k < times; ++k)
Jens Geyer2b2ea622021-04-09 22:55:11 +02001003 await client.testVoid(token);
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001004 sw.Stop();
1005 Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times);
1006 return returnCode;
1007 }
1008 }
1009}