blob: dd9646ad914d4c5ec70ed04b2f576a1b7ee82ddd [file] [log] [blame]
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001// Licensed to the Apache Software Foundation(ASF) under one
2// or more contributor license agreements.See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18using System;
19using System.IO;
20using System.Text;
21using System.Threading.Tasks;
22using KellermanSoftware.CompareNetObjects;
23using Microsoft.VisualStudio.TestTools.UnitTesting;
24using Thrift.Protocol;
25using Thrift.Protocol.Entities;
26using Thrift.Transport.Client;
27
Jens Geyer98a23252022-01-09 16:50:52 +010028#pragma warning disable IDE0063 // using
29
Jens Geyeraa0c8b32019-01-28 23:27:45 +010030namespace Thrift.IntegrationTests.Protocols
31{
32 [TestClass]
33 public class ProtocolsOperationsTests
34 {
Jens Geyer98a23252022-01-09 16:50:52 +010035 private readonly CompareLogic _compareLogic = new();
36 private static readonly TConfiguration Configuration = new();
Jens Geyeraa0c8b32019-01-28 23:27:45 +010037
38 [DataTestMethod]
39 [DataRow(typeof(TBinaryProtocol), TMessageType.Call)]
40 [DataRow(typeof(TBinaryProtocol), TMessageType.Exception)]
41 [DataRow(typeof(TBinaryProtocol), TMessageType.Oneway)]
42 [DataRow(typeof(TBinaryProtocol), TMessageType.Reply)]
43 [DataRow(typeof(TCompactProtocol), TMessageType.Call)]
44 [DataRow(typeof(TCompactProtocol), TMessageType.Exception)]
45 [DataRow(typeof(TCompactProtocol), TMessageType.Oneway)]
46 [DataRow(typeof(TCompactProtocol), TMessageType.Reply)]
47 [DataRow(typeof(TJsonProtocol), TMessageType.Call)]
48 [DataRow(typeof(TJsonProtocol), TMessageType.Exception)]
49 [DataRow(typeof(TJsonProtocol), TMessageType.Oneway)]
50 [DataRow(typeof(TJsonProtocol), TMessageType.Reply)]
51 public async Task WriteReadMessage_Test(Type protocolType, TMessageType messageType)
52 {
53 var expected = new TMessage(nameof(TMessage), messageType, 1);
54
55 try
56 {
57 var tuple = GetProtocolInstance(protocolType);
58 using (var stream = tuple.Item1)
59 {
60 var protocol = tuple.Item2;
61
Jens Geyerdce22992020-05-16 23:02:27 +020062 await protocol.WriteMessageBeginAsync(expected, default);
63 await protocol.WriteMessageEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +010064
65 stream.Seek(0, SeekOrigin.Begin);
66
Jens Geyerdce22992020-05-16 23:02:27 +020067 var actualMessage = await protocol.ReadMessageBeginAsync(default);
68 await protocol.ReadMessageEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +010069
70 var result = _compareLogic.Compare(expected, actualMessage);
71 Assert.IsTrue(result.AreEqual, result.DifferencesString);
72 }
73 }
74 catch (Exception e)
75 {
76 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
77 }
78 }
79
80 [DataTestMethod]
81 [DataRow(typeof(TBinaryProtocol))]
82 [DataRow(typeof(TCompactProtocol))]
83 [DataRow(typeof(TJsonProtocol))]
84 [ExpectedException(typeof(Exception))]
85 public async Task WriteReadStruct_Test(Type protocolType)
86 {
87 var expected = new TStruct(nameof(TStruct));
88
89 try
90 {
91 var tuple = GetProtocolInstance(protocolType);
92 using (var stream = tuple.Item1)
93 {
94 var protocol = tuple.Item2;
95
Jens Geyerdce22992020-05-16 23:02:27 +020096 await protocol.WriteStructBeginAsync(expected, default);
97 await protocol.WriteStructEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +010098
99 stream?.Seek(0, SeekOrigin.Begin);
100
Jens Geyerdce22992020-05-16 23:02:27 +0200101 var actual = await protocol.ReadStructBeginAsync(default);
102 await protocol.ReadStructEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100103
104 var result = _compareLogic.Compare(expected, actual);
105 Assert.IsTrue(result.AreEqual, result.DifferencesString);
106 }
107
108 }
109 catch (Exception e)
110 {
111 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
112 }
113 }
114
115 [DataTestMethod]
116 [DataRow(typeof(TBinaryProtocol))]
117 [DataRow(typeof(TCompactProtocol))]
118 [DataRow(typeof(TJsonProtocol))]
119 [ExpectedException(typeof(Exception))]
120 public async Task WriteReadField_Test(Type protocolType)
121 {
122 var expected = new TField(nameof(TField), TType.String, 1);
123
124 try
125 {
126 var tuple = GetProtocolInstance(protocolType);
127 using (var stream = tuple.Item1)
128 {
129 var protocol = tuple.Item2;
130
Jens Geyerdce22992020-05-16 23:02:27 +0200131 await protocol.WriteFieldBeginAsync(expected, default);
132 await protocol.WriteFieldEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100133
134 stream?.Seek(0, SeekOrigin.Begin);
135
Jens Geyerdce22992020-05-16 23:02:27 +0200136 var actual = await protocol.ReadFieldBeginAsync(default);
137 await protocol.ReadFieldEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100138
139 var result = _compareLogic.Compare(expected, actual);
140 Assert.IsTrue(result.AreEqual, result.DifferencesString);
141 }
142 }
143 catch (Exception e)
144 {
145 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
146 }
147 }
148
149 [DataTestMethod]
150 [DataRow(typeof(TBinaryProtocol))]
151 [DataRow(typeof(TCompactProtocol))]
152 [DataRow(typeof(TJsonProtocol))]
153 public async Task WriteReadMap_Test(Type protocolType)
154 {
155 var expected = new TMap(TType.String, TType.String, 1);
156
157 try
158 {
159 var tuple = GetProtocolInstance(protocolType);
160 using (var stream = tuple.Item1)
161 {
162 var protocol = tuple.Item2;
163
Jens Geyerdce22992020-05-16 23:02:27 +0200164 await protocol.WriteMapBeginAsync(expected, default);
165 await protocol.WriteMapEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100166
167 stream?.Seek(0, SeekOrigin.Begin);
168
Jens Geyerdce22992020-05-16 23:02:27 +0200169 var actual = await protocol.ReadMapBeginAsync(default);
170 await protocol.ReadMapEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100171
172 var result = _compareLogic.Compare(expected, actual);
173 Assert.IsTrue(result.AreEqual, result.DifferencesString);
174 }
175 }
176 catch (Exception e)
177 {
178 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
179 }
180
181 }
182
183 [DataTestMethod]
184 [DataRow(typeof(TBinaryProtocol))]
185 [DataRow(typeof(TCompactProtocol))]
186 [DataRow(typeof(TJsonProtocol))]
187 public async Task WriteReadList_Test(Type protocolType)
188 {
189 var expected = new TList(TType.String, 1);
190
191 try
192 {
193 var tuple = GetProtocolInstance(protocolType);
194 using (var stream = tuple.Item1)
195 {
196 var protocol = tuple.Item2;
197
Jens Geyerdce22992020-05-16 23:02:27 +0200198 await protocol.WriteListBeginAsync(expected, default);
199 await protocol.WriteListEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100200
201 stream?.Seek(0, SeekOrigin.Begin);
202
Jens Geyerdce22992020-05-16 23:02:27 +0200203 var actual = await protocol.ReadListBeginAsync(default);
204 await protocol.ReadListEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100205
206 var result = _compareLogic.Compare(expected, actual);
207 Assert.IsTrue(result.AreEqual, result.DifferencesString);
208 }
209 }
210 catch (Exception e)
211 {
212 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
213 }
214 }
215
216 [DataTestMethod]
217 [DataRow(typeof(TBinaryProtocol))]
218 [DataRow(typeof(TCompactProtocol))]
219 [DataRow(typeof(TJsonProtocol))]
220 public async Task WriteReadSet_Test(Type protocolType)
221 {
222 var expected = new TSet(TType.String, 1);
223
224 try
225 {
226 var tuple = GetProtocolInstance(protocolType);
227 using (var stream = tuple.Item1)
228 {
229 var protocol = tuple.Item2;
230
Jens Geyerdce22992020-05-16 23:02:27 +0200231 await protocol.WriteSetBeginAsync(expected, default);
232 await protocol.WriteSetEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100233
234 stream?.Seek(0, SeekOrigin.Begin);
235
Jens Geyerdce22992020-05-16 23:02:27 +0200236 var actual = await protocol.ReadSetBeginAsync(default);
237 await protocol.ReadSetEndAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100238
239 var result = _compareLogic.Compare(expected, actual);
240 Assert.IsTrue(result.AreEqual, result.DifferencesString);
241 }
242 }
243 catch (Exception e)
244 {
245 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
246 }
247 }
248
249 [DataTestMethod]
250 [DataRow(typeof(TBinaryProtocol))]
251 [DataRow(typeof(TCompactProtocol))]
252 [DataRow(typeof(TJsonProtocol))]
253 public async Task WriteReadBool_Test(Type protocolType)
254 {
255 var expected = true;
256
257 try
258 {
259 var tuple = GetProtocolInstance(protocolType);
260 using (var stream = tuple.Item1)
261 {
262 var protocol = tuple.Item2;
263
Jens Geyerdce22992020-05-16 23:02:27 +0200264 await protocol.WriteBoolAsync(expected, default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100265
266 stream?.Seek(0, SeekOrigin.Begin);
267
Jens Geyerdce22992020-05-16 23:02:27 +0200268 var actual = await protocol.ReadBoolAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100269
270 var result = _compareLogic.Compare(expected, actual);
271 Assert.IsTrue(result.AreEqual, result.DifferencesString);
272 }
273 }
274 catch (Exception e)
275 {
276 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
277 }
278 }
279
280 [DataTestMethod]
281 [DataRow(typeof(TBinaryProtocol))]
282 [DataRow(typeof(TCompactProtocol))]
283 [DataRow(typeof(TJsonProtocol))]
284 public async Task WriteReadByte_Test(Type protocolType)
285 {
286 var expected = sbyte.MaxValue;
287
288 try
289 {
290 var tuple = GetProtocolInstance(protocolType);
291 using (var stream = tuple.Item1)
292 {
293 var protocol = tuple.Item2;
294
Jens Geyerdce22992020-05-16 23:02:27 +0200295 await protocol.WriteByteAsync(expected, default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100296
297 stream?.Seek(0, SeekOrigin.Begin);
298
Jens Geyerdce22992020-05-16 23:02:27 +0200299 var actual = await protocol.ReadByteAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100300
301 var result = _compareLogic.Compare(expected, actual);
302 Assert.IsTrue(result.AreEqual, result.DifferencesString);
303 }
304 }
305 catch (Exception e)
306 {
307 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
308 }
309 }
310
311 [DataTestMethod]
312 [DataRow(typeof(TBinaryProtocol))]
313 [DataRow(typeof(TCompactProtocol))]
314 [DataRow(typeof(TJsonProtocol))]
315 public async Task WriteReadI16_Test(Type protocolType)
316 {
317 var expected = short.MaxValue;
318
319 try
320 {
321 var tuple = GetProtocolInstance(protocolType);
322 using (var stream = tuple.Item1)
323 {
324 var protocol = tuple.Item2;
325
Jens Geyerdce22992020-05-16 23:02:27 +0200326 await protocol.WriteI16Async(expected, default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100327
328 stream?.Seek(0, SeekOrigin.Begin);
329
Jens Geyerdce22992020-05-16 23:02:27 +0200330 var actual = await protocol.ReadI16Async(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100331
332 var result = _compareLogic.Compare(expected, actual);
333 Assert.IsTrue(result.AreEqual, result.DifferencesString);
334 }
335 }
336 catch (Exception e)
337 {
338 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
339 }
340 }
341
342 [DataTestMethod]
343 [DataRow(typeof(TBinaryProtocol))]
344 [DataRow(typeof(TCompactProtocol))]
345 [DataRow(typeof(TJsonProtocol))]
346 public async Task WriteReadI32_Test(Type protocolType)
347 {
348 var expected = int.MaxValue;
349
350 try
351 {
352 var tuple = GetProtocolInstance(protocolType);
353 using (var stream = tuple.Item1)
354 {
355 var protocol = tuple.Item2;
356
Jens Geyerdce22992020-05-16 23:02:27 +0200357 await protocol.WriteI32Async(expected, default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100358
359 stream?.Seek(0, SeekOrigin.Begin);
360
Jens Geyerdce22992020-05-16 23:02:27 +0200361 var actual = await protocol.ReadI32Async(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100362
363 var result = _compareLogic.Compare(expected, actual);
364 Assert.IsTrue(result.AreEqual, result.DifferencesString);
365 }
366 }
367 catch (Exception e)
368 {
369 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
370 }
371 }
372
373 [DataTestMethod]
374 [DataRow(typeof(TBinaryProtocol))]
375 [DataRow(typeof(TCompactProtocol))]
376 [DataRow(typeof(TJsonProtocol))]
377 public async Task WriteReadI64_Test(Type protocolType)
378 {
379 var expected = long.MaxValue;
380
381 try
382 {
383 var tuple = GetProtocolInstance(protocolType);
384 using (var stream = tuple.Item1)
385 {
386 var protocol = tuple.Item2;
387
Jens Geyerdce22992020-05-16 23:02:27 +0200388 await protocol.WriteI64Async(expected, default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100389
390 stream?.Seek(0, SeekOrigin.Begin);
391
Jens Geyerdce22992020-05-16 23:02:27 +0200392 var actual = await protocol.ReadI64Async(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100393
394 var result = _compareLogic.Compare(expected, actual);
395 Assert.IsTrue(result.AreEqual, result.DifferencesString);
396 }
397 }
398 catch (Exception e)
399 {
400 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
401 }
402 }
403
404 [DataTestMethod]
405 [DataRow(typeof(TBinaryProtocol))]
406 [DataRow(typeof(TCompactProtocol))]
407 [DataRow(typeof(TJsonProtocol))]
408 public async Task WriteReadDouble_Test(Type protocolType)
409 {
410 var expected = double.MaxValue;
411
412 try
413 {
414 var tuple = GetProtocolInstance(protocolType);
415 using (var stream = tuple.Item1)
416 {
417 var protocol = tuple.Item2;
418
Jens Geyerdce22992020-05-16 23:02:27 +0200419 await protocol.WriteDoubleAsync(expected, default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100420
421 stream?.Seek(0, SeekOrigin.Begin);
422
Jens Geyerdce22992020-05-16 23:02:27 +0200423 var actual = await protocol.ReadDoubleAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100424
425 var result = _compareLogic.Compare(expected, actual);
426 Assert.IsTrue(result.AreEqual, result.DifferencesString);
427 }
428 }
429 catch (Exception e)
430 {
431 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
432 }
433 }
434
435 [DataTestMethod]
436 [DataRow(typeof(TBinaryProtocol))]
437 [DataRow(typeof(TCompactProtocol))]
438 [DataRow(typeof(TJsonProtocol))]
439 public async Task WriteReadString_Test(Type protocolType)
440 {
441 var expected = nameof(String);
442
443 try
444 {
445 var tuple = GetProtocolInstance(protocolType);
446 using (var stream = tuple.Item1)
447 {
448 var protocol = tuple.Item2;
449
Jens Geyerdce22992020-05-16 23:02:27 +0200450 await protocol.WriteStringAsync(expected, default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100451
452 stream?.Seek(0, SeekOrigin.Begin);
453
Jens Geyerdce22992020-05-16 23:02:27 +0200454 var actual = await protocol.ReadStringAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100455
456 var result = _compareLogic.Compare(expected, actual);
457 Assert.IsTrue(result.AreEqual, result.DifferencesString);
458 }
459 }
460 catch (Exception e)
461 {
462 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
463 }
464 }
465
466 [DataTestMethod]
467 [DataRow(typeof(TBinaryProtocol))]
468 [DataRow(typeof(TCompactProtocol))]
469 [DataRow(typeof(TJsonProtocol))]
470 public async Task WriteReadBinary_Test(Type protocolType)
471 {
472 var expected = Encoding.UTF8.GetBytes(nameof(String));
473
474 try
475 {
476 var tuple = GetProtocolInstance(protocolType);
477 using (var stream = tuple.Item1)
478 {
479 var protocol = tuple.Item2;
480
Jens Geyerdce22992020-05-16 23:02:27 +0200481 await protocol.WriteBinaryAsync(expected, default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100482
483 stream?.Seek(0, SeekOrigin.Begin);
484
Jens Geyerdce22992020-05-16 23:02:27 +0200485 var actual = await protocol.ReadBinaryAsync(default);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100486
487 var result = _compareLogic.Compare(expected, actual);
488 Assert.IsTrue(result.AreEqual, result.DifferencesString);
489 }
490 }
491 catch (Exception e)
492 {
493 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
494 }
495 }
496
497 private static Tuple<Stream, TProtocol> GetProtocolInstance(Type protocolType)
498 {
499 var memoryStream = new MemoryStream();
Jens Geyereacd1d42019-11-20 19:03:14 +0100500 var streamClientTransport = new TStreamTransport(memoryStream, memoryStream,Configuration);
Jens Geyer98a23252022-01-09 16:50:52 +0100501 if( Activator.CreateInstance(protocolType, streamClientTransport) is TProtocol protocol)
502 return new Tuple<Stream, TProtocol>(memoryStream, protocol);
503 throw new Exception("Unexpected");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100504 }
505 }
506}