Protocol conformity and serialization tests for UUIDs
diff --git a/lib/delphi/src/Thrift.Serializer.pas b/lib/delphi/src/Thrift.Serializer.pas
index 8ee8a35..c354a37 100644
--- a/lib/delphi/src/Thrift.Serializer.pas
+++ b/lib/delphi/src/Thrift.Serializer.pas
@@ -66,7 +66,7 @@
   public
     constructor Create( const aProtFact  : IProtocolFactory = nil;    // defaults to TBinaryProtocol
                         const aTransFact : ITransportFactory = nil;
-                        const aConfig   : IThriftConfiguration = nil);
+                        const aConfig    : IThriftConfiguration = nil);
 
     // DTOR
     destructor Destroy;  override;
@@ -74,6 +74,11 @@
     // Deserialize the Thrift object data.
     procedure Deserialize( const input : TBytes; const target : IBase);  overload;
     procedure Deserialize( const input : TStream; const target : IBase);  overload;
+
+    // helper
+    property  Protocol  : IProtocol read FProtocol;
+    property  Transport : ITransport read FTransport;
+    property  Stream    : TThriftMemoryStream read FStream;
   end;
 
 
@@ -164,14 +169,14 @@
 
 constructor TDeserializer.Create( const aProtFact  : IProtocolFactory;
                                   const aTransFact : ITransportFactory;
-                                  const aConfig   : IThriftConfiguration);
+                                  const aConfig    : IThriftConfiguration);
 var adapter : IThriftStream;
     protfact : IProtocolFactory;
 begin
   inherited Create;
 
-  FStream    := TThriftMemoryStream.Create;
-  adapter    := TThriftStreamAdapterDelphi.Create( FStream, FALSE);
+  FStream := TThriftMemoryStream.Create;
+  adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE);
 
   FTransport := TStreamTransportImpl.Create( adapter, nil, aConfig);
   if aTransfact <> nil then FTransport := aTransfact.GetTransport( FTransport);
@@ -205,8 +210,11 @@
   try
     iBytes := Length(input);
     FStream.Size := iBytes;
-    if iBytes > 0
-    then Move( input[0], FStream.Memory^, iBytes);
+    if iBytes > 0 then begin
+      Move( input[0], FStream.Memory^, iBytes);
+      Transport.ResetMessageSizeAndConsumedBytes();  // size has changed
+      Transport.UpdateKnownMessageSize(iBytes);
+    end;
 
     target.Read( FProtocol);
   finally
@@ -221,9 +229,15 @@
 var before : Int64;
 begin
   try
-    before := FStream.Position;
-    FStream.CopyFrom( input, COPY_ENTIRE_STREAM);
-    FStream.Position := before;
+    if Assigned(input) then begin
+      before := FStream.Position;
+      ASSERT( before = 0);
+      FStream.CopyFrom( input, COPY_ENTIRE_STREAM);
+      FStream.Position := before;
+      Transport.ResetMessageSizeAndConsumedBytes();  // size has changed
+      Transport.UpdateKnownMessageSize(FStream.Size);
+    end;
+
     target.Read( FProtocol);
   finally
     FStream.Size := 0;  // free any allocated memory
diff --git a/lib/delphi/test/serializer/TestSerializer.Tests.pas b/lib/delphi/test/serializer/TestSerializer.Tests.pas
index 466fb26..7f5b829 100644
--- a/lib/delphi/test/serializer/TestSerializer.Tests.pas
+++ b/lib/delphi/test/serializer/TestSerializer.Tests.pas
@@ -49,7 +49,7 @@
 
 type
   TFactoryPair = record
-    prot : IProtocolFactory;
+    proto : IProtocolFactory;
     trans : ITransportFactory;
   end;
 
@@ -72,8 +72,9 @@
     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 procedure Deserialize( const input : TBytes; out target : TGuid; const factory : TFactoryPair);  overload;
+
+    class procedure ValidateReadToEnd( const serial : TDeserializer);  overload;
 
     class function LengthOf( const bytes : TBytes) : Integer; overload; inline;
     class function LengthOf( const bytes : IThriftBytes) : Integer; overload; inline;
