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 |
| 20 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 21 | using System; |
| 22 | using System.Collections.Generic; |
| 23 | using System.Diagnostics; |
| 24 | using System.IO; |
| 25 | using System.Linq; |
| 26 | using System.Net; |
| 27 | using System.Reflection; |
| 28 | using System.Security.Authentication; |
| 29 | using System.Security.Cryptography.X509Certificates; |
| 30 | using System.ServiceModel; |
| 31 | using System.Text; |
| 32 | using System.Threading; |
| 33 | using System.Threading.Tasks; |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 34 | using Thrift; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 35 | using Thrift.Collections; |
| 36 | using Thrift.Protocol; |
| 37 | using Thrift.Transport; |
| 38 | using Thrift.Transport.Client; |
| 39 | |
| 40 | namespace ThriftTest |
| 41 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 42 | internal enum ProtocolChoice |
| 43 | { |
| 44 | Binary, |
| 45 | Compact, |
| 46 | Json |
| 47 | } |
| 48 | |
| 49 | // it does not make much sense to use buffered when we already use framed |
| 50 | internal enum LayeredChoice |
| 51 | { |
| 52 | None, |
| 53 | Buffered, |
| 54 | Framed |
| 55 | } |
| 56 | |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 57 | internal enum TransportChoice |
| 58 | { |
| 59 | Socket, |
| 60 | TlsSocket, |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 61 | Http, |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 62 | NamedPipe |
| 63 | } |
| 64 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 65 | public class TestClient |
| 66 | { |
| 67 | private class TestParams |
| 68 | { |
| 69 | public int numIterations = 1; |
Jens Geyer | 22c412e | 2019-03-12 01:06:25 +0100 | [diff] [blame] | 70 | public string host = "localhost"; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 71 | public int port = 9090; |
| 72 | public int numThreads = 1; |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame] | 73 | public string url = string.Empty; |
| 74 | public string pipe = string.Empty; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 75 | public LayeredChoice layered = LayeredChoice.None; |
| 76 | public ProtocolChoice protocol = ProtocolChoice.Binary; |
| 77 | public TransportChoice transport = TransportChoice.Socket; |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame] | 78 | private readonly TConfiguration Configuration = new(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 79 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 80 | internal void Parse(List<string> args) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 81 | { |
| 82 | for (var i = 0; i < args.Count; ++i) |
| 83 | { |
| 84 | if (args[i] == "-u") |
| 85 | { |
| 86 | url = args[++i]; |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 87 | transport = TransportChoice.Http; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 88 | } |
| 89 | else if (args[i] == "-n") |
| 90 | { |
| 91 | numIterations = Convert.ToInt32(args[++i]); |
| 92 | } |
| 93 | else if (args[i].StartsWith("--pipe=")) |
| 94 | { |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 95 | pipe = args[i].Substring(args[i].IndexOf('=') + 1); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 96 | transport = TransportChoice.NamedPipe; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 97 | } |
| 98 | else if (args[i].StartsWith("--host=")) |
| 99 | { |
| 100 | // check there for ipaddress |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 101 | host = args[i].Substring(args[i].IndexOf('=') + 1); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 102 | if (transport != TransportChoice.TlsSocket) |
| 103 | transport = TransportChoice.Socket; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 104 | } |
| 105 | else if (args[i].StartsWith("--port=")) |
| 106 | { |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 107 | port = int.Parse(args[i].Substring(args[i].IndexOf('=') + 1)); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 108 | if (transport != TransportChoice.TlsSocket) |
| 109 | transport = TransportChoice.Socket; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 110 | } |
| 111 | else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered") |
| 112 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 113 | layered = LayeredChoice.Buffered; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 114 | } |
| 115 | else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed") |
| 116 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 117 | layered = LayeredChoice.Framed; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 118 | } |
| 119 | else if (args[i] == "-t") |
| 120 | { |
| 121 | numThreads = Convert.ToInt32(args[++i]); |
| 122 | } |
| 123 | else if (args[i] == "--binary" || args[i] == "--protocol=binary") |
| 124 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 125 | protocol = ProtocolChoice.Binary; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 126 | } |
| 127 | else if (args[i] == "--compact" || args[i] == "--protocol=compact") |
| 128 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 129 | protocol = ProtocolChoice.Compact; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 130 | } |
| 131 | else if (args[i] == "--json" || args[i] == "--protocol=json") |
| 132 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 133 | protocol = ProtocolChoice.Json; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 134 | } |
| 135 | else if (args[i] == "--ssl") |
| 136 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 137 | transport = TransportChoice.TlsSocket; |
| 138 | } |
| 139 | else if (args[i] == "--help") |
| 140 | { |
| 141 | PrintOptionsHelp(); |
| 142 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 143 | } |
| 144 | else |
| 145 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 146 | Console.WriteLine("Invalid argument: {0}", args[i]); |
| 147 | PrintOptionsHelp(); |
| 148 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 151 | |
| 152 | switch (transport) |
| 153 | { |
| 154 | case TransportChoice.Socket: |
| 155 | Console.WriteLine("Using socket transport"); |
| 156 | break; |
| 157 | case TransportChoice.TlsSocket: |
| 158 | Console.WriteLine("Using encrypted transport"); |
| 159 | break; |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 160 | case TransportChoice.Http: |
| 161 | Console.WriteLine("Using HTTP transport"); |
| 162 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 163 | case TransportChoice.NamedPipe: |
| 164 | Console.WriteLine("Using named pipes transport"); |
| 165 | break; |
| 166 | default: // unhandled case |
| 167 | Debug.Assert(false); |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | switch (layered) |
| 172 | { |
| 173 | case LayeredChoice.Framed: |
| 174 | Console.WriteLine("Using framed transport"); |
| 175 | break; |
| 176 | case LayeredChoice.Buffered: |
| 177 | Console.WriteLine("Using buffered transport"); |
| 178 | break; |
| 179 | default: // unhandled case? |
| 180 | Debug.Assert(layered == LayeredChoice.None); |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | switch (protocol) |
| 185 | { |
| 186 | case ProtocolChoice.Binary: |
| 187 | Console.WriteLine("Using binary protocol"); |
| 188 | break; |
| 189 | case ProtocolChoice.Compact: |
| 190 | Console.WriteLine("Using compact protocol"); |
| 191 | break; |
| 192 | case ProtocolChoice.Json: |
| 193 | Console.WriteLine("Using JSON protocol"); |
| 194 | break; |
| 195 | default: // unhandled case? |
| 196 | Debug.Assert(false); |
| 197 | break; |
| 198 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | private static X509Certificate2 GetClientCert() |
| 202 | { |
| 203 | var clientCertName = "client.p12"; |
| 204 | var possiblePaths = new List<string> |
| 205 | { |
| 206 | "../../../keys/", |
| 207 | "../../keys/", |
| 208 | "../keys/", |
| 209 | "keys/", |
| 210 | }; |
| 211 | |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame] | 212 | var existingPath = string.Empty; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 213 | foreach (var possiblePath in possiblePaths) |
| 214 | { |
| 215 | var path = Path.GetFullPath(possiblePath + clientCertName); |
| 216 | if (File.Exists(path)) |
| 217 | { |
| 218 | existingPath = path; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if (string.IsNullOrEmpty(existingPath)) |
| 224 | { |
| 225 | throw new FileNotFoundException($"Cannot find file: {clientCertName}"); |
| 226 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 227 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 228 | var cert = new X509Certificate2(existingPath, "thrift"); |
| 229 | |
| 230 | return cert; |
| 231 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 232 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 233 | public TTransport CreateTransport() |
| 234 | { |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 235 | // endpoint transport |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame] | 236 | TTransport trans; |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 237 | switch (transport) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 238 | { |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 239 | case TransportChoice.Http: |
| 240 | Debug.Assert(url != null); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 241 | trans = new THttpTransport(new Uri(url), Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 242 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 243 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 244 | case TransportChoice.NamedPipe: |
| 245 | Debug.Assert(pipe != null); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 246 | trans = new TNamedPipeTransport(pipe,Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 247 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 248 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 249 | case TransportChoice.TlsSocket: |
| 250 | var cert = GetClientCert(); |
| 251 | if (cert == null || !cert.HasPrivateKey) |
| 252 | { |
| 253 | throw new InvalidOperationException("Certificate doesn't contain private key"); |
| 254 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 255 | |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 256 | trans = new TTlsSocketTransport(host, port, Configuration, 0, |
| 257 | cert, |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 258 | (sender, certificate, chain, errors) => true, |
Jens Geyer | a06eedc | 2023-11-16 23:23:04 +0100 | [diff] [blame] | 259 | null); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 260 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 261 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 262 | case TransportChoice.Socket: |
| 263 | default: |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 264 | trans = new TSocketTransport(host, port, Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 265 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 268 | |
| 269 | // layered transport |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 270 | switch (layered) |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 271 | { |
| 272 | case LayeredChoice.Buffered: |
| 273 | trans = new TBufferedTransport(trans); |
| 274 | break; |
| 275 | case LayeredChoice.Framed: |
| 276 | trans = new TFramedTransport(trans); |
| 277 | break; |
| 278 | default: |
| 279 | Debug.Assert(layered == LayeredChoice.None); |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | return trans; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | public TProtocol CreateProtocol(TTransport transport) |
| 287 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 288 | switch (protocol) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 289 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 290 | case ProtocolChoice.Compact: |
| 291 | return new TCompactProtocol(transport); |
| 292 | case ProtocolChoice.Json: |
| 293 | return new TJsonProtocol(transport); |
| 294 | case ProtocolChoice.Binary: |
| 295 | default: |
| 296 | return new TBinaryProtocol(transport); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 297 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | |
| 302 | private const int ErrorBaseTypes = 1; |
| 303 | private const int ErrorStructs = 2; |
| 304 | private const int ErrorContainers = 4; |
| 305 | private const int ErrorExceptions = 8; |
| 306 | private const int ErrorUnknown = 64; |
| 307 | |
| 308 | private class ClientTest |
| 309 | { |
| 310 | private readonly TTransport transport; |
| 311 | private readonly ThriftTest.Client client; |
| 312 | private readonly int numIterations; |
| 313 | private bool done; |
| 314 | |
| 315 | public int ReturnCode { get; set; } |
| 316 | |
| 317 | public ClientTest(TestParams param) |
| 318 | { |
| 319 | transport = param.CreateTransport(); |
| 320 | client = new ThriftTest.Client(param.CreateProtocol(transport)); |
| 321 | numIterations = param.numIterations; |
| 322 | } |
| 323 | |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 324 | public async Task Execute() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 325 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 326 | if (done) |
| 327 | { |
| 328 | Console.WriteLine("Execute called more than once"); |
| 329 | throw new InvalidOperationException(); |
| 330 | } |
| 331 | |
| 332 | for (var i = 0; i < numIterations; i++) |
| 333 | { |
| 334 | try |
| 335 | { |
| 336 | if (!transport.IsOpen) |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 337 | await transport.OpenAsync(MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 338 | } |
| 339 | catch (TTransportException ex) |
| 340 | { |
| 341 | Console.WriteLine("*** FAILED ***"); |
| 342 | Console.WriteLine("Connect failed: " + ex.Message); |
| 343 | ReturnCode |= ErrorUnknown; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 344 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 345 | continue; |
| 346 | } |
| 347 | catch (Exception ex) |
| 348 | { |
| 349 | Console.WriteLine("*** FAILED ***"); |
| 350 | Console.WriteLine("Connect failed: " + ex.Message); |
| 351 | ReturnCode |= ErrorUnknown; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 352 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 353 | continue; |
| 354 | } |
| 355 | |
| 356 | try |
| 357 | { |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 358 | ReturnCode |= await ExecuteClientTest(client); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 359 | } |
| 360 | catch (Exception ex) |
| 361 | { |
| 362 | Console.WriteLine("*** FAILED ***"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 363 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 364 | ReturnCode |= ErrorUnknown; |
| 365 | } |
| 366 | } |
| 367 | try |
| 368 | { |
| 369 | transport.Close(); |
| 370 | } |
| 371 | catch (Exception ex) |
| 372 | { |
| 373 | Console.WriteLine("Error while closing transport"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 374 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 375 | } |
| 376 | done = true; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | internal static void PrintOptionsHelp() |
| 381 | { |
| 382 | Console.WriteLine("Client options:"); |
| 383 | Console.WriteLine(" -u <URL>"); |
| 384 | Console.WriteLine(" -t <# of threads to run> default = 1"); |
| 385 | Console.WriteLine(" -n <# of iterations> per thread"); |
| 386 | Console.WriteLine(" --pipe=<pipe name>"); |
| 387 | Console.WriteLine(" --host=<IP address>"); |
| 388 | Console.WriteLine(" --port=<port number>"); |
| 389 | Console.WriteLine(" --transport=<transport name> one of buffered,framed (defaults to none)"); |
| 390 | Console.WriteLine(" --protocol=<protocol name> one of compact,json (defaults to binary)"); |
| 391 | Console.WriteLine(" --ssl"); |
| 392 | Console.WriteLine(); |
| 393 | } |
| 394 | |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 395 | public static Task<int> Execute(List<string> args) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 396 | { |
| 397 | try |
| 398 | { |
| 399 | var param = new TestParams(); |
| 400 | |
| 401 | try |
| 402 | { |
| 403 | param.Parse(args); |
| 404 | } |
| 405 | catch (Exception ex) |
| 406 | { |
| 407 | Console.WriteLine("*** FAILED ***"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 408 | Console.WriteLine("Error while parsing arguments"); |
Jens Geyer | 5e37d57 | 2022-09-08 23:07:11 +0200 | [diff] [blame] | 409 | Console.WriteLine("{0} {1}\nStack:\n{2}", ex.GetType().Name, ex.Message, ex.StackTrace); |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 410 | return Task.FromResult(ErrorUnknown); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 413 | //issue tests on separate threads simultaneously |
Jens Geyer | d2c28b3 | 2022-02-10 23:03:02 +0100 | [diff] [blame] | 414 | var nThreads = Math.Max(param.numThreads, 1); |
| 415 | Console.Write("Starting {0} test thread(s) ", nThreads); |
| 416 | var tasks = new Task[nThreads]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 417 | var start = DateTime.Now; |
Jens Geyer | d2c28b3 | 2022-02-10 23:03:02 +0100 | [diff] [blame] | 418 | var retcode = 0; |
| 419 | for (var i = 0; i < tasks.Length; ++i) |
| 420 | { |
| 421 | Console.Write('.'); |
| 422 | tasks[i] = Task.Run(async () => |
| 423 | { |
| 424 | var test = new ClientTest(param); |
| 425 | await test.Execute(); |
| 426 | lock (tasks) |
| 427 | retcode |= test.ReturnCode; |
| 428 | }); |
| 429 | } |
| 430 | Console.WriteLine(" OK"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 431 | Task.WaitAll(tasks); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 432 | Console.WriteLine("Total time: " + (DateTime.Now - start)); |
| 433 | Console.WriteLine(); |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 434 | return Task.FromResult(retcode); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 435 | } |
| 436 | catch (Exception outerEx) |
| 437 | { |
| 438 | Console.WriteLine("*** FAILED ***"); |
| 439 | Console.WriteLine("Unexpected error"); |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 440 | Console.WriteLine(outerEx.Message + "\n" + outerEx.StackTrace); |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 441 | return Task.FromResult(ErrorUnknown); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
| 445 | public static string BytesToHex(byte[] data) |
| 446 | { |
| 447 | return BitConverter.ToString(data).Replace("-", string.Empty); |
| 448 | } |
| 449 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 450 | |
| 451 | public enum BinaryTestSize |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 452 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 453 | Empty, // Edge case: the zero-length empty binary |
| 454 | Normal, // Fairly small array of usual size (256 bytes) |
| 455 | Large, // Large writes/reads may cause range check errors |
| 456 | 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] | 457 | FifteenMB // that's quite a bit of data |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 458 | }; |
| 459 | |
| 460 | public static byte[] PrepareTestData(bool randomDist, BinaryTestSize testcase) |
| 461 | { |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 462 | int amount; |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 463 | switch (testcase) |
| 464 | { |
| 465 | case BinaryTestSize.Empty: |
| 466 | amount = 0; |
| 467 | break; |
| 468 | case BinaryTestSize.Normal: |
| 469 | amount = 0x100; |
| 470 | break; |
| 471 | case BinaryTestSize.Large: |
| 472 | amount = 0x8000 + 128; |
| 473 | break; |
| 474 | case BinaryTestSize.PipeWriteLimit: |
| 475 | amount = 0xFFFF + 128; |
| 476 | break; |
Jens Geyer | 5080645 | 2019-11-23 01:55:58 +0100 | [diff] [blame] | 477 | case BinaryTestSize.FifteenMB: |
| 478 | amount = 15 * 1024 * 1024; |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 479 | break; |
| 480 | default: |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 481 | throw new ArgumentException("invalid argument",nameof(testcase)); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | var retval = new byte[amount]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 485 | |
| 486 | // linear distribution, unless random is requested |
| 487 | if (!randomDist) |
| 488 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 489 | for (var i = 0; i < retval.Length; ++i) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 490 | { |
| 491 | retval[i] = (byte)i; |
| 492 | } |
| 493 | return retval; |
| 494 | } |
| 495 | |
| 496 | // random distribution |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 497 | var rnd = new Random(); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 498 | for (var i = 1; i < retval.Length; ++i) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 499 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 500 | retval[i] = (byte)rnd.Next(0x100); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 501 | } |
| 502 | return retval; |
| 503 | } |
| 504 | |
Jens Geyer | d2c28b3 | 2022-02-10 23:03:02 +0100 | [diff] [blame] | 505 | private static CancellationToken MakeTimeoutToken(int msec = 15_000) |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 506 | { |
| 507 | var token = new CancellationTokenSource(msec); |
| 508 | return token.Token; |
| 509 | } |
| 510 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 511 | public static async Task<int> ExecuteClientTest(ThriftTest.Client client) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 512 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 513 | var returnCode = 0; |
| 514 | |
| 515 | Console.Write("testVoid()"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 516 | await client.testVoid(MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 517 | Console.WriteLine(" = void"); |
| 518 | |
| 519 | Console.Write("testString(\"Test\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 520 | var s = await client.testString("Test", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 521 | Console.WriteLine(" = \"" + s + "\""); |
| 522 | if ("Test" != s) |
| 523 | { |
| 524 | Console.WriteLine("*** FAILED ***"); |
| 525 | returnCode |= ErrorBaseTypes; |
| 526 | } |
| 527 | |
| 528 | Console.Write("testBool(true)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 529 | var t = await client.testBool((bool)true, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 530 | Console.WriteLine(" = " + t); |
| 531 | if (!t) |
| 532 | { |
| 533 | Console.WriteLine("*** FAILED ***"); |
| 534 | returnCode |= ErrorBaseTypes; |
| 535 | } |
| 536 | Console.Write("testBool(false)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 537 | var f = await client.testBool((bool)false, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 538 | Console.WriteLine(" = " + f); |
| 539 | if (f) |
| 540 | { |
| 541 | Console.WriteLine("*** FAILED ***"); |
| 542 | returnCode |= ErrorBaseTypes; |
| 543 | } |
| 544 | |
| 545 | Console.Write("testByte(1)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 546 | var i8 = await client.testByte((sbyte)1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 547 | Console.WriteLine(" = " + i8); |
| 548 | if (1 != i8) |
| 549 | { |
| 550 | Console.WriteLine("*** FAILED ***"); |
| 551 | returnCode |= ErrorBaseTypes; |
| 552 | } |
| 553 | |
| 554 | Console.Write("testI32(-1)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 555 | var i32 = await client.testI32(-1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 556 | Console.WriteLine(" = " + i32); |
| 557 | if (-1 != i32) |
| 558 | { |
| 559 | Console.WriteLine("*** FAILED ***"); |
| 560 | returnCode |= ErrorBaseTypes; |
| 561 | } |
| 562 | |
| 563 | Console.Write("testI64(-34359738368)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 564 | var i64 = await client.testI64(-34359738368, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 565 | Console.WriteLine(" = " + i64); |
| 566 | if (-34359738368 != i64) |
| 567 | { |
| 568 | Console.WriteLine("*** FAILED ***"); |
| 569 | returnCode |= ErrorBaseTypes; |
| 570 | } |
| 571 | |
| 572 | // TODO: Validate received message |
| 573 | Console.Write("testDouble(5.325098235)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 574 | var dub = await client.testDouble(5.325098235, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 575 | Console.WriteLine(" = " + dub); |
| 576 | if (5.325098235 != dub) |
| 577 | { |
| 578 | Console.WriteLine("*** FAILED ***"); |
| 579 | returnCode |= ErrorBaseTypes; |
| 580 | } |
| 581 | Console.Write("testDouble(-0.000341012439638598279)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 582 | dub = await client.testDouble(-0.000341012439638598279, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 583 | Console.WriteLine(" = " + dub); |
| 584 | if (-0.000341012439638598279 != dub) |
| 585 | { |
| 586 | Console.WriteLine("*** FAILED ***"); |
| 587 | returnCode |= ErrorBaseTypes; |
| 588 | } |
| 589 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 590 | // testUuid() |
| 591 | var uuidOut = new Guid("{00112233-4455-6677-8899-AABBCCDDEEFF}"); |
| 592 | Console.Write("testUuid({0})", uuidOut); |
| 593 | try |
| 594 | { |
| 595 | var uuidIn = await client.testUuid(uuidOut, MakeTimeoutToken()); |
| 596 | Console.WriteLine(" = {0}", uuidIn); |
| 597 | if (!uuidIn.Equals(uuidOut)) |
| 598 | { |
| 599 | Console.WriteLine("*** FAILED ***"); |
| 600 | returnCode |= ErrorBaseTypes; |
| 601 | } |
| 602 | } |
| 603 | catch (Thrift.TApplicationException ex) |
| 604 | { |
| 605 | Console.WriteLine("*** FAILED ***"); |
| 606 | returnCode |= ErrorBaseTypes; |
| 607 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
| 608 | } |
| 609 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 610 | // testBinary() |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 611 | foreach (BinaryTestSize binTestCase in Enum.GetValues(typeof(BinaryTestSize))) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 612 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 613 | var binOut = PrepareTestData(true, binTestCase); |
| 614 | |
| 615 | Console.Write("testBinary({0} bytes)", binOut.Length); |
| 616 | try |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 617 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 618 | var binIn = await client.testBinary(binOut, MakeTimeoutToken()); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 619 | Console.WriteLine(" = {0} bytes", binIn.Length); |
| 620 | if (binIn.Length != binOut.Length) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 621 | { |
| 622 | Console.WriteLine("*** FAILED ***"); |
| 623 | returnCode |= ErrorBaseTypes; |
| 624 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 625 | for (var ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs) |
| 626 | { |
| 627 | if (binIn[ofs] != binOut[ofs]) |
| 628 | { |
| 629 | Console.WriteLine("*** FAILED ***"); |
| 630 | returnCode |= ErrorBaseTypes; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | catch (Thrift.TApplicationException ex) |
| 635 | { |
| 636 | Console.WriteLine("*** FAILED ***"); |
| 637 | returnCode |= ErrorBaseTypes; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 638 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 639 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 642 | // CrazyNesting |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 643 | Console.WriteLine("Test CrazyNesting"); |
| 644 | var one = new CrazyNesting(); |
| 645 | var two = new CrazyNesting(); |
| 646 | one.String_field = "crazy"; |
| 647 | two.String_field = "crazy"; |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 648 | one.Binary_field = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF]; |
| 649 | two.Binary_field = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 650 | if (typeof(CrazyNesting).GetMethod("Equals")?.DeclaringType == typeof(CrazyNesting)) |
| 651 | { |
| 652 | if (!one.Equals(two)) |
| 653 | { |
| 654 | Console.WriteLine("*** FAILED ***"); |
| 655 | returnCode |= ErrorContainers; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | // TODO: Validate received message |
| 660 | Console.Write("testStruct({\"Zero\", 1, -3, -5})"); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 661 | var o = new Xtruct |
| 662 | { |
| 663 | String_thing = "Zero", |
| 664 | Byte_thing = (sbyte)1, |
| 665 | I32_thing = -3, |
| 666 | I64_thing = -5 |
| 667 | }; |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 668 | var i = await client.testStruct(o, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 669 | Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}"); |
| 670 | |
| 671 | // TODO: Validate received message |
| 672 | Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})"); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 673 | var o2 = new Xtruct2 |
| 674 | { |
| 675 | Byte_thing = (sbyte)1, |
| 676 | Struct_thing = o, |
| 677 | I32_thing = 5 |
| 678 | }; |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame] | 679 | Xtruct2 i2 = await client.testNest(o2, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 680 | i = i2.Struct_thing; |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame] | 681 | Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" |
| 682 | + (i?.String_thing ?? "<null>") + "\", " |
| 683 | + (i?.Byte_thing ?? 0) + ", " |
| 684 | + (i?.I32_thing ?? 0) + ", " |
| 685 | + (i?.I64_thing ?? 0) + "}, " |
| 686 | + i2.I32_thing + "}"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 687 | |
| 688 | var mapout = new Dictionary<int, int>(); |
| 689 | for (var j = 0; j < 5; j++) |
| 690 | { |
| 691 | mapout[j] = j - 10; |
| 692 | } |
| 693 | Console.Write("testMap({"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 694 | Console.Write(string.Join(", ", mapout.Select((pair) => { return pair.Key + " => " + pair.Value; }))); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 695 | Console.Write("})"); |
| 696 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 697 | var mapin = await client.testMap(mapout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 698 | |
| 699 | Console.Write(" = {"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 700 | Console.Write(string.Join(", ", mapin.Select((pair) => { return pair.Key + " => " + pair.Value; }))); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 701 | Console.WriteLine("}"); |
| 702 | |
| 703 | // TODO: Validate received message |
| 704 | var listout = new List<int>(); |
| 705 | for (var j = -2; j < 3; j++) |
| 706 | { |
| 707 | listout.Add(j); |
| 708 | } |
| 709 | Console.Write("testList({"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 710 | Console.Write(string.Join(", ", listout)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 711 | Console.Write("})"); |
| 712 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 713 | var listin = await client.testList(listout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 714 | |
| 715 | Console.Write(" = {"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 716 | Console.Write(string.Join(", ", listin)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 717 | Console.WriteLine("}"); |
| 718 | |
| 719 | //set |
| 720 | // TODO: Validate received message |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame] | 721 | var setout = new HashSet<int>(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 722 | for (var j = -2; j < 3; j++) |
| 723 | { |
| 724 | setout.Add(j); |
| 725 | } |
| 726 | Console.Write("testSet({"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 727 | Console.Write(string.Join(", ", setout)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 728 | Console.Write("})"); |
| 729 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 730 | var setin = await client.testSet(setout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 731 | |
| 732 | Console.Write(" = {"); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 733 | Console.Write(string.Join(", ", setin)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 734 | Console.WriteLine("}"); |
| 735 | |
| 736 | |
| 737 | Console.Write("testEnum(ONE)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 738 | var ret = await client.testEnum(Numberz.ONE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 739 | Console.WriteLine(" = " + ret); |
| 740 | if (Numberz.ONE != ret) |
| 741 | { |
| 742 | Console.WriteLine("*** FAILED ***"); |
| 743 | returnCode |= ErrorStructs; |
| 744 | } |
| 745 | |
| 746 | Console.Write("testEnum(TWO)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 747 | ret = await client.testEnum(Numberz.TWO, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 748 | Console.WriteLine(" = " + ret); |
| 749 | if (Numberz.TWO != ret) |
| 750 | { |
| 751 | Console.WriteLine("*** FAILED ***"); |
| 752 | returnCode |= ErrorStructs; |
| 753 | } |
| 754 | |
| 755 | Console.Write("testEnum(THREE)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 756 | ret = await client.testEnum(Numberz.THREE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 757 | Console.WriteLine(" = " + ret); |
| 758 | if (Numberz.THREE != ret) |
| 759 | { |
| 760 | Console.WriteLine("*** FAILED ***"); |
| 761 | returnCode |= ErrorStructs; |
| 762 | } |
| 763 | |
| 764 | Console.Write("testEnum(FIVE)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 765 | ret = await client.testEnum(Numberz.FIVE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 766 | Console.WriteLine(" = " + ret); |
| 767 | if (Numberz.FIVE != ret) |
| 768 | { |
| 769 | Console.WriteLine("*** FAILED ***"); |
| 770 | returnCode |= ErrorStructs; |
| 771 | } |
| 772 | |
| 773 | Console.Write("testEnum(EIGHT)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 774 | ret = await client.testEnum(Numberz.EIGHT, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 775 | Console.WriteLine(" = " + ret); |
| 776 | if (Numberz.EIGHT != ret) |
| 777 | { |
| 778 | Console.WriteLine("*** FAILED ***"); |
| 779 | returnCode |= ErrorStructs; |
| 780 | } |
| 781 | |
| 782 | Console.Write("testTypedef(309858235082523)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 783 | var uid = await client.testTypedef(309858235082523L, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 784 | Console.WriteLine(" = " + uid); |
| 785 | if (309858235082523L != uid) |
| 786 | { |
| 787 | Console.WriteLine("*** FAILED ***"); |
| 788 | returnCode |= ErrorStructs; |
| 789 | } |
| 790 | |
| 791 | // TODO: Validate received message |
| 792 | Console.Write("testMapMap(1)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 793 | var mm = await client.testMapMap(1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 794 | Console.Write(" = {"); |
| 795 | foreach (var key in mm.Keys) |
| 796 | { |
| 797 | Console.Write(key + " => {"); |
| 798 | var m2 = mm[key]; |
| 799 | foreach (var k2 in m2.Keys) |
| 800 | { |
| 801 | Console.Write(k2 + " => " + m2[k2] + ", "); |
| 802 | } |
| 803 | Console.Write("}, "); |
| 804 | } |
| 805 | Console.WriteLine("}"); |
| 806 | |
| 807 | // TODO: Validate received message |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 808 | var insane = new Insanity |
| 809 | { |
| 810 | UserMap = new Dictionary<Numberz, long> |
| 811 | { |
| 812 | [Numberz.FIVE] = 5000L |
| 813 | } |
| 814 | }; |
| 815 | var truck = new Xtruct |
| 816 | { |
| 817 | String_thing = "Truck", |
| 818 | Byte_thing = (sbyte)8, |
| 819 | I32_thing = 8, |
| 820 | I64_thing = 8 |
| 821 | }; |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 822 | insane.Xtructs = [ truck ]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 823 | Console.Write("testInsanity()"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 824 | var whoa = await client.testInsanity(insane, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 825 | Console.Write(" = {"); |
| 826 | foreach (var key in whoa.Keys) |
| 827 | { |
| 828 | var val = whoa[key]; |
| 829 | Console.Write(key + " => {"); |
| 830 | |
| 831 | foreach (var k2 in val.Keys) |
| 832 | { |
| 833 | var v2 = val[k2]; |
| 834 | |
| 835 | Console.Write(k2 + " => {"); |
| 836 | var userMap = v2.UserMap; |
| 837 | |
| 838 | Console.Write("{"); |
| 839 | if (userMap != null) |
| 840 | { |
| 841 | foreach (var k3 in userMap.Keys) |
| 842 | { |
| 843 | Console.Write(k3 + " => " + userMap[k3] + ", "); |
| 844 | } |
| 845 | } |
| 846 | else |
| 847 | { |
| 848 | Console.Write("null"); |
| 849 | } |
| 850 | Console.Write("}, "); |
| 851 | |
| 852 | var xtructs = v2.Xtructs; |
| 853 | |
| 854 | Console.Write("{"); |
| 855 | if (xtructs != null) |
| 856 | { |
| 857 | foreach (var x in xtructs) |
| 858 | { |
| 859 | Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, "); |
| 860 | } |
| 861 | } |
| 862 | else |
| 863 | { |
| 864 | Console.Write("null"); |
| 865 | } |
| 866 | Console.Write("}"); |
| 867 | |
| 868 | Console.Write("}, "); |
| 869 | } |
| 870 | Console.Write("}, "); |
| 871 | } |
| 872 | Console.WriteLine("}"); |
| 873 | |
| 874 | sbyte arg0 = 1; |
| 875 | var arg1 = 2; |
| 876 | var arg2 = long.MaxValue; |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 877 | var multiDict = new Dictionary<short, string> |
| 878 | { |
| 879 | [1] = "one" |
| 880 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 881 | |
| 882 | var tmpMultiDict = new List<string>(); |
| 883 | foreach (var pair in multiDict) |
| 884 | tmpMultiDict.Add(pair.Key +" => "+ pair.Value); |
| 885 | |
| 886 | var arg4 = Numberz.FIVE; |
| 887 | long arg5 = 5000000; |
| 888 | Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + ",{" + string.Join(",", tmpMultiDict) + "}," + arg4 + "," + arg5 + ")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 889 | var multiResponse = await client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 890 | Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing |
| 891 | + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n"); |
| 892 | |
| 893 | try |
| 894 | { |
| 895 | Console.WriteLine("testException(\"Xception\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 896 | await client.testException("Xception", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 897 | Console.WriteLine("*** FAILED ***"); |
| 898 | returnCode |= ErrorExceptions; |
| 899 | } |
| 900 | catch (Xception ex) |
| 901 | { |
| 902 | if (ex.ErrorCode != 1001 || ex.Message != "Xception") |
| 903 | { |
| 904 | Console.WriteLine("*** FAILED ***"); |
| 905 | returnCode |= ErrorExceptions; |
| 906 | } |
| 907 | } |
| 908 | catch (Exception ex) |
| 909 | { |
| 910 | Console.WriteLine("*** FAILED ***"); |
| 911 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 912 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 913 | } |
| 914 | try |
| 915 | { |
| 916 | Console.WriteLine("testException(\"TException\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 917 | await client.testException("TException", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 918 | Console.WriteLine("*** FAILED ***"); |
| 919 | returnCode |= ErrorExceptions; |
| 920 | } |
| 921 | catch (Thrift.TException) |
| 922 | { |
| 923 | // OK |
| 924 | } |
| 925 | catch (Exception ex) |
| 926 | { |
| 927 | Console.WriteLine("*** FAILED ***"); |
| 928 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 929 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 930 | } |
| 931 | try |
| 932 | { |
| 933 | Console.WriteLine("testException(\"ok\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 934 | await client.testException("ok", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 935 | // OK |
| 936 | } |
| 937 | catch (Exception ex) |
| 938 | { |
| 939 | Console.WriteLine("*** FAILED ***"); |
| 940 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 941 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | try |
| 945 | { |
| 946 | Console.WriteLine("testMultiException(\"Xception\", ...)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 947 | await client.testMultiException("Xception", "ignore", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 948 | Console.WriteLine("*** FAILED ***"); |
| 949 | returnCode |= ErrorExceptions; |
| 950 | } |
| 951 | catch (Xception ex) |
| 952 | { |
| 953 | if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception") |
| 954 | { |
| 955 | Console.WriteLine("*** FAILED ***"); |
| 956 | returnCode |= ErrorExceptions; |
| 957 | } |
| 958 | } |
| 959 | catch (Exception ex) |
| 960 | { |
| 961 | Console.WriteLine("*** FAILED ***"); |
| 962 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 963 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 964 | } |
| 965 | try |
| 966 | { |
| 967 | Console.WriteLine("testMultiException(\"Xception2\", ...)"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 968 | await client.testMultiException("Xception2", "ignore", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 969 | Console.WriteLine("*** FAILED ***"); |
| 970 | returnCode |= ErrorExceptions; |
| 971 | } |
| 972 | catch (Xception2 ex) |
| 973 | { |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame] | 974 | 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] | 975 | { |
| 976 | Console.WriteLine("*** FAILED ***"); |
| 977 | returnCode |= ErrorExceptions; |
| 978 | } |
| 979 | } |
| 980 | catch (Exception ex) |
| 981 | { |
| 982 | Console.WriteLine("*** FAILED ***"); |
| 983 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 984 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 985 | } |
| 986 | try |
| 987 | { |
| 988 | Console.WriteLine("testMultiException(\"success\", \"OK\")"); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 989 | if ("OK" != (await client.testMultiException("success", "OK", MakeTimeoutToken())).String_thing) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 990 | { |
| 991 | Console.WriteLine("*** FAILED ***"); |
| 992 | returnCode |= ErrorExceptions; |
| 993 | } |
| 994 | } |
| 995 | catch (Exception ex) |
| 996 | { |
| 997 | Console.WriteLine("*** FAILED ***"); |
| 998 | returnCode |= ErrorExceptions; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 999 | Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
Jens Geyer | b11f63c | 2019-03-14 21:12:38 +0100 | [diff] [blame] | 1002 | Console.WriteLine("Test Oneway(1)"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1003 | var sw = new Stopwatch(); |
| 1004 | sw.Start(); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 1005 | await client.testOneway(1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1006 | sw.Stop(); |
| 1007 | if (sw.ElapsedMilliseconds > 1000) |
| 1008 | { |
| 1009 | Console.WriteLine("*** FAILED ***"); |
| 1010 | returnCode |= ErrorBaseTypes; |
| 1011 | } |
| 1012 | |
| 1013 | Console.Write("Test Calltime()"); |
| 1014 | var times = 50; |
| 1015 | sw.Reset(); |
| 1016 | sw.Start(); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 1017 | var token = MakeTimeoutToken(20000); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1018 | for (var k = 0; k < times; ++k) |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 1019 | await client.testVoid(token); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1020 | sw.Stop(); |
| 1021 | Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times); |
| 1022 | return returnCode; |
| 1023 | } |
| 1024 | } |
| 1025 | } |