raw
smg_comms_skeys_s...    1   -- Tests for the serialization of data structures in SMG Protocol
smg_comms_skeys_s... 2 -- S.MG, 2018
smg_comms_skeys_s... 3
smg_comms_skeys_s... 4 with RNG;
smg_comms_skeys_s... 5 with Data_Structs; use Data_Structs;
smg_comms_skeys_s... 6 with Messages; use Messages;
smg_comms_skeys_s... 7 with Interfaces; use Interfaces;
smg_comms_skeys_s... 8 with System;
smg_comms_skeys_s... 9 with System.Storage_Elements; use System.Storage_Elements;
smg_comms_skeys_s... 10 with Ada.Text_IO; use Ada.Text_IO;
smg_comms_skeys_s... 11
smg_comms_skeys_s... 12 package body Test_Serializing is
smg_comms_skeys_s... 13
smg_comms_skeys_s... 14 procedure Serialize_Keyset_SS is
smg_comms_keymgm 15 Msg : Serpent_Msg;
smg_comms_keymgm 16 RMsg : RSA_Msg;
smg_comms_keymgm 17 KSet : Serpent_Keyset(5);
smg_comms_keymgm 18 LSB : Interfaces.Unsigned_8 := 16#01#;
smg_comms_keymgm 19 MSB : Interfaces.Unsigned_8 := 16#80#;
smg_comms_keymgm 20 LMSB : Interfaces.Unsigned_8 := 16#81#;
smg_comms_keymgm 21 Counter : Interfaces.Unsigned_16 := 101;
smg_comms_keymgm 22 NewSetS : Serpent_Keyset;
smg_comms_keymgm 23 NewSetR : Serpent_Keyset;
smg_comms_keymgm 24 NewCounterR: Interfaces.Unsigned_16:=0;
smg_comms_keymgm 25 NewCounterS: Interfaces.Unsigned_16:=0;
smg_comms_skeys_s... 26 begin
smg_comms_skeys_s... 27 Put_Line("Generating the Serpent Keys...");
smg_comms_skeys_s... 28 -- fill a set of Serpent Keys
smg_comms_skeys_s... 29 for I in 1..KSet.N loop
smg_comms_skeys_s... 30 RNG.Get_Octets(KSet.Keys(Interfaces.Unsigned_8(I)));
smg_comms_skeys_s... 31 end loop;
smg_comms_skeys_s... 32 KSet.Flag := LSB;
smg_comms_skeys_s... 33
smg_comms_keymgm 34 Put_Line("Writing the keys to messages...");
smg_comms_keymgm 35 -- write keyset to serpent & rsa messages
smg_comms_skeys_s... 36 Write_SKeys_SMsg( KSet, Counter, Msg );
smg_comms_keymgm 37 Write_SKeys_RMsg( KSet, Counter, RMsg );
smg_comms_skeys_s... 38
smg_comms_keymgm 39 Put_Line("Reading keys back from messages...");
smg_comms_keymgm 40 -- read keyset from serpent and rsa messages
smg_comms_keymgm 41 Read_SKeys_SMsg( Msg, NewCounterS, NewSetS );
smg_comms_keymgm 42 Read_SKeys_RMsg( RMsg, NewCounterR, NewSetR );
smg_comms_skeys_s... 43
smg_comms_skeys_s... 44 Put_Line("Comparing the keysets...");
smg_comms_skeys_s... 45 -- compare the two keysets
smg_comms_keymgm 46 if NewCounterS /= Counter or NewCounterS /= Counter or
smg_comms_keymgm 47 NewSetS /= KSet or NewSetR /= KSet then
smg_comms_skeys_s... 48 Put_Line("FAIL: keysets are different!");
smg_comms_skeys_s... 49 else
smg_comms_skeys_s... 50 Put_Line("PASS: keysets are the same!");
smg_comms_skeys_s... 51 end if;
smg_comms_skeys_s... 52
smg_comms_skeys_s... 53 Put_Line("Attempting to read from mangled message");
smg_comms_skeys_s... 54 begin
smg_comms_skeys_s... 55 Msg(Msg'First) := Msg(Msg'First)+25;
smg_comms_keymgm 56 Read_SKeys_SMsg( Msg, Counter, NewSetS);
smg_comms_skeys_s... 57 Put_Line("FAIL: read failed to raise invalid message exception!");
smg_comms_skeys_s... 58 exception
smg_comms_skeys_s... 59 when Invalid_Msg =>
smg_comms_skeys_s... 60 Put_Line("PASS: exception correctly raised for invalid message");
smg_comms_skeys_s... 61 end;
smg_comms_skeys_s... 62 end Serialize_Keyset_SS;
smg_comms_skeys_s... 63
smg_comms_keymgm 64 procedure Serialize_Keys_Mgm is
smg_comms_keymgm 65 N_Burnt : Counter_8bits;
smg_comms_keymgm 66 Counter : Interfaces.Unsigned_16 := 16#EDA9#;
smg_comms_keymgm 67 Mgm_S : Keys_Mgm;
smg_comms_keymgm 68 Mgm_R : Keys_Mgm;
smg_comms_keymgm 69 Cnt_S : Interfaces.Unsigned_16:=0;
smg_comms_keymgm 70 Cnt_R : Interfaces.Unsigned_16:=0;
smg_comms_keymgm 71 O1 : Octets_1;
smg_comms_keymgm 72 SMsg : Serpent_Msg;
smg_comms_keymgm 73 RMsg : RSA_Msg;
smg_comms_keymgm 74 begin
smg_comms_keymgm 75 -- fill the struct with random stuff
smg_comms_keymgm 76 RNG.Get_Octets( O1 );
smg_comms_keymgm 77 N_Burnt := Cast(O1);
smg_comms_keymgm 78 declare
smg_comms_keymgm 79 Mgm: Keys_Mgm(N_Burnt);
smg_comms_keymgm 80 begin
smg_comms_keymgm 81 RNG.Get_Octets( O1 );
smg_comms_keymgm 82 Mgm.N_Server := O1(O1'First);
smg_comms_keymgm 83 RNG.Get_Octets( O1 );
smg_comms_keymgm 84 Mgm.N_Client := O1(O1'First);
smg_comms_keymgm 85 RNG.Get_Octets( O1 );
smg_comms_keymgm 86 Mgm.Key_ID := O1(O1'First);
smg_comms_keymgm 87 if N_Burnt > 0 then
smg_comms_keymgm 88 RNG.Get_Octets( Mgm.Burnt );
smg_comms_keymgm 89 end if;
smg_comms_keymgm 90 -- write it to Serpent and RSA messages
smg_comms_keymgm 91 Write_KMgm_SMsg(Mgm, Counter, SMsg);
smg_comms_keymgm 92 Write_KMgm_RMsg(Mgm, Counter, RMsg);
smg_comms_keymgm 93
smg_comms_keymgm 94 -- read it back from Serpent and RSA messages
smg_comms_keymgm 95 Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S );
smg_comms_keymgm 96 Read_KMgm_RMsg( RMsg, Cnt_R, Mgm_R );
smg_comms_keymgm 97
smg_comms_keymgm 98 -- check results
smg_comms_keymgm 99 if Cnt_S /= Counter or
smg_comms_keymgm 100 Mgm_S.N_Burnt /= Mgm.N_Burnt or
smg_comms_keymgm 101 Mgm_S /= Mgm then
smg_comms_keymgm 102 Put_Line("FAIL: read/write key management struct to S msg.");
smg_comms_keymgm 103 else
smg_comms_keymgm 104 Put_Line("PASS: read/write key management struct to S msg.");
smg_comms_keymgm 105 end if;
smg_comms_keymgm 106
smg_comms_keymgm 107 if Cnt_R /= Counter or
smg_comms_keymgm 108 Mgm_R.N_Burnt /= Mgm.N_Burnt or
smg_comms_keymgm 109 Mgm_R /= Mgm then
smg_comms_keymgm 110 Put_Line("FAIL: read/write key management struct to R msg.");
smg_comms_keymgm 111 Put_Line("Cnt_R is " & Unsigned_16'Image(Cnt_R));
smg_comms_keymgm 112 Put_Line("Counter is " & Unsigned_16'Image(Counter));
smg_comms_keymgm 113 Put_Line("Mgm_R.N_Burnt is " & Counter_8bits'Image(Mgm_R.N_Burnt));
smg_comms_keymgm 114 Put_Line("Mgm.N_Burnt is " & Counter_8bits'Image(Mgm.N_Burnt));
smg_comms_keymgm 115 else
smg_comms_keymgm 116 Put_Line("PASS: read/write key management struct to R msg.");
smg_comms_keymgm 117 end if;
smg_comms_keymgm 118
smg_comms_keymgm 119 -- attempt mangled call - should raise exception
smg_comms_keymgm 120 begin
smg_comms_keymgm 121 SMsg(SMsg'First) := SMsg(SMsg'First) + 1;
smg_comms_keymgm 122 Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S);
smg_comms_keymgm 123 Put_Line("FAIL: Read_KMgm_SMsg failed to raise exception!");
smg_comms_keymgm 124 exception
smg_comms_keymgm 125 when Invalid_Msg =>
smg_comms_keymgm 126 Put_Line("PASS: Read_KMgm_SMsg correctly raised exception.");
smg_comms_keymgm 127 end;
smg_comms_keymgm 128
smg_comms_keymgm 129 end;
smg_comms_keymgm 130
smg_comms_keymgm 131 end Serialize_Keys_Mgm;
smg_comms_keymgm 132
smg_comms_files 133 procedure Serialize_File_Request( Reps: in Positive) is
smg_comms_files 134 MaxSz: Positive := 340;
smg_comms_files 135 O2 : Raw_Types.Octets_2;
smg_comms_files 136 U16 : Interfaces.Unsigned_16;
smg_comms_files 137 Counter : Interfaces.Unsigned_16;
smg_comms_files 138 ReadCounter: Interfaces.Unsigned_16;
smg_comms_files 139 Written: Natural;
smg_comms_files 140 Msg : Raw_Types.Serpent_Msg;
smg_comms_files 141 F_No, Sz: Text_Len;
smg_comms_files 142 begin
smg_comms_files 143 for I in 1 .. Reps loop
smg_comms_files 144 -- generate a random size
smg_comms_files 145 RNG.Get_Octets( O2 );
smg_comms_files 146 U16 := Raw_Types.Cast( O2 );
smg_comms_files 147 Sz := Text_Len( Positive(U16) mod MaxSz + 1);
smg_comms_files 148
smg_comms_files 149 -- generate a random number of files
smg_comms_files 150 RNG.Get_Octets( O2 );
smg_comms_files 151 U16 := Raw_Types.Cast( O2 );
smg_comms_files 152 F_No := Text_Len( Positive(U16) mod Sz );
smg_comms_files 153 if F_No = 0 then
smg_comms_files 154 F_No := 1;
smg_comms_files 155 end if;
smg_comms_files 156
smg_comms_files 157 declare
smg_comms_files 158 FR: Filenames(F_No, Sz);
smg_comms_files 159 ReadFR: Filenames;
smg_comms_files 160 O : Octets(1..Sz);
smg_comms_files 161 Len : Positive := Sz / F_No;
smg_comms_files 162 begin
smg_comms_files 163 Put_Line("Generating test for File Request with " &
smg_comms_files 164 Integer'Image(FR.F_No) & " filenames.");
smg_comms_files 165 -- generate a random counter
smg_comms_files 166 RNG.Get_Octets(O2);
smg_comms_files 167 Counter := Raw_Types.Cast(O2);
smg_comms_files 168
smg_comms_files 169 -- fill FR
smg_comms_files 170 RNG.Get_Octets( O );
smg_comms_files 171 -- replace separators if any
smg_comms_files 172 for I in O'Range loop
smg_comms_files 173 if O(I) = 59 then
smg_comms_files 174 O(I) := 69;
smg_comms_files 175 end if;
smg_comms_files 176 end loop;
smg_comms_files 177
smg_comms_files 178 Octets_To_String( O, FR.S );
smg_comms_files 179 for I in FR.Starts'Range loop
smg_comms_files 180 FR.Starts(I) := Interfaces.Unsigned_16( FR.Starts'First +
smg_comms_files 181 Len*(I-FR.Starts'First));
smg_comms_files 182 end loop;
smg_comms_files 183
smg_comms_files 184 -- write FR to message
smg_comms_files 185 Write_File_Request(FR, Counter, Msg, Written);
smg_comms_files 186
smg_comms_files 187 -- check how many filenames were written
smg_comms_files 188 if Written /= FR.F_No then
smg_comms_files 189 Put_Line("FAIL: only " & Natural'Image(Written) &
smg_comms_files 190 " filenames written out of " & Natural'Image(FR.F_No));
smg_comms_files 191 else
smg_comms_files 192 Put_Line("PASS: wrote " & Natural'Image(Written) & " filenames.");
smg_comms_files 193 end if;
smg_comms_files 194
smg_comms_files 195 -- read it from message and check result
smg_comms_files 196 Read_File_Request(Msg, ReadCounter, ReadFR);
smg_comms_files 197 if ReadCounter /= Counter then
smg_comms_files 198 Put_Line("FAIL: ReadCounter is " & Unsigned_16'Image(ReadCounter) &
smg_comms_files 199 " instead of expected " & Unsigned_16'Image(Counter) );
smg_comms_files 200 else
smg_comms_files 201 Put_Line("PASS: ReadCounter was read correctly as " &
smg_comms_files 202 Unsigned_16'Image(ReadCounter));
smg_comms_files 203 end if;
smg_comms_files 204 if ReadFr.Sz /= FR.Sz then
smg_comms_files 205 Put_Line("FAIL: Read FR.Sz = " & Text_Len'Image(ReadFr.Sz) &
smg_comms_files 206 " while expected sz is " & Text_Len'Image(FR.Sz));
smg_comms_files 207 else
smg_comms_files 208 Put_Line("PASS: Read FR.Sz as expected = " &
smg_comms_files 209 Text_Len'Image(ReadFr.Sz));
smg_comms_files 210 end if;
smg_comms_files 211 if ReadFr /= FR then
smg_comms_files 212 Put_Line("FAIL: ReadFr different from FR.");
smg_comms_files 213 else
smg_comms_files 214 Put_Line("PASS: ReadFr as expected, same as FR.");
smg_comms_files 215 end if;
smg_comms_files 216 end;
smg_comms_files 217 end loop;
smg_comms_files 218
smg_comms_files 219 -- test with more files than can fit
smg_comms_files 220 Put_Line("Test case for File Request with more files than fit.");
smg_comms_files 221 declare
smg_comms_files 222 F: Filenames(4, 16384);
smg_comms_files 223 ReadF: Filenames;
smg_comms_files 224 begin
smg_comms_files 225 F.Starts(1) := Interfaces.Unsigned_16(F.S'First);
smg_comms_files 226 F.Starts(2) := Interfaces.Unsigned_16(F.S'First + 342);
smg_comms_files 227 F.Starts(3) := 16370;
smg_comms_files 228 F.Starts(4) := 16380;
smg_comms_files 229 Write_File_Request(F, Counter, Msg, Written);
smg_comms_files 230 if Written /= 1 then
smg_comms_files 231 Put_Line("FAIL: Written is " & Natural'Image(Written) &
smg_comms_files 232 " instead of expected 1.");
smg_comms_files 233 else
smg_comms_files 234 Put_Line("PASS: Written is 1 out of 4, as expected.");
smg_comms_files 235 end if;
smg_comms_files 236 Read_File_Request(Msg, ReadCounter, ReadF);
smg_comms_files 237 if ReadF.F_No /= 1 or ReadF.Starts(1) /= 1 or ReadF.Sz /= 342 then
smg_comms_files 238 Put_Line("FAIL: F_No is " & Text_Len'Image(ReadF.F_No) &
smg_comms_files 239 " Sz is " & Text_Len'Image(ReadF.Sz));
smg_comms_files 240 else
smg_comms_files 241 Put_line("PASS: written 1 out of 4 correctly.");
smg_comms_files 242 end if;
smg_comms_files 243 end;
smg_comms_files 244 end Serialize_File_Request;
smg_comms_files 245
smg_comms_files 246 procedure Serialize_File_Chunk is
smg_comms_files 247 Filename : String := "afile.png";
smg_comms_files 248 FC : File_Chunk( Len => 945,
smg_comms_files 249 Count => 0,
smg_comms_files 250 Name_Len => Filename'Length);
smg_comms_files 251 ReadFC : File_Chunk;
smg_comms_files 252 Msg : Raw_Types.Serpent_Msg;
smg_comms_files 253 begin
smg_comms_files 254 -- fill FC with random content
smg_comms_files 255 FC.Filename := Filename;
smg_comms_files 256 RNG.Get_Octets(FC.Content);
smg_comms_files 257
smg_comms_files 258 -- write FC to message
smg_comms_files 259 Write_File_Transfer( FC, Msg );
smg_comms_files 260
smg_comms_files 261 -- read FC and check
smg_comms_files 262 Read_File_Transfer( Msg, ReadFC );
smg_comms_files 263
smg_comms_files 264 if FC /= ReadFC then
smg_comms_files 265 Put_Line("FAIL: read/write file chunk.");
smg_comms_files 266 else
smg_comms_files 267 Put_Line("PASS: read/write file chunk.");
smg_comms_files 268 end if;
smg_comms_files 269 end Serialize_File_Chunk;
smg_comms_files 270
smg_comms_files 271 procedure Converter_String_Octets is
smg_comms_files 272 Len: constant Natural := 234;
smg_comms_files 273 -- original values
smg_comms_files 274 S: String(1..Len);
smg_comms_files 275 O: Octets(1..Len);
smg_comms_files 276 -- converted values
smg_comms_files 277 CS: String(1..Len);
smg_comms_files 278 CO: Octets(1..Len);
smg_comms_files 279 begin
smg_comms_files 280 -- original octets
smg_comms_files 281 RNG.Get_Octets(O);
smg_comms_files 282 Octets_To_String(O, CS);
smg_comms_files 283 String_To_Octets(CS, CO);
smg_comms_files 284 if CO /= O then
smg_comms_files 285 Put_Line("FAIL: octets different after string/octets conversion.");
smg_comms_files 286 else
smg_comms_files 287 Put_Line("PASS: octets same after string/octets conversion.");
smg_comms_files 288 end if;
smg_comms_files 289
smg_comms_files 290 -- original string
smg_comms_files 291 for I in S'Range loop
smg_comms_files 292 S(I) := Character'Val(I mod 12);
smg_comms_files 293 end loop;
smg_comms_files 294 String_To_Octets(S, CO);
smg_comms_files 295 Octets_To_String(CO, CS);
smg_comms_files 296 if CS /= S then
smg_comms_files 297 Put_Line("FAIL: string different after string/octets conversion.");
smg_comms_files 298 else
smg_comms_files 299 Put_Line("PASS: string same after string/octets conversion.");
smg_comms_files 300 end if;
smg_comms_files 301 end Converter_String_Octets;
smg_comms_files 302
smg_comms_skeys_s... 303 end Test_Serializing;
smg_comms_skeys_s... 304