@@ -90,6 +91,9 @@
     procedure Test_ExceptionStruct( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
     procedure Test_SimpleException( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
 
+    procedure Test_ProtocolConformity( const factory : TFactoryPair; const stream : TFileStream);
+    procedure Test_UuidDeserialization( const factory : TFactoryPair; const stream : TFileStream);
+
   public
     constructor Create;
     destructor Destroy;  override;
@@ -143,7 +147,7 @@
 procedure TTestSerializer.AddFactoryCombination( const aProto : IProtocolFactory; const aTrans : ITransportFactory);
 var rec : TFactoryPair;
 begin
-  rec.prot  := aProto;
+  rec.proto := aProto;
   rec.trans := aTrans;
   FProtocols.Add( rec);
 end;
@@ -177,6 +181,53 @@
 end;
 
 
+procedure TTestSerializer.Test_ProtocolConformity( const factory : TFactoryPair; const stream : TFileStream);
+begin
+  Test_UuidDeserialization( factory, stream);
+  // add more tests here
+end;
+
+
+procedure TTestSerializer.Test_UuidDeserialization( const factory : TFactoryPair; const stream : TFileStream);
+
+  function CreateGuidBytes : TBytes;
+  var obj : TObject;
+      i : Integer;
+  begin
+    obj := factory.proto as TObject;
+
+    if obj is TJSONProtocolImpl.TFactory then begin
+      result := TEncoding.UTF8.GetBytes('"00112233-4455-6677-8899-aabbccddeeff"');
+      Exit;
+    end;
+
+    if (obj is TBinaryProtocolImpl.TFactory)
+    or (obj is TCompactProtocolImpl.TFactory)
+    then begin
+      SetLength(result,16);
+      for i := 0 to Length(result)-1 do result[i] := (i * $10) + i;
+      Exit;
+    end;
+
+    raise Exception.Create('Unhandled case');
+  end;
+
+
+var tested, correct : TGuid;
+    bytes   : TBytes;
+begin
+  // write
+  bytes := CreateGuidBytes();
+
+  // init + read
+  Deserialize( bytes, tested, factory);
+
+  // check
+  correct := TGuid.Create('{00112233-4455-6677-8899-aabbccddeeff}');
+  ASSERT( tested = correct);
+end;
+
+
 procedure TTestSerializer.Test_OneOfEach( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
 var tested, correct : IOneOfEach;
     bytes   : TBytes;
@@ -385,6 +436,11 @@
       for factory in FProtocols do begin
         Writeln('- '+UserFriendlyName(factory));
 
+        // protocol conformity tests
+        if (method = TMethod.mt_Bytes) and (factory.trans = nil)
+        then Test_ProtocolConformity( factory, stream);
+
+        // normal objects
         Test_OneOfEach(       method, factory, stream);
         Test_CompactStruct(   method, factory, stream);
         Test_ExceptionStruct( method, factory, stream);
@@ -402,7 +458,7 @@
 
 class function TTestSerializer.UserFriendlyName( const factory : TFactoryPair) : string;
 begin
-  result := Copy( (factory.prot as TObject).ClassName, 2, MAXINT);
+  result := Copy( (factory.proto as TObject).ClassName, 2, MAXINT);
 
   if factory.trans <> nil
   then result := Copy( (factory.trans as TObject).ClassName, 2, MAXINT) +' '+ result;
@@ -472,7 +528,7 @@
   config := TThriftConfigurationImpl.Create;
   //config.MaxMessageSize := 0;   // we don't read anything here
 
-  serial := TSerializer.Create( factory.prot, factory.trans, config);
+  serial := TSerializer.Create( factory.proto, factory.trans, config);
   try
     result := serial.Serialize( input);
   finally
@@ -488,7 +544,7 @@
   config := TThriftConfigurationImpl.Create;
   //config.MaxMessageSize := 0;   // we don't read anything here
 
-  serial := TSerializer.Create( factory.prot, factory.trans, config);
+  serial := TSerializer.Create( factory.proto, factory.trans, config);
   try
     serial.Serialize( input, aStream);
   finally
@@ -504,10 +560,10 @@
   config := TThriftConfigurationImpl.Create;
   config.MaxMessageSize := Length(input);
 
-  serial := TDeserializer.Create( factory.prot, factory.trans, config);
+  serial := TDeserializer.Create( factory.proto, factory.trans, config);
   try
     serial.Deserialize( input, target);
-    ValidateReadToEnd( input, serial);
+    ValidateReadToEnd( serial);
   finally
     serial.Free;
   end;
@@ -521,44 +577,49 @@
   config := TThriftConfigurationImpl.Create;
   config.MaxMessageSize := input.Size;
 
-  serial := TDeserializer.Create( factory.prot, factory.trans, config);
+  serial := TDeserializer.Create( factory.proto, factory.trans, config);
   try
     serial.Deserialize( input, target);
-    ValidateReadToEnd( input, serial);
+    ValidateReadToEnd( serial);
   finally
     serial.Free;
   end;
 end;
 
 
-class procedure TTestSerializer.ValidateReadToEnd( const input : TBytes; const serial : TDeserializer);
+class procedure TTestSerializer.Deserialize( const input : TBytes; out target : TGuid; const factory : TFactoryPair);
+var serial : TDeserializer;
+    config : IThriftConfiguration;
+begin
+  config := TThriftConfigurationImpl.Create;
+  config.MaxMessageSize := Length(input);
+
+  serial := TDeserializer.Create( factory.proto, factory.trans, config);
+  try
+    serial.Stream.Write(input[0], Length(input));
+    serial.Stream.Position := 0;
+    serial.Transport.ResetMessageSizeAndConsumedBytes();  // size has changed
+
+    target := serial.Protocol.ReadUuid;
+  finally
+    serial.Free;
+  end;
+end;
+
+
+class procedure TTestSerializer.ValidateReadToEnd( const serial : TDeserializer);
 // we should not have any more byte to read
 var dummy : IBase;
 begin
   try
     dummy := TOneOfEachImpl.Create;
-    serial.Deserialize( input, dummy);
+    serial.Deserialize( nil, dummy);
     raise EInOutError.Create('Expected exception not thrown?');
   except
-    on e:TTransportExceptionEndOfFile do {expected};
+    on e:TTransportException 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.