blob: 0c80b9c484c4ff2784be9c077789aa06d8026b46 [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
414 var tests = Enumerable.Range(0, param.numThreads).Select(_ => new ClientTest(param)).ToArray();
415
416 //issue tests on separate threads simultaneously
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100417 var start = DateTime.Now;
Jens Geyer71569402021-11-13 23:51:16 +0100418 var tasks = tests.Select(test => test.Execute()).ToArray();
419 Task.WaitAll(tasks);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100420 Console.WriteLine("Total time: " + (DateTime.Now - start));
421 Console.WriteLine();
422 return tests.Select(t => t.ReturnCode).Aggregate((r1, r2) => r1 | r2);
423 }
424 catch (Exception outerEx)
425 {
426 Console.WriteLine("*** FAILED ***");
427 Console.WriteLine("Unexpected error");
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200428 Console.WriteLine(outerEx.Message + "\n" + outerEx.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100429 return ErrorUnknown;
430 }
431 }
432
433 public static string BytesToHex(byte[] data)
434 {
435 return BitConverter.ToString(data).Replace("-", string.Empty);
436 }
437
Jens Geyerbd1a2732019-06-26 22:52:44 +0200438
439 public enum BinaryTestSize
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100440 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200441 Empty, // Edge case: the zero-length empty binary
442 Normal, // Fairly small array of usual size (256 bytes)
443 Large, // Large writes/reads may cause range check errors
444 PipeWriteLimit, // Windows Limit: Pipe write operations across a network are limited to 65,535 bytes per write.
Jens Geyer50806452019-11-23 01:55:58 +0100445 FifteenMB // that's quite a bit of data
Jens Geyerbd1a2732019-06-26 22:52:44 +0200446 };
447
448 public static byte[] PrepareTestData(bool randomDist, BinaryTestSize testcase)
449 {
Jens Geyer261cad32019-11-20 19:03:14 +0100450 int amount;
Jens Geyerbd1a2732019-06-26 22:52:44 +0200451 switch (testcase)
452 {
453 case BinaryTestSize.Empty:
454 amount = 0;
455 break;
456 case BinaryTestSize.Normal:
457 amount = 0x100;
458 break;
459 case BinaryTestSize.Large:
460 amount = 0x8000 + 128;
461 break;
462 case BinaryTestSize.PipeWriteLimit:
463 amount = 0xFFFF + 128;
464 break;
Jens Geyer50806452019-11-23 01:55:58 +0100465 case BinaryTestSize.FifteenMB:
466 amount = 15 * 1024 * 1024;
Jens Geyerbd1a2732019-06-26 22:52:44 +0200467 break;
468 default:
Jens Geyer2b2ea622021-04-09 22:55:11 +0200469 throw new ArgumentException("invalid argument",nameof(testcase));
Jens Geyerbd1a2732019-06-26 22:52:44 +0200470 }
471
472 var retval = new byte[amount];
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100473
474 // linear distribution, unless random is requested
475 if (!randomDist)
476 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200477 for (var i = 0; i < retval.Length; ++i)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100478 {
479 retval[i] = (byte)i;
480 }
481 return retval;
482 }
483
484 // random distribution
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100485 var rnd = new Random();
Jens Geyerbd1a2732019-06-26 22:52:44 +0200486 for (var i = 1; i < retval.Length; ++i)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100487 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200488 retval[i] = (byte)rnd.Next(0x100);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100489 }
490 return retval;
491 }
492
Jens Geyer1b770f22019-03-12 01:19:43 +0100493 private static CancellationToken MakeTimeoutToken(int msec = 5000)
494 {
495 var token = new CancellationTokenSource(msec);
496 return token.Token;
497 }
498
Jens Geyer2b2ea622021-04-09 22:55:11 +0200499 public static async Task<int> ExecuteClientTest(ThriftTest.Client client)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100500 {
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100501 var returnCode = 0;
502
503 Console.Write("testVoid()");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200504 await client.testVoid(MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100505 Console.WriteLine(" = void");
506
507 Console.Write("testString(\"Test\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200508 var s = await client.testString("Test", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100509 Console.WriteLine(" = \"" + s + "\"");
510 if ("Test" != s)
511 {
512 Console.WriteLine("*** FAILED ***");
513 returnCode |= ErrorBaseTypes;
514 }
515
516 Console.Write("testBool(true)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200517 var t = await client.testBool((bool)true, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100518 Console.WriteLine(" = " + t);
519 if (!t)
520 {
521 Console.WriteLine("*** FAILED ***");
522 returnCode |= ErrorBaseTypes;
523 }
524 Console.Write("testBool(false)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200525 var f = await client.testBool((bool)false, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100526 Console.WriteLine(" = " + f);
527 if (f)
528 {
529 Console.WriteLine("*** FAILED ***");
530 returnCode |= ErrorBaseTypes;
531 }
532
533 Console.Write("testByte(1)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200534 var i8 = await client.testByte((sbyte)1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100535 Console.WriteLine(" = " + i8);
536 if (1 != i8)
537 {
538 Console.WriteLine("*** FAILED ***");
539 returnCode |= ErrorBaseTypes;
540 }
541
542 Console.Write("testI32(-1)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200543 var i32 = await client.testI32(-1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100544 Console.WriteLine(" = " + i32);
545 if (-1 != i32)
546 {
547 Console.WriteLine("*** FAILED ***");
548 returnCode |= ErrorBaseTypes;
549 }
550
551 Console.Write("testI64(-34359738368)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200552 var i64 = await client.testI64(-34359738368, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100553 Console.WriteLine(" = " + i64);
554 if (-34359738368 != i64)
555 {
556 Console.WriteLine("*** FAILED ***");
557 returnCode |= ErrorBaseTypes;
558 }
559
560 // TODO: Validate received message
561 Console.Write("testDouble(5.325098235)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200562 var dub = await client.testDouble(5.325098235, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100563 Console.WriteLine(" = " + dub);
564 if (5.325098235 != dub)
565 {
566 Console.WriteLine("*** FAILED ***");
567 returnCode |= ErrorBaseTypes;
568 }
569 Console.Write("testDouble(-0.000341012439638598279)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200570 dub = await client.testDouble(-0.000341012439638598279, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100571 Console.WriteLine(" = " + dub);
572 if (-0.000341012439638598279 != dub)
573 {
574 Console.WriteLine("*** FAILED ***");
575 returnCode |= ErrorBaseTypes;
576 }
577
Jens Geyerbd1a2732019-06-26 22:52:44 +0200578 // testBinary()
579 foreach(BinaryTestSize binTestCase in Enum.GetValues(typeof(BinaryTestSize)))
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100580 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200581 var binOut = PrepareTestData(true, binTestCase);
582
583 Console.Write("testBinary({0} bytes)", binOut.Length);
584 try
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100585 {
Jens Geyer2b2ea622021-04-09 22:55:11 +0200586 var binIn = await client.testBinary(binOut, MakeTimeoutToken());
Jens Geyerbd1a2732019-06-26 22:52:44 +0200587 Console.WriteLine(" = {0} bytes", binIn.Length);
588 if (binIn.Length != binOut.Length)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100589 {
590 Console.WriteLine("*** FAILED ***");
591 returnCode |= ErrorBaseTypes;
592 }
Jens Geyerbd1a2732019-06-26 22:52:44 +0200593 for (var ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
594 {
595 if (binIn[ofs] != binOut[ofs])
596 {
597 Console.WriteLine("*** FAILED ***");
598 returnCode |= ErrorBaseTypes;
599 }
600 }
601 }
602 catch (Thrift.TApplicationException ex)
603 {
604 Console.WriteLine("*** FAILED ***");
605 returnCode |= ErrorBaseTypes;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200606 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyerbd1a2732019-06-26 22:52:44 +0200607 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100608 }
609
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200610 // CrazyNesting
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100611 Console.WriteLine("Test CrazyNesting");
612 var one = new CrazyNesting();
613 var two = new CrazyNesting();
614 one.String_field = "crazy";
615 two.String_field = "crazy";
616 one.Binary_field = new byte[] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
617 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
618 if (typeof(CrazyNesting).GetMethod("Equals")?.DeclaringType == typeof(CrazyNesting))
619 {
620 if (!one.Equals(two))
621 {
622 Console.WriteLine("*** FAILED ***");
623 returnCode |= ErrorContainers;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100624 }
625 }
626
627 // TODO: Validate received message
628 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
Jens Geyer261cad32019-11-20 19:03:14 +0100629 var o = new Xtruct
630 {
631 String_thing = "Zero",
632 Byte_thing = (sbyte)1,
633 I32_thing = -3,
634 I64_thing = -5
635 };
Jens Geyer2b2ea622021-04-09 22:55:11 +0200636 var i = await client.testStruct(o, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100637 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
638
639 // TODO: Validate received message
640 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
Jens Geyer261cad32019-11-20 19:03:14 +0100641 var o2 = new Xtruct2
642 {
643 Byte_thing = (sbyte)1,
644 Struct_thing = o,
645 I32_thing = 5
646 };
Jens Geyer3cac3202022-01-31 18:04:35 +0100647 Xtruct2 i2 = await client.testNest(o2, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100648 i = i2.Struct_thing;
Jens Geyer3cac3202022-01-31 18:04:35 +0100649 Console.WriteLine(" = {" + i2.Byte_thing + ", {\""
650 + (i?.String_thing ?? "<null>") + "\", "
651 + (i?.Byte_thing ?? 0) + ", "
652 + (i?.I32_thing ?? 0) + ", "
653 + (i?.I64_thing ?? 0) + "}, "
654 + i2.I32_thing + "}");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100655
656 var mapout = new Dictionary<int, int>();
657 for (var j = 0; j < 5; j++)
658 {
659 mapout[j] = j - 10;
660 }
661 Console.Write("testMap({");
Jens Geyer71569402021-11-13 23:51:16 +0100662 Console.Write(string.Join(", ", mapout.Select((pair) => { return pair.Key + " => " + pair.Value; })));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100663 Console.Write("})");
664
Jens Geyer2b2ea622021-04-09 22:55:11 +0200665 var mapin = await client.testMap(mapout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100666
667 Console.Write(" = {");
Jens Geyer71569402021-11-13 23:51:16 +0100668 Console.Write(string.Join(", ", mapin.Select((pair) => { return pair.Key + " => " + pair.Value; })));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100669 Console.WriteLine("}");
670
671 // TODO: Validate received message
672 var listout = new List<int>();
673 for (var j = -2; j < 3; j++)
674 {
675 listout.Add(j);
676 }
677 Console.Write("testList({");
Jens Geyer71569402021-11-13 23:51:16 +0100678 Console.Write(string.Join(", ", listout));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100679 Console.Write("})");
680
Jens Geyer2b2ea622021-04-09 22:55:11 +0200681 var listin = await client.testList(listout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100682
683 Console.Write(" = {");
Jens Geyer71569402021-11-13 23:51:16 +0100684 Console.Write(string.Join(", ", listin));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100685 Console.WriteLine("}");
686
687 //set
688 // TODO: Validate received message
Jens Geyer3cac3202022-01-31 18:04:35 +0100689 var setout = new HashSet<int>();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100690 for (var j = -2; j < 3; j++)
691 {
692 setout.Add(j);
693 }
694 Console.Write("testSet({");
Jens Geyer71569402021-11-13 23:51:16 +0100695 Console.Write(string.Join(", ", setout));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100696 Console.Write("})");
697
Jens Geyer2b2ea622021-04-09 22:55:11 +0200698 var setin = await client.testSet(setout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100699
700 Console.Write(" = {");
Jens Geyer71569402021-11-13 23:51:16 +0100701 Console.Write(string.Join(", ", setin));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100702 Console.WriteLine("}");
703
704
705 Console.Write("testEnum(ONE)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200706 var ret = await client.testEnum(Numberz.ONE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100707 Console.WriteLine(" = " + ret);
708 if (Numberz.ONE != ret)
709 {
710 Console.WriteLine("*** FAILED ***");
711 returnCode |= ErrorStructs;
712 }
713
714 Console.Write("testEnum(TWO)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200715 ret = await client.testEnum(Numberz.TWO, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100716 Console.WriteLine(" = " + ret);
717 if (Numberz.TWO != ret)
718 {
719 Console.WriteLine("*** FAILED ***");
720 returnCode |= ErrorStructs;
721 }
722
723 Console.Write("testEnum(THREE)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200724 ret = await client.testEnum(Numberz.THREE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100725 Console.WriteLine(" = " + ret);
726 if (Numberz.THREE != ret)
727 {
728 Console.WriteLine("*** FAILED ***");
729 returnCode |= ErrorStructs;
730 }
731
732 Console.Write("testEnum(FIVE)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200733 ret = await client.testEnum(Numberz.FIVE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100734 Console.WriteLine(" = " + ret);
735 if (Numberz.FIVE != ret)
736 {
737 Console.WriteLine("*** FAILED ***");
738 returnCode |= ErrorStructs;
739 }
740
741 Console.Write("testEnum(EIGHT)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200742 ret = await client.testEnum(Numberz.EIGHT, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100743 Console.WriteLine(" = " + ret);
744 if (Numberz.EIGHT != ret)
745 {
746 Console.WriteLine("*** FAILED ***");
747 returnCode |= ErrorStructs;
748 }
749
750 Console.Write("testTypedef(309858235082523)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200751 var uid = await client.testTypedef(309858235082523L, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100752 Console.WriteLine(" = " + uid);
753 if (309858235082523L != uid)
754 {
755 Console.WriteLine("*** FAILED ***");
756 returnCode |= ErrorStructs;
757 }
758
759 // TODO: Validate received message
760 Console.Write("testMapMap(1)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200761 var mm = await client.testMapMap(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100762 Console.Write(" = {");
763 foreach (var key in mm.Keys)
764 {
765 Console.Write(key + " => {");
766 var m2 = mm[key];
767 foreach (var k2 in m2.Keys)
768 {
769 Console.Write(k2 + " => " + m2[k2] + ", ");
770 }
771 Console.Write("}, ");
772 }
773 Console.WriteLine("}");
774
775 // TODO: Validate received message
Jens Geyer261cad32019-11-20 19:03:14 +0100776 var insane = new Insanity
777 {
778 UserMap = new Dictionary<Numberz, long>
779 {
780 [Numberz.FIVE] = 5000L
781 }
782 };
783 var truck = new Xtruct
784 {
785 String_thing = "Truck",
786 Byte_thing = (sbyte)8,
787 I32_thing = 8,
788 I64_thing = 8
789 };
790 insane.Xtructs = new List<Xtruct>
791 {
792 truck
793 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100794 Console.Write("testInsanity()");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200795 var whoa = await client.testInsanity(insane, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100796 Console.Write(" = {");
797 foreach (var key in whoa.Keys)
798 {
799 var val = whoa[key];
800 Console.Write(key + " => {");
801
802 foreach (var k2 in val.Keys)
803 {
804 var v2 = val[k2];
805
806 Console.Write(k2 + " => {");
807 var userMap = v2.UserMap;
808
809 Console.Write("{");
810 if (userMap != null)
811 {
812 foreach (var k3 in userMap.Keys)
813 {
814 Console.Write(k3 + " => " + userMap[k3] + ", ");
815 }
816 }
817 else
818 {
819 Console.Write("null");
820 }
821 Console.Write("}, ");
822
823 var xtructs = v2.Xtructs;
824
825 Console.Write("{");
826 if (xtructs != null)
827 {
828 foreach (var x in xtructs)
829 {
830 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
831 }
832 }
833 else
834 {
835 Console.Write("null");
836 }
837 Console.Write("}");
838
839 Console.Write("}, ");
840 }
841 Console.Write("}, ");
842 }
843 Console.WriteLine("}");
844
845 sbyte arg0 = 1;
846 var arg1 = 2;
847 var arg2 = long.MaxValue;
Jens Geyer261cad32019-11-20 19:03:14 +0100848 var multiDict = new Dictionary<short, string>
849 {
850 [1] = "one"
851 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100852
853 var tmpMultiDict = new List<string>();
854 foreach (var pair in multiDict)
855 tmpMultiDict.Add(pair.Key +" => "+ pair.Value);
856
857 var arg4 = Numberz.FIVE;
858 long arg5 = 5000000;
859 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + ",{" + string.Join(",", tmpMultiDict) + "}," + arg4 + "," + arg5 + ")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200860 var multiResponse = await client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100861 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
862 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
863
864 try
865 {
866 Console.WriteLine("testException(\"Xception\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200867 await client.testException("Xception", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100868 Console.WriteLine("*** FAILED ***");
869 returnCode |= ErrorExceptions;
870 }
871 catch (Xception ex)
872 {
873 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
874 {
875 Console.WriteLine("*** FAILED ***");
876 returnCode |= ErrorExceptions;
877 }
878 }
879 catch (Exception ex)
880 {
881 Console.WriteLine("*** FAILED ***");
882 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200883 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100884 }
885 try
886 {
887 Console.WriteLine("testException(\"TException\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200888 await client.testException("TException", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100889 Console.WriteLine("*** FAILED ***");
890 returnCode |= ErrorExceptions;
891 }
892 catch (Thrift.TException)
893 {
894 // OK
895 }
896 catch (Exception ex)
897 {
898 Console.WriteLine("*** FAILED ***");
899 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200900 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100901 }
902 try
903 {
904 Console.WriteLine("testException(\"ok\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200905 await client.testException("ok", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100906 // OK
907 }
908 catch (Exception ex)
909 {
910 Console.WriteLine("*** FAILED ***");
911 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200912 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100913 }
914
915 try
916 {
917 Console.WriteLine("testMultiException(\"Xception\", ...)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200918 await client.testMultiException("Xception", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100919 Console.WriteLine("*** FAILED ***");
920 returnCode |= ErrorExceptions;
921 }
922 catch (Xception ex)
923 {
924 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
925 {
926 Console.WriteLine("*** FAILED ***");
927 returnCode |= ErrorExceptions;
928 }
929 }
930 catch (Exception ex)
931 {
932 Console.WriteLine("*** FAILED ***");
933 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200934 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100935 }
936 try
937 {
938 Console.WriteLine("testMultiException(\"Xception2\", ...)");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200939 await client.testMultiException("Xception2", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100940 Console.WriteLine("*** FAILED ***");
941 returnCode |= ErrorExceptions;
942 }
943 catch (Xception2 ex)
944 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100945 if (ex.ErrorCode != 2002 || ex.Struct_thing?.String_thing != "This is an Xception2")
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100946 {
947 Console.WriteLine("*** FAILED ***");
948 returnCode |= ErrorExceptions;
949 }
950 }
951 catch (Exception ex)
952 {
953 Console.WriteLine("*** FAILED ***");
954 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200955 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100956 }
957 try
958 {
959 Console.WriteLine("testMultiException(\"success\", \"OK\")");
Jens Geyer2b2ea622021-04-09 22:55:11 +0200960 if ("OK" != (await client.testMultiException("success", "OK", MakeTimeoutToken())).String_thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100961 {
962 Console.WriteLine("*** FAILED ***");
963 returnCode |= ErrorExceptions;
964 }
965 }
966 catch (Exception ex)
967 {
968 Console.WriteLine("*** FAILED ***");
969 returnCode |= ErrorExceptions;
Mario Emmenlauer47e49232020-04-07 18:43:46 +0200970 Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100971 }
972
Jens Geyerb11f63c2019-03-14 21:12:38 +0100973 Console.WriteLine("Test Oneway(1)");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100974 var sw = new Stopwatch();
975 sw.Start();
Jens Geyer2b2ea622021-04-09 22:55:11 +0200976 await client.testOneway(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100977 sw.Stop();
978 if (sw.ElapsedMilliseconds > 1000)
979 {
980 Console.WriteLine("*** FAILED ***");
981 returnCode |= ErrorBaseTypes;
982 }
983
984 Console.Write("Test Calltime()");
985 var times = 50;
986 sw.Reset();
987 sw.Start();
Jens Geyer1b770f22019-03-12 01:19:43 +0100988 var token = MakeTimeoutToken(20000);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100989 for (var k = 0; k < times; ++k)
Jens Geyer2b2ea622021-04-09 22:55:11 +0200990 await client.testVoid(token);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100991 sw.Stop();
992 Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times);
993 return returnCode;
994 }
995 }
996}