SpaceOS wire protocol codecs for host-guest communication
at main 48 lines 2.0 kB view raw
1open Space_wire 2 3let encode codec v = 4 let ws = Wire.Codec.wire_size codec in 5 let buf = Bytes.create ws in 6 Wire.Codec.encode codec v buf 0; 7 Bytes.unsafe_to_string buf 8 9let decode codec s = Wire.Codec.decode codec (Bytes.of_string s) 0 10 11let test_param_entry_roundtrip () = 12 let p = Param_entry.v ~param_id:42 ~generation:7 "hello" in 13 let encoded = encode Param_entry.codec p in 14 Alcotest.(check int) "param size" 252 (String.length encoded); 15 let decoded = decode Param_entry.codec encoded in 16 Alcotest.(check bool) "roundtrip" true (Param_entry.equal p decoded) 17 18let test_param_entry_crc () = 19 let p = Param_entry.v ~param_id:42 ~generation:1 "value" in 20 Alcotest.(check bool) "crc ok" true (Param_entry.check_crc p); 21 let bad = { p with Param_entry.param_id = 999 } in 22 Alcotest.(check bool) "crc bad" false (Param_entry.check_crc bad) 23 24let test_param_entry_layout () = 25 let p = Param_entry.v ~param_id:0x01020304 ~generation:3 "AB" in 26 let encoded = encode Param_entry.codec p in 27 (* param_id = 0x01020304 big-endian at offset 0-3 *) 28 Alcotest.(check int) "pid[0]" 0x01 (Char.code encoded.[0]); 29 Alcotest.(check int) "pid[1]" 0x02 (Char.code encoded.[1]); 30 Alcotest.(check int) "pid[2]" 0x03 (Char.code encoded.[2]); 31 Alcotest.(check int) "pid[3]" 0x04 (Char.code encoded.[3]); 32 (* len=2 at offset 4-5 *) 33 Alcotest.(check int) "len high" 0x00 (Char.code encoded.[4]); 34 Alcotest.(check int) "len low" 0x02 (Char.code encoded.[5]); 35 (* generation=3 at offset 6-7 *) 36 Alcotest.(check int) "gen high" 0x00 (Char.code encoded.[6]); 37 Alcotest.(check int) "gen low" 0x03 (Char.code encoded.[7]); 38 (* value starts at offset 8 *) 39 Alcotest.(check char) "value[0]" 'A' encoded.[8]; 40 Alcotest.(check char) "value[1]" 'B' encoded.[9] 41 42let suite = 43 ( "param_entry", 44 [ 45 Alcotest.test_case "roundtrip" `Quick test_param_entry_roundtrip; 46 Alcotest.test_case "crc" `Quick test_param_entry_crc; 47 Alcotest.test_case "layout" `Quick test_param_entry_layout; 48 ] )