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_superblock_roundtrip () =
12 let sb =
13 Superblock.v ~tenant_id:1 ~total_blocks:1024 ~dp_start:33 ~dp_size:991
14 ~epoch:1000000L ~uuid:(String.make 16 '\xAB')
15 in
16 let encoded = encode Superblock.codec sb in
17 Alcotest.(check int) "superblock size" 48 (String.length encoded);
18 let decoded = decode Superblock.codec encoded in
19 Alcotest.(check bool) "roundtrip" true (Superblock.equal sb decoded)
20
21let test_superblock_magic () =
22 let sb =
23 Superblock.v ~tenant_id:1 ~total_blocks:1024 ~dp_start:33 ~dp_size:991
24 ~epoch:0L ~uuid:(String.make 16 '\x00')
25 in
26 Alcotest.(check bool) "magic ok" true (Superblock.check_magic sb);
27 let bad = { sb with magic = 0 } in
28 Alcotest.(check bool) "magic bad" false (Superblock.check_magic bad)
29
30let test_superblock_crc () =
31 let sb =
32 Superblock.v ~tenant_id:1 ~total_blocks:1024 ~dp_start:33 ~dp_size:991
33 ~epoch:0L ~uuid:(String.make 16 '\x00')
34 in
35 Alcotest.(check bool) "crc ok" true (Superblock.check_crc sb);
36 let bad = { sb with tenant_id = 999 } in
37 Alcotest.(check bool) "crc bad" false (Superblock.check_crc bad)
38
39let test_superblock_layout () =
40 let sb =
41 Superblock.v ~tenant_id:1 ~total_blocks:1024 ~dp_start:33 ~dp_size:991
42 ~epoch:0L ~uuid:(String.make 16 '\x00')
43 in
44 let encoded = encode Superblock.codec sb in
45 (* magic = 0x53504F53 big-endian *)
46 Alcotest.(check int) "magic[0]" 0x53 (Char.code encoded.[0]);
47 Alcotest.(check int) "magic[1]" 0x50 (Char.code encoded.[1]);
48 Alcotest.(check int) "magic[2]" 0x4F (Char.code encoded.[2]);
49 Alcotest.(check int) "magic[3]" 0x53 (Char.code encoded.[3]);
50 (* format_version = 0x01 at offset 4 *)
51 Alcotest.(check int) "version" 0x01 (Char.code encoded.[4])
52
53let suite =
54 ( "superblock",
55 [
56 Alcotest.test_case "roundtrip" `Quick test_superblock_roundtrip;
57 Alcotest.test_case "magic" `Quick test_superblock_magic;
58 Alcotest.test_case "crc" `Quick test_superblock_crc;
59 Alcotest.test_case "layout" `Quick test_superblock_layout;
60 ] )