SpaceOS wire protocol codecs for host-guest communication
at main 71 lines 2.7 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_frame_roundtrip () = 12 let frame = Msg.v TM ~apid:0x42 "hello world" in 13 let encoded = encode Msg.codec frame in 14 Alcotest.(check int) "frame size" 256 (String.length encoded); 15 let decoded = decode Msg.codec encoded in 16 Alcotest.(check bool) "roundtrip" true (Msg.equal frame decoded) 17 18let test_frame_header_layout () = 19 let frame = Msg.v TC ~apid:0x123 "test" in 20 let encoded = encode Msg.codec frame in 21 (* version=0x01 at offset 0 *) 22 Alcotest.(check int) "version" 0x01 (Char.code encoded.[0]); 23 (* type=0x01 (TC) at offset 1 *) 24 Alcotest.(check int) "type" 0x01 (Char.code encoded.[1]); 25 (* apid=0x0123 big-endian at offset 2-3 *) 26 Alcotest.(check int) "apid high" 0x01 (Char.code encoded.[2]); 27 Alcotest.(check int) "apid low" 0x23 (Char.code encoded.[3]); 28 (* payload_length=4 at offset 4-5 *) 29 Alcotest.(check int) "pay_len high" 0x00 (Char.code encoded.[4]); 30 Alcotest.(check int) "pay_len low" 0x04 (Char.code encoded.[5]); 31 (* reserved=0 at offset 6-7 *) 32 Alcotest.(check int) "reserved high" 0x00 (Char.code encoded.[6]); 33 Alcotest.(check int) "reserved low" 0x00 (Char.code encoded.[7]); 34 (* payload starts at offset 8 *) 35 Alcotest.(check char) "payload[0]" 't' encoded.[8] 36 37let test_frame_all_types () = 38 let types = 39 Msg.[ TM; TC; EVR; PRM_GET; PRM_SET; PRM_RSP; DP; HEALTH; LOG; ERROR ] 40 in 41 List.iter 42 (fun typ -> 43 let frame = Msg.v typ ~apid:1 "" in 44 let encoded = encode Msg.codec frame in 45 let decoded = decode Msg.codec encoded in 46 let typ_int = Msg.kind_to_int typ in 47 Alcotest.(check int) 48 (Fmt.str "type %a" Msg.pp_kind typ) 49 typ_int decoded.msg_type) 50 types 51 52let test_frame_payload_bytes () = 53 let frame = Msg.v TM ~apid:1 "abc" in 54 let encoded = encode Msg.codec frame in 55 let decoded = decode Msg.codec encoded in 56 Alcotest.(check string) "payload_bytes" "abc" (Msg.payload_bytes decoded) 57 58let test_frame_max_payload () = 59 let big = String.make 300 'x' in 60 let frame = Msg.v TM ~apid:1 big in 61 Alcotest.(check int) "payload_length capped" 248 frame.payload_length 62 63let suite = 64 ( "msg", 65 [ 66 Alcotest.test_case "roundtrip" `Quick test_frame_roundtrip; 67 Alcotest.test_case "header layout" `Quick test_frame_header_layout; 68 Alcotest.test_case "all types" `Quick test_frame_all_types; 69 Alcotest.test_case "payload_bytes" `Quick test_frame_payload_bytes; 70 Alcotest.test_case "max payload" `Quick test_frame_max_payload; 71 ] )