Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
| 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
Jens Geyer | 273607d | 2021-07-31 23:25:51 +0200 | [diff] [blame] | 18 | #pragma warning disable IDE0066 // switch expression |
| 19 | #pragma warning disable IDE0057 // substring |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 20 | #pragma warning disable CS1998 // no await in async method |
Jens Geyer | 273607d | 2021-07-31 23:25:51 +0200 | [diff] [blame] | 21 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 22 | using System; |
| 23 | using System.Collections.Generic; |
| 24 | using System.Diagnostics; |
| 25 | using System.IO; |
| 26 | using System.Linq; |
| 27 | using System.Net; |
| 28 | using System.Reflection; |
| 29 | using System.Security.Authentication; |
| 30 | using System.Security.Cryptography.X509Certificates; |
| 31 | using System.ServiceModel; |
| 32 | using System.Text; |
| 33 | using System.Threading; |
| 34 | using System.Threading.Tasks; |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 35 | using Thrift; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 36 | using Thrift.Collections; |
| 37 | using Thrift.Protocol; |
| 38 | using Thrift.Transport; |
| 39 | using Thrift.Transport.Client; |
| 40 | |
| 41 | namespace ThriftTest |
| 42 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 43 | 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 Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 58 | internal enum TransportChoice |
| 59 | { |
| 60 | Socket, |
| 61 | TlsSocket, |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 62 | Http, |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 63 | NamedPipe |
| 64 | } |
| 65 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 66 | public class TestClient |
| 67 | { |
| 68 | private class TestParams |
| 69 | { |
| 70 | public int numIterations = 1; |
Jens Geyer | 22c412e | 2019-03-12 01:06:25 +0100 | [diff] [blame] | 71 | public string host = "localhost"; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 72 | public int port = 9090; |
| 73 | public int numThreads = 1; |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame] | 74 | public string url = string.Empty; |
| 75 | public string pipe = string.Empty; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 76 | public LayeredChoice layered = LayeredChoice.None; |
| 77 | public ProtocolChoice protocol = ProtocolChoice.Binary; |
| 78 | public TransportChoice transport = TransportChoice.Socket; |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame] | 79 | private readonly TConfiguration Configuration = new(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 80 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 81 | internal void Parse(List<string> args) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 82 | { |
| 83 | for (var i = 0; i < args.Count; ++i) |
| 84 | { |
| 85 | if (args[i] == "-u") |
| 86 | { |
| 87 | url = args[++i]; |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 88 | transport = TransportChoice.Http; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 89 | } |
| 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 Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 97 | transport = TransportChoice.NamedPipe; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 98 | } |
| 99 | else if (args[i].StartsWith("--host=")) |
| 100 | { |
| 101 | // check there for ipaddress |
Jens Geyer | 22c412e | 2019-03-12 01:06:25 +0100 | [diff] [blame] | 102 | host = args[i].Substring(args[i].IndexOf("=") + 1); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 103 | if (transport != TransportChoice.TlsSocket) |
| 104 | transport = TransportChoice.Socket; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 105 | } |
| 106 | else if (args[i].StartsWith("--port=")) |
| 107 | { |
| 108 | port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1)); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 109 | if (transport != TransportChoice.TlsSocket) |
| 110 | transport = TransportChoice.Socket; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 111 | } |
| 112 | else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered") |
| 113 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 114 | layered = LayeredChoice.Buffered; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 115 | } |
| 116 | else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed") |
| 117 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 118 | layered = LayeredChoice.Framed; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 119 | } |
| 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 Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 126 | protocol = ProtocolChoice.Binary; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 127 | } |
| 128 | else if (args[i] == "--compact" || args[i] == "--protocol=compact") |
| 129 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 130 | protocol = ProtocolChoice.Compact; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 131 | } |
| 132 | else if (args[i] == "--json" || args[i] == "--protocol=json") |
| 133 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 134 | protocol = ProtocolChoice.Json; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 135 | } |
| 136 | else if (args[i] == "--ssl") |
| 137 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 138 | transport = TransportChoice.TlsSocket; |
| 139 | } |
| 140 | else if (args[i] == "--help") |
| 141 | { |
| 142 | PrintOptionsHelp(); |
| 143 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 144 | } |
| 145 | else |
| 146 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 147 | Console.WriteLine("Invalid argument: {0}", args[i]); |
| 148 | PrintOptionsHelp(); |
| 149 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 152 | |
| 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 Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 161 | case TransportChoice.Http: |
| 162 | Console.WriteLine("Using HTTP transport"); |
| 163 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 164 | 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 200 | } |
| 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 Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame] | 213 | var existingPath = string.Empty; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 214 | 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 Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 228 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 229 | var cert = new X509Certificate2(existingPath, "thrift"); |
| 230 | |
| 231 | return cert; |
| 232 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 233 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 234 | public TTransport CreateTransport() |
| 235 | { |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 236 | // endpoint transport |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame] | 237 | TTransport trans; |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 238 | switch (transport) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 239 | { |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 240 | case TransportChoice.Http: |
| 241 | Debug.Assert(url != null); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 242 | trans = new THttpTransport(new Uri(url), Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 243 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 244 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 245 | case TransportChoice.NamedPipe: |
| 246 | Debug.Assert(pipe != null); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 247 | trans = new TNamedPipeTransport(pipe,Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 248 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 249 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 250 | 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 Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 256 | |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 257 | trans = new TTlsSocketTransport(host, port, Configuration, 0, |
| 258 | cert, |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 259 | (sender, certificate, chain, errors) => true, |
| 260 | null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12); |
| 261 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 262 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 263 | case TransportChoice.Socket: |
| 264 | default: |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 265 | trans = new TSocketTransport(host, port, Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 266 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 269 | |
| 270 | // layered transport |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 271 | switch (layered) |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 272 | { |
| 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | public TProtocol CreateProtocol(TTransport transport) |
| 288 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 289 | switch (protocol) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 290 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 291 | 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 298 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 299 | } |
| 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 Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 325 | public async Task Execute() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 326 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 327 | 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 Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 338 | await transport.OpenAsync(MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 339 | } |
| 340 | catch (TTransportException ex) |
| 341 | { |
| 342 | Console.WriteLine("*** FAILED ***"); |
| 343 | Console.WriteLine("Connect failed: " + ex.Message); |
| 344 | ReturnCode |= ErrorUnknown; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 345 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 346 | continue; |
| 347 | } |
| 348 | catch (Exception ex) |
| 349 | { |
| 350 | Console.WriteLine("*** FAILED ***"); |
| 351 | Console.WriteLine("Connect failed: " + ex.Message); |
| 352 | ReturnCode |= ErrorUnknown; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 353 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 354 | continue; |
| 355 | } |
| 356 | |
| 357 | try |
| 358 | { |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 359 | ReturnCode |= await ExecuteClientTest(client); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 360 | } |
| 361 | catch (Exception ex) |
| 362 | { |
| 363 | Console.WriteLine("*** FAILED ***"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 364 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 365 | ReturnCode |= ErrorUnknown; |
| 366 | } |
| 367 | } |
| 368 | try |
| 369 | { |
| 370 | transport.Close(); |
| 371 | } |
| 372 | catch (Exception ex) |
| 373 | { |
| 374 | Console.WriteLine("Error while closing transport"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 375 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 376 | } |
| 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 Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 396 | public static async Task<int> Execute(List<string> args) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 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 Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 409 | Console.WriteLine("Error while parsing arguments"); |
| 410 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 411 | return ErrorUnknown; |
| 412 | } |
| 413 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 414 | //issue tests on separate threads simultaneously |
Jens Geyer | d2c28b3 | 2022-02-10 23:03:02 +0100 | [diff] [blame] | 415 | var nThreads = Math.Max(param.numThreads, 1); |
| 416 | Console.Write("Starting {0} test thread(s) ", nThreads); |
| 417 | var tasks = new Task[nThreads]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 418 | var start = DateTime.Now; |
Jens Geyer | d2c28b3 | 2022-02-10 23:03:02 +0100 | [diff] [blame] | 419 | 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 Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 432 | Task.WaitAll(tasks); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 433 | Console.WriteLine("Total time: " + (DateTime.Now - start)); |
| 434 | Console.WriteLine(); |
Jens Geyer | d2c28b3 | 2022-02-10 23:03:02 +0100 | [diff] [blame] | 435 | return retcode; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 436 | } |
| 437 | catch (Exception outerEx) |
| 438 | { |
| 439 | Console.WriteLine("*** FAILED ***"); |
| 440 | Console.WriteLine("Unexpected error"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 441 | Console.WriteLine(outerEx.Message + "\n" + outerEx.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 442 | return ErrorUnknown; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | public static string BytesToHex(byte[] data) |
| 447 | { |
| 448 | return BitConverter.ToString(data).Replace("-", string.Empty); |
| 449 | } |
| 450 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 451 | |
| 452 | public enum BinaryTestSize |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 453 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 454 | 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 Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 458 | FifteenMB // that's quite a bit of data |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 459 | }; |
| 460 | |
| 461 | public static byte[] PrepareTestData(bool randomDist, BinaryTestSize testcase) |
| 462 | { |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 463 | int amount; |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 464 | 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 Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 478 | case BinaryTestSize.FifteenMB: |
| 479 | amount = 15 * 1024 * 1024; |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 480 | break; |
| 481 | default: |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 482 | throw new ArgumentException("invalid argument",nameof(testcase)); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | var retval = new byte[amount]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 486 | |
| 487 | // linear distribution, unless random is requested |
| 488 | if (!randomDist) |
| 489 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 490 | for (var i = 0; i < retval.Length; ++i) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 491 | { |
| 492 | retval[i] = (byte)i; |
| 493 | } |
| 494 | return retval; |
| 495 | } |
| 496 | |
| 497 | // random distribution |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 498 | var rnd = new Random(); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 499 | for (var i = 1; i < retval.Length; ++i) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 500 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 501 | retval[i] = (byte)rnd.Next(0x100); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 502 | } |
| 503 | return retval; |
| 504 | } |
| 505 | |
Jens Geyer | d2c28b3 | 2022-02-10 23:03:02 +0100 | [diff] [blame] | 506 | private static CancellationToken MakeTimeoutToken(int msec = 15_000) |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 507 | { |
| 508 | var token = new CancellationTokenSource(msec); |
| 509 | return token.Token; |
| 510 | } |
| 511 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 512 | public static async Task<int> ExecuteClientTest(ThriftTest.Client client) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 513 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 514 | var returnCode = 0; |
| 515 | |
| 516 | Console.Write("testVoid()"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 517 | await client.testVoid(MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 518 | Console.WriteLine(" = void"); |
| 519 | |
| 520 | Console.Write("testString(\"Test\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 521 | var s = await client.testString("Test", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 522 | Console.WriteLine(" = \"" + s + "\""); |
| 523 | if ("Test" != s) |
| 524 | { |
| 525 | Console.WriteLine("*** FAILED ***"); |
| 526 | returnCode |= ErrorBaseTypes; |
| 527 | } |
| 528 | |
| 529 | Console.Write("testBool(true)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 530 | var t = await client.testBool((bool)true, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 531 | Console.WriteLine(" = " + t); |
| 532 | if (!t) |
| 533 | { |
| 534 | Console.WriteLine("*** FAILED ***"); |
| 535 | returnCode |= ErrorBaseTypes; |
| 536 | } |
| 537 | Console.Write("testBool(false)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 538 | var f = await client.testBool((bool)false, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 539 | Console.WriteLine(" = " + f); |
| 540 | if (f) |
| 541 | { |
| 542 | Console.WriteLine("*** FAILED ***"); |
| 543 | returnCode |= ErrorBaseTypes; |
| 544 | } |
| 545 | |
| 546 | Console.Write("testByte(1)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 547 | var i8 = await client.testByte((sbyte)1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 548 | Console.WriteLine(" = " + i8); |
| 549 | if (1 != i8) |
| 550 | { |
| 551 | Console.WriteLine("*** FAILED ***"); |
| 552 | returnCode |= ErrorBaseTypes; |
| 553 | } |
| 554 | |
| 555 | Console.Write("testI32(-1)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 556 | var i32 = await client.testI32(-1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 557 | Console.WriteLine(" = " + i32); |
| 558 | if (-1 != i32) |
| 559 | { |
| 560 | Console.WriteLine("*** FAILED ***"); |
| 561 | returnCode |= ErrorBaseTypes; |
| 562 | } |
| 563 | |
| 564 | Console.Write("testI64(-34359738368)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 565 | var i64 = await client.testI64(-34359738368, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 566 | 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 Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 575 | var dub = await client.testDouble(5.325098235, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 576 | Console.WriteLine(" = " + dub); |
| 577 | if (5.325098235 != dub) |
| 578 | { |
| 579 | Console.WriteLine("*** FAILED ***"); |
| 580 | returnCode |= ErrorBaseTypes; |
| 581 | } |
| 582 | Console.Write("testDouble(-0.000341012439638598279)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 583 | dub = await client.testDouble(-0.000341012439638598279, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 584 | Console.WriteLine(" = " + dub); |
| 585 | if (-0.000341012439638598279 != dub) |
| 586 | { |
| 587 | Console.WriteLine("*** FAILED ***"); |
| 588 | returnCode |= ErrorBaseTypes; |
| 589 | } |
| 590 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame^] | 591 | // testUuid() |
| 592 | var uuidOut = new Guid("{00112233-4455-6677-8899-AABBCCDDEEFF}"); |
| 593 | Console.Write("testUuid({0})", uuidOut); |
| 594 | try |
| 595 | { |
| 596 | var uuidIn = await client.testUuid(uuidOut, MakeTimeoutToken()); |
| 597 | Console.WriteLine(" = {0}", uuidIn); |
| 598 | if (!uuidIn.Equals(uuidOut)) |
| 599 | { |
| 600 | Console.WriteLine("*** FAILED ***"); |
| 601 | returnCode |= ErrorBaseTypes; |
| 602 | } |
| 603 | } |
| 604 | catch (Thrift.TApplicationException ex) |
| 605 | { |
| 606 | Console.WriteLine("*** FAILED ***"); |
| 607 | returnCode |= ErrorBaseTypes; |
| 608 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
| 609 | } |
| 610 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 611 | // testBinary() |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame^] | 612 | foreach (BinaryTestSize binTestCase in Enum.GetValues(typeof(BinaryTestSize))) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 613 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 614 | var binOut = PrepareTestData(true, binTestCase); |
| 615 | |
| 616 | Console.Write("testBinary({0} bytes)", binOut.Length); |
| 617 | try |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 618 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 619 | var binIn = await client.testBinary(binOut, MakeTimeoutToken()); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 620 | Console.WriteLine(" = {0} bytes", binIn.Length); |
| 621 | if (binIn.Length != binOut.Length) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 622 | { |
| 623 | Console.WriteLine("*** FAILED ***"); |
| 624 | returnCode |= ErrorBaseTypes; |
| 625 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 626 | for (var ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs) |
| 627 | { |
| 628 | if (binIn[ofs] != binOut[ofs]) |
| 629 | { |
| 630 | Console.WriteLine("*** FAILED ***"); |
| 631 | returnCode |= ErrorBaseTypes; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | catch (Thrift.TApplicationException ex) |
| 636 | { |
| 637 | Console.WriteLine("*** FAILED ***"); |
| 638 | returnCode |= ErrorBaseTypes; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 639 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 640 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 641 | } |
| 642 | |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 643 | // CrazyNesting |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 644 | Console.WriteLine("Test CrazyNesting"); |
| 645 | var one = new CrazyNesting(); |
| 646 | var two = new CrazyNesting(); |
| 647 | one.String_field = "crazy"; |
| 648 | two.String_field = "crazy"; |
| 649 | one.Binary_field = new byte[] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF }; |
| 650 | two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF }; |
| 651 | if (typeof(CrazyNesting).GetMethod("Equals")?.DeclaringType == typeof(CrazyNesting)) |
| 652 | { |
| 653 | if (!one.Equals(two)) |
| 654 | { |
| 655 | Console.WriteLine("*** FAILED ***"); |
| 656 | returnCode |= ErrorContainers; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | // TODO: Validate received message |
| 661 | Console.Write("testStruct({\"Zero\", 1, -3, -5})"); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 662 | var o = new Xtruct |
| 663 | { |
| 664 | String_thing = "Zero", |
| 665 | Byte_thing = (sbyte)1, |
| 666 | I32_thing = -3, |
| 667 | I64_thing = -5 |
| 668 | }; |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 669 | var i = await client.testStruct(o, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 670 | Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}"); |
| 671 | |
| 672 | // TODO: Validate received message |
| 673 | Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})"); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 674 | var o2 = new Xtruct2 |
| 675 | { |
| 676 | Byte_thing = (sbyte)1, |
| 677 | Struct_thing = o, |
| 678 | I32_thing = 5 |
| 679 | }; |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame] | 680 | Xtruct2 i2 = await client.testNest(o2, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 681 | i = i2.Struct_thing; |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame] | 682 | Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" |
| 683 | + (i?.String_thing ?? "<null>") + "\", " |
| 684 | + (i?.Byte_thing ?? 0) + ", " |
| 685 | + (i?.I32_thing ?? 0) + ", " |
| 686 | + (i?.I64_thing ?? 0) + "}, " |
| 687 | + i2.I32_thing + "}"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 688 | |
| 689 | var mapout = new Dictionary<int, int>(); |
| 690 | for (var j = 0; j < 5; j++) |
| 691 | { |
| 692 | mapout[j] = j - 10; |
| 693 | } |
| 694 | Console.Write("testMap({"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 695 | Console.Write(string.Join(", ", mapout.Select((pair) => { return pair.Key + " => " + pair.Value; }))); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 696 | Console.Write("})"); |
| 697 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 698 | var mapin = await client.testMap(mapout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 699 | |
| 700 | Console.Write(" = {"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 701 | Console.Write(string.Join(", ", mapin.Select((pair) => { return pair.Key + " => " + pair.Value; }))); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 702 | Console.WriteLine("}"); |
| 703 | |
| 704 | // TODO: Validate received message |
| 705 | var listout = new List<int>(); |
| 706 | for (var j = -2; j < 3; j++) |
| 707 | { |
| 708 | listout.Add(j); |
| 709 | } |
| 710 | Console.Write("testList({"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 711 | Console.Write(string.Join(", ", listout)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 712 | Console.Write("})"); |
| 713 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 714 | var listin = await client.testList(listout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 715 | |
| 716 | Console.Write(" = {"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 717 | Console.Write(string.Join(", ", listin)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 718 | Console.WriteLine("}"); |
| 719 | |
| 720 | //set |
| 721 | // TODO: Validate received message |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame] | 722 | var setout = new HashSet<int>(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 723 | for (var j = -2; j < 3; j++) |
| 724 | { |
| 725 | setout.Add(j); |
| 726 | } |
| 727 | Console.Write("testSet({"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 728 | Console.Write(string.Join(", ", setout)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 729 | Console.Write("})"); |
| 730 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 731 | var setin = await client.testSet(setout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 732 | |
| 733 | Console.Write(" = {"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 734 | Console.Write(string.Join(", ", setin)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 735 | Console.WriteLine("}"); |
| 736 | |
| 737 | |
| 738 | Console.Write("testEnum(ONE)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 739 | var ret = await client.testEnum(Numberz.ONE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 740 | Console.WriteLine(" = " + ret); |
| 741 | if (Numberz.ONE != ret) |
| 742 | { |
| 743 | Console.WriteLine("*** FAILED ***"); |
| 744 | returnCode |= ErrorStructs; |
| 745 | } |
| 746 | |
| 747 | Console.Write("testEnum(TWO)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 748 | ret = await client.testEnum(Numberz.TWO, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 749 | Console.WriteLine(" = " + ret); |
| 750 | if (Numberz.TWO != ret) |
| 751 | { |
| 752 | Console.WriteLine("*** FAILED ***"); |
| 753 | returnCode |= ErrorStructs; |
| 754 | } |
| 755 | |
| 756 | Console.Write("testEnum(THREE)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 757 | ret = await client.testEnum(Numberz.THREE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 758 | Console.WriteLine(" = " + ret); |
| 759 | if (Numberz.THREE != ret) |
| 760 | { |
| 761 | Console.WriteLine("*** FAILED ***"); |
| 762 | returnCode |= ErrorStructs; |
| 763 | } |
| 764 | |
| 765 | Console.Write("testEnum(FIVE)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 766 | ret = await client.testEnum(Numberz.FIVE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 767 | Console.WriteLine(" = " + ret); |
| 768 | if (Numberz.FIVE != ret) |
| 769 | { |
| 770 | Console.WriteLine("*** FAILED ***"); |
| 771 | returnCode |= ErrorStructs; |
| 772 | } |
| 773 | |
| 774 | Console.Write("testEnum(EIGHT)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 775 | ret = await client.testEnum(Numberz.EIGHT, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 776 | Console.WriteLine(" = " + ret); |
| 777 | if (Numberz.EIGHT != ret) |
| 778 | { |
| 779 | Console.WriteLine("*** FAILED ***"); |
| 780 | returnCode |= ErrorStructs; |
| 781 | } |
| 782 | |
| 783 | Console.Write("testTypedef(309858235082523)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 784 | var uid = await client.testTypedef(309858235082523L, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 785 | Console.WriteLine(" = " + uid); |
| 786 | if (309858235082523L != uid) |
| 787 | { |
| 788 | Console.WriteLine("*** FAILED ***"); |
| 789 | returnCode |= ErrorStructs; |
| 790 | } |
| 791 | |
| 792 | // TODO: Validate received message |
| 793 | Console.Write("testMapMap(1)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 794 | var mm = await client.testMapMap(1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 795 | Console.Write(" = {"); |
| 796 | foreach (var key in mm.Keys) |
| 797 | { |
| 798 | Console.Write(key + " => {"); |
| 799 | var m2 = mm[key]; |
| 800 | foreach (var k2 in m2.Keys) |
| 801 | { |
| 802 | Console.Write(k2 + " => " + m2[k2] + ", "); |
| 803 | } |
| 804 | Console.Write("}, "); |
| 805 | } |
| 806 | Console.WriteLine("}"); |
| 807 | |
| 808 | // TODO: Validate received message |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 809 | var insane = new Insanity |
| 810 | { |
| 811 | UserMap = new Dictionary<Numberz, long> |
| 812 | { |
| 813 | [Numberz.FIVE] = 5000L |
| 814 | } |
| 815 | }; |
| 816 | var truck = new Xtruct |
| 817 | { |
| 818 | String_thing = "Truck", |
| 819 | Byte_thing = (sbyte)8, |
| 820 | I32_thing = 8, |
| 821 | I64_thing = 8 |
| 822 | }; |
| 823 | insane.Xtructs = new List<Xtruct> |
| 824 | { |
| 825 | truck |
| 826 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 827 | Console.Write("testInsanity()"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 828 | var whoa = await client.testInsanity(insane, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 829 | Console.Write(" = {"); |
| 830 | foreach (var key in whoa.Keys) |
| 831 | { |
| 832 | var val = whoa[key]; |
| 833 | Console.Write(key + " => {"); |
| 834 | |
| 835 | foreach (var k2 in val.Keys) |
| 836 | { |
| 837 | var v2 = val[k2]; |
| 838 | |
| 839 | Console.Write(k2 + " => {"); |
| 840 | var userMap = v2.UserMap; |
| 841 | |
| 842 | Console.Write("{"); |
| 843 | if (userMap != null) |
| 844 | { |
| 845 | foreach (var k3 in userMap.Keys) |
| 846 | { |
| 847 | Console.Write(k3 + " => " + userMap[k3] + ", "); |
| 848 | } |
| 849 | } |
| 850 | else |
| 851 | { |
| 852 | Console.Write("null"); |
| 853 | } |
| 854 | Console.Write("}, "); |
| 855 | |
| 856 | var xtructs = v2.Xtructs; |
| 857 | |
| 858 | Console.Write("{"); |
| 859 | if (xtructs != null) |
| 860 | { |
| 861 | foreach (var x in xtructs) |
| 862 | { |
| 863 | Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, "); |
| 864 | } |
| 865 | } |
| 866 | else |
| 867 | { |
| 868 | Console.Write("null"); |
| 869 | } |
| 870 | Console.Write("}"); |
| 871 | |
| 872 | Console.Write("}, "); |
| 873 | } |
| 874 | Console.Write("}, "); |
| 875 | } |
| 876 | Console.WriteLine("}"); |
| 877 | |
| 878 | sbyte arg0 = 1; |
| 879 | var arg1 = 2; |
| 880 | var arg2 = long.MaxValue; |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 881 | var multiDict = new Dictionary<short, string> |
| 882 | { |
| 883 | [1] = "one" |
| 884 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 885 | |
| 886 | var tmpMultiDict = new List<string>(); |
| 887 | foreach (var pair in multiDict) |
| 888 | tmpMultiDict.Add(pair.Key +" => "+ pair.Value); |
| 889 | |
| 890 | var arg4 = Numberz.FIVE; |
| 891 | long arg5 = 5000000; |
| 892 | Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + ",{" + string.Join(",", tmpMultiDict) + "}," + arg4 + "," + arg5 + ")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 893 | var multiResponse = await client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 894 | Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing |
| 895 | + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n"); |
| 896 | |
| 897 | try |
| 898 | { |
| 899 | Console.WriteLine("testException(\"Xception\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 900 | await client.testException("Xception", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 901 | Console.WriteLine("*** FAILED ***"); |
| 902 | returnCode |= ErrorExceptions; |
| 903 | } |
| 904 | catch (Xception ex) |
| 905 | { |
| 906 | if (ex.ErrorCode != 1001 || ex.Message != "Xception") |
| 907 | { |
| 908 | Console.WriteLine("*** FAILED ***"); |
| 909 | returnCode |= ErrorExceptions; |
| 910 | } |
| 911 | } |
| 912 | catch (Exception ex) |
| 913 | { |
| 914 | Console.WriteLine("*** FAILED ***"); |
| 915 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 916 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 917 | } |
| 918 | try |
| 919 | { |
| 920 | Console.WriteLine("testException(\"TException\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 921 | await client.testException("TException", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 922 | Console.WriteLine("*** FAILED ***"); |
| 923 | returnCode |= ErrorExceptions; |
| 924 | } |
| 925 | catch (Thrift.TException) |
| 926 | { |
| 927 | // OK |
| 928 | } |
| 929 | catch (Exception ex) |
| 930 | { |
| 931 | Console.WriteLine("*** FAILED ***"); |
| 932 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 933 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 934 | } |
| 935 | try |
| 936 | { |
| 937 | Console.WriteLine("testException(\"ok\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 938 | await client.testException("ok", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 939 | // OK |
| 940 | } |
| 941 | catch (Exception ex) |
| 942 | { |
| 943 | Console.WriteLine("*** FAILED ***"); |
| 944 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 945 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | try |
| 949 | { |
| 950 | Console.WriteLine("testMultiException(\"Xception\", ...)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 951 | await client.testMultiException("Xception", "ignore", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 952 | Console.WriteLine("*** FAILED ***"); |
| 953 | returnCode |= ErrorExceptions; |
| 954 | } |
| 955 | catch (Xception ex) |
| 956 | { |
| 957 | if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception") |
| 958 | { |
| 959 | Console.WriteLine("*** FAILED ***"); |
| 960 | returnCode |= ErrorExceptions; |
| 961 | } |
| 962 | } |
| 963 | catch (Exception ex) |
| 964 | { |
| 965 | Console.WriteLine("*** FAILED ***"); |
| 966 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 967 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 968 | } |
| 969 | try |
| 970 | { |
| 971 | Console.WriteLine("testMultiException(\"Xception2\", ...)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 972 | await client.testMultiException("Xception2", "ignore", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 973 | Console.WriteLine("*** FAILED ***"); |
| 974 | returnCode |= ErrorExceptions; |
| 975 | } |
| 976 | catch (Xception2 ex) |
| 977 | { |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame] | 978 | if (ex.ErrorCode != 2002 || ex.Struct_thing?.String_thing != "This is an Xception2") |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 979 | { |
| 980 | Console.WriteLine("*** FAILED ***"); |
| 981 | returnCode |= ErrorExceptions; |
| 982 | } |
| 983 | } |
| 984 | catch (Exception ex) |
| 985 | { |
| 986 | Console.WriteLine("*** FAILED ***"); |
| 987 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 988 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 989 | } |
| 990 | try |
| 991 | { |
| 992 | Console.WriteLine("testMultiException(\"success\", \"OK\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 993 | if ("OK" != (await client.testMultiException("success", "OK", MakeTimeoutToken())).String_thing) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 994 | { |
| 995 | Console.WriteLine("*** FAILED ***"); |
| 996 | returnCode |= ErrorExceptions; |
| 997 | } |
| 998 | } |
| 999 | catch (Exception ex) |
| 1000 | { |
| 1001 | Console.WriteLine("*** FAILED ***"); |
| 1002 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 1003 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1004 | } |
| 1005 | |
Jens Geyer | b11f63c | 2019-03-14 21:12:38 +0100 | [diff] [blame] | 1006 | Console.WriteLine("Test Oneway(1)"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1007 | var sw = new Stopwatch(); |
| 1008 | sw.Start(); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 1009 | await client.testOneway(1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1010 | sw.Stop(); |
| 1011 | if (sw.ElapsedMilliseconds > 1000) |
| 1012 | { |
| 1013 | Console.WriteLine("*** FAILED ***"); |
| 1014 | returnCode |= ErrorBaseTypes; |
| 1015 | } |
| 1016 | |
| 1017 | Console.Write("Test Calltime()"); |
| 1018 | var times = 50; |
| 1019 | sw.Reset(); |
| 1020 | sw.Start(); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 1021 | var token = MakeTimeoutToken(20000); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1022 | for (var k = 0; k < times; ++k) |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 1023 | await client.testVoid(token); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1024 | sw.Stop(); |
| 1025 | Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times); |
| 1026 | return returnCode; |
| 1027 | } |
| 1028 | } |
| 1029 | } |