raw
smg_comms_actions...    1   -- Tests for the serialization of data structures in SMG Protocol
smg_comms_actions... 2 -- S.MG, 2018
smg_comms_actions... 3
smg_comms_actions... 4 with RNG;
smg_comms_actions... 5 with Data_Structs;
smg_comms_actions... 6 with Messages; use Messages;
smg_comms_actions... 7 with Interfaces; use Interfaces;
smg_comms_actions... 8 with System;
smg_comms_actions... 9 with System.Storage_Elements; use System.Storage_Elements;
smg_comms_actions... 10 with Ada.Text_IO; use Ada.Text_IO;
smg_comms_actions... 11
smg_comms_actions... 12 package body Messages.Test_Serializing is
smg_comms_actions... 13
smg_comms_actions... 14 procedure Serialize_Keyset_RSA is
smg_comms_actions... 15 Msg : RSA_Msg;
smg_comms_actions... 16 ReadK : Player_RSA;
smg_comms_actions... 17 ReadC : Interfaces.Unsigned_16;
smg_comms_actions... 18 WMsg : RSA_Msg;
smg_comms_actions... 19 TypeID : constant Interfaces.Unsigned_8 := 251;
smg_comms_actions... 20 begin
smg_comms_actions... 21 Put_Line("Generating data for 5.1 RSA key set in RSA message.");
smg_comms_actions... 22 -- fill message with random data
smg_comms_actions... 23 RNG.Get_Octets(Msg);
smg_comms_actions... 24
smg_comms_actions... 25 -- attempt read - it should fail
smg_comms_actions... 26 begin
smg_comms_actions... 27 Read_RKeys_RMsg( Msg, ReadC, ReadK );
smg_comms_actions... 28 exception
smg_comms_actions... 29 when Invalid_Msg =>
smg_comms_actions... 30 if Msg(Msg'First) /= TypeID then
smg_comms_actions... 31 Put_Line("PASS: exception correctly raised for invalid message");
smg_comms_actions... 32 else
smg_comms_actions... 33 Put_Line("FAIL: exception raised on VALID message!");
smg_comms_actions... 34 end if;
smg_comms_actions... 35 end;
smg_comms_actions... 36
smg_comms_actions... 37 -- set correct type id
smg_comms_actions... 38 Msg(Msg'First) := TypeID;
smg_comms_actions... 39
smg_comms_actions... 40 -- attempt read + write and compare
smg_comms_actions... 41 Read_RKeys_RMsg( Msg, ReadC, ReadK );
smg_comms_actions... 42 Write_RKeys_RMsg( ReadK, ReadC, RNG_PAD, WMsg );
smg_comms_actions... 43
smg_comms_actions... 44 -- check result (without trailing padding since that CAN be different!)
smg_comms_actions... 45 if WMsg(1..520) /= Msg(1..520) then
smg_comms_actions... 46 Put_Line("FAIL: read+write of RSA keys to RSA message.");
smg_comms_actions... 47 for I in 1..520 loop
smg_comms_actions... 48 if WMsg(I) /= Msg(I) then
smg_comms_actions... 49 Put_Line("At " & Integer'Image(I) & " WMsg is " &
smg_comms_actions... 50 Unsigned_8'Image(WMsg(I)) & " while Msg is " &
smg_comms_actions... 51 Unsigned_8'Image(Msg(I)));
smg_comms_actions... 52 end if;
smg_comms_actions... 53 end loop;
smg_comms_actions... 54 else
smg_comms_actions... 55 Put_Line("PASS: read+write of RSA keys to RSA message.");
smg_comms_actions... 56 end if;
smg_comms_actions... 57 end Serialize_Keyset_RSA;
smg_comms_actions... 58
smg_comms_actions... 59 procedure Serialize_Keyset_SS is
smg_comms_actions... 60 Msg : Serpent_Msg;
smg_comms_actions... 61 RMsg : RSA_Msg;
smg_comms_actions... 62 KSet : Serpent_Keyset(5);
smg_comms_actions... 63 LSB : Interfaces.Unsigned_8 := 16#01#;
smg_comms_actions... 64 MSB : Interfaces.Unsigned_8 := 16#80#;
smg_comms_actions... 65 LMSB : Interfaces.Unsigned_8 := 16#81#;
smg_comms_actions... 66 Counter : Interfaces.Unsigned_16 := 101;
smg_comms_actions... 67 NewSetS : Serpent_Keyset;
smg_comms_actions... 68 NewSetR : Serpent_Keyset;
smg_comms_actions... 69 NewCounterR: Interfaces.Unsigned_16:=0;
smg_comms_actions... 70 NewCounterS: Interfaces.Unsigned_16:=0;
smg_comms_actions... 71 begin
smg_comms_actions... 72 Put_Line("Generating the Serpent Keys...");
smg_comms_actions... 73 -- fill a set of Serpent Keys
smg_comms_actions... 74 for I in 1..KSet.N loop
smg_comms_actions... 75 RNG.Get_Octets(KSet.Keys(Interfaces.Unsigned_8(I)));
smg_comms_actions... 76 end loop;
smg_comms_actions... 77 KSet.Flag := LSB;
smg_comms_actions... 78
smg_comms_actions... 79 Put_Line("Writing the keys to messages...");
smg_comms_actions... 80 -- write keyset to serpent & rsa messages
smg_comms_actions... 81 Write_SKeys_SMsg( KSet, Counter, RNG_PAD, Msg );
smg_comms_actions... 82 Write_SKeys_RMsg( KSet, Counter, RNG_PAD, RMsg );
smg_comms_actions... 83
smg_comms_actions... 84 Put_Line("Reading keys back from messages...");
smg_comms_actions... 85 -- read keyset from serpent and rsa messages
smg_comms_actions... 86 Read_SKeys_SMsg( Msg, NewCounterS, NewSetS );
smg_comms_actions... 87 Read_SKeys_RMsg( RMsg, NewCounterR, NewSetR );
smg_comms_actions... 88
smg_comms_actions... 89 Put_Line("Comparing the keysets...");
smg_comms_actions... 90 -- compare the two keysets
smg_comms_actions... 91 if NewCounterS /= Counter or NewCounterS /= Counter or
smg_comms_actions... 92 NewSetS /= KSet or NewSetR /= KSet then
smg_comms_actions... 93 Put_Line("FAIL: keysets are different!");
smg_comms_actions... 94 else
smg_comms_actions... 95 Put_Line("PASS: keysets are the same!");
smg_comms_actions... 96 end if;
smg_comms_actions... 97
smg_comms_actions... 98 Put_Line("Attempting to read from mangled message");
smg_comms_actions... 99 begin
smg_comms_actions... 100 Msg(Msg'First) := Msg(Msg'First)+25;
smg_comms_actions... 101 Read_SKeys_SMsg( Msg, Counter, NewSetS);
smg_comms_actions... 102 Put_Line("FAIL: read failed to raise invalid message exception!");
smg_comms_actions... 103 exception
smg_comms_actions... 104 when Invalid_Msg =>
smg_comms_actions... 105 Put_Line("PASS: exception correctly raised for invalid message");
smg_comms_actions... 106 end;
smg_comms_actions... 107 end Serialize_Keyset_SS;
smg_comms_actions... 108
smg_comms_actions... 109 procedure Serialize_Keys_Mgm is
smg_comms_actions... 110 N_Burnt : Counter_8bits;
smg_comms_actions... 111 Counter : Interfaces.Unsigned_16 := 16#EDA9#;
smg_comms_actions... 112 Mgm_S : Keys_Mgm;
smg_comms_actions... 113 Mgm_R : Keys_Mgm;
smg_comms_actions... 114 Cnt_S : Interfaces.Unsigned_16:=0;
smg_comms_actions... 115 Cnt_R : Interfaces.Unsigned_16:=0;
smg_comms_actions... 116 O1 : Octets_1;
smg_comms_actions... 117 SMsg : Serpent_Msg;
smg_comms_actions... 118 RMsg : RSA_Msg;
smg_comms_actions... 119 begin
smg_comms_actions... 120 -- fill the struct with random stuff
smg_comms_actions... 121 RNG.Get_Octets( O1 );
smg_comms_actions... 122 N_Burnt := Cast(O1);
smg_comms_actions... 123 declare
smg_comms_actions... 124 Mgm: Keys_Mgm(N_Burnt);
smg_comms_actions... 125 begin
smg_comms_actions... 126 RNG.Get_Octets( O1 );
smg_comms_actions... 127 Mgm.N_Server := O1(O1'First);
smg_comms_actions... 128 RNG.Get_Octets( O1 );
smg_comms_actions... 129 Mgm.N_Client := O1(O1'First);
smg_comms_actions... 130 RNG.Get_Octets( O1 );
smg_comms_actions... 131 Mgm.Key_ID := O1(O1'First);
smg_comms_actions... 132 if N_Burnt > 0 then
smg_comms_actions... 133 RNG.Get_Octets( Mgm.Burnt );
smg_comms_actions... 134 end if;
smg_comms_actions... 135 -- write it to Serpent and RSA messages
smg_comms_actions... 136 Write_KMgm_SMsg(Mgm, Counter, RNG_PAD, SMsg);
smg_comms_actions... 137 Write_KMgm_RMsg(Mgm, Counter, RNG_PAD, RMsg);
smg_comms_actions... 138
smg_comms_actions... 139 -- read it back from Serpent and RSA messages
smg_comms_actions... 140 Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S );
smg_comms_actions... 141 Read_KMgm_RMsg( RMsg, Cnt_R, Mgm_R );
smg_comms_actions... 142
smg_comms_actions... 143 -- check results
smg_comms_actions... 144 if Cnt_S /= Counter or
smg_comms_actions... 145 Mgm_S.N_Burnt /= Mgm.N_Burnt or
smg_comms_actions... 146 Mgm_S /= Mgm then
smg_comms_actions... 147 Put_Line("FAIL: read/write key management struct to S msg.");
smg_comms_actions... 148 else
smg_comms_actions... 149 Put_Line("PASS: read/write key management struct to S msg.");
smg_comms_actions... 150 end if;
smg_comms_actions... 151
smg_comms_actions... 152 if Cnt_R /= Counter or
smg_comms_actions... 153 Mgm_R.N_Burnt /= Mgm.N_Burnt or
smg_comms_actions... 154 Mgm_R /= Mgm then
smg_comms_actions... 155 Put_Line("FAIL: read/write key management struct to R msg.");
smg_comms_actions... 156 Put_Line("Cnt_R is " & Unsigned_16'Image(Cnt_R));
smg_comms_actions... 157 Put_Line("Counter is " & Unsigned_16'Image(Counter));
smg_comms_actions... 158 Put_Line("Mgm_R.N_Burnt is " & Counter_8bits'Image(Mgm_R.N_Burnt));
smg_comms_actions... 159 Put_Line("Mgm.N_Burnt is " & Counter_8bits'Image(Mgm.N_Burnt));
smg_comms_actions... 160 else
smg_comms_actions... 161 Put_Line("PASS: read/write key management struct to R msg.");
smg_comms_actions... 162 end if;
smg_comms_actions... 163
smg_comms_actions... 164 -- attempt mangled call - should raise exception
smg_comms_actions... 165 begin
smg_comms_actions... 166 SMsg(SMsg'First) := SMsg(SMsg'First) + 1;
smg_comms_actions... 167 Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S);
smg_comms_actions... 168 Put_Line("FAIL: Read_KMgm_SMsg failed to raise exception!");
smg_comms_actions... 169 exception
smg_comms_actions... 170 when Invalid_Msg =>
smg_comms_actions... 171 Put_Line("PASS: Read_KMgm_SMsg correctly raised exception.");
smg_comms_actions... 172 end;
smg_comms_actions... 173
smg_comms_actions... 174 end;
smg_comms_actions... 175
smg_comms_actions... 176 end Serialize_Keys_Mgm;
smg_comms_actions... 177
smg_comms_actions... 178 procedure Serialize_File_Request( Reps: in Positive) is
smg_comms_actions... 179 MaxSz: Positive := 340;
smg_comms_actions... 180 O2 : Raw_Types.Octets_2;
smg_comms_actions... 181 U16 : Interfaces.Unsigned_16;
smg_comms_actions... 182 Counter : Interfaces.Unsigned_16;
smg_comms_actions... 183 ReadCounter: Interfaces.Unsigned_16;
smg_comms_actions... 184 Written: Natural;
smg_comms_actions... 185 Msg : Raw_Types.Serpent_Msg;
smg_comms_actions... 186 F_No, Sz: Text_Len;
smg_comms_actions... 187 begin
smg_comms_actions... 188 for I in 1 .. Reps loop
smg_comms_actions... 189 -- generate a random size
smg_comms_actions... 190 RNG.Get_Octets( O2 );
smg_comms_actions... 191 U16 := Raw_Types.Cast( O2 );
smg_comms_actions... 192 Sz := Text_Len( Positive(U16) mod MaxSz + 1);
smg_comms_actions... 193
smg_comms_actions... 194 -- generate a random number of files
smg_comms_actions... 195 RNG.Get_Octets( O2 );
smg_comms_actions... 196 U16 := Raw_Types.Cast( O2 );
smg_comms_actions... 197 -- make sure it's within Positive range i.e. not 0
smg_comms_actions... 198 if U16 = 0 then
smg_comms_actions... 199 U16 := 1;
smg_comms_actions... 200 end if;
smg_comms_actions... 201 F_No := Text_Len( Positive(U16) mod Sz + 1 );
smg_comms_actions... 202
smg_comms_actions... 203 declare
smg_comms_actions... 204 FR: Filenames(F_No, Sz);
smg_comms_actions... 205 ReadFR: Filenames;
smg_comms_actions... 206 O : Octets(1..Sz);
smg_comms_actions... 207 Len : Positive := Sz / F_No;
smg_comms_actions... 208 begin
smg_comms_actions... 209 Put_Line("Generating test for File Request with " &
smg_comms_actions... 210 Integer'Image(FR.F_No) & " filenames.");
smg_comms_actions... 211 -- generate a random counter
smg_comms_actions... 212 RNG.Get_Octets(O2);
smg_comms_actions... 213 Counter := Raw_Types.Cast(O2);
smg_comms_actions... 214
smg_comms_actions... 215 -- fill FR
smg_comms_actions... 216 RNG.Get_Octets( O );
smg_comms_actions... 217 -- replace separators if any
smg_comms_actions... 218 for I in O'Range loop
smg_comms_actions... 219 if O(I) = 59 then
smg_comms_actions... 220 O(I) := 69;
smg_comms_actions... 221 end if;
smg_comms_actions... 222 end loop;
smg_comms_actions... 223
smg_comms_actions... 224 Octets_To_String( O, FR.S );
smg_comms_actions... 225 for I in FR.Starts'Range loop
smg_comms_actions... 226 FR.Starts(I) := Interfaces.Unsigned_16( FR.Starts'First +
smg_comms_actions... 227 Len*(I-FR.Starts'First));
smg_comms_actions... 228 end loop;
smg_comms_actions... 229
smg_comms_actions... 230 -- write FR to message
smg_comms_actions... 231 Write_File_Request(FR, Counter, RNG_PAD, Msg, Written);
smg_comms_actions... 232
smg_comms_actions... 233 -- check how many filenames were written
smg_comms_actions... 234 if Written /= FR.F_No then
smg_comms_actions... 235 Put_Line("FAIL: only " & Natural'Image(Written) &
smg_comms_actions... 236 " filenames written out of " & Natural'Image(FR.F_No));
smg_comms_actions... 237 else
smg_comms_actions... 238 Put_Line("PASS: wrote " & Natural'Image(Written) & " filenames.");
smg_comms_actions... 239 end if;
smg_comms_actions... 240
smg_comms_actions... 241 -- read it from message and check result
smg_comms_actions... 242 Read_File_Request(Msg, ReadCounter, ReadFR);
smg_comms_actions... 243 if ReadCounter /= Counter then
smg_comms_actions... 244 Put_Line("FAIL: ReadCounter is " & Unsigned_16'Image(ReadCounter) &
smg_comms_actions... 245 " instead of expected " & Unsigned_16'Image(Counter) );
smg_comms_actions... 246 else
smg_comms_actions... 247 Put_Line("PASS: ReadCounter was read correctly as " &
smg_comms_actions... 248 Unsigned_16'Image(ReadCounter));
smg_comms_actions... 249 end if;
smg_comms_actions... 250 if ReadFr.Sz /= FR.Sz then
smg_comms_actions... 251 Put_Line("FAIL: Read FR.Sz = " & Text_Len'Image(ReadFr.Sz) &
smg_comms_actions... 252 " while expected sz is " & Text_Len'Image(FR.Sz));
smg_comms_actions... 253 else
smg_comms_actions... 254 Put_Line("PASS: Read FR.Sz as expected = " &
smg_comms_actions... 255 Text_Len'Image(ReadFr.Sz));
smg_comms_actions... 256 end if;
smg_comms_actions... 257 if ReadFr /= FR then
smg_comms_actions... 258 Put_Line("FAIL: ReadFr different from FR.");
smg_comms_actions... 259 else
smg_comms_actions... 260 Put_Line("PASS: ReadFr as expected, same as FR.");
smg_comms_actions... 261 end if;
smg_comms_actions... 262 end;
smg_comms_actions... 263 end loop;
smg_comms_actions... 264
smg_comms_actions... 265 -- test with more files than can fit
smg_comms_actions... 266 Put_Line("Test case for File Request with more files than fit.");
smg_comms_actions... 267 declare
smg_comms_actions... 268 F: Filenames(4, 16384);
smg_comms_actions... 269 ReadF: Filenames;
smg_comms_actions... 270 begin
smg_comms_actions... 271 F.Starts(1) := Interfaces.Unsigned_16(F.S'First);
smg_comms_actions... 272 F.Starts(2) := Interfaces.Unsigned_16(F.S'First + 342);
smg_comms_actions... 273 F.Starts(3) := 16370;
smg_comms_actions... 274 F.Starts(4) := 16380;
smg_comms_actions... 275 Write_File_Request(F, Counter, RNG_PAD, Msg, Written);
smg_comms_actions... 276 if Written /= 1 then
smg_comms_actions... 277 Put_Line("FAIL: Written is " & Natural'Image(Written) &
smg_comms_actions... 278 " instead of expected 1.");
smg_comms_actions... 279 else
smg_comms_actions... 280 Put_Line("PASS: Written is 1 out of 4, as expected.");
smg_comms_actions... 281 end if;
smg_comms_actions... 282 Read_File_Request(Msg, ReadCounter, ReadF);
smg_comms_actions... 283 if ReadF.F_No /= 1 or ReadF.Starts(1) /= 1 or ReadF.Sz /= 342 then
smg_comms_actions... 284 Put_Line("FAIL: F_No is " & Text_Len'Image(ReadF.F_No) &
smg_comms_actions... 285 " Sz is " & Text_Len'Image(ReadF.Sz));
smg_comms_actions... 286 else
smg_comms_actions... 287 Put_line("PASS: written 1 out of 4 correctly.");
smg_comms_actions... 288 end if;
smg_comms_actions... 289 end;
smg_comms_actions... 290 end Serialize_File_Request;
smg_comms_actions... 291
smg_comms_actions... 292 procedure Serialize_File_Chunk is
smg_comms_actions... 293 Filename : String := "afile.png";
smg_comms_actions... 294 FC : File_Chunk( Len => 945,
smg_comms_actions... 295 Count => 0,
smg_comms_actions... 296 Name_Len => Filename'Length);
smg_comms_actions... 297 ReadFC : File_Chunk;
smg_comms_actions... 298 Msg : Raw_Types.Serpent_Msg;
smg_comms_actions... 299 Pad : Raw_Types.Octets_8 := RNG_PAD;
smg_comms_actions... 300 begin
smg_comms_actions... 301 -- fill FC with random content
smg_comms_actions... 302 FC.Filename := Filename;
smg_comms_actions... 303 RNG.Get_Octets(FC.Content);
smg_comms_actions... 304
smg_comms_actions... 305 -- write FC to message with random padding
smg_comms_actions... 306 Write_File_Transfer( FC, Pad, Msg );
smg_comms_actions... 307
smg_comms_actions... 308 -- read FC and check
smg_comms_actions... 309 Read_File_Transfer( Msg, ReadFC );
smg_comms_actions... 310
smg_comms_actions... 311 if FC /= ReadFC then
smg_comms_actions... 312 Put_Line("FAIL: read/write file chunk.");
smg_comms_actions... 313 else
smg_comms_actions... 314 Put_Line("PASS: read/write file chunk.");
smg_comms_actions... 315 end if;
smg_comms_actions... 316 end Serialize_File_Chunk;
smg_comms_actions... 317
smg_comms_actions... 318 procedure Serialize_Action is
smg_comms_actions... 319 O2 : Raw_Types.Octets_2;
smg_comms_actions... 320 U16: Interfaces.Unsigned_16;
smg_comms_actions... 321 Len: Raw_Types.Text_Len;
smg_comms_actions... 322 Counter: Interfaces.Unsigned_16;
smg_comms_actions... 323 begin
smg_comms_actions... 324 Put_Line("Generating a random action for testing.");
smg_comms_actions... 325 -- generate random counter
smg_comms_actions... 326 RNG.Get_Octets( O2 );
smg_comms_actions... 327 Counter := Raw_Types.Cast( O2 );
smg_comms_actions... 328
smg_comms_actions... 329 -- generate action length
smg_comms_actions... 330 RNG.Get_Octets( O2 );
smg_comms_actions... 331 U16 := Raw_Types.Cast( O2 );
smg_comms_actions... 332 if U16 < 1 then
smg_comms_actions... 333 U16 := 1;
smg_comms_actions... 334 else
smg_comms_actions... 335 if U16 + 5 > Raw_Types.Serpent_Msg'Length then
smg_comms_actions... 336 U16 := Raw_Types.Serpent_Msg'Length - 5;
smg_comms_actions... 337 end if;
smg_comms_actions... 338 end if;
smg_comms_actions... 339 Len := Raw_Types.Text_Len( U16 );
smg_comms_actions... 340
smg_comms_actions... 341 declare
smg_comms_actions... 342 A: Raw_Types.Text_Octets( Len );
smg_comms_actions... 343 B: Raw_Types.Text_Octets;
smg_comms_actions... 344 Msg: Raw_Types.Serpent_Msg;
smg_comms_actions... 345 ReadC : Interfaces.Unsigned_16;
smg_comms_actions... 346 begin
smg_comms_actions... 347 RNG.Get_Octets( A.Content );
smg_comms_actions... 348 begin
smg_comms_actions... 349 Write_Action( A, Counter, RNG_PAD, Msg );
smg_comms_actions... 350 Read_Action( Msg, ReadC, B );
smg_comms_actions... 351 if B /= A then
smg_comms_actions... 352 Put_Line("FAIL: read/write of Action.");
smg_comms_actions... 353 else
smg_comms_actions... 354 Put_Line("PASS: read/write of Action.");
smg_comms_actions... 355 end if;
smg_comms_actions... 356 exception
smg_comms_actions... 357 when Invalid_Msg =>
smg_comms_actions... 358 if Len + 5 > Raw_Types.Serpent_Msg'Length then
smg_comms_actions... 359 Put_Line("PASS: exception correctly raised for Action too long");
smg_comms_actions... 360 else
smg_comms_actions... 361 Put_Line("FAIL: exception INCORRECTLY raised at action r/w!");
smg_comms_actions... 362 end if;
smg_comms_actions... 363 end;
smg_comms_actions... 364 end;
smg_comms_actions... 365 end Serialize_Action;
smg_comms_actions... 366
smg_comms_actions... 367 procedure Converter_String_Octets is
smg_comms_actions... 368 Len: constant Natural := 234;
smg_comms_actions... 369 -- original values
smg_comms_actions... 370 S: String(1..Len);
smg_comms_actions... 371 O: Octets(1..Len);
smg_comms_actions... 372 -- converted values
smg_comms_actions... 373 CS: String(1..Len);
smg_comms_actions... 374 CO: Octets(1..Len);
smg_comms_actions... 375 begin
smg_comms_actions... 376 -- original octets
smg_comms_actions... 377 RNG.Get_Octets(O);
smg_comms_actions... 378 Octets_To_String(O, CS);
smg_comms_actions... 379 String_To_Octets(CS, CO);
smg_comms_actions... 380 if CO /= O then
smg_comms_actions... 381 Put_Line("FAIL: octets different after string/octets conversion.");
smg_comms_actions... 382 else
smg_comms_actions... 383 Put_Line("PASS: octets same after string/octets conversion.");
smg_comms_actions... 384 end if;
smg_comms_actions... 385
smg_comms_actions... 386 -- original string
smg_comms_actions... 387 for I in S'Range loop
smg_comms_actions... 388 S(I) := Character'Val(I mod 12);
smg_comms_actions... 389 end loop;
smg_comms_actions... 390 String_To_Octets(S, CO);
smg_comms_actions... 391 Octets_To_String(CO, CS);
smg_comms_actions... 392 if CS /= S then
smg_comms_actions... 393 Put_Line("FAIL: string different after string/octets conversion.");
smg_comms_actions... 394 else
smg_comms_actions... 395 Put_Line("PASS: string same after string/octets conversion.");
smg_comms_actions... 396 end if;
smg_comms_actions... 397 end Converter_String_Octets;
smg_comms_actions... 398
smg_comms_actions... 399 procedure Test_Padding is
smg_comms_actions... 400 Msg : Raw_Types.Serpent_Msg := (others => 12);
smg_comms_actions... 401 Old : Raw_Types.Serpent_Msg := Msg;
smg_comms_actions... 402 Pos : Natural := 16;
smg_comms_actions... 403 NewPos : Natural := Pos;
smg_comms_actions... 404 Counter : Interfaces.Unsigned_16;
smg_comms_actions... 405 U16 : Interfaces.Unsigned_16;
smg_comms_actions... 406 O2 : Raw_Types.Octets_2;
smg_comms_actions... 407 Pad : Raw_Types.Octets_8;
smg_comms_actions... 408 Pass : Boolean;
smg_comms_actions... 409 begin
smg_comms_actions... 410 -- get random counter
smg_comms_actions... 411 RNG.Get_Octets( O2 );
smg_comms_actions... 412 Counter := Raw_Types.Cast( O2 );
smg_comms_actions... 413
smg_comms_actions... 414 -- test with random padding
smg_comms_actions... 415 Pad := RNG_PAD;
smg_comms_actions... 416 Write_End( Msg, NewPos, Counter, Pad );
smg_comms_actions... 417 -- check NewPos and counter
smg_comms_actions... 418 Pass := True;
smg_comms_actions... 419 if NewPos /= Msg'Last + 1 then
smg_comms_actions... 420 Put_Line("FAIL: incorrect Pos value after Write_End with rng.");
smg_comms_actions... 421 Pass := False;
smg_comms_actions... 422 end if;
smg_comms_actions... 423 Read_U16(Msg, Pos, U16);
smg_comms_actions... 424 if U16 /= Counter then
smg_comms_actions... 425 Put_Line("FAIL: incorrect Counter by Write_End with rng.");
smg_comms_actions... 426 Pass := False;
smg_comms_actions... 427 end if;
smg_comms_actions... 428 -- check that the padding is at least different...
smg_comms_actions... 429 if Msg(Pos..Msg'Last) = Old(Pos..Old'Last) or
smg_comms_actions... 430 Msg(Pos..Pos+Pad'Length-1) = Pad then
smg_comms_actions... 431 Put_Line("FAIL: no padding written by Write_End with rng.");
smg_comms_actions... 432 Pass := False;
smg_comms_actions... 433 end if;
smg_comms_actions... 434 if Pass then
smg_comms_actions... 435 Put_Line("PASS: Write_End with rng.");
smg_comms_actions... 436 end if;
smg_comms_actions... 437
smg_comms_actions... 438 -- prepare for the next test
smg_comms_actions... 439 Pass := True;
smg_comms_actions... 440 Pos := Pos - 2;
smg_comms_actions... 441 NewPos := Pos;
smg_comms_actions... 442 Msg := Old;
smg_comms_actions... 443
smg_comms_actions... 444 -- get random padding
smg_comms_actions... 445 RNG.Get_Octets( Pad );
smg_comms_actions... 446
smg_comms_actions... 447 -- write with fixed padding and check
smg_comms_actions... 448 Write_End( Msg, NewPos, Counter, Pad );
smg_comms_actions... 449 Pass := True;
smg_comms_actions... 450
smg_comms_actions... 451 if NewPos = Msg'Last + 1 then
smg_comms_actions... 452 -- check counter + padding
smg_comms_actions... 453 Read_U16( Msg, Pos, U16 );
smg_comms_actions... 454 if U16 /= Counter then
smg_comms_actions... 455 Put_Line("FAIL: Counter was not written by Write_End.");
smg_comms_actions... 456 Pass := False;
smg_comms_actions... 457 end if;
smg_comms_actions... 458 for I in Pos..Msg'Last loop
smg_comms_actions... 459 if Msg( I ) /= Pad( Pad'First + (I - Pos) mod Pad'Length ) then
smg_comms_actions... 460 Put_Line("FAIL: Msg(" & Natural'Image(I) & ")=" &
smg_comms_actions... 461 Unsigned_8'Image(Msg(I)) & " /= Pad(" &
smg_comms_actions... 462 Natural'Image(Pad'First+(I-Pos) mod Pad'Length) &
smg_comms_actions... 463 ") which is " &
smg_comms_actions... 464 Unsigned_8'Image(Pad(Pad'First+(I-Pos) mod Pad'Length)));
smg_comms_actions... 465 Pass := False;
smg_comms_actions... 466 end if;
smg_comms_actions... 467 end loop;
smg_comms_actions... 468 else
smg_comms_actions... 469 Put_Line("FAIL: Pos is wrong after call to Write_End.");
smg_comms_actions... 470 Pass := False;
smg_comms_actions... 471 end if;
smg_comms_actions... 472 if Pass then
smg_comms_actions... 473 Put_Line("PASS: test for Write_End with fixed padding.");
smg_comms_actions... 474 end if;
smg_comms_actions... 475 end Test_Padding;
smg_comms_actions... 476
smg_comms_actions... 477 end Messages.Test_Serializing;
smg_comms_actions... 478