SpaceOS wire protocol codecs for host-guest communication
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_error_roundtrip () =
12 let err =
13 Error_payload.v Unknown_type ~offending_type:0xFF ~offending_apid:0x42
14 ~offending_pay_len:248
15 in
16 let encoded = encode Error_payload.codec err in
17 Alcotest.(check int) "error size" 8 (String.length encoded);
18 let decoded = decode Error_payload.codec encoded in
19 Alcotest.(check bool) "roundtrip" true (Error_payload.equal err decoded)
20
21let test_error_layout () =
22 let err =
23 Error_payload.v Host_busy ~offending_type:0x07 ~offending_apid:0x100
24 ~offending_pay_len:0x1234
25 in
26 let encoded = encode Error_payload.codec err in
27 Alcotest.(check int) "error_code" 0x05 (Char.code encoded.[0]);
28 Alcotest.(check int) "offending_type" 0x07 (Char.code encoded.[1]);
29 (* apid 0x0100 big-endian *)
30 Alcotest.(check int) "apid high" 0x01 (Char.code encoded.[2]);
31 Alcotest.(check int) "apid low" 0x00 (Char.code encoded.[3]);
32 (* pay_len 0x1234 big-endian at offset 4-5 *)
33 Alcotest.(check int) "pay_len high" 0x12 (Char.code encoded.[4]);
34 Alcotest.(check int) "pay_len low" 0x34 (Char.code encoded.[5]);
35 (* reserved = 0 at offset 6-7 *)
36 Alcotest.(check int) "reserved high" 0x00 (Char.code encoded.[6]);
37 Alcotest.(check int) "reserved low" 0x00 (Char.code encoded.[7])
38
39let suite =
40 ( "error_payload",
41 [
42 Alcotest.test_case "roundtrip" `Quick test_error_roundtrip;
43 Alcotest.test_case "layout" `Quick test_error_layout;
44 ] )