docs: Move release notes to their own section.
[nbdkit/ericb.git] / tests / test_ocaml_plugin.ml
blob3cf8fd90e2d0fa3e9e8b87870a7e2bb725d8a859
1 let sector_size = 512
2 let nr_sectors = 2048
4 let disk = Bytes.make (nr_sectors*sector_size) '\000' (* disk image *)
5 let sparse = Bytes.make nr_sectors '\000' (* sparseness bitmap *)
7 let test_load () =
8 NBDKit.debug "test ocaml plugin loaded"
10 let test_unload () =
11 NBDKit.debug "test ocaml plugin unloaded"
13 let params = ref []
15 let test_config k v =
16 params := (k, v) :: !params
18 let test_config_complete () =
19 let params = List.rev !params in
20 assert (params = [ "a", "1"; "b", "2"; "c", "3" ])
22 let test_open readonly =
23 NBDKit.debug "test ocaml plugin handle opened readonly=%b" readonly;
26 let test_close () =
29 let test_get_size () =
30 Int64.of_int (Bytes.length disk)
32 let test_pread () count offset _ =
33 let count = Int32.to_int count in
34 let buf = Bytes.create count in
35 Bytes.blit disk (Int64.to_int offset) buf 0 count;
36 Bytes.unsafe_to_string buf
38 let set_non_sparse offset len =
39 Bytes.fill sparse (offset/sector_size) ((len-1)/sector_size) '\001'
41 let test_pwrite () buf offset _ =
42 let len = String.length buf in
43 let offset = Int64.to_int offset in
44 String.blit buf 0 disk offset len;
45 set_non_sparse offset len
47 let test_extents () count offset _ =
48 let extents = Array.init nr_sectors (
49 fun sector ->
50 { NBDKit.offset = Int64.of_int (sector*sector_size);
51 length = Int64.of_int sector_size;
52 is_hole = true; is_zero = false }
53 ) in
54 Bytes.iteri (
55 fun i c ->
56 if c = '\001' then (* not sparse *)
57 extents.(i) <- { extents.(i) with is_hole = false }
58 ) sparse;
59 Array.to_list extents
61 let plugin = {
62 NBDKit.default_callbacks with
63 NBDKit.name = "testocaml";
64 version = "1.0";
66 load = Some test_load;
67 unload = Some test_unload;
68 config = Some test_config;
69 config_complete = Some test_config_complete;
71 open_connection = Some test_open;
72 close = Some test_close;
73 get_size = Some test_get_size;
74 pread = Some test_pread;
75 pwrite = Some test_pwrite;
77 extents = Some test_extents;
78 thread_model = Some (fun () -> NBDKit.THREAD_MODEL_SERIALIZE_CONNECTIONS);
81 let () = NBDKit.register_plugin plugin