-- Tests for the serialization of data structures in SMG Protocol -- S.MG, 2018 with RNG; with Data_Structs; use Data_Structs; with Messages; use Messages; with Interfaces; use Interfaces; with System; with System.Storage_Elements; use System.Storage_Elements; with Ada.Text_IO; use Ada.Text_IO; package body Test_Serializing is procedure Serialize_Keyset_SS is Msg : Serpent_Msg; RMsg : RSA_Msg; KSet : Serpent_Keyset(5); LSB : Interfaces.Unsigned_8 := 16#01#; MSB : Interfaces.Unsigned_8 := 16#80#; LMSB : Interfaces.Unsigned_8 := 16#81#; Counter : Interfaces.Unsigned_16 := 101; NewSetS : Serpent_Keyset; NewSetR : Serpent_Keyset; NewCounterR: Interfaces.Unsigned_16:=0; NewCounterS: Interfaces.Unsigned_16:=0; begin Put_Line("Generating the Serpent Keys..."); -- fill a set of Serpent Keys for I in 1..KSet.N loop RNG.Get_Octets(KSet.Keys(Interfaces.Unsigned_8(I))); end loop; KSet.Flag := LSB; Put_Line("Writing the keys to messages..."); -- write keyset to serpent & rsa messages Write_SKeys_SMsg( KSet, Counter, Msg ); Write_SKeys_RMsg( KSet, Counter, RMsg ); Put_Line("Reading keys back from messages..."); -- read keyset from serpent and rsa messages Read_SKeys_SMsg( Msg, NewCounterS, NewSetS ); Read_SKeys_RMsg( RMsg, NewCounterR, NewSetR ); Put_Line("Comparing the keysets..."); -- compare the two keysets if NewCounterS /= Counter or NewCounterS /= Counter or NewSetS /= KSet or NewSetR /= KSet then Put_Line("FAIL: keysets are different!"); else Put_Line("PASS: keysets are the same!"); end if; Put_Line("Attempting to read from mangled message"); begin Msg(Msg'First) := Msg(Msg'First)+25; Read_SKeys_SMsg( Msg, Counter, NewSetS); Put_Line("FAIL: read failed to raise invalid message exception!"); exception when Invalid_Msg => Put_Line("PASS: exception correctly raised for invalid message"); end; end Serialize_Keyset_SS; procedure Serialize_Keys_Mgm is N_Burnt : Counter_8bits; Counter : Interfaces.Unsigned_16 := 16#EDA9#; Mgm_S : Keys_Mgm; Mgm_R : Keys_Mgm; Cnt_S : Interfaces.Unsigned_16:=0; Cnt_R : Interfaces.Unsigned_16:=0; O1 : Octets_1; SMsg : Serpent_Msg; RMsg : RSA_Msg; begin -- fill the struct with random stuff RNG.Get_Octets( O1 ); N_Burnt := Cast(O1); declare Mgm: Keys_Mgm(N_Burnt); begin RNG.Get_Octets( O1 ); Mgm.N_Server := O1(O1'First); RNG.Get_Octets( O1 ); Mgm.N_Client := O1(O1'First); RNG.Get_Octets( O1 ); Mgm.Key_ID := O1(O1'First); if N_Burnt > 0 then RNG.Get_Octets( Mgm.Burnt ); end if; -- write it to Serpent and RSA messages Write_KMgm_SMsg(Mgm, Counter, SMsg); Write_KMgm_RMsg(Mgm, Counter, RMsg); -- read it back from Serpent and RSA messages Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S ); Read_KMgm_RMsg( RMsg, Cnt_R, Mgm_R ); -- check results if Cnt_S /= Counter or Mgm_S.N_Burnt /= Mgm.N_Burnt or Mgm_S /= Mgm then Put_Line("FAIL: read/write key management struct to S msg."); else Put_Line("PASS: read/write key management struct to S msg."); end if; if Cnt_R /= Counter or Mgm_R.N_Burnt /= Mgm.N_Burnt or Mgm_R /= Mgm then Put_Line("FAIL: read/write key management struct to R msg."); Put_Line("Cnt_R is " & Unsigned_16'Image(Cnt_R)); Put_Line("Counter is " & Unsigned_16'Image(Counter)); Put_Line("Mgm_R.N_Burnt is " & Counter_8bits'Image(Mgm_R.N_Burnt)); Put_Line("Mgm.N_Burnt is " & Counter_8bits'Image(Mgm.N_Burnt)); else Put_Line("PASS: read/write key management struct to R msg."); end if; -- attempt mangled call - should raise exception begin SMsg(SMsg'First) := SMsg(SMsg'First) + 1; Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S); Put_Line("FAIL: Read_KMgm_SMsg failed to raise exception!"); exception when Invalid_Msg => Put_Line("PASS: Read_KMgm_SMsg correctly raised exception."); end; end; end Serialize_Keys_Mgm; end Test_Serializing;