1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
unit TestSerializer.Tests;
(*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*)
interface
uses
Classes,
Windows,
SysUtils,
Generics.Collections,
Thrift,
Thrift.Exception,
Thrift.Socket,
Thrift.Transport,
Thrift.Protocol,
Thrift.Protocol.JSON,
Thrift.Protocol.Compact,
Thrift.Collections,
Thrift.Configuration,
Thrift.Server,
Thrift.Utils,
Thrift.Serializer,
Thrift.Stream,
Thrift.WinHTTP,
Thrift.TypeRegistry,
System_,
test.ExceptionStruct,
test.SimpleException,
DebugProtoTest;
{$TYPEINFO ON}
type
TFactoryPair = record
prot : IProtocolFactory;
trans : ITransportFactory;
end;
TTestSerializer = class //extends TestCase {
private type
TMethod = (
mt_Bytes,
mt_Stream
);
strict private
FProtocols : TList< TFactoryPair>;
procedure AddFactoryCombination( const aProto : IProtocolFactory; const aTrans : ITransportFactory);
class function UserFriendlyName( const factory : TFactoryPair) : string; overload;
class function UserFriendlyName( const method : TMethod) : string; overload;
class function Serialize(const input : IBase; const factory : TFactoryPair) : TBytes; overload;
class procedure Serialize(const input : IBase; const factory : TFactoryPair; const aStream : TStream); overload;
class procedure Deserialize( const input : TBytes; const target : IBase; const factory : TFactoryPair); overload;
class procedure Deserialize( const input : TStream; const target : IBase; const factory : TFactoryPair); overload;
class procedure ValidateReadToEnd( const input : TBytes; const serial : TDeserializer); overload;
class procedure ValidateReadToEnd( const input : TStream; const serial : TDeserializer); overload;
class function LengthOf( const bytes : TBytes) : Integer; overload; inline;
class function LengthOf( const bytes : IThriftBytes) : Integer; overload; inline;
class function DataPtrOf( const bytes : TBytes) : Pointer; overload; inline;
class function DataPtrOf( const bytes : IThriftBytes) : Pointer; overload; inline;
procedure Test_Serializer_Deserializer;
procedure Test_COM_Types;
procedure Test_ThriftBytesCTORs;
procedure Test_OneOfEach( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
procedure Test_CompactStruct( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
procedure Test_ExceptionStruct( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
procedure Test_SimpleException( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
public
constructor Create;
destructor Destroy; override;
procedure RunTests;
end;
implementation
const SERIALIZERDATA_DLL = 'SerializerData.dll';
function CreateOneOfEach : IOneOfEach; stdcall; external SERIALIZERDATA_DLL;
function CreateNesting : INesting; stdcall; external SERIALIZERDATA_DLL;
function CreateHolyMoley : IHolyMoley; stdcall; external SERIALIZERDATA_DLL;
function CreateCompactProtoTestStruct : ICompactProtoTestStruct; stdcall; external SERIALIZERDATA_DLL;
function CreateBatchGetResponse : IBatchGetResponse; stdcall; external SERIALIZERDATA_DLL;
function CreateSimpleException : IError; stdcall; external SERIALIZERDATA_DLL;
{ TTestSerializer }
constructor TTestSerializer.Create;
begin
inherited Create;
FProtocols := TList< TFactoryPair>.Create;
AddFactoryCombination( TBinaryProtocolImpl.TFactory.Create, nil);
AddFactoryCombination( TCompactProtocolImpl.TFactory.Create, nil);
AddFactoryCombination( TJSONProtocolImpl.TFactory.Create, nil);
AddFactoryCombination( TBinaryProtocolImpl.TFactory.Create, TFramedTransportImpl.TFactory.Create);
AddFactoryCombination( TCompactProtocolImpl.TFactory.Create, TFramedTransportImpl.TFactory.Create);
AddFactoryCombination( TJSONProtocolImpl.TFactory.Create, TFramedTransportImpl.TFactory.Create);
AddFactoryCombination( TBinaryProtocolImpl.TFactory.Create, TBufferedTransportImpl.TFactory.Create);
AddFactoryCombination( TCompactProtocolImpl.TFactory.Create, TBufferedTransportImpl.TFactory.Create);
AddFactoryCombination( TJSONProtocolImpl.TFactory.Create, TBufferedTransportImpl.TFactory.Create);
end;
destructor TTestSerializer.Destroy;
begin
try
FreeAndNil( FProtocols);
finally
inherited Destroy;
end;
end;
procedure TTestSerializer.AddFactoryCombination( const aProto : IProtocolFactory; const aTrans : ITransportFactory);
var rec : TFactoryPair;
begin
rec.prot := aProto;
rec.trans := aTrans;
FProtocols.Add( rec);
end;
class function TTestSerializer.LengthOf( const bytes : TBytes) : Integer;
begin
result := Length(bytes);
end;
class function TTestSerializer.LengthOf( const bytes : IThriftBytes) : Integer;
begin
if bytes <> nil
then result := bytes.Count
else result := 0;
end;
class function TTestSerializer.DataPtrOf( const bytes : TBytes) : Pointer;
begin
result := bytes;
end;
class function TTestSerializer.DataPtrOf( const bytes : IThriftBytes) : Pointer;
begin
if bytes <> nil
then result := bytes.QueryRawDataPtr
else result := nil;
end;
procedure TTestSerializer.Test_OneOfEach( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
var tested, correct : IOneOfEach;
bytes : TBytes;
i : Integer;
begin
// write
tested := CreateOneOfEach;
case method of
mt_Bytes: bytes := Serialize( tested, factory);
mt_Stream: begin
stream.Size := 0;
Serialize( tested, factory, stream);
end
else
ASSERT( FALSE);
end;
// init + read
tested := TOneOfEachImpl.Create;
case method of
mt_Bytes: Deserialize( bytes, tested, factory);
mt_Stream: begin
stream.Position := 0;
Deserialize( stream, tested, factory);
end
else
ASSERT( FALSE);
end;
// check
correct := CreateOneOfEach;
ASSERT( tested.Im_true = correct.Im_true);
ASSERT( tested.Im_false = correct.Im_false);
ASSERT( tested.A_bite = correct.A_bite);
ASSERT( tested.Integer16 = correct.Integer16);
ASSERT( tested.Integer32 = correct.Integer32);
ASSERT( tested.Integer64 = correct.Integer64);
ASSERT( Abs( tested.Double_precision - correct.Double_precision) < 1E-12);
ASSERT( tested.Some_characters = correct.Some_characters);
ASSERT( tested.Zomg_unicode = correct.Zomg_unicode);
ASSERT( tested.Rfc4122_uuid = correct.Rfc4122_uuid);
ASSERT( tested.What_who = correct.What_who);
ASSERT( LengthOf(tested.Base64) = LengthOf(correct.Base64));
ASSERT( CompareMem( DataPtrOf(tested.Base64), DataPtrOf(correct.Base64), LengthOf(correct.Base64)));
ASSERT( tested.Byte_list.Count = correct.Byte_list.Count);
for i := 0 to tested.Byte_list.Count-1
do ASSERT( tested.Byte_list[i] = correct.Byte_list[i]);
ASSERT( tested.I16_list.Count = correct.I16_list.Count);
for i := 0 to tested.I16_list.Count-1
do ASSERT( tested.I16_list[i] = correct.I16_list[i]);
ASSERT( tested.I64_list.Count = correct.I64_list.Count);
for i := 0 to tested.I64_list.Count-1
do ASSERT( tested.I64_list[i] = correct.I64_list[i]);
end;
procedure TTestSerializer.Test_CompactStruct( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
var tested, correct : ICompactProtoTestStruct;
bytes : TBytes;
begin
// write
tested := CreateCompactProtoTestStruct;
case method of
mt_Bytes: bytes := Serialize( tested, factory);
mt_Stream: begin
stream.Size := 0;
Serialize( tested, factory, stream);
end
else
ASSERT( FALSE);
end;
// init + read
correct := TCompactProtoTestStructImpl.Create;
case method of
mt_Bytes: Deserialize( bytes, tested, factory);
mt_Stream: begin
stream.Position := 0;
Deserialize( stream, tested, factory);
end
else
ASSERT( FALSE);
end;
// check
correct := CreateCompactProtoTestStruct;
ASSERT( correct.Field500 = tested.Field500);
ASSERT( correct.Field5000 = tested.Field5000);
ASSERT( correct.Field20000 = tested.Field20000);
end;
procedure TTestSerializer.Test_ExceptionStruct( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
var tested, correct : IBatchGetResponse;
bytes : TBytes;
corrDP, testDP : TPair<WideString, IGetRequest>;
corrEP, testEP : TPair<WideString, ISomeException>;
begin
// write
tested := CreateBatchGetResponse;
case method of
mt_Bytes: bytes := Serialize( tested, factory);
mt_Stream: begin
stream.Size := 0;
Serialize( tested, factory, stream);
end
else
ASSERT( FALSE);
end;
// init + read
correct := TBatchGetResponseImpl.Create;
case method of
mt_Bytes: Deserialize( bytes, tested, factory);
mt_Stream: begin
stream.Position := 0;
Deserialize( stream, tested, factory);
end
else
ASSERT( FALSE);
end;
// check
correct := CreateBatchGetResponse;
// rewrite the following test if not
ASSERT( tested.Responses.Count = 1);
ASSERT( correct.Responses.Count = tested.Responses.Count);
for corrDP in correct.Responses do begin
for testDP in tested.Responses do begin
ASSERT( corrDP.Key = testDP.Key);
ASSERT( corrDP.Value.Id = testDP.Value.Id);
ASSERT( corrDP.Value.Data.Count = testDP.Value.Data.Count);
end;
end;
// rewrite the following test if not
ASSERT( tested.Errors.Count = 1);
ASSERT( correct.Errors.Count = tested.Errors.Count);
for corrEP in correct.Errors do begin
for testEP in tested.Errors do begin
ASSERT( corrEP.Key = testEP.Key);
ASSERT( corrEP.Value.Error = testEP.Value.Error);
end;
end;
end;
procedure TTestSerializer.Test_SimpleException( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
var tested, correct : IError;
bytes : TBytes;
begin
// write
tested := CreateSimpleException;
case method of
mt_Bytes: bytes := Serialize( tested, factory);
mt_Stream: begin
stream.Size := 0;
Serialize( tested, factory, stream);
end
else
ASSERT( FALSE);
end;
// init + read
correct := TErrorImpl.Create;
case method of
mt_Bytes: Deserialize( bytes, tested, factory);
mt_Stream: begin
stream.Position := 0;
Deserialize( stream, tested, factory);
end
else
ASSERT( FALSE);
end;
// check
correct := CreateSimpleException;
while correct <> nil do begin
// validate
ASSERT( correct.ErrorCode = tested.ErrorCode);
ASSERT( IsEqualGUID( correct.ExceptionData, tested.ExceptionData));
// iterate
correct := correct.InnerException;
tested := tested.InnerException;
ASSERT( (tested <> nil) xor (correct = nil)); // both or none
end;
end;
procedure TTestSerializer.Test_Serializer_Deserializer;
var factory : TFactoryPair;
stream : TFileStream;
method : TMethod;
begin
stream := TFileStream.Create( 'TestSerializer.dat', fmCreate);
try
for method in [Low(TMethod)..High(TMethod)] do begin
Writeln( UserFriendlyName(method));
for factory in FProtocols do begin
Writeln('- '+UserFriendlyName(factory));
Test_OneOfEach( method, factory, stream);
Test_CompactStruct( method, factory, stream);
Test_ExceptionStruct( method, factory, stream);
Test_SimpleException( method, factory, stream);
end;
Writeln;
end;
finally
stream.Free;
end;
end;
class function TTestSerializer.UserFriendlyName( const factory : TFactoryPair) : string;
begin
result := Copy( (factory.prot as TObject).ClassName, 2, MAXINT);
if factory.trans <> nil
then result := Copy( (factory.trans as TObject).ClassName, 2, MAXINT) +' '+ result;
result := StringReplace( result, 'Impl', '', [rfReplaceAll]);
result := StringReplace( result, 'Transport.TFactory', '', [rfReplaceAll]);
result := StringReplace( result, 'Protocol.TFactory', '', [rfReplaceAll]);
end;
class function TTestSerializer.UserFriendlyName( const method : TMethod) : string;
const NAMES : array[TMethod] of string = ('TBytes','Stream');
begin
result := NAMES[method];
end;
procedure TTestSerializer.Test_COM_Types;
var tested : IOneOfEach;
begin
{$IF cDebugProtoTest_Option_COM_types}
ASSERT( SizeOf(TSomeEnum) = SizeOf(Int32)); // -> MINENUMSIZE 4
// try to set values that allocate memory
tested := CreateOneOfEach;
tested.Zomg_unicode := 'This is a test';
tested.Base64 := TThriftBytesImpl.Create( TEncoding.UTF8.GetBytes('abc'));
{$IFEND}
end;
procedure TTestSerializer.Test_ThriftBytesCTORs;
var one, two : IThriftBytes;
bytes : TBytes;
sAscii : AnsiString;
begin
sAscii := 'ABC/xzy';
bytes := TEncoding.ASCII.GetBytes(string(sAscii));
one := TThriftBytesImpl.Create( PAnsiChar(sAscii), Length(sAscii));
two := TThriftBytesImpl.Create( bytes, TRUE);
ASSERT( one.Count = two.Count);
ASSERT( CompareMem( one.QueryRawDataPtr, two.QueryRawDataPtr, one.Count));
end;
procedure TTestSerializer.RunTests;
begin
try
Test_Serializer_Deserializer;
Test_COM_Types;
Test_ThriftBytesCTORs;
except
on e:Exception do begin
Writeln( e.ClassName+': '+ e.Message);
Write('Hit ENTER to close ... '); Readln;
end;
end;
end;
class function TTestSerializer.Serialize(const input : IBase; const factory : TFactoryPair) : TBytes;
var serial : TSerializer;
config : IThriftConfiguration;
begin
config := TThriftConfigurationImpl.Create;
//config.MaxMessageSize := 0; // we don't read anything here
serial := TSerializer.Create( factory.prot, factory.trans, config);
try
result := serial.Serialize( input);
finally
serial.Free;
end;
end;
class procedure TTestSerializer.Serialize(const input : IBase; const factory : TFactoryPair; const aStream : TStream);
var serial : TSerializer;
config : IThriftConfiguration;
begin
config := TThriftConfigurationImpl.Create;
//config.MaxMessageSize := 0; // we don't read anything here
serial := TSerializer.Create( factory.prot, factory.trans, config);
try
serial.Serialize( input, aStream);
finally
serial.Free;
end;
end;
class procedure TTestSerializer.Deserialize( const input : TBytes; const target : IBase; const factory : TFactoryPair);
var serial : TDeserializer;
config : IThriftConfiguration;
begin
config := TThriftConfigurationImpl.Create;
config.MaxMessageSize := Length(input);
serial := TDeserializer.Create( factory.prot, factory.trans, config);
try
serial.Deserialize( input, target);
ValidateReadToEnd( input, serial);
finally
serial.Free;
end;
end;
class procedure TTestSerializer.Deserialize( const input : TStream; const target : IBase; const factory : TFactoryPair);
var serial : TDeserializer;
config : IThriftConfiguration;
begin
config := TThriftConfigurationImpl.Create;
config.MaxMessageSize := input.Size;
serial := TDeserializer.Create( factory.prot, factory.trans, config);
try
serial.Deserialize( input, target);
ValidateReadToEnd( input, serial);
finally
serial.Free;
end;
end;
class procedure TTestSerializer.ValidateReadToEnd( const input : TBytes; const serial : TDeserializer);
// we should not have any more byte to read
var dummy : IBase;
begin
try
dummy := TOneOfEachImpl.Create;
serial.Deserialize( input, dummy);
raise EInOutError.Create('Expected exception not thrown?');
except
on e:TTransportExceptionEndOfFile do {expected};
on e:Exception do raise; // unexpected
end;
end;
class procedure TTestSerializer.ValidateReadToEnd( const input : TStream; const serial : TDeserializer);
// we should not have any more byte to read
var dummy : IBase;
begin
try
input.Position := 0;
dummy := TOneOfEachImpl.Create;
serial.Deserialize( input, dummy);
raise EInOutError.Create('Expected exception not thrown?');
except
on e:TTransportExceptionEndOfFile do {expected};
on e:Exception do raise; // unexpected
end;
end;
end.
